Use TEST app-templates labels
[apps/agl-service-can-low-level.git] / 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/application.hpp"
24
25 #define AFB_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                 permanent_ = adr.permanent_;
46                 wait_for_multiple_responses_ = adr.wait_for_multiple_responses_;
47                 frequency_clock_ = adr.frequency_clock_;
48                 timeout_clock_ = adr.timeout_clock_;
49                 socket_ = adr.socket_;
50         }
51
52         return *this;
53 }
54
55 active_diagnostic_request_t::active_diagnostic_request_t()
56         : bus_{nullptr},
57           id_{0},
58           handle_{nullptr},
59           name_{""},
60           decoder_{nullptr},
61           callback_{nullptr},
62           recurring_{false},
63           permanent_{false},
64           wait_for_multiple_responses_{false},
65           frequency_clock_{frequency_clock_t()},
66           timeout_clock_{frequency_clock_t()},
67           socket_{}
68 {}
69
70 active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, uint32_t id,
71                 const std::string& name,
72                 bool wait_for_multiple_responses,
73                 const DiagnosticResponseDecoder decoder,
74                 const DiagnosticResponseCallback callback,
75                 float frequencyHz,
76                 bool permanent)
77         : bus_{bus},
78           id_{id},
79           handle_{nullptr},
80           name_{name},
81           decoder_{decoder},
82           callback_{callback},
83           recurring_{frequencyHz ? true : false},
84           permanent_{permanent},
85           wait_for_multiple_responses_{wait_for_multiple_responses},
86           frequency_clock_{frequency_clock_t(frequencyHz)},
87           timeout_clock_{frequency_clock_t(10)},
88           socket_{}
89 {}
90
91 active_diagnostic_request_t::~active_diagnostic_request_t()
92 {
93         socket_.close();
94         delete handle_;
95         handle_ = nullptr;
96 }
97
98 uint32_t active_diagnostic_request_t::get_id() const
99 {
100         return id_;
101 }
102
103 uint16_t active_diagnostic_request_t::get_pid() const
104 {
105         if (handle_->request.has_pid)
106                 return handle_->request.pid;
107         return AFB_ERROR_PID;
108 }
109
110 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
111 {
112         return handle_;
113 }
114
115 const std::string active_diagnostic_request_t::get_name() const
116 {
117         return name_;
118 }
119
120 std::string& active_diagnostic_request_t::get_prefix()
121 {
122         return active_diagnostic_request_t::prefix_;
123 }
124
125 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
126 {
127         return decoder_;
128 }
129
130 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
131 {
132         return callback_;
133 }
134
135 bool active_diagnostic_request_t::get_recurring() const
136 {
137         return recurring_;
138 }
139
140 bool active_diagnostic_request_t::get_permanent() const
141 {
142         return permanent_;
143 }
144
145 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
146 {
147         return frequency_clock_;
148 }
149
150 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
151 {
152         return timeout_clock_;
153 }
154
155 utils::socketcan_bcm_t& active_diagnostic_request_t::get_socket()
156 {
157         return socket_;
158 }
159
160 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
161 {
162         handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
163 }
164
165 ///
166 /// @brief Check if requested signal name is a diagnostic message. If the name
167 ///  begin with the diagnostic message prefix then true else false.
168 ///
169 /// @param[in] name - A signal name.
170 ///
171 /// @return true if name began with the diagnostic message prefix else false.
172 ///
173 bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
174 {
175         const std::string p = active_diagnostic_request_t::prefix_ + "*";
176         if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
177                 return true;
178         return false;
179 }
180
181 /// @brief Returns true if a sufficient response has been received for a
182 /// diagnostic request.
183 ///
184 /// This is true when at least one response has been received and the request is
185 /// configured to not wait for multiple responses. Functional broadcast requests
186 /// may often wish to wait the full 100ms for modules to respond.
187 bool active_diagnostic_request_t::response_received() const
188 {
189         return !wait_for_multiple_responses_ &&
190                                 handle_->completed && handle_->success;
191 }