Change the way to check signal type making prefix_
[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 bool active_diagnostic_request_t::operator==(const active_diagnostic_request_t& b)
21 {
22         return (bus_ == b.bus_ && id_ == b.id_ && handle_ == b.handle_) ? true : false;
23 }
24
25 active_diagnostic_request_t& active_diagnostic_request_t::operator=(const active_diagnostic_request_t& adr)
26 {
27         if (this != &adr)
28         {
29                 bus_ = adr.bus_;
30                 id_ = adr.id_;
31                 handle_ = adr.handle_;
32                 name_ = adr.name_;
33                 decoder_ = adr.decoder_;
34                 callback_ = adr.callback_;
35                 recurring_ = adr.recurring_; 
36                 wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
37                 in_flight_ = adr.in_flight_;
38                 frequency_clock_ = adr.frequency_clock_;
39                 timeout_clock_ = adr.timeout_clock_;
40         }
41         
42         return *this;
43 }
44
45 active_diagnostic_request_t::active_diagnostic_request_t()
46         : bus_{nullptr}, id_{0}, handle_{nullptr}, name_{""},
47           decoder_{nullptr}, callback_{nullptr}, recurring_{false}, wait_for_multiple_responses_{false},
48           in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
49 {}
50
51 active_diagnostic_request_t::active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
52                 const std::string& name, bool wait_for_multiple_responses,
53                 const DiagnosticResponseDecoder decoder,
54                 const DiagnosticResponseCallback callback, float frequencyHz)
55         : bus_{bus}, id_{request->arbitration_id}, handle_{nullptr}, name_{name},
56           decoder_{decoder}, callback_{callback}, recurring_{frequencyHz ? true : false}, wait_for_multiple_responses_{wait_for_multiple_responses},
57           in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
58 {}
59
60 uint32_t active_diagnostic_request_t::get_id() const
61 {
62         return id_;
63 }
64
65 std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
66 {
67         return bus_;
68 }
69
70 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
71 {
72         return handle_;
73 }
74
75 bool active_diagnostic_request_t::get_recurring() const
76 {
77         return recurring_;
78 }
79
80 bool active_diagnostic_request_t::get_in_flight() const
81 {
82         return in_flight_;
83 }
84
85 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
86 {
87         return frequency_clock_;
88 }
89
90 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
91 {
92         return timeout_clock_;
93 }
94
95 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
96 {
97         handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
98 }
99
100 void active_diagnostic_request_t::set_in_flight(bool val)
101 {
102         in_flight_ = val;
103 }
104
105 bool active_diagnostic_request_t::should_send()
106 {
107         return !get_in_flight() && (
108                         (!get_recurring() && !request_completed()) ||
109                         (get_recurring() && get_frequency_clock().elapsed(true)));
110 }
111
112 bool active_diagnostic_request_t::timed_out()
113 {
114         // don't use staggered start with the timeout clock
115         return timeout_clock_.elapsed(false);
116 }
117
118 /// @brief Returns true if a sufficient response has been received for a
119 /// diagnostic request.
120 ///
121 /// This is true when at least one response has been received and the request is
122 /// configured to not wait for multiple responses. Functional broadcast requests
123 /// may often wish to wait the full 100ms for modules to respond.
124 bool active_diagnostic_request_t::response_received() const
125 {
126         return !wait_for_multiple_responses_ &&
127                                 handle_->completed;
128 }
129
130 /// @brief Returns true if the request has timed out waiting for a response,
131 /// or a sufficient number of responses has been received.
132 bool active_diagnostic_request_t::request_completed()
133 {
134         return response_received() || 
135                 (timed_out() && diagnostic_request_sent(handle_));
136 }