diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-04-25 19:56:56 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-04-25 19:56:56 +0200 |
commit | c1dc2bdc685cff74ceef67e9658e1b193b01b67f (patch) | |
tree | 5b0de5e9e67fb648818c304ca13ceb1c6b8acc74 | |
parent | 933dbcf8cb1e82763856f0f822d3c68f5b389d46 (diff) |
Implement reading stream for socketcan_t
Will read a can_message_t object from socket
Change-Id: Ibe3561f9d7bbf4e41b6fd81fa4672bae1c14aca5
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | CAN-binder/low-can-binding/utils/socketcan.cpp | 22 | ||||
-rw-r--r-- | CAN-binder/low-can-binding/utils/socketcan.hpp | 7 |
2 files changed, 27 insertions, 2 deletions
diff --git a/CAN-binder/low-can-binding/utils/socketcan.cpp b/CAN-binder/low-can-binding/utils/socketcan.cpp index 17ce6ce..c462f76 100644 --- a/CAN-binder/low-can-binding/utils/socketcan.cpp +++ b/CAN-binder/low-can-binding/utils/socketcan.cpp @@ -24,6 +24,7 @@ #include "socketcan.hpp" #include "low-can-binding.hpp" +#include "can-message.hpp" namespace utils { @@ -139,6 +140,27 @@ namespace utils return socket_; } + socketcan_t& operator>>(socketcan_t& s, const can_message_t& cm) + { + struct { + struct bcm_msg_head msg_head; + struct canfd_frame frames; + } msg; + + 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); + ifr.ifr_ifindex = addr.can_ifindex; + ioctl(s.socket(), SIOCGIFNAME, &ifr); + + printf("Data available: %i bytes read\n", (int)nbytes); + printf("read: Found on bus %s:\n id: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X\n", ifr.ifr_name, msg.msg_head.can_id, msg.frames.len, + 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]); + return s; + } + socketcan_t& operator<<(socketcan_t& s, const struct bcm_msg_head& obj) { struct sockaddr_can addr = s.get_tx_address(); diff --git a/CAN-binder/low-can-binding/utils/socketcan.hpp b/CAN-binder/low-can-binding/utils/socketcan.hpp index 2c79036..c7cd5c9 100644 --- a/CAN-binder/low-can-binding/utils/socketcan.hpp +++ b/CAN-binder/low-can-binding/utils/socketcan.hpp @@ -17,10 +17,12 @@ * limitations under the License. */ +#include <vector> + #include <sys/socket.h> #include <linux/can/bcm.h> -#include <vector> +#include "../can/can-message.hpp" #define INVALID_SOCKET -1 @@ -71,10 +73,11 @@ namespace utils 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); + + socketcan_t& operator>>(socketcan_t& s, const can_message_t& cm); } |