- {
- DEBUG(binder_interface, "%s: Can't add request, one already exists with same key", __FUNCTION__);
- added = false;
- }
- return added;
-}
-
-/// @brief Returns true if there are two active requests running for the same arbitration ID.
-bool diagnostic_manager_t::conflicting(active_diagnostic_request_t* request, active_diagnostic_request_t* candidate) const
-{
- return (candidate->get_in_flight() && candidate != request &&
- candidate->get_can_bus_dev() == request->get_can_bus_dev() &&
- candidate->get_id() == request->get_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)(adr->get_frequency_clock().frequency_to_period());
- DEBUG(binder_interface, "%s: Event loop state: %d. usec: %ld", __FUNCTION__, 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.
-///
-/// This should be called from systemd binder event loop and the event is created on add_recurring_request
-///
-/// @param[in] s - Systemd event source pointer used to reschedule the new iteration.
-/// @param[in] usec - previous call timestamp in microseconds.
-/// @param[in] userdata - the DiagnosticRequest struct, use to retrieve the active request from the list.
-///
-/// @return positive integer if sent and rescheduled or negative value if something wrong. If an error occurs
-/// event will be disabled.
-int diagnostic_manager_t::send_request(sd_event_source *s, uint64_t usec, void *userdata)
-{
- diagnostic_manager_t& dm = configuration_t::instance().get_diagnostic_manager();
- DiagnosticRequest* request = (DiagnosticRequest*)userdata;
- active_diagnostic_request_t* adr = dm.find_recurring_request(request);
-
- dm.cleanup_active_requests(false);
- 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)
- {
- ERROR(binder_interface, "%s: Fatal error sending diagnostic request", __FUNCTION__);
- sd_event_source_unref(s);
- return -1;
- }
-
- adr->get_timeout_clock().tick();
- adr->set_in_flight(true);
- }
-
- if(adr != nullptr && adr->get_recurring())
- {
- return dm.reschedule_request(s, usec, adr);
- }
-
- sd_event_source_unref(s);
- NOTICE(binder_interface, "%s: Request doesn't exist anymore. Canceling.'", __FUNCTION__);
- return -2;