Smalls improvements 57/18757/3
authorRomain Forlot <romain.forlot@iot.bzh>
Fri, 7 Dec 2018 16:17:43 +0000 (17:17 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Fri, 14 Dec 2018 08:59:54 +0000 (08:59 +0000)
- Improve robustness reading a BCM socket
  Adding checks on system calls and remove the initialization of the
  struct that will hold the received message, not so much needed in
  that case.
- More accurate log message at subscription/unsubscription step whether
  this is a FD CAN messages or not and if this a subscription or an
  unsubscription that is requested.
- Initialize the full struct of vehicle message to avoid memory warning
  about conditionnal jump based on uninitialized bytes.
- Memleak: Free raw pointer on active diagnostic requests

Change-Id: I4bbf4d851c0fa1efdb6fa6034fac3d1dcafa1a73
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-cb.cpp
low-can-binding/can/can-bus.cpp
low-can-binding/diagnostic/diagnostic-manager.cpp
low-can-binding/utils/openxc-utils.cpp
low-can-binding/utils/socketcan-bcm.cpp

index daeb5a0..f200b19 100644 (file)
@@ -263,7 +263,7 @@ static int subscribe_unsubscribe_can_signals(afb_req_t request,
                        {return -1;}
 
                rets++;
-               AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str());
+               AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
        }
        return rets;
 }
index 49a6b60..e798618 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Copyright (C) 2015, 2018 "IoT.bzh"
  * Author "Romain Forlot" <romain.forlot@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -87,12 +87,12 @@ void can_bus_t::process_can_signals(const can_message_t& can_message, std::map<i
        openxc_DynamicField decoded_message;
        openxc_VehicleMessage vehicle_message;
 
-       // First we have to found which can_signal_t it is
-       std::shared_ptr<low_can_subscription_t> sig = s[subscription_id];
-
        if( s.find(subscription_id) != s.end() && afb_event_is_valid(s[subscription_id]->get_event()))
        {
                bool send = true;
+               // First we have to found which can_signal_t it is
+               std::shared_ptr<low_can_subscription_t> sig = s[subscription_id];
+
                decoded_message = decoder_t::translate_signal(*sig->get_can_signal(), can_message, &send);
                openxc_SimpleMessage s_message = build_SimpleMessage(sig->get_name(), decoded_message);
                vehicle_message = build_VehicleMessage(s_message, can_message.get_timestamp());
index ba4b0c8..55e6108 100644 (file)
@@ -188,6 +188,8 @@ void diagnostic_manager_t::find_and_erase(active_diagnostic_request_t* entry, st
 void diagnostic_manager_t::cancel_request(active_diagnostic_request_t* entry)
 {
        entry->get_socket().close();
+       if(entry->get_handle())
+               delete(entry->get_handle());
        delete entry;
        entry = nullptr;
 }
index e454121..c06beab 100644 (file)
@@ -34,6 +34,7 @@
 const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
 {
        openxc_VehicleMessage message;
+       ::memset(&message, 0, sizeof(message));
        application_t& app = application_t::instance();
 
        message.has_type = true;
@@ -98,6 +99,7 @@ const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* re
 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message, uint64_t timestamp)
 {
        openxc_VehicleMessage v;
+       ::memset(&v, 0, sizeof(v));
 
        v.has_type = true,
        v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
@@ -120,6 +122,7 @@ const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& mes
 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
 {
        openxc_VehicleMessage v;
+       ::memset(&v, 0, sizeof(v));
 
        v.has_type = true,
        v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
@@ -141,7 +144,7 @@ openxc_VehicleMessage build_VehicleMessage()
 {
        openxc_VehicleMessage v;
 
-       ::memset(&v, 0, sizeof(openxc_VehicleMessage));
+       ::memset(&v, 0, sizeof(v));
        return v;
 }
 
index d1fd8e0..3f3a096 100644 (file)
@@ -74,14 +74,22 @@ namespace utils
        {
                struct utils::bcm_msg msg;
 
-               ::memset(&msg, 0, sizeof(msg));
                const struct sockaddr_can& addr = s.get_tx_address();
                socklen_t addrlen = sizeof(addr);
                struct ifreq ifr;
 
                ssize_t nbytes = ::recvfrom(s.socket(), &msg, sizeof(msg), 0, (struct sockaddr*)&addr, &addrlen);
+               if(nbytes < 0)
+               {
+                       AFB_ERROR("Can't read the next message from socket '%d'. '%s'", s.socket(), strerror(errno));
+                       return s;
+               }
                ifr.ifr_ifindex = addr.can_ifindex;
-               ioctl(s.socket(), SIOCGIFNAME, &ifr);
+               if(ioctl(s.socket(), SIOCGIFNAME, &ifr) < 0)
+               {
+                       AFB_ERROR("Can't read the interface name. '%s'", strerror(errno));
+                       return s;
+               }
                long unsigned int frame_size = nbytes-sizeof(struct bcm_msg_head);
 
                AFB_DEBUG("Data available: %li bytes read. BCM head, opcode: %i, can_id: %i, nframes: %i", frame_size, msg.msg_head.opcode, msg.msg_head.can_id, msg.msg_head.nframes);