Fix: can bus object life is now expanded and don't
[apps/agl-service-can-low-level.git] / src / can-bus.hpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include <mutex>
21 #include <queue>
22 #include <thread>
23 #include <linux/can.h>
24 #include <condition_variable>
25
26 #include "timer.hpp"
27 #include "openxc.pb.h"
28 #include "can-signals.hpp"
29 #include "can-message.hpp"
30 #include "low-can-binding.hpp"
31
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
36
37 #define CAN_ACTIVE_TIMEOUT_S 30
38
39 class can_bus_dev_t;
40
41 /** 
42  * @class can_bus_t
43  * @brief Object used to handle decoding and manage event queue to be pushed.
44  *
45  * This object is also used to initialize can_bus_dev_t object after reading 
46  * json conf file describing the CAN devices to use. Thus, those object will read 
47  * on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
48  *
49  * That queue will be later used to be decoded and pushed to subscribers.
50  */
51 class can_bus_t {
52         private:
53                 int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
54
55                 /**
56                  * @brief thread to decoding raw CAN messages. 
57                  *
58                  * @desc It will take from the can_message_q_ queue the next can message to process then it will search 
59                  *  about signal subscribed if there is a valid afb_event for it. We only decode signal for which a 
60                  *  subscription has been made. Can message will be decoded using translateSignal that will pass it to the
61                  *  corresponding decoding function if there is one assigned for that signal. If not, it will be the default
62                  *  noopDecoder function that will operate on it.
63                  */
64                 void can_decode_message();
65                 std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
66                 bool is_decoding_; /*!< boolean member controling thread while loop*/
67
68                 /**
69                  * @brief thread to push events to suscribers. It will read subscribed_signals map to look 
70                  * which are events that has to be pushed.
71                  */
72                 void can_event_push();
73                 std::thread th_pushing_; /*!<  thread that'll handle pushing decoded can frame to subscribers */
74                 bool is_pushing_; /*!< boolean member controling thread while loop*/
75
76                 std::condition_variable new_can_message_; /*!< condition_variable use to wait until there is a new CAN message to read*/
77                 std::mutex can_message_mutex_; /*!< mutex protecting the can_message_q_ queue.*/
78                 bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */
79                 std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
80
81                 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_*/
82                 std::mutex decoded_can_message_mutex_;  /*!< mutex protecting the vehicle_message_q_ queue.*/
83                 bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */
84                 std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
85
86                 std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_devices_m_; /*!< Can device map containing all can_bus_dev_t objects initialized during init_can_dev function*/
87
88         public:
89                 /**
90                  * @brief Class constructor
91                  *
92                  * @param struct afb_binding_interface *interface between daemon and binding
93                  * @param int file handle to the json configuration file.
94                  */
95                 can_bus_t(int conf_file);
96                 
97                 /**
98                  * @brief Will initialize can_bus_dev_t objects after reading 
99                  * the configuration file passed in the constructor.
100                  */
101                 int init_can_dev();
102
103                 /**
104                  * @brief read the conf_file_ and will parse json objects
105                  * in it searching for canbus objects devices name.
106                  *
107                  * @return Vector of can bus device name string.
108                  */
109                 std::vector<std::string> read_conf();
110                 
111                 /**
112                  * @brief Will initialize threads that will decode
113                  *  and push subscribed events.
114                  */
115                 void start_threads();
116
117                 /**
118                  * @brief Will stop all threads holded by can_bus_t object
119                  *  which are decoding and pushing then will wait that's 
120                  * they'll finish their job.
121                  */
122                 void stop_threads();
123
124                 /**
125                  * @brief Telling if the decoding thread is running.
126                  *  This is the boolean value on which the while loop
127                  *  take its condition. Set it to false will stop the 
128                  *  according thread.
129                  *
130                  * @return true if decoding thread is running, false if not.
131                  */
132                 bool is_decoding();
133
134                 /**
135                  * @brief Telling if the pushing thread is running
136                  *  This is the boolean value on which the while loop
137                  *  take its condition. Set it to false will stop the 
138                  *  according thread.
139                  *
140                  * @return true if pushing thread is running, false if not.
141                  */
142                 bool is_pushing();
143
144                 /**
145                  * @brief Return first can_message_t on the queue 
146                  *
147                  * @return a can_message_t 
148                  */
149                 can_message_t next_can_message();
150                 
151                 /**
152                  * @brief Push a can_message_t into the queue
153                  *
154                  * @param the const reference can_message_t object to push into the queue
155                  */
156                 void push_new_can_message(const can_message_t& can_msg);                
157                 
158                 /**
159                  * @brief return can_message_mutex_ member
160                  *
161                  * @return  return can_message_mutex_ member
162                  */
163                 std::mutex& get_can_message_mutex();
164                 
165                 /**
166                  * @brief return new_can_message_ member
167                  *
168                  * @return  return new_can_message_ member
169                  */
170                 std::condition_variable& get_new_can_message();
171
172
173                 /**
174                  * @brief Return first openxc_VehicleMessage on the queue 
175                  *
176                  * @return a openxc_VehicleMessage containing a decoded can message
177                  */
178                 openxc_VehicleMessage next_vehicle_message();
179                 
180                 /**
181                  * @brief Push a openxc_VehicleMessage into the queue
182                  *
183                  * @param the const reference openxc_VehicleMessage object to push into the queue
184                  */
185                 void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
186 };
187
188
189 /**
190  * @class can_bus_dev_t 
191  *
192  * @brief Object representing a can device. Handle opening, closing and reading on the
193  *  socket. This is the low level object to be use by can_bus_t.
194  */
195 class can_bus_dev_t {
196         private:
197                 std::string device_name_; /*!< std::string device_name_ - name of the linux device handling the can bus. Generally vcan0, can0, etc. */
198                 int can_socket_; /*!< socket handler for the can device */
199                 bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */
200                 struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */
201                 
202                 std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */
203                 bool is_running_; /*!< boolean telling whether or not reading is running or not */
204                 
205                 /**
206                 *
207                 * @brief Thread function used to read the can socket.
208                 *
209                 * @param[in] can_bus_dev_t object to be used to read the can socket
210                 * @param[in] can_bus_t object used to fill can_message_q_ queue
211                 */
212                 void can_reader(can_bus_t& can_bus);
213
214         public:
215                 /**
216                  * @brief Class constructor 
217                  * 
218                  * @param const string representing the device name into the linux /dev tree
219                  */
220                 can_bus_dev_t(const std::string& dev_name);
221
222                 /**
223                  * @brief Open the can socket and returning it 
224                  *
225                  * @return 
226                  */
227                 int open();
228                 
229                 /**
230                  * @brief Open the can socket and returning it 
231                  *
232                  * @return 
233                  */
234                 int close();
235                 
236                 /**
237                  * @brief Telling if the reading thread is running
238                  *  This is the boolean value on which the while loop
239                  *  take its condition. Set it to false will stop the 
240                  *  according thread.
241                  *
242                  * @return true if reading thread is running, false if not.
243                  */
244                 bool is_running();
245                 
246                 /**
247                  * @brief start reading threads and set flag is_running_
248                  *
249                  * @param can_bus_t reference can_bus_t. it will be passed to the thread 
250                  *  to allow using can_bus_t queue.
251                  */
252                 void start_reading(can_bus_t& can_bus);
253
254                 /**
255                  * @brief stop the reading thread setting flag is_running_ to false and
256                  * and wait that the thread finish its job.
257                  */
258                 void stop_reading();
259
260                 /**
261                 * @brief Read the can socket and retrieve canfd_frame
262                 *
263                 * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
264                 *  using application framework logger.
265                 */
266                 canfd_frame read();
267                 
268                 /**
269                 * @brief Send a can message from a can_message_t object.
270                 * 
271                 * @param const can_message_t& can_msg: the can message object to send 
272                 * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
273                 *  using application framework logger.
274                 */
275                 int send_can_message(can_message_t& can_msg);
276 };
277
278 /**
279  * @brief Return an array of the metadata for the 2 CAN buses you want to
280  * monitor. The size of this array is fixed at 2.
281  */
282 can_bus_dev_t getCanBuses();
283
284 /**
285  * @fn void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
286  * @brief Pre initialize actions made before CAN bus initialization
287  *
288  * @param[in] can_bus_dev_t bus - A CanBus struct defining the bus's metadata
289  * @param[in] bool writable - configure the controller in a writable mode. If false, it will be
290  *              configured as "listen only" and will not allow writes or even CAN ACKs.
291  * @param[in] buses - An array of all CAN buses.
292  * @param[in] int busCount - The length of the buses array.
293  */
294 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
295
296 /**
297  * @fn void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
298  * @brief Post-initialize actions made after CAN bus initialization
299  *
300  * @param[in] bus - A CanBus struct defining the bus's metadata
301  * @param[in] writable - configure the controller in a writable mode. If false, it will be
302  *              configured as "listen only" and will not allow writes or even CAN ACKs.
303  * @param[in] buses - An array of all CAN buses.
304  * @param[in] busCount - The length of the buses array.
305  */
306 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
307
308 /**
309  * @fn bool isBusActive(can_bus_dev_t* bus);
310  * @brief Check if the device is connected to an active CAN bus, i.e. it's
311  * received a message in the recent past.
312  *
313  * @return true if a message was received on the CAN bus within
314  * CAN_ACTIVE_TIMEOUT_S seconds.
315  */
316 bool isBusActive(can_bus_dev_t* bus);
317
318 /**
319  * @fn void logBusStatistics(can_bus_dev_t* buses, const int busCount);
320  * @brief Log transfer statistics about all active CAN buses to the debug log.
321  *
322  * @param[in] buses - an array of active CAN buses.
323  * @param[in] busCount - the length of the buses array.
324  */
325 void logBusStatistics(can_bus_dev_t* buses, const int busCount);
326
327 /**
328  * @fn void can_decode_message(can_bus_t& can_bus);
329  *
330  * @brief Thread function used to decode can messages read into the can_message_q_
331  *
332  * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
333  * into vehicle_message_q_ queue.
334  */
335 void can_decode_message(can_bus_t& can_bus);
336
337 /**
338  * @fn void can_decode_message(can_bus_t& can_bus);
339  *
340  * @brief Thread function used to push afb_event
341  *
342  * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
343  * into vehicle_message_q_ queue.
344  */
345 void can_event_push(can_bus_t& can_bus);