Fix: Avoid segfault if diagnostic bus isn't correctly set.
authorRomain Forlot <romain.forlot@iot.bzh>
Mon, 20 Mar 2017 09:08:12 +0000 (09:08 +0000)
committerRomain Forlot <romain.forlot@iot.bzh>
Mon, 20 Mar 2017 09:08:12 +0000 (09:08 +0000)
There is a segfault when binding configuration doesn't matches configuration
generated. Improve general CAN bus device initialization.

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

index a81c142..cfb9d86 100644 (file)
@@ -240,7 +240,10 @@ int can_bus_t::init_can_dev()
                                i++;
                        }
                        else
+                       {
                                ERROR(binder_interface, "Can't open device %s", device.c_str());
+                               return 1;
+                       }
                }
 
                NOTICE(binder_interface, "Initialized %d/%d can bus device(s)", i, t);
index 69e2a7c..017601c 100644 (file)
@@ -81,7 +81,10 @@ void diagnostic_manager_t::reset()
 bool diagnostic_manager_t::shims_send(const uint32_t arbitration_id, const uint8_t* data, const uint8_t size)
 {
        std::shared_ptr<can_bus_dev_t> can_bus_dev = can_bus_t::get_can_device(configuration_t::instance().get_diagnostic_manager().bus_);
-       return can_bus_dev->shims_send(arbitration_id, data, size);
+       if(can_bus_dev != nullptr)
+               return can_bus_dev->shims_send(arbitration_id, data, size);
+       ERROR(binder_interface, "shims_send: Can not retrieve diagnostic bus: %s", configuration_t::instance().get_diagnostic_manager().bus_.c_str());
+       return false;
 }
 
 /// @brief The type signature for an optional logging function, if the user
@@ -446,12 +449,18 @@ int diagnostic_manager_t::send_request(sd_event_source *s, uint64_t usec, void *
        {
                adr->get_frequency_clock().tick();
                start_diagnostic_request(&dm.shims_, adr->get_handle());
-               if(adr->get_handle()->completed && !adr->get_handle()->success)
+               if(adr->get_handle()->completed)
                {
-                       DEBUG(binder_interface, "send_request: Fatal error sending diagnostic request");
-                       sd_event_source_unref(s);
-                       return -1;
+                       if(!adr->get_handle()->success)
+                       {
+                               ERROR(binder_interface, "send_request: Fatal error sending diagnostic request");
+                               sd_event_source_unref(s);
+                               return -1;
+                       }
                }
+               else
+                       WARNING(binder_interface, "send_request: There was a problem sending your request using bus %s.", adr->get_can_bus_dev()->get_device_name().c_str());
+
                adr->get_timeout_clock().tick();
                adr->set_in_flight(true);
 
index 3e87072..45213d9 100644 (file)
@@ -255,13 +255,15 @@ extern "C"
 
                /// Initialize CAN socket
                if(can_bus_manager.init_can_dev() == 0)
+               {
                        can_bus_manager.start_threads();
 
-               /// Initialize Diagnostic manager that will handle obd2 requests.
-               /// We pass by default the first CAN bus device to its Initialization.
-               /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
-               if(configuration_t::instance().get_diagnostic_manager().initialize())
-                       return 0;
+                       /// Initialize Diagnostic manager that will handle obd2 requests.
+                       /// We pass by default the first CAN bus device to its Initialization.
+                       /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
+                       if(configuration_t::instance().get_diagnostic_manager().initialize())
+                               return 0;
+               }
 
                ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe");
                return 1;