Simplification of diagnostic manager. Delete uneeded vector
authorRomain Forlot <romain.forlot@iot.bzh>
Tue, 14 Mar 2017 23:47:06 +0000 (00:47 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 16 Mar 2017 16:21:57 +0000 (17:21 +0100)
Control size manually, don't rely on STL container.

Change-Id: Iede2f9b233c5b8a6dc77c9b146ffc92da8e7a989
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
src/diagnostic/diagnostic-manager.cpp
src/diagnostic/diagnostic-manager.hpp

index 2821dc0..a9af6e4 100644 (file)
 
 #define MAX_RECURRING_DIAGNOSTIC_FREQUENCY_HZ 10
 #define MAX_SIMULTANEOUS_DIAG_REQUESTS 50
-#define MAX_REQUEST_ENTRIES 50
 #define TIMERFD_ACCURACY 0
 #define MICRO 1000000
 
 diagnostic_manager_t::diagnostic_manager_t()
-       : request_list_entries_(MAX_REQUEST_ENTRIES, new active_diagnostic_request_t()), initialized_{false}
+       : initialized_{false}
 {}
 
 bool diagnostic_manager_t::initialize(std::shared_ptr<can_bus_dev_t> cbd)
@@ -65,12 +64,6 @@ void diagnostic_manager_t::reset()
                DEBUG(binder_interface, "Clearing existing diagnostic requests");
                cleanup_active_requests(true);
        }
-
-       for(uint8_t i = 0; i < MAX_REQUEST_ENTRIES; i++)
-       {
-               free_request_entries_.push_back(request_list_entries_.back());
-               request_list_entries_.pop_back();
-       }
 }
 
 
