Fix: Initialization and entries flow
[apps/agl-service-can-low-level.git] / src / diagnostic / diagnostic-manager.cpp
index 6867ec3..915b449 100644 (file)
@@ -20,6 +20,7 @@
 #include "diagnostic-manager.hpp"
 
 #include "uds/uds.h"
+#include "../utils/openxc-utils.hpp"
 #include "../configuration.hpp"
 
 #define MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ 10
@@ -27,7 +28,7 @@
 #define MAX_REQUEST_ENTRIES 50
 
 diagnostic_manager_t::diagnostic_manager_t()
-       : request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
+       : request_list_entries_(MAX_REQUEST_ENTRIES, new active_diagnostic_request_t()), initialized_{false}
 {}
 
 bool diagnostic_manager_t::initialize(std::shared_ptr<can_bus_dev_t> cbd)
@@ -62,8 +63,11 @@ void diagnostic_manager_t::reset()
                cleanup_active_requests(true);
        }
 
-       for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
-               free_request_entries_.push_back(request_list_entries_[i]);
+       for(uint8_t i = 0; i < MAX_REQUEST_ENTRIES; i++)
+       {
+               free_request_entries_.push_back(request_list_entries_.back());
+               request_list_entries_.pop_back();
+       }
 }
 
 
@@ -160,11 +164,11 @@ std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
 
 active_diagnostic_request_t* diagnostic_manager_t::get_free_entry()
 {
-       if (request_list_entries_.empty())
+       if (free_request_entries_.empty())
                return nullptr;
 
-       active_diagnostic_request_t* adr = request_list_entries_.back();
-       request_list_entries_.pop_back();
+       active_diagnostic_request_t* adr = free_request_entries_.back();
+       free_request_entries_.pop_back();
        return adr;
 }
 
@@ -240,8 +244,8 @@ bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, con
                                                sizeof(request_string));
 
                                find_and_erase(entry, recurring_requests_);
-                               DEBUG(binder_interface, "Added recurring diagnostic request (freq: %f) on bus %d: %s",
-                                               frequencyHz, bus_->get_device_name(), request_string);
+                               DEBUG(binder_interface, "Added recurring diagnostic request (freq: %f) on bus %s: %s",
+                                               frequencyHz, bus_->get_device_name().c_str(), request_string);
 
                                recurring_requests_.push_back(entry);
                }
@@ -259,6 +263,62 @@ bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, con
        return added;
 }
 
+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 &&