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_);
129 void diagnostic_manager_t::reset()
133 DEBUG(binder_interface, "Clearing existing diagnostic requests");
134 cleanup_active_requests(true);
137 for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
138 free_request_entries_.push_back(request_list_entries_[i]);
141 can_bus_dev_t* diagnostic_manager_t::get_can_bus_dev()
146 active_diagnostic_request_t* diagnostic_manager_t::get_free_entry()
148 if (request_list_entries_.empty())
151 active_diagnostic_request_t* adr = request_list_entries_.back();
152 request_list_entries_.pop_back();
156 bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::string name,
157 bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
158 const DiagnosticResponseCallback callback)
160 cleanup_active_requests(false);
163 active_diagnostic_request_t* entry = get_free_entry();
165 if (entry != nullptr)
167 // TODO: implement Acceptance Filter
168 // if(updateRequiredAcceptanceFilters(bus, request)) {
169 entry = new 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);
185 WARNING(binder_interface, "There isn't enough request entry. Vector exhausted %d/%d", (int)request_list_entries_.size(), (int)request_list_entries_.max_size());
191 bool diagnostic_manager_t::validate_optional_request_attributes(float frequencyHz)
193 if(frequencyHz > MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ) {
194 DEBUG(binder_interface, "Requested recurring diagnostic frequency %d is higher than maximum of %d",
195 frequencyHz, MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ);
201 bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, const char* name,
202 bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
203 const DiagnosticResponseCallback callback, float frequencyHz)
205 if(!validate_optional_request_attributes(frequencyHz))
208 cleanup_active_requests(false);
211 if(lookup_recurring_request(request))
213 active_diagnostic_request_t* entry = get_free_entry();
217 // TODO: implement Acceptance Filter
218 //if(updateRequiredAcceptanceFilters(bus, request)) {
219 entry = new active_diagnostic_request_t(bus_, request, name,
220 wait_for_multiple_responses, decoder, callback, frequencyHz);
221 entry->set_handle(shims_, request);
223 char request_string[128] = {0};
224 diagnostic_request_to_string(&entry->get_handle()->request, request_string,
225 sizeof(request_string));
227 find_and_erase(entry, recurring_requests_);
228 DEBUG(binder_interface, "Added recurring diagnostic request (freq: %f) on bus %d: %s",
229 frequencyHz, bus_->get_device_name(), request_string);
231 recurring_requests_.push_back(entry);
235 WARNING(binder_interface, "There isn't enough request entry. Vector exhausted %d/%d", (int)request_list_entries_.size(), (int)request_list_entries_.max_size());
241 DEBUG(binder_interface, "Can't add request, one already exists with same key");
247 bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
249 can_bus_dev_t *can_bus_dev = config->get_diagnostic_manager().get_can_bus_dev();
250 return can_bus_dev->shims_send(arbitration_id, data, size);
253 void diagnostic_manager_t::shims_logger(const char* m, ...)
255 DEBUG(binder_interface, "%s", m);
258 void diagnostic_manager_t::shims_timer()
262 * @brief initialize shims used by UDS lib and set initialized_ to true.
263 * It is needed before used the diagnostic manager fully because shims are
264 * required by most member functions.
266 void diagnostic_manager_t::init_diagnostic_shims()
268 shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);