@@ -85,7 +78,7 @@ void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t* entry, st
 /// CAN filters it used.
 void diagnostic_manager_t::cancel_request(active_diagnostic_request_t* entry)
 {
-       free_request_entries_.push_back(entry);
+
        /* TODO: implement acceptance filters.
        if(entry.arbitration_id_ == OBD2_FUNCTIONAL_BROADCAST_ID) {
                for(uint32_t filter = OBD2_FUNCTIONAL_RESPONSE_START;
@@ -118,43 +111,41 @@ void diagnostic_manager_t::cleanup_request(active_diagnostic_request_t* entry, b
                        find_and_erase(entry, recurring_requests_);
                        if(force)
                                cancel_request(entry);
-                       else
-                       {
-                               DEBUG(binder_interface, "Moving completed recurring request to the back of the queue: %s", request_string);
-                               recurring_requests_.push_back(entry);
-                       }
                }
                else
                {
-                       DEBUG(binder_interface, "Cancelling completed, non-recurring request: %s", request_string);
+                       DEBUG(binder_interface, "cleanup_request: Cancelling completed, non-recurring request: %s", request_string);
                        find_and_erase(entry, non_recurring_requests_);
                        cancel_request(entry);
                }
        }
 }
 
-/// @brief Clean up the request list, move as many to the free list as possible
+/// @brief Clean up the request list
 void diagnostic_manager_t::cleanup_active_requests(bool force)
 {
        for(auto& entry : non_recurring_requests_)
-               cleanup_request(entry, force);
+               if (entry != nullptr)
+                       cleanup_request(entry, force);
 
        for(auto& entry : recurring_requests_)
-               cleanup_request(entry, force);
+               if (entry != nullptr)
+                       cleanup_request(entry, force);
 }
 
-/// @brief Note that this pops it off of whichver list it was on and returns it, so make
-/// sure to add it to some other list or it'll be lost.
+/// @brief Will return the active_diagnostic_request_t pointer of found request or nullptr if
+/// not found.
 active_diagnostic_request_t* diagnostic_manager_t::find_recurring_request(const DiagnosticRequest* request)
 {
        for (auto& entry : recurring_requests_)
        {
-               active_diagnostic_request_t* candidate = entry;
-               if(diagnostic_request_equals(&candidate->get_handle()->request, request))
+               if(entry != nullptr)
                {
-                       find_and_erase(entry, recurring_requests_);
-                       return entry;
-                       break;
+                       if(diagnostic_request_equals(&entry->get_handle()->request, request))
+                       {
+                               return entry;
+                               break;
+                       }
                }
        }
        return nullptr;
@@ -165,16 +156,6 @@ std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
        return bus_;
 }
 
-active_diagnostic_request_t* diagnostic_manager_t::get_free_entry()
-{
-       if (free_request_entries_.empty())
-               return nullptr;
-
-       active_diagnostic_request_t* adr = free_request_entries_.back();
-       free_request_entries_.pop_back();
-       return adr;
-}
-
 bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::string name,
        bool wait_for_multiple_responses, const DiagnosticResponseDecoder decoder,
        const DiagnosticResponseCallback callback)
@@ -182,13 +163,12 @@ bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::st
        cleanup_active_requests(false);
 
        bool added = true;
-       active_diagnostic_request_t* entry = get_free_entry();
 
-       if (entry != nullptr)
+       if (non_recurring_requests_.size() <= MAX_SIMULTANEOUS_DIAG_REQUESTS)
        {
                // TODO: implement Acceptance Filter
                //      if(updateRequiredAcceptanceFilters(bus, request)) {
-                       entry = new active_diagnostic_request_t(bus_, request, name,
+                       active_diagnostic_request_t* entry = new active_diagnostic_request_t(bus_, request, name,
                                        wait_for_multiple_responses, decoder, callback, 0);
                        entry->set_handle(shims_, request);
 
@@ -204,7 +184,8 @@ bool diagnostic_manager_t::add_request(DiagnosticRequest* request, const std::st
        }
        else
        {
-               WARNING(binder_interface, "There isn't enough request entry. Vector exhausted %d/%d", (int)request_list_entries_.size(), (int)request_list_entries_.max_size());
+               WARNING(binder_interface, "There isn't enough request entry. Vector exhausted %d/%d", (int)non_recurring_requests_.size());
+               non_recurring_requests_.resize(MAX_SIMULTANEOUS_DIAG_REQUESTS);
                added = false;
        }
        return added;
index 061f5f9..71662ba 100644 (file)
@@ -18,7 +18,6 @@
 #pragma once
 
 #include <systemd/sd-event.h>
-#include <queue>
 #include <vector>
 
 #include "uds/uds.h"
@@ -54,17 +53,9 @@ private:
                                                         * library (uds-c) into the VI's CAN peripheral.*/
        std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - A pointer to the CAN bus that should be used for all standard OBD-II requests, if the bus is not
                                                  * explicitly spcified in the request. If NULL, all requests require an explicit bus.*/
-       std::vector<active_diagnostic_request_t*> recurring_requests_; /*!< recurringRequests - A queue of active, recurring diagnostic requests. When
-                                                                                                                                 * a response is received for a recurring request or it times out, it is
-                                                                                                                                 * popped from the queue and pushed onto the back. */
+       std::vector<active_diagnostic_request_t*> recurring_requests_; /*!< recurringRequests - A list of active recurring diagnostic requests.*/
        std::vector<active_diagnostic_request_t*> non_recurring_requests_; /*!< nonrecurringRequests - A list of active one-time diagnostic requests. When a
-                                                                                                                                          * response is received for a non-recurring request or it times out, it is
-                                                                                                                                          * removed from this list and placed back in the free list.*/
-       std::vector<active_diagnostic_request_t*> free_request_entries_; /*!< freeRequestEntries - A list of all available slots for active diagnostic
-                                                                                                                                        * requests. This free list is backed by statically allocated entries in
-                                                                                                                                        * the requestListEntries attribute.*/
-       std::vector<active_diagnostic_request_t*> request_list_entries_; /*!< requestListEntries - Static allocation for all active diagnostic requests.*/
-
+                                                                                                                                          * response is received for a non-recurring request or it times out, it is removed*/
        bool initialized_; /*!< * initialized - True if the DiagnosticsManager has been initialized with shims. It will interface with the uds-c lib*/
 
        void init_diagnostic_shims();
@@ -75,7 +66,6 @@ public:
        bool initialize(std::shared_ptr<can_bus_dev_t> cbd);
 
        std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
-       active_diagnostic_request_t* get_free_entry();
        DiagnosticShims& get_shims();
 
        void find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list);