examples: updates
[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
19 #include <fnmatch.h>
20 #include <map>
21
22 #include "active-diagnostic-request.hpp"
23
24 #include "../binding/application.hpp"
25
26 #define AFB_ERROR_PID 0xFF
27
28 std::string active_diagnostic_request_t::prefix_ = "diagnostic_messages";
29
30 active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, uint32_t id,
31                 const std::string& name,
32                 bool wait_for_multiple_responses,
33                 const DiagnosticResponseDecoder decoder,
34                 const DiagnosticResponseCallback callback,
35                 float frequencyHz,
36                 bool permanent)
37         : bus_{bus},
38           id_{id},
39           handle_{nullptr},
40           name_{name},
41           decoder_{decoder},
42           callback_{callback},
43           recurring_{frequencyHz ? true : false},
44           permanent_{permanent},
45           wait_for_multiple_responses_{wait_for_multiple_responses},
46           frequency_clock_{frequency_clock_t(frequencyHz)},
47           timeout_clock_{frequency_clock_t(10)},
48           socket_{}
49 {}
50
51 active_diagnostic_request_t::~active_diagnostic_request_t()
52 {
53         socket_.close();
54         delete handle_;
55         handle_ = nullptr;
56 }
57
58 uint32_t active_diagnostic_request_t::get_id() const
59 {
60         return id_;
61 }
62
63 uint16_t active_diagnostic_request_t::get_pid() const
64 {
65         if (handle_->request.has_pid)
66                 return handle_->request.pid;
67         return AFB_ERROR_PID;
68 }
69
70 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
71 {
72         return handle_;
73 }
74
75 const std::string active_diagnostic_request_t::get_name() const
76 {
77         return name_;
78 }
79
80 std::string& active_diagnostic_request_t::get_prefix()
81 {
82         return active_diagnostic_request_t::prefix_;
83 }
84
85 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
86 {
87         return decoder_;
88 }
89
90 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
91 {
92         return callback_;
93 }
94
95 bool active_diagnostic_request_t::get_recurring() const
96 {
97         return recurring_;
98 }
99
100 bool active_diagnostic_request_t::get_permanent() const
101 {
102         return permanent_;
103 }
104
105 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
106 {
107         return frequency_clock_;
108 }
109
110 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
111 {
112         return timeout_clock_;
113 }
114
115 utils::socketcan_bcm_t& active_diagnostic_request_t::get_socket()
116 {
117         return socket_;
118 }
119
120 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
121 {
122         handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
123 }
124
125 /// @brief Returns true if a sufficient response has been received for a
126 /// diagnostic request.
127 ///
128 /// This is true when at least one response has been received and the request is
129 /// configured to not wait for multiple responses. Functional broadcast requests
130 /// may often wish to wait the full 100ms for modules to respond.
131 bool active_diagnostic_request_t::response_received() const
132 {
133         return !wait_for_multiple_responses_ &&
134                                 handle_->completed && handle_->success;
135 }