Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
traction_modem::Defs Struct Reference

Useful definitions for the traction modem. More...

#include <TractionModemDefs.hxx>

Classes

struct  CRC
 
struct  Header
 The header of a message. More...
 
struct  Message
 The definition of a message. More...
 

Public Types

enum  Command : uint16_t {
  CMD_PING = 0x0000 , CMD_NOP = 0x0001 , CMD_REBOOT = 0x0002 , CMD_BAUD_RATE_QUERY = 0x0003 ,
  CMD_BAUD_RATE_REQUEST = 0x0004 , CMD_SPEED_SET = 0x0100 , CMD_FN_SET = 0x0101 , CMD_ESTOP_SET = 0x0102 ,
  CMD_SPEED_QUERY = 0x0110 , CMD_FN_QUERY = 0x0111 , CMD_DC_DCC_PRESENT = 0x0200 , CMD_WIRELESS_PRESENT = 0x0201 ,
  CMD_MEM_R = 0x1000 , CMD_MEM_W = 0x1001 , RESP_PING = RESPONSE | CMD_PING , RESP_BAUD_RATE_QUERY = RESPONSE | CMD_BAUD_RATE_QUERY ,
  RESP_SPEED_SET = RESPONSE | CMD_SPEED_SET , RESP_FN_SET = RESPONSE | CMD_FN_SET , RESP_ESTOP_SET = RESPONSE | CMD_ESTOP_SET , RESP_SPEED_QUERY = RESPONSE | CMD_SPEED_QUERY ,
  RESP_FN_QUERY = RESPONSE | CMD_FN_QUERY , RESP_DC_DCC_PRESENT = RESPONSE | CMD_DC_DCC_PRESENT , RESP_WIRELESS_PRESENT = RESPONSE | CMD_WIRELESS_PRESENT , RESP_MEM_R = RESPONSE | CMD_MEM_R ,
  RESP_MEM_W = RESPONSE | CMD_MEM_W
}
 Command values. More...
 
using Payload = std::string
 

Static Public Member Functions

static Payload get_wireless_present_payload (bool is_present)
 Computes payload for the wireless present message.
 
static Payload get_fn_set_payload (unsigned fn, uint16_t value)
 Computes payload to set a function.
 
static Payload get_speed_set_payload (openlcb::Velocity v)
 Computes payload to set speed and direction.
 
static Payload get_estop_payload ()
 Computes payload to set estop.
 
static Payload get_memr_payload (uint8_t space, uint32_t address, uint8_t count)
 Computes payload to read some data.
 
static Payload get_memw_payload (uint8_t space, uint32_t address, const std::string &data)
 Computes payload to write some data.
 
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 void append_uint32 (Payload *p, uint32_t v)
 Appends an uint32_t in network byte order to the payload.
 
static void append_uint16 (Payload *p, uint16_t v)
 Appends an uint16_t in network byte order to the payload.
 
static void append_uint8 (Payload *p, uint8_t v)
 Appends an uint8_t to the payload.
 
static void prepare (Payload *p, Command cmd, uint16_t len)
 Prepares the header of a packet.
 
static void append_crc (Payload *p)
 Computes and appends the CRC to the payload.
 
static uint32_t get_uint32 (const Payload &p, unsigned ofs)
 Extract uint32_t value from a given offset in a wire formatted payload.
 
static uint16_t get_uint16 (const Payload &p, unsigned ofs)
 Extract uint16_t value from a given offset in a wire formatted payload.
 
static const CRC get_crc (const Payload &p, uint16_t length)
 Extract the CRC value(s) from a given payload.
 
static bool is_valid (const Payload &p)
 Verifies that a given payload is a valid packet.
 

Static Public Attributes

static constexpr uint32_t PREAMBLE = 0x41d2c37a
 Every command starts with these bytes.
 
static constexpr char PREAMBLE_FIRST = 0x41
 First byte of the preamble.
 
static constexpr uint16_t RESPONSE = 0x8000
 The most significant bit of each command means "response" to a previous message.
 
