2112ee9e958f97d31d9ad3513c719b2b2b56cf15
[apps/low-level-can-service.git] / src / configuration.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 <vector>
21 #include <fcntl.h>
22
23 #include "can/can-bus.hpp"
24 #include "can/can-signals.hpp"
25 #include "can/can-message.hpp"
26 #include "diagnostic/diagnostic-manager.hpp"
27
28 #include "low-can-binding.hpp"
29
30 /**
31  * @brief Class representing a configuration attached to the binding.
32  *
33  * @desc It regroups all needed objects instance from other class
34  *  that will be used along the binding life. It gets a global vision 
35  *  on which signals are implemented for that binding. 
36  *  Here, it is only the definition of the class with predefined accessors
37  *  methods used in the binding.
38  *
39  *  It will be the reference point to needed objects.
40  */
41 class configuration_t
42 {
43         private:
44                 can_bus_t can_bus_manager_ = can_bus_t(
45                         afb_daemon_rootdir_open_locale(
46                                 binder_interface->daemon, "etc/can_buses.json", O_RDONLY, NULL)); ///< instanciate the CAN bus
47                                 ///< This will read the configuration file and initialize all CAN devices specified in it.
48                 diagnostic_manager_t diagnostic_manager_; ///< Diagnostic manager use to manage diagnostic message communication.
49                 uint8_t active_message_set_ = 0; ///< Which is the active message set ? Default to 0.
50
51                 std::vector<can_message_set_t> can_message_set_;
52                 std::vector<std::vector<can_message_definition_t>> can_message_definition_;
53                 std::vector<std::vector<can_signal_t>> can_signals_;
54                 std::vector<std::vector<diagnostic_message_t>> diagnostic_messages_;
55
56                 configuration_t(); ///< Private constructor with implementation generated by the AGL generator.
57
58         public:
59                 static configuration_t& instance();
60
61                 can_bus_t& get_can_bus_manager();
62
63                 const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& get_can_bus_devices();
64
65                 const std::string get_diagnostic_bus() const;
66
67                 diagnostic_manager_t& get_diagnostic_manager() ;
68
69                 uint8_t get_active_message_set() const;
70
71                 const std::vector<can_message_set_t>& get_can_message_set();
72
73                 std::vector<can_signal_t>& get_can_signals();
74
75                 std::vector<diagnostic_message_t>& get_diagnostic_messages();
76
77                 const std::vector<std::string>& get_signals_prefix() const;
78
79                 const std::vector<can_message_definition_t>& get_can_message_definition();
80                 const can_message_definition_t& get_can_message_definition(std::uint8_t message_set_id, std::uint8_t message_id);
81
82                 uint32_t get_signal_id(diagnostic_message_t& sig) const;
83
84                 uint32_t get_signal_id(can_signal_t& sig) const;
85
86                 void set_active_message_set(uint8_t id);
87
88                 void find_diagnostic_messages(const openxc_DynamicField &key, std::vector<diagnostic_message_t*>& found_signals);
89
90                 void find_can_signals(const openxc_DynamicField &key, std::vector<can_signal_t*>& found_signals);
91
92 /*
93                 /// TODO: implement this function as method into can_bus class
94                 /// @brief Pre initialize actions made before CAN bus initialization
95                 /// @param[in] bus A CanBus struct defining the bus's metadata
96                 /// @param[in] writable Configure the controller in a writable mode. If false, it will be configured as "listen only" and will not allow writes or even CAN ACKs.
97                 /// @param[in] buses An array of all CAN buses.
98                 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
99                 /// TODO: implement this function as method into can_bus class
100                 /// @brief Post-initialize actions made after CAN bus initialization
101                 /// @param[in] bus A CanBus struct defining the bus's metadata
102                 /// @param[in] writable Configure the controller in a writable mode. If false, it will be configured as "listen only" and will not allow writes or even CAN ACKs.
103                 /// @param[in] buses An array of all CAN buses.
104                 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
105                 /// TODO: implement this function as method into can_bus class
106                 /// @brief Check if the device is connected to an active CAN bus, i.e. it's received a message in the recent past.
107                 /// @return true if a message was received on the CAN bus within CAN_ACTIVE_TIMEOUT_S seconds.
108                 void logBusStatistics(can_bus_dev_t* buses, const int busCount);
109                 /// TODO: implement this function as method into can_bus class
110                 /// @brief Log transfer statistics about all active CAN buses to the debug log.
111                 /// @param[in] buses An array of active CAN buses.
112                 bool isBusActive(can_bus_dev_t* bus);
113                 */
114 };
115