Restore /etc/dev-mapping.conf support
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-bus.hpp
1 /*
2  * Copyright (C) 2015, 2016, 2017, 2018, 2019 "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 <utility>
22 #include <mutex>
23 #include <queue>
24 #include <thread>
25 #include <linux/can.h>
26 #include <condition_variable>
27 #include "openxc.pb.h"
28 #include "message/can-message.hpp"
29 #include "../binding/low-can-subscription.hpp"
30
31 #define CAN_ACTIVE_TIMEOUT_S 30
32
33 class diagnostic_manager_t;
34
35 /// @brief Object used to handle decoding and manage event queue to be pushed.
36 ///
37 /// This object is also used to initialize can_bus_dev_t object after reading
38 /// json conf file describing the CAN devices to use. Thus, those object will read
39 /// on the device the CAN frame and push them into the can_bus_t can_message_q_ queue.
40 ///
41 /// That queue will later be decoded and pushed to subscribers.
42 class can_bus_t
43 {
44 private:
45         bool apply_filter(const openxc_VehicleMessage& vehicle_message, std::shared_ptr<low_can_subscription_t> can_subscription);
46         void process_signals(std::shared_ptr<message_t> message, map_subscription& s);
47         void process_diagnostic_signals(diagnostic_manager_t& manager, std::shared_ptr<message_t> can_message, map_subscription& s);
48
49         void can_decode_message();
50         std::thread th_decoding_; ///< thread that will handle decoding a can frame
51         bool is_decoding_ = false; ///< boolean member controling thread while loop
52
53         void can_event_push();
54         std::thread th_pushing_; ///< thread that will handle pushing decoded can frame to subscribers
55         bool is_pushing_ = false; ///< boolean member controling thread while loop
56
57         std::condition_variable new_can_message_cv_; ///< condition_variable use to wait until there is a new CAN message to read
58         std::mutex can_message_mutex_; ///< mutex protecting the can_message_q_ queue.
59         std::queue <std::shared_ptr<message_t>> can_message_q_; ///< queue that will store can_message_t to be decoded
60
61         std::condition_variable new_decoded_can_message_; ///< condition_variable use to wait until there is a new vehicle message
62                                                           /// to read from the queue vehicle_message_q_
63         std::mutex decoded_can_message_mutex_;  ///< mutex protecting the vehicle_message_q_ queue.
64         std::queue <std::pair<int, openxc_VehicleMessage> > vehicle_message_q_; ///< queue that will store openxc_VehicleMessage to be pushed
65
66         std::vector<std::pair<std::string, std::string> > can_devices_mapping_; ///< can_devices_mapping_ - holds a mapping between logical CAN devices names and linux CAN devices names.
67 public:
68         explicit can_bus_t();
69         can_bus_t(can_bus_t&&);
70         ~can_bus_t();
71
72         void set_can_devices(json_object *mapping);
73         void set_can_devices(const std::vector<std::pair<std::string, std::string> >& mapping);
74
75         int get_can_device_index(const std::string& bus_name) const;
76         const std::string get_can_device_name(const std::string& id_name) const;
77
78         void start_threads();
79         void stop_threads();
80
81         std::shared_ptr<message_t> next_can_message();
82         void push_new_can_message(std::shared_ptr<message_t> can_msg);
83         std::mutex& get_can_message_mutex();
84         std::condition_variable& get_new_can_message_cv();
85
86         std::pair<int, openxc_VehicleMessage> next_vehicle_message();
87         void push_new_vehicle_message(int subscription_id, const openxc_VehicleMessage& v_msg);
88 };