From 68d8bc019fab3e73654a330baf6b8dd7c11859ca Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 26 Nov 2019 16:18:36 +0100 Subject: Add feature to build messages and fix some functions Allows to build a message (J1939,BCM) with a signal and a value. Bug-AGL: SPEC-2386 Bug-AGL: SPEC-2976 Signed-off-by: Arthur Guyader Change-Id: Iadca13a927ff83f713f39da441c88356695a1285 Signed-off-by: Romain Forlot --- low-can-binding/utils/socketcan-bcm.cpp | 22 ++++++++-------------- low-can-binding/utils/socketcan-bcm.hpp | 5 +---- low-can-binding/utils/socketcan-j1939.cpp | 3 ++- low-can-binding/utils/socketcan-j1939.hpp | 1 + low-can-binding/utils/socketcan-raw.cpp | 16 +++------------- low-can-binding/utils/socketcan-raw.hpp | 6 +----- low-can-binding/utils/socketcan.cpp | 28 ++++++++++++++++++++++++++++ low-can-binding/utils/socketcan.hpp | 17 +++++++++++++++-- 8 files changed, 59 insertions(+), 39 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 0f6bc75c..df41fba2 100644 --- a/low-can-binding/utils/socketcan-bcm.cpp +++ b/low-can-binding/utils/socketcan-bcm.cpp @@ -25,12 +25,6 @@ namespace utils { - /// @brief Connect the socket. - /// @return 0 if success. - int socketcan_bcm_t::connect(const struct sockaddr* addr, socklen_t len) - { - return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0; - } /// @brief Open a raw socket CAN. /// @param[in] device_name is the kernel network device name of the CAN interface. @@ -104,19 +98,19 @@ namespace utils return cm; } - void socketcan_bcm_t::write_message(std::vector>& vobj) + int socketcan_bcm_t::write_message(message_t& m) { - for(const auto& obj : vobj) - write_message(obj); - } - void socketcan_bcm_t::write_message(std::shared_ptr m) - { - struct bcm_msg obj = m->get_bcm_msg(); + can_message_t& cm = reinterpret_cast(m); + struct bcm_msg obj = cm.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); 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)); + { + AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno)); + return -1; + } + return 0; } } diff --git a/low-can-binding/utils/socketcan-bcm.hpp b/low-can-binding/utils/socketcan-bcm.hpp index 5e86aa4f..1f5ec50c 100644 --- a/low-can-binding/utils/socketcan-bcm.hpp +++ b/low-can-binding/utils/socketcan-bcm.hpp @@ -30,11 +30,8 @@ namespace utils 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); + virtual int write_message(message_t& 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 5f977f04..a7938d61 100644 --- a/low-can-binding/utils/socketcan-j1939.cpp +++ b/low-can-binding/utils/socketcan-j1939.cpp @@ -79,7 +79,8 @@ namespace utils } ++filter_on; } - if(filter_on){ + if(filter_on) + { setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter)); } } diff --git a/low-can-binding/utils/socketcan-j1939.hpp b/low-can-binding/utils/socketcan-j1939.hpp index d3b01fc4..2b370179 100644 --- a/low-can-binding/utils/socketcan-j1939.hpp +++ b/low-can-binding/utils/socketcan-j1939.hpp @@ -34,6 +34,7 @@ namespace utils virtual std::shared_ptr read_message(); virtual void write_message(std::shared_ptr obj); virtual void write_message(std::vector>& vobj); + virtual int write_message(message_t& obj); private: int connect(const struct sockaddr* addr, socklen_t len); diff --git a/low-can-binding/utils/socketcan-raw.cpp b/low-can-binding/utils/socketcan-raw.cpp index 6bd41c3b..31387c72 100644 --- a/low-can-binding/utils/socketcan-raw.cpp +++ b/low-can-binding/utils/socketcan-raw.cpp @@ -22,12 +22,6 @@ namespace utils { - /// @brief Bind the socket. - /// @return 0 if success. - int socketcan_raw_t::bind(const struct sockaddr* addr, socklen_t len) - { - return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0; - } /// @brief Open a raw socket CAN. /// @param[in] device_name is the kernel network device name of the CAN interface. @@ -75,7 +69,7 @@ namespace utils ifr.ifr_ifindex = addr.can_ifindex; ioctl(socket(), SIOCGIFNAME, &ifr); - AFB_DEBUG("Data available: %i bytes read", (int)nbytes); + //AFB_DEBUG("Data available: %i bytes read", (int)nbytes); AFB_DEBUG("read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", ifr.ifr_name, frame.can_id, frame.len, frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]); @@ -87,13 +81,9 @@ namespace utils return cm; } - void socketcan_raw_t::write_message(std::vector>& vobj) - { - AFB_WARNING("Not implemented"); - } - - void socketcan_raw_t::write_message(std::shared_ptr cm) + int socketcan_raw_t::write_message(message_t& m) { AFB_WARNING("Not implemented"); + return -1; } } diff --git a/low-can-binding/utils/socketcan-raw.hpp b/low-can-binding/utils/socketcan-raw.hpp index 42e98746..966bf351 100644 --- a/low-can-binding/utils/socketcan-raw.hpp +++ b/low-can-binding/utils/socketcan-raw.hpp @@ -27,11 +27,7 @@ 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::shared_ptr cm); - virtual void write_message(std::vector>& vobj); - - private: - int bind(const struct sockaddr* addr, socklen_t len); + virtual int write_message(message_t& cm); }; } diff --git a/low-can-binding/utils/socketcan.cpp b/low-can-binding/utils/socketcan.cpp index b943d1d7..f542f4e8 100644 --- a/low-can-binding/utils/socketcan.cpp +++ b/low-can-binding/utils/socketcan.cpp @@ -96,4 +96,32 @@ namespace utils { return socket_; } + + /// @brief Connect the socket. + /// @return 0 if success. + int socketcan_t::connect(const struct sockaddr* addr, socklen_t len) + { + return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0; + } + + /// @brief Bind the socket. + /// @return 0 if success. + int socketcan_t::bind(const struct sockaddr* addr, socklen_t len) + { + return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0; + } + + int socketcan_t::write_message(std::vector& vobj) + { + for(int i=0;i read_message() = 0; - virtual void write_message(std::shared_ptr obj) = 0; - virtual void write_message(std::vector>& vobj) = 0; + virtual int write_message(message_t& obj) = 0; + virtual int write_message(std::vector& vobj); protected: int socket_; struct sockaddr_can tx_address_; int open(int domain, int type, int protocol); + int bind(const struct sockaddr* addr, socklen_t len); + int connect(const struct sockaddr* addr, socklen_t len); }; -- cgit 1.2.3-korg