Move member from obd2_signals_t class to
[apps/agl-service-can-low-level.git] / src / diagnostic / active-diagnostic-request.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
18 #include "active-diagnostic-request.hpp"
19
20 std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
21
22 bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
23 {
24         return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_) ? true : false;
25 }
26
27 active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
28 {
29         if (this != &adr)
30         {
31                 bus_ = adr.bus_;
32                 id_ = adr.id_;
33                 handle_ = adr.handle_;
34                 name_ = adr.name_;
35                 decoder_ = adr.decoder_;
36                 callback_ = adr.callback_;
37                 recurring_ = adr.recurring_; 
38                 wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
39                 in_flight_ = adr.in_flight_;
40                 frequency_clock_ = adr.frequency_clock_;
41                 timeout_clock_ = adr.timeout_clock_;
42         }
43         
44         return *this;
45 }
46
47 active_diagnostic_request_t::active_diagnostic_request_t()
48         : bus_{nullptr}, id_{0}, handle_{nullptr}, name_{""},
49           decoder_{nullptr}, callback_{nullptr}, recurring_{false}, wait_for_multiple_responses_{false},
50           in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
51 {}
52
53 active_diagnostic_request_t::active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
54                 const std::string& name, bool wait_for_multiple_responses,
55                 const DiagnosticResponseDecoder decoder,
56                 const DiagnosticResponseCallback callback, float frequencyHz)
57         : bus_{bus}, id_{request->arbitration_id}, handle_{nullptr}, name_{name},
58           decoder_{decoder}, callback_{callback}, recurring_{frequencyHz ? true : false}, wait_for_multiple_responses_{wait_for_multiple_responses},
59           in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
60 {}
61
62 uint32_t active_diagnostic_request_t::get_id() const
63 {
64         return id_;
65 }
66
67 std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
68 {
69         return bus_;
70 }
71
72 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
73 {
74         return handle_;
75 }
76
77 std::string& active_diagnostic_request_t::get_name()
78 {
79         return name_;
80 }
81
82 std::string& active_diagnostic_request_t::get_prefix()
83 {
84         return active_diagnostic_request_t::prefix_;
85 }
86
87 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
88 {
89         return decoder_;
90 }
91
92 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
93 {
94         return callback_;
95 }
96
97 bool active_diagnostic_request_t::get_recurring() const
98 {
99         return recurring_;
100 }
101
102 bool active_diagnostic_request_t::get_in_flight() const
103 {
104         return in_flight_;
105 }
106
107 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
108 {
109         return frequency_clock_;
110 }
111
112 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
113 {
114         return timeout_clock_;
115 }
116
117 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
118 {
119         handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
120 }
121
122 void active_diagnostic_request_t::set_in_flight(bool val)
123 {
124         in_flight_ = val;
125 }
126
127 /**
128 * @brief Check if requested signal name is an obd2 pid
129
130 * @return true if name began with obd2 else false.
131 */
132 bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
133 {
134         if(name.find_first_of(prefix_.c_str(), 0, prefix_.size()))
135                 return true;
136         return false;
137 }
138
139 bool active_diagnostic_request_t::should_send()
140 {
141         return !get_in_flight() && (
142                         (!get_recurring() && !request_completed()) ||
143                         (get_recurring() && get_frequency_clock().elapsed(true)));
144 }
145
146 bool active_diagnostic_request_t::timed_out()
147 {
148         // don't use staggered start with the timeout clock
149         return timeout_clock_.elapsed(false);
150 }
151
152 /// @brief Returns true if a sufficient response has been received for a
153 /// diagnostic request.
154 ///
155 /// This is true when at least one response has been received and the request is
156 /// configured to not wait for multiple responses. Functional broadcast requests
157 /// may often wish to wait the full 100ms for modules to respond.
158 bool active_diagnostic_request_t::response_received() const
159 {
160         return !wait_for_multiple_responses_ &&
161                                 handle_->completed;
162 }
163
164 /// @brief Returns true if the request has timed out waiting for a response,
165 /// or a sufficient number of responses has been received.
166 bool active_diagnostic_request_t::request_completed()
167 {
168         return response_received() || 
169                 (timed_out() && diagnostic_request_sent(handle_));
170 }