From d2d2620bbb8f0aad0181e67e9f6affc3d6cb4a51 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 26 Jun 2019 10:34:04 +0200 Subject: Change can_message_t class usage for new j1939 This commit transforms the class can_message_t as the base class and creates two derived classes: j1939_message_t and can_message_t. Bug-AGL: SPEC-2386 Change-Id: I6d3afd8e4f5abff2cd0ec4e9910bd52a2893de76 Signed-off-by: Arthur Guyader Signed-off-by: Stephane Desneux Signed-off-by: Romain Forlot --- low-can-binding/utils/socketcan-bcm.cpp | 23 +++++++---------------- low-can-binding/utils/socketcan-bcm.hpp | 20 ++++---------------- low-can-binding/utils/socketcan-j1939.cpp | 4 ++-- low-can-binding/utils/socketcan.hpp | 8 ++++---- 4 files changed, 17 insertions(+), 38 deletions(-) (limited to 'low-can-binding/utils') diff --git a/low-can-binding/utils/socketcan-bcm.cpp b/low-can-binding/utils/socketcan-bcm.cpp index b9124ee3..0f6bc75c 100644 --- a/low-can-binding/utils/socketcan-bcm.cpp +++ b/low-can-binding/utils/socketcan-bcm.cpp @@ -70,7 +70,7 @@ namespace utils /// Read the socket to retrieve the associated CAN message. All the hard work is do into /// convert_from_frame method and if there isn't CAN message retrieve, only BCM head struct, /// then CAN message will be zeroed and must be handled later. - std::shared_ptr socketcan_bcm_t::read_message() + std::shared_ptr socketcan_bcm_t::read_message() { struct bcm_msg msg; std::shared_ptr cm = std::make_shared(); @@ -98,33 +98,24 @@ namespace utils struct timeval tv; ioctl(socket(), SIOCGSTAMP, &tv); uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec; - *cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp); + cm = can_message_t::convert_from_frame(msg.fd_frames[0] , frame_size, timestamp); cm->set_sub_id((int)socket()); return cm; } - void socketcan_bcm_t::write_message(std::vector>& vobj) + void socketcan_bcm_t::write_message(std::vector>& vobj) { for(const auto& obj : vobj) write_message(obj); } - void socketcan_bcm_t::write_message(std::shared_ptr m) - { - struct can_frame obj; - obj.can_id = m->get_id(); - obj.can_dlc = m->get_length(); - ::memcpy(obj.data, m->get_data(), CAN_MAX_DLEN); - if (::sendto(socket(), &obj, sizeof(&obj), 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0) - AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno)); - } - - void socketcan_bcm_t::write_message(struct bcm_msg& obj) + void socketcan_bcm_t::write_message(std::shared_ptr m) { + struct bcm_msg obj = m->get_bcm_msg(); size_t size = (obj.msg_head.flags & CAN_FD_FRAME) ? - (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj): - (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj); + (size_t)((char*)&obj.fd_frames[obj.msg_head.nframes] - (char*)&obj): + (size_t)((char*)&obj.frames[obj.msg_head.nframes] - (char*)&obj); if (::sendto(socket(), &obj, size, 0, (const struct sockaddr*)&get_tx_address(), sizeof(get_tx_address())) < 0) AFB_API_ERROR(afbBindingV3root, "Error sending : %i %s", errno, ::strerror(errno)); } diff --git a/low-can-binding/utils/socketcan-bcm.hpp b/low-can-binding/utils/socketcan-bcm.hpp index 00028905..5e86aa4f 100644 --- a/low-can-binding/utils/socketcan-bcm.hpp +++ b/low-can-binding/utils/socketcan-bcm.hpp @@ -18,21 +18,10 @@ #pragma once #include "socketcan.hpp" -#include "../can/can-message.hpp" - -#define MAX_BCM_CAN_FRAMES 257 +#include "../can/message/can-message.hpp" namespace utils { - struct bcm_msg - { - struct bcm_msg_head msg_head; - union { - struct canfd_frame fd_frames[MAX_BCM_CAN_FRAMES]; - struct can_frame frames[MAX_BCM_CAN_FRAMES]; - }; - }; - /// @brief derivated socketcan class specialized for BCM CAN socket.make_bcm_head class socketcan_bcm_t : public socketcan_t { @@ -40,10 +29,9 @@ namespace utils using socketcan_t::socketcan_t; virtual int open(std::string device_name); - virtual std::shared_ptr read_message(); - virtual void write_message(std::vector>& vobj); - virtual void write_message(std::shared_ptr obj); - void write_message(struct bcm_msg& obj); + virtual std::shared_ptr read_message(); + virtual void write_message(std::vector>& vobj); + virtual void write_message(std::shared_ptr obj); private: int connect(const struct sockaddr* addr, socklen_t len); diff --git a/low-can-binding/utils/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939.cpp index f914ac27..5f977f04 100644 --- a/low-can-binding/utils/socketcan-j1939.cpp +++ b/low-can-binding/utils/socketcan-j1939.cpp @@ -181,12 +181,12 @@ namespace utils } - void socketcan_j1939_t::write_message(std::vector>& vobj) + void socketcan_j1939_t::write_message(std::vector>& vobj) { AFB_WARNING("Not implemented"); } - void socketcan_j1939_t::write_message(std::shared_ptr cm) + void socketcan_j1939_t::write_message(std::shared_ptr cm) { AFB_WARNING("Not implemented"); } diff --git a/low-can-binding/utils/socketcan.hpp b/low-can-binding/utils/socketcan.hpp index 6419175d..fdf8c949 100644 --- a/low-can-binding/utils/socketcan.hpp +++ b/low-can-binding/utils/socketcan.hpp @@ -24,7 +24,7 @@ #include #include "../binding/low-can-hat.hpp" -#include "../can/can-message.hpp" +#include "../can/message/can-message.hpp" #define INVALID_SOCKET -1 @@ -47,9 +47,9 @@ namespace utils virtual int open(std::string device_name) = 0; int setopt(int level, int optname, const void* optval, socklen_t optlen); virtual int close(); - virtual std::shared_ptr read_message() = 0; - virtual void write_message(std::shared_ptr obj) = 0; - virtual void write_message(std::vector>& vobj) = 0; + virtual std::shared_ptr read_message() = 0; + virtual void write_message(std::shared_ptr obj) = 0; + virtual void write_message(std::vector>& vobj) = 0; protected: int socket_; -- cgit 1.2.3-korg