Use UDS PID has key to record subscriptions
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / utils / socketcan-bcm.cpp
index b0148d9..cd84a85 100644 (file)
@@ -19,6 +19,9 @@
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <fcntl.h>
+
+#include "../binding/application.hpp"
 
 namespace utils
 {
@@ -58,6 +61,8 @@ namespace utils
                                ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno));
                                close();
                        }
+                       // Needed because of using systemD event loop. See sd_event_add_io manual.
+                       fcntl(socketcan_t::socket_, F_SETFL, O_NONBLOCK);
                }
                return socket_;
        }
@@ -81,7 +86,16 @@ namespace utils
                DEBUG(binder_interface, "read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, msg.msg_head.can_id, msg.frames.can_dlc,
                        msg.frames.data[0], msg.frames.data[1], msg.frames.data[2], msg.frames.data[3], msg.frames.data[4], msg.frames.data[5], msg.frames.data[6], msg.frames.data[7]);
 
-               cm = ::can_message_t::convert_from_frame(msg.frames , nbytes-sizeof(struct bcm_msg_head));
+               struct timeval tv;
+               ioctl(s.socket(), SIOCGSTAMP, &tv);
+               uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+               cm = ::can_message_t::convert_from_frame(msg.frames ,
+                               nbytes-sizeof(struct bcm_msg_head),
+                               timestamp);
+               if(application_t::instance().get_diagnostic_manager().is_diagnostic_response(cm))
+                       {cm.set_sub_id(msg.frames.data[2]);}
+               else
+                       {cm.set_sub_id((int)s.socket());}
 
                return s;
        }