#define MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ 10
#define MAX_SIMULTANEOUS_DIAG_REQUESTS 50
+// There are only 8 slots of in flight diagnostic requests
+#define MAX_SIMULTANEOUS_IN_FLIGHT_REQUESTS 8
#define TIMERFD_ACCURACY 0
#define MICRO 1000000
/// to have 1 diagnostic bus which are the first bus declared in the JSON
/// description file. Configuration instance will return it.
///
-/// @desc this will initialize DiagnosticShims and cancel all active requests
+/// this will initialize DiagnosticShims and cancel all active requests
/// if there are any.
bool diagnostic_manager_t::initialize()
{
bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
{
std::shared_ptr<can_bus_dev_t> can_bus_dev = can_bus_t::get_can_device(configuration_t::instance().get_diagnostic_manager().bus_);
- return can_bus_dev->shims_send(arbitration_id, data, size);
+ if(can_bus_dev != nullptr)
+ return can_bus_dev->shims_send(arbitration_id, data, size);
+ ERROR(binder_interface, "shims_send: Can not retrieve diagnostic bus: %s", configuration_t::instance().get_diagnostic_manager().bus_.c_str());
+ return false;
}
/// @brief The type signature for an optional logging function, if the user
/// @param[in] force - Force the cleaning or not ?
void diagnostic_manager_t::cleanup_request(active_diagnostic_request_t* entry, bool force)
{
- if(force || (entry->get_in_flight() && entry->request_completed()))
+ if((force || (entry->get_in_flight() && entry->request_completed())) && entry != nullptr)
{
entry->set_in_flight(false);
/// @brief Add and send a new one-time diagnostic request.
///
-/// @desc A one-time (aka non-recurring) request can existing in parallel with a
+/// A one-time (aka non-recurring) request can existing in parallel with a
/// recurring request for the same PID or mode, that's not a problem.
///
/// For an example, see the docs for addRecurringRequest. This function is very
/// @param[in] name - Human readable name this response, to be used when
/// publishing received responses. TODO: If the name is NULL, the published output
/// will use the raw OBD-II response format.
-/// @param[in] waitForMultipleResponses - If false, When any response is received
+/// @param[in] wait_for_multiple_responses - If false, When any response is received
/// for this request it will be removed from the active list. If true, the
/// request will remain active until the timeout clock expires, to allow it
/// to receive multiple response. Functional broadcast requests will always
/// @param[in] name - An optional human readable name this response, to be used when
/// publishing received responses. If the name is NULL, the published output
/// will use the raw OBD-II response format.
-/// @param[in] waitForMultipleResponses - If false, When any response is received
+/// @param[in] wait_for_multiple_responses - If false, When any response is received
/// for this request it will be removed from the active list. If true, the
/// request will remain active until the timeout clock expires, to allow it
/// to receive multiple response. Functional broadcast requests will always
}
-/// @brief Returns true if there are no other active requests to the same arbitration ID.
+/// @brief Returns true if there are no other active requests to the same arbitration ID
+/// and if there aren't more than 8 requests in flight at the same time.
bool diagnostic_manager_t::clear_to_send(active_diagnostic_request_t* request) const
{
+ int total_in_flight = 0;
for ( auto entry : non_recurring_requests_)
{
if(conflicting(request, entry))
return false;
+ if(entry->get_in_flight())
+ total_in_flight++;
}
for ( auto entry : recurring_requests_)
{
if(conflicting(request, entry))
return false;
+ if(entry->get_in_flight())
+ total_in_flight++;
}
+
+ if(total_in_flight > MAX_SIMULTANEOUS_IN_FLIGHT_REQUESTS)
+ return false;
return true;
}
+int diagnostic_manager_t::reschedule_request(sd_event_source *s, uint64_t usec, active_diagnostic_request_t* adr)
+{
+ usec = usec + (uint64_t)(frequency_clock_t::frequency_to_period(adr->get_frequency_clock().get_frequency())*MICRO);
+ DEBUG(binder_interface, "send_request: Event loop state: %d. usec: %ld", sd_event_get_state(afb_daemon_get_event_loop(binder_interface->daemon)), usec);
+ if(sd_event_source_set_time(s, usec) >= 0)
+ if(sd_event_source_set_enabled(s, SD_EVENT_ON) >= 0)
+ return 0;
+ sd_event_source_unref(s);
+ return -1;
+}
+
/// @brief Systemd timer event callback use to send CAN messages at regular interval. Depending
/// on the diagnostic message frequency.
///
DiagnosticRequest* request = (DiagnosticRequest*)userdata;
active_diagnostic_request_t* adr = dm.find_recurring_request(request);
-// if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev() && adr->should_send() &&
-// dm.clear_to_send(adr))
- if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev())
+ if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev() && adr->should_send() &&
+ dm.clear_to_send(adr))
{
adr->get_frequency_clock().tick();
start_diagnostic_request(&dm.shims_, adr->get_handle());
if(adr->get_handle()->completed && !adr->get_handle()->success)
{
- DEBUG(binder_interface, "send_request: Fatal error sending diagnostic request");
- sd_event_source_unref(s);
- return -1;
+ ERROR(binder_interface, "send_request: Fatal error sending diagnostic request");
+ sd_event_source_unref(s);
+ return -1;
}
+
adr->get_timeout_clock().tick();
adr->set_in_flight(true);
+ }
- if(adr->get_recurring())
- {
- usec = usec + (uint64_t)(frequency_clock_t::frequency_to_period(adr->get_frequency_clock().get_frequency())*MICRO);
- DEBUG(binder_interface, "send_request: Event loop state: %d. usec: %ld", sd_event_get_state(afb_daemon_get_event_loop(binder_interface->daemon)), usec);
- if(sd_event_source_set_time(s, usec+1000000) >= 0)
- if(sd_event_source_set_enabled(s, SD_EVENT_ON) >= 0)
- return 0;
- sd_event_source_unref(s);
- return -1;
- }
+ if(adr->get_recurring())
+ {
+ return dm.reschedule_request(s, usec, adr);
}
+
sd_event_source_unref(s);
ERROR(binder_interface, "send_request: Something goes wrong when submitting a new request to the CAN bus");
return -2;
found_signals.front()->set_supported(false);
cleanup_request(adr, true);
NOTICE(binder_interface, "relay_diagnostic_response: PID not supported or ill formed. Please unsubscribe from it. Error code : %d", response.negative_response_code);
+ message = build_VehicleMessage(build_SimpleMessage(adr->get_name(), build_DynamicField("This PID isn't supported by your vehicle.")));
}
if(adr->get_callback() != nullptr)