static constexpr unsigned LEN_HEADER = 4+2+2
 Length of a the header. 4 bytes preamble, 2 bytes cmd, 2 bytes length.
 
static constexpr unsigned LEN_BASE = 14
 Length of a zero-payload packet.
 
static constexpr unsigned LEN_FN_SET = 6
 Length of the data payload of a set function packet.
 
static constexpr unsigned LEN_SPEED_SET = 1
 Length of the data payload of a set speed packet.
 
static constexpr unsigned LEN_ESTOP_SET = 0
 Length of the data payload of a set estop packet.
 
static constexpr unsigned LEN_WIRELESS_PRESENT = 1
 Length of the data payload of a wireless present packet.
 
static constexpr unsigned LEN_MEM_R = 6
 Length of the data payload of a set estop packet.
 
static constexpr unsigned LEN_MEM_W = 5
 Base length of the data, add the number of payload bytes.
 
static constexpr unsigned MAX_LEN = 512
 Maximum allowed len value.
 
static constexpr unsigned OFS_CMD = 4
 Offset of the command in the packet.
 
static constexpr unsigned OFS_LEN = 6
 Offset of the length in the packet.
 
static constexpr unsigned OFS_DATA = 8
 Offset of the first data byte in the packet.
 

Detailed Description

Useful definitions for the traction modem.

Definition at line 46 of file TractionModemDefs.hxx.

Member Typedef Documentation

◆ Payload

using traction_modem::Defs::Payload = std::string

Definition at line 48 of file TractionModemDefs.hxx.

Member Enumeration Documentation

◆ Command

Command values.

Enumerator
CMD_PING 

ping

CMD_NOP 

no-operation (do nothing)

CMD_REBOOT 

reboot request

CMD_BAUD_RATE_QUERY 

query the supported baud rates

CMD_BAUD_RATE_REQUEST 

request a specific baud rate

CMD_SPEED_SET 

set velocity

CMD_FN_SET 

set function

CMD_ESTOP_SET 

emergency stop request

CMD_SPEED_QUERY 

query current speed

CMD_FN_QUERY 

query function status

CMD_DC_DCC_PRESENT 

DC/DCC present.

CMD_WIRELESS_PRESENT 

wireless present

CMD_MEM_R 

memory read

CMD_MEM_W 

memory write

RESP_PING 

ping response

RESP_BAUD_RATE_QUERY 

baud rate query response

RESP_SPEED_SET 

set velocity response

RESP_FN_SET 

set function response

RESP_ESTOP_SET 

emergency stop response

RESP_SPEED_QUERY 

query current speed response

RESP_FN_QUERY 

query function status response

RESP_DC_DCC_PRESENT 

DC/DCC present response.

RESP_WIRELESS_PRESENT 

wireless present response

RESP_MEM_R 

memory read response

RESP_MEM_W 

memory write response

Definition at line 60 of file TractionModemDefs.hxx.

Member Function Documentation

◆ append_crc()

static void traction_modem::Defs::append_crc ( Payload *  p)
inlinestatic

Computes and appends the CRC to the payload.

Parameters
pwire formatted payload, including prepended preamble, to append to

Definition at line 322 of file TractionModemDefs.hxx.

◆ append_uint16()

static void traction_modem::Defs::append_uint16 ( Payload *  p,
uint16_t  v 
)
inlinestatic

Appends an uint16_t in network byte order to the payload.

Parameters
pwire formatted payload to append to
vvalue to append

Definition at line 293 of file TractionModemDefs.hxx.

◆ append_uint32()

static void traction_modem::Defs::append_uint32 ( Payload *  p,
uint32_t  v 
)
inlinestatic

Appends an uint32_t in network byte order to the payload.

Parameters
pwire formatted payload to append to
vvalue to append

Definition at line 282 of file TractionModemDefs.hxx.

◆ append_uint8()

static void traction_modem::Defs::append_uint8 ( Payload *  p,
uint8_t  v 
)
inlinestatic

Appends an uint8_t to the payload.

Parameters
pwire formatted payload to append to
vvalue to append

