Class Packet

Class Documentation

class Packet

A CAN packet containing an address and data.

Public Functions

Packet() = default

Default constructor - zero address, no data.

Packet(const addressing::flagged_address_t &address, const uint8_t data[] = nullptr, size_t data_len = 0)

Construct a packet from an address and data array.

Parameters:
const addressing::flagged_address_t &address

The address of the packet

const uint8_t data[] = nullptr

The packet data

size_t data_len = 0

The length of the data array

Packet(const addressing::flagged_address_t &address, const std::vector<uint8_t> &data)

Construct a packet from an address and data vector.

Parameters:
const addressing::flagged_address_t &address

The address of the packet

const std::vector<uint8_t> &data

The data vector

template<typename T>
inline Packet(const addressing::flagged_address_t &address, const T &data)

Construct a packet from an address and data.

Template Parameters:
typename T

The type of the data

Parameters:
const addressing::flagged_address_t &address

The address of the packet

const T &data

The data to copy into the packet

template<typename T>
inline std::optional<T> get_data() const

Copies a packet’s data into a buffer.

Template Parameters:
typename T

The type of the buffer to copy it into

Returns:

The buffer with the data copied into it, or std::nullopt if the buffer does not match the packet size

inline const auto &get_data() const

Get the packet data.

Returns:

The packet data

void set_data(const uint8_t data[], size_t data_len)

Set the packet data.

Parameters:
const uint8_t data[]

The data to copy into the packet

size_t data_len

The length of the data array

Throws:

std::invalid_argument – If the data is too large to fit in the packet

void set_data(const std::vector<uint8_t> &data)

Set the packet data.

Parameters:
const std::vector<uint8_t> &data

The data to copy into the packet

Throws:

std::invalid_argument – If the data is too large to fit in the packet

template<typename T>
inline void set_data(const T &data)

Set the packet data.

Template Parameters:
typename T

The type of the data

Parameters:
const T &data

The data to copy into the packet

Throws:

std::invalid_argument – If the data is too large to fit in the packet

inline auto get_data_len() const

Get the length of the data in the packet.

Returns:

The data length

inline auto get_address() const

Get the packet address.

Returns:

The packet address

void set_address(const addressing::flagged_address_t &address)

Set the packet address.

Parameters:
const addressing::flagged_address_t &address

The address to set

inline bool get_is_rtr() const

Get whether the packet address is RTR.

Returns:

Whether the address is RTR

inline void set_is_rtr(const bool &is_rtr)

Set whether the packet address is RTR.

Parameters:
const bool &is_rtr

Whether the address is RTR

inline bool get_is_error() const

Get whether the packet address is an error.

Returns:

Whether the address is an error

inline void set_is_error(const bool &is_error)

Set whether the packet address is an error.

Parameters:
const bool &is_error

Whether the address is an error

inline bool get_is_extended() const

Get whether the packet address is extended.

Returns:

Whether the address is extended

inline void set_is_extended(const bool &is_extended)

Set whether the packet address is extended.

Parameters:
const bool &is_extended

Whether the address is extended

inline auto operator<=>(const Packet &other) const

Compare two packets (for sorting). Sorts only by address.

Parameters:
const Packet &other

Packet to compare against

inline auto operator==(const Packet &other) const

Check if two packets are equal. Checks address and data.

Parameters:
const Packet &other

Packet to compare against

Returns:

Whether the packets are equal

inline auto operator!=(const Packet &other) const

Check if two packets are not equal.

Parameters:
const Packet &other

Packet to compare against

Returns:

Whether the packets differ