diff options
Diffstat (limited to 'CAN-binder/low-can-binding/utils')
-rw-r--r-- | CAN-binder/low-can-binding/utils/socketcan.cpp (renamed from CAN-binder/low-can-binding/utils/socket.cpp) | 56 | ||||
-rw-r--r-- | CAN-binder/low-can-binding/utils/socketcan.hpp (renamed from CAN-binder/low-can-binding/utils/socket.hpp) | 35 |
2 files changed, 76 insertions, 15 deletions
diff --git a/CAN-binder/low-can-binding/utils/socket.cpp b/CAN-binder/low-can-binding/utils/socketcan.cpp index 9decaa8e..17ce6ce7 100644 --- a/CAN-binder/low-can-binding/utils/socket.cpp +++ b/CAN-binder/low-can-binding/utils/socketcan.cpp @@ -22,11 +22,12 @@ #include <net/if.h> #include <sys/ioctl.h> -#include "socket.hpp" +#include "socketcan.hpp" #include "low-can-binding.hpp" namespace utils { + /// @brief Construct a default, invalid, socket. socketcan_t::socketcan_t() : socket_{INVALID_SOCKET} @@ -46,6 +47,11 @@ namespace utils ::close(socket_); } + const struct sockaddr_can& socketcan_t::get_tx_address() const + { + return tx_address_; + } + /// @brief Test if socket is valid. /// @return true if valid, false otherwise. socketcan_t::operator bool() const @@ -121,10 +127,10 @@ namespace utils } else { - txAddress_.can_family = AF_CAN; - txAddress_.can_ifindex = ifr.ifr_ifindex; + tx_address_.can_family = AF_CAN; + tx_address_.can_ifindex = ifr.ifr_ifindex; - if(connect((struct sockaddr *)&txAddress_, sizeof(txAddress_)) < 0) + if(connect((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0) { ERROR(binder_interface, "%s: Connect failed. %s", __FUNCTION__, strerror(errno)); close(); @@ -133,14 +139,38 @@ namespace utils return socket_; } - /// @brief Send a CAN message through the socket. - /// - /// @param[in] f - the CAN FD frame to send - /// - /// @return number of sent bytes if message sent, 0 on invalid socket and -1 if something wrong. - ssize_t socketcan_t::send(const struct canfd_frame& f) + socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj) + { + struct sockaddr_can addr = s.get_tx_address(); + ::sendto(s.socket(), &obj, sizeof(bcm_msg_head), 0, (struct sockaddr*)&addr, sizeof(addr)); + return s; + } + + socketcan_t& operator<<(socketcan_t& s, const struct canfd_frame& obj) + { + struct sockaddr_can addr = s.get_tx_address(); + ::sendto(s.socket(), &obj, sizeof(canfd_frame), 0, (struct sockaddr*)&addr, sizeof(addr)); + return s; + } + + socketcan_t& operator<<(socketcan_t& s, const struct can_frame& obj) + { + struct sockaddr_can addr = s.get_tx_address(); + ::sendto(s.socket(), &obj, sizeof(can_frame), 0, (struct sockaddr*)&addr, sizeof(addr)); + return s; + } + + socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<struct can_frame>& obj) + { + s << obj.msg_head; + s << obj.frames; + return s; + } + + socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj) { - return socket_ != INVALID_SOCKET ? ::sendto(socket_, &f, sizeof(struct canfd_frame), 0, - (struct sockaddr*)&txAddress_, sizeof(txAddress_)) : 0; + s << obj.msg_head; + s << obj.frames; + return s; } -} +}
\ No newline at end of file diff --git a/CAN-binder/low-can-binding/utils/socket.hpp b/CAN-binder/low-can-binding/utils/socketcan.hpp index b981ad3e..2c790364 100644 --- a/CAN-binder/low-can-binding/utils/socket.hpp +++ b/CAN-binder/low-can-binding/utils/socketcan.hpp @@ -18,11 +18,27 @@ */ #include <sys/socket.h> +#include <linux/can/bcm.h> + +#include <vector> #define INVALID_SOCKET -1 namespace utils { + + template <typename T> + struct basic_bcm_msg + { + bcm_msg_head msg_head; + std::vector<T> frames; + }; + + struct canfd_bcm_msg : public basic_bcm_msg<canfd_frame> + { + canfd_bcm_msg() { msg_head.flags |= CAN_FD_FRAME; } + }; + class socketcan_t { public: @@ -31,19 +47,34 @@ namespace utils socketcan_t(socketcan_t&&); ~socketcan_t(); + const struct sockaddr_can& get_tx_address() const; explicit operator bool() const; int socket() const; int open(std::string device_name); int setopt(int level, int optname, const void* optval, socklen_t optlen); - ssize_t send(const struct canfd_frame& f); int close(); + private: int socket_; - struct sockaddr_can txAddress_; /// < internal member using to bind to the 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); }; + + template <typename T> + socketcan_t& operator<<(socketcan_t& s, const std::vector<T>& vobj) + { + for(const auto& obj : vobj) + s << obj; + return s; + } + + socketcan_t& operator<<(socketcan_t& s, const canfd_frame& frame); + socketcan_t& operator<<(socketcan_t& s, const can_frame& frame); + socketcan_t& operator<<(socketcan_t& s, const struct basic_bcm_msg<can_frame>& obj); + socketcan_t& operator<<(socketcan_t& s, const struct canfd_bcm_msg& obj); + socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj); } |