Add some typedef to reduce the size of the lines
[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 /// @brief Return singleton instance of configuration object.
29 application_t& application_t::instance()
30 {
31         static application_t config;
32         return config;
33 }
34
35 can_bus_t& application_t::get_can_bus_manager()
36 {
37         return can_bus_manager_;
38 }
39
40 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& application_t::get_can_devices()
41 {
42         return can_devices_;
43 }
44
45 diagnostic_manager_t& application_t::get_diagnostic_manager()
46 {
47         return diagnostic_manager_;
48 }
49
50 uint8_t application_t::get_active_message_set() const
51 {
52         return active_message_set_;
53 }
54
55 std::vector<std::shared_ptr<message_set_t> > application_t::get_message_set()
56 {
57         return message_set_;
58 }
59
60 vect_ptr_signal_t application_t::get_all_signals()
61 {
62         return message_set_[active_message_set_]->get_all_signals();
63 }
64
65 vect_ptr_diag_msg_t application_t::get_diagnostic_messages()
66 {
67         return message_set_[active_message_set_]->get_diagnostic_messages();
68 }
69
70 vect_ptr_msg_def_t application_t::get_messages_definition()
71 {
72         return message_set_[active_message_set_]->get_messages_definition();
73 }
74
75
76 std::shared_ptr<message_definition_t> application_t::get_message_definition(uint32_t id)
77 {
78         std::shared_ptr<message_definition_t> ret = nullptr;
79         vect_ptr_msg_def_t messages_definition = get_messages_definition();
80         for(std::shared_ptr<message_definition_t> &msg_def : messages_definition)
81         {
82                 if(msg_def->get_id() == id)
83                 {
84                         ret = msg_def;
85                         break;
86                 }
87         }
88         return ret;
89 }
90
91
92 uint32_t application_t::get_signal_id(diagnostic_message_t& sig) const
93 {
94         return sig.get_pid();
95 }
96
97 uint32_t application_t::get_signal_id(signal_t& sig) const
98 {
99         return sig.get_message()->get_id();
100 }
101
102 void application_t::set_active_message_set(uint8_t id)
103 {
104         active_message_set_ = id;
105 }
106
107 bool application_t::isEngineOn()
108 {
109         struct utils::signals_found sf;
110         openxc_DynamicField search_key = build_DynamicField("engine.speed");
111         sf = utils::signals_manager_t::instance().find_signals(search_key);
112         bool engine_on = false;
113         uint64_t last_timestamp_in_s;
114
115         if(sf.signals.size() == 1)
116         {
117                 last_timestamp_in_s = sf.signals.front()->get_last_value_with_timestamp().second
118                                                 / MICROSECONDS_IN_SECOND;
119
120                 if(sf.signals.front()->get_last_value_with_timestamp().first > 0 &&
121                    std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
122                 {
123                         engine_on = true;
124                 }
125                 else
126                 {
127                         AFB_NOTICE("is_engine_on: engine.speed CAN signal found, but engine seems off");
128                 }
129         }
130         else
131         {
132                 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed CAN signal");
133         }
134
135         if(sf.diagnostic_messages.size() == 1)
136         {
137                 last_timestamp_in_s = sf.diagnostic_messages.front()->get_last_value_with_timestamp().second
138                                                 / MICROSECONDS_IN_SECOND;
139
140                 if(sf.diagnostic_messages.front()->get_last_value_with_timestamp().first > 0 &&
141                    std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
142                 {
143                         engine_on = true;
144                 }
145                 else
146                 {
147                         AFB_NOTICE("is_engine_on: engine.speed diagnostic message found, but engine seems off");
148                 }
149         }
150         else
151         {
152                 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed diagnostic message");
153         }
154
155         return engine_on;
156 }
157
158 #ifdef USE_FEATURE_J1939
159 std::shared_ptr<utils::socketcan_t> application_t::get_socket_address_claiming()
160 {
161         return subscription_address_claiming_->get_socket();
162 }
163
164 std::shared_ptr<low_can_subscription_t> application_t::get_subscription_address_claiming()
165 {
166         return subscription_address_claiming_;
167 }
168
169
170 void application_t::set_subscription_address_claiming(std::shared_ptr<low_can_subscription_t> new_subscription)
171 {
172         subscription_address_claiming_ = new_subscription;
173 }
174
175 #endif