2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "application.hpp"
21 #include "../utils/signals.hpp"
22 #include "../utils/openxc-utils.hpp"
23 #include "low-can-subscription.hpp"
25 #define MICROSECONDS_IN_SECOND 1000000
26 #define ENGINE_VALUE_TIMEOUT 5
28 /// @brief Return singleton instance of configuration object.
29 application_t& application_t::instance()
31 static application_t config;
35 can_bus_t& application_t::get_can_bus_manager()
37 return can_bus_manager_;
40 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& application_t::get_can_devices()
45 diagnostic_manager_t& application_t::get_diagnostic_manager()
47 return diagnostic_manager_;
50 uint8_t application_t::get_active_message_set() const
52 return active_message_set_;
55 std::vector<std::shared_ptr<message_set_t> > application_t::get_message_set()
60 std::vector<std::shared_ptr<signal_t> > application_t::get_all_signals()
62 return message_set_[active_message_set_]->get_all_signals();
65 std::vector<std::shared_ptr<diagnostic_message_t> > application_t::get_diagnostic_messages()
67 return message_set_[active_message_set_]->get_diagnostic_messages();
70 std::vector<std::shared_ptr<message_definition_t>> application_t::get_messages_definition()
72 return message_set_[active_message_set_]->get_messages_definition();
75 uint32_t application_t::get_signal_id(diagnostic_message_t& sig) const
80 uint32_t application_t::get_signal_id(signal_t& sig) const
82 return sig.get_message()->get_id();
85 void application_t::set_active_message_set(uint8_t id)
87 active_message_set_ = id;
90 bool application_t::isEngineOn()
92 struct utils::signals_found sf;
93 openxc_DynamicField search_key = build_DynamicField("engine.speed");
94 sf = utils::signals_manager_t::instance().find_signals(search_key);
95 bool engine_on = false;
96 uint64_t last_timestamp_in_s;
98 if(sf.signals.size() == 1)
100 last_timestamp_in_s = sf.signals.front()->get_last_value_with_timestamp().second
101 / MICROSECONDS_IN_SECOND;
103 if(sf.signals.front()->get_last_value_with_timestamp().first > 0 &&
104 std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
110 AFB_NOTICE("is_engine_on: engine.speed CAN signal found, but engine seems off");
115 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed CAN signal");
118 if(sf.diagnostic_messages.size() == 1)
120 last_timestamp_in_s = sf.diagnostic_messages.front()->get_last_value_with_timestamp().second
121 / MICROSECONDS_IN_SECOND;
123 if(sf.diagnostic_messages.front()->get_last_value_with_timestamp().first > 0 &&
124 std::difftime(std::time(nullptr), last_timestamp_in_s) < ENGINE_VALUE_TIMEOUT)
130 AFB_NOTICE("is_engine_on: engine.speed diagnostic message found, but engine seems off");
135 AFB_NOTICE("is_engine_on: Can't identify a useable engine.speed diagnostic message");