diag-mngr: Fix sending and change init 94/23294/4
authorRomain Forlot <romain.forlot@iot.bzh>
Fri, 6 Dec 2019 10:34:05 +0000 (11:34 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 9 Jan 2020 15:25:36 +0000 (16:25 +0100)
This commit fix the wrong bcm ival1 value set because the struct
hasn't been initialized so it use a wrong value that can not be
handled by the socket.

Initialize diagnotstic manager depending on JSON configuration file

This avoid to request engine_speed signal when there isn't any diagnostic_bus
configured in the JSON file.

Change-Id: I98145a6e20c29a644c856d818f104c6bd8069f9f
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-cb.cpp
low-can-binding/diagnostic/diagnostic-manager.cpp
low-can-binding/diagnostic/diagnostic-manager.hpp

index 1ff1d9d..1ebb392 100644 (file)
@@ -63,7 +63,7 @@ int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_
 
        application_t *application = (application_t*) ctrlConfig->external;
 
-       if(wrap_json_unpack(json_obj, "{si, ss}",
+       if(wrap_json_unpack(json_obj, "{si, s?s}",
                              "active_message_set", &active_message_set,
                              "diagnostic_bus", &diagnotic_bus))
                return -1;
@@ -78,11 +78,8 @@ int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_
 
        /// Initialize Diagnostic manager that will handle obd2 requests.
        /// We pass by default the first CAN bus device to its Initialization.
-       if(! application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus))
-       {
-               AFB_ERROR("Diagnostic Manager: error at initialization");
-               return -1;
-       }
+       if(! diagnotic_bus || application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus))
+               AFB_WARNING("Diagnostic Manager: not initialized");
 
        return 0;
 }
@@ -872,22 +869,24 @@ int init_binding(afb_api_t api)
        can_bus_manager.start_threads();
        utils::signals_manager_t& sm = utils::signals_manager_t::instance();
 
-       // Add a recurring dignostic message request to get engine speed at all times.
-       openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
-       struct utils::signals_found sf = sm.find_signals(search_key);
-
-       if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
+       if (application.get_diagnostic_manager().is_initialized())
        {
-               afb_req_t request = nullptr;
+               // Add a recurring dignostic message request to get engine speed at all times.
+               openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
+               struct utils::signals_found sf = sm.find_signals(search_key);
 
-               struct event_filter_t event_filter;
-               event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
+               if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
+               {
+                       afb_req_t request = nullptr;
 
-               map_subscription& s = sm.get_subscribed_signals();
+                       struct event_filter_t event_filter;
+                       event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
 
-               subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
-       }
+                       map_subscription& s = sm.get_subscribed_signals();
 
+                       subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
+               }
+       }
 
 #ifdef USE_FEATURE_J1939
        std::string j1939_bus;
index f3c513c..9357883 100644 (file)
@@ -72,6 +72,11 @@ bool diagnostic_manager_t::initialize(std::string diagnostic_bus)
        return initialized_;
 }
 
+bool diagnostic_manager_t::is_initialized() const
+{
+       return initialized_;
+}
+
 /// @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.
@@ -116,6 +121,8 @@ bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8
        bcm_msg.msg_head.can_id  = arbitration_id;
        bcm_msg.msg_head.flags = SETTIMER|STARTTIMER|TX_CP_CAN_ID;
        bcm_msg.msg_head.count = 0;
+       bcm_msg.msg_head.ival1.tv_sec = 0;
+       bcm_msg.msg_head.ival1.tv_usec = 0;
        bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec;
        bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
        bcm_msg.msg_head.nframes = 1;
index c0a8255..3f74ab7 100644 (file)
@@ -60,6 +60,7 @@ public:
        ~diagnostic_manager_t();
 
        bool initialize(std::string diagnostic_bus);
+       bool is_initialized() const;
 
        const std::string get_bus_name() const;
        const std::string get_bus_device_name() const;