2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "diagnostic-manager.hpp"
23 #include "../configuration.hpp"
25 #define MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ 10
26 #define MAX_SIMULTANEOUS_DIAG_REQUESTS 50
27 #define MAX_REQUEST_ENTRIES 50
29 diagnostic_manager_t::diagnostic_manager_t()
30 : request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
35 diagnostic_manager_t::diagnostic_manager_t(can_bus_dev_t& bus)
36 : bus_(&bus), request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
41 void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t& entry, std::vector<active_diagnostic_request_t>& requests_list)
43 auto i = std::find(requests_list.begin(), requests_list.end(), entry);
44 if ( i != requests_list.end())
45 requests_list.erase(i);
48 /// Move the entry to the free list and decrement the lock count for any
49 /// CAN filters it used.
50 void diagnostic_manager_t::cancel_request(active_diagnostic_request_t& entry)
52 free_request_entries_.push_back(entry);
53 /* TODO: implement acceptance filters.
54 if(entry.arbitration_id_ == OBD2_FUNCTIONAL_BROADCAST_ID) {
55 for(uint32_t filter = OBD2_FUNCTIONAL_RESPONSE_START;
56 filter < OBD2_FUNCTIONAL_RESPONSE_START +
57 OBD2_FUNCTIONAL_RESPONSE_COUNT;
59 removeAcceptanceFilter(entry.bus_, filter,
60 CanMessageFormat::STANDARD, getCanBuses(),
64 removeAcceptanceFilter(entry.bus_,
65 entry.arbitration_id_ +
66 DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET,
67 CanMessageFormat::STANDARD, getCanBuses(), getCanBusCount());
71 void diagnostic_manager_t::cleanup_request(active_diagnostic_request_t& entry, bool force)
73 if(force || (entry.get_in_flight() && entry.request_completed()))
75 entry.set_in_flight(false);
77 char request_string[128] = {0};
78 diagnostic_request_to_string(&entry.get_handle().request,
79 request_string, sizeof(request_string));
80 if(entry.get_recurring())
82 find_and_erase(entry, recurring_requests_);
84 cancel_request(entry);
87 DEBUG(binder_interface, "Moving completed recurring request to the back of the queue: %s", request_string);
88 recurring_requests_.push_back(entry);
93 DEBUG(binder_interface, "Cancelling completed, non-recurring request: %s", request_string);
94 find_and_erase(entry, non_recurring_requests_);
95 cancel_request(entry);
100 /// @brief Clean up the request list, move as many to the free list as possible
101 void diagnostic_manager_t::cleanup_active_requests(bool force)
103 for(auto& entry : non_recurring_requests_)
104 cleanup_request(entry, force);
106 for(auto& entry : recurring_requests_)
107 cleanup_request(entry, force);
110 /// @brief Note that this pops it off of whichver list it was on and returns it, so make
111 /// sure to add it to some other list or it'll be lost.
112 bool diagnostic_manager_t::lookup_recurring_request(const DiagnosticRequest* request)
114 active_diagnostic_request_t existingEntry;
115 for (auto& entry : recurring_requests_)
117 active_diagnostic_request_t& candidate = entry;
118 if(candidate.get_can_bus_dev()->get_device_name() == bus_->get_device_name() &&
119 diagnostic_request_equals(&candidate.get_handle().request, request))
121 find_and_erase(entry, recurring_requests_);
122 //existingEntry = entry;
130 void diagnostic_manager_t::reset()
134 DEBUG(binder_interface, "Clearing existing diagnostic requests");
135 cleanup_active_requests(true);
138 for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
139 free_request_entries_.push_back(request_list_entries_[i]);
142 can_bus_dev_t* diagnostic_manager_t::get_can_bus_dev()
147 active_diagnostic_request_t& diagnostic_manager_t::get_free_entry()
149 //FIXME: Test against empty vector
150 //if (request_list_entries_.empty())
153 active_diagnostic_request_t& adr = request_list_entries_.back();
154 request_list_entries_.pop_back();
158 bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::string name,
159 bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
160 const DiagnosticResponseCallback callback)
162 cleanup_active_requests(false);
165 active_diagnostic_request_t& entry = get_free_entry();
167 // TODO: implement Acceptance Filter
168 // if(updateRequiredAcceptanceFilters(bus, request)) {
169 entry = active_diagnostic_request_t(bus_, request, name,
170 wait_for_multiple_responses, decoder, callback, 0);
171 entry.set_handle(shims_, request);
173 char request_string[128] = {0};
174 diagnostic_request_to_string(&entry.get_handle().request, request_string,
175 sizeof(request_string));
177 find_and_erase(entry, non_recurring_requests_);
178 DEBUG(binder_interface, "Added one-time diagnostic request on bus %s: %s",
179 bus_->get_device_name(), request_string);
181 non_recurring_requests_.push_back(entry);
186 bool diagnostic_manager_t::validate_optional_request_attributes(float frequencyHz)
188 if(frequencyHz > MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ) {
189 DEBUG(binder_interface, "Requested recurring diagnostic frequency %d is higher than maximum of %d",
190 frequencyHz, MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ);
196 bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, const char* name,
197 bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
198 const DiagnosticResponseCallback callback, float frequencyHz)
200 if(!validate_optional_request_attributes(frequencyHz))
203 cleanup_active_requests(false);
206 if(lookup_recurring_request(request))
208 active_diagnostic_request_t& entry = get_free_entry();
209 // TODO: implement Acceptance Filter
210 //if(updateRequiredAcceptanceFilters(bus, request)) {
211 entry = active_diagnostic_request_t(bus_, request, name,
212 wait_for_multiple_responses, decoder, callback, frequencyHz);
213 entry.set_handle(shims_, request);
215 char request_string[128] = {0};
216 diagnostic_request_to_string(&entry.get_handle().request, request_string,
217 sizeof(request_string));
219 find_and_erase(entry, recurring_requests_);
220 DEBUG(binder_interface, "Added recurring diagnostic request (freq: %f) on bus %d: %s",
221 frequencyHz, bus_->get_device_name(), request_string);
223 recurring_requests_.push_back(entry);
227 DEBUG(binder_interface, "Can't add request, one already exists with same key");
233 bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
235 can_bus_dev_t *can_bus_dev = config->get_diagnostic_manager().get_can_bus_dev();
236 return can_bus_dev->shims_send(arbitration_id, data, size);
239 void diagnostic_manager_t::shims_logger(const char* m, ...)
241 DEBUG(binder_interface, "%s", m);
244 void diagnostic_manager_t::shims_timer()
248 * @brief initialize shims used by UDS lib and set initialized_ to true.
249 * It is needed before used the diagnostic manager fully because shims are
250 * required by most member functions.
252 void diagnostic_manager_t::init_diagnostic_shims()
254 shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);