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