2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include <linux/can.h>
24 #include <condition_variable>
27 #include "openxc.pb.h"
28 #include "can-signals.hpp"
29 #include "can-message.hpp"
30 #include "low-can-binding.hpp"
32 // TODO actual max is 32 but dropped to 24 for memory considerations
33 #define MAX_ACCEPTANCE_FILTERS 24
34 // TODO this takes up a ton of memory
35 #define MAX_DYNAMIC_MESSAGE_COUNT 12
37 #define CAN_ACTIVE_TIMEOUT_S 30
41 * @brief Object used to handle decoding and manage event queue to be pushed.
43 * This object is also used to initialize can_bus_dev_t object after reading
44 * json conf file describing the CAN devices to use. Thus, those object will read
45 * on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
47 * That queue will be later used to be decoded and pushed to subscribers.
51 int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
54 * @brief thread to decoding raw CAN messages.
56 * @desc It will take from the can_message_q_ queue the next can message to process then it will search
57 * about signal subscribed if there is a valid afb_event for it. We only decode signal for which a
58 * subscription has been made. Can message will be decoded using translateSignal that will pass it to the
59 * corresponding decoding function if there is one assigned for that signal. If not, it will be the default
60 * noopDecoder function that will operate on it.
62 void can_decode_message();
63 std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
64 bool is_decoding_; /*!< boolean member controling thread while loop*/
67 * @brief thread to push events to suscribers. It will read subscribed_signals map to look
68 * which are events that has to be pushed.
70 void can_event_push();
71 std::thread th_pushing_; /*!< thread that'll handle pushing decoded can frame to subscribers */
72 bool is_pushing_; /*!< boolean member controling thread while loop*/
74 std::condition_variable new_can_message_; /*!< condition_variable use to wait until there is a new CAN message to read*/
75 std::mutex can_message_mutex_; /*!< mutex protecting the can_message_q_ queue.*/
76 bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */
77 std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
79 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_*/
80 std::mutex decoded_can_message_mutex_; /*!< mutex protecting the vehicle_message_q_ queue.*/
81 bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */
82 std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
86 * @brief Class constructor
88 * @param struct afb_binding_interface *interface between daemon and binding
89 * @param int file handle to the json configuration file.
91 can_bus_t(int& conf_file);
94 * @brief Will initialize can_bus_dev_t objects after reading
95 * the configuration file passed in the constructor.
100 * @brief read the conf_file_ and will parse json objects
101 * in it searching for canbus objects devices name.
103 * @return Vector of can bus device name string.
105 std::vector<std::string> read_conf();
108 * @brief Will initialize threads that will decode
109 * and push subscribed events.
111 void start_threads();
114 * @brief Will stop all threads holded by can_bus_t object
115 * which are decoding and pushing then will wait that's
116 * they'll finish their job.
121 * @brief Telling if the decoding thread is running.
122 * This is the boolean value on which the while loop
123 * take its condition. Set it to false will stop the
126 * @return true if decoding thread is running, false if not.
131 * @brief Telling if the pushing thread is running
132 * This is the boolean value on which the while loop
133 * take its condition. Set it to false will stop the
136 * @return true if pushing thread is running, false if not.
141 * @brief Return first can_message_t on the queue
143 * @return a can_message_t
145 can_message_t next_can_message();
148 * @brief Push a can_message_t into the queue
150 * @param the const reference can_message_t object to push into the queue
152 void push_new_can_message(const can_message_t& can_msg);
155 * @brief Return a boolean telling if there is any can_message into the queue
157 * @return true if there is at least a can_message_t, false if not.
159 std::mutex& get_can_message_mutex();
160 std::condition_variable& get_new_can_message();
164 * @brief Return first openxc_VehicleMessage on the queue
166 * @return a openxc_VehicleMessage containing a decoded can message
168 openxc_VehicleMessage next_vehicle_message();
171 * @brief Push a openxc_VehicleMessage into the queue
173 * @param the const reference openxc_VehicleMessage object to push into the queue
175 void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
179 * @class can_bus_dev_t
181 * @brief Object representing a can device. Handle opening, closing and reading on the
182 * socket. This is the low level object to be use by can_bus_t.
184 class can_bus_dev_t {
186 std::string device_name_; /*!< std::string device_name_ - name of the linux device handling the can bus. Generally vcan0, can0, etc. */
187 int can_socket_; /*!< socket handler for the can device */
188 bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */
189 struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */
191 std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */
192 bool is_running_; /*!< boolean telling whether or not reading is running or not */
196 * @brief Thread function used to read the can socket.
198 * @param[in] can_bus_dev_t object to be used to read the can socket
199 * @param[in] can_bus_t object used to fill can_message_q_ queue
201 void can_reader(can_bus_t& can_bus);
205 * @brief Class constructor
207 * @param const string representing the device name into the linux /dev tree
209 can_bus_dev_t(const std::string& dev_name);
212 * @brief Open the can socket and returning it
219 * @brief Open the can socket and returning it
226 * @brief Telling if the reading thread is running
227 * This is the boolean value on which the while loop
228 * take its condition. Set it to false will stop the
231 * @return true if reading thread is running, false if not.
236 * @brief start reading threads and set flag is_running_
238 * @param can_bus_t reference can_bus_t. it will be passed to the thread
239 * to allow using can_bus_t queue.
241 void start_reading(can_bus_t& can_bus);
244 * @brief stop the reading thread setting flag is_running_ to false and
245 * and wait that the thread finish its job.
250 * @brief Read the can socket and retrieve canfd_frame
252 * @param const struct afb_binding_interface* interface pointer. Used to be able to log
253 * using application framework logger.
258 * @brief Send a can message from a can_message_t object.
260 * @param const can_message_t& can_msg: the can message object to send
261 * @param const struct afb_binding_interface* interface pointer. Used to be able to log
262 * using application framework logger.
264 int send_can_message(can_message_t& can_msg);
268 * @brief Return an array of the metadata for the 2 CAN buses you want to
269 * monitor. The size of this array is fixed at 2.
271 can_bus_dev_t getCanBuses();
274 * @fn void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
275 * @brief Pre initialize actions made before CAN bus initialization
277 * @param[in] can_bus_dev_t bus - A CanBus struct defining the bus's metadata
278 * @param[in] bool writable - configure the controller in a writable mode. If false, it will be
279 * configured as "listen only" and will not allow writes or even CAN ACKs.
280 * @param[in] buses - An array of all CAN buses.
281 * @param[in] int busCount - The length of the buses array.
283 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
286 * @fn void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
287 * @brief Post-initialize actions made after CAN bus initialization
289 * @param[in] bus - A CanBus struct defining the bus's metadata
290 * @param[in] writable - configure the controller in a writable mode. If false, it will be
291 * configured as "listen only" and will not allow writes or even CAN ACKs.
292 * @param[in] buses - An array of all CAN buses.
293 * @param[in] busCount - The length of the buses array.
295 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
298 * @fn bool isBusActive(can_bus_dev_t* bus);
299 * @brief Check if the device is connected to an active CAN bus, i.e. it's
300 * received a message in the recent past.
302 * @return true if a message was received on the CAN bus within
303 * CAN_ACTIVE_TIMEOUT_S seconds.
305 bool isBusActive(can_bus_dev_t* bus);
308 * @fn void logBusStatistics(can_bus_dev_t* buses, const int busCount);
309 * @brief Log transfer statistics about all active CAN buses to the debug log.
311 * @param[in] buses - an array of active CAN buses.
312 * @param[in] busCount - the length of the buses array.
314 void logBusStatistics(can_bus_dev_t* buses, const int busCount);
317 * @fn void can_decode_message(can_bus_t& can_bus);
319 * @brief Thread function used to decode can messages read into the can_message_q_
321 * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
322 * into vehicle_message_q_ queue.
324 void can_decode_message(can_bus_t& can_bus);
327 * @fn void can_decode_message(can_bus_t& can_bus);
329 * @brief Thread function used to push afb_event
331 * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
332 * into vehicle_message_q_ queue.
334 void can_event_push(can_bus_t& can_bus);