Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / binding / application.cpp
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 #include <ctime>
18
19 #include "application.hpp"
20
21 #include "../utils/signals.hpp"
22 #include "../utils/openxc-utils.hpp"
23 #include "low-can-subscription.hpp"
24
25 #define MICROSECONDS_IN_SECOND  1000000
26 #define ENGINE_VALUE_TIMEOUT    5
27
28 application_t::application_t()
29         : can_bus_manager_{}
30         , message_set_{}
31 {}
32
33 /// @brief Return singleton instance of configuration object.
34 application_t& application_t::instance()
35 {
36         static application_t config;
37         return config;
38 }
39
40 can_bus_t& application_t::get_can_bus_manager()
41 {
42         return can_bus_manager_;
43 }
44
45 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& application_t::get_can_devices()
46 {
47         return can_devices_;
48 }
49
50 diagnostic_manager_t& application_t::get_diagnostic_manager()
51 {
52         return diagnostic_manager_;
53 }
54
55 uint8_t application_t::get_active_message_set() const
56 {
57         return active_message_set_;
58 }
59
60 int application_t::add_message_set(std::shared_ptr<message_set_t> new_message_set)
61 {
62         set_parents(new_message_set);
63
64         for(auto old_msg_set : message_set_)
65         {
66                 if(old_msg_set->get_index() == new_message_set->get_index())
67                 {
68                         for(auto new_msg_def : new_message_set->get_messages_definition())
69                         {
70                                 if(old_msg_set->add_message_definition(new_msg_def) < 0)
71                                         return -1;
72                         }
73
74                         if(diagnostic_manager_.is_initialized())
75                         {
76                                 for(auto new_diag_msg : new_message_set->get_diagnostic_messages())
77                                 {
78                                         if(old_msg_set->add_diagnostic_message(new_diag_msg) < 0)
79                                                 return -1;
80                                 }
81                         }
82                         return 0;
83                 }
84         }
85
86         message_set_.push_back(new_message_set);
87         return 0;
88 }
89
90 std::vector<std::shared_ptr<message_set_t> > application_t::get_message_set()
91 {
92         return message_set_;
93 }
94
95 vect_ptr_signal_t application_t::get_all_signals()
96 {
97         return message_set_[active_message_set_]->get_all_signals();
98 }
99
100 vect_ptr_diag_msg_t application_t::get_diagnostic_messages()
101 {
102         return message_set_[active_message_set_]->get_diagnostic_messages();
103 }
104
105 vect_ptr_msg_def_t application_t::get_messages_definition()
106 {
107         return message_set_[active_message_set_]->get_messages_definition();
108 }
109
110
111 std::shared_ptr<message_definition_t> application_t::get_message_definition(uint32_t id)
112 {
113         std::shared_ptr<message_definition_t> ret = nullptr;
114         vect_ptr_msg_def_t messages_definition = get_messages_definition();
115         for(std::shared_ptr<message_definition_t> &msg_def : messages_definition)
116         {
117                 if(msg_def->get_id() == id)
118                 {
119                         ret = msg_def;
120                         break;
121                 }
122         }
123         return ret;
124 }
125
126
127 uint32_t application_t::get_signal_id(diagnostic_message_t& sig) const
128 {
129         return sig.get_pid();
130 }
131
132 uint32_t application_t::get_signal_id(signal_t& sig) const
133 {
134         return sig.get_message()->get_id();
135 }
136
137 void application_t::set_active_message_set(uint8_t id)
138 {
139         active_message_set_ = id;
140 }
141
142 bool application_t::is_engine_on()
143 {
144         struct utils::signals_found sf;
145         openxc_DynamicField search_key = build_DynamicField("engine.speed");
146         sf = utils::signals_manager_t::instance().find_signals(search_key);
147         bool engine_on = false;
148         uint64_t last_timestamp_in_s;
149
150         if(sf.signals.size() == 1)
151         {
152                 last_timestamp_in_s = sf.signals.front()->get_last_value_with_timestamp().second
153                                                 / MICROSECONDS_IN_SECOND;
154
155                 if(sf.signals.front()->get_last_value_with_timestamp().first > 0 &&
156                    std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
157                         engine_on = true;
158                 else
159                         AFB_NOTICE("is_engine_on: engine.speed CAN signal found, but engine seems off");
160         }
161         else
162         {
163                 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed CAN signal");
164         }
165
166         if(sf.diagnostic_messages.size() == 1)
167         {
168                 last_timestamp_in_s = sf.diagnostic_messages.front()->get_last_value_with_timestamp().second
169                                                 / MICROSECONDS_IN_SECOND;
170
171                 if(sf.diagnostic_messages.front()->get_last_value_with_timestamp().first > 0 &&
172                    std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
173                         engine_on = true;
174                 else
175                         AFB_NOTICE("is_engine_on: engine.speed diagnostic message found, but engine seems off");
176         }
177         else
178         {
179                 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed diagnostic message");
180         }
181
182         return engine_on;
183 }
184
185 void application_t::set_parents(std::shared_ptr<message_set_t> new_message_set)
186 {
187         vect_ptr_msg_def_t messages_definition = new_message_set->get_messages_definition();
188         for(std::shared_ptr<message_definition_t> cmd : messages_definition)
189         {
190                 cmd->set_parent(new_message_set);
191                 std::vector<std::shared_ptr<signal_t>> signals = cmd->get_signals();
192                 for(std::shared_ptr<signal_t> sig: signals)
193                         sig->set_parent(cmd);
194         }
195
196         std::vector<std::shared_ptr<diagnostic_message_t>> diagnostic_messages = new_message_set->get_diagnostic_messages();
197         for(std::shared_ptr<diagnostic_message_t> dm : diagnostic_messages)
198                 dm->set_parent(new_message_set);
199 }
200
201 #ifdef USE_FEATURE_J1939
202 std::shared_ptr<utils::socketcan_t> application_t::get_socket_address_claiming()
203 {
204         return subscription_address_claiming_->get_socket();
205 }
206
207 std::shared_ptr<low_can_subscription_t> application_t::get_subscription_address_claiming()
208 {
209         return subscription_address_claiming_;
210 }
211
212
213 void application_t::set_subscription_address_claiming(std::shared_ptr<low_can_subscription_t> new_subscription)
214 {
215         subscription_address_claiming_ = new_subscription;
216 }
217
218 #endif