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.
21 #include "active-diagnostic-request.hpp"
23 #include "../configuration.hpp"
25 #define ERROR_PID 0xFF
28 std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
30 bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
32 return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_) ? true : false;
35 active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
41 handle_ = adr.handle_;
43 decoder_ = adr.decoder_;
44 callback_ = adr.callback_;
45 recurring_ = adr.recurring_;
46 wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
47 in_flight_ = adr.in_flight_;
48 frequency_clock_ = adr.frequency_clock_;
49 timeout_clock_ = adr.timeout_clock_;
55 active_diagnostic_request_t::active_diagnostic_request_t()
56 : bus_{nullptr}, id_{0}, handle_{nullptr}, name_{""},
57 decoder_{nullptr}, callback_{nullptr}, recurring_{false}, wait_for_multiple_responses_{false},
58 in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
61 active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, DiagnosticRequest* request,
62 const std::string& name, bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
63 const DiagnosticResponseCallback callback, float frequencyHz)
64 : bus_{bus}, id_{request->arbitration_id}, handle_{nullptr}, name_{name},
65 decoder_{decoder}, callback_{callback}, recurring_{frequencyHz ? true : false}, wait_for_multiple_responses_{wait_for_multiple_responses},
66 in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
69 uint32_t active_diagnostic_request_t::get_id() const
74 const std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev() const
76 return can_bus_t::get_can_device(bus_);
79 uint16_t active_diagnostic_request_t::get_pid() const
81 if (handle_->request.has_pid)
82 return handle_->request.pid;
86 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
91 const std::string active_diagnostic_request_t::get_name() const
96 std::string& active_diagnostic_request_t::get_prefix()
98 return active_diagnostic_request_t::prefix_;
101 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
106 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
111 bool active_diagnostic_request_t::get_recurring() const
116 bool active_diagnostic_request_t::get_in_flight() const
121 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
123 return frequency_clock_;
126 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
128 return timeout_clock_;
131 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
133 handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
136 void active_diagnostic_request_t::set_in_flight(bool val)
142 /// @brief Check if requested signal name is a diagnostic message. If the name
143 /// begin with the diagnostic message prefix then true else false.
145 /// @param[in] name - A signal name.
147 /// @return true if name began with the diagnostic message prefix else false.
149 bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
151 const std::string p = active_diagnostic_request_t::prefix_ + "*";
152 if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
157 /// @brief Check is the request should be sent or not
159 /// @return true if the request isn't already running and not
160 /// recurring nor completed, or it is recurring its clock elapsed so it's
161 /// time to send another one.
162 bool active_diagnostic_request_t::should_send()
164 return !get_in_flight() && (
165 (!get_recurring() && !request_completed()) ||
166 (get_recurring() && get_frequency_clock().elapsed(true)));
169 /// @brief check if the timeout clock has elapsed
171 /// @return true if elapsed, so it is a timeout, else false.
172 bool active_diagnostic_request_t::timed_out()
174 // don't use staggered start with the timeout clock
175 return timeout_clock_.elapsed(false);
178 /// @brief Returns true if a sufficient response has been received for a
179 /// diagnostic request.
181 /// This is true when at least one response has been received and the request is
182 /// configured to not wait for multiple responses. Functional broadcast requests
183 /// may often wish to wait the full 100ms for modules to respond.
184 bool active_diagnostic_request_t::response_received() const
186 return !wait_for_multiple_responses_ &&
190 /// @brief Returns true if the request has timed out waiting for a response,
191 /// or a sufficient number of responses has been received.
192 bool active_diagnostic_request_t::request_completed()
194 return response_received() ||
195 (timed_out() && diagnostic_request_sent(handle_));