From 2e93fc880e497ac553111ba27f2de4b44ea94027 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 28 Feb 2017 16:05:37 +0100 Subject: Make the thread function members of can_bus_t and can_bus_dev_t objects. Change-Id: I3cf06998c6ff6d859c7fdf6bf52a9b6ff061c556 Signed-off-by: Romain Forlot --- src/CMakeLists.txt | 2 +- src/can-bus.cpp | 158 +++++++++++++++++++++++++++++++++++---------- src/can-bus.hpp | 82 +++++++++++++---------- src/can_decode_message.cpp | 73 --------------------- src/can_decode_message.hpp | 19 ------ src/can_event_push.cpp | 55 ---------------- src/can_event_push.hpp | 19 ------ src/can_reader.cpp | 38 ----------- src/can_reader.hpp | 19 ------ 9 files changed, 171 insertions(+), 294 deletions(-) delete mode 100644 src/can_decode_message.cpp delete mode 100644 src/can_decode_message.hpp delete mode 100644 src/can_event_push.cpp delete mode 100644 src/can_event_push.hpp delete mode 100644 src/can_reader.cpp delete mode 100644 src/can_reader.hpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c47f4b6a..c60bc49b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,7 +84,7 @@ add_library(openxc STATIC ${PROJECT_LIBDIR}/openxc-message-format/gen/cpp/openxc message(STATUS "Creation of ${PROJECT_NAME} binding for AFB-DAEMON") ########################################################################### -add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}.cpp can-bus.cpp can-message.cpp can-signals.cpp can-decoder.cpp can_reader.cpp can_decode_message.cpp can_event_push.cpp openxc-utils.cpp timer.cpp) +add_library(${PROJECT_NAME} MODULE ${PROJECT_NAME}.cpp can-bus.cpp can-message.cpp can-signals.cpp can-decoder.cpp openxc-utils.cpp timer.cpp) target_link_libraries(${PROJECT_NAME} ${EXTRAS_LIBRARIES} bitfield isotp uds openxc pthread) set_target_properties(${PROJECT_NAME} PROPERTIES diff --git a/src/can-bus.cpp b/src/can-bus.cpp index 29cadba5..84cbaaa4 100644 --- a/src/can-bus.cpp +++ b/src/can-bus.cpp @@ -44,11 +44,89 @@ can_bus_t::can_bus_t(int& conf_file) { } +void can_bus_t::can_decode_message() +{ + can_message_t can_message; + std::vector signals; + std::vector ::iterator signals_i; + openxc_VehicleMessage vehicle_message; + openxc_DynamicField search_key, decoded_message; + + decoder_t decoder; + + DEBUG(binder_interface, "Beginning of decoding thread."); + while(is_decoding()) + { + { + std::unique_lock can_message_lock(can_message_mutex_); + new_can_message_.wait(can_message_lock); + can_message = next_can_message(); + } + + /* First we have to found which CanSignal it is */ + search_key = build_DynamicField((double)can_message.get_id()); + signals = find_can_signals(search_key); + + /* Decoding the message ! Don't kill the messenger ! */ + for(auto& sig : signals) + { + { + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); + std::map subscribed_signals = get_subscribed_signals(); + const auto& it_event = subscribed_signals.find(sig.genericName); + + if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) + { + decoded_message = decoder.translateSignal(sig, can_message, getSignals()); + + openxc_SimpleMessage s_message = build_SimpleMessage(sig.genericName, decoded_message); + vehicle_message = build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type::openxc_DynamicField_Type_NUM, s_message); + + std::lock_guard decoded_can_message_lock(decoded_can_message_mutex_); + push_new_vehicle_message(vehicle_message); + } + new_decoded_can_message_.notify_one(); + } + } + } +} + +void can_bus_t::can_event_push() +{ + openxc_VehicleMessage v_message; + openxc_SimpleMessage s_message; + json_object* jo; + + DEBUG(binder_interface, "Beginning of the pushing thread"); + while(is_pushing()) + { + { + std::unique_lock decoded_can_message_lock(decoded_can_message_mutex_); + new_decoded_can_message_.wait(decoded_can_message_lock); + v_message = next_vehicle_message(); + } + + s_message = get_simple_message(v_message); + + { + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); + std::map subscribed_signals = get_subscribed_signals(); + const auto& it_event = subscribed_signals.find(s_message.name); + if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) + { + jo = json_object_new_object(); + jsonify_simple(s_message, jo); + afb_event_push(it_event->second, jo); + } + } + } +} + void can_bus_t::start_threads() { - th_decoding_ = std::thread(can_decode_message, std::ref(*this)); + th_decoding_ = std::thread(&can_bus_t::can_decode_message, this); is_decoding_ = true; - th_pushing_ = std::thread(can_event_push, std::ref(*this)); + th_pushing_ = std::thread(&can_bus_t::can_event_push, this); is_pushing_ = true; } @@ -56,6 +134,8 @@ void can_bus_t::stop_threads() { is_decoding_ = false; is_pushing_ = false; + th_decoding_.join(); + th_pushing_.join(); } bool can_bus_t::is_decoding() @@ -154,16 +234,6 @@ std::mutex& can_bus_t::get_can_message_mutex() return can_message_mutex_; } -std::condition_variable& can_bus_t::get_new_decoded_can_message() -{ - return new_decoded_can_message_; -} - -std::mutex& can_bus_t::get_decoded_can_message_mutex() -{ - return decoded_can_message_mutex_; -} - can_message_t can_bus_t::next_can_message() { can_message_t can_msg; @@ -186,11 +256,6 @@ void can_bus_t::push_new_can_message(const can_message_t& can_msg) can_message_q_.push(can_msg); } -bool can_bus_t::has_can_message() const -{ - return has_can_message_; -} - openxc_VehicleMessage can_bus_t::next_vehicle_message() { openxc_VehicleMessage v_msg; @@ -214,11 +279,6 @@ void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg) has_vehicle_message_ = true; } -bool can_bus_t::has_vehicle_message() const -{ - return has_vehicle_message_; -} - /******************************************************************************** * * can_bus_dev_t method implementation @@ -226,50 +286,57 @@ bool can_bus_t::has_vehicle_message() const *********************************************************************************/ can_bus_dev_t::can_bus_dev_t(const std::string &dev_name) - : device_name_{dev_name} + : device_name_{dev_name}, can_socket_{-1} { } int can_bus_dev_t::open() { const int canfd_on = 1; + const int timestamp_on = 1; struct ifreq ifr; - struct timeval timeout = {1, 0}; + struct timeval timeout; DEBUG(binder_interface, "CAN Handler socket : %d", can_socket_); if (can_socket_ >= 0) return 0; can_socket_ = ::socket(PF_CAN, SOCK_RAW, CAN_RAW); + DEBUG(binder_interface, "CAN Handler socket correctly initialized : %d", can_socket_); if (can_socket_ < 0) - { - ERROR(binder_interface, "socket could not be created"); - } + ERROR(binder_interface, "socket could not be created. Error was : %s", ::strerror(errno)); else { /* Set timeout for read */ ::setsockopt(can_socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); + /* Set timestamp for receveid frame */ + if (::setsockopt(can_socket_, SOL_SOCKET, SO_TIMESTAMP, ×tamp_on, sizeof(timestamp_on)) < 0) + WARNING(binder_interface, "setsockopt SO_TIMESTAMP error: %s", ::strerror(errno)); + DEBUG(binder_interface, "Switch CAN Handler socket to use fd mode"); /* try to switch the socket into CAN_FD mode */ if (::setsockopt(can_socket_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { NOTICE(binder_interface, "Can not switch into CAN Extended frame format."); is_fdmode_on_ = false; } else { + DEBUG(binder_interface, "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, "ifr_name is : %s", ifr.ifr_name); if(::ioctl(can_socket_, SIOCGIFINDEX, &ifr) < 0) - ERROR(binder_interface, "ioctl failed"); + ERROR(binder_interface, "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 (::bind(can_socket_, (struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0) - ERROR(binder_interface, "Bind failed"); + ERROR(binder_interface, "Bind failed. %s", strerror(errno)); else return 0; } @@ -322,18 +389,39 @@ canfd_frame can_bus_dev_t::read() return canfd_frame; } +bool can_bus_dev_t::is_running() +{ + return is_running_; +} + void can_bus_dev_t::start_reading(can_bus_t& can_bus) { - th_reading_ = std::thread(can_reader, std::ref(*this), std::ref(can_bus)); + DEBUG(binder_interface, "Launching reading thread"); + th_reading_ = std::thread(&can_bus_dev_t::can_reader, this, std::ref(can_bus)); is_running_ = true; } -/* - * Return is_running_ bool - */ -bool can_bus_dev_t::is_running() +void can_bus_dev_t::stop_reading() { - return is_running_; + is_running_ = false; + th_reading_.join(); +} + +void can_bus_dev_t::can_reader(can_bus_t& can_bus) +{ + can_message_t can_message; + + DEBUG(binder_interface, "Beginning of reading thread"); + while(is_running()) + { + can_message.convert_from_canfd_frame(read()); + + { + std::lock_guard can_message_lock(can_bus.get_can_message_mutex()); + can_bus.push_new_can_message(can_message); + } + can_bus.get_new_can_message().notify_one(); + } } int can_bus_dev_t::send_can_message(can_message_t& can_msg) diff --git a/src/can-bus.hpp b/src/can-bus.hpp index 9100ff80..302514d8 100644 --- a/src/can-bus.hpp +++ b/src/can-bus.hpp @@ -49,19 +49,35 @@ class can_bus_t { private: int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/ - + + /** + * @brief thread to decoding raw CAN messages. + * + * @desc It will take from the can_message_q_ queue the next can message to process then it will 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. + */ + void can_decode_message(); std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */ bool is_decoding_; /*!< boolean member controling thread while loop*/ + + /** + * @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_event_push(); std::thread th_pushing_; /*!< thread that'll handle pushing decoded can frame to subscribers */ bool is_pushing_; /*!< boolean member controling thread while loop*/ - std::condition_variable new_can_message_; - std::mutex can_message_mutex_; + std::condition_variable new_can_message_; /*!< 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.*/ bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */ std::queue can_message_q_; /*!< queue that'll store can_message_t to decoded */ - std::condition_variable new_decoded_can_message_; - std::mutex decoded_can_message_mutex_; + 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.*/ bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */ std::queue vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */ @@ -88,11 +104,6 @@ class can_bus_t { */ std::vector read_conf(); - std::condition_variable& get_new_can_message(); - std::mutex& get_can_message_mutex(); - std::condition_variable& get_new_decoded_can_message(); - std::mutex& get_decoded_can_message_mutex(); - /** * @brief Will initialize threads that will decode * and push subscribed events. @@ -101,7 +112,8 @@ class can_bus_t { /** * @brief Will stop all threads holded by can_bus_t object - * which are decoding and pushing threads. + * which are decoding and pushing then will wait that's + * they'll finish their job. */ void stop_threads(); @@ -144,8 +156,10 @@ class can_bus_t { * * @return true if there is at least a can_message_t, false if not. */ - bool has_can_message() const; - + std::mutex& get_can_message_mutex(); + std::condition_variable& get_new_can_message(); + + /** * @brief Return first openxc_VehicleMessage on the queue * @@ -159,13 +173,6 @@ class can_bus_t { * @param the const reference openxc_VehicleMessage object to push into the queue */ void push_new_vehicle_message(const openxc_VehicleMessage& v_msg); - - /** - * @brief Return a boolean telling if there is any openxc_VehicleMessage into the queue - * - * @return true if there is at least a openxc_VehicleMessage, false if not. - */ - bool has_vehicle_message() const; }; /** @@ -183,6 +190,15 @@ class can_bus_dev_t { std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */ bool is_running_; /*!< boolean telling whether or not reading is running or not */ + + /** + * + * @brief Thread function used to read the can socket. + * + * @param[in] can_bus_dev_t object to be used to read the can socket + * @param[in] can_bus_t object used to fill can_message_q_ queue + */ + void can_reader(can_bus_t& can_bus); public: /** @@ -217,13 +233,19 @@ class can_bus_dev_t { bool is_running(); /** - * @brief start reading threads and set flag is_running_ - * - * @param can_bus_t reference can_bus_t. it will be passed to the thread - * to allow using can_bus_t queue. - */ + * @brief start reading threads and set flag is_running_ + * + * @param can_bus_t reference can_bus_t. it will be passed to the thread + * to allow using can_bus_t queue. + */ void start_reading(can_bus_t& can_bus); + /** + * @brief stop the reading thread setting flag is_running_ to false and + * and wait that the thread finish its job. + */ + void stop_reading(); + /** * @brief Read the can socket and retrieve canfd_frame * @@ -291,16 +313,6 @@ bool isBusActive(can_bus_dev_t* bus); */ void logBusStatistics(can_bus_dev_t* buses, const int busCount); -/** - * @fn void can_reader(can_bus_dev_t& can_bus_dev, can_bus_t& can_bus); - * - * @brief Thread function used to read the can socket. - * - * @param[in] can_bus_dev_t object to be used to read the can socket - * @param[in] can_bus_t object used to fill can_message_q_ queue - */ -void can_reader(can_bus_dev_t& can_bus_dev, can_bus_t& can_bus); - /** * @fn void can_decode_message(can_bus_t& can_bus); * diff --git a/src/can_decode_message.cpp b/src/can_decode_message.cpp deleted file mode 100644 index d4443ee9..00000000 --- a/src/can_decode_message.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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_decode_message.hpp" - -#include "can-bus.hpp" -#include "openxc-utils.hpp" -#include "can-signals.hpp" -#include "can-decoder.hpp" - -#include "can_reader.hpp" - -void can_decode_message(can_bus_t &can_bus) -{ - can_message_t can_message; - std::vector signals; - std::vector ::iterator signals_i; - openxc_VehicleMessage vehicle_message; - openxc_DynamicField search_key, decoded_message; - - decoder_t decoder; - - while(can_bus.is_decoding()) - { - { - std::unique_lock can_message_lock(can_bus.get_can_message_mutex()); - can_bus.get_new_can_message().wait(can_message_lock); - can_message = can_bus.next_can_message(); - } - - /* First we have to found which CanSignal it is */ - search_key = build_DynamicField((double)can_message.get_id()); - signals = find_can_signals(search_key); - - /* Decoding the message ! Don't kill the messenger ! */ - for(auto& sig : signals) - { - { - std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); - std::map subscribed_signals = get_subscribed_signals(); - const auto& it_event = subscribed_signals.find(sig.genericName); - - if(it_event != subscribed_signals.end() && - afb_event_is_valid(it_event->second)) - { - decoded_message = decoder.translateSignal(sig, can_message, getSignals()); - - openxc_SimpleMessage s_message = build_SimpleMessage(sig.genericName, decoded_message); - vehicle_message = build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type::openxc_DynamicField_Type_NUM, s_message); - - std::lock_guard decoded_can_message_lock(can_bus.get_decoded_can_message_mutex()); - can_bus.push_new_vehicle_message(vehicle_message); - } - can_bus.get_new_decoded_can_message().notify_one(); - } - } - } -} diff --git a/src/can_decode_message.hpp b/src/can_decode_message.hpp deleted file mode 100644 index e5972416..00000000 --- a/src/can_decode_message.hpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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 diff --git a/src/can_event_push.cpp b/src/can_event_push.cpp deleted file mode 100644 index 3baaaf77..00000000 --- a/src/can_event_push.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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_event_push.hpp" - -#include "can-bus.hpp" -#include "can-signals.hpp" -#include "openxc-utils.hpp" - -#include "can_decode_message.hpp" - -void can_event_push(can_bus_t& can_bus) -{ - openxc_VehicleMessage v_message; - openxc_SimpleMessage s_message; - json_object* jo; - - while(can_bus.is_pushing()) - { - { - std::unique_lock decoded_can_message_lock(can_bus.get_decoded_can_message_mutex()); - can_bus.get_new_decoded_can_message().wait(decoded_can_message_lock); - v_message = can_bus.next_vehicle_message(); - } - - s_message = get_simple_message(v_message); - - { - std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); - std::map subscribed_signals = get_subscribed_signals(); - const auto& it_event = subscribed_signals.find(s_message.name); - if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) - { - jo = json_object_new_object(); - jsonify_simple(s_message, jo); - afb_event_push(it_event->second, jo); - } - } - } -} \ No newline at end of file diff --git a/src/can_event_push.hpp b/src/can_event_push.hpp deleted file mode 100644 index e5972416..00000000 --- a/src/can_event_push.hpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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 diff --git a/src/can_reader.cpp b/src/can_reader.cpp deleted file mode 100644 index 4ee1c48b..00000000 --- a/src/can_reader.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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_reader.hpp" - -#include "low-can-binding.hpp" -#include "can-bus.hpp" - -void can_reader(can_bus_dev_t &can_bus_dev, can_bus_t& can_bus) -{ - can_message_t can_message; - - while(can_bus_dev.is_running()) - { - can_message.convert_from_canfd_frame(can_bus_dev.read()); - - { - std::lock_guard can_message_lock(can_bus.get_can_message_mutex()); - can_bus.push_new_can_message(can_message); - } - can_bus.get_new_can_message().notify_one(); - } -} \ No newline at end of file diff --git a/src/can_reader.hpp b/src/can_reader.hpp deleted file mode 100644 index e5972416..00000000 --- a/src/can_reader.hpp +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2015, 2016 "IoT.bzh" - * Author "Romain Forlot" - * Author "Loic Collignon" - * - * 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 -- cgit 1.2.3-korg