Make diagnostic manager initialization processus.
authorRomain Forlot <romain.forlot@iot.bzh>
Sun, 12 Mar 2017 18:38:49 +0000 (19:38 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 16 Mar 2017 16:10:41 +0000 (17:10 +0100)
It is initiliazed with by default the first CAN bus
device in the CAN bus device list from CAN bus manager.

The object is instancied at configuration_t object first
invokation and after all CAN buses has been initialized then
the diag manager is initialized too.

Change-Id: I4894f2c62f575676c34efec3608b97de8c5326e1
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
src/can/can-bus.cpp
src/can/can-bus.hpp
src/diagnostic/active-diagnostic-request.cpp
src/diagnostic/active-diagnostic-request.hpp
src/diagnostic/diagnostic-manager.cpp
src/diagnostic/diagnostic-manager.hpp
src/low-can-binding.cpp

index 7a18ce8..8edf5ba 100644 (file)
@@ -186,13 +186,13 @@ int can_bus_t::init_can_dev()
 
                for(const auto& device : devices_name)
                {
-                       can_devices_m_[device] = std::make_shared<can_bus_dev_t>(device, i);
-                       if (can_devices_m_[device]->open() == 0)
+                       can_devices_.push_back(std::make_shared<can_bus_dev_t>(device, i));
+                       if (can_devices_[i]->open() == 0)
                        {
                                i++;
                                DEBUG(binder_interface, "Start reading thread");
                                NOTICE(binder_interface, "%s device opened and reading", device.c_str());
-                               can_devices_m_[device]->start_reading(*this);
+                               can_devices_[i]->start_reading(*this);
                        }
                        else
                                ERROR(binder_interface, "Can't open device %s", device.c_str());
@@ -349,8 +349,8 @@ void can_bus_t::push_new_vehicle_message(const openxc_VehicleMessage& v_msg)
 *
 * @return map can_bus_dev_m_ map
 */
-std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_bus_t::get_can_devices()
+const std::vector<std::shared_ptr<can_bus_dev_t>>& can_bus_t::get_can_devices() const
 {
-       return can_devices_m_;
+       return can_devices_;
 }
 
index 17e60d2..d4d6d74 100644 (file)
@@ -65,9 +65,9 @@ private:
 
        std::condition_variable new_decoded_can_message_; /// < condition_variable use to wait until there is a new vehicle message to read from the queue vehicle_message_q_
        std::mutex decoded_can_message_mutex_;  /// < mutex protecting the vehicle_message_q_ queue.
-       std::queue <openxc_VehicleMessage> vehicle_message_q_; /// < queue that'll store openxc_VehicleMessage to pushed
+       std::queue <openxc_VehicleMessage> vehicle_message_q_; /// < queue that'll store openxc_VehicleMessage to pushed 
 
-       std::map<std::string, std::shared_ptr<can_bus_dev_t>> can_devices_m_; /// < Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
+       std::vector<std::shared_ptr<can_bus_dev_t>> can_devices_; /// < Can device map containing all can_bus_dev_t objects initialized during init_can_dev function
 
 public:
        can_bus_t(int conf_file);
@@ -87,6 +87,6 @@ public:
        openxc_VehicleMessage next_vehicle_message();
        void push_new_vehicle_message(const openxc_VehicleMessage& v_msg);
 
-       std::map<std::string, std::shared_ptr<can_bus_dev_t>> get_can_devices();
+       const std::vector<std::shared_ptr<can_bus_dev_t>>& get_can_devices() const;
 };
 
index baa1184..505d816 100644 (file)
@@ -48,7 +48,7 @@ active_diagnostic_request_t::active_diagnostic_request_t()
          in_flight_{false}, frequency_clock_{frequency_clock_t()}, timeout_clock_{frequency_clock_t()}
 {}
 
-active_diagnostic_request_t::active_diagnostic_request_t(can_bus_dev_t* bus, DiagnosticRequest* request,
+active_diagnostic_request_t::active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
                const std::string& name, bool wait_for_multiple_responses,
                const DiagnosticResponseDecoder decoder,
                const DiagnosticResponseCallback callback, float frequencyHz)
@@ -57,7 +57,7 @@ active_diagnostic_request_t::active_diagnostic_request_t(can_bus_dev_t* bus, Dia
          in_flight_{false}, frequency_clock_{frequency_clock_t(frequencyHz)}, timeout_clock_{frequency_clock_t(10)}
 {}
 
-can_bus_dev_t* active_diagnostic_request_t::get_can_bus_dev()
+std::shared_ptr<can_bus_dev_t> active_diagnostic_request_t::get_can_bus_dev()
 {
        return bus_;
 }
index a9abc13..88d4008 100644 (file)
@@ -56,7 +56,7 @@ typedef void (*DiagnosticResponseCallback)(const active_diagnostic_request_t* re
  */
 class active_diagnostic_request_t {
 private:
-       can_bus_dev_t* bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
+       std::shared_ptr<can_bus_dev_t> bus_; /*!< bus_ - The CAN bus this request should be made on, or is currently in flight-on*/
        uint32_t id_; /*!< id_ - The arbitration ID (aka message ID) for the request.*/
        DiagnosticRequestHandle* handle_; /*!< handle_ - A handle for the request to keep track of it between
                                                                                * sending the frames of the request and receiving all frames of the response.*/
@@ -83,12 +83,12 @@ public:
        active_diagnostic_request_t();
        active_diagnostic_request_t(active_diagnostic_request_t&&) = default;
        //active_diagnostic_request_t(const active_diagnostic_request_t&) = default;
-       active_diagnostic_request_t(can_bus_dev_t* bus, DiagnosticRequest* request,
+       active_diagnostic_request_t(std::shared_ptr<can_bus_dev_t> bus, DiagnosticRequest* request,
                const std::string& name, bool wait_for_multiple_responses,
                const DiagnosticResponseDecoder decoder,
                const DiagnosticResponseCallback callback, float frequencyHz);
        
-       can_bus_dev_t* get_can_bus_dev();
+       std::shared_ptr<can_bus_dev_t> get_can_bus_dev();
        DiagnosticRequestHandle* get_handle();
        bool get_recurring() const;
        bool get_in_flight() const;
index 92ce33b..5103bf1 100644 (file)
 
 diagnostic_manager_t::diagnostic_manager_t()
        : request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
+{}
+
+bool diagnostic_manager_t::initialize(std::shared_ptr<can_bus_dev_t> cbd)
 {
+       // Mandatory to set the bus before intiliaze shims.
+       bus_ = cbd;
+
+       init_diagnostic_shims();
        reset();
+
+       initialized_ = true;
+       DEBUG(binder_interface, "initialize: Diagnostic Manager initialized");
+       return initialized_;
 }
 
-diagnostic_manager_t::diagnostic_manager_t(can_bus_dev_t& bus)
-       : bus_(&bus), request_list_entries_(MAX_REQUEST_ENTRIES), initialized_{false}
+/**
+ * @brief initialize shims used by UDS lib and set initialized_ to true.
+ *  It is needed before used the diagnostic manager fully because shims are
+ *  required by most member functions.
+ */
+void diagnostic_manager_t::init_diagnostic_shims()
 {
-       reset();
+       shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);
+       DEBUG(binder_interface, "init_diagnostic_shims: Shims initialized");
+}
+
+void diagnostic_manager_t::reset()
+{
+       if(initialized_)
+       {
+               DEBUG(binder_interface, "Clearing existing diagnostic requests");
+               cleanup_active_requests(true);
+       }
+
+       for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
+               free_request_entries_.push_back(request_list_entries_[i]);
 }
 
+
 void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t* entry, std::vector<active_diagnostic_request_t*>& requests_list)
 {
        auto i = std::find(requests_list.begin(), requests_list.end(), entry);
@@ -126,19 +155,7 @@ bool diagnostic_manager_t::lookup_recurring_request(const DiagnosticRequest* req
        return false;
 }
 
-void diagnostic_manager_t::reset()
-{
-       if(initialized_)
-       {
-               DEBUG(binder_interface, "Clearing existing diagnostic requests");
-               cleanup_active_requests(true);
-       }
-
-       for(int i = 0; i < MAX_SIMULTANEOUS_DIAG_REQUESTS; i++)
-               free_request_entries_.push_back(request_list_entries_[i]);
-}
-
-can_bus_dev_t* diagnostic_manager_t::get_can_bus_dev()
+std::shared_ptr<can_bus_dev_t> diagnostic_manager_t::get_can_bus_dev()
 {
        return bus_;
 }
@@ -246,7 +263,7 @@ bool diagnostic_manager_t::add_recurring_request(DiagnosticRequest* request, con
 
 bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
 {
-       can_bus_dev_t *can_bus_dev = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev();
+       std::shared_ptr<can_bus_dev_t> can_bus_dev = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev();
        return can_bus_dev->shims_send(arbitration_id, data, size);
 }
 
@@ -258,13 +275,3 @@ void diagnostic_manager_t::shims_logger(const char* m, ...)
 void diagnostic_manager_t::shims_timer()
 {}
 
-/**
- * @brief initialize shims used by UDS lib and set initialized_ to true.
- *  It is needed before used the diagnostic manager fully because shims are
- *  required by most member functions.
- */
-void diagnostic_manager_t::init_diagnostic_shims()
-{
-       shims_ = diagnostic_init_shims(shims_logger, shims_send, NULL);
-       initialized_ = true;
-}
index 8602db8..39aae32 100644 (file)
@@ -49,7 +49,7 @@ protected:
 private:
        DiagnosticShims shims_; /*!< shims_ - An array of shim functions for each CAN bus that plug the diagnostics
                                                         * library (uds-c) into the VI's CAN peripheral.*/
-       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
+       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
@@ -64,14 +64,16 @@ private:
 
        bool initialized_; /*!< * initialized - True if the DiagnosticsManager has been initialized with shims. It will interface with the uds-c lib*/
 
+       void init_diagnostic_shims();
+       void reset();
 public:
        diagnostic_manager_t();
-       diagnostic_manager_t(can_bus_dev_t& bus);
 
-       void init_diagnostic_shims();
+       bool initialize(std::shared_ptr<can_bus_dev_t> cbd);
 
-       can_bus_dev_t* get_can_bus_dev();
+       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);
        void cancel_request(active_diagnostic_request_t* entry);
@@ -81,7 +83,6 @@ public:
 
 
        bool validate_optional_request_attributes(float frequencyHz);
-       void reset();
 
        void checkSupportedPids(const active_diagnostic_request_t& request,
                const DiagnosticResponse& response, float parsedPayload);
index a2b5456..394d906 100644 (file)
@@ -223,13 +223,17 @@ extern "C"
        {
                can_bus_t& can_bus_manager = configuration_t::instance().get_can_bus_manager();
 
-               /* Open CAN socket */
+               /// Initialize CAN socket
                if(can_bus_manager.init_can_dev() == 0)
                {
                        can_bus_manager.start_threads();
                        return 0;
                }
 
+               /// Initialize Diagnostic manager that will handle obd2 requests
+               diagnostic_manager_t& diag_manager = configuration_t::instance().get_diagnostic_manager();
+               diag_manager.initialize(can_bus_manager.get_can_devices().front());
+
                ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");
                return 1;
        }