97b683d7f543382d6a563b6a9e8caf7cfd86489f
[apps/low-level-can-service.git] / src / can / can-bus.hpp
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #pragma once
20
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 #include <linux/can.h>
25 #include <condition_variable>
26
27 #include "openxc.pb.h"
28 #include "../utils/timer.hpp"
29 #include "can-signals.hpp"
30 #include "can-message.hpp"
31 #include "can-bus-dev.hpp"
32 #include "../obd2/active-diagnostic-request.hpp"
33
34 #include "../low-can-binding.hpp"
35
36 // TODO actual max is 32 but dropped to 24 for memory considerations
37 #define MAX_ACCEPTANCE_FILTERS 24
38 // TODO this takes up a ton of memory
39 #define MAX_DYNAMIC_MESSAGE_COUNT 12
40
41 #define CAN_ACTIVE_TIMEOUT_S 30
42
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 class can_bus_t
51 {
52 private:
53         int conf_file_; /// < configuration file handle used to initialize can_bus_dev_t objects.
54
55         void can_decode_message();
56         std::thread th_decoding_; /// < thread that'll handle decoding a can frame
57         bool is_decoding_ = false; /// < boolean member controling thread while loop
58
59         void can_event_push();
60         std::thread th_pushing_; /// < thread that'll handle pushing decoded can frame to subscribers
61         bool is_pushing_ = false; /// < boolean member controling thread while loop
62
63         std::condition_variable new_can_message_cv_; /// < condition_variable use to wait until there is a new CAN message to read
64         std::mutex can_message_mutex_; /// < mutex protecting the can_message_q_ queue.
65         std::queue <can_message_t> can_message_q_; /// < queue that'll store can_message_t to decoded
66
67         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_
68         std::mutex decoded_can_message_mutex_;  /// < mutex protecting the vehicle_message_q_ queue.
69         std::queue <openxc_VehicleMessage> vehicle_message_q_; /// < queue that'll store openxc_VehicleMessage to pushed
70
71         std::vector<std::shared_ptr<can_bus_dev_t>> can_devices_; /// < Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
72
73 public:
74         can_bus_t(int conf_file);
75         can_bus_t(can_bus_t&&);
76
77         int init_can_dev();
78         std::vector<std::string> read_conf();
79
80         void start_threads();
81         void stop_threads();
82
83         int process_can_signals(can_message_t& can_message);
84         int process_diagnostic_signals(active_diagnostic_request_t* adr, const can_message_t& can_message);
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         const std::vector<std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
95 };