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