Cleaning, set up binding version in config.cmake
[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 active_diagnostic_request_t::active_diagnostic_request_t(const std::string& bus, uint32_t id,
30                 const std::string& name,
31                 bool wait_for_multiple_responses,
32                 const DiagnosticResponseDecoder decoder,
33                 const DiagnosticResponseCallback callback,
34                 float frequencyHz,
35                 bool permanent)
36         : bus_{bus},
37           id_{id},
38           handle_{nullptr},
39           name_{name},
40           decoder_{decoder},
41           callback_{callback},
42           recurring_{frequencyHz ? true : false},
43           permanent_{permanent},
44           wait_for_multiple_responses_{wait_for_multiple_responses},
45           frequency_clock_{frequency_clock_t(frequencyHz)},
46           timeout_clock_{frequency_clock_t(10)},
47           socket_{}
48 {}
49
50 active_diagnostic_request_t::~active_diagnostic_request_t()
51 {
52         socket_.close();
53         delete handle_;
54         handle_ = nullptr;
55 }
56
57 uint32_t active_diagnostic_request_t::get_id() const
58 {
59         return id_;
60 }
61
62 uint16_t active_diagnostic_request_t::get_pid() const
63 {
64         if (handle_->request.has_pid)
65                 return handle_->request.pid;
66         return AFB_ERROR_PID;
67 }
68
69 DiagnosticRequestHandle* active_diagnostic_request_t::get_handle()
70 {
71         return handle_;
72 }
73
74 const std::string active_diagnostic_request_t::get_name() const
75 {
76         return name_;
77 }
78
79 std::string& active_diagnostic_request_t::get_prefix()
80 {
81         return active_diagnostic_request_t::prefix_;
82 }
83
84 DiagnosticResponseDecoder& active_diagnostic_request_t::get_decoder()
85 {
86         return decoder_;
87 }
88
89 DiagnosticResponseCallback& active_diagnostic_request_t::get_callback()
90 {
91         return callback_;
92 }
93
94 bool active_diagnostic_request_t::get_recurring() const
95 {
96         return recurring_;
97 }
98
99 bool active_diagnostic_request_t::get_permanent() const
100 {
101         return permanent_;
102 }
103
104 frequency_clock_t& active_diagnostic_request_t::get_frequency_clock()
105 {
106         return frequency_clock_;
107 }
108
109 frequency_clock_t& active_diagnostic_request_t::get_timeout_clock()
110 {
111         return timeout_clock_;
112 }
113
114 utils::socketcan_bcm_t& active_diagnostic_request_t::get_socket()
115 {
116         return socket_;
117 }
118
119 void active_diagnostic_request_t::set_handle(DiagnosticShims& shims, DiagnosticRequest* request)
120 {
121         handle_ = new DiagnosticRequestHandle(generate_diagnostic_request(&shims, request, nullptr));
122 }
123
124 ///
125 /// @brief Check if requested signal name is a diagnostic message. If the name
126 ///  begin with the diagnostic message prefix then true else false.
127 ///
128 /// @param[in] name - A signal name.
129 ///
130 /// @return true if name began with the diagnostic message prefix else false.
131 ///
132 bool active_diagnostic_request_t::is_diagnostic_signal(const std::string& name)
133 {
134         const std::string p = active_diagnostic_request_t::prefix_ + "*";
135         if(::fnmatch(p.c_str(), name.c_str(), FNM_CASEFOLD) == 0)
136                 return true;
137         return false;
138 }
139
140 /// @brief Returns true if a sufficient response has been received for a
141 /// diagnostic request.
142 ///
143 /// This is true when at least one response has been received and the request is
144 /// configured to not wait for multiple responses. Functional broadcast requests
145 /// may often wish to wait the full 100ms for modules to respond.
146 bool active_diagnostic_request_t::response_received() const
147 {
148         return !wait_for_multiple_responses_ &&
149                                 handle_->completed && handle_->success;
150 }