Introducing diagnostic manager class.
[apps/low-level-can-service.git] / src / can / 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 "openxc.pb.h"
27 #include "utils/timer.hpp"
28 #include "can/can-signals.hpp"
29 #include "can/can-message.hpp"
30 #include "obd2/diagnostic-manager.hpp"
31
32 #include "low-can-binding.hpp"
33
34 // TODO actual max is 32 but dropped to 24 for memory considerations
35 #define MAX_ACCEPTANCE_FILTERS 24
36 // TODO this takes up a ton of memory
37 #define MAX_DYNAMIC_MESSAGE_COUNT 12
38
39 #define CAN_ACTIVE_TIMEOUT_S 30
40
41 class can_bus_dev_t;
42 class diagnostic_manager_t;
43
44 /** 
45  * @class can_bus_t
46  * @brief Object used to handle decoding and manage event queue to be pushed.
47  *
48  * This object is also used to initialize can_bus_dev_t object after reading 
49  * json conf file describing the CAN devices to use. Thus, those object will read 
50  * on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
51  *
52  * That queue will be later used to be decoded and pushed to subscribers.
53  */
54 class can_bus_t {
55         private:
56                 int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
57
58                 void can_decode_message();
59                 std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
60                 bool is_decoding_; /*!< boolean member controling thread while loop*/
61
62                 void can_event_push();
63                 std::thread th_pushing_; /*!<  thread that'll handle pushing decoded can frame to subscribers */
64                 bool is_pushing_; /*!< boolean member controling thread while loop*/
65
66                 std::condition_variable new_can_message_cv_; /*!< condition_variable use to wait until there is a new CAN message to read*/
67                 std::mutex can_message_mutex_; /*!< mutex protecting the can_message_q_ queue.*/
68                 std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
69
70                 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_*/
71                 std::mutex decoded_can_message_mutex_;  /*!< mutex protecting the vehicle_message_q_ queue.*/
72                 std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
73
74                 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*/
75
76         public:
77                 can_bus_t(int conf_file);
78
79                 int init_can_dev();
80                 std::vector<std::string> read_conf();
81                 
82
83                 void start_threads();
84                 void stop_threads();
85
86                 can_message_t next_can_message();
87                 void push_new_can_message(const can_message_t& can_msg);                
88                 std::mutex& get_can_message_mutex();
89                 std::condition_variable& get_new_can_message_cv();
90
91                 openxc_VehicleMessage next_vehicle_message();
92                 void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
93
94                 std::map<std::string, std::shared_ptr<can_bus_dev_t>> get_can_devices();
95 };
96
97
98 /**
99  * @class can_bus_dev_t 
100  *
101  * @brief Object representing a can device. Handle opening, closing and reading on the
102  *  socket. This is the low level object to be use by can_bus_t.
103  */
104 class can_bus_dev_t {
105         private:
106                 int32_t id_; /*!< int32_t id_ - an identifier used through binding that refer to that device*/
107                 std::string device_name_; /*!< std::string device_name_ - name of the linux device handling the can bus. Generally vcan0, can0, etc. */
108                 int can_socket_; /*!< socket handler for the can device */
109                 bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */
110                 struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */
111
112                 diagnostic_manager_t diagnostic_manager_; /*!< diagnostic_manager_t diagnostic_manager_ - A diagnostic_manager_t object instance
113                                                                                                    * that will handle diagnostic obd2 protocol requests and responses.*/
114
115                 std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */
116                 bool is_running_; /*!< boolean telling whether or not reading is running or not */
117                 void can_reader(can_bus_t& can_bus);
118
119         public:
120                 can_bus_dev_t(const std::string& dev_name);
121
122                 int open();
123                 int close();
124
125                 void start_reading(can_bus_t& can_bus);
126
127                 void stop_reading();
128
129                 std::pair<struct canfd_frame&, size_t> read();
130                 
131                 int send_can_message(can_message_t& can_msg);
132 };
133
134 /** TODO: implement this function as method into can_bus class
135  * @fn void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
136  * @brief Pre initialize actions made before CAN bus initialization
137  *
138  * @param[in] can_bus_dev_t bus - A CanBus struct defining the bus's metadata
139  * @param[in] bool writable - configure the controller in a writable mode. If false, it will be
140  *              configured as "listen only" and will not allow writes or even CAN ACKs.
141  * @param[in] buses - An array of all CAN buses.
142  * @param[in] int busCount - The length of the buses array.
143  */
144 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
145
146 /** TODO: implement this function as method into can_bus class
147  * @fn void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
148  * @brief Post-initialize actions made after CAN bus initialization
149  *
150  * @param[in] bus - A CanBus struct defining the bus's metadata
151  * @param[in] writable - configure the controller in a writable mode. If false, it will be
152  *              configured as "listen only" and will not allow writes or even CAN ACKs.
153  * @param[in] buses - An array of all CAN buses.
154  * @param[in] busCount - The length of the buses array.
155  */
156 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
157
158 /** TODO: implement this function as method into can_bus class
159  * @fn bool isBusActive(can_bus_dev_t* bus);
160  * @brief Check if the device is connected to an active CAN bus, i.e. it's
161  * received a message in the recent past.
162  *
163  * @return true if a message was received on the CAN bus within
164  * CAN_ACTIVE_TIMEOUT_S seconds.
165  */
166 bool isBusActive(can_bus_dev_t* bus);
167
168 /** TODO: implement this function as method into can_bus class
169  *
170  * @fn void logBusStatistics(can_bus_dev_t* buses, const int busCount);
171  * @brief Log transfer statistics about all active CAN buses to the debug log.
172  *
173  * @param[in] buses - an array of active CAN buses.
174  * @param[in] busCount - the length of the buses array.
175  */
176 void logBusStatistics(can_bus_dev_t* buses, const int busCount);