Skip to content

Group dxl2_protocol

Modules > dxl2_protocol

CRC-16, incremental stream parser and packet encoder with byte stuffing. More...

Classes

Type Name
struct dxl2_packet
One decoded Protocol 2.0 packet.
struct dxl2_parser
Incremental parser state for a byte stream.

Public Functions

Type Name
uint16_t dxl2_crc16 (const uint8_t * data, size_t length)
Calculate the Protocol 2.0 CRC-16 checksum.
int dxl2_encode (uint8_t id, uint8_t instruction, const uint8_t * parameters, uint16_t parameter_length, uint8_t * output, size_t output_capacity, size_t * output_length)
Encode a Protocol 2.0 packet, adding byte stuffing and CRC.
int dxl2_parser_feed (struct dxl2_parser * parser, uint8_t byte, struct dxl2_packet * packet)
Feed one byte to the incremental packet parser.
void dxl2_parser_init (struct dxl2_parser * parser)
Reset a parser before starting or restarting a byte stream.

Macros

Type Name
define DXL2_MAX_PARAMETER_SIZE ([**DXL2\_MAX\_PACKET\_SIZE**](group__dxl2__config.md#define-dxl2_max_packet_size) - 10U)
Maximum unstuffed parameter payload size supported by this implementation.
define DXL2_PARSE_ERROR (-1)
Error returned for invalid input, malformed packets, or insufficient capacity.

Detailed Description

** **

Public Functions Documentation

function dxl2_crc16

Calculate the Protocol 2.0 CRC-16 checksum.

uint16_t dxl2_crc16 (
    const uint8_t * data,
    size_t length
) 

Uses polynomial 0x8005 and an initial CRC value of zero.

Parameters:

  • data Bytes covered by the CRC.
  • length Number of bytes to process.

Returns:

Calculated 16-bit CRC; 0 (the initial value) when data is NULL.


function dxl2_encode

Encode a Protocol 2.0 packet, adding byte stuffing and CRC.

int dxl2_encode (
    uint8_t id,
    uint8_t instruction,
    const uint8_t * parameters,
    uint16_t parameter_length,
    uint8_t * output,
    size_t output_capacity,
    size_t * output_length
) 

Parameters:

  • id Packet ID.
  • instruction Instruction byte.
  • parameters Unstuffed parameter bytes, or NULL when the length is zero.
  • parameter_length Number of parameter bytes.
  • output Destination buffer for the complete wire packet.
  • output_capacity Capacity of output in bytes.
  • output_length Receives the encoded packet length on success.

Return value:

  • 0 Packet encoded successfully.
  • DXL2_PARSE_ERROR Invalid input or insufficient output capacity.

function dxl2_parser_feed

Feed one byte to the incremental packet parser.

int dxl2_parser_feed (
    struct dxl2_parser * parser,
    uint8_t byte,
    struct dxl2_packet * packet
) 

The parser searches for the Protocol 2.0 header, waits for the declared packet length, validates CRC, then removes byte stuffing. Continue feeding bytes after a parse error so the parser can find the next packet header.

Parameters:

  • parser Parser state.
  • byte Next byte from the input stream.
  • packet Output packet; its contents are valid only when the call returns 1.

Return value:

  • 1 A complete valid packet is available in packet.
  • 0 More bytes are required.
  • DXL2_PARSE_ERROR Invalid arguments or a malformed/CRC-invalid packet.

function dxl2_parser_init

Reset a parser before starting or restarting a byte stream.

void dxl2_parser_init (
    struct dxl2_parser * parser
) 

Parameters:

  • parser Parser state to initialize; NULL is ignored.

Macro Definition Documentation

define DXL2_MAX_PARAMETER_SIZE

Maximum unstuffed parameter payload size supported by this implementation.

#define DXL2_MAX_PARAMETER_SIZE `( DXL2_MAX_PACKET_SIZE - 10U)`


define DXL2_PARSE_ERROR

Error returned for invalid input, malformed packets, or insufficient capacity.

#define DXL2_PARSE_ERROR `(-1)`