summaryrefslogtreecommitdiffstats
path: root/CAN-binder/low-can-binding
diff options
context:
space:
mode:
Diffstat (limited to 'CAN-binder/low-can-binding')
-rw-r--r--CAN-binder/low-can-binding/CMakeLists.txt66
-rw-r--r--CAN-binder/low-can-binding/binding/configuration-generated.cpp486
-rw-r--r--CAN-binder/low-can-binding/binding/configuration.cpp96
-rw-r--r--CAN-binder/low-can-binding/binding/configuration.hpp109
-rw-r--r--CAN-binder/low-can-binding/binding/low-can-cb.cpp306
-rw-r--r--CAN-binder/low-can-binding/binding/low-can-hat.cpp83
-rw-r--r--CAN-binder/low-can-binding/binding/low-can-hat.hpp38
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.cpp329
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.hpp91
-rw-r--r--CAN-binder/low-can-binding/can/can-command.hpp55
-rw-r--r--CAN-binder/low-can-binding/can/can-decoder.cpp244
-rw-r--r--CAN-binder/low-can-binding/can/can-decoder.hpp49
-rw-r--r--CAN-binder/low-can-binding/can/can-message-definition.cpp92
-rw-r--r--CAN-binder/low-can-binding/can/can-message-definition.hpp68
-rw-r--r--CAN-binder/low-can-binding/can/can-message-set.cpp58
-rw-r--r--CAN-binder/low-can-binding/can/can-message-set.hpp50
-rw-r--r--CAN-binder/low-can-binding/can/can-message.cpp365
-rw-r--r--CAN-binder/low-can-binding/can/can-message.hpp79
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.cpp262
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.hpp160
-rw-r--r--CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp172
-rw-r--r--CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp116
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp584
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp94
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp113
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-message.hpp84
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.cpp67
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.hpp44
-rw-r--r--CAN-binder/low-can-binding/utils/openxc-utils.cpp284
-rw-r--r--CAN-binder/low-can-binding/utils/openxc-utils.hpp42
-rw-r--r--CAN-binder/low-can-binding/utils/signals.cpp77
-rw-r--r--CAN-binder/low-can-binding/utils/signals.hpp81
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan-bcm.cpp96
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan-bcm.hpp52
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan-raw.cpp88
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan-raw.hpp35
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan.cpp93
-rw-r--r--CAN-binder/low-can-binding/utils/socketcan.hpp70
-rw-r--r--CAN-binder/low-can-binding/utils/timer.cpp121
-rw-r--r--CAN-binder/low-can-binding/utils/timer.hpp57
40 files changed, 5456 insertions, 0 deletions
diff --git a/CAN-binder/low-can-binding/CMakeLists.txt b/CAN-binder/low-can-binding/CMakeLists.txt
new file mode 100644
index 00000000..276097fc
--- /dev/null
+++ b/CAN-binder/low-can-binding/CMakeLists.txt
@@ -0,0 +1,66 @@
+###########################################################################
+# Copyright 2015, 2016, 2017 IoT.bzh
+#
+# author: Fulup Ar Foll <fulup@iot.bzh>
+# contrib: Romain Forlot <romain.forlot@iot.bzh>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+###########################################################################
+
+# Add target to project dependency list
+PROJECT_TARGET_ADD(low-can)
+
+ # Define project Targets
+ add_library(${TARGET_NAME} MODULE
+ binding/${TARGET_NAME}-hat.cpp
+ binding/${TARGET_NAME}-cb.cpp
+ binding/configuration.cpp
+ binding/configuration-generated.cpp
+ can/can-bus.cpp
+ can/can-message-set.cpp
+ can/can-message-definition.cpp
+ can/can-message.cpp
+ can/can-signals.cpp
+ can/can-decoder.cpp
+ diagnostic/diagnostic-message.cpp
+ diagnostic/diagnostic-manager.cpp
+ diagnostic/active-diagnostic-request.cpp
+ utils/signals.cpp
+ utils/openxc-utils.cpp
+ utils/timer.cpp
+ utils/socketcan.cpp
+ utils/socketcan-raw.cpp
+ utils/socketcan-bcm.cpp
+ utils/config-parser.cpp)
+
+ # Binder exposes a unique public entry point
+ SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
+ PREFIX "afs-"
+ LABELS "BINDING"
+ LINK_FLAGS ${BINDINGS_LINK_FLAG}
+ OUTPUT_NAME ${TARGET_NAME}
+ )
+
+ # Library dependencies (include updates automatically)
+ TARGET_LINK_LIBRARIES(${TARGET_NAME}
+ ini-config
+ openxc-message-format
+ uds-c
+ isotp-c
+ bitfield-c
+ ${link_libraries})
+
+ # installation directory
+ INSTALL(TARGETS ${TARGET_NAME}
+ LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR})
+
diff --git a/CAN-binder/low-can-binding/binding/configuration-generated.cpp b/CAN-binder/low-can-binding/binding/configuration-generated.cpp
new file mode 100644
index 00000000..bce469e5
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/configuration-generated.cpp
@@ -0,0 +1,486 @@
+#include "configuration.hpp"
+#include "../can/can-decoder.hpp"
+
+configuration_t::configuration_t()
+ : can_bus_manager_{utils::config_parser_t{"/etc/dev-mapping.conf"}}
+ , can_message_set_{
+ {std::make_shared<can_message_set_t>(can_message_set_t{0,"example",
+ { // beginning can_message_definition_ vector
+ {std::make_shared<can_message_definition_t>(can_message_definition_t{"can0",0x3D9,can_message_format_t::STANDARD,frequency_clock_t(5.00000f),true,
+ { // beginning can_signals vector
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "engine.speed",
+ 16,
+ 16,
+ 0.250000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ nullptr,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "fuel.level.low",
+ 55,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "fuel.level",
+ 8,
+ 8,
+ 0.392157f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ nullptr,
+ nullptr,
+ false
+ })}
+ } // end can_signals vector
+ })} // end can_message_definition entry
+, {std::make_shared<can_message_definition_t>(can_message_definition_t{"can0",0x3E9,can_message_format_t::STANDARD,frequency_clock_t(5.00000f),true,
+ { // beginning can_signals vector
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "vehicle.average.speed",
+ 0,
+ 15,
+ 0.0156250f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ nullptr,
+ nullptr,
+ false
+ })}
+ } // end can_signals vector
+ })} // end can_message_definition entry
+, {std::make_shared<can_message_definition_t>(can_message_definition_t{"can0",0x4D1,can_message_format_t::STANDARD,frequency_clock_t(5.00000f),true,
+ { // beginning can_signals vector
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "engine.oil.temp",
+ 16,
+ 8,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ nullptr,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "engine.oil.temp.high",
+ 7,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })}
+ } // end can_signals vector
+ })} // end can_message_definition entry
+, {std::make_shared<can_message_definition_t>(can_message_definition_t{"can0",0x620,can_message_format_t::STANDARD,frequency_clock_t(5.00000f),true,
+ { // beginning can_signals vector
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "doors.boot.open",
+ 56,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "doors.front_left.open",
+ 46,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "doors.front_right.open",
+ 47,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "doors.rear_left.open",
+ 54,
+ 1,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })},
+ {std::make_shared<can_signal_t> (can_signal_t{
+ "doors.rear_right.open",
+ 53,
+ 4,
+ 1.00000f,
+ 0.00000,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ })}
+ } // end can_signals vector
+ })} // end can_message_definition entry
+ }, // end can_message_definition vector
+ { // beginning diagnostic_messages_ vector
+ {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 4,
+ "engine.load",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 5,
+ "engine.coolant.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 10,
+ "fuel.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 11,
+ "intake.manifold.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 12,
+ "engine.speed",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 13,
+ "vehicle.speed",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 15,
+ "intake.air.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 16,
+ "mass.airflow",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 17,
+ "throttle.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 31,
+ "running.time",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 45,
+ "EGR.error",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 0.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 47,
+ "fuel.level",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 51,
+ "barometric.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 70,
+ "ambient.air.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 76,
+ "commanded.throttle.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 82,
+ "ethanol.fuel.percentage",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 90,
+ "accelerator.pedal.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 91,
+ "hybrid.battery-pack.remaining.life",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 92,
+ "engine.oil.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 94,
+ "engine.fuel.rate",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+, {std::make_shared<diagnostic_message_t>(diagnostic_message_t{
+ 99,
+ "engine.torque",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ })}
+ } // end diagnostic_messages_ vector
+ })} // end can_message_set entry
+ } // end can_message_set vector
+{
+ for(auto& cms: can_message_set_)
+ {
+ std::vector<std::shared_ptr<can_message_definition_t> >& can_messages_definition = cms->get_can_message_definition();
+ for(auto& cmd : can_messages_definition)
+ {
+ cmd->set_parent(cms.get());
+ std::vector<std::shared_ptr<can_signal_t> >& can_signals = cmd->get_can_signals();
+ for(auto& sig: can_signals)
+ {
+ sig->set_parent(cmd.get());
+ }
+ }
+
+ std::vector<std::shared_ptr<diagnostic_message_t> >& diagnostic_messages = cms->get_diagnostic_messages();
+ for(auto& dm : diagnostic_messages)
+ {
+ dm->set_parent(cms.get());
+ }
+ }
+}
+
+const std::string configuration_t::get_diagnostic_bus() const
+{
+ return "can0";
+}
+
+
diff --git a/CAN-binder/low-can-binding/binding/configuration.cpp b/CAN-binder/low-can-binding/binding/configuration.cpp
new file mode 100644
index 00000000..623ba785
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/configuration.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "configuration.hpp"
+
+#include "../utils/signals.hpp"
+#include "../utils/openxc-utils.hpp"
+
+/// @brief Return singleton instance of configuration object.
+configuration_t& configuration_t::instance()
+{
+ static configuration_t config;
+ return config;
+}
+
+can_bus_t& configuration_t::get_can_bus_manager()
+{
+ return can_bus_manager_;
+}
+
+diagnostic_manager_t& configuration_t::get_diagnostic_manager()
+{
+ return diagnostic_manager_;
+}
+
+uint8_t configuration_t::get_active_message_set() const
+{
+ return active_message_set_;
+}
+
+std::vector<std::shared_ptr<can_message_set_t> > configuration_t::get_can_message_set()
+{
+ return can_message_set_;
+}
+
+std::vector<std::shared_ptr<can_signal_t> > configuration_t::get_all_can_signals()
+{
+ return can_message_set_[active_message_set_]->get_all_can_signals();
+}
+
+std::vector<std::shared_ptr<diagnostic_message_t> >& configuration_t::get_diagnostic_messages()
+{
+ return can_message_set_[active_message_set_]->get_diagnostic_messages();
+}
+
+std::vector<std::shared_ptr<can_message_definition_t> >& configuration_t::get_can_message_definition()
+{
+ return can_message_set_[active_message_set_]->get_can_message_definition();
+}
+
+uint32_t configuration_t::get_signal_id(diagnostic_message_t& sig) const
+{
+ return sig.get_pid();
+}
+
+uint32_t configuration_t::get_signal_id(can_signal_t& sig) const
+{
+ return sig.get_message()->get_id();
+}
+
+void configuration_t::set_active_message_set(uint8_t id)
+{
+ active_message_set_ = id;
+}
+
+
+std::shared_ptr<diagnostic_message_t> configuration_t::get_diagnostic_message(std::string message_name) const
+{
+ struct utils::signals_found found;
+ found = utils::signals_manager_t::instance().find_signals(build_DynamicField(message_name));
+ if(! found.diagnostic_messages.empty())
+ return found.diagnostic_messages.front();
+ return nullptr;
+}
+
+DiagnosticRequest* configuration_t::get_request_from_diagnostic_message(std::string message_name) const
+{
+ std::shared_ptr<diagnostic_message_t> diag_msg = get_diagnostic_message(message_name);
+ if( diag_msg != nullptr && diag_msg->get_supported())
+ return new DiagnosticRequest(diag_msg->build_diagnostic_request());
+ return nullptr;
+}
diff --git a/CAN-binder/low-can-binding/binding/configuration.hpp b/CAN-binder/low-can-binding/binding/configuration.hpp
new file mode 100644
index 00000000..3ab88ee5
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/configuration.hpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <map>
+#include <vector>
+#include <string>
+#include <memory>
+
+#include "../can/can-bus.hpp"
+#include "../can/can-message-set.hpp"
+#include "../can/can-signals.hpp"
+#include "../diagnostic/diagnostic-manager.hpp"
+
+#include "low-can-hat.hpp"
+
+///
+/// @brief Class representing a configuration attached to the binding.
+///
+/// It regroups all needed objects instance from other class
+/// that will be used along the binding life. It gets a global vision
+/// on which signals are implemented for that binding.
+/// Here, it is only the definition of the class with predefined accessors
+/// methods used in the binding.
+///
+/// It will be the reference point to needed objects.
+///
+class configuration_t
+{
+ private:
+ can_bus_t can_bus_manager_; ///< instanciate the CAN bus manager. It's the one in charge of initialize the CAN bus devices.
+ diagnostic_manager_t diagnostic_manager_; ///< Diagnostic manager use to manage diagnostic message communication.
+ uint8_t active_message_set_ = 0; ///< Which is the active message set ? Default to 0.
+
+ std::vector<std::shared_ptr<can_message_set_t> > can_message_set_; ///< Vector holding all message set from JSON signals description file
+ //std::vector<std::vector<can_message_definition_t>> can_message_definition_; ///< Vector of vector holding all can message definition from JSON signals description file. This describe a CAN message. First vector map to message set
+ //std::vector<std::vector<can_signal_t>> can_signals_; ///< Vector of vector holding all can signasl from JSON signals description file. A CAN signal is a part of a CAN message. First vector map to message set
+ //std::vector<std::vector<std::shared_ptr<diagnostic_message_t> > > diagnostic_messages_; ///< Vector of vector holding all diagnostics messages from JSON signals description file. First vector map to message set
+
+ configuration_t(); ///< Private constructor with implementation generated by the AGL generator.
+
+ public:
+ static configuration_t& instance();
+
+ can_bus_t& get_can_bus_manager();
+
+ const std::string get_diagnostic_bus() const;
+
+ diagnostic_manager_t& get_diagnostic_manager() ;
+
+ uint8_t get_active_message_set() const;
+
+ std::vector<std::shared_ptr<can_message_set_t> > get_can_message_set();
+
+ std::vector<std::shared_ptr<can_signal_t> > get_all_can_signals();
+
+ std::vector<std::shared_ptr<diagnostic_message_t> >& get_diagnostic_messages();
+
+ const std::vector<std::string>& get_signals_prefix() const;
+
+ std::vector<std::shared_ptr<can_message_definition_t> >& get_can_message_definition();
+
+ uint32_t get_signal_id(diagnostic_message_t& sig) const;
+
+ uint32_t get_signal_id(can_signal_t& sig) const;
+
+ void set_active_message_set(uint8_t id);
+
+ std::shared_ptr<diagnostic_message_t> get_diagnostic_message(std::string message_name) const;
+ DiagnosticRequest* get_request_from_diagnostic_message(std::string message_name) const;
+/*
+ /// TODO: implement this function as method into can_bus class
+ /// @brief Pre initialize actions made before CAN bus initialization
+ /// @param[in] bus A CanBus struct defining the bus's metadata
+ /// @param[in] writable Configure the controller in a writable mode. If false, it will be configured as "listen only" and will not allow writes or even CAN ACKs.
+ /// @param[in] buses An array of all CAN buses.
+ void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
+ /// TODO: implement this function as method into can_bus class
+ /// @brief Post-initialize actions made after CAN bus initialization
+ /// @param[in] bus A CanBus struct defining the bus's metadata
+ /// @param[in] writable Configure the controller in a writable mode. If false, it will be configured as "listen only" and will not allow writes or even CAN ACKs.
+ /// @param[in] buses An array of all CAN buses.
+ void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
+ /// TODO: implement this function as method into can_bus class
+ /// @brief Check if the device is connected to an active CAN bus, i.e. it's received a message in the recent past.
+ /// @return true if a message was received on the CAN bus within CAN_ACTIVE_TIMEOUT_S seconds.
+ void logBusStatistics(can_bus_dev_t* buses, const int busCount);
+ /// TODO: implement this function as method into can_bus class
+ /// @brief Log transfer statistics about all active CAN buses to the debug log.
+ /// @param[in] buses An array of active CAN buses.
+ bool isBusActive(can_bus_dev_t* bus);
+ */
+};
+
diff --git a/CAN-binder/low-can-binding/binding/low-can-cb.cpp b/CAN-binder/low-can-binding/binding/low-can-cb.cpp
new file mode 100644
index 00000000..4faae852
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/low-can-cb.cpp
@@ -0,0 +1,306 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loic Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "low-can-hat.hpp"
+
+#include <map>
+#include <queue>
+#include <mutex>
+#include <vector>
+#include <thread>
+#include <json-c/json.h>
+#include <systemd/sd-event.h>
+
+#include "openxc.pb.h"
+#include "configuration.hpp"
+#include "../can/can-bus.hpp"
+#include "../can/can-signals.hpp"
+#include "../can/can-message.hpp"
+#include "../utils/timer.hpp"
+#include "../utils/signals.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+#include "../utils/openxc-utils.hpp"
+
+extern "C"
+{
+ #include <afb/afb-service-itf.h>
+};
+
+void on_no_clients(std::string message)
+{
+ DiagnosticRequest* diag_req = configuration_t::instance().get_request_from_diagnostic_message(message);
+ if(diag_req != nullptr)
+ {
+ active_diagnostic_request_t* adr = configuration_t::instance().get_diagnostic_manager().find_recurring_request(diag_req);
+ if( adr != nullptr)
+ configuration_t::instance().get_diagnostic_manager().cleanup_request(adr, true);
+ }
+}
+
+static void push_n_notify(const can_message_t cm)
+{
+ can_bus_t& cbm = configuration_t::instance().get_can_bus_manager();
+ std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
+ { cbm.push_new_can_message(cm); }
+ cbm.get_new_can_message_cv().notify_one();
+}
+
+int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata)
+{
+ can_message_t cm;
+ can_signal_t* sig;
+ diagnostic_manager_t& diag_m = configuration_t::instance().get_diagnostic_manager();
+
+ if(userdata != nullptr)
+ {
+ sig = (can_signal_t*)userdata;
+ utils::socketcan_bcm_t s = sig->get_socket();
+ s >> cm;
+ }
+ else
+ {
+ utils::socketcan_bcm_t s = diag_m.get_socket();
+ s >> cm;
+ }
+
+ push_n_notify(cm);
+
+ /* check if error or hangup */
+ if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
+ {
+ sd_event_source_unref(s);
+ if(userdata != nullptr)
+ {
+ sig->get_socket().close();
+ sig->create_rx_filter();
+ NOTICE(binder_interface, "%s: Recreated RX_SETUP BCM job for signal: %s", __FUNCTION__, sig->get_name().c_str());
+ }
+ else
+ {
+ diag_m.get_socket().close();
+ diag_m.cleanup_active_requests(true);
+ ERROR(binder_interface, "%s: Error on diagnostic manager socket, cancelling active requests.", __FUNCTION__);
+ }
+ return -1;
+ }
+
+ return 0;
+}
+
+
+///******************************************************************************
+///
+/// Subscription and unsubscription
+///
+///*******************************************************************************/
+
+static int make_subscription_unsubscription(struct afb_req request, const std::string& sig_name, std::map<std::string, struct afb_event>& s, bool subscribe)
+{
+ /* Make the subscription or unsubscription to the event */
+ if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[sig_name])) < 0)
+ {
+ ERROR(binder_interface, "%s: Operation goes wrong for signal: %s", __FUNCTION__, sig_name.c_str());
+ return 0;
+ }
+ return 1;
+}
+
+static int create_event_handle(const std::string& sig_name, std::map<std::string, struct afb_event>& s)
+{
+ s[sig_name] = afb_daemon_make_event(binder_interface->daemon, sig_name.c_str());
+ if (!afb_event_is_valid(s[sig_name]))
+ {
+ ERROR(binder_interface, "%s: Can't create an event for %s, something goes wrong.", __FUNCTION__, sig_name.c_str());
+ return 0;
+ }
+ return 1;
+}
+
+/// @brief Will determine if it is needed or not to create the event handle and checks it to be sure that
+/// we got a valid afb_event to get subscribe or unsubscribe. Then launch the subscription or unsubscription
+/// against the application framework using that event handle.
+static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig)
+{
+ int ret;
+
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+ if (s.find(sig) != s.end())
+ {
+ if (!afb_event_is_valid(s[sig]) && !subscribe)
+ {
+ NOTICE(binder_interface, "%s: Event isn't valid, no need to unsubscribed.", __FUNCTION__);
+ ret = -1;
+ }
+ else
+ {
+ // Event it isn't valid annymore, recreate it
+ ret = create_event_handle(sig, s);
+ }
+ }
+ else
+ {
+ /* Event doesn't exist , so let's create it */
+ struct afb_event empty_event = {nullptr, nullptr};
+ s[sig] = empty_event;
+ ret = create_event_handle(sig, s);
+ }
+
+ // Check whether or not the event handler has been correctly created and
+ // make the subscription/unsubscription operation is so.
+ if (ret <= 0)
+ return ret;
+ return make_subscription_unsubscription(request, sig, s, subscribe);
+}
+
+///
+/// @brief subscribe to all signals in the vector signals
+///
+/// @param[in] afb_req request : contain original request use to subscribe or unsubscribe
+/// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription
+/// @param[in] signals - struct containing vectors with can_signal_t and diagnostic_messages to subscribe
+///
+/// @return Number of correctly subscribed signal
+///
+static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const struct utils::signals_found& signals)
+{
+ int ret, rets = 0;
+
+ //TODO: Implement way to dynamically call the right function no matter
+ // how much signals types we have.
+ configuration_t& conf = configuration_t::instance();
+
+ for(const auto& sig : signals.diagnostic_messages)
+ {
+ diagnostic_manager_t& diag_m = conf.get_diagnostic_manager();
+ DiagnosticRequest* diag_req = conf.get_request_from_diagnostic_message(sig->get_name());
+
+ // If the requested diagnostic message isn't supported by the car then unsubcribe it
+ // no matter what we want, worse case will be a fail unsubscription but at least we don't
+ // poll a PID for nothing.
+ if(sig->get_supported() && subscribe)
+ {
+ float frequency = sig->get_frequency();
+ diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), (float)frequency);
+ //TODO: Adding callback requesting ignition status: diag_req, sig.c_str(), false, diagnostic_message_t::decode_obd2_response, diagnostic_message_t::check_ignition_status, frequency);
+ }
+ else
+ {
+ diag_m.cleanup_request(
+ diag_m.find_recurring_request(diag_req), true);
+ WARNING(binder_interface, "%s: signal: %s isn't supported. Canceling operation.", __FUNCTION__, sig->get_name().c_str());
+ return -1;
+ }
+
+ ret = subscribe_unsubscribe_signal(request, subscribe, sig->get_name());
+ if(ret <= 0)
+ return ret;
+ rets++;
+ DEBUG(binder_interface, "%s: Signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
+ }
+
+ for(const auto& sig: signals.can_signals)
+ {
+ if(sig->create_rx_filter() <= 0 &&
+ subscribe_unsubscribe_signal(request, subscribe, sig->get_name()) <=0)
+ {
+ return -1;
+ }
+ struct sd_event_source* e_source;
+ sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon), &e_source, sig->get_socket().socket(), EPOLLIN, read_message, sig.get());
+ rets++;
+ DEBUG(binder_interface, "%s: signal: %s subscribed", __FUNCTION__, sig->get_name().c_str());
+ }
+ return rets;
+}
+
+static int process_args(struct afb_req request, const std::vector<std::string>& args, bool subscribe)
+{
+ struct utils::signals_found sf;
+ int ok = 0, total = 0;
+
+ for(const auto& sig: args)
+ {
+ openxc_DynamicField search_key = build_DynamicField(sig);
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+ total = (int)sf.can_signals.size() + (int)sf.diagnostic_messages.size();
+
+ if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
+ NOTICE(binder_interface, "%s: No signal(s) found for %s.", __FUNCTION__, sig.c_str());
+ else
+ ok = subscribe_unsubscribe_signals(request, subscribe, sf);
+ }
+ NOTICE(binder_interface, "%s: Subscribed/unsubscribe correctly to %d/%d signal(s).", __FUNCTION__, ok, total);
+ return ok;
+}
+
+static const std::vector<std::string> parse_args_from_request(struct afb_req request)
+{
+ int i, n;
+ std::vector<std::string> ret;
+ struct json_object *args, *a, *x;
+
+ /* retrieve signals to subscribe */
+ args = afb_req_json(request);
+ if (args == NULL || !json_object_object_get_ex(args, "event", &a))
+ {
+ ret.push_back("*");
+ }
+ else if (json_object_get_type(a) != json_type_array)
+ {
+ ret.push_back(json_object_get_string(a));
+ }
+ else
+ {
+ n = json_object_array_length(a);
+ for (i = 0 ; i < n ; i++)
+ {
+ x = json_object_array_get_idx(a, i);
+ ret.push_back(json_object_get_string(x));
+ }
+ }
+
+ return ret;
+}
+
+void subscribe(struct afb_req request)
+{
+ bool subscribe = true;
+
+ const std::vector<std::string> args = parse_args_from_request(request);
+
+ if (process_args(request, args, subscribe) > 0)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
+}
+
+void unsubscribe(struct afb_req request)
+{
+ std::vector<std::string> args;
+ bool subscribe = false;
+
+ args = parse_args_from_request(request);
+
+ if (process_args(request, args, subscribe) > 0)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/binding/low-can-hat.cpp b/CAN-binder/low-can-binding/binding/low-can-hat.cpp
new file mode 100644
index 00000000..918625f6
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/low-can-hat.cpp
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loic Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "low-can-hat.hpp"
+
+#include <map>
+#include <queue>
+#include <mutex>
+#include <vector>
+#include <json-c/json.h>
+
+#include "configuration.hpp"
+#include "../can/can-bus.hpp"
+
+extern "C"
+{
+ #include <afb/afb-service-itf.h>
+};
+
+// Interface between the daemon and the binding
+const struct afb_binding_interface *binder_interface;
+
+extern "C"
+{
+ static const struct afb_verb_desc_v1 verbs[]=
+ {
+ { .name= "subscribe", .session= AFB_SESSION_NONE, .callback= subscribe, .info= "subscribe to notification of CAN bus messages." },
+ { .name= "unsubscribe", .session= AFB_SESSION_NONE, .callback= unsubscribe, .info= "unsubscribe a previous subscription." }
+ };
+
+ static const struct afb_binding binding_desc {
+ AFB_BINDING_VERSION_1,
+ {
+ "Low level CAN bus service",
+ "low-can",
+ verbs
+ }
+ };
+
+ const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
+ {
+ binder_interface = itf;
+
+ return &binding_desc;
+ }
+
+ /// @brief Initialize the binding.
+ ///
+ /// @param[in] service Structure which represent the Application Framework Binder.
+ ///
+ /// @return Exit code, zero if success.
+ int afbBindingV1ServiceInit(struct afb_service service)
+ {
+ can_bus_t& can_bus_manager = configuration_t::instance().get_can_bus_manager();
+
+ can_bus_manager.set_can_devices();
+ can_bus_manager.start_threads();
+
+ /// Initialize Diagnostic manager that will handle obd2 requests.
+ /// We pass by default the first CAN bus device to its Initialization.
+ /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
+ if(configuration_t::instance().get_diagnostic_manager().initialize())
+ return 0;
+
+ ERROR(binder_interface, "%s: There was something wrong with CAN device Initialization.", __FUNCTION__);
+ return 1;
+ }
+};
diff --git a/CAN-binder/low-can-binding/binding/low-can-hat.hpp b/CAN-binder/low-can-binding/binding/low-can-hat.hpp
new file mode 100644
index 00000000..b3384d77
--- /dev/null
+++ b/CAN-binder/low-can-binding/binding/low-can-hat.hpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loic Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstddef>
+#include <string>
+#include <systemd/sd-event.h>
+
+extern "C"
+{
+ #include <afb/afb-binding.h>
+};
+
+extern "C" struct afb_binding_interface;
+
+extern const struct afb_binding_interface *binder_interface;
+
+void on_no_clients(std::string message);
+int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata);
+
+void subscribe(struct afb_req request);
+void unsubscribe(struct afb_req request); \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-bus.cpp b/CAN-binder/low-can-binding/can/can-bus.cpp
new file mode 100644
index 00000000..c2968054
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus.cpp
@@ -0,0 +1,329 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <net/if.h>
+#include <sys/socket.h>
+#include <json-c/json.h>
+#include <linux/can/raw.h>
+#include <map>
+#include <cerrno>
+#include <vector>
+#include <string>
+#include <algorithm>
+
+#include "can-bus.hpp"
+
+#include "can-signals.hpp"
+#include "can-decoder.hpp"
+#include "../binding/configuration.hpp"
+#include "../utils/signals.hpp"
+#include "../utils/openxc-utils.hpp"
+
+extern "C"
+{
+ #include <afb/afb-binding.h>
+}
+
+/// @brief Class constructor
+///
+/// @param[in] conf_file - handle to the json configuration file.
+can_bus_t::can_bus_t(utils::config_parser_t conf_file)
+ : conf_file_{conf_file}
+{}
+
+/// @brief Will make the decoding operation on a classic CAN message. It will not
+/// handle CAN commands nor diagnostic messages that have their own method to get
+/// this happens.
+///
+/// It will add to the vehicle_message queue the decoded message and tell the event push
+/// thread to process it.
+///
+/// @param[in] can_message - a single CAN message from the CAN socket read, to be decode.
+///
+/// @return How many signals has been decoded.
+int can_bus_t::process_can_signals(const can_message_t& can_message)
+{
+ int processed_signals = 0;
+ struct utils::signals_found signals;
+ openxc_DynamicField search_key, decoded_message;
+ openxc_VehicleMessage vehicle_message;
+ configuration_t& conf = configuration_t::instance();
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ // First we have to found which can_signal_t it is
+ search_key = build_DynamicField((double)can_message.get_id());
+ signals = sm.find_signals(search_key);
+
+ // Decoding the message ! Don't kill the messenger !
+ for(const auto& sig : signals.can_signals)
+ {
+ std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+
+ if( s.find(sig->get_name()) != s.end() && afb_event_is_valid(s[sig->get_name()]))
+ {
+ bool send = true;
+ decoded_message = decoder_t::translateSignal(*sig, can_message, conf.get_all_can_signals(), &send);
+
+ if(send)
+ {
+ openxc_SimpleMessage s_message = build_SimpleMessage(sig->get_name(), decoded_message);
+ vehicle_message = build_VehicleMessage(s_message);
+
+ std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
+ push_new_vehicle_message(vehicle_message);
+ }
+ processed_signals++;
+ }
+ }
+
+ DEBUG(binder_interface, "%s: %d/%d CAN signals processed.", __FUNCTION__, processed_signals, (int)signals.can_signals.size());
+ return processed_signals;
+}
+
+/// @brief Will make the decoding operation on a diagnostic CAN message.Then it find the subscribed signal
+/// corresponding and will add the vehicle_message to the queue of event to pushed before notifying
+/// the event push thread to process it.
+///
+/// @param[in] manager - the diagnostic manager object that handle diagnostic communication
+/// @param[in] can_message - a single CAN message from the CAN socket read, to be decode.
+///
+/// @return How many signals has been decoded.
+int can_bus_t::process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message)
+{
+ int processed_signals = 0;
+
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+
+ openxc_VehicleMessage vehicle_message = manager.find_and_decode_adr(can_message);
+ if( (vehicle_message.has_simple_message && vehicle_message.simple_message.has_name) &&
+ (s.find(vehicle_message.simple_message.name) != s.end() && afb_event_is_valid(s[vehicle_message.simple_message.name])))
+ {
+ std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
+ push_new_vehicle_message(vehicle_message);
+ processed_signals++;
+ }
+
+ return processed_signals;
+}
+
+/// @brief thread to decoding raw CAN messages.
+///
+/// Depending on the nature of message, if arbitration ID matches ID for a diagnostic response
+/// then decoding a diagnostic message else use classic CAN signals decoding functions.
+///
+/// It will take from the can_message_q_ queue the next can message to process then it search
+/// about signal subscribed if there is a valid afb_event for it. We only decode signal for which a
+/// subscription has been made. Can message will be decoded using translateSignal that will pass it to the
+/// corresponding decoding function if there is one assigned for that signal. If not, it will be the default
+/// noopDecoder function that will operate on it.
+///
+/// TODO: make diagnostic messages parsing optionnal.
+void can_bus_t::can_decode_message()
+{
+ can_message_t can_message;
+
+ while(is_decoding_)
+ {
+ {
+ std::unique_lock<std::mutex> can_message_lock(can_message_mutex_);
+ new_can_message_cv_.wait(can_message_lock);
+ while(!can_message_q_.empty())
+ {
+ can_message = next_can_message();
+
+ if(configuration_t::instance().get_diagnostic_manager().is_diagnostic_response(can_message))
+ process_diagnostic_signals(configuration_t::instance().get_diagnostic_manager(), can_message);
+ else
+ process_can_signals(can_message);
+ }
+ }
+ new_decoded_can_message_.notify_one();
+ }
+}
+
+/// @brief thread to push events to suscribers. It will read subscribed_signals map to look
+/// which are events that has to be pushed.
+void can_bus_t::can_event_push()
+{
+ openxc_VehicleMessage v_message;
+ openxc_SimpleMessage s_message;
+ json_object* jo;
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ while(is_pushing_)
+ {
+ std::unique_lock<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
+ new_decoded_can_message_.wait(decoded_can_message_lock);
+ while(!vehicle_message_q_.empty())
+ {
+ v_message = next_vehicle_message();
+
+ s_message = get_simple_message(v_message);
+ {
+ std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = sm.get_subscribed_signals();
+ if(s.find(std::string(s_message.name)) != s.end() && afb_event_is_valid(s[std::string(s_message.name)]))
+ {
+ jo = json_object_new_object();
+ jsonify_simple(s_message, jo);
+ if(afb_event_push(s[std::string(s_message.name)], jo) == 0)
+ on_no_clients(std::string(s_message.name));
+ }
+ }
+ }
+ }
+}
+
+/// @brief Will initialize threads that will decode
+/// and push subscribed events.
+void can_bus_t::start_threads()
+{
+ is_decoding_ = true;
+ th_decoding_ = std::thread(&can_bus_t::can_decode_message, this);
+ if(!th_decoding_.joinable())
+ is_decoding_ = false;
+
+ is_pushing_ = true;
+ th_pushing_ = std::thread(&can_bus_t::can_event_push, this);
+ if(!th_pushing_.joinable())
+ is_pushing_ = false;
+}
+
+/// @brief Will stop all threads holded by can_bus_t object
+/// which are decoding and pushing then will wait that's
+/// they'll finish their job.
+void can_bus_t::stop_threads()
+{
+ is_decoding_ = false;
+ is_pushing_ = false;
+}
+
+/// @brief return new_can_message_cv_ member
+///
+/// @return return new_can_message_cv_ member
+std::condition_variable& can_bus_t::get_new_can_message_cv()
+{
+ return new_can_message_cv_;
+}
+
+/// @brief return can_message_mutex_ member
+///
+/// @return return can_message_mutex_ member
+std::mutex& can_bus_t::get_can_message_mutex()
+{
+ return can_message_mutex_;
+}
+
+/// @brief Return first can_message_t on the queue
+///
+/// @return a can_message_t
+can_message_t can_bus_t::next_can_message()
+{
+ can_message_t can_msg;
+
+ if(!can_message_q_.empty())
+ {
+ can_msg = can_message_q_.front();
+ can_message_q_.pop();
+ DEBUG(binder_interface, "%s: Here is the next can message : id %X, length %X, data %02X%02X%02X%02X%02X%02X%02X%02X", __FUNCTION__, can_msg.get_id(), can_msg.get_length(),
+ can_msg.get_data()[0], can_msg.get_data()[1], can_msg.get_data()[2], can_msg.get_data()[3], can_msg.get_data()[4], can_msg.get_data()[5], can_msg.get_data()[6], can_msg.get_data()[7]);
+ return can_msg;
+ }
+
+ return can_msg;
+}
+
+/// @brief Push a can_message_t into the queue
+///
+/// @param[in] can_msg - the const reference can_message_t object to push into the queue
+void can_bus_t::push_new_can_message(const can_message_t& can_msg)
+{
+ can_message_q_.push(can_msg);
+}
+
+/// @brief Return first openxc_VehicleMessage on the queue
+///
+/// @return a openxc_VehicleMessage containing a decoded can message
+openxc_VehicleMessage can_bus_t::next_vehicle_message()
+{
+ openxc_VehicleMessage v_msg;
+
+ if(! vehicle_message_q_.empty())
+ {
+ v_msg = vehicle_message_q_.front();
+ vehicle_message_q_.pop();
+ DEBUG(binder_interface, "%s: next vehicle message poped", __FUNCTION__);
+ return v_msg;
+ }
+
+ return v_msg;
+}
+
+/// @brief Push a openxc_VehicleMessage into the queue
+///
+/// @param[in] v_msg - const reference openxc_VehicleMessage object to push into the queue
+void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
+{
+ vehicle_message_q_.push(v_msg);
+}
+
+/// @brief Return the shared pointer on the can_bus_dev_t initialized
+/// with device_name "bus"
+///
+/// @param[in] bus - CAN bus device name to retrieve.
+///
+/// @return A shared pointer on an object can_bus_dev_t
+void can_bus_t::set_can_devices()
+{
+ can_devices_ = conf_file_.get_devices_name();
+
+ if(can_devices_.empty())
+ {
+ ERROR(binder_interface, "%s: No mapping found in config file: '%s'. Check it that it have a CANbus-mapping section.",
+ __FUNCTION__, conf_file_.filepath().c_str());
+ }
+}
+
+int can_bus_t::get_can_device_index(const std::string& bus_name) const
+{
+ int i = 0;
+ for(const auto& d: can_devices_)
+ {
+ if(d.first == bus_name)
+ break;
+ i++;
+ }
+ return i;
+}
+
+const std::string can_bus_t::get_can_device_name(const std::string& id_name) const
+{
+ std::string ret;
+ for(const auto& d: can_devices_)
+ {
+ if(d.first == id_name)
+ {
+ ret = d.second;
+ break;
+ }
+ }
+ return ret;
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-bus.hpp b/CAN-binder/low-can-binding/can/can-bus.hpp
new file mode 100644
index 00000000..1efd2049
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus.hpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <mutex>
+#include <queue>
+#include <thread>
+#include <linux/can.h>
+#include <condition_variable>
+
+#include "openxc.pb.h"
+#include "can-message.hpp"
+#include "../utils/config-parser.hpp"
+#include "../binding/low-can-hat.hpp"
+
+// TODO actual max is 32 but dropped to 24 for memory considerations
+#define MAX_ACCEPTANCE_FILTERS 24
+// TODO this takes up a ton of memory
+#define MAX_DYNAMIC_MESSAGE_COUNT 12
+
+#define CAN_ACTIVE_TIMEOUT_S 30
+
+class diagnostic_manager_t;
+
+/// @brief Object used to handle decoding and manage event queue to be pushed.
+///
+/// This object is also used to initialize can_bus_dev_t object after reading
+/// json conf file describing the CAN devices to use. Thus, those object will read
+/// on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
+///
+/// That queue will be later used to be decoded and pushed to subscribers.
+class can_bus_t
+{
+private:
+ utils::config_parser_t conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects.
+
+ int process_can_signals(const can_message_t& can_message);
+ int process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message);
+
+ void can_decode_message();
+ std::thread th_decoding_; ///< thread that'll handle decoding a can frame
+ bool is_decoding_ = false; ///< boolean member controling thread while loop
+
+ void can_event_push();
+ std::thread th_pushing_; ///< thread that'll handle pushing decoded can frame to subscribers
+ bool is_pushing_ = false; ///< boolean member controling thread while loop
+
+ std::condition_variable new_can_message_cv_; ///< condition_variable use to wait until there is a new CAN message to read
+ std::mutex can_message_mutex_; ///< mutex protecting the can_message_q_ queue.
+ std::queue <can_message_t> can_message_q_; ///< queue that'll store can_message_t to decoded
+
+ std::condition_variable new_decoded_can_message_; ///< condition_variable use to wait until there is a new vehicle message to read from the queue vehicle_message_q_
+ std::mutex decoded_can_message_mutex_; ///< mutex protecting the vehicle_message_q_ queue.
+ std::queue <openxc_VehicleMessage> vehicle_message_q_; ///< queue that'll store openxc_VehicleMessage to pushed
+
+ std::vector<std::pair<std::string, std::string> > can_devices_;
+public:
+ can_bus_t(utils::config_parser_t conf_file);
+ can_bus_t(can_bus_t&&);
+
+ void set_can_devices();
+ int get_can_device_index(const std::string& bus_name) const;
+ const std::string get_can_device_name(const std::string& id_name) const;
+
+ void start_threads();
+ void stop_threads();
+
+ can_message_t next_can_message();
+ void push_new_can_message(const can_message_t& can_msg);
+ std::mutex& get_can_message_mutex();
+ std::condition_variable& get_new_can_message_cv();
+
+ openxc_VehicleMessage next_vehicle_message();
+ void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
+};
diff --git a/CAN-binder/low-can-binding/can/can-command.hpp b/CAN-binder/low-can-binding/can/can-command.hpp
new file mode 100644
index 00000000..4cf20cb5
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-command.hpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "openxc.pb.h"
+#include "can-signals.hpp"
+
+///
+/// @brief The type signature for a function to handle a custom OpenXC command.
+///
+/// @param[in] name - the name of the received command.
+/// @param[in] value - the value of the received command, in a DynamicField. The actual type
+/// may be a number, string or bool.
+/// @param[in] event - an optional event from the received command, in a DynamicField. The
+/// actual type may be a number, string or bool.
+/// @param[in] signals - The list of all signals.
+/// @param[in] signalCount - The length of the signals array.
+///
+typedef void (*CommandHandler)(const char* name, openxc_DynamicField* value,
+ openxc_DynamicField* event, can_signal_t* signals, int signalCount);
+
+/// @struct CanCommand
+/// @brief The structure to represent a supported custom OpenXC command.
+///
+/// For completely customized CAN commands without a 1-1 mapping between an
+/// OpenXC message from the host and a CAN signal, you can define the name of the
+/// command and a custom function to handle it in the VI. An example is
+/// the "turn_signal_status" command in OpenXC, which has a value of "left" or
+/// "right". The vehicle may have separate CAN signals for the left and right
+/// turn signals, so you will need to implement a custom command handler to send
+/// the correct signals.
+///
+/// Command handlers are also useful if you want to trigger multiple CAN messages
+/// or signals from a signal OpenXC message.
+///
+typedef struct {
+ const char* generic_name; /*!< generic_name - The name of the command.*/
+ CommandHandler handler; /*!< handler - An function to process the received command's data and perform some
+ * action.*/
+} CanCommand;
diff --git a/CAN-binder/low-can-binding/can/can-decoder.cpp b/CAN-binder/low-can-binding/can/can-decoder.cpp
new file mode 100644
index 00000000..aa1f57ed
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-decoder.cpp
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "can-decoder.hpp"
+
+#include "canutil/read.h"
+#include "../utils/openxc-utils.hpp"
+#include "can-message-definition.hpp"
+#include "../binding/low-can-hat.hpp"
+
+/// @brief Parse the signal's bitfield from the given data and return the raw
+/// value.
+///
+/// @param[in] signal - The signal to parse from the data.
+/// @param[in] message - can_message_t to parse
+///
+/// @return Returns the raw value of the signal parsed as a bitfield from the given byte
+/// array.
+///
+float decoder_t::parseSignalBitfield(can_signal_t& signal, const can_message_t& message)
+{
+ return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE,
+ signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
+ signal.get_offset());
+}
+
+/// @brief Wrap a raw CAN signal value in a DynamicField without modification.
+///
+/// This is an implementation of the SignalDecoder type signature, and can be
+/// used directly in the can_signal_t.decoder field.
+///
+/// @param[in] signal - The details of the signal that contains the state mapping.
+/// @param[in] signals - The list of all signals
+/// @param[in] value - The numerical value that will be wrapped in a DynamicField.
+/// @param[out] send - An output argument that will be set to false if the value should
+/// not be sent for any reason.
+///
+/// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
+/// its numeric value. The 'send' argument will not be modified as this decoder
+/// always succeeds.
+///
+openxc_DynamicField decoder_t::noopDecoder(can_signal_t& signal,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, float value, bool* send)
+{
+ openxc_DynamicField decoded_value = build_DynamicField(value);
+
+ return decoded_value;
+}
+/// @brief Coerces a numerical value to a boolean.
+///
+/// This is an implementation of the SignalDecoder type signature, and can be
+/// used directly in the can_signal_t.decoder field.
+///
+/// @param[in] signal - The details of the signal that contains the state mapping.
+/// @param[in] signals - The list of all signals
+/// @param[in] value - The numerical value that will be converted to a boolean.
+/// @param[out] send - An output argument that will be set to false if the value should
+/// not be sent for any reason.
+///
+/// @return Returns a DynamicField with a boolean value of false if the raw signal value
+/// is 0.0, otherwise true. The 'send' argument will not be modified as this
+/// decoder always succeeds.
+///
+openxc_DynamicField decoder_t::booleanDecoder(can_signal_t& signal,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, float value, bool* send)
+{
+ openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
+
+ return decoded_value;
+}
+/// @brief Update the metadata for a signal and the newly received value.
+///
+/// This is an implementation of the SignalDecoder type signature, and can be
+/// used directly in the can_signal_t.decoder field.
+///
+/// This function always flips 'send' to false.
+///
+/// @param[in] signal - The details of the signal that contains the state mapping.
+/// @param[in] signals - The list of all signals.
+/// @param[in] value - The numerical value that will be converted to a boolean.
+/// @param[out] send - This output argument will always be set to false, so the caller will
+/// know not to publish this value to the pipeline.
+///
+/// @return Return value is undefined.
+///
+openxc_DynamicField decoder_t::ignoreDecoder(can_signal_t& signal,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, float value, bool* send)
+{
+ if(send)
+ *send = false;
+
+ openxc_DynamicField decoded_value;
+
+ return decoded_value;
+}
+
+/// @brief Find and return the corresponding string state for a CAN signal's
+/// raw integer value.
+///
+/// This is an implementation of the SignalDecoder type signature, and can be
+/// used directly in the can_signal_t.decoder field.
+///
+/// @param[in] signal - The details of the signal that contains the state mapping.
+/// @param[in] signals - The list of all signals.
+/// @param[in] value - The numerical value that should map to a state.
+/// @param[out] send - An output argument that will be set to false if the value should
+/// not be sent for any reason.
+///
+/// @return Returns a DynamicField with a string value if a matching state is found in
+/// the signal. If an equivalent isn't found, send is sent to false and the
+/// return value is undefined.
+///
+openxc_DynamicField decoder_t::stateDecoder(can_signal_t& signal,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, float value, bool* send)
+{
+ const std::string signal_state = signal.get_states((uint8_t)value);
+ openxc_DynamicField decoded_value = build_DynamicField(signal_state);
+ if(signal_state.size() <= 0)
+ {
+ *send = false;
+ ERROR(binder_interface, "%s: No state found with index: %d", __FUNCTION__, (int)value);
+ }
+ return decoded_value;
+}
+
+
+/// @brief Parse a signal from a CAN message, apply any required transforations
+/// to get a human readable value and public the result to the pipeline.
+///
+/// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
+/// will be passed to the decoder before publishing.
+///
+/// @param[in] signal - The details of the signal to decode and forward.
+/// @param[in] message - The received CAN message that should contain this signal.
+/// @param[in] signals - an array of all active signals.
+/// @param[out] send - An output parameter that will be flipped to false if the value could
+/// not be decoded.
+///
+/// The decoder returns an openxc_DynamicField, which may contain a number,
+/// string or boolean.
+///
+openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, const can_message_t& message,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send)
+{
+ float value = decoder_t::parseSignalBitfield(signal, message);
+ DEBUG(binder_interface, "%s: Decoded message from parseSignalBitfield: %f", __FUNCTION__, value);
+
+ // Must call the decoders every time, regardless of if we are going to
+ // decide to send the signal or not.
+ openxc_DynamicField decoded_value = decoder_t::decodeSignal(signal,
+ value, signals, send);
+
+ signal.set_received(true);
+
+ // Don't send if they is no changes
+ if ((signal.get_last_value() == value && !signal.get_send_same()) || !send )
+ {
+ *send = false;
+ }
+ signal.set_last_value(value);
+ signal.set_timestamp(message.get_timestamp());
+ signal.get_message()->set_last_value(message);
+ return decoded_value;
+}
+
+/// @brief Parse a signal from a CAN message and apply any required
+/// transforations to get a human readable value.
+///
+/// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
+/// will be passed to the decoder before returning.
+///
+/// @param[in] signal - The details of the signal to decode and forward.
+/// @param[in] value - The numerical value that will be converted to a boolean.
+/// @param[in] signals - an array of all active signals.
+/// @param[out] send - An output parameter that will be flipped to false if the value could
+/// not be decoded.
+///
+/// @return The decoder returns an openxc_DynamicField, which may contain a number,
+/// string or boolean. If 'send' is false, the return value is undefined.
+///
+openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
+ float value, const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send)
+{
+ SignalDecoder decoder = signal.get_decoder() == nullptr ?
+ noopDecoder : signal.get_decoder();
+ openxc_DynamicField decoded_value = decoder(signal, signals,
+ value, send);
+ return decoded_value;
+}
+
+/// @brief Decode a transformed, human readable value from an raw CAN signal
+/// already parsed from a CAN message.
+///
+/// This is the same as decodeSignal but you must parse the bitfield value of the signal from the CAN
+/// message yourself. This is useful if you need that raw value for something
+/// else.
+///
+/// @param[in] signal - The details of the signal to decode and forward.
+/// @param[in] message - Raw CAN message to decode
+/// @param[in] signals - an array of all active signals.
+/// @param[out] send - An output parameter that will be flipped to false if the value could
+/// not be decoded.
+///
+openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
+ const can_message_t& message, const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send)
+{
+ float value = parseSignalBitfield(signal, message);
+ return decodeSignal(signal, value, signals, send);
+}
+
+
+///
+/// @brief Decode the payload of an OBD-II PID.
+///
+/// This function matches the type signature for a DiagnosticResponseDecoder, so
+/// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
+/// value of the PID, using the standard formulas (see
+/// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
+///
+/// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
+/// a byte array). This is most often used when the byte order is
+/// signiticant, i.e. with many OBD-II PID formulas.
+/// @param[in] parsed_payload - the entire payload of the response parsed as an int.
+///
+/// @return Float decoded value.
+///
+float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
+{
+ return diagnostic_decode_obd2_pid(response);
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-decoder.hpp b/CAN-binder/low-can-binding/can/can-decoder.hpp
new file mode 100644
index 00000000..238bfeae
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-decoder.hpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "can-signals.hpp"
+#include "can-message.hpp"
+#include "openxc.pb.h"
+
+class decoder_t
+{
+public:
+ static float parseSignalBitfield(can_signal_t& signal, const can_message_t& message);
+
+ static openxc_DynamicField stateDecoder(can_signal_t& signal, const std::vector<std::shared_ptr<can_signal_t> > signals,
+ float value, bool* send);
+ static openxc_DynamicField booleanDecoder(can_signal_t& signal, const std::vector<std::shared_ptr<can_signal_t> > signals,
+ float value, bool* send);
+ static openxc_DynamicField ignoreDecoder(can_signal_t& signal, const std::vector<std::shared_ptr<can_signal_t> > signals,
+ float value, bool* send);
+ static openxc_DynamicField noopDecoder(can_signal_t& signal, const std::vector<std::shared_ptr<can_signal_t> > signals,
+ float value, bool* send);
+
+ static openxc_DynamicField translateSignal(can_signal_t& signal, const can_message_t& message,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send);
+
+ static openxc_DynamicField decodeSignal(can_signal_t& signal, const can_message_t& message,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send);
+
+ static openxc_DynamicField decodeSignal(can_signal_t& signal, float value,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, bool* send);
+
+ static float decode_obd2_response(const DiagnosticResponse* response, float parsed_payload);
+
+}; \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-message-definition.cpp b/CAN-binder/low-can-binding/can/can-message-definition.cpp
new file mode 100644
index 00000000..ec6292af
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-definition.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "can-message-definition.hpp"
+
+can_message_definition_t::can_message_definition_t(const std::string bus)
+ : parent_{nullptr}, bus_{bus}, last_value_{CAN_MESSAGE_SIZE}
+{}
+
+can_message_definition_t::can_message_definition_t(
+ const std::string bus,
+ uint32_t id,
+ frequency_clock_t frequency_clock,
+ bool force_send_changed)
+ : parent_{nullptr},
+ bus_{bus},
+ id_{id},
+ frequency_clock_{frequency_clock},
+ force_send_changed_{force_send_changed},
+ last_value_{CAN_MESSAGE_SIZE}
+{}
+
+can_message_definition_t::can_message_definition_t(
+ const std::string bus,
+ uint32_t id,
+ can_message_format_t format,
+ frequency_clock_t frequency_clock,
+ bool force_send_changed)
+ : parent_{nullptr},
+ bus_{bus},
+ id_{id},
+ format_{format},
+ frequency_clock_{frequency_clock},
+ force_send_changed_{force_send_changed},
+ last_value_{CAN_MESSAGE_SIZE}
+{}
+
+can_message_definition_t::can_message_definition_t(
+ const std::string bus,
+ uint32_t id,
+ can_message_format_t format,
+ frequency_clock_t frequency_clock,
+ bool force_send_changed,
+ const std::vector<std::shared_ptr<can_signal_t> >& can_signals)
+ : parent_{nullptr},
+ bus_{bus},
+ id_{id},
+ format_{format},
+ frequency_clock_{frequency_clock},
+ force_send_changed_{force_send_changed},
+ last_value_{CAN_MESSAGE_SIZE},
+ can_signals_{can_signals}
+{}
+
+const std::string can_message_definition_t::get_bus_name() const
+{
+ return bus_;
+}
+
+uint32_t can_message_definition_t::get_id() const
+{
+ return id_;
+}
+
+std::vector<std::shared_ptr<can_signal_t> >& can_message_definition_t::get_can_signals()
+{
+ return can_signals_;
+}
+
+void can_message_definition_t::set_parent(can_message_set_t* parent)
+{
+ parent_= parent;
+}
+
+void can_message_definition_t::set_last_value(const can_message_t& cm)
+{
+ last_value_= cm.get_data_vector();
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-message-definition.hpp b/CAN-binder/low-can-binding/can/can-message-definition.hpp
new file mode 100644
index 00000000..233d8ce8
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-definition.hpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @class can_message_definition_t
+ *
+ * @brief The definition of a CAN message. This includes a lot of metadata, so
+ * to save memory this struct should not be used for storing incoming and
+ * outgoing CAN messages.
+ */
+
+#pragma once
+
+#include <vector>
+#include <memory>
+
+#include "can-signals.hpp"
+#include "can-message.hpp"
+#include "can-message-set.hpp"
+#include "../utils/timer.hpp"
+
+class can_message_set_t;
+
+class can_message_definition_t
+{
+private:
+ can_message_set_t* parent_; /*!< parent_ - Pointer to the CAN message set holding this CAN message definition */
+ std::string bus_; /*!< bus_ - Address of CAN bus device. */
+ uint32_t id_; /*!< id_ - The ID of the message.*/
+ can_message_format_t format_; /*!< format_ - the format of the message's ID.*/
+ frequency_clock_t frequency_clock_; /*!< clock_ - an optional frequency clock to control the output of this
+ * message, if sent raw, or simply to mark the max frequency for custom
+ * handlers to retrieve.*/
+ bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send CAN
+ * message if it has changed when using raw passthrough.*/
+ std::vector<uint8_t> last_value_; /*!< last_value_ - The last received value of the message. Defaults to undefined.
+ * This is required for the forceSendChanged functionality, as the stack
+ * needs to compare an incoming CAN message with the previous frame.*/
+ std::vector<std::shared_ptr<can_signal_t> > can_signals_; /*!< can_signals_ - Vector holding can_signal_t object which share the same arbitration ID */
+
+public:
+ //can_message_definition_t(const can_message_definition_t& b);
+ can_message_definition_t(const std::string bus);
+ can_message_definition_t(const std::string bus, uint32_t id, frequency_clock_t frequency_clock, bool force_send_changed);
+ can_message_definition_t(const std::string bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed);
+ can_message_definition_t(const std::string bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed, const std::vector<std::shared_ptr<can_signal_t> >& can_signals);
+
+ const std::string get_bus_name() const;
+ uint32_t get_id() const;
+ std::vector<std::shared_ptr<can_signal_t> >& get_can_signals();
+
+ void set_parent(can_message_set_t* parent);
+ void set_last_value(const can_message_t& cm);
+};
diff --git a/CAN-binder/low-can-binding/can/can-message-set.cpp b/CAN-binder/low-can-binding/can/can-message-set.cpp
new file mode 100644
index 00000000..5b3a220f
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-set.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "can-message-set.hpp"
+
+#include "../can/can-message-definition.hpp"
+
+can_message_set_t::can_message_set_t(
+ uint8_t index,
+ const std::string name,
+ const std::vector<std::shared_ptr<can_message_definition_t> >& can_messages_definition,
+ const std::vector<std::shared_ptr<diagnostic_message_t> >& diagnostic_messages)
+ : index_{index}
+ , name_{name}
+ , can_messages_definition_{can_messages_definition}
+ , diagnostic_messages_{diagnostic_messages}
+{}
+
+/// @brief Return vector holding all message definition handled by this message set.
+std::vector<std::shared_ptr<can_message_definition_t> >& can_message_set_t::get_can_message_definition()
+{
+ return can_messages_definition_;
+}
+
+std::vector<std::shared_ptr<can_signal_t> > can_message_set_t::get_all_can_signals() const
+{
+ std::vector<std::shared_ptr<can_signal_t> > can_signals;
+ for(const auto& cmd: can_messages_definition_)
+ {
+ std::vector<std::shared_ptr<can_signal_t> >& cmd_signals = cmd->get_can_signals();
+ can_signals.insert( can_signals.end(),
+ cmd_signals.begin(),
+ cmd_signals.end()
+ );
+ }
+
+ return can_signals;
+}
+
+std::vector<std::shared_ptr<diagnostic_message_t> >& can_message_set_t::get_diagnostic_messages()
+{
+ return diagnostic_messages_;
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/can/can-message-set.hpp b/CAN-binder/low-can-binding/can/can-message-set.hpp
new file mode 100644
index 00000000..ca01abc8
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-set.hpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstdint>
+#include <string>
+#include <vector>
+#include <memory>
+
+class can_signal_t;
+class can_message_definition_t;
+class diagnostic_message_t;
+
+/// @brief A parent wrapper for a particular set of CAN messages and associated
+/// CAN buses(e.g. a vehicle or program).
+class can_message_set_t
+{
+private:
+ uint8_t index_; /// < A numerical ID for the message set, ideally the index in an array for fast lookup
+ const std::string name_; /// < The name of the message set.
+ std::vector<std::shared_ptr<can_message_definition_t> > can_messages_definition_; ///< Vector holding all message definition handled by this message set.
+ std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages_; ///< Vector holding all diagnostics messages from JSON signals description file. First vector map to message set
+
+public:
+ can_message_set_t(
+ uint8_t index,
+ const std::string name,
+ const std::vector<std::shared_ptr<can_message_definition_t> >& can_messages_definition,
+ const std::vector<std::shared_ptr<diagnostic_message_t> >& diagnostic_messages);
+
+ std::vector<std::shared_ptr<can_message_definition_t> >& get_can_message_definition();
+ std::vector<std::shared_ptr<can_signal_t> > get_all_can_signals() const;
+ std::vector<std::shared_ptr<diagnostic_message_t> >& get_diagnostic_messages();
+};
diff --git a/CAN-binder/low-can-binding/can/can-message.cpp b/CAN-binder/low-can-binding/can/can-message.cpp
new file mode 100644
index 00000000..772734d7
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message.cpp
@@ -0,0 +1,365 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "can-message.hpp"
+
+#include <cstring>
+
+#include "../binding/low-can-hat.hpp"
+
+///
+/// @brief Class constructor
+///
+/// Constructor about can_message_t class.
+///
+can_message_t::can_message_t()
+ : maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::INVALID}, rtr_flag_{false}, flags_{0}, timestamp_{0}
+{}
+
+can_message_t::can_message_t(uint8_t maxdlen,
+ uint32_t id,
+ uint8_t length,
+ can_message_format_t format,
+ bool rtr_flag,
+ uint8_t flags,
+ std::vector<uint8_t> data,
+ uint64_t timestamp)
+ : maxdlen_{maxdlen},
+ id_{id},
+ length_{length},
+ format_{format},
+ rtr_flag_{rtr_flag},
+ flags_{flags},
+ data_{data},
+ timestamp_{timestamp}
+{}
+
+///
+/// @brief Retrieve id_ member value.
+///
+/// @return id_ class member
+///
+uint32_t can_message_t::get_id() const
+{
+ return id_;
+}
+
+///
+/// @brief Retrieve RTR flag member.
+///
+/// @return rtr_flags_ class member
+///
+bool can_message_t::get_rtr_flag_() const
+{
+ return rtr_flag_;
+}
+
+///
+/// @brief Retrieve format_ member value.
+///
+/// @return format_ class member
+///
+can_message_format_t can_message_t::get_format() const
+{
+ if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
+ return can_message_format_t::INVALID;
+ return format_;
+}
+
+///
+/// @brief Retrieve flags_ member value.
+///
+/// @return flags_ class member
+///
+uint8_t can_message_t::get_flags() const
+{
+ return flags_;
+}
+
+///
+/// @brief Retrieve data_ member value.
+///
+/// @return pointer to the first element
+/// of class member data_
+///
+const uint8_t* can_message_t::get_data() const
+{
+ return data_.data();
+}
+
+///
+/// @brief Retrieve data_ member whole vector
+///
+/// @return the vector as is
+///
+const std::vector<uint8_t> can_message_t::get_data_vector() const
+{
+ return data_;
+}
+
+///
+/// @brief Retrieve length_ member value.
+///
+/// @return length_ class member
+///
+uint8_t can_message_t::get_length() const
+{
+ return length_;
+}
+
+uint64_t can_message_t::get_timestamp() const
+{
+ return timestamp_;
+}
+
+void can_message_t::set_timestamp(uint64_t timestamp)
+{
+ timestamp_ = timestamp;
+}
+
+///
+/// @brief Control whether the object is correctly initialized
+/// to be sent over the CAN bus
+///
+/// @return true if object correctly initialized and false if not.
+///
+bool can_message_t::is_correct_to_send()
+{
+ if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
+ {
+ int i;
+ for(i=0;i<CAN_MESSAGE_SIZE;i++)
+ if(data_[i] != 0)
+ return true;
+ }
+ return false;
+}
+
+///
+/// @brief Set format_ member value.
+///
+/// Preferred way to initialize these members by using
+/// convert_from_canfd_frame method.
+///
+/// @param[in] new_format - class member
+///
+void can_message_t::set_format(const can_message_format_t new_format)
+{
+ if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::INVALID)
+ format_ = new_format;
+ else
+ ERROR(binder_interface, "%s: Can set format, wrong format chosen", __FUNCTION__);
+}
+
+/// @brief Take a canfd_frame struct to initialize class members
+///
+/// This is the preferred way to initialize class members.
+///
+/// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
+/// @param[in] nbytes - bytes read from socket read operation.
+///
+/// @return A can_message_t object fully initialized with canfd_frame values.
+///
+can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
+{
+ uint8_t maxdlen, length, flags = (uint8_t)NULL;
+ uint32_t id;
+ can_message_format_t format;
+ bool rtr_flag;
+ std::vector<uint8_t> data;
+
+ switch(nbytes)
+ {
+ case CANFD_MTU:
+ DEBUG(binder_interface, "%s: Got an CAN FD frame", __FUNCTION__);
+ maxdlen = CANFD_MAX_DLEN;
+ break;
+ case CAN_MTU:
+ DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
+ maxdlen = CAN_MAX_DLEN;
+ break;
+ default:
+ ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
+ break;
+ }
+
+ if (frame.can_id & CAN_ERR_FLAG)
+ {
+ format = can_message_format_t::INVALID;
+ id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+ }
+ else if (frame.can_id & CAN_EFF_FLAG)
+ {
+ format = can_message_format_t::EXTENDED;
+ id = frame.can_id & CAN_EFF_MASK;
+ }
+ else
+ {
+ format = can_message_format_t::STANDARD;
+ id = frame.can_id & CAN_SFF_MASK;
+ }
+
+ /* Overwrite length_ if RTR flags is detected.
+ * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
+ if (frame.can_id & CAN_RTR_FLAG)
+ {
+ rtr_flag = true;
+ if(frame.len && frame.len <= CAN_MAX_DLC)
+ {
+ if(rtr_flag)
+ length = frame.len& 0xF;
+ else
+ {
+ length = (frame.len > maxdlen) ? maxdlen : frame.len;
+ }
+ }
+ }
+ else
+ {
+ length = (frame.len > maxdlen) ? maxdlen : frame.len;
+
+ /* Flags field only present for CAN FD frames*/
+ if(maxdlen == CANFD_MAX_DLEN)
+ flags = frame.flags & 0xF;
+
+ if (data.capacity() < maxdlen)
+ data.reserve(maxdlen);
+ int i;
+
+ data.clear();
+ /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
+ for(i=0;i<maxdlen;i++)
+ {
+ data.push_back(frame.data[i]);
+ };
+
+ DEBUG(binder_interface, "%s: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", __FUNCTION__,
+ id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
+ }
+
+ return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
+}
+
+can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes, uint64_t timestamp)
+{
+ uint8_t maxdlen, length, flags = (uint8_t)NULL;
+ uint32_t id;
+ can_message_format_t format;
+ bool rtr_flag;
+ std::vector<uint8_t> data;
+
+ if(nbytes <= CAN_MTU)
+ {
+ DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
+ maxdlen = CAN_MAX_DLEN;
+ }
+ else
+ {
+ ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
+ }
+
+ if (frame.can_id & CAN_ERR_FLAG)
+ {
+ format = can_message_format_t::INVALID;
+ id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+ }
+ else if (frame.can_id & CAN_EFF_FLAG)
+ {
+ format = can_message_format_t::EXTENDED;
+ id = frame.can_id & CAN_EFF_MASK;
+ }
+ else
+ {
+ format = can_message_format_t::STANDARD;
+ id = frame.can_id & CAN_SFF_MASK;
+ }
+
+ /* Overwrite length_ if RTR flags is detected.
+ * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
+ if (frame.can_id & CAN_RTR_FLAG)
+ {
+ rtr_flag = true;
+ if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
+ {
+ if(rtr_flag)
+ length = frame.can_dlc& 0xF;
+ else
+ {
+ length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
+ }
+ }
+ }
+ else
+ {
+ length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
+
+ if (data.capacity() < maxdlen)
+ data.reserve(maxdlen);
+ int i;
+
+ data.clear();
+ /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
+ for(i=0;i<maxdlen;i++)
+ {
+ data.push_back(frame.data[i]);
+ };
+
+// DEBUG(binder_interface, "%s: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", __FUNCTION__,
+// id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
+ }
+
+ return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
+}
+
+///
+/// @brief Take all initialized class's members and build an
+/// canfd_frame struct that can be use to send a CAN message over
+/// the bus.
+///
+/// @return canfd_frame struct built from class members.
+///
+struct canfd_frame can_message_t::convert_to_canfd_frame()
+{
+ canfd_frame frame;
+
+ if(is_correct_to_send())
+ {
+ frame.can_id = get_id();
+ frame.len = get_length();
+ ::memcpy(frame.data, get_data(), length_);
+ }
+ else
+ ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
+
+ return frame;
+}
+
+struct can_frame can_message_t::convert_to_can_frame()
+{
+ can_frame frame;
+
+ if(is_correct_to_send())
+ {
+ frame.can_id = get_id();
+ frame.can_dlc = get_length();
+ ::memcpy(frame.data, get_data(), length_);
+ }
+ else
+ ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
+
+ return frame;
+}
diff --git a/CAN-binder/low-can-binding/can/can-message.hpp b/CAN-binder/low-can-binding/can/can-message.hpp
new file mode 100644
index 00000000..910b3e9f
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message.hpp
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <vector>
+#include <string>
+#include <cstdint>
+#include <linux/can.h>
+
+#include "../utils/timer.hpp"
+
+#define CAN_MESSAGE_SIZE 8
+
+/**
+ * @enum can_message_format_t
+ * @brief The ID format for a CAN message.
+ */
+enum class can_message_format_t {
+ STANDARD, /*!< STANDARD - standard 11-bit CAN arbitration ID. */
+ EXTENDED, /*!< EXTENDED - an extended frame, with a 29-bit arbitration ID. */
+ INVALID, /*!< INVALID - INVALID code used at initialization to signify that it isn't usable'*/
+};
+
+/**
+ * @class can_message_t
+ *
+ * @brief A compact representation of a single CAN message, meant to be used in in/out
+ * buffers.
+ */
+class can_message_t {
+private:
+ uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/
+ uint32_t id_; /*!< id_ - The ID of the message. */
+ uint8_t length_; /*!< length_ - the length of the data array (max 8). */
+ can_message_format_t format_; /*!< format_ - the format of the message's ID.*/
+ bool rtr_flag_; /*!< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/
+ uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/
+ std::vector<uint8_t> data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/
+ uint64_t timestamp_; /*!< timestamp_ - timestamp of the received message*/
+
+public:
+ can_message_t();
+ can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector<uint8_t> data, uint64_t timestamp);
+
+ uint32_t get_id() const;
+ bool get_rtr_flag_() const;
+ can_message_format_t get_format() const;
+ uint8_t get_flags() const;
+ const uint8_t* get_data() const;
+ const std::vector<uint8_t> get_data_vector() const;
+ uint8_t get_length() const;
+ uint64_t get_timestamp() const;
+
+ void set_timestamp(uint64_t timestamp);
+ void set_format(const can_message_format_t new_format);
+
+ bool is_correct_to_send();
+
+ static can_message_t convert_from_frame(const canfd_frame& frame, size_t nbytes, uint64_t timestamp);
+ static can_message_t convert_from_frame(const can_frame& frame, size_t nbytes, uint64_t timestamp);
+
+ struct canfd_frame convert_to_canfd_frame();
+ struct can_frame convert_to_can_frame();
+};
diff --git a/CAN-binder/low-can-binding/can/can-signals.cpp b/CAN-binder/low-can-binding/can/can-signals.cpp
new file mode 100644
index 00000000..9a876136
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-signals.cpp
@@ -0,0 +1,262 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fnmatch.h>
+
+#include "can-signals.hpp"
+
+#include "../binding/configuration.hpp"
+#include "../utils/signals.hpp"
+#include "can-decoder.hpp"
+#include "can-message.hpp"
+#include "can-bus.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+#include "canutil/write.h"
+
+std::string can_signal_t::prefix_ = "messages";
+
+can_signal_t::can_signal_t(
+ std::string generic_name,
+ uint8_t bit_position,
+ uint8_t bit_size,
+ float factor,
+ float offset,
+ float min_value,
+ float max_value,
+ frequency_clock_t frequency,
+ bool send_same,
+ bool force_send_changed,
+ std::map<uint8_t, std::string> states,
+ bool writable,
+ SignalDecoder decoder,
+ SignalEncoder encoder,
+ bool received)
+ : parent_{nullptr},
+ generic_name_{ generic_name }
+ , bit_position_{ bit_position }
+ , bit_size_{ bit_size }
+ , factor_{ factor }
+ , offset_{ offset }
+ , min_value_{min_value}
+ , max_value_{max_value}
+ , frequency_{frequency}
+ , send_same_{send_same}
+ , force_send_changed_{force_send_changed}
+ , states_{states}
+ , writable_{writable}
+ , decoder_{decoder}
+ , encoder_{encoder}
+ , received_{received}
+ , last_value_{.0f}
+{}
+
+/*can_signal_t::can_signal_t(const can_signal_t& b)
+ : parent_{b.parent_},
+ generic_name_{ b.generic_name_}
+ , bit_position_{ b.bit_position_}
+ , bit_size_{ b.bit_size_}
+ , factor_{ b.factor_ }
+ , offset_{ b.offset_}
+ , min_value_{b.min_value_}
+ , max_value_{b.max_value_}
+ , frequency_{b.frequency_}
+ , send_same_{b.send_same_}
+ , force_send_changed_{b.force_send_changed_}
+ , states_{b.states_}
+ , writable_{b.writable_}
+ , decoder_{b.decoder_}
+ , encoder_{b.encoder_}
+ , received_{b.received_}
+ , last_value_{b.last_value_}
+{}*/
+
+utils::socketcan_bcm_t can_signal_t::get_socket() const
+{
+ return socket_;
+}
+
+can_message_definition_t* can_signal_t::get_message() const
+{
+ return parent_;
+}
+
+const std::string can_signal_t::get_generic_name() const
+{
+ return generic_name_;
+}
+
+const std::string can_signal_t::get_name() const
+{
+ return prefix_ + "." + generic_name_;
+}
+
+const std::string can_signal_t::get_prefix() const
+{
+ return prefix_;
+}
+
+uint8_t can_signal_t::get_bit_position() const
+{
+ return bit_position_;
+}
+
+uint8_t can_signal_t::get_bit_size() const
+{
+ return bit_size_;
+}
+
+float can_signal_t::get_factor() const
+{
+ return factor_;
+}
+
+float can_signal_t::get_offset() const
+{
+ return offset_;
+}
+
+float can_signal_t::get_min_value() const
+{
+ return min_value_;
+}
+
+float can_signal_t::get_max_value() const
+{
+ return max_value_;
+}
+
+frequency_clock_t& can_signal_t::get_frequency()
+{
+ return frequency_;
+}
+
+bool can_signal_t::get_send_same() const
+{
+ return send_same_;
+}
+
+bool can_signal_t::get_force_send_changed() const
+{
+ return force_send_changed_;
+}
+
+const std::map<uint8_t, std::string>& can_signal_t::get_states() const
+{
+ return states_;
+}
+
+const std::string can_signal_t::get_states(uint8_t value)
+{
+ if (value < states_.size())
+ return states_[value];
+ return std::string();
+}
+
+size_t can_signal_t::get_state_count() const
+{
+ return states_.size();
+}
+
+bool can_signal_t::get_writable() const
+{
+ return writable_;
+}
+
+SignalDecoder& can_signal_t::get_decoder()
+{
+ return decoder_;
+}
+
+SignalEncoder& can_signal_t::get_encoder()
+{
+ return encoder_;
+}
+
+bool can_signal_t::get_received() const
+{
+ return received_;
+}
+float can_signal_t::get_last_value() const
+{
+ return last_value_;
+}
+
+void can_signal_t::set_parent(can_message_definition_t* parent)
+{
+ parent_ = parent;
+}
+
+void can_signal_t::set_prefix(std::string val)
+{
+ prefix_ = val;
+}
+
+void can_signal_t::set_received(bool r)
+{
+ received_ = r;
+}
+
+void can_signal_t::set_last_value(float val)
+{
+ last_value_ = val;
+}
+
+void can_signal_t::set_timestamp(uint64_t timestamp)
+{
+ frequency_.tick(timestamp);
+}
+
+/// @brief Create a RX_SETUP receive job used by the BCM socket.
+///
+/// @return 0 if ok else -1
+int can_signal_t::create_rx_filter()
+{
+ // Make sure that socket has been opened.
+ if(! socket_)
+ socket_.open(
+ get_message()->get_bus_name());
+
+ uint32_t can_id = get_message()->get_id();
+
+ struct utils::simple_bcm_msg bcm_msg;
+ struct can_frame cfd;
+
+ memset(&cfd, 0, sizeof(cfd));
+ memset(&bcm_msg.msg_head, 0, sizeof(bcm_msg.msg_head));
+ float val = (float)(1 << bit_size_)-1;
+ struct timeval freq = frequency_.get_timeval_from_period();
+
+ bcm_msg.msg_head.opcode = RX_SETUP;
+ bcm_msg.msg_head.can_id = can_id;
+ bcm_msg.msg_head.flags = SETTIMER|RX_NO_AUTOTIMER;
+ bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec ;
+ bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
+ bcm_msg.msg_head.nframes = 1;
+ bitfield_encode_float(val,
+ bit_position_,
+ bit_size_,
+ factor_,
+ offset_,
+ cfd.data,
+ CAN_MAX_DLEN);
+
+ bcm_msg.frames = cfd;
+
+ if(socket_ << bcm_msg)
+ return 0;
+ return -1;
+}
diff --git a/CAN-binder/low-can-binding/can/can-signals.hpp b/CAN-binder/low-can-binding/can/can-signals.hpp
new file mode 100644
index 00000000..cc4b6da3
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-signals.hpp
@@ -0,0 +1,160 @@
+///
+/// Copyright (C) 2015, 2016 "IoT.bzh"
+/// Author "Romain Forlot" <romain.forlot@iot.bzh>
+///
+/// Licensed under the Apache License, Version 2.0 (the "License");
+/// you may not use this file except in compliance with the License.
+/// You may obtain a copy of the License at
+///
+/// http://www.apache.org/licenses/LICENSE-2.0
+///
+/// Unless required by applicable law or agreed to in writing, software
+/// distributed under the License is distributed on an "AS IS" BASIS,
+/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+/// See the License for the specific language governing permissions and
+/// limitations under the License.
+///
+
+#pragma once
+
+#include <map>
+#include <mutex>
+#include <vector>
+#include <string>
+#include <memory>
+
+#include "openxc.pb.h"
+#include "can-message-definition.hpp"
+#include "../utils/timer.hpp"
+#include "../utils/socketcan-bcm.hpp"
+#include "can-message.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+
+extern "C"
+{
+ #include <afb/afb-binding.h>
+ #include <afb/afb-event-itf.h>
+}
+
+#define MESSAGE_SET_ID 0
+
+class can_signal_t;
+///
+/// @brief The type signature for a CAN signal decoder.
+///
+/// A SignalDecoder transforms a raw floating point CAN signal into a number,
+/// string or boolean.
+///
+/// @param[in] signal - The CAN signal that we are decoding.
+/// @param[in] signals - The list of all signals.
+/// @param[in] signalCount - The length of the signals array.
+/// @param[in] value - The CAN signal parsed from the message as a raw floating point
+/// value.
+/// @param[out] send - An output parameter. If the decoding failed or the CAN signal should
+/// not send for some other reason, this should be flipped to false.
+///
+/// @return a decoded value in an openxc_DynamicField struct.
+///
+typedef openxc_DynamicField (*SignalDecoder)(can_signal_t& signal,
+ const std::vector<std::shared_ptr<can_signal_t> > signals, float value, bool* send);
+
+///
+/// @brief: The type signature for a CAN signal encoder.
+///
+/// A SignalEncoder transforms a number, string or boolean into a raw floating
+/// point value that fits in the CAN signal.
+///
+/// @param[in] signal - The CAN signal to encode.
+/// @param[in] value - The dynamic field to encode.
+/// @param[out] send - An output parameter. If the encoding failed or the CAN signal should
+/// not be encoded for some other reason, this will be flipped to false.
+///
+typedef uint64_t (*SignalEncoder)(can_signal_t* signal,
+ openxc_DynamicField* value, bool* send);
+
+class can_signal_t
+{
+private:
+ can_message_definition_t* parent_; /*!< parent_ - pointer to the parent message definition holding this signal*/
+ utils::socketcan_bcm_t socket_; /*!< socket_ - Specific BCM socket that filter the signal read from CAN device */
+ std::string generic_name_; /*!< generic_name_ - The name of the signal to be output.*/
+ static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
+ * which make easier to sort message when the come in.*/
+ uint8_t bit_position_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
+ * non-inverted bit numbering, i.e. the most significant bit of
+ * each byte is 0) */
+ uint8_t bit_size_; /*!< bit_size_ - The width of the bit field in the CAN message. */
+ float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
+ * don't need a factor. */
+ float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
+ * don't need an offset. */
+ float min_value_; /*!< min_value_ - The minimum value for the processed signal.*/
+ float max_value_; /*!< max_value_ - The maximum value for the processed signal. */
+ frequency_clock_t frequency_; /*!< frequency_ - A frequency_clock_t struct to control the maximum frequency to
+ * process and send this signal. To process every value, set the
+ * clock's frequency to 0. */
+ bool send_same_; /*!< send_same_ - If true, will re-send even if the value hasn't changed.*/
+ bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send the
+ * value if it has changed. */
+ std::map<uint8_t, std::string> states_; /*!< states_ - A map of CAN signal state describing the mapping
+ * between numerical and string values for valid states. */
+ bool writable_; /*!< writable - True if the signal is allowed to be written from the USB host
+ * back to CAN. Defaults to false.*/
+ SignalDecoder decoder_; /*!< decoder_ - An optional function to decode a signal from the bus to a human
+ * readable value. If NULL, the default numerical decoder is used. */
+ SignalEncoder encoder_; /*!< encoder_ - An optional function to encode a signal value to be written to
+ * CAN into a byte array. If NULL, the default numerical encoder
+ * is used. */
+ bool received_; /*!< received_ - True if this signal has ever been received.*/
+ float last_value_; /*!< lastValue_ - The last received value of the signal. If 'received' is false,
+ * this value is undefined. */
+
+public:
+ can_signal_t(
+ std::string generic_name,
+ uint8_t bit_position,
+ uint8_t bit_size,
+ float factor,
+ float offset,
+ float min_value,
+ float max_value,
+ frequency_clock_t frequency,
+ bool send_same,
+ bool force_send_changed,
+ std::map<uint8_t, std::string> states,
+ bool writable,
+ SignalDecoder decoder,
+ SignalEncoder encoder,
+ bool received);
+
+ utils::socketcan_bcm_t get_socket() const;
+ can_message_definition_t* get_message() const;
+ const std::string get_generic_name() const;
+ const std::string get_name() const;
+ const std::string get_prefix() const;
+ uint8_t get_bit_position() const;
+ uint8_t get_bit_size() const;
+ float get_factor() const;
+ float get_offset() const;
+ float get_min_value() const;
+ float get_max_value() const;
+ frequency_clock_t& get_frequency();
+ bool get_send_same() const;
+ bool get_force_send_changed() const;
+ const std::map<uint8_t, std::string>& get_states() const;
+ const std::string get_states(uint8_t value);
+ size_t get_state_count() const;
+ bool get_writable() const;
+ SignalDecoder& get_decoder();
+ SignalEncoder& get_encoder();
+ bool get_received() const;
+ float get_last_value() const;
+
+ void set_parent(can_message_definition_t* parent);
+ void set_prefix(std::string val);
+ void set_received(bool r);
+ void set_last_value(float val);
+ void set_timestamp(uint64_t timestamp);
+
+ int create_rx_filter();
+};
diff --git a/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp
new file mode 100644
index 00000000..e657456f
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <map>
+#include <fnmatch.h>
+
+#include "active-diagnostic-request.hpp"
+
+#include "../binding/configuration.hpp"
+
+#define ERROR_PID 0xFF
+
+std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
+
+bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
+{
+ return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_);
+}
+
+active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
+{
+ if (this != &adr)
+ {
+ bus_ = adr.bus_;
+ id_ = adr.id_;
+ handle_ = adr.handle_;
+ name_ = adr.name_;
+ decoder_ = adr.decoder_;
+ callback_ = adr.callback_;
+ recurring_ = adr.recurring_;
+ wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
+ frequency_clock_ = adr.frequency_clock_;
+ timeout_clock_ = adr.timeout_clock_;
+ }
+
+ return *this;
+}
+
+active_diagnostic_request_t::active_diagnostic_request_t()
+ : bus_{nullptr},
+ id_{0},
+ handle_{nullptr},
+ name_{""},
+ decoder_{nullptr},
+ callback_{nullptr},
+ recurring_{false},
+ wait_for_multiple_responses_{false},
+ frequency_clock_{frequency_clock_t()},
+ timeout_clock_{frequency_clock_t()}
+{}
+
+active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, DiagnosticRequest* request,
+ const std::string& name,
+ bool wait_for_multiple_responses,
+ const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback,
+ float frequencyHz)
+ : bus_{bus},
+ id_{request->arbitration_id},
+ handle_{nullptr},
+ name_{name},
+ decoder_{decoder},
+ callback_{callback},
+ recurring_{frequencyHz ? true : false},
+ wait_for_multiple_responses_{wait_for_multiple_responses},
+ frequency_clock_{frequency_clock_t(frequencyHz)},
+ timeout_clock_{frequency_clock_t(10)}
+{}
+
+uint32_t active_diagnostic_request_t::get_id() const
+{
+ return id_;
+}
+
+uint16_t active_diagnostic_request_t::get_pid() const
+{
+ if (handle_->request.has_pid)
+ return handle_->request.pid;
+ return ERROR_PID;
+}
+
+DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
+{
+ return handle_;
+}
+
+const std::string active_diagnostic_request_t::get_name() const
+{
+ return name_;
+}
+
+std::string& active_diagnostic_request_t::get_prefix()
+{
+ return active_diagnostic_request_t::prefix_;
+}
+
+DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
+{
+ return decoder_;
+}
+
+DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
+{
+ return callback_;
+}
+
+bool active_diagnostic_request_t::get_recurring() const
+{
+ return recurring_;
+}
+
+frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
+{
+ return frequency_clock_;
+}
+
+frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
+{
+ return timeout_clock_;
+}
+
+utils::socketcan_bcm_t& active_diagnostic_request_t::get_socket()
+{
+ return socket_;
+}
+
+void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
+{
+ handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
+}
+
+///
+/// @brief Check if requested signal name is a diagnostic message. If the name
+/// begin with the diagnostic message prefix then true else false.
+///
+/// @param[in] name - A signal name.
+///
+/// @return true if name began with the diagnostic message prefix else false.
+///
+bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
+{
+ const std::string p = active_diagnostic_request_t::prefix_ + "*";
+ if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
+ return true;
+ return false;
+}
+
+/// @brief Returns true if a sufficient response has been received for a
+/// diagnostic request.
+///
+/// This is true when at least one response has been received and the request is
+/// configured to not wait for multiple responses. Functional broadcast requests
+/// may often wish to wait the full 100ms for modules to respond.
+bool active_diagnostic_request_t::response_received() const
+{
+ return !wait_for_multiple_responses_ &&
+ handle_->completed && handle_->success;
+}
diff --git a/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp
new file mode 100644
index 00000000..c295bed3
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "../utils/socketcan-bcm.hpp"
+#include "uds/uds.h"
+#include "uds/uds_types.h"
+#include "../utils/timer.hpp"
+
+class active_diagnostic_request_t;
+class diagnostic_manager_t;
+
+/// @brief The signature for an optional function that can apply the neccessary
+/// formula to translate the binary payload into meaningful data.
+///
+/// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
+/// a byte array). This is most often used when the byte order is signiticant, i.e. with many OBD-II PID formulas.
+/// @param[in] parsed_payload - the entire payload of the response parsed as an int.
+///
+/// @return float value after decoding.
+///
+typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response,
+ float parsed_payload);
+
+/// @brief: The signature for an optional function to handle a new diagnostic
+/// response.
+///
+/// @param[in] request - The original diagnostic request.
+/// @param[in] response - The response object that was just received.
+/// @param[in] parsed_payload - The payload of the response, parsed as a float.
+///
+typedef void (*DiagnosticResponseCallback)(const active_diagnostic_request_t* request,
+ const DiagnosticResponse* response, float parsed_payload);
+
+///
+/// @brief An active diagnostic request, either recurring or one-time.
+///
+/// Will host a diagnostic_message_t class members to describe an on going
+/// diagnostic request on the CAN bus. Diagnostic message will be converted to
+/// a DiagnosticRequest using ad-hoc method build_diagnostic_request from diagnostic message.
+/// Then missing member, that can not be hosted into a DiagnosticRequest struct, will be passed
+/// as argument when adding the request to (non)-recurrent vector. Argument will be used to instanciate
+/// an active_diagnostic_request_t object before sending it.
+///
+class active_diagnostic_request_t {
+private:
+ std::string bus_; ///< bus_ - The CAN bus this request should be made on, or is currently in flight-on
+ uint32_t id_; ///< id_ - The arbitration ID (aka message ID) for the request.
+ DiagnosticRequestHandle* handle_; ///< handle_ - A handle for the request to keep track of it between
+ ///< sending the frames of the request and receiving all frames of the response.
+ std::string name_; ///< name_ - Human readable name, to be used when publishing received responses.
+ ///< TODO: If the name is NULL, the published output will use the raw OBD-II response format.
+ static std::string prefix_; ///< prefix_ - It has to reflect the JSON object which it comes from. It makes easier sorting
+ ///< incoming CAN messages.
+ DiagnosticResponseDecoder decoder_; ///< decoder_ - An optional DiagnosticResponseDecoder to parse the payload of responses
+ ///< to this request. If the decoder is NULL, the output will include the raw payload
+ ///< instead of a parsed value.
+ DiagnosticResponseCallback callback_; ///< callback_ - An optional DiagnosticResponseCallback to be notified whenever a
+ ///< response is received for this request.
+ bool recurring_; ///< bool recurring_ - If true, this is a recurring request and it will remain as active until explicitly cancelled.
+ ///< The frequencyClock attribute controls how often a recurrin request is made.
+ bool wait_for_multiple_responses_; ///< wait_for_multiple_responses_ - False by default, when any response is received for a request
+ ///< it will be removed from the active list. If true, the request will remain active until the timeout
+ ///< clock expires, to allow it to receive multiple response (e.g. to a functional broadcast request).
+ frequency_clock_t frequency_clock_; ///< frequency_clock_ - A frequency_clock_t object to control the send rate for a
+ ///< recurring request. If the request is not reecurring, this attribute is not used.
+ frequency_clock_t timeout_clock_; ///< timeout_clock_ - A frequency_clock_t object to monitor how long it's been since
+ ///< this request was sent.
+ utils::socketcan_bcm_t socket_; ///< socket_ - A BCM socket setup to send cyclic message to CAN ID 7DF.
+public:
+ bool operator==(const active_diagnostic_request_t& b);
+ active_diagnostic_request_t& operator=(const active_diagnostic_request_t& adr);
+
+ active_diagnostic_request_t();
+ active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
+ active_diagnostic_request_t(const std::string& bus, DiagnosticRequest* request,
+ const std::string& name, bool wait_for_multiple_responses,
+ const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback, float frequencyHz);
+
+ uint32_t get_id() const;
+ DiagnosticRequestHandle* get_handle();
+ uint16_t get_pid() const;
+ const std::string get_name() const;
+ static std::string& get_prefix();
+ DiagnosticResponseDecoder& get_decoder();
+ DiagnosticResponseCallback& get_callback();
+ bool get_recurring() const;
+ frequency_clock_t& get_frequency_clock();
+ frequency_clock_t& get_timeout_clock();
+ utils::socketcan_bcm_t& get_socket();
+
+ void set_handle(DiagnosticShims& shims, DiagnosticRequest* request);
+
+ static bool is_diagnostic_signal(const std::string& name);
+
+ bool response_received() const;
+};
diff --git a/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp
new file mode 100644
index 00000000..1a382af9
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp
@@ -0,0 +1,584 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <systemd/sd-event.h>
+#include <algorithm>
+#include <string.h>
+
+#include "diagnostic-manager.hpp"
+
+#include "../utils/openxc-utils.hpp"
+#include "../utils/signals.hpp"
+#include "../binding/configuration.hpp"
+
+#define MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ 10
+#define MAX_SIMULTANEOUS_DIAG_REQUESTS 50
+// There are only 8 slots of in flight diagnostic requests
+#define MAX_SIMULTANEOUS_IN_FLIGHT_REQUESTS 8
+#define TIMERFD_ACCURACY 0
+#define MICRO 1000000
+
+diagnostic_manager_t::diagnostic_manager_t()
+ : initialized_{false}
+{}
+
+/// @brief Diagnostic manager isn't initialized at launch but after
+/// CAN bus devices initialization. For the moment, it is only possible
+/// to have 1 diagnostic bus which are the first bus declared in the JSON
+/// description file. Configuration instance will return it.
+///
+/// this will initialize DiagnosticShims and cancel all active requests
+/// if there are any.
+bool diagnostic_manager_t::initialize()
+{
+ // Mandatory to set the bus before intialize shims.
+ bus_ = configuration_t::instance().get_diagnostic_bus();
+
+ init_diagnostic_shims();
+ event_source_ = nullptr;
+ reset();
+
+ initialized_ = true;
+ DEBUG(binder_interface, "%s: Diagnostic Manager initialized", __FUNCTION__);
+ return initialized_;
+}
+
+utils::socketcan_bcm_t& diagnostic_manager_t::get_socket()
+{
+ return socket_;
+}
+
+/// @brief initialize shims used by UDS lib and set initialized_ to true.
+/// It is needed before used the diagnostic manager fully because shims are
+/// required by most member functions.
+void diagnostic_manager_t::init_diagnostic_shims()
+{
+ shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);
+ DEBUG(binder_interface, "%s: Shims initialized", __FUNCTION__);
+}
+
+/// @brief Force cleanup all active requests.
+void diagnostic_manager_t::reset()
+{
+ DEBUG(binder_interface, "%s: Clearing existing diagnostic requests", __FUNCTION__);
+ cleanup_active_requests(true);
+}
+
+/// @brief Adds 8 RX_SETUP jobs to the BCM rx_socket_ then diagnotic manager
+/// listens on CAN ID range 7E8 - 7EF affected to the OBD2 communications.
+///
+/// @return -1 or negative value on error, 0 if ok.
+int diagnostic_manager_t::add_rx_filter(uint32_t can_id)
+{
+ // Make sure that socket has been opened.
+ if(! socket_)
+ socket_.open(bus_);
+
+ struct utils::simple_bcm_msg bcm_msg;
+ memset(&bcm_msg.msg_head, 0, sizeof(bcm_msg.msg_head));
+
+ const struct timeval freq = recurring_requests_.back()->get_timeout_clock().get_timeval_from_period();
+
+ bcm_msg.msg_head.opcode = RX_SETUP;
+ bcm_msg.msg_head.flags = SETTIMER|RX_FILTER_ID;
+ bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec;
+ bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
+
+ // If it isn't an OBD2 CAN ID then just add a simple RX_SETUP job
+ if(can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
+ {
+ bcm_msg.msg_head.can_id = can_id;
+
+ socket_ << bcm_msg;
+ if(! socket_)
+ return -1;
+ }
+ else
+ {
+ for(uint8_t i = 0; i < 8; i++)
+ {
+ can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
+ bcm_msg.msg_head.can_id = can_id;
+
+ socket_ << bcm_msg;
+ if(! socket_)
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/// @brief send function use by diagnostic library. Only one bus used for now
+/// so diagnostic request is sent using the default diagnostic bus not matter of
+/// which is specified in the diagnostic message definition.
+///
+/// @param[in] arbitration_id - CAN arbitration ID to use when send message. OBD2 broadcast ID
+/// is 0x7DF by example.
+/// @param[in] data - The data payload for the message. NULL is valid if size is also 0.
+/// @param[in] size - The size of the data payload, in bytes.
+///
+/// @return true if the CAN message was sent successfully.
+bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
+{
+ diagnostic_manager_t& dm = configuration_t::instance().get_diagnostic_manager();
+ active_diagnostic_request_t* current_adr = dm.get_last_recurring_requests();
+ utils::socketcan_bcm_t& tx_socket = current_adr->get_socket();
+
+ // Make sure that socket has been opened.
+ if(! tx_socket)
+ tx_socket.open(
+ dm.get_can_bus());
+
+ struct utils::simple_bcm_msg bcm_msg;
+ struct can_frame cfd;
+
+ memset(&cfd, 0, sizeof(cfd));
+ memset(&bcm_msg.msg_head, 0, sizeof(bcm_msg.msg_head));
+
+ struct timeval freq = current_adr->get_frequency_clock().get_timeval_from_period();
+
+ bcm_msg.msg_head.opcode = TX_SETUP;
+ bcm_msg.msg_head.can_id = arbitration_id;
+ bcm_msg.msg_head.flags = SETTIMER|STARTTIMER|TX_CP_CAN_ID;
+ bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec;
+ bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
+ bcm_msg.msg_head.nframes = 1;
+ cfd.can_dlc = size;
+ ::memcpy(cfd.data, data, size);
+
+ bcm_msg.frames = cfd;
+
+ tx_socket << bcm_msg;
+ if(tx_socket)
+ return true;
+ return false;
+}
+
+/// @brief The type signature for an optional logging function, if the user
+/// wishes to provide one. It should print, store or otherwise display the
+/// message.
+///
+/// message - A format string to log using the given parameters.
+/// ... (vargs) - the parameters for the format string.
+///
+void diagnostic_manager_t::shims_logger(const char* format, ...)
+{
+ va_list args;
+ va_start(args, format);
+
+ char buffer[256];
+ vsnprintf(buffer, 256, format, args);
+
+ DEBUG(binder_interface, "%s: %s", __FUNCTION__, buffer);
+}
+
+/// @brief The type signature for a... OpenXC TODO: not used yet.
+void diagnostic_manager_t::shims_timer()
+{}
+
+std::string diagnostic_manager_t::get_can_bus()
+{
+ return bus_;
+}
+
+active_diagnostic_request_t* diagnostic_manager_t::get_last_recurring_requests() const
+{
+ return recurring_requests_.back();
+}
+
+/// @brief Return diagnostic manager shims member.
+DiagnosticShims& diagnostic_manager_t::get_shims()
+{
+ return shims_;
+}
+
+/// @brief Search for a specific active diagnostic request in the provided requests list
+/// and erase it from the vector. This is useful at unsubscription to clean up the list otherwize
+/// all received CAN messages will be passed to DiagnosticRequestHandle of all active diagnostic request
+/// contained in the vector but no event if connected to, so we will decode uneeded request.
+///
+/// @param[in] entry - a pointer of an active_diagnostic_request instance to clean up
+/// @param[in] requests_list - a vector where to make the search and cleaning.
+void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list)
+{
+ auto i = std::find(requests_list.begin(), requests_list.end(), entry);
+ if ( i != requests_list.end())
+ requests_list.erase(i);
+}
+
+// @brief TODO: implement cancel_request if needed... Don't know.
+void diagnostic_manager_t::cancel_request(active_diagnostic_request_t* entry)
+{
+ entry->get_socket().close();
+}
+
+/// @brief Cleanup a specific request if it isn't running and get complete. As it is almost
+/// impossible to get that state for a recurring request without waiting for that, you can
+/// force the cleaning operation.
+///
+/// @param[in] entry - the request to clean
+/// @param[in] force - Force the cleaning or not ?
+void diagnostic_manager_t::cleanup_request(active_diagnostic_request_t* entry, bool force)
+{
+ if((force || (entry != nullptr && entry->response_received())))
+ {
+ char request_string[128] = {0};
+ diagnostic_request_to_string(&entry->get_handle()->request,
+ request_string, sizeof(request_string));
+ if(force && entry->get_recurring())
+ {
+ find_and_erase(entry, recurring_requests_);
+ cancel_request(entry);
+ DEBUG(binder_interface, "%s: Cancelling completed, recurring request: %s", __FUNCTION__, request_string);
+ }
+ else
+ {
+ DEBUG(binder_interface, "%s: Cancelling completed, non-recurring request: %s", __FUNCTION__, request_string);
+ find_and_erase(entry, non_recurring_requests_);
+ cancel_request(entry);
+ }
+ }
+}
+
+/// @brief Clean up all requests lists, recurring and not recurring.
+///
+/// @param[in] force - Force the cleaning or not ? If true, that will do
+/// the same effect as a call to reset().
+void diagnostic_manager_t::cleanup_active_requests(bool force)
+{
+ for(auto& entry : non_recurring_requests_)
+ if (entry != nullptr)
+ cleanup_request(entry, force);
+
+ for(auto& entry : recurring_requests_)
+ if (entry != nullptr)
+ cleanup_request(entry, force);
+}
+
+/// @brief Will return the active_diagnostic_request_t pointer for theDiagnosticRequest or nullptr if
+/// not found.
+///
+/// @param[in] request - Search key, method will go through recurring list to see if it find that request
+/// holded by the DiagnosticHandle member.
+active_diagnostic_request_t* diagnostic_manager_t::find_recurring_request(const DiagnosticRequest* request)
+{
+ for (auto& entry : recurring_requests_)
+ {
+ if(entry != nullptr)
+ {
+ if(diagnostic_request_equals(&entry->get_handle()->request, request))
+ {
+ return entry;
+ break;
+ }
+ }
+ }
+ return nullptr;
+}
+
+/// @brief Add and send a new one-time diagnostic request.
+///
+/// A one-time (aka non-recurring) request can existing in parallel with a
+/// recurring request for the same PID or mode, that's not a problem.
+///
+/// For an example, see the docs for addRecurringRequest. This function is very
+/// similar but leaves out the frequencyHz parameter.
+///
+/// @param[in] request - The parameters for the request.
+/// @param[in] name - Human readable name this response, to be used when
+/// publishing received responses. TODO: If the name is NULL, the published output
+/// will use the raw OBD-II response format.
+/// @param[in] wait_for_multiple_responses - If false, When any response is received
+/// for this request it will be removed from the active list. If true, the
+/// request will remain active until the timeout clock expires, to allow it
+/// to receive multiple response. Functional broadcast requests will always
+/// waint for the timeout, regardless of this parameter.
+/// @param[in] decoder - An optional DiagnosticResponseDecoder to parse the payload of
+/// responses to this request. If the decoder is NULL, the output will
+/// include the raw payload instead of a parsed value.
+/// @param[in] callback - An optional DiagnosticResponseCallback to be notified whenever a
+/// response is received for this request.
+///
+/// @return true if the request was added successfully. Returns false if there
+/// wasn't a free active request entry, if the frequency was too high or if the
+/// CAN acceptance filters could not be configured,
+active_diagnostic_request_t* diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::string name,
+ bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback)
+{
+ cleanup_active_requests(false);
+
+ active_diagnostic_request_t* entry = nullptr;
+
+ if (non_recurring_requests_.size() <= MAX_SIMULTANEOUS_DIAG_REQUESTS)
+ {
+ // TODO: implement Acceptance Filter
+ // if(updateRequiredAcceptanceFilters(bus, request)) {
+ active_diagnostic_request_t* entry = new active_diagnostic_request_t(bus_, request, name,
+ wait_for_multiple_responses, decoder, callback, 0);
+ entry->set_handle(shims_, request);
+
+ char request_string[128] = {0};
+ diagnostic_request_to_string(&entry->get_handle()->request, request_string,
+ sizeof(request_string));
+
+ find_and_erase(entry, non_recurring_requests_);
+ DEBUG(binder_interface, "%s: Added one-time diagnostic request on bus %s: %s", __FUNCTION__,
+ bus_.c_str(), request_string);
+
+ non_recurring_requests_.push_back(entry);
+ }
+ else
+ {
+ WARNING(binder_interface, "%s: There isn't enough request entry. Vector exhausted %d/%d", __FUNCTION__, (int)non_recurring_requests_.size(), MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ non_recurring_requests_.resize(MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ }
+ return entry;
+}
+
+bool diagnostic_manager_t::validate_optional_request_attributes(float frequencyHz)
+{
+ if(frequencyHz > MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ) {
+ DEBUG(binder_interface, "%s: Requested recurring diagnostic frequency %lf is higher than maximum of %d", __FUNCTION__,
+ frequencyHz, MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ);
+ return false;
+ }
+ return true;
+}
+
+/// @brief Add and send a new recurring diagnostic request.
+///
+/// At most one recurring request can be active for the same arbitration ID, mode
+/// and (if set) PID on the same bus at one time. If you try and call
+/// addRecurringRequest with the same key, it will return an error.
+///
+/// TODO: This also adds any neccessary CAN acceptance filters so we can receive the
+/// response. If the request is to the functional broadcast ID (0x7df) filters
+/// are added for all functional addresses (0x7e8 to 0x7f0).
+///
+/// Example:
+///
+/// // Creating a functional broadcast, mode 1 request for PID 2.
+/// DiagnosticRequest request = {
+/// arbitration_id: 0x7df,
+/// mode: 1,
+/// has_pid: true,
+/// pid: 2
+/// };
+///
+/// // Add a recurring request, to be sent at 1Hz, and published with the
+/// // name "my_pid_request"
+/// addRecurringRequest(&getConfiguration()->diagnosticsManager,
+/// canBus,
+/// &request,
+/// "my_pid_request",
+/// false,
+/// NULL,
+/// NULL,
+/// 1);
+///
+/// @param[in] request - The parameters for the request.
+/// @param[in] name - An optional human readable name this response, to be used when
+/// publishing received responses. If the name is NULL, the published output
+/// will use the raw OBD-II response format.
+/// @param[in] wait_for_multiple_responses - If false, When any response is received
+/// for this request it will be removed from the active list. If true, the
+/// request will remain active until the timeout clock expires, to allow it
+/// to receive multiple response. Functional broadcast requests will always
+/// waint for the timeout, regardless of this parameter.
+/// @param[in] decoder - An optional DiagnosticResponseDecoder to parse the payload of
+/// responses to this request. If the decoder is NULL, the output will
+/// include the raw payload instead of a parsed value.
+/// @param[in] callback - An optional DiagnosticResponseCallback to be notified whenever a
+/// response is received for this request.
+/// @param[in] frequencyHz - The frequency (in Hz) to send the request. A frequency above
+/// MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ is not allowed, and will make this
+/// function return false.
+///
+/// @return true if the request was added successfully. Returns false if there
+/// was too much already running requests, if the frequency was too high TODO:or if the
+/// CAN acceptance filters could not be configured,
+active_diagnostic_request_t* diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, const char* name,
+ bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback, float frequencyHz)
+{
+ active_diagnostic_request_t* entry = nullptr;
+
+ if(!validate_optional_request_attributes(frequencyHz))
+ return entry;
+
+ cleanup_active_requests(false);
+
+ if(find_recurring_request(request) == nullptr)
+ {
+ if(recurring_requests_.size() <= MAX_SIMULTANEOUS_DIAG_REQUESTS)
+ {
+ // TODO: implement Acceptance Filter
+ //if(updateRequiredAcceptanceFilters(bus, request)) {
+ entry = new active_diagnostic_request_t(bus_, request, name,
+ wait_for_multiple_responses, decoder, callback, frequencyHz);
+ recurring_requests_.push_back(entry);
+
+ entry->set_handle(shims_, request);
+ if(add_rx_filter(OBD2_FUNCTIONAL_BROADCAST_ID) < 0)
+ { recurring_requests_.pop_back(); }
+ else
+ {
+ start_diagnostic_request(&shims_, entry->get_handle());
+ if(event_source_ == nullptr && sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon),
+ &event_source_,
+ socket_.socket(),
+ EPOLLIN,
+ read_message,
+ nullptr) < 0)
+ {
+ cleanup_request(entry, true);
+ WARNING(binder_interface, "%s: signal: %s isn't supported. Canceling operation.", __FUNCTION__, entry->get_name().c_str());
+ return entry;
+ }
+ }
+ }
+ else
+ {
+ WARNING(binder_interface, "%s: There isn't enough request entry. Vector exhausted %d/%d", __FUNCTION__, (int)recurring_requests_.size(), MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ recurring_requests_.resize(MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ }
+ }
+ else
+ { DEBUG(binder_interface, "%s: Can't add request, one already exists with same key", __FUNCTION__);}
+ return entry;
+}
+
+/// @brief Will decode the diagnostic response and build the final openxc_VehicleMessage to return.
+///
+/// @param[in] adr - A pointer to an active diagnostic request holding a valid diagnostic handle
+/// @param[in] response - The response to decode from which the Vehicle message will be built and returned
+///
+/// @return A filled openxc_VehicleMessage or a zeroed struct if there is an error.
+openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_response(active_diagnostic_request_t* adr, const DiagnosticResponse& response)
+{
+ openxc_VehicleMessage message = build_VehicleMessage();
+ float value = (float)diagnostic_payload_to_integer(&response);
+ if(adr->get_decoder() != nullptr)
+ {
+ value = adr->get_decoder()(&response, value);
+ }
+
+ if((response.success && adr->get_name().size()) > 0)
+ {
+ // If name, include 'value' instead of payload, and leave of response
+ // details.
+ message = build_VehicleMessage(build_SimpleMessage(adr->get_name(), build_DynamicField(value)));
+ }
+ else
+ {
+ // If no name, send full details of response but still include 'value'
+ // instead of 'payload' if they provided a decoder. The one case you
+ // can't get is the full detailed response with 'value'. We could add
+ // another parameter for that but it's onerous to carry that around.
+ message = build_VehicleMessage(adr, response, value);
+ }
+
+ // If not success but completed then the pid isn't supported
+ if(!response.success)
+ {
+ struct utils::signals_found found_signals;
+ found_signals = utils::signals_manager_t::instance().find_signals(build_DynamicField(adr->get_name()));
+ found_signals.diagnostic_messages.front()->set_supported(false);
+ cleanup_request(adr, true);
+ NOTICE(binder_interface, "%s: PID not supported or ill formed. Please unsubscribe from it. Error code : %d", __FUNCTION__, response.negative_response_code);
+ message = build_VehicleMessage(build_SimpleMessage(adr->get_name(), build_DynamicField("This PID isn't supported by your vehicle.")));
+ }
+
+ if(adr->get_callback() != nullptr)
+ {
+ adr->get_callback()(adr, &response, value);
+ }
+
+ // Reset the completed flag handle to make sure that it will be reprocessed the next time.
+ adr->get_handle()->success = false;
+ return message;
+}
+
+/// @brief Will take the CAN message and pass it to the receive functions that will process
+/// diagnostic handle for each active diagnostic request then depending on the result we will
+/// return pass the diagnostic response to decode it.
+///
+/// @param[in] entry - A pointer to an active diagnostic request holding a valid diagnostic handle
+/// @param[in] cm - A raw CAN message.
+///
+/// @return A pointer to a filled openxc_VehicleMessage or a nullptr if nothing has been found.
+openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_handle(active_diagnostic_request_t* entry, const can_message_t& cm)
+{
+ DiagnosticResponse response = diagnostic_receive_can_frame(&shims_, entry->get_handle(), cm.get_id(), cm.get_data(), cm.get_length());
+ if(response.completed && entry->get_handle()->completed)
+ {
+ if(entry->get_handle()->success)
+ return relay_diagnostic_response(entry, response);
+ }
+ else if(!response.completed && response.multi_frame)
+ {
+ // Reset the timeout clock while completing the multi-frame receive
+ entry->get_timeout_clock().tick(
+ entry->get_timeout_clock().get_time_function()());
+ }
+
+ return build_VehicleMessage();
+}
+
+/// @brief Find the active diagnostic request with the correct DiagnosticRequestHandle
+/// member that will understand the CAN message using diagnostic_receive_can_frame function
+/// from UDS-C library. Then decode it with an ad-hoc method.
+///
+/// @param[in] cm - Raw CAN message received
+///
+/// @return VehicleMessage with decoded value.
+openxc_VehicleMessage diagnostic_manager_t::find_and_decode_adr(const can_message_t& cm)
+{
+ openxc_VehicleMessage vehicle_message = build_VehicleMessage();
+
+ for ( auto entry : non_recurring_requests_)
+ {
+ vehicle_message = relay_diagnostic_handle(entry, cm);
+ if (is_valid(vehicle_message))
+ return vehicle_message;
+ }
+
+ for ( auto entry : recurring_requests_)
+ {
+ vehicle_message = relay_diagnostic_handle(entry, cm);
+ if (is_valid(vehicle_message))
+ return vehicle_message;
+ }
+
+ return vehicle_message;
+}
+
+/// @brief Tell if the CAN message received is a diagnostic response.
+/// Request broadcast ID use 0x7DF and assigned ID goes from 0x7E0 to Ox7E7. That allows up to 8 ECU to respond
+/// at the same time. The response is the assigned ID + 0x8, so response ID can goes from 0x7E8 to 0x7EF.
+///
+/// @param[in] cm - CAN message received from the socket.
+///
+/// @return True if the active diagnostic request match the response.
+bool diagnostic_manager_t::is_diagnostic_response(const can_message_t& cm)
+{
+ if (cm.get_id() >= 0x7e8 && cm.get_id() <= 0x7ef)
+ return true;
+ return false;
+}
diff --git a/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp
new file mode 100644
index 00000000..894cf630
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <systemd/sd-event.h>
+#include <map>
+#include <vector>
+
+#include "../utils/socketcan-bcm.hpp"
+#include "uds/uds.h"
+#include "openxc.pb.h"
+#include "../can/can-bus.hpp"
+#include "active-diagnostic-request.hpp"
+
+/// Each CAN bus needs its own set of shim functions, so this should
+/// match the maximum CAN controller count.
+///
+#define MAX_SHIM_COUNT can_bus_t.get_can_devices().size()
+#define DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET 0x8
+
+class active_diagnostic_request_t;
+
+///
+/// @brief The core structure for running the diagnostics module by the binding.
+///
+/// This stores details about the active requests and shims required to connect
+/// the diagnostics library to the CAN device.
+///
+class diagnostic_manager_t {
+private:
+ DiagnosticShims shims_; /*!< shims_ - Shim functions for each CAN bus that plug the diagnostics
+ * library (uds-c) into the VI's CAN peripheral.*/
+ std::string bus_; /*!< bus_ - A pointer to the CAN bus that should be used for all standard OBD-II requests, if the bus is not
+ * explicitly spcified in the request. Default to the first bus CAN at initialization.*/
+ std::vector<active_diagnostic_request_t*> recurring_requests_; /*!< recurringRequests - A list of active recurring diagnostic requests.*/
+ std::vector<active_diagnostic_request_t*> non_recurring_requests_; /*!< nonrecurringRequests - A list of active one-time diagnostic requests. When a
+ * response is received for a non-recurring request or it times out, it is removed*/
+ bool initialized_; /*!< * initialized - True if the DiagnosticsManager has been initialized with shims. It will interface with the uds-c lib*/
+ utils::socketcan_bcm_t socket_; ///< socket_ - a BCM socket with 8 RX_SETUP jobs for the 8 CAN ID on which ECU could respond.
+ struct sd_event_source* event_source_;
+
+ void init_diagnostic_shims();
+ void reset();
+ int add_rx_filter(uint32_t can_id);
+
+ static bool shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size);
+ static void shims_logger(const char* m, ...);
+ static void shims_timer();
+public:
+ diagnostic_manager_t();
+
+ bool initialize();
+
+ utils::socketcan_bcm_t& get_socket();
+ std::string get_can_bus();
+ active_diagnostic_request_t* get_last_recurring_requests() const;
+ DiagnosticShims& get_shims();
+
+ void find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list);
+ void cancel_request(active_diagnostic_request_t* entry);
+ void cleanup_request(active_diagnostic_request_t* entry, bool force);
+ void cleanup_active_requests(bool force);
+ active_diagnostic_request_t* find_recurring_request(const DiagnosticRequest* request);
+
+ // Subscription parts
+ active_diagnostic_request_t* add_request(DiagnosticRequest* request, const std::string name,
+ bool waitForMultipleResponses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback);
+ bool validate_optional_request_attributes(float frequencyHz);
+ active_diagnostic_request_t* add_recurring_request(DiagnosticRequest* request, const char* name,
+ bool waitForMultipleResponses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback, float frequencyHz);
+
+ // Decoding part
+ openxc_VehicleMessage relay_diagnostic_response(active_diagnostic_request_t* adr, const DiagnosticResponse& response);
+ openxc_VehicleMessage relay_diagnostic_handle(active_diagnostic_request_t* entry, const can_message_t& cm);
+ openxc_VehicleMessage find_and_decode_adr(const can_message_t& cm);
+ bool is_diagnostic_response(const can_message_t& cm);
+};
diff --git a/CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp b/CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp
new file mode 100644
index 00000000..4455aa89
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <map>
+
+#include "diagnostic-message.hpp"
+
+#include "../binding/configuration.hpp"
+#include "../utils/signals.hpp"
+
+const char *UNIT_NAMES[10] = {
+ "POURCENT",
+ "DEGREES_CELSIUS",
+ "KPA",
+ "RPM",
+ "GRAMS_SEC",
+ "SECONDS",
+ "KM",
+ "KM_H",
+ "PA",
+ "NM"
+};
+
+diagnostic_message_t::diagnostic_message_t(uint8_t pid, const std::string generic_name, const int min,
+ const int max, enum UNIT unit, float frequency, DiagnosticResponseDecoder decoder,
+ DiagnosticResponseCallback callback, bool supported)
+ : pid_{pid}, generic_name_{generic_name}, min_{min}, max_{max}, unit_{unit},
+ frequency_{frequency}, decoder_{decoder}, callback_{callback}, supported_{supported}
+{}
+
+uint32_t diagnostic_message_t::get_pid()
+{
+ return (uint32_t)pid_;
+}
+
+const std::string diagnostic_message_t::get_generic_name() const
+{
+ return generic_name_;
+}
+
+const std::string diagnostic_message_t::get_name() const
+{
+ return active_diagnostic_request_t::get_prefix() + "." + generic_name_;
+}
+
+float diagnostic_message_t::get_frequency() const
+{
+ return frequency_;
+}
+
+DiagnosticResponseDecoder diagnostic_message_t::get_decoder() const
+{
+ return decoder_;
+}
+DiagnosticResponseCallback diagnostic_message_t::get_callback() const
+{
+ return callback_;
+}
+
+bool diagnostic_message_t::get_supported() const
+{
+ return supported_;
+}
+
+void diagnostic_message_t::set_supported(bool value)
+{
+ supported_ = value;
+}
+
+void diagnostic_message_t::set_parent(can_message_set_t* parent)
+{
+ parent_ = parent;
+}
+
+///
+/// @brief Build a DiagnosticRequest struct to be passed
+/// to diagnostic manager instance.
+///
+const DiagnosticRequest diagnostic_message_t::build_diagnostic_request()
+{
+ return {/*arbitration_id: */OBD2_FUNCTIONAL_BROADCAST_ID,
+ /*mode: */0x1,
+ /*has_pid: */true,
+ /*pid: */pid_,
+ /*pid_length: */0,
+ /*payload[]: */{0},
+ /*payload_length: */0,
+ /*no_frame_padding: */false,
+ /*DiagnosticRequestType: */DiagnosticRequestType::DIAGNOSTIC_REQUEST_TYPE_PID };
+}
+
+///
+/// @brief Check if a request is an OBD-II PID request.
+///
+/// @return true if the request is a mode 1 request and it has a 1 byte PID.
+///
+bool diagnostic_message_t::is_obd2_request(const DiagnosticRequest* request)
+{
+ return request->mode == 0x1 && request->has_pid && request->pid < 0xff;
+}
diff --git a/CAN-binder/low-can-binding/diagnostic/diagnostic-message.hpp b/CAN-binder/low-can-binding/diagnostic/diagnostic-message.hpp
new file mode 100644
index 00000000..ac427b97
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-message.hpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <vector>
+#include <string>
+
+#include "uds/uds.h"
+#include "../can/can-message-set.hpp"
+#include "../can/can-message.hpp"
+#include "active-diagnostic-request.hpp"
+
+enum UNIT {
+ POURCENT,
+ DEGREES_CELSIUS,
+ KPA,
+ RPM,
+ GRAMS_SEC,
+ SECONDS,
+ KM,
+ KM_H,
+ PA,
+ NM,
+ INVALID
+};
+
+class can_message_set_t;
+
+///
+/// @brief - A representation of an OBD-II PID.
+///
+class diagnostic_message_t
+{
+ private:
+ can_message_set_t* parent_; /*!< parent_ - Pointer to the CAN message set holding this diagnostic message */
+ uint8_t pid_; /*!< pid_ - The 1 byte PID.*/
+ std::string generic_name_; /*!< generic_name_ - A human readable name to use for this PID when published.*/
+ int min_; /*!< min_ - Minimum value that can take this pid */
+ int max_; /*!< max_ - Maximum value that can take this pid */
+ enum UNIT unit_; /*!< unit_ : Which unit system is used by that pid. See enum UNIT above.*/
+ float frequency_; /*!< frequency_ - The frequency to request this PID if supported by the vehicle when automatic, recurring OBD-II requests are enabled.*/
+ DiagnosticResponseDecoder decoder_; /*!< decoder_ - An optional DiagnosticResponseDecoder to parse the payload of responses
+ * to this request. If the decoder is NULL, the output will include the raw payload
+ * instead of a parsed value.*/
+ DiagnosticResponseCallback callback_; /*!< callback_ - An optional DiagnosticResponseCallback to be notified whenever a
+ * response is received for this request.*/
+
+ bool supported_; /*!< supported_ - boolean indicating whether this pid is supported by the vehicle or not.*/
+
+ public:
+ const char* generic_name = generic_name_.c_str();
+ diagnostic_message_t(uint8_t pid, const std::string generic_name, const int min, const int max, enum UNIT unit, float frequency,
+ DiagnosticResponseDecoder decoder, DiagnosticResponseCallback callback, bool supported);
+
+ uint32_t get_pid();
+ const std::string get_generic_name() const;
+ const std::string get_name() const;
+ float get_frequency() const;
+ DiagnosticResponseDecoder get_decoder() const;
+ DiagnosticResponseCallback get_callback() const;
+ bool get_supported() const;
+
+ void set_supported(bool value);
+ void set_parent(can_message_set_t* parent);
+ const DiagnosticRequest build_diagnostic_request();
+
+ bool is_obd2_response(const can_message_t& can_message);
+ bool is_obd2_request(const DiagnosticRequest *request);
+};
diff --git a/CAN-binder/low-can-binding/utils/config-parser.cpp b/CAN-binder/low-can-binding/utils/config-parser.cpp
new file mode 100644
index 00000000..3f338121
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/config-parser.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config-parser.hpp"
+
+#include "../binding/low-can-hat.hpp"
+
+namespace utils
+{
+ /// @brief constructor using path to file
+ config_parser_t::config_parser_t(std::string conf_file)
+ : filepath_{conf_file}, config_content_{}
+ {
+ config_content_.read_file(conf_file);
+ }
+
+ const std::string& config_parser_t::filepath() const
+ {
+ return filepath_;
+ }
+
+ /// @brief read the conf_file_ and parse it into an INIReader object
+ /// to search into later.
+ bool config_parser_t::check_conf()
+ {
+ if (config_content_.size() <= 0)
+ {
+ ERROR(binder_interface, "%s: Can't load the INI config file.", __FUNCTION__);
+ return false;
+ }
+ DEBUG(binder_interface, "%s: Configuration file parsed", __FUNCTION__);
+ return true;
+ }
+
+ /// @brief Public method to access devices_name_ vector. If vector size equal 0
+ /// then it will parses the configuration file content to fill it. It could be empty even
+ /// after parsing if content file just don't have a correct "canbus" directive so you
+ /// have to test the returned value.
+ ///
+ /// @return A const vector with string of linux CAN devices.
+ const std::vector<std::pair<std::string, std::string> > config_parser_t::get_devices_name()
+ {
+ std::vector<std::pair<std::string, std::string> > devices_name;
+
+ std::map<std::string, std::string> bus_mapping = config_content_.get_keys("CANbus-mapping");
+ for(const auto& busIt : bus_mapping )
+ {
+ devices_name.push_back(std::make_pair(busIt.first, busIt.second));
+ }
+
+ return devices_name;
+ }
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/config-parser.hpp b/CAN-binder/low-can-binding/utils/config-parser.hpp
new file mode 100644
index 00000000..04531108
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/config-parser.hpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include <utility>
+#include "ini-config.hpp"
+
+namespace utils
+{
+ /// @brief A configuration file parser that handle INI configuration
+ /// file format.
+ class config_parser_t
+ {
+ private:
+ const std::string filepath_; /*!< filepath_ - Path to the config file*/
+ ini_config config_content_; /*!< config_content_ - Parsed content of INI file.*/
+
+ public:
+ config_parser_t(config_parser_t&&) = default;
+ config_parser_t(const config_parser_t&) = default;
+ config_parser_t(std::string conf_file);
+
+ const std::string& filepath() const;
+ bool check_conf();
+ const std::vector<std::pair<std::string, std::string> > get_devices_name();
+ };
+}
diff --git a/CAN-binder/low-can-binding/utils/openxc-utils.cpp b/CAN-binder/low-can-binding/utils/openxc-utils.cpp
new file mode 100644
index 00000000..286b2f53
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/openxc-utils.cpp
@@ -0,0 +1,284 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loic Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "openxc-utils.hpp"
+
+#include "../binding/configuration.hpp"
+
+///
+/// @brief Build a specific VehicleMessage containing a DiagnosticResponse.
+///
+/// @param[in] request - Original request use to retrieve decoder and callback
+/// @param[in] response - Response to the request that will be decoded if decoder set
+/// and put into the DiagnosticResponse of the VehicleMessage.
+/// @param[in] parsed_value - raw parsed value of the payload from CAN message
+///
+/// @return a vehicle message including simple message that will be convert into
+/// a JSON object before being pushed to the subscribers
+///
+openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
+{
+ openxc_VehicleMessage message;
+ configuration_t& conf = configuration_t::instance();
+
+ message.has_type = true;
+ message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC;
+ message.has_diagnostic_response = true;
+ message.diagnostic_response.has_bus = true;
+ message.diagnostic_response.bus = conf.get_can_bus_manager().get_can_device_index(
+ conf.get_diagnostic_manager().get_can_bus());
+ message.diagnostic_response.has_message_id = true;
+
+ if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID)
+ {
+ message.diagnostic_response.message_id = response.arbitration_id
+ - DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
+ }
+ else
+ {
+ // must preserve responding arb ID for responses to functional broadcast
+ // requests, as they are the actual module address and not just arb ID +
+ // 8.
+ message.diagnostic_response.message_id = response.arbitration_id;
+ }
+
+ message.diagnostic_response.has_mode = true;
+ message.diagnostic_response.mode = response.mode;
+ message.diagnostic_response.has_pid = response.has_pid;
+ if(message.diagnostic_response.has_pid)
+ message.diagnostic_response.pid = response.pid;
+ message.diagnostic_response.has_success = true;
+ message.diagnostic_response.success = response.success;
+ message.diagnostic_response.has_negative_response_code = !response.success;
+ message.diagnostic_response.negative_response_code =
+ response.negative_response_code;
+
+ if(response.payload_length > 0)
+ {
+ if(request->get_decoder() != nullptr)
+ {
+ message.diagnostic_response.has_value = true;
+ message.diagnostic_response.value = parsed_value;
+ }
+ else
+ {
+ message.diagnostic_response.has_payload = true;
+ ::memcpy(message.diagnostic_response.payload.bytes, response.payload,
+ response.payload_length);
+ message.diagnostic_response.payload.size = response.payload_length;
+ }
+ }
+
+ return message;
+}
+
+///
+/// @brief Build a specific VehicleMessage containing a SimpleMessage.
+///
+/// @param[in] message - simple message to include into openxc_VehicleMessage
+///
+/// @return a vehicle message including simple message that will be convert into
+/// a JSON object before being pushed to the subscribers
+///
+openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
+{
+ openxc_VehicleMessage v;
+
+ v.has_type = true,
+ v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
+ v.has_simple_message = true;
+ v.simple_message = message;
+ v.has_timestamp = true;
+ v.timestamp = system_time_ms();
+
+ return v;
+}
+
+///
+/// @brief Build an empty VehicleMessage that isn't usable by at least the struct
+/// is initialized for the most part and can be use to check a false return value.
+///
+/// @return A VehicleMessage with all boolean value to false.
+///
+openxc_VehicleMessage build_VehicleMessage()
+{
+ openxc_VehicleMessage v;
+
+ ::memset(&v, 0, sizeof(openxc_VehicleMessage));
+ return v;
+}
+
+bool is_valid(const openxc_VehicleMessage& v)
+{
+ if (v.has_type == false &&
+ v.has_can_message == false &&
+ v.has_simple_message == false &&
+ v.has_diagnostic_response == false &&
+ v.has_control_command == false &&
+ v.has_command_response == false &&
+ v.has_timestamp == false)
+ return false;
+ return true;
+}
+
+///
+/// @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField
+///
+/// @param[in] name - const string reference name to assign to the created SimpleMessage
+/// this will set has_name member to true and assign name to the name member. Maximum size for name is
+/// set to 100 char.
+/// @param[in] value - const reference with DynamicField to assign to SimpleMessage
+/// value.
+///
+/// @return an openxc_SimpleMessage struct initialized with name and value provided.
+///
+openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
+{
+ openxc_SimpleMessage s;
+
+ s.has_name = true;
+ ::strncpy(s.name, name.c_str(), 100);
+ s.has_value = true;
+ s.value = value;
+
+ return s;
+}
+
+///
+/// @brief Build an openxc_DynamicField with a string value
+///
+/// @param[in] value - const string reference value to assign to builded
+/// openxc_DynamicField.
+///
+/// @return openxc_DynamicField initialized with a string value.
+///
+openxc_DynamicField build_DynamicField(const std::string& value)
+{
+ openxc_DynamicField d;
+ d.has_type = true;
+ d.type = openxc_DynamicField_Type_STRING;
+
+ d.has_string_value = true;
+ d.has_numeric_value = false;
+ d.has_boolean_value = false;
+ ::strncpy(d.string_value, value.c_str(), 100);
+
+ return d;
+}
+
+///
+/// @fn openxc_DynamicField build_DynamicField(double value);
+///
+/// @brief Build an openxc_DynamicField with a double value
+///
+/// @param[in] value - double value to assign to builded openxc_DynamicField.
+///
+/// @return openxc_DynamicField initialized with a double value.
+///
+openxc_DynamicField build_DynamicField(double value)
+{
+ openxc_DynamicField d;
+ d.has_type = true;
+ d.type = openxc_DynamicField_Type_NUM;
+
+ d.has_string_value = false;
+ d.has_numeric_value = true;
+ d.has_boolean_value = false;
+ d.numeric_value = value;
+
+ return d;
+}
+
+///
+/// @brief Build an openxc_DynamicField with a boolean value
+///
+/// @param[in] value - boolean value to assign to builded openxc_DynamicField.
+///
+/// @return openxc_DynamicField initialized with a boolean value.
+///
+openxc_DynamicField build_DynamicField(bool value)
+{
+ openxc_DynamicField d;
+ d.has_type = true;
+ d.type = openxc_DynamicField_Type_BOOL;
+
+ d.has_string_value = false;
+ d.has_numeric_value = false;
+ d.has_boolean_value = true;
+ d.boolean_value = value;
+
+ return d;
+}
+
+///
+/// @brief Extract the simple message value from an openxc_VehicleMessage
+/// and return it. If there isn't SimpleMessage in the VehicleMessage then
+/// returned value will be a SimpleMessage with all field set at false.
+/// DynamicField from SimpleMessage will be boolean DynamicField set to false too.
+///
+/// @param[in] v_msg - const reference to openxc_VehicleMessage
+///
+/// @return A simpleMessage from the provided VehicleMessage.
+///
+openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
+{
+ if (v_msg.has_simple_message)
+ return v_msg.simple_message;
+
+ openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
+ return s_msg;
+}
+
+///
+/// @brief Make a JSON object from a DynamicField
+///
+/// @param[in] field - openxc_DynamicField struct to convert into
+/// a json object.
+/// @param[out] value - pointer to the object to set up.
+///
+void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
+{
+ if(field.has_numeric_value)
+ json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
+ else if(field.has_boolean_value)
+ json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
+ else if(field.has_string_value)
+ json_object_object_add(value, "value", json_object_new_string(field.string_value));
+}
+
+///
+/// @brief Make a JSON object from a SimpleMessage
+///
+/// @param[in] s_msg - const reference to an openxc_SimpleMessage
+/// struct to convert into a json object.
+/// @param[out] json - pointer with the DynamicField converted into json object
+///
+/// @return True if SimpleMessage has been transformed into json object
+/// and false if not. In such case, a json object is returned { "error": "error msg"}
+///
+bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
+{
+ if(s_msg.has_name)
+ {
+ json_object_object_add(json, "name", json_object_new_string(s_msg.name));
+ jsonify_DynamicField(s_msg.value, json);
+ return true;
+ }
+ json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
+ return false;
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/openxc-utils.hpp b/CAN-binder/low-can-binding/utils/openxc-utils.hpp
new file mode 100644
index 00000000..d36b359b
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/openxc-utils.hpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loic Collignon" <loic.collignon@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <json-c/json.h>
+#include <sys/timeb.h>
+
+#include "openxc.pb.h"
+#include "../diagnostic/active-diagnostic-request.hpp"
+
+openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value);
+openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message);
+openxc_VehicleMessage build_VehicleMessage();
+bool is_valid(const openxc_VehicleMessage& v);
+
+openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value);
+openxc_DynamicField build_DynamicField(const std::string& value);
+openxc_DynamicField build_DynamicField(double value);
+openxc_DynamicField build_DynamicField(bool value);
+
+openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg);
+
+void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value);
+
+bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json); \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/signals.cpp b/CAN-binder/low-can-binding/utils/signals.cpp
new file mode 100644
index 00000000..261f572b
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/signals.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "signals.hpp"
+
+namespace utils
+{
+ signals_manager_t::signals_manager_t()
+ {}
+
+ /// @brief Return singleton instance of configuration object.
+ signals_manager_t& signals_manager_t::instance()
+ {
+ static signals_manager_t sm;
+ return sm;
+ }
+
+ /// @brief Return Subscribed signals map mutex.
+ std::mutex& signals_manager_t::get_subscribed_signals_mutex()
+ {
+ return subscribed_signals_mutex_;
+ }
+
+ ///
+ /// @brief return the subscribed_signals map.
+ ///
+ /// @return Map of subscribed signals.
+ std::map<std::string, struct afb_event>& signals_manager_t::get_subscribed_signals()
+ {
+ return subscribed_signals_;
+ }
+
+ ///
+ /// @fn std::vector<std::string> find_signals(const openxc_DynamicField &key)
+ /// @brief return signals name found searching through CAN_signals and OBD2 pid
+ ///
+ /// @param[in] key : can contain numeric or string value in order to search against
+ /// can signals or obd2 signals name.
+ ///
+ /// @return Vector of signals name found.
+ ///
+ struct signals_found signals_manager_t::find_signals(const openxc_DynamicField &key)
+ {
+ struct signals_found sf;
+
+ switch(key.type)
+ {
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
+ lookup_signals_by_name(key.string_value, configuration_t::instance().get_all_can_signals(), sf.can_signals);
+ lookup_signals_by_name(key.string_value, configuration_t::instance().get_diagnostic_messages(), sf.diagnostic_messages);
+ break;
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
+ lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_all_can_signals(), sf.can_signals);
+ lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_diagnostic_messages(), sf.diagnostic_messages);
+ break;
+ default:
+ ERROR(binder_interface, "%s: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.", __FUNCTION__);
+ break;
+ }
+ DEBUG(binder_interface, "%s: Found %d signal(s)", __FUNCTION__, (int)(sf.can_signals.size() + sf.diagnostic_messages.size()));
+ return sf;
+ }
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/signals.hpp b/CAN-binder/low-can-binding/utils/signals.hpp
new file mode 100644
index 00000000..358ddbe6
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/signals.hpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <vector>
+#include <string>
+#include <fnmatch.h>
+
+#include "openxc.pb.h"
+#include "../binding/configuration.hpp"
+#include "../can/can-signals.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+
+#include "../binding/low-can-hat.hpp"
+
+namespace utils
+{
+ struct signals_found
+ {
+ std::vector<std::shared_ptr<can_signal_t> > can_signals;
+ std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages;
+ };
+
+ class signals_manager_t
+ {
+ private:
+ std::mutex subscribed_signals_mutex_;
+ std::map<std::string, struct afb_event> subscribed_signals_;
+
+ signals_manager_t(); ///< Private constructor to make singleton class.
+
+ public:
+ static signals_manager_t& instance();
+
+ std::mutex& get_subscribed_signals_mutex();
+ std::map<std::string, struct afb_event>& get_subscribed_signals();
+
+ struct signals_found find_signals(const openxc_DynamicField &key);
+ void find_diagnostic_messages(const openxc_DynamicField &key, std::vector<std::shared_ptr<diagnostic_message_t> >& found_signals);
+ void find_can_signals(const openxc_DynamicField &key, std::vector<std::shared_ptr<can_signal_t> >& found_signals);
+
+ template <typename T>
+ void lookup_signals_by_name(const std::string& key, std::vector<std::shared_ptr<T> > signals, std::vector<std::shared_ptr<T> >& found_signals)
+ {
+ for(std::shared_ptr<T> s : signals)
+ {
+ if(::fnmatch(key.c_str(), s->get_generic_name().c_str(), FNM_CASEFOLD) == 0)
+ found_signals.push_back(s);
+ else if(::fnmatch(key.c_str(), s->get_name().c_str(), FNM_CASEFOLD) == 0)
+ found_signals.push_back(s);
+ }
+ }
+
+ template <typename T>
+ void lookup_signals_by_id(const double key, std::vector<std::shared_ptr<T> > signals, std::vector<std::shared_ptr<T> >& found_signals)
+ {
+ for(std::shared_ptr<T> s : signals)
+ {
+ if(configuration_t::instance().get_signal_id(*s) == key)
+ {
+ found_signals.push_back(s);
+ }
+ }
+ }
+ };
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/socketcan-bcm.cpp b/CAN-binder/low-can-binding/utils/socketcan-bcm.cpp
new file mode 100644
index 00000000..38f48522
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan-bcm.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "socketcan-bcm.hpp"
+
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+namespace utils
+{
+ /// @brief Connect the socket.
+ /// @return 0 if success.
+ int socketcan_bcm_t::connect(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
+ }
+
+ /// @brief Open a raw socket CAN.
+ /// @param[in] device_name is the kernel network device name of the CAN interface.
+ ///
+ /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
+ int socketcan_bcm_t::open(std::string device_name)
+ {
+ close();
+
+ struct ifreq ifr;
+ socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_BCM);
+
+ // Attempts to open a socket to CAN bus
+ ::strcpy(ifr.ifr_name, device_name.c_str());
+ DEBUG(binder_interface, "%s: BCM socket ifr_name is : %s", __FUNCTION__, ifr.ifr_name);
+ if(::ioctl(socket_, SIOCGIFINDEX, &ifr) < 0)
+ {
+ ERROR(binder_interface, "%s: ioctl failed. Error was : %s", __FUNCTION__, strerror(errno));
+ close();
+ }
+ else
+ {
+ tx_address_.can_family = AF_CAN;
+ tx_address_.can_ifindex = ifr.ifr_ifindex;
+
+ if(connect((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
+ close();
+ }
+ // Needed because of using systemD event loop. See sd_event_add_io manual.
+ fcntl(socketcan_t::socket_, F_SETFL, O_NONBLOCK);
+ }
+ return socket_;
+ }
+
+ socketcan_bcm_t& operator>>(socketcan_bcm_t& s, can_message_t& cm)
+ {
+ struct {
+ struct bcm_msg_head msg_head;
+ struct can_frame frames;
+ } msg;
+
+ const struct sockaddr_can& addr = s.get_tx_address();
+ socklen_t addrlen = sizeof(addr);
+ struct ifreq ifr;
+
+ ssize_t nbytes = ::recvfrom(s.socket(), &msg, sizeof(msg), 0, (struct sockaddr*)&addr, &addrlen);
+ ifr.ifr_ifindex = addr.can_ifindex;
+ ioctl(s.socket(), SIOCGIFNAME, &ifr);
+
+ DEBUG(binder_interface, "Data available: %i bytes read", (int)nbytes);
+ DEBUG(binder_interface, "read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, msg.msg_head.can_id, msg.frames.can_dlc,
+ msg.frames.data[0], msg.frames.data[1], msg.frames.data[2], msg.frames.data[3], msg.frames.data[4], msg.frames.data[5], msg.frames.data[6], msg.frames.data[7]);
+
+ struct timeval tv;
+ ioctl(s.socket(), SIOCGSTAMP, &tv);
+ uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+ cm = ::can_message_t::convert_from_frame(msg.frames ,
+ nbytes-sizeof(struct bcm_msg_head),
+ timestamp);
+
+ return s;
+ }
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/socketcan-bcm.hpp b/CAN-binder/low-can-binding/utils/socketcan-bcm.hpp
new file mode 100644
index 00000000..2e4547ba
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan-bcm.hpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "socketcan.hpp"
+#include "../can/can-message.hpp"
+
+namespace utils
+{
+ struct simple_bcm_msg
+ {
+ struct bcm_msg_head msg_head;
+ struct can_frame frames;
+ };
+
+ struct canfd_bcm_msg
+ {
+ struct bcm_msg_head msg_head;
+ struct canfd_frame frames;
+ canfd_bcm_msg() { msg_head.flags |= CAN_FD_FRAME; }
+ };
+
+ class socketcan_bcm_t : public socketcan_t
+ {
+ public:
+ using socketcan_t::socketcan_t;
+
+ virtual int open(std::string device_name);
+
+ private:
+ int connect(const struct sockaddr* addr, socklen_t len);
+ };
+
+ socketcan_bcm_t& operator>>(socketcan_bcm_t& s, can_message_t& cm);
+// socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const struct simple_bcm_msg& obj);
+// socketcan_bcm_t& operator<<(socketcan_bcm_t& s, const struct canfd_bcm_msg& obj);
+}
diff --git a/CAN-binder/low-can-binding/utils/socketcan-raw.cpp b/CAN-binder/low-can-binding/utils/socketcan-raw.cpp
new file mode 100644
index 00000000..a35a99d4
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan-raw.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "socketcan-raw.hpp"
+
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+namespace utils
+{
+ /// @brief Bind the socket.
+ /// @return 0 if success.
+ int socketcan_raw_t::bind(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
+ }
+
+ /// @brief Open a raw socket CAN.
+ /// @param[in] device_name is the kernel network device name of the CAN interface.
+ ///
+ /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
+ int socketcan_raw_t::open(std::string device_name)
+ {
+ close();
+
+ struct ifreq ifr;
+ socket_ = socketcan_t::open(PF_CAN, SOCK_RAW, CAN_RAW);
+
+ // Attempts to open a socket to CAN bus
+ ::strcpy(ifr.ifr_name, device_name.c_str());
+ DEBUG(binder_interface, "%s: ifr_name is : %s", __FUNCTION__, ifr.ifr_name);
+ if(::ioctl(socket_, SIOCGIFINDEX, &ifr) < 0)
+ {
+ ERROR(binder_interface, "%s: ioctl failed. Error was : %s", __FUNCTION__, strerror(errno));
+ close();
+ }
+ else
+ {
+ tx_address_.can_family = AF_CAN;
+ tx_address_.can_ifindex = ifr.ifr_ifindex;
+
+ if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ ERROR(binder_interface, "%s: Bind failed. %s", __FUNCTION__, strerror(errno));
+ close();
+ }
+ }
+ return socket_;
+ }
+
+ socketcan_raw_t& operator>>(socketcan_raw_t& s, can_message_t& cm)
+ {
+ struct canfd_frame frame;
+
+ const struct sockaddr_can& addr = s.get_tx_address();
+ socklen_t addrlen = sizeof(addr);
+ struct ifreq ifr;
+
+ ssize_t nbytes = ::recvfrom(s.socket(), &frame, sizeof(frame), 0, (struct sockaddr*)&addr, &addrlen);
+ ifr.ifr_ifindex = addr.can_ifindex;
+ ioctl(s.socket(), SIOCGIFNAME, &ifr);
+
+ DEBUG(binder_interface, "Data available: %i bytes read", (int)nbytes);
+ DEBUG(binder_interface, "read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, frame.can_id, frame.len,
+ frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
+
+ struct timeval tv;
+ ioctl(s.socket(), SIOCGSTAMP, &tv);
+ uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+ cm = ::can_message_t::convert_from_frame(frame , nbytes, timestamp);
+
+ return s;
+ }
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/socketcan-raw.hpp b/CAN-binder/low-can-binding/utils/socketcan-raw.hpp
new file mode 100644
index 00000000..61672411
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan-raw.hpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "socketcan.hpp"
+#include "../can/can-message.hpp"
+
+namespace utils
+{
+ class socketcan_raw_t : public socketcan_t
+ {
+ public:
+ using socketcan_t::socketcan_t;
+ virtual int open(std::string device_name);
+
+ private:
+ int bind(const struct sockaddr* addr, socklen_t len);
+ };
+
+ socketcan_raw_t& operator>>(socketcan_raw_t& s, can_message_t& cm);
+}
diff --git a/CAN-binder/low-can-binding/utils/socketcan.cpp b/CAN-binder/low-can-binding/utils/socketcan.cpp
new file mode 100644
index 00000000..d66822de
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <unistd.h>
+#include <string>
+#include <linux/can/raw.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+
+#include "socketcan.hpp"
+
+namespace utils
+{
+
+ /// @brief Construct a default, invalid, socket.
+ socketcan_t::socketcan_t()
+ : socket_{INVALID_SOCKET}
+ {}
+
+ /// @brief Construct a socket by copying an existing one.
+ socketcan_t::socketcan_t(const socketcan_t& s)
+ : socket_{s.socket_}
+ {}
+
+ /// @brief Construct a socket by moving an existing one.
+ socketcan_t::socketcan_t(socketcan_t&& s)
+ : socket_{s.socket_}
+ {
+ s.socket_ = INVALID_SOCKET;
+ }
+
+ socketcan_t::~socketcan_t()
+ {}
+
+ const struct sockaddr_can& socketcan_t::get_tx_address() const
+ {
+ return tx_address_;
+ }
+
+ /// @brief Test if socket is valid.
+ /// @return true if valid, false otherwise.
+ socketcan_t::operator bool() const
+ {
+ return socket_ != INVALID_SOCKET;
+ }
+
+ /// @brief Open the socket.
+ /// @param[in] domain Specifies the communications domain in which a socket is to be created.
+ /// @param[in] type Specifies the type of socket to be created.
+ /// @param[in] protocol Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.
+ /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
+ int socketcan_t::open(int domain, int type, int protocol)
+ {
+ close();
+ socket_ = ::socket(domain, type, protocol);
+ return socket_;
+ }
+
+ /// @brief Close the socket.
+ /// @return 0 if success.
+ int socketcan_t::close()
+ {
+ return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
+ }
+
+ /// @brief Set socket option.
+ /// @return 0 if success.
+ int socketcan_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
+ {
+ return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
+ }
+
+ /// @brief Get the file descriptor.
+ /// @return The socket's file descriptor
+ int socketcan_t::socket() const
+ {
+ return socket_;
+ }
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/socketcan.hpp b/CAN-binder/low-can-binding/utils/socketcan.hpp
new file mode 100644
index 00000000..c3abb2ff
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socketcan.hpp
@@ -0,0 +1,70 @@
+#pragma once
+
+/*
+ * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ * Author "Loïc Collignon" <loic.collignon@iot.bzh>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vector>
+
+#include <sys/socket.h>
+#include <linux/can/bcm.h>
+#include <string.h>
+
+#include "../binding/low-can-hat.hpp"
+
+#define INVALID_SOCKET -1
+
+namespace utils
+{
+ class socketcan_t
+ {
+ public:
+ socketcan_t();
+ socketcan_t(const socketcan_t& s);
+ socketcan_t(socketcan_t&&);
+ virtual ~socketcan_t();
+
+ const struct sockaddr_can& get_tx_address() const;
+ explicit operator bool() const;
+
+ int socket() const;
+ virtual int open(std::string device_name) = 0;
+ int setopt(int level, int optname, const void* optval, socklen_t optlen);
+ int close();
+
+ protected:
+ int socket_;
+ struct sockaddr_can tx_address_;
+
+ int open(int domain, int type, int protocol);
+ };
+
+ template <typename T>
+ socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj)
+ {
+ for(const auto& obj : vobj)
+ s << obj;
+ return s;
+ }
+
+ template <typename T>
+ socketcan_t& operator<<(socketcan_t& s, const T& obj)
+ {
+ if (::sendto(s.socket(), &obj, sizeof(obj), 0, (const struct sockaddr*)&s.get_tx_address(), sizeof(s.get_tx_address())) < 0)
+ ERROR(binder_interface, "%s: Error sending : %i %s", __FUNCTION__, errno, ::strerror(errno));
+ return s;
+ }
+}
diff --git a/CAN-binder/low-can-binding/utils/timer.cpp b/CAN-binder/low-can-binding/utils/timer.cpp
new file mode 100644
index 00000000..9c22bd0d
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/timer.cpp
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <time.h>
+#include <stdlib.h>
+#include <cmath>
+
+#include "timer.hpp"
+
+long long int system_time_us()
+{
+ struct timespec t_usec;
+ long long int timestamp_usec;
+
+ if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec))
+ timestamp_usec = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll);
+ return timestamp_usec;
+}
+
+long long int system_time_ms()
+{
+ struct timespec t_msec;
+ long long int timestamp_msec;
+
+ if(!::clock_gettime(CLOCK_MONOTONIC, &t_msec))
+ timestamp_msec = (t_msec.tv_nsec / 1000000ll) + (t_msec.tv_sec* 1000ll);
+ return timestamp_msec;
+}
+
+long long int system_time_s()
+{
+ struct timespec t_sec;
+ long long int timestamp_sec;
+
+ if(!::clock_gettime(CLOCK_MONOTONIC, &t_sec))
+ timestamp_sec = t_sec.tv_sec;
+ return timestamp_sec;
+}
+
+frequency_clock_t::frequency_clock_t()
+ : unit_{1000000}, frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
+{}
+
+frequency_clock_t::frequency_clock_t(float frequency)
+ : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
+{
+ if(frequency_ <= 0)
+ frequency_ = 1;
+}
+
+frequency_clock_t::frequency_clock_t(float frequency, uint64_t last_tick, time_function_t time_function)
+ : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
+{
+ if(frequency_ <= 0)
+ frequency_ = 1;
+}
+/// @brief Return the period in ms given the frequency in hertz.
+/// @param[in] frequency - Frequency to convert, in hertz
+float frequency_clock_t::frequency_to_period() const
+{
+ return frequency_ == 0 ? 0 : 1 / frequency_;
+}
+
+const struct timeval frequency_clock_t::get_timeval_from_period() const
+{
+ struct timeval freq = {0, 0};
+ float f;
+ freq.tv_usec = (long int)(std::modf(frequency_to_period(), &f) * unit_);
+ freq.tv_sec = (time_t)f;
+
+ return freq;
+}
+
+bool frequency_clock_t::started()
+{
+ return last_tick_ != 0;
+}
+
+time_function_t frequency_clock_t::get_time_function()
+{
+ return time_function_ != nullptr ? time_function_ : system_time_us;
+}
+
+bool frequency_clock_t::elapsed(bool stagger)
+{
+ float period = frequency_to_period();
+ float elapsed_time = 0;
+ if(!started() && stagger)
+ last_tick_ = get_time_function()() - (rand() % int(period));
+
+ // Make sure it ticks the the first call
+ elapsed_time = !started() ? period : (float)get_time_function()() - (float)last_tick_;
+
+ return frequency_ == 0 || elapsed_time >= period;
+}
+
+float frequency_clock_t::get_frequency() const
+{
+ return frequency_;
+}
+
+/// @brief Force the clock to tick, regardless of it its time has actually
+/// elapsed.
+void frequency_clock_t::tick(uint64_t timestamp)
+{
+ last_tick_ = timestamp;
+} \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/utils/timer.hpp b/CAN-binder/low-can-binding/utils/timer.hpp
new file mode 100644
index 00000000..432d513d
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/timer.hpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Author "Romain Forlot" <romain.forlot@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <sys/time.h>
+#include <stdint.h>
+
+/// @brief return epoch in milliseconds
+///
+/// @return long long int epoch in milliseconds
+typedef long long int (*time_function_t)();
+
+long long int system_time_us();
+long long int system_time_ms();
+long long int system_time_s();
+
+
+/// @brief A frequency counting clock.
+/// Utility class allowing some time function.
+class frequency_clock_t
+{
+private:
+ float unit_; ///< unit_ - multiplicator to make operation to be in the right unit (milli, micro, nano, etc)
+ float frequency_; ///< the clock frequency in Hz.
+ uint64_t last_tick_; ///< the last time (in microseconds since startup) that the clock ticked.
+ time_function_t time_function_; ///< a function returning current time
+
+public:
+ frequency_clock_t();
+ frequency_clock_t(float frequency);
+ frequency_clock_t(float frequency, uint64_t last_tick, time_function_t time_function);
+
+ float get_frequency() const;
+ const struct timeval get_timeval_from_period() const;
+
+ float frequency_to_period() const;
+ bool started();
+ time_function_t get_time_function();
+ bool elapsed(bool stagger);
+
+ void tick(uint64_t timestamp);
+}; \ No newline at end of file