34#ifndef _TRACTIONMODEM_TRACTIONMODEMDEFS_HXX_
35#define _TRACTIONMODEM_TRACTIONMODEMDEFS_HXX_
48 using Payload = std::string;
51 static constexpr uint32_t
PREAMBLE = 0x41d2c37a;
237 uint8_t space, uint32_t address, uint8_t count)
255 uint8_t space, uint32_t address,
const std::string& data)
257 return get_memw_payload(space, address, (
const uint8_t*)data.data(), data.size());
267 uint8_t space, uint32_t address,
const uint8_t* buf,
size_t count)
273 p.append((
char*)buf, count);
284 p->push_back(v >> 24);
285 p->push_back((v >> 16) & 0xff);
286 p->push_back((v >> 8) & 0xff);
287 p->push_back((v)&0xff);
295 p->push_back((v >> 8) & 0xff);
296 p->push_back((v)&0xff);
304 p->push_back((v)&0xff);
326 p->size() -
sizeof(uint32_t), crc.crc);
339 if ((ofs + 4) <= p.size())
341 memcpy(&ret, p.data() + ofs, 4);
354 if ((ofs + 2) <= p.size())
356 memcpy(&ret, p.data() + ofs, 2);
static void crc3_crc16_ccitt(const void *data, size_t length_bytes, uint16_t checksum[3])
Computes the triple-CRC value over a chunk of data.
This class provides a mechanism for working with velocity in different forms.
uint8_t get_dcc_128()
Get the speed in DCC 128 speed step format.
bool operator!=(const CRC &c)
Overload != operator.
bool operator==(const CRC &c)
Overload == operator.
uint16_t even_
CRC of even bytes.
uint16_t odd_
CRC of odd bytes.
uint16_t all_
CRC of all bytes.
The definition of a message.
uint8_t data[0]
start of the message data
Header header_
packet command
Useful definitions for the traction modem.
static const CRC get_crc(const Payload &p, uint16_t length)
Extract the CRC value(s) from a given payload.
static void append_uint32(Payload *p, uint32_t v)
Appends an uint32_t in network byte order to the payload.
static bool is_valid(const Payload &p)
Verifies that a given payload is a valid packet.
static uint16_t get_uint16(const Payload &p, unsigned ofs)
Extract uint16_t value from a given offset in a wire formatted payload.
static constexpr unsigned MAX_LEN
Maximum allowed len value.
static constexpr unsigned LEN_HEADER
Length of a the header. 4 bytes preamble, 2 bytes cmd, 2 bytes length.
static constexpr unsigned LEN_MEM_R
Length of the data payload of a set estop packet.
@ RESP_ESTOP_SET
emergency stop response
@ CMD_ESTOP_SET
emergency stop request
@ CMD_SPEED_QUERY
query current speed
@ CMD_NOP
no-operation (do nothing)
@ CMD_FN_QUERY
query function status
@ CMD_WIRELESS_PRESENT
wireless present
@ RESP_MEM_R
memory read response
@ RESP_SPEED_QUERY
query current speed response
@ CMD_REBOOT
reboot request
@ RESP_SPEED_SET
set velocity response
@ RESP_WIRELESS_PRESENT
wireless present response
@ CMD_BAUD_RATE_QUERY
query the supported baud rates
@ RESP_FN_SET
set function response
@ RESP_BAUD_RATE_QUERY
baud rate query response
@ RESP_DC_DCC_PRESENT
DC/DCC present response.
@ RESP_MEM_W
memory write response
@ CMD_SPEED_SET
set velocity
@ CMD_BAUD_RATE_REQUEST
request a specific baud rate
@ RESP_FN_QUERY
query function status response
@ CMD_DC_DCC_PRESENT
DC/DCC present.
static constexpr unsigned OFS_LEN
Offset of the length in the packet.
static Payload get_speed_set_payload(openlcb::Velocity v)
Computes payload to set speed and direction.
static Payload get_memw_payload(uint8_t space, uint32_t address, const uint8_t *buf, size_t count)
Computes payload to write some data.
static Payload get_memr_payload(uint8_t space, uint32_t address, uint8_t count)
Computes payload to read some data.
static constexpr uint32_t PREAMBLE
Every command starts with these bytes.
static constexpr unsigned OFS_CMD
Offset of the command in the packet.
static Payload get_wireless_present_payload(bool is_present)
Computes payload for the wireless present message.
static constexpr unsigned LEN_ESTOP_SET
Length of the data payload of a set estop packet.
static constexpr unsigned LEN_BASE
Length of a zero-payload packet.
static constexpr unsigned LEN_WIRELESS_PRESENT
Length of the data payload of a wireless present packet.
static void append_crc(Payload *p)
Computes and appends the CRC to the payload.
static Payload get_fn_set_payload(unsigned fn, uint16_t value)
Computes payload to set a function.
static constexpr unsigned LEN_SPEED_SET
Length of the data payload of a set speed packet.
static void prepare(Payload *p, Command cmd, uint16_t len)
Prepares the header of a packet.
static constexpr unsigned OFS_DATA
Offset of the first data byte in the packet.
static constexpr unsigned LEN_FN_SET
Length of the data payload of a set function packet.
static uint32_t get_uint32(const Payload &p, unsigned ofs)
Extract uint32_t value from a given offset in a wire formatted payload.
static Payload get_estop_payload()
Computes payload to set estop.
static constexpr uint16_t RESPONSE
The most significant bit of each command means "response" to a previous message.
static constexpr unsigned LEN_MEM_W
Base length of the data, add the number of payload bytes.
static constexpr char PREAMBLE_FIRST
First byte of the preamble.
static void append_uint8(Payload *p, uint8_t v)
Appends an uint8_t to the payload.
static Payload get_memw_payload(uint8_t space, uint32_t address, const std::string &data)
Computes payload to write some data.
static void append_uint16(Payload *p, uint16_t v)
Appends an uint16_t in network byte order to the payload.