Class SocketCanReceiver

Nested Relationships

Nested Types

Class Documentation

class SocketCanReceiver

Simple RAII wrapper around a raw CAN receiver.

Public Functions

explicit SocketCanReceiver(const std::string &interface = "can0", const bool enable_fd = false)

Constructor.

~SocketCanReceiver() noexcept

Destructor.

void SetCanFilters(const CanFilterList &filters)

Set SocketCAN filters

Parameters:

filters[in] List of filters to be applied.

Throws:

std::runtime_error – If filters couldn’t be applied

CanId receive(void *const data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const

Receive CAN data

Parameters:
  • data[out] A buffer to be written with data bytes. Must be at least 8 bytes in size

  • timeout[in] Maximum duration to wait for data on the file descriptor. Negative durations are treated the same as zero timeout

Throws:
Returns:

The CanId for the received can_frame, with length appropriately populated

template<typename T, typename = std::enable_if_t<!std::is_pointer<T>::value>>
inline CanId receive(T &data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const

Receive typed CAN data. Slightly less efficient than untyped interface; has extra copy and branches

Template Parameters:

Type – of data to receive, must be 8 bytes or smaller

Parameters:
  • data[out] A buffer to be written with data bytes. Must be at least 8 bytes in size

  • timeout[in] Maximum duration to wait for data on the file descriptor. Negative durations are treated the same as zero timeout

Throws:
  • SocketCanTimeout – On timeout

  • std::runtime_error – If received data would not fit into provided type

  • std::runtime_error – on other errors

Returns:

The CanId for the received can_frame, with length appropriately populated

CanId receive_fd(void *const data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const

Receive CAN FD data

Parameters:
  • data[out] A buffer to be written with data bytes. Must be at least 64 bytes in size

  • timeout[in] Maximum duration to wait for data on the file descriptor. Negative durations are treated the same as zero timeout

Throws:
Returns:

The CanId for the received canfd_frame, with length appropriately populated

template<typename T, typename = std::enable_if_t<!std::is_pointer<T>::value>>
inline CanId receive_fd(T &data, const std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()) const

Receive typed CAN FD data. Slightly less efficient than untyped interface; has extra copy and branches

Template Parameters:

Type – of data to receive, must be 64 bytes or smaller

Parameters:
  • data[out] A buffer to be written with data bytes. Must be at least 64 bytes in size

  • timeout[in] Maximum duration to wait for data on the file descriptor. Negative durations are treated the same as zero timeout

Throws:
  • SocketCanTimeout – On timeout

  • std::runtime_error – If received data would not fit into provided type

  • std::runtime_error – on other errors

Returns:

The CanId for the received canfd_frame, with length appropriately populated

struct CanFilterList

Structure containing possible CAN filter options.

Public Functions

CanFilterList() = default

Default constructor.

explicit CanFilterList(const char *str)

Parse CAN filters string:

Filters:

Comma separated filters can be specified for each given CAN interface.

<can_id>:<can_mask>

(matches when <received_can_id> & mask == can_id & mask)

<can_id>~<can_mask>

(matches when <received_can_id> & mask != can_id & mask)

#<error_mask>

(set error frame filter, see include/linux/can/error.h)

[j|J]

(join the given CAN filters - logical AND semantic)

CAN IDs, masks and data content are given and expected in hexadecimal values. When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.

Parameters:

str[in] Input to be parsed.

Throws:

std::runtime_error – if string couldn’t be parsed.

Returns:

Populated CanFilterList structure.

explicit CanFilterList(const std::string &str)

Parse CAN filters string:

Filters:

Comma separated filters can be specified for each given CAN interface.

<can_id>:<can_mask>

(matches when <received_can_id> & mask == can_id & mask)

<can_id>~<can_mask>

(matches when <received_can_id> & mask != can_id & mask)

#<error_mask>

(set error frame filter, see include/linux/can/error.h)

[j|J]

(join the given CAN filters - logical AND semantic)

CAN IDs, masks and data content are given and expected in hexadecimal values. When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.

Parameters:

str[in] Input to be parsed.

Throws:

std::runtime_error – if string couldn’t be parsed.

Returns:

Populated CanFilterList structure.

Public Members

std::vector<struct can_filter> filters
can_err_mask_t error_mask = 0
bool join_filters = false

Public Static Functions

static CanFilterList ParseFilters(const std::string &str)

Parse CAN filters string:

Filters:

Comma separated filters can be specified for each given CAN interface.

<can_id>:<can_mask>

(matches when <received_can_id> & mask == can_id & mask)

<can_id>~<can_mask>

(matches when <received_can_id> & mask != can_id & mask)

#<error_mask>

(set error frame filter, see include/linux/can/error.h)

[j|J]

(join the given CAN filters - logical AND semantic)

CAN IDs, masks and data content are given and expected in hexadecimal values. When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.

Parameters:

str[in] Input to be parsed.

Throws:

std::runtime_error – if string couldn’t be parsed.

Returns:

Populated CanFilterList structure.