+bool diagnostic_manager_t::is_diagnostic_response(const active_diagnostic_request_t& adr, const can_message_t& cm) const
+{
+ if(cm.get_id() == adr.get_id() + DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET)
+ return true;
+ DEBUG(binder_interface, "Doesn't find an active diagnostic request that matches.");
+ return false;
+}
+
+active_diagnostic_request_t* diagnostic_manager_t::is_diagnostic_response(const can_message_t& can_message)
+{
+ for (auto& entry : non_recurring_requests_)
+ {
+ if(is_diagnostic_response(*entry, can_message))
+ return entry;
+ }
+
+ for (auto& entry : recurring_requests_)
+ {
+ if(is_diagnostic_response(*entry, can_message))
+ return entry;
+ }
+ return nullptr;
+}
+
+openxc_VehicleMessage diagnostic_manager_t::relay_diagnostic_response(active_diagnostic_request_t* adr, const DiagnosticResponse& response) const
+{
+ openxc_VehicleMessage message;
+ float value = (float)diagnostic_payload_to_integer(&response);
+ if(adr->get_decoder() != nullptr)
+ {
+ value = adr->get_decoder()(&response, value);
+ }
+
+ if((response.success && strnlen(adr->get_name().c_str(), adr->get_name().size())) > 0)
+ {
+ // If name, include 'value' instead of payload, and leave of response
+ // details.
+ message = build_VehicleMessage(build_SimpleMessage(adr->get_name(), build_DynamicField(value)));
+ }
+ else
+ {
+ // If no name, send full details of response but still include 'value'
+ // instead of 'payload' if they provided a decoder. The one case you
+ // can't get is the full detailed response with 'value'. We could add
+ // another parameter for that but it's onerous to carry that around.
+ message = build_VehicleMessage(adr, response, value);
+ }
+
+ if(adr->get_callback() != nullptr)
+ {
+ adr->get_callback()(adr, &response, value);
+ }
+
+ return message;
+}
+
+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.
+bool diagnostic_manager_t::clear_to_send(active_diagnostic_request_t* request) const
+{
+ for ( auto entry : non_recurring_requests_)
+ {
+ if(conflicting(request, entry))
+ return false;
+ }
+
+ for ( auto entry : recurring_requests_)
+ {
+ if(conflicting(request, entry))
+ return false;
+ }
+ return true;
+}
+
+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);
+
+ if(adr != nullptr && adr->get_can_bus_dev() == dm.get_can_bus_dev() && adr->should_send() &&
+ dm.clear_to_send(adr))
+ {
+ DEBUG(binder_interface, "Got active_diagnostic_request from recurring_requests_ queue.");
+ adr->get_frequency_clock().tick();
+ start_diagnostic_request(&dm.get_shims(), adr->get_handle());
+ if(adr->get_handle()->completed && !adr->get_handle()->success)
+ {
+ DEBUG(binder_interface, "Fatal error sending diagnostic request");
+ return 0;
+ }
+ adr->get_timeout_clock().tick();
+ adr->set_in_flight(true);
+ return 1;
+ }
+ return -1;
+}
+
+DiagnosticShims& diagnostic_manager_t::get_shims()
+{
+ return shims_;
+}
+