Static code review fixes.
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / 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 "can-message.hpp"
29 #include "../utils/config-parser.hpp"
30 #include "../binding/low-can-hat.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 diagnostic_manager_t;
40
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 class can_bus_t
49 {
50 private:
51         utils::config_parser_t conf_file_; ///< configuration file handle used to initialize can_bus_dev_t objects.
52
53         int process_can_signals(const can_message_t& can_message);
54         int process_diagnostic_signals(diagnostic_manager_t& manager, const can_message_t& can_message);
55
56         void can_decode_message();
57         std::thread th_decoding_; ///< thread that'll handle decoding a can frame
58         bool is_decoding_ = false; ///< boolean member controling thread while loop
59
60         void can_event_push();
61         std::thread th_pushing_; ///< thread that'll handle pushing decoded can frame to subscribers
62         bool is_pushing_ = false; ///< boolean member controling thread while loop
63
64         std::condition_variable new_can_message_cv_; ///< condition_variable use to wait until there is a new CAN message to read
65         std::mutex can_message_mutex_; ///< mutex protecting the can_message_q_ queue.
66         std::queue <can_message_t> can_message_q_; ///< queue that'll store can_message_t to decoded
67
68         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_
69         std::mutex decoded_can_message_mutex_;  ///< mutex protecting the vehicle_message_q_ queue.
70         std::queue <openxc_VehicleMessage> vehicle_message_q_; ///< queue that'll store openxc_VehicleMessage to pushed
71
72         std::vector<std::pair<std::string, std::string> > can_devices_;
73 public:
74         explicit can_bus_t(utils::config_parser_t conf_file);
75         can_bus_t(can_bus_t&&);
76
77         void set_can_devices();
78         int get_can_device_index(const std::string& bus_name) const;
79         const std::string get_can_device_name(const std::string& id_name) const;
80
81         void start_threads();
82         void stop_threads();
83
84         can_message_t next_can_message();
85         void push_new_can_message(const can_message_t& can_msg);
86         std::mutex& get_can_message_mutex();
87         std::condition_variable& get_new_can_message_cv();
88
89         openxc_VehicleMessage next_vehicle_message();
90         void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
91 };