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 "../binding/application.hpp"
25 #define ERROR_PID 0xFF
27 std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
29 bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
31 return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_);
34 active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
40 handle_ = adr.handle_;
42 decoder_ = adr.decoder_;
43 callback_ = adr.callback_;
44 recurring_ = adr.recurring_;
45 wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
46 frequency_clock_ = adr.frequency_clock_;
47 timeout_clock_ = adr.timeout_clock_;
48 socket_ = adr.socket_;
54 active_diagnostic_request_t::active_diagnostic_request_t()
62 wait_for_multiple_responses_{false},
63 frequency_clock_{frequency_clock_t()},
64 timeout_clock_{frequency_clock_t()},
68 active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, uint32_t id,
69 const std::string& name,
70 bool wait_for_multiple_responses,
71 const DiagnosticResponseDecoder decoder,
72 const DiagnosticResponseCallback callback,
80 recurring_{frequencyHz ? true : false},
81 wait_for_multiple_responses_{wait_for_multiple_responses},
82 frequency_clock_{frequency_clock_t(frequencyHz)},
83 timeout_clock_{frequency_clock_t(10)},
87 active_diagnostic_request_t::~active_diagnostic_request_t()
94 uint32_t active_diagnostic_request_t::get_id() const
99 uint16_t active_diagnostic_request_t::get_pid() const
101 if (handle_->request.has_pid)
102 return handle_->request.pid;
106 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
111 const std::string active_diagnostic_request_t::get_name() const
116 std::string& active_diagnostic_request_t::get_prefix()
118 return active_diagnostic_request_t::prefix_;
121 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
126 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
131 bool active_diagnostic_request_t::get_recurring() const
136 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
138 return frequency_clock_;
141 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
143 return timeout_clock_;
146 utils::socketcan_bcm_t& active_diagnostic_request_t::get_socket()
151 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
153 handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
157 /// @brief Check if requested signal name is a diagnostic message. If the name
158 /// begin with the diagnostic message prefix then true else false.
160 /// @param[in] name - A signal name.
162 /// @return true if name began with the diagnostic message prefix else false.
164 bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
166 const std::string p = active_diagnostic_request_t::prefix_ + "*";
167 if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
172 /// @brief Returns true if a sufficient response has been received for a
173 /// diagnostic request.
175 /// This is true when at least one response has been received and the request is
176 /// configured to not wait for multiple responses. Functional broadcast requests
177 /// may often wish to wait the full 100ms for modules to respond.
178 bool active_diagnostic_request_t::response_received() const
180 return !wait_for_multiple_responses_ &&
181 handle_->completed && handle_->success;