1#include "CANRawSocket.h"
2#include "TempFailure.h"
6#include <linux/can/raw.h>
32 sockaddr_can destAddr;
33 destAddr.can_family = AF_CAN;
36 int sent = TEMP_FAILURE_RETRY (::sendto( m_socket, (
const raw_type*)&frame,
sizeof(frame), 0, (sockaddr*)&destAddr,
sizeof(destAddr)));
39 if( sent !=
sizeof(frame))
45 sockaddr_can clientAddr;
46 socklen_t addrLen =
sizeof(clientAddr);
48 int ret = TEMP_FAILURE_RETRY (::recvfrom( m_socket, (raw_type*)&frame,
sizeof(frame), 0, (sockaddr*)&clientAddr, &addrLen));
52 interface = getInterfaceName(clientAddr);
60 poll.events = POLLIN | POLLPRI | POLLRDHUP;
62 int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
64 if( ret == 0)
return 0;
67 if( poll.revents & POLLRDHUP || poll.revents & POLLHUP)
68 m_peerDisconnected =
true;
70 if( poll.revents & POLLIN || poll.revents & POLLPRI)
73 if( poll.revents & (POLLERR | POLLNVAL))
81 if( ::setsockopt( m_socket, SOL_CAN_RAW, CAN_RAW_FILTER, filter,
sizeof(::can_filter) * len) < 0)
87 if( ::setsockopt( m_socket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &filter,
sizeof(::can_err_mask_t)) < 0)
93 const int value = enable;
94 if( ::setsockopt( m_socket, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &value,
sizeof(value)) < 0)
100 const int value = enable;
101 if( ::setsockopt( m_socket, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, &value,
sizeof(value)) < 0)
void sendTo(const ::can_frame &frame, std::string_view interface)
int receiveFrom(::can_frame &frame, std::string &interface)
void setReceiveFilter(const ::can_filter *filters, size_t len)
void setLocalLoopback(bool enable)
int receive(::can_frame &frame)
(overload) receive can frame from a bound socket
int timedReceiveFrom(::can_frame &frame, std::string &interface, int timeout)
void send(const ::can_frame &frame)
(overload) send can_frame through a connected socket
int timedReceive(::can_frame &frame, int timeout)
(overload) receive can frame from a bound socket with timeout
void setErrorFilter(::can_err_mask_t filter)
void setReceiveOwnMessages(bool enable)
int getInterfaceIndex(std::string_view interface) const
get CAN interface index from interface name
CANSocket(int type, int protocol)
allows a subclass to create new socket
int timedReceive(void *buffer, size_t len, int timeout)
receive data from a bound socket, return after the given timespan
int send(const void *buffer, size_t len)
send data through a connected socket
int receive(void *buffer, size_t len)
receive data from a bound socket
Signals a problem with the execution of a socket call.