aboutsummaryrefslogtreecommitdiffstats
path: root/CAN-binder/low-can-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-04-14 13:24:07 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-04-14 13:24:07 +0200
commit49fe0eec8f17698fc5f86d0abe01777af1fb2b23 (patch)
tree356cd3e3261934a3fb90786e1789ee4ad001cbe5 /CAN-binder/low-can-binding
parent044ef27d3667a114964549f89c63ab1ea6655877 (diff)
Change directory architecture to use 2 separated projects.
Each projects, binder and generator, has to be compiled separatly. CAN-binder will host high and low level binding CAN-config-generator only the generator used for low level binding. build.sh script just launch both build in their respective dir. Change-Id: Ic77932660fcca507b23a631d4e4e790f608880ae Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'CAN-binder/low-can-binding')
-rw-r--r--CAN-binder/low-can-binding/CMakeLists.txt50
-rw-r--r--CAN-binder/low-can-binding/can/can-bus-dev.cpp255
-rw-r--r--CAN-binder/low-can-binding/can/can-bus-dev.hpp65
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.cpp398
-rw-r--r--CAN-binder/low-can-binding/can/can-bus.hpp94
-rw-r--r--CAN-binder/low-can-binding/can/can-command.hpp55
-rw-r--r--CAN-binder/low-can-binding/can/can-decoder.cpp233
-rw-r--r--CAN-binder/low-can-binding/can/can-decoder.hpp49
-rw-r--r--CAN-binder/low-can-binding/can/can-message-definition.cpp55
-rw-r--r--CAN-binder/low-can-binding/can/can-message-definition.hpp57
-rw-r--r--CAN-binder/low-can-binding/can/can-message-set.cpp37
-rw-r--r--CAN-binder/low-can-binding/can/can-message-set.hpp47
-rw-r--r--CAN-binder/low-can-binding/can/can-message.cpp264
-rw-r--r--CAN-binder/low-can-binding/can/can-message.hpp75
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.cpp186
-rw-r--r--CAN-binder/low-can-binding/can/can-signals.hpp158
-rw-r--r--CAN-binder/low-can-binding/configuration-generated.cpp350
-rw-r--r--CAN-binder/low-can-binding/configuration.cpp156
-rw-r--r--CAN-binder/low-can-binding/configuration.hpp120
-rw-r--r--CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp194
-rw-r--r--CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp122
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp618
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp99
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp108
-rw-r--r--CAN-binder/low-can-binding/diagnostic/diagnostic-message.hpp79
-rw-r--r--CAN-binder/low-can-binding/low-can-binding.cpp279
-rw-r--r--CAN-binder/low-can-binding/low-can-binding.hpp33
-rw-r--r--CAN-binder/low-can-binding/packaging/wgt/config.xml11
-rw-r--r--CAN-binder/low-can-binding/packaging/wgt/etc/can_buses.json3
-rw-r--r--CAN-binder/low-can-binding/packaging/wgt/icon.pngbin0 -> 4035 bytes
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.cpp84
-rw-r--r--CAN-binder/low-can-binding/utils/config-parser.hpp43
-rw-r--r--CAN-binder/low-can-binding/utils/openxc-utils.cpp282
-rw-r--r--CAN-binder/low-can-binding/utils/openxc-utils.hpp42
-rw-r--r--CAN-binder/low-can-binding/utils/signals.cpp76
-rw-r--r--CAN-binder/low-can-binding/utils/signals.hpp90
-rw-r--r--CAN-binder/low-can-binding/utils/socket.cpp89
-rw-r--r--CAN-binder/low-can-binding/utils/socket.hpp46
-rw-r--r--CAN-binder/low-can-binding/utils/timer.cpp102
-rw-r--r--CAN-binder/low-can-binding/utils/timer.hpp53
40 files changed, 5157 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..a1768abb
--- /dev/null
+++ b/CAN-binder/low-can-binding/CMakeLists.txt
@@ -0,0 +1,50 @@
+###########################################################################
+# 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-binding)
+
+ # Define project Targets
+ add_library(${TARGET_NAME} MODULE ${TARGET_NAME}.cpp configuration.cpp configuration-generated.cpp
+ can/can-bus.cpp can/can-bus-dev.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/socket.cpp)
+
+ # Binder exposes a unique public entry point
+ SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
+ PREFIX ""
+ LINK_FLAGS ${BINDINGS_LINK_FLAG}
+ OUTPUT_NAME ${TARGET_NAME}
+ )
+
+ # Library dependencies (include updates automatically)
+ TARGET_LINK_LIBRARIES(${TARGET_NAME}
+ openxc-message-format
+ nanopb
+ uds-c
+ isotp-c
+ bitfield-c
+ ${link_libraries}
+ )
+
+ # installation directory
+ INSTALL(TARGETS ${TARGET_NAME}
+ LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR})
+
+ build_widget("BINDING")
diff --git a/CAN-binder/low-can-binding/can/can-bus-dev.cpp b/CAN-binder/low-can-binding/can/can-bus-dev.cpp
new file mode 100644
index 00000000..fb76498f
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus-dev.cpp
@@ -0,0 +1,255 @@
+/*
+* 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 <map>
+#include <mutex>
+#include <unistd.h>
+#include <string.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <linux/can/raw.h>
+
+#include "can-bus-dev.hpp"
+
+#include "can-bus.hpp"
+#include "can-message.hpp"
+#include "../low-can-binding.hpp"
+
+/// @brief Class constructor
+///
+/// @param[in] dev_name - String representing the device name into the linux /dev tree
+/// @param[in] address - integer identifier of the bus, set using init_can_dev from can_bus_t.
+can_bus_dev_t::can_bus_dev_t(const std::string& dev_name, int32_t address)
+ : device_name_{dev_name}, address_{address}
+{}
+
+std::string can_bus_dev_t::get_device_name() const
+{
+ return device_name_;
+}
+
+uint32_t can_bus_dev_t::get_address() const
+{
+ return address_;
+}
+
+/// @brief Open the can socket and returning it
+///
+/// We try to open CAN socket and apply the following options
+/// timestamp received messages and pass the socket to FD mode.
+///
+/// @return -1 if something wrong.
+int can_bus_dev_t::open()
+{
+ const int canfd_on = 1;
+ const int timestamp_on = 1;
+ struct ifreq ifr;
+ struct timeval timeout;
+
+ DEBUG(binder_interface, "open: CAN Handler socket : %d", can_socket_.socket());
+ if (can_socket_)
+ return 0;
+
+ can_socket_.open(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (can_socket_)
+ {
+ DEBUG(binder_interface, "open: CAN Handler socket correctly initialized : %d", can_socket_.socket());
+
+ // Set timeout for read
+ can_socket_.setopt(SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+
+ // Set timestamp for receveid frame
+ if (can_socket_.setopt(SOL_SOCKET, SO_TIMESTAMP, &timestamp_on, sizeof(timestamp_on)) < 0)
+ WARNING(binder_interface, "open: setsockopt SO_TIMESTAMP error: %s", ::strerror(errno));
+ DEBUG(binder_interface, "open: Switch CAN Handler socket to use fd mode");
+
+ // try to switch the socket into CAN_FD mode
+ if (can_socket_.setopt(SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
+ {
+ NOTICE(binder_interface, "open: Can not switch into CAN Extended frame format.");
+ is_fdmode_on_ = false;
+ }
+ else
+ {
+ DEBUG(binder_interface, "open: Correctly set up CAN socket to use FD frames.");
+ is_fdmode_on_ = true;
+ }
+
+ // Attempts to open a socket to CAN bus
+ ::strcpy(ifr.ifr_name, device_name_.c_str());
+ DEBUG(binder_interface, "open: ifr_name is : %s", ifr.ifr_name);
+ if(::ioctl(can_socket_.socket(), SIOCGIFINDEX, &ifr) < 0)
+ {
+ ERROR(binder_interface, "open: ioctl failed. Error was : %s", strerror(errno));
+ }
+ else
+ {
+ txAddress_.can_family = AF_CAN;
+ txAddress_.can_ifindex = ifr.ifr_ifindex;
+
+ // And bind it to txAddress
+ DEBUG(binder_interface, "Bind the socket");
+ if (can_socket_.bind((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0)
+ ERROR(binder_interface, "Bind failed. %s", strerror(errno));
+ else return 0;
+ }
+ close();
+ }
+ else ERROR(binder_interface, "open: socket could not be created. Error was : %s", ::strerror(errno));
+ return -1;
+}
+
+/// @brief Close the bus.
+///
+/// @return interger return value of socket.close() function
+int can_bus_dev_t::close()
+{
+ return can_socket_.close();
+}
+
+/// @brief Read the can socket and retrieve canfd_frame.
+///
+/// Read operation are blocking and we try to read CANFD frame
+/// rather than classic CAN frame. CANFD frame are retro compatible.
+can_message_t can_bus_dev_t::read()
+{
+ ssize_t nbytes;
+ struct canfd_frame cfd;
+
+ // Test that socket is really opened
+ if (!can_socket_)
+ {
+ ERROR(binder_interface, "read: Socket unavailable. Closing thread.");
+ is_running_ = false;
+ }
+
+ nbytes = ::read(can_socket_.socket(), &cfd, CANFD_MTU);
+
+ // if we did not fit into CAN sized messages then stop_reading.
+ if (nbytes != CANFD_MTU && nbytes != CAN_MTU)
+ {
+ if (errno == ENETDOWN)
+ ERROR(binder_interface, "read: %s CAN device down", device_name_.c_str());
+ ERROR(binder_interface, "read: Incomplete CAN(FD) frame");
+ ::memset(&cfd, 0, sizeof(cfd));
+ }
+
+ DEBUG(binder_interface, "read: Found id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", cfd.can_id, cfd.len,
+ cfd.data[0], cfd.data[1], cfd.data[2], cfd.data[3], cfd.data[4], cfd.data[5], cfd.data[6], cfd.data[7]);
+ return can_message_t::convert_from_canfd_frame(cfd, nbytes);
+}
+
+/// @brief start reading threads and set flag is_running_
+/// @param[in] can_bus reference can_bus_t. it will be passed to the thread to allow using can_bus_t queue.
+void can_bus_dev_t::start_reading(can_bus_t& can_bus)
+{
+ DEBUG(binder_interface, "Launching reading thread");
+ is_running_ = true;
+ th_reading_ = std::thread(&can_bus_dev_t::can_reader, this, std::ref(can_bus));
+ if(!th_reading_.joinable())
+ is_running_ = false;
+}
+
+/// @brief stop the reading thread setting flag is_running_ to false and and wait that the thread finish its job.
+void can_bus_dev_t::stop_reading()
+{
+ is_running_ = false;
+}
+
+/// @brief Thread function used to read the can socket.
+/// @param[in] can_bus - object to be used to read the can socket
+void can_bus_dev_t::can_reader(can_bus_t& can_bus)
+{
+ while(is_running_)
+ {
+ can_message_t msg = read();
+ {
+ std::lock_guard<std::mutex> can_message_lock(can_bus.get_can_message_mutex());
+ can_bus.push_new_can_message(msg);
+ }
+ can_bus.get_new_can_message_cv().notify_one();
+ }
+}
+
+/// @brief Send a can message from a can_message_t object.
+/// @param[in] can_msg - the can message object to send
+///
+/// @return 0 if message snet, -1 if something wrong.
+int can_bus_dev_t::send(can_message_t& can_msg)
+{
+ ssize_t nbytes;
+ canfd_frame f;
+
+ f = can_msg.convert_to_canfd_frame();
+
+ if(can_socket_)
+ {
+ nbytes = ::sendto(can_socket_.socket(), &f, sizeof(struct canfd_frame), 0,
+ (struct sockaddr*)&txAddress_, sizeof(txAddress_));
+ if (nbytes == -1)
+ {
+ ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
+ return -1;
+ }
+ return (int)nbytes;
+ }
+ else
+ {
+ ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+ open();
+ }
+ return 0;
+}
+
+/// @brief Static method used to send diagnostic CAN message
+/// that follow isotp SendShimsMessage signature. This method is launched
+/// from diagnostic manager's' same name method. It will use the diagnostic
+/// manager configured CAN bus device to send the CAN message.
+///
+/// @param[in] arbitration_id - CAN arbitration id.
+/// @param[in] data - CAN message payload to send
+/// @param[in] size - size of the data to send
+///
+/// @return True if message sent, false if not.
+bool can_bus_dev_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
+{
+ ssize_t nbytes;
+ canfd_frame f;
+
+ f.can_id = arbitration_id;
+ f.len = size;
+ ::memcpy(f.data, data, size);
+
+ if(can_socket_)
+ {
+ nbytes = ::sendto(can_socket_.socket(), &f, sizeof(struct canfd_frame), 0,
+ (struct sockaddr*)&txAddress_, sizeof(txAddress_));
+ if (nbytes == -1)
+ {
+ ERROR(binder_interface, "send_can_message: Sending CAN frame failed.");
+ return false;
+ }
+ return true;
+ }
+ else
+ {
+ ERROR(binder_interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+ open();
+ }
+ return false;
+}
diff --git a/CAN-binder/low-can-binding/can/can-bus-dev.hpp b/CAN-binder/low-can-binding/can/can-bus-dev.hpp
new file mode 100644
index 00000000..a612e7d8
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus-dev.hpp
@@ -0,0 +1,65 @@
+/*
+ * 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 <stdint.h>
+#include <linux/can.h>
+#include <string>
+#include <thread>
+
+#include "../utils/socket.hpp"
+
+class can_bus_t;
+class can_message_t;
+
+/// @brief Object representing a can device. Handle opening, closing and reading on the
+/// socket. This is the low level object to be initialized and use by can_bus_t.
+class can_bus_dev_t
+{
+private:
+ std::string device_name_; ///< a string identifier identitfying the linux CAN device.
+ utils::socket_t can_socket_;
+
+ int32_t address_; ///< an identifier used through binding that refer to that device
+
+ bool is_fdmode_on_; ///< boolean telling if whether or not the can socket use fdmode.
+ struct sockaddr_can txAddress_; /// < internal member using to bind to the socket
+
+ std::thread th_reading_; ///< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t
+ bool is_running_ = false; ///< boolean telling whether or not reading is running or not
+ void can_reader(can_bus_t& can_bus);
+
+public:
+ can_bus_dev_t(const std::string& dev_name, int32_t address);
+
+ std::string get_device_name() const;
+ uint32_t get_address() const;
+
+ int open();
+ int close();
+
+ void start_reading(can_bus_t& can_bus);
+
+ void stop_reading();
+
+ can_message_t read();
+
+ int send(can_message_t& can_msg);
+ bool shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size);
+};
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..33f58fd9
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus.cpp
@@ -0,0 +1,398 @@
+/*
+ * 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 <cerrno>
+#include <vector>
+#include <string>
+#include <fcntl.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <json-c/json.h>
+#include <linux/can/raw.h>
+
+#include "can-bus.hpp"
+
+#include "can-signals.hpp"
+#include "can-decoder.hpp"
+#include "../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(int conf_file)
+ : conf_file_{conf_file}
+{
+}
+
+std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::can_devices_;
+
+/// @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(can_message_t& can_message)
+{
+ int processed_signals = 0;
+ std::vector <can_signal_t*> signals;
+ openxc_DynamicField search_key, decoded_message;
+ openxc_VehicleMessage vehicle_message;
+
+ // First we have to found which can_signal_t it is
+ search_key = build_DynamicField((double)can_message.get_id());
+ configuration_t::instance().find_can_signals(search_key, signals);
+
+ // Decoding the message ! Don't kill the messenger !
+ for(auto& sig : signals)
+ {
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+
+ // DEBUG message to make easier debugger STL containers...
+ //DEBUG(binder_interface, "Operator[] key char: %s, event valid? %d", sig.generic_name, afb_event_is_valid(s[sig.generic_name]));
+ //DEBUG(binder_interface, "Operator[] key string: %s, event valid? %d", sig.generic_name, afb_event_is_valid(s[std::string(sig.generic_name)]));
+ //DEBUG(binder_interface, "Nb elt matched char: %d", (int)s.count(sig.generic_name));
+ //DEBUG(binder_interface, "Nb elt matched string: %d", (int)s.count(std::string(sig.generic_name));
+ if( s.find(sig->get_name()) != s.end() && afb_event_is_valid(s[sig->get_name()]))
+ {
+ decoded_message = decoder_t::translateSignal(*sig, can_message, configuration_t::instance().get_can_signals());
+
+ 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, "process_can_signals: %d/%d CAN signals processed.", processed_signals, (int)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;
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = 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;
+
+ 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(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = 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 Will initialize can_bus_dev_t objects after reading
+/// the configuration file passed in the constructor. All CAN buses
+/// Initialized here will be added to a vector holding them for
+/// inventory and later access.
+///
+/// That will initialize CAN socket reading too using a new thread.
+///
+/// @return 0 if ok, other if not.
+int can_bus_t::init_can_dev()
+{
+ std::vector<std::string> devices_name;
+ int i = 0;
+ size_t t;
+
+ devices_name = read_conf();
+
+ if (! devices_name.empty())
+ {
+ t = devices_name.size();
+
+ for(const auto& device : devices_name)
+ {
+ can_bus_t::can_devices_[device] = std::make_shared<can_bus_dev_t>(device, i);
+ if (can_bus_t::can_devices_[device]->open() == 0)
+ {
+ DEBUG(binder_interface, "Start reading thread");
+ NOTICE(binder_interface, "%s device opened and reading", device.c_str());
+ can_bus_t::can_devices_[device]->start_reading(*this);
+ i++;
+ }
+ else
+ {
+ ERROR(binder_interface, "Can't open device %s", device.c_str());
+ return 1;
+ }
+ }
+
+ NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, (int)t);
+ return 0;
+ }
+ ERROR(binder_interface, "init_can_dev: Error at CAN device initialization. No devices read from configuration file. Did you specify canbus JSON object ?");
+ return 1;
+}
+
+/// @brief read the conf_file_ and will parse json objects
+/// in it searching for canbus objects devices name.
+///
+/// @return Vector of can bus device name string.
+std::vector<std::string> can_bus_t::read_conf()
+{
+ std::vector<std::string> ret;
+ json_object *jo, *canbus;
+ int n, i;
+ const char* taxi;
+
+ FILE *fd = fdopen(conf_file_, "r");
+ if (fd)
+ {
+ std::string fd_conf_content;
+ std::fseek(fd, 0, SEEK_END);
+ fd_conf_content.resize(std::ftell(fd));
+ std::rewind(fd);
+ std::fread(&fd_conf_content[0], 1, fd_conf_content.size(), fd);
+ std::fclose(fd);
+
+ DEBUG(binder_interface, "Configuration file content : %s", fd_conf_content.c_str());
+ jo = json_tokener_parse(fd_conf_content.c_str());
+
+ if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
+ {
+ ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
+ ret.clear();
+ }
+ else if (json_object_get_type(canbus) != json_type_array)
+ {
+ taxi = json_object_get_string(canbus);
+ DEBUG(binder_interface, "Can bus found: %s", taxi);
+ ret.push_back(std::string(taxi));
+ }
+ else
+ {
+ n = json_object_array_length(canbus);
+ for (i = 0 ; i < n ; i++)
+ ret.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
+ }
+ return ret;
+ }
+ ERROR(binder_interface, "Problem at reading the conf file");
+ ret.clear();
+ return ret;
+}
+
+/// @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, "next_can_message: Here is the next can message : id %X, length %X, data %02X%02X%02X%02X%02X%02X%02X%02X", 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, "next_vehicle_message: next vehicle message poped");
+ 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 a map with the can_bus_dev_t initialized
+///
+/// @return map can_bus_dev_m_ map
+const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& can_bus_t::get_can_devices() const
+{
+ return can_bus_t::can_devices_;
+}
+
+/// @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
+std::shared_ptr<can_bus_dev_t> can_bus_t::get_can_device(std::string bus)
+{
+ return can_bus_t::can_devices_[bus];
+}
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..eb474769
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-bus.hpp
@@ -0,0 +1,94 @@
+/*
+ * 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 "can-bus-dev.hpp"
+#include "../diagnostic/active-diagnostic-request.hpp"
+
+#include "../low-can-binding.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
+
+/// @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:
+ int conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects.
+
+ 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
+
+ static std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_devices_; ///< Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
+
+public:
+ can_bus_t(int conf_file);
+ can_bus_t(can_bus_t&&);
+
+ int init_can_dev();
+ std::vector<std::string> read_conf();
+
+ void start_threads();
+ void stop_threads();
+
+ int process_can_signals(can_message_t& can_message);
+ int process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message);
+
+ 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);
+
+ const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
+ static std::shared_ptr<can_bus_dev_t> get_can_device(std::string bus);
+};
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..8843059d
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-decoder.cpp
@@ -0,0 +1,233 @@
+/*
+ * 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"
+
+/// @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<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<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<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<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, "stateDecoder: No state found with index: %d", (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.
+///
+/// The decoder returns an openxc_DynamicField, which may contain a number,
+/// string or boolean.
+///
+openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, can_message_t& message,
+ const std::vector<can_signal_t>& signals)
+{
+ float value = decoder_t::parseSignalBitfield(signal, message);
+ DEBUG(binder_interface, "translateSignal: Decoded message from parseSignalBitfield: %f", value);
+
+ bool send = true;
+ // 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);
+ signal.set_last_value(value);
+ 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<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<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..ebe1de20
--- /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<can_signal_t>& signals,
+ float value, bool* send);
+ static openxc_DynamicField booleanDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
+ float value, bool* send);
+ static openxc_DynamicField ignoreDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
+ float value, bool* send);
+ static openxc_DynamicField noopDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
+ float value, bool* send);
+
+ static openxc_DynamicField translateSignal(can_signal_t& signal, can_message_t& message,
+ const std::vector<can_signal_t>& signals);
+
+ static openxc_DynamicField decodeSignal(can_signal_t& signal, const can_message_t& message,
+ const std::vector<can_signal_t>& signals, bool* send);
+
+ static openxc_DynamicField decodeSignal(can_signal_t& signal, float value,
+ const std::vector<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..206d0b0c
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-definition.cpp
@@ -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.
+ */
+
+#include "can-message-definition.hpp"
+
+can_message_definition_t::can_message_definition_t(std::uint8_t message_set_id, const std::string bus)
+ : message_set_id_{message_set_id}, bus_{bus}, last_value_{CAN_MESSAGE_SIZE}
+{}
+
+can_message_definition_t::can_message_definition_t(std::uint8_t message_set_id,
+ const std::string bus,
+ uint32_t id,
+ frequency_clock_t frequency_clock,
+ bool force_send_changed)
+ : message_set_id_{message_set_id},
+ 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(std::uint8_t message_set_id,
+ const std::string bus,
+ uint32_t id,
+ can_message_format_t format,
+ frequency_clock_t frequency_clock,
+ bool force_send_changed)
+ : message_set_id_{message_set_id},
+ bus_{bus},
+ id_{id},
+ format_{format},
+ frequency_clock_{frequency_clock},
+ force_send_changed_{force_send_changed},
+ last_value_{CAN_MESSAGE_SIZE}
+{}
+
+uint32_t can_message_definition_t::get_id() const
+{
+ return id_;
+} \ 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..98cb2c5d
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-definition.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.
+ */
+
+/**
+ * @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-bus-dev.hpp"
+#include "can-message.hpp"
+#include "../utils/timer.hpp"
+
+class can_message_definition_t
+{
+private:
+ std::uint8_t message_set_id_;
+ 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.*/
+
+public:
+ can_message_definition_t(std::uint8_t message_set_id, const std::string bus);
+ can_message_definition_t(std::uint8_t message_set_id, const std::string bus, uint32_t id, frequency_clock_t frequency_clock, bool force_send_changed);
+ can_message_definition_t(std::uint8_t message_set_id, const std::string bus, uint32_t id, can_message_format_t format, frequency_clock_t frequency_clock, bool force_send_changed);
+
+ uint32_t get_id() const;
+};
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..a9f8cf23
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-set.cpp
@@ -0,0 +1,37 @@
+/*
+ * 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"
+
+can_message_set_t::can_message_set_t(
+ uint8_t index,
+ const std::string& name,
+ uint8_t can_bus_count,
+ unsigned short can_message_count,
+ unsigned short can_signal_count,
+ unsigned short can_command_count,
+ unsigned short obd2_signal_count)
+ : index_{index}
+ , name_{name}
+ , can_bus_count_{can_bus_count}
+ , can_message_count_{can_message_count}
+ , can_signal_count_{can_signal_count}
+ , can_command_count_{can_command_count}
+ , obd2_signal_count_{obd2_signal_count}
+{
+}
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..94e2d8d5
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message-set.hpp
@@ -0,0 +1,47 @@
+/*
+ * 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>
+
+/// @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.
+ uint8_t can_bus_count_; /// < The number of CAN buses defined for this message set.
+ uint16_t can_message_count_; /// < The number of CAN messages (across all buses) defined for this message set.
+ uint16_t can_signal_count_; /// < The number of CAN signals (across all messages) defined for this message set.
+ uint16_t can_command_count_; /// < The number of CanCommmands defined for this message set.
+ uint16_t obd2_signal_count_; /// < The number of obd2 signals defined for this message set.
+
+public:
+ can_message_set_t(
+ uint8_t index,
+ const std::string& name,
+ uint8_t can_bus_count,
+ short unsigned int can_message_count,
+ short unsigned int can_signal_count,
+ short unsigned int can_command_count,
+ short unsigned int obd2_signal_count);
+
+};
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..f61de67e
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message.cpp
@@ -0,0 +1,264 @@
+/*
+ * 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 "../low-can-binding.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::ERROR}, rtr_flag_{false}, flags_{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)
+ : maxdlen_{maxdlen},
+ id_{id},
+ length_{length},
+ format_{format},
+ rtr_flag_{rtr_flag},
+ flags_{flags},
+ data_{data}
+{}
+
+///
+/// @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::ERROR;
+ 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 length_ member value.
+///
+/// @return length_ class member
+///
+uint8_t can_message_t::get_length() const
+{
+ return length_;
+}
+
+///
+/// @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::ERROR)
+ {
+ 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::ERROR)
+ format_ = new_format;
+ else
+ ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
+}
+
+///
+/// @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_canfd_frame(const struct canfd_frame& frame, size_t nbytes)
+{
+ 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, "set_max_data_length: Got an CAN FD frame");
+ maxdlen = CANFD_MAX_DLEN;
+ break;
+ case CAN_MTU:
+ DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame");
+ maxdlen = CAN_MAX_DLEN;
+ break;
+ default:
+ ERROR(binder_interface, "set_max_data_length: unsupported CAN frame");
+ break;
+ }
+
+ if (frame.can_id & CAN_ERR_FLAG)
+ format = can_message_format_t::ERROR;
+ else if (frame.can_id & CAN_EFF_FLAG)
+ format = can_message_format_t::EXTENDED;
+ else
+ format = can_message_format_t::STANDARD;
+
+ switch(format)
+ {
+ case can_message_format_t::STANDARD:
+ id = frame.can_id & CAN_SFF_MASK;
+ break;
+ case can_message_format_t::EXTENDED:
+ id = frame.can_id & CAN_EFF_MASK;
+ break;
+ case can_message_format_t::ERROR:
+ id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+ break;
+ default:
+ ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
+ break;
+ }
+
+ /* 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, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
+ 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);
+}
+
+///
+/// @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.
+///
+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, "can_message_t not correctly initialized to be sent");
+
+ 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..6fe03172
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-message.hpp
@@ -0,0 +1,75 @@
+/*
+ * 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"
+#include "can-message-set.hpp"
+
+#define CAN_MESSAGE_SIZE 8
+
+class can_bus_dev_t;
+
+/**
+ * @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. */
+ ERROR, /*!< ERROR - ERROR 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.*/
+
+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);
+
+ 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;
+ uint8_t get_length() const;
+
+ void set_format(const can_message_format_t new_format);
+
+ bool is_correct_to_send();
+
+static can_message_t convert_from_canfd_frame(const struct canfd_frame& frame, size_t nbytes);
+ canfd_frame convert_to_canfd_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..6211fa1d
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-signals.cpp
@@ -0,0 +1,186 @@
+/*
+ * 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 "../configuration.hpp"
+#include "../utils/signals.hpp"
+#include "can-decoder.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+
+std::string can_signal_t::prefix_ = "messages";
+
+can_signal_t::can_signal_t(std::uint8_t message_set_id,
+ std::uint8_t message_id,
+ 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)
+ : message_set_id_{ message_set_id }
+ , message_id_{ message_id }
+ , 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}
+{}
+
+
+const can_message_definition_t& can_signal_t::get_message() const
+{
+ return configuration_t::instance().get_can_message_definition(message_set_id_, message_id_);
+}
+
+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_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;
+}
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..34ebeebd
--- /dev/null
+++ b/CAN-binder/low-can-binding/can/can-signals.hpp
@@ -0,0 +1,158 @@
+///
+/// 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 <queue>
+#include <vector>
+#include <string>
+
+#include "openxc.pb.h"
+#include "../utils/timer.hpp"
+#include "can-bus.hpp"
+#include "can-message.hpp"
+#include "can-message-definition.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<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:
+ std::uint8_t message_set_id_;
+ std::uint8_t message_id_;
+ 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_; /*!< bitSize_ - 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::uint8_t message_set_id,
+ std::uint8_t message_id,
+ 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);
+
+ 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_prefix(std::string val);
+ void set_received(bool r);
+ void set_last_value(float val);
+}; \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/configuration-generated.cpp b/CAN-binder/low-can-binding/configuration-generated.cpp
new file mode 100644
index 00000000..2e965703
--- /dev/null
+++ b/CAN-binder/low-can-binding/configuration-generated.cpp
@@ -0,0 +1,350 @@
+#include "configuration.hpp"
+#include "can/can-decoder.hpp"
+
+configuration_t::configuration_t()
+ : can_message_set_{{0, "example", 0, 1, 5, 0, 20}}
+ , can_message_definition_
+ {
+ {
+ can_message_definition_t(0, "can0", 0x620, can_message_format_t::STANDARD, frequency_clock_t(0.00000f), true)
+ }
+ }
+ , can_signals_
+ {
+ {
+ {
+ 0,
+ 0,
+ "doors.coffer.open",
+ 88,
+ 1,
+ 0.00000f,
+ 0,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ },
+ {
+ 0,
+ 0,
+ "doors.driver.open",
+ 78,
+ 1,
+ 0.00000f,
+ 0,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ },
+ {
+ 0,
+ 0,
+ "doors.passenger.open",
+ 79,
+ 1,
+ 0.00000f,
+ 0,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ },
+ {
+ 0,
+ 0,
+ "doors.rearleft.open",
+ 86,
+ 1,
+ 0.00000f,
+ 0,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ },
+ {
+ 0,
+ 0,
+ "doors.rearright.open",
+ 85,
+ 4,
+ 0.00000f,
+ 0,
+ 0,
+ 0,
+ frequency_clock_t(0.00000f),
+ true,
+ false,
+ {
+ },
+ false,
+ decoder_t::booleanDecoder,
+ nullptr,
+ false
+ }
+ }
+ }
+ , diagnostic_messages_
+ {
+ {
+ {
+ 4,
+ "engine.load",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 5,
+ "engine.coolant.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 10,
+ "fuel.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 11,
+ "intake.manifold.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 12,
+ "engine.speed",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 13,
+ "vehicle.speed",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 15,
+ "intake.air.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 16,
+ "mass.airflow",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 17,
+ "throttle.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 31,
+ "running.time",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 45,
+ "EGR.error",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 0.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 47,
+ "fuel.level",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 51,
+ "barometric.pressure",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 76,
+ "commanded.throttle.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 82,
+ "ethanol.fuel.percentage",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 90,
+ "accelerator.pedal.position",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 91,
+ "hybrid.battery-pack.remaining.life",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 5.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 92,
+ "engine.oil.temperature",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 94,
+ "engine.fuel.rate",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ },
+ {
+ 99,
+ "engine.torque",
+ 0,
+ 0,
+ UNIT::INVALID,
+ 1.00000f,
+ decoder_t::decode_obd2_response,
+ nullptr,
+ true
+ }
+ }
+ }
+{
+}
+
+const std::string configuration_t::get_diagnostic_bus() const
+{
+ return "can0";
+}
+
+
diff --git a/CAN-binder/low-can-binding/configuration.cpp b/CAN-binder/low-can-binding/configuration.cpp
new file mode 100644
index 00000000..8a50cbc4
--- /dev/null
+++ b/CAN-binder/low-can-binding/configuration.cpp
@@ -0,0 +1,156 @@
+/*
+ * 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_;
+}
+
+const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& configuration_t::get_can_bus_devices()
+{
+ return can_bus_manager_.get_can_devices();
+}
+
+diagnostic_manager_t& configuration_t::get_diagnostic_manager()
+{
+ return diagnostic_manager_;
+}
+
+uint8_t configuration_t::get_active_message_set() const
+{
+ return active_message_set_;
+}
+
+const std::vector<can_message_set_t>& configuration_t::get_can_message_set()
+{
+ return can_message_set_;
+}
+
+std::vector<can_signal_t>& configuration_t::get_can_signals()
+{
+ return can_signals_[active_message_set_];
+}
+
+std::vector<diagnostic_message_t>& configuration_t::get_diagnostic_messages()
+{
+ return diagnostic_messages_[active_message_set_];
+}
+
+const std::vector<can_message_definition_t>& configuration_t::get_can_message_definition()
+{
+ return can_message_definition_[active_message_set_];
+}
+
+const can_message_definition_t& configuration_t::get_can_message_definition(std::uint8_t message_set_id, std::uint8_t message_id)
+{
+ return can_message_definition_[message_set_id][message_id];
+}
+
+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;
+}
+
+/// @brief return diagnostic messages name found searching through diagnostic messages list.
+///
+/// @param[in] key - can contain numeric or string value in order to search against
+/// can signals or obd2 signals name.
+/// @param[out] found_signals - Vector of signals name found.
+///
+void configuration_t::find_diagnostic_messages(const openxc_DynamicField &key, std::vector<diagnostic_message_t*>& found_signals)
+{
+ switch(key.type)
+ {
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
+ lookup_signals_by_name(key.string_value, diagnostic_messages_[active_message_set_], found_signals);
+ break;
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
+ lookup_signals_by_id(key.numeric_value, diagnostic_messages_[active_message_set_], found_signals);
+ break;
+ default:
+ ERROR(binder_interface, "find_diagnostic_messages: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
+ break;
+ }
+ DEBUG(binder_interface, "find_diagnostic_messages: Found %d signal(s)", (int)found_signals.size());
+}
+
+diagnostic_message_t* configuration_t::get_diagnostic_message(std::string message_name) const
+{
+ std::vector<diagnostic_message_t*> found;
+ configuration_t::instance().find_diagnostic_messages(build_DynamicField(message_name), found);
+ if(! found.empty())
+ return found.front();
+ return nullptr;
+}
+
+DiagnosticRequest* configuration_t::get_request_from_diagnostic_message(diagnostic_message_t* diag_msg) const
+{
+ return new DiagnosticRequest(diag_msg->build_diagnostic_request());
+}
+
+DiagnosticRequest* configuration_t::get_request_from_diagnostic_message(std::string message_name) const
+{
+ diagnostic_message_t* diag_msg = get_diagnostic_message(message_name);
+ if( diag_msg != nullptr)
+ return new DiagnosticRequest(diag_msg->build_diagnostic_request());
+ return nullptr;
+}
+
+/// @brief return signals name found searching through CAN signals list.
+///
+/// @param[in] key - can contain numeric or string value in order to search against
+/// can signals or obd2 signals name.
+/// @param[out] found_signals - provided vector to fill with pointer to matched signals.
+///
+void configuration_t::find_can_signals(const openxc_DynamicField& key, std::vector<can_signal_t*>& found_signals)
+{
+ switch(key.type)
+ {
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
+ lookup_signals_by_name(std::string(key.string_value), can_signals_[active_message_set_], found_signals);
+ break;
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
+ lookup_signals_by_id(key.numeric_value, can_signals_[active_message_set_], found_signals);
+ break;
+ default:
+ ERROR(binder_interface, "find_can_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
+ break;
+ }
+ DEBUG(binder_interface, "find_can_signals: Found %d signal(s)", (int)found_signals.size());
+}
diff --git a/CAN-binder/low-can-binding/configuration.hpp b/CAN-binder/low-can-binding/configuration.hpp
new file mode 100644
index 00000000..017b148d
--- /dev/null
+++ b/CAN-binder/low-can-binding/configuration.hpp
@@ -0,0 +1,120 @@
+/*
+ * 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 <fcntl.h>
+
+#include "can/can-bus.hpp"
+#include "can/can-signals.hpp"
+#include "can/can-message.hpp"
+#include "diagnostic/diagnostic-manager.hpp"
+
+#include "low-can-binding.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_ = can_bus_t(
+ afb_daemon_rootdir_open_locale(
+ binder_interface->daemon, "etc/can_buses.json", O_RDONLY, NULL)); ///< instanciate the CAN bus
+ ///< This will read the configuration file and initialize all CAN devices specified in it.
+ 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<can_message_set_t> can_message_set_;
+ std::vector<std::vector<can_message_definition_t>> can_message_definition_;
+ std::vector<std::vector<can_signal_t>> can_signals_;
+ std::vector<std::vector<diagnostic_message_t>> diagnostic_messages_;
+
+ 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::map<std::string, std::shared_ptr<can_bus_dev_t>>& get_can_bus_devices();
+
+ const std::string get_diagnostic_bus() const;
+
+ diagnostic_manager_t& get_diagnostic_manager() ;
+
+ uint8_t get_active_message_set() const;
+
+ const std::vector<can_message_set_t>& get_can_message_set();
+
+ std::vector<can_signal_t>& get_can_signals();
+
+ std::vector<diagnostic_message_t>& get_diagnostic_messages();
+
+ const std::vector<std::string>& get_signals_prefix() const;
+
+ const std::vector<can_message_definition_t>& get_can_message_definition();
+ const can_message_definition_t& get_can_message_definition(std::uint8_t message_set_id, std::uint8_t message_id);
+
+ 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);
+
+ void find_diagnostic_messages(const openxc_DynamicField &key, std::vector<diagnostic_message_t*>& found_signals);
+ diagnostic_message_t* get_diagnostic_message(std::string message_name) const;
+ DiagnosticRequest* get_request_from_diagnostic_message(diagnostic_message_t* diag_msg) const;
+ DiagnosticRequest* get_request_from_diagnostic_message(std::string message_name) const;
+
+ void find_can_signals(const openxc_DynamicField &key, std::vector<can_signal_t*>& found_signals);
+
+/*
+ /// 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/diagnostic/active-diagnostic-request.cpp b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp
new file mode 100644
index 00000000..7ddf1d51
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.cpp
@@ -0,0 +1,194 @@
+/*
+ * 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 "../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_;
+ in_flight_ = adr.in_flight_;
+ 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},
+ in_flight_{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},
+ in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
+{}
+
+uint32_t active_diagnostic_request_t::get_id() const
+{
+ return id_;
+}
+
+const std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev() const
+{
+ return can_bus_t::get_can_device(bus_);
+}
+
+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_;
+}
+
+bool active_diagnostic_request_t::get_in_flight() const
+{
+ return in_flight_;
+}
+
+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_;
+}
+
+void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
+{
+ handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
+}
+
+void active_diagnostic_request_t::set_in_flight(bool val)
+{
+ in_flight_ = val;
+}
+
+///
+/// @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 Check is the request should be sent or not
+///
+/// @return true if the request is not running or recurring nor completed,
+/// or it's recurring, its clock elapsed
+/// so it's time to send another one.
+bool active_diagnostic_request_t::should_send()
+{
+ return !in_flight_ && ( (!recurring_ && !request_completed()) ||
+ (recurring_ && frequency_clock_.elapsed(true)) );
+}
+
+/// @brief check if the timeout clock has elapsed
+///
+/// @return true if elapsed, so it is a timeout, else false.
+bool active_diagnostic_request_t::timed_out()
+{
+ // don't use staggered start with the timeout clock
+ return timeout_clock_.elapsed(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;
+}
+
+/// @brief Returns true if the request has timed out waiting for a response,
+/// or a sufficient number of responses has been received.
+bool active_diagnostic_request_t::request_completed()
+{
+ return response_received() ||
+ (timed_out() && diagnostic_request_sent(handle_));
+}
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..247eeb17
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/active-diagnostic-request.hpp
@@ -0,0 +1,122 @@
+/*
+ * 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 "uds/uds.h"
+#include "uds/uds_types.h"
+#include "../utils/timer.hpp"
+#include "../can/can-bus-dev.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).
+ bool in_flight_; ///< in_flight_ - True if the request has been sent and we are waiting for a response.
+ 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.
+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;
+ const std::shared_ptr<can_bus_dev_t> get_can_bus_dev() 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;
+ bool get_in_flight() const;
+ frequency_clock_t& get_frequency_clock();
+ frequency_clock_t& get_timeout_clock();
+
+ void set_handle(DiagnosticShims& shims, DiagnosticRequest* request);
+ void set_in_flight(bool val);
+
+ static bool is_diagnostic_signal(const std::string& name);
+
+ bool should_send();
+
+ bool timed_out();
+ bool response_received() const;
+ bool request_completed();
+};
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..e3a78d0f
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.cpp
@@ -0,0 +1,618 @@
+/*
+ * 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 "diagnostic-manager.hpp"
+
+#include "../utils/openxc-utils.hpp"
+#include "../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();
+ reset();
+
+ initialized_ = true;
+ DEBUG(binder_interface, "initialize: Diagnostic Manager initialized");
+ return initialized_;
+}
+
+/// @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, "init_diagnostic_shims: Shims initialized");
+}
+
+/// @brief Force cleanup all active requests.
+void diagnostic_manager_t::reset()
+{
+ DEBUG(binder_interface, "Clearing existing diagnostic requests");
+ cleanup_active_requests(true);
+}
+
+/// @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)
+{
+ std::shared_ptr<can_bus_dev_t> can_bus_dev = can_bus_t::get_can_device(configuration_t::instance().get_diagnostic_manager().bus_);
+ if(can_bus_dev != nullptr)
+ return can_bus_dev->shims_send(arbitration_id, data, size);
+ ERROR(binder_interface, "shims_send: Can not retrieve diagnostic bus: %s", configuration_t::instance().get_diagnostic_manager().bus_.c_str());
+ 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, "shims_logger: %s", buffer);
+}
+
+/// @brief The type signature for a... OpenXC TODO: not used yet.
+void diagnostic_manager_t::shims_timer()
+{}
+
+std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
+{
+ return can_bus_t::get_can_device(bus_);
+}
+
+/// @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)
+{
+
+ /* TODO: implement acceptance filters.
+ if(entry.arbitration_id_ == OBD2_FUNCTIONAL_BROADCAST_ID) {
+ for(uint32_t filter = OBD2_FUNCTIONAL_RESPONSE_START;
+ filter < OBD2_FUNCTIONAL_RESPONSE_START +
+ OBD2_FUNCTIONAL_RESPONSE_COUNT;
+ filter++) {
+ removeAcceptanceFilter(entry.bus_, filter,
+ CanMessageFormat::STANDARD, getCanBuses(),
+ getCanBusCount());
+ }
+ } else {
+ removeAcceptanceFilter(entry.bus_,
+ entry.arbitration_id_ +
+ DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET,
+ CanMessageFormat::STANDARD, getCanBuses(), getCanBusCount());
+ }*/
+}
+
+/// @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->get_in_flight() && entry->request_completed())))
+ {
+ entry->set_in_flight(false);
+
+ 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, "cleanup_request: Cancelling completed, recurring request: %s", request_string);
+ }
+ else
+ {
+ DEBUG(binder_interface, "cleanup_request: Cancelling completed, non-recurring request: %s", 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,
+bool 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);
+
+ bool added = true;
+
+ 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, "Added one-time diagnostic request on bus %s: %s",
+ bus_.c_str(), request_string);
+
+ non_recurring_requests_.push_back(entry);
+ }
+ else
+ {
+ WARNING(binder_interface, "There isn't enough request entry. Vector exhausted %d/%d", (int)non_recurring_requests_.size(), MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ non_recurring_requests_.resize(MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ added = false;
+ }
+ return added;
+}
+
+bool diagnostic_manager_t::validate_optional_request_attributes(float frequencyHz)
+{
+ if(frequencyHz > MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ) {
+ DEBUG(binder_interface, "Requested recurring diagnostic frequency %lf is higher than maximum of %d",
+ 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,
+///
+bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, const char* name,
+ bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback, float frequencyHz)
+{
+ if(!validate_optional_request_attributes(frequencyHz))
+ return false;
+
+ cleanup_active_requests(false);
+
+ bool added = true;
+ if(find_recurring_request(request) == nullptr)
+ {
+ if(recurring_requests_.size() <= MAX_SIMULTANEOUS_DIAG_REQUESTS)
+ {
+ sd_event_source *source;
+ // 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, frequencyHz);
+ entry->set_handle(shims_, request);
+
+ //start_diagnostic_request(&shims_, entry->get_handle());
+ //char request_string[128] = {0};
+ //diagnostic_request_to_string(&entry->get_handle()->request, request_string,
+ // sizeof(request_string));
+
+ uint64_t usec;
+ sd_event_now(afb_daemon_get_event_loop(binder_interface->daemon), CLOCK_BOOTTIME, &usec);
+ if(recurring_requests_.size() > 0)
+ {
+ DEBUG(binder_interface, "add_recurring_request: Added 100ms to usec to stagger sending requests");
+ usec += 100000;
+ }
+
+ DEBUG(binder_interface, "add_recurring_request: Added recurring diagnostic request (freq: %f) on bus %s at %ld. Event loop state: %d",
+ frequencyHz,
+ bus_.c_str(),
+ usec,
+ sd_event_get_state(afb_daemon_get_event_loop(binder_interface->daemon)));
+
+ if(sd_event_add_time(afb_daemon_get_event_loop(binder_interface->daemon), &source,
+ CLOCK_BOOTTIME, usec, TIMERFD_ACCURACY, send_request, request) < 0)
+ {
+ ERROR(binder_interface, "add_recurring_request: Request fails to be schedule through event loop");
+ added = false;
+ }
+ recurring_requests_.push_back(entry);
+ }
+ else
+ {
+ WARNING(binder_interface, "add_recurring_request: There isn't enough request entry. Vector exhausted %d/%d", (int)recurring_requests_.size(), MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ recurring_requests_.resize(MAX_SIMULTANEOUS_DIAG_REQUESTS);
+ added = false;
+ }
+ }
+ else
+ {
+ DEBUG(binder_interface, "add_recurring_request: Can't add request, one already exists with same key");
+ added = false;
+ }
+ return added;
+}
+
+/// @brief Returns true if there are two active requests running for the same arbitration ID.
+bool diagnostic_manager_t::conflicting(active_diagnostic_request_t* request, active_diagnostic_request_t* candidate) const
+{
+ return (candidate->get_in_flight() && candidate != request &&
+ candidate->get_can_bus_dev() == request->get_can_bus_dev() &&
+ candidate->get_id() == request->get_id());
+}
+
+
+/// @brief Returns true if there are no other active requests to the same arbitration ID
+/// and if there aren't more than 8 requests in flight at the same time.
+bool diagnostic_manager_t::clear_to_send(active_diagnostic_request_t* request) const
+{
+ int total_in_flight = 0;
+ for ( auto entry : non_recurring_requests_)
+ {
+ if(conflicting(request, entry))
+ return false;
+ if(entry->get_in_flight())
+ total_in_flight++;
+ }
+
+ for ( auto entry : recurring_requests_)
+ {
+ if(conflicting(request, entry))
+ return false;
+ if(entry->get_in_flight())
+ total_in_flight++;
+ }
+
+ if(total_in_flight > MAX_SIMULTANEOUS_IN_FLIGHT_REQUESTS)
+ return false;
+ return true;
+}
+
+int diagnostic_manager_t::reschedule_request(sd_event_source *s, uint64_t usec, active_diagnostic_request_t* adr)
+{
+ usec = usec + (uint64_t)(adr->get_frequency_clock().frequency_to_period());
+ DEBUG(binder_interface, "send_request: Event loop state: %d. usec: %ld", sd_event_get_state(afb_daemon_get_event_loop(binder_interface->daemon)), usec);
+ if(sd_event_source_set_time(s, usec) >= 0)
+ if(sd_event_source_set_enabled(s, SD_EVENT_ON) >= 0)
+ return 0;
+ sd_event_source_unref(s);
+ return -1;
+}
+
+/// @brief Systemd timer event callback use to send CAN messages at regular interval. Depending
+/// on the diagnostic message frequency.
+///
+/// This should be called from systemd binder event loop and the event is created on add_recurring_request
+///
+/// @param[in] s - Systemd event source pointer used to reschedule the new iteration.
+/// @param[in] usec - previous call timestamp in microseconds.
+/// @param[in] userdata - the DiagnosticRequest struct, use to retrieve the active request from the list.
+///
+/// @return positive integer if sent and rescheduled or negative value if something wrong. If an error occurs
+/// event will be disabled.
+int diagnostic_manager_t::send_request(sd_event_source *s, uint64_t usec, void *userdata)
+{
+ diagnostic_manager_t& dm = configuration_t::instance().get_diagnostic_manager();
+ DiagnosticRequest* request = (DiagnosticRequest*)userdata;
+ active_diagnostic_request_t* adr = dm.find_recurring_request(request);
+
+ dm.cleanup_active_requests(false);
+ if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev() && adr->should_send() &&
+ dm.clear_to_send(adr))
+ {
+ adr->get_frequency_clock().tick();
+ start_diagnostic_request(&dm.shims_, adr->get_handle());
+ if(adr->get_handle()->completed && !adr->get_handle()->success)
+ {
+ ERROR(binder_interface, "send_request: Fatal error sending diagnostic request");
+ sd_event_source_unref(s);
+ return -1;
+ }
+
+ adr->get_timeout_clock().tick();
+ adr->set_in_flight(true);
+ }
+
+ if(adr != nullptr && adr->get_recurring())
+ {
+ return dm.reschedule_request(s, usec, adr);
+ }
+
+ sd_event_source_unref(s);
+ NOTICE(binder_interface, "send_request: Request doesn't exist anymore. Canceling.'");
+ return -2;
+}
+
+/// @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 && strnlen(adr->get_name().c_str(), 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)
+ {
+ std::vector<diagnostic_message_t*> found_signals;
+ configuration_t::instance().find_diagnostic_messages( build_DynamicField(adr->get_name()), found_signals );
+ found_signals.front()->set_supported(false);
+ cleanup_request(adr, true);
+ NOTICE(binder_interface, "relay_diagnostic_response: PID not supported or ill formed. Please unsubscribe from it. Error code : %d", 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);
+ }
+
+ 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();
+ }
+
+ 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..3edb2b12
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-manager.hpp
@@ -0,0 +1,99 @@
+/*
+ * 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 "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 {
+protected:
+ 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();
+
+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*/
+
+ void init_diagnostic_shims();
+ void reset();
+public:
+ diagnostic_manager_t();
+
+ bool initialize();
+
+ std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
+ 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);
+
+ void checkSupportedPids(const active_diagnostic_request_t& request,
+ const DiagnosticResponse& response, float parsedPayload);
+
+ // Subscription parts
+ bool add_request(DiagnosticRequest* request, const std::string name,
+ bool waitForMultipleResponses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback);
+ bool validate_optional_request_attributes(float frequencyHz);
+ bool add_recurring_request(DiagnosticRequest* request, const char* name,
+ bool waitForMultipleResponses, const DiagnosticResponseDecoder decoder,
+ const DiagnosticResponseCallback callback, float frequencyHz);
+
+ // Sendig requests part
+ bool conflicting(active_diagnostic_request_t* request, active_diagnostic_request_t* candidate) const;
+ bool clear_to_send(active_diagnostic_request_t* request) const;
+ int reschedule_request(sd_event_source *s, uint64_t usec, active_diagnostic_request_t* adr);
+ static int send_request(sd_event_source *s, uint64_t usec, void *userdata);
+
+ // 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..6f615574
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-message.cpp
@@ -0,0 +1,108 @@
+/*
+ * 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 "../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;
+}
+
+///
+/// @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..064904d2
--- /dev/null
+++ b/CAN-binder/low-can-binding/diagnostic/diagnostic-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 "uds/uds.h"
+#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
+};
+
+///
+/// @brief - A representation of an OBD-II PID.
+///
+class diagnostic_message_t {
+ private:
+ 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);
+
+ 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/low-can-binding.cpp b/CAN-binder/low-can-binding/low-can-binding.cpp
new file mode 100644
index 00000000..07b3f1ea
--- /dev/null
+++ b/CAN-binder/low-can-binding/low-can-binding.cpp
@@ -0,0 +1,279 @@
+/*
+ * 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-binding.hpp"
+
+#include <map>
+#include <queue>
+#include <mutex>
+#include <vector>
+#include <thread>
+#include <time.h>
+#include <linux/can.h>
+#include <json-c/json.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>
+};
+
+// Interface between the daemon and the binding
+const struct afb_binding_interface *binder_interface;
+
+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);
+ }
+}
+
+///******************************************************************************
+///
+/// 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, "make_subscription_unsubscription: Operation goes wrong for signal: %s", 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, "create_event_handle: Can't create an event for %s, something goes wrong.", sig_name.c_str());
+ return 0;
+ }
+ return 1;
+}
+
+static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig)
+{
+ int ret;
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+ if (s.find(sig) != s.end())
+ {
+ if (!afb_event_is_valid(s[sig]) && !subscribe)
+ {
+ NOTICE(binder_interface, "subscribe_unsubscribe_signal: Event isn't valid, it can't be unsubscribed.");
+ 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};
+ subscribed_signals[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);
+}
+
+///
+/// @fn static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<can_signal_t>& signals)
+/// @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] can_signal_t vector with can_signal_t to subscribe
+///
+/// @return Number of correctly subscribed signal
+///
+static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector<std::string>& signals)
+{
+ int rets = 0;
+
+ //TODO: Implement way to dynamically call the right function no matter
+ // how much signals types we have.
+
+ for(const std::string& sig : signals)
+ {
+ int ret;
+ if (active_diagnostic_request_t::is_diagnostic_signal(sig))
+ {
+ diagnostic_message_t* diag_msg = configuration_t::instance().get_diagnostic_message(sig);
+ DiagnosticRequest* diag_req = configuration_t::instance().get_request_from_diagnostic_message(diag_msg);
+
+ // If the requested diagnostic message isn't supported by the car then unssubcribe.
+ // no matter what we want, worse case will be a fail unsubscription but at least we don't
+ // poll a PID for nothing.
+ if(diag_msg->get_supported() && subscribe)
+ {
+ float frequency = diag_msg->get_frequency();
+ subscribe = configuration_t::instance().get_diagnostic_manager().add_recurring_request(
+ diag_req, sig.c_str(), false, diag_msg->get_decoder(), diag_msg->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
+ {
+ configuration_t::instance().get_diagnostic_manager().cleanup_request(
+ configuration_t::instance().get_diagnostic_manager().find_recurring_request(diag_req), true);
+ WARNING(binder_interface, "Signal: %s isn't supported. Canceling operation.", sig.c_str());
+ return -1;
+ }
+ }
+
+ ret = subscribe_unsubscribe_signal(request, subscribe, sig);
+ if(ret <= 0)
+ return ret;
+ rets++;
+
+ DEBUG(binder_interface, "Signal: %s subscribed", sig.c_str());
+ }
+ return rets;
+}
+
+static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name)
+{
+ std::vector<std::string> signals;
+ int ret = 0;
+
+ openxc_DynamicField search_key = build_DynamicField(std::string(name));
+ signals = find_signals(search_key);
+ if (signals.empty())
+ ret = 0;
+
+ ret = subscribe_unsubscribe_signals(request, subscribe, signals);
+ NOTICE(binder_interface, "Subscribed/unsubscribe correctly to %d/%d signal(s).", ret, (int)signals.size());
+
+ return ret;
+}
+
+static void subscribe_unsubscribe(struct afb_req request, bool subscribe)
+{
+ int ok, i, n;
+ struct json_object *args, *a, *x;
+
+ /* makes the subscription/unsubscription */
+ args = afb_req_json(request);
+ if (args == NULL || !json_object_object_get_ex(args, "event", &a)) {
+ ok = subscribe_unsubscribe_name(request, subscribe, "*");
+ } else if (json_object_get_type(a) != json_type_array) {
+ ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a));
+ } else {
+ n = json_object_array_length(a);
+ ok = 0;
+ for (i = 0 ; i < n ; i++) {
+ x = json_object_array_get_idx(a, i);
+ if (subscribe_unsubscribe_name(request, subscribe, json_object_get_string(x)))
+ ok++;
+ }
+ ok = (ok == n);
+ }
+
+ /* send the report */
+ if (ok)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
+}
+
+extern "C"
+{
+ static void subscribe(struct afb_req request)
+ {
+ subscribe_unsubscribe(request, true);
+ }
+
+ static void unsubscribe(struct afb_req request)
+ {
+ subscribe_unsubscribe(request, false);
+ }
+
+ 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();
+
+ /// Initialize CAN socket
+ if(can_bus_manager.init_can_dev() == 0)
+ {
+ 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, "There was something wrong with CAN device Initialization. Check your config file maybe");
+ return 1;
+ }
+};
diff --git a/CAN-binder/low-can-binding/low-can-binding.hpp b/CAN-binder/low-can-binding/low-can-binding.hpp
new file mode 100644
index 00000000..ed5741d8
--- /dev/null
+++ b/CAN-binder/low-can-binding/low-can-binding.hpp
@@ -0,0 +1,33 @@
+/*
+ * 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>
+
+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); \ No newline at end of file
diff --git a/CAN-binder/low-can-binding/packaging/wgt/config.xml b/CAN-binder/low-can-binding/packaging/wgt/config.xml
new file mode 100644
index 00000000..d735ce2b
--- /dev/null
+++ b/CAN-binder/low-can-binding/packaging/wgt/config.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<widget xmlns="http://www.w3.org/ns/widgets" id="@PROJECT_NAME@" version="@PROJECT_VERSION@">
+ <name>@PROJECT_NAME@</name>
+ <icon src="icon.png"/>
+ <content src="libs" type="application/vnd.agl.service"/>
+ <description>This is a demo application used to control and dialog with HVAC system</description>
+ <author>Romain Forlot &lt;romain.forlot@iot.bzh&gt;</author>
+ <license>APL 2.0</license>
+</widget>
+
+
diff --git a/CAN-binder/low-can-binding/packaging/wgt/etc/can_buses.json b/CAN-binder/low-can-binding/packaging/wgt/etc/can_buses.json
new file mode 100644
index 00000000..c4292df2
--- /dev/null
+++ b/CAN-binder/low-can-binding/packaging/wgt/etc/can_buses.json
@@ -0,0 +1,3 @@
+{
+ "canbus": "can0"
+}
diff --git a/CAN-binder/low-can-binding/packaging/wgt/icon.png b/CAN-binder/low-can-binding/packaging/wgt/icon.png
new file mode 100644
index 00000000..9bd6a6e4
--- /dev/null
+++ b/CAN-binder/low-can-binding/packaging/wgt/icon.png
Binary files differ
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..e73b0f94
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/config-parser.cpp
@@ -0,0 +1,84 @@
+/*
+ * 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.
+ */
+
+namespace utils
+{
+ config_parser_t::config_parser_t(int conf_file)
+ : conf_file_{conf_file}, devices_name{}
+ {}
+
+ /// @brief read the conf_file_ and will parse json objects
+ /// in it searching for canbus objects devices name.
+ ///
+ /// @return Vector of can bus device name string.
+ void can_bus_t::read_conf()
+ {
+ FILE *fd = fdopen(conf_file_, "r");
+ if (fd)
+ {
+ std::fseek(fd, 0, SEEK_END);
+ config_content_.resize(std::ftell(fd));
+ std::rewind(fd);
+ std::fread(&config_content_[0], 1, config_content_.size(), fd);
+ std::fclose(fd);
+
+ DEBUG(binder_interface, "Configuration file content : %s", config_content_.c_str());
+ }
+ ERROR(binder_interface, "Problem at reading the conf file");
+ }
+
+ void parse_devices_name()
+ {
+ json_object *jo, *canbus;
+ const char* taxi;
+
+ jo = json_tokener_parse(config_content_.c_str());
+
+ if (jo == NULL || !json_object_object_get_ex(jo, "canbus", &canbus))
+ {
+ ERROR(binder_interface, "Can't find canbus node in the configuration file. Please review it.");
+ devices_name_.clear();
+ }
+ else if (json_object_get_type(canbus) != json_type_array)
+ {
+ taxi = json_object_get_string(canbus);
+ DEBUG(binder_interface, "Can bus found: %s", taxi);
+ devices_name_.push_back(std::string(taxi));
+ }
+ else
+ {
+ int n, i;
+ n = json_object_array_length(canbus);
+ for (i = 0 ; i < n ; i++)
+ devices_name_.push_back(json_object_get_string(json_object_array_get_idx(canbus, i)));
+ }
+ }
+
+ /// @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::string>& get_devices_name()
+ {
+ if(devices_name_.empty())
+ parse_devices_name();
+
+ 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..e6bd9d22
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/config-parser.hpp
@@ -0,0 +1,43 @@
+/*
+ * 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 <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <json-c/json.h>
+
+#include <string>
+
+namespace utils
+{
+ class config_parser_t
+ {
+ private:
+ int conf_file_; /*!< conf_file_ - file that handle the binding configuration file */
+ std::string config_content_; /*!< config_content_ - String that contains the content of config file */
+ std::vector<std::string> devices_name_; /*!< devices_name - Found devices name after reading configuration file */
+
+ void parse_devices_name();
+ public:
+ config_parser_t(int conf_file);
+
+ void read_conf();
+ std::vector<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..52b49d29
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/openxc-utils.cpp
@@ -0,0 +1,282 @@
+/*
+ * 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 "../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;
+
+ 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 = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev()->get_address();
+ 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..2e14a98f
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/signals.cpp
@@ -0,0 +1,76 @@
+/*
+ * 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"
+
+///
+/// @brief Can signal event map making access to afb_event
+/// externaly to an openxc existing structure.
+///
+/// Event map is making relation between can_signal_t generic name
+/// and the afb_event struct used by application framework to pushed
+/// to the subscriber.
+///
+std::map<std::string, struct afb_event> subscribed_signals;
+
+///
+/// @brief Mutex allowing safe manipulation on subscribed_signals map.
+/// To ensure that the map object isn't modified when we read it, you
+/// have to set this mutex before use subscribed_signals map object.
+///
+std::mutex subscribed_signals_mutex;
+
+std::mutex& get_subscribed_signals_mutex()
+{
+ return subscribed_signals_mutex;
+}
+
+std::map<std::string, struct afb_event>& 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.
+///
+std::vector<std::string> find_signals(const openxc_DynamicField &key)
+{
+ std::vector<std::string> found_signals_name;
+
+ switch(key.type)
+ {
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
+ lookup_signals_by_name(key.string_value, configuration_t::instance().get_can_signals(), found_signals_name);
+ lookup_signals_by_name(key.string_value, configuration_t::instance().get_diagnostic_messages(), found_signals_name);
+ break;
+ case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
+ lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_can_signals(), found_signals_name);
+ lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_diagnostic_messages(), found_signals_name);
+ break;
+ default:
+ ERROR(binder_interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
+ break;
+ }
+ DEBUG(binder_interface, "find_signals: Found %d signal(s)", (int)found_signals_name.size());
+ return found_signals_name;
+}
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..e9417569
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/signals.hpp
@@ -0,0 +1,90 @@
+/*
+ * 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 "../configuration.hpp"
+#include "../can/can-signals.hpp"
+#include "../diagnostic/diagnostic-message.hpp"
+
+#include "../low-can-binding.hpp"
+
+extern std::mutex subscribed_signals_mutex;
+std::mutex& get_subscribed_signals_mutex();
+
+/**
+ * @brief return the subscribed_signals map.
+ *
+ * return Map of subscribed signals.
+ */
+extern std::map<std::string, struct afb_event> subscribed_signals;
+std::map<std::string, struct afb_event>& get_subscribed_signals();
+
+template <typename T>
+void lookup_signals_by_name(const std::string& key, std::vector<T>& signals, std::vector<T*>& found_signals)
+{
+ for(T& s : signals)
+ {
+ if(::fnmatch(key.c_str(), s.get_generic_name().c_str(), FNM_CASEFOLD) == 0)
+ found_signals.push_back(&s);
+ 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_name(const std::string& key, std::vector<T>& signals, std::vector<std::string>& found_signals_name)
+{
+ for(T& s : signals)
+ {
+ if(::fnmatch(key.c_str(), s.get_generic_name().c_str(), FNM_CASEFOLD) == 0)
+ found_signals_name.push_back(s.get_name());
+ if(::fnmatch(key.c_str(), s.get_name().c_str(), FNM_CASEFOLD) == 0)
+ found_signals_name.push_back(s.get_name());
+ }
+}
+
+template <typename T>
+void lookup_signals_by_id(const double key, std::vector<T>& signals, std::vector<T*>& found_signals)
+{
+ for(T& s : signals)
+ {
+ if(configuration_t::instance().get_signal_id(s) == key)
+ {
+ found_signals.push_back(&s);
+ }
+ }
+}
+
+template <typename T>
+void lookup_signals_by_id(const double key, std::vector<T>& signals, std::vector<std::string>& found_signals_name)
+{
+ for(T& s : signals)
+ {
+ if(configuration_t::instance().get_signal_id(s) == key)
+ {
+ found_signals_name.push_back(s.get_name());
+ }
+ }
+}
+
+std::vector<std::string> find_signals(const openxc_DynamicField &key);
diff --git a/CAN-binder/low-can-binding/utils/socket.cpp b/CAN-binder/low-can-binding/utils/socket.cpp
new file mode 100644
index 00000000..e75e27f2
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socket.cpp
@@ -0,0 +1,89 @@
+/*
+ * 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 "socket.hpp"
+
+namespace utils
+{
+ /// @brief Construct a default, invalid, socket.
+ socket_t::socket_t()
+ : socket_{INVALID_SOCKET}
+ {
+ }
+
+ /// @brief Construct a socket by moving an existing one.
+ socket_t::socket_t(socket_t&& s)
+ : socket_{s.socket_}
+ {
+ s.socket_ = INVALID_SOCKET;
+ }
+
+ /// @brief Destruct the socket.
+ socket_t::~socket_t()
+ {
+ if(socket_ != INVALID_SOCKET)
+ ::close(socket_);
+ }
+
+ /// @brief Test if socket is valid.
+ /// @return true if valid, false otherwise.
+ socket_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 socket_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 socket_t::close()
+ {
+ return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
+ }
+
+ /// @brief Set socket option.
+ /// @return 0 if success.
+ int socket_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
+ {
+ return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
+ }
+
+ /// @brief Bind the socket.
+ /// @return 0 if success.
+ int socket_t::bind(const struct sockaddr* addr, socklen_t len)
+ {
+ return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
+ }
+
+ /// @brief Get the file descriptor.
+ /// @return The socket's file descriptor
+ int socket_t::socket() const
+ {
+ return socket_;
+ }
+}
diff --git a/CAN-binder/low-can-binding/utils/socket.hpp b/CAN-binder/low-can-binding/utils/socket.hpp
new file mode 100644
index 00000000..b42eee6a
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/socket.hpp
@@ -0,0 +1,46 @@
+#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 <sys/socket.h>
+
+#define INVALID_SOCKET -1
+
+namespace utils
+{
+ class socket_t
+ {
+ public:
+ socket_t();
+ socket_t(const socket_t&) = delete;
+ socket_t(socket_t&&);
+ ~socket_t();
+
+ explicit operator bool() const;
+
+ int open(int domain, int type, int protocol);
+ int close();
+ int setopt(int level, int optname, const void* optval, socklen_t optlen);
+ int socket() const;
+ int bind(const struct sockaddr* addr, socklen_t len);
+
+ private:
+ int socket_;
+ };
+}
+
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..a35a3e9f
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/timer.cpp
@@ -0,0 +1,102 @@
+/*
+ * 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 "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}
+{}
+
+/// @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()
+{
+ return 1 / frequency_ * unit_;
+}
+
+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 : get_time_function()() - 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()
+{
+ last_tick_ = get_time_function()();
+} \ 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..f5659044
--- /dev/null
+++ b/CAN-binder/low-can-binding/utils/timer.hpp
@@ -0,0 +1,53 @@
+/*
+ * 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
+
+/// @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.
+ unsigned long last_tick_; ///< the last time (in milliseconds 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, unsigned long last_tick, time_function_t time_function);
+
+ float get_frequency() const;
+
+ float frequency_to_period();
+ bool started();
+ time_function_t get_time_function();
+ bool elapsed(bool stagger);
+
+ void tick();
+}; \ No newline at end of file