Definition at line 302 of file TractionModemDefs.hxx.

◆ get_crc()

static const CRC traction_modem::Defs::get_crc ( const Payload &  p,
uint16_t  length 
)
inlinestatic

Extract the CRC value(s) from a given payload.

The assumption is that p contains a valid (correctly formatted) message. Additional partial or complete messages contained within p are ignored.

Parameters
pwire formatted payload
lengthlength field from the Header in host endianess (length in bytes of the message data)

Definition at line 368 of file TractionModemDefs.hxx.

◆ get_estop_payload()

static Payload traction_modem::Defs::get_estop_payload ( )
inlinestatic

Computes payload to set estop.

Returns
wire formatted payload

Definition at line 222 of file TractionModemDefs.hxx.

◆ get_fn_set_payload()

static Payload traction_modem::Defs::get_fn_set_payload ( unsigned  fn,
uint16_t  value 
)
inlinestatic

Computes payload to set a function.

Parameters
fnfunction number
valuefunction value. For binary functions, 0 is off, 1 is on.
Returns
wire formatted payload

Definition at line 196 of file TractionModemDefs.hxx.

◆ get_memr_payload()

static Payload traction_modem::Defs::get_memr_payload ( uint8_t  space,
uint32_t  address,
uint8_t  count 
)
inlinestatic

Computes payload to read some data.

Parameters
spaceaddress space
addressaddress offset within address space
countsize of read data requested in bytes
Returns
wire formatted payload

Definition at line 236 of file TractionModemDefs.hxx.

◆ get_memw_payload() [1/2]

static Payload traction_modem::Defs::get_memw_payload ( uint8_t  space,
uint32_t  address,
const std::string &  data 
)
inlinestatic

Computes payload to write some data.

Parameters
spaceaddress space
addressaddress offset within address space
datadata to write
Returns
wire formatted payload

Definition at line 254 of file TractionModemDefs.hxx.

◆ get_memw_payload() [2/2]

static Payload traction_modem::Defs::get_memw_payload ( uint8_t  space,
uint32_t  address,
const uint8_t *  buf,
size_t  count 
)
inlinestatic

Computes payload to write some data.

Parameters
spaceaddress space
addressaddress offset within address space
bufdata to write
countsize of data to write in bytes
Returns
wire formatted payload

Definition at line 266 of file TractionModemDefs.hxx.

◆ get_speed_set_payload()

static Payload traction_modem::Defs::get_speed_set_payload ( openlcb::Velocity  v)
inlinestatic

Computes payload to set speed and direction.

Parameters
vspeed and direction data.
Returns
wire formatted payload

Definition at line 210 of file TractionModemDefs.hxx.

◆ get_uint16()

static uint16_t traction_modem::Defs::get_uint16 ( const Payload &  p,
unsigned  ofs 
)
inlinestatic

Extract uint16_t value from a given offset in a wire formatted payload.

Parameters
pwire formatted payload
ofsbyte offset to start from for extraction
Returns
extracted value

Definition at line 351 of file TractionModemDefs.hxx.

◆ get_uint32()

static uint32_t traction_modem::Defs::get_uint32 ( const Payload &  p,
unsigned  ofs 
)
inlinestatic

Extract uint32_t value from a given offset in a wire formatted payload.

Parameters
pwire formatted payload
ofsbyte offset to start from for extraction
Returns
extracted value

Definition at line 336 of file TractionModemDefs.hxx.

◆ get_wireless_present_payload()

static Payload traction_modem::Defs::get_wireless_present_payload ( bool  is_present)
inlinestatic

Computes payload for the wireless present message.

Parameters
is_presenttrue if "present", else false
Returns
wire formatted payload

Definition at line 182 of file TractionModemDefs.hxx.

◆ is_valid()

static bool traction_modem::Defs::is_valid ( const Payload &  p)
inlinestatic

Verifies that a given payload is a valid packet.

This checks for the preamble and the length matching what is needed for the packet size. Does not check the CRC.

Parameters
pwire formatted payload
Returns
true if valid, else false.

Definition at line 382 of file TractionModemDefs.hxx.

◆ prepare()

static void traction_modem::Defs::prepare ( Payload *  p,
Command  cmd,
uint16_t  len 
)
inlinestatic

Prepares the header of a packet.

Parameters
ppacket buffer.
cmdcommand to add to header.
lenlength of data payload that will come after the header.

Definition at line 311 of file TractionModemDefs.hxx.

Member Data Documentation

◆ LEN_BASE

constexpr unsigned traction_modem::Defs::LEN_BASE = 14
staticconstexpr

Length of a zero-payload packet.

4 bytes preamble, 2 bytes cmd, 2 bytes length, 6 bytes CRC.

Definition at line 109 of file TractionModemDefs.hxx.

◆ LEN_ESTOP_SET

constexpr unsigned traction_modem::Defs::LEN_ESTOP_SET = 0
staticconstexpr

Length of the data payload of a set estop packet.

Definition at line 115 of file TractionModemDefs.hxx.

◆ LEN_FN_SET

constexpr unsigned traction_modem::Defs::LEN_FN_SET = 6
staticconstexpr

Length of the data payload of a set function packet.

Definition at line 111 of file TractionModemDefs.hxx.

◆ LEN_HEADER

constexpr unsigned traction_modem::Defs::LEN_HEADER = 4+2+2
staticconstexpr

Length of a the header. 4 bytes preamble, 2 bytes cmd, 2 bytes length.

Definition at line 105 of file TractionModemDefs.hxx.

◆ LEN_MEM_R

constexpr unsigned traction_modem::Defs::LEN_MEM_R = 6
staticconstexpr

Length of the data payload of a set estop packet.

Definition at line 119 of file TractionModemDefs.hxx.

◆ LEN_MEM_W

constexpr unsigned traction_modem::Defs::LEN_MEM_W = 5
staticconstexpr

Base length of the data, add the number of payload bytes.

Definition at line 121 of file TractionModemDefs.hxx.

◆ LEN_SPEED_SET

constexpr unsigned traction_modem::Defs::LEN_SPEED_SET = 1
staticconstexpr

Length of the data payload of a set speed packet.

Definition at line 113 of file TractionModemDefs.hxx.

◆ LEN_WIRELESS_PRESENT

constexpr unsigned traction_modem::Defs::LEN_WIRELESS_PRESENT = 1
staticconstexpr

Length of the data payload of a wireless present packet.

Definition at line 117 of file TractionModemDefs.hxx.

◆ MAX_LEN

constexpr unsigned traction_modem::Defs::MAX_LEN = 512
staticconstexpr

Maximum allowed len value.

Definition at line 124 of file TractionModemDefs.hxx.

◆ OFS_CMD

constexpr unsigned traction_modem::Defs::OFS_CMD = 4
staticconstexpr

Offset of the command in the packet.

Definition at line 127 of file TractionModemDefs.hxx.

◆ OFS_DATA

constexpr unsigned traction_modem::Defs::OFS_DATA = 8
staticconstexpr

Offset of the first data byte in the packet.

Definition at line 131 of file TractionModemDefs.hxx.

◆ OFS_LEN

constexpr unsigned traction_modem::Defs::OFS_LEN = 6
staticconstexpr

Offset of the length in the packet.

Definition at line 129 of file TractionModemDefs.hxx.

◆ PREAMBLE

constexpr uint32_t traction_modem::Defs::PREAMBLE = 0x41d2c37a
staticconstexpr

Every command starts with these bytes.

Definition at line 51 of file TractionModemDefs.hxx.

◆ PREAMBLE_FIRST

constexpr char traction_modem::Defs::PREAMBLE_FIRST = 0x41
staticconstexpr

First byte of the preamble.

Definition at line 53 of file TractionModemDefs.hxx.

◆ RESPONSE

constexpr uint16_t traction_modem::Defs::RESPONSE = 0x8000
staticconstexpr

The most significant bit of each command means "response" to a previous message.

Definition at line 57 of file TractionModemDefs.hxx.


The documentation for this struct was generated from the following file: