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