Re-arranging objects splitting all objects over
[apps/low-level-can-service.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 /** 
40  * @class can_bus_t
41  * @brief Object used to handle decoding and manage event queue to be pushed.
42  *
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.
46  *
47  * That queue will be later used to be decoded and pushed to subscribers.
48  */
49 class can_bus_t {
50         private:
51                 int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
52                 
53                 std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
54                 bool is_decoding_; /*!< boolean member controling thread while loop*/
55                 std::thread th_pushing_; /*!<  thread that'll handle pushing decoded can frame to subscribers */
56                 bool is_pushing_; /*!< boolean member controling thread while loop*/
57
58                 std::condition_variable new_can_message_;
59                 std::mutex can_message_mutex_;
60                 bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */
61                 std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
62
63                 std::condition_variable new_decoded_can_message_;
64                 std::mutex decoded_can_message_mutex_;
65                 bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */
66                 std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
67
68         public:
69                 /**
70                  * @brief Class constructor
71                  *
72                  * @param struct afb_binding_interface *interface between daemon and binding
73                  * @param int file handle to the json configuration file.
74                  */
75                 can_bus_t(int& conf_file);
76                 
77                 /**
78                  * @brief Will initialize can_bus_dev_t objects after reading 
79                  * the configuration file passed in the constructor.
80                  */
81                 int init_can_dev();
82
83                 /**
84                  * @brief read the conf_file_ and will parse json objects
85                  * in it searching for canbus objects devices name.
86                  *
87                  * @return Vector of can bus device name string.
88                  */
89                 std::vector<std::string> read_conf();
90                 
91                 std::condition_variable& get_new_can_message();
92                 std::mutex& get_can_message_mutex();
93                 std::condition_variable& get_new_decoded_can_message();
94                 std::mutex& get_decoded_can_message_mutex();
95
96                 /**
97                  * @brief Will initialize threads that will decode
98                  *  and push subscribed events.
99                  */
100                 void start_threads();
101
102                 /**
103                  * @brief Will stop all threads holded by can_bus_t object
104                  *  which are decoding and pushing threads.
105                  */
106                 void stop_threads();
107
108                 /**
109                  * @brief Telling if the decoding thread is running.
110                  *  This is the boolean value on which the while loop
111                  *  take its condition. Set it to false will stop the 
112                  *  according thread.
113                  *
114                  * @return true if decoding thread is running, false if not.
115                  */
116                 bool is_decoding();
117
118                 /**
119                  * @brief Telling if the pushing thread is running
120                  *  This is the boolean value on which the while loop
121                  *  take its condition. Set it to false will stop the 
122                  *  according thread.
123                  *
124                  * @return true if pushing thread is running, false if not.
125                  */
126                 bool is_pushing();
127
128                 /**
129                  * @brief Return first can_message_t on the queue 
130                  *
131                  * @return a can_message_t 
132                  */
133                 can_message_t next_can_message();
134                 
135                 /**
136                  * @brief Push a can_message_t into the queue
137                  *
138                  * @param the const reference can_message_t object to push into the queue
139                  */
140                 void push_new_can_message(const can_message_t& can_msg);                
141                 
142                 /**
143                  * @brief Return a boolean telling if there is any can_message into the queue
144                  *
145                  * @return true if there is at least a can_message_t, false if not.
146                  */
147                 bool has_can_message() const;
148                 
149                 /**
150                  * @brief Return first openxc_VehicleMessage on the queue 
151                  *
152                  * @return a openxc_VehicleMessage containing a decoded can message
153                  */
154                 openxc_VehicleMessage next_vehicle_message();
155                 
156                 /**
157                  * @brief Push a openxc_VehicleMessage into the queue
158                  *
159                  * @param the const reference openxc_VehicleMessage object to push into the queue
160                  */
161                 void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
162                 
163                 /**
164                  * @brief Return a boolean telling if there is any openxc_VehicleMessage into the queue
165                  *
166                  * @return true if there is at least a openxc_VehicleMessage, false if not.
167                  */
168                 bool has_vehicle_message() const;
169 };
170
171 /**
172  * @class can_bus_dev_t 
173  *
174  * @brief Object representing a can device. Handle opening, closing and reading on the
175  *  socket. This is the low level object to be use by can_bus_t.
176  */
177 class can_bus_dev_t {
178         private:
179                 std::string device_name_; /*!< std::string device_name_ - name of the linux device handling the can bus. Generally vcan0, can0, etc. */
180                 int can_socket_; /*!< socket handler for the can device */
181                 bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */
182                 struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */
183                 
184                 std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */
185                 bool is_running_; /*!< boolean telling whether or not reading is running or not */
186
187         public:
188                 /**
189                  * @brief Class constructor 
190                  * 
191                  * @param const string representing the device name into the linux /dev tree
192                  */
193                 can_bus_dev_t(const std::string& dev_name);
194
195                 /**
196                  * @brief Open the can socket and returning it 
197                  *
198                  * @return 
199                  */
200                 int open();
201                 
202                 /**
203                  * @brief Open the can socket and returning it 
204                  *
205                  * @return 
206                  */
207                 int close();
208                 
209                 /**
210                  * @brief Telling if the reading thread is running
211                  *  This is the boolean value on which the while loop
212                  *  take its condition. Set it to false will stop the 
213                  *  according thread.
214                  *
215                  * @return true if reading thread is running, false if not.
216                  */
217                 bool is_running();
218                 
219                 /**
220                 * @brief start reading threads and set flag is_running_
221                 *
222                 * @param can_bus_t reference can_bus_t. it will be passed to the thread 
223                 *  to allow using can_bus_t queue.
224                 */
225                 void start_reading(can_bus_t& can_bus);
226
227                 /**
228                 * @brief Read the can socket and retrieve canfd_frame
229                 *
230                 * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
231                 *  using application framework logger.
232                 */
233                 canfd_frame read();
234                 
235                 /**
236                 * @brief Send a can message from a can_message_t object.
237                 * 
238                 * @param const can_message_t& can_msg: the can message object to send 
239                 * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
240                 *  using application framework logger.
241                 */
242                 int send_can_message(can_message_t& can_msg);
243 };
244
245 /**
246  * @brief Return an array of the metadata for the 2 CAN buses you want to
247  * monitor. The size of this array is fixed at 2.
248  */
249 can_bus_dev_t getCanBuses();
250
251 /**
252  * @fn void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
253  * @brief Pre initialize actions made before CAN bus initialization
254  *
255  * @param[in] can_bus_dev_t bus - A CanBus struct defining the bus's metadata
256  * @param[in] bool writable - configure the controller in a writable mode. If false, it will be
257  *              configured as "listen only" and will not allow writes or even CAN ACKs.
258  * @param[in] buses - An array of all CAN buses.
259  * @param[in] int busCount - The length of the buses array.
260  */
261 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
262
263 /**
264  * @fn void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
265  * @brief Post-initialize actions made after CAN bus initialization
266  *
267  * @param[in] bus - A CanBus struct defining the bus's metadata
268  * @param[in] writable - configure the controller in a writable mode. If false, it will be
269  *              configured as "listen only" and will not allow writes or even CAN ACKs.
270  * @param[in] buses - An array of all CAN buses.
271  * @param[in] busCount - The length of the buses array.
272  */
273 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
274
275 /**
276  * @fn bool isBusActive(can_bus_dev_t* bus);
277  * @brief Check if the device is connected to an active CAN bus, i.e. it's
278  * received a message in the recent past.
279  *
280  * @return true if a message was received on the CAN bus within
281  * CAN_ACTIVE_TIMEOUT_S seconds.
282  */
283 bool isBusActive(can_bus_dev_t* bus);
284
285 /**
286  * @fn void logBusStatistics(can_bus_dev_t* buses, const int busCount);
287  * @brief Log transfer statistics about all active CAN buses to the debug log.
288  *
289  * @param[in] buses - an array of active CAN buses.
290  * @param[in] busCount - the length of the buses array.
291  */
292 void logBusStatistics(can_bus_dev_t* buses, const int busCount);
293
294 /**
295  * @fn void can_reader(can_bus_dev_t& can_bus_dev, can_bus_t& can_bus);
296  *
297  * @brief Thread function used to read the can socket.
298  *
299  * @param[in] can_bus_dev_t object to be used to read the can socket
300  * @param[in] can_bus_t object used to fill can_message_q_ queue
301  */
302 void can_reader(can_bus_dev_t& can_bus_dev, can_bus_t& can_bus);
303
304 /**
305  * @fn void can_decode_message(can_bus_t& can_bus);
306  *
307  * @brief Thread function used to decode can messages read into the can_message_q_
308  *
309  * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
310  * into vehicle_message_q_ queue.
311  */
312 void can_decode_message(can_bus_t& can_bus);
313
314 /**
315  * @fn void can_decode_message(can_bus_t& can_bus);
316  *
317  * @brief Thread function used to push afb_event
318  *
319  * @param[in] can_bus_t object used to pop can_message_q_ queue and fill decoded message
320  * into vehicle_message_q_ queue.
321  */
322 void can_event_push(can_bus_t& can_bus);