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