Group dxl2_link¶
Transport-independent link that owns a parser and dispatches packets. More...
Classes¶
| Type | Name |
|---|---|
| struct | dxl2_link Link state. |
| struct | dxl2_link_config Link configuration passed to dxl2_link_init . |
Public Types¶
| Type | Name |
|---|---|
| typedef void(* | dxl2_packet_handler_t Handle one valid packet received by the link. |
| typedef int(* | dxl2_write_fn Write a complete wire packet to the transport. |
Public Functions¶
| Type | Name |
|---|---|
| int | dxl2_link_init (struct dxl2_link * link, const struct dxl2_link_config * config) Initialize a link and reset its parser and counters. |
| uint32_t | dxl2_link_invalid_packets (const struct dxl2_link * link) Number of malformed or CRC-invalid packets rejected since dxl2_link_init . |
| void | dxl2_link_receive (struct dxl2_link * link, const uint8_t * data, size_t length) Feed received bytes; calls the handler for each complete valid packet. |
| uint32_t | dxl2_link_rx_packets (const struct dxl2_link * link) Number of valid packets received since dxl2_link_init . |
| int | dxl2_link_send (struct dxl2_link * link, uint8_t id, uint8_t instruction, const uint8_t * parameters, uint16_t parameter_length) Encode one packet (stuffing and CRC added) and write it. |
| int | dxl2_link_send_status (struct dxl2_link * link, uint8_t id, uint8_t error, const uint8_t * data, uint16_t data_length) Send a Status Packet: error byte followed by data . |
Detailed Description¶
The link owns a parser, dispatches every valid packet to a handler, and encodes outgoing packets into a caller-supplied write function. It has no RTOS or driver dependency: the integrator moves received bytes from the UART/USB/socket into dxl2_link_receive and provides the byte writer.
A link is not thread-safe. Call dxl2_link_receive from one context only (for example an RX thread, not an interrupt handler), and serialize dxl2_link_send calls from several contexts with a mutex.
** **
- Lifecycle: dxl2_link_init.
- Message flow: dxl2_link_receive, dxl2_link_send, dxl2_link_send_status.
- Diagnostics: dxl2_link_rx_packets, dxl2_link_invalid_packets.
Public Types Documentation¶
typedef dxl2_packet_handler_t¶
Handle one valid packet received by the link.
The packet pointer is valid only for the duration of the callback. Copy any data that must be retained after the callback returns. The handler may call dxl2_link_send on the same link to answer.
Parameters:
packetDecoded packet with byte stuffing removed.contextValue of dxl2_link_config::handler_context.
typedef dxl2_write_fn¶
Write a complete wire packet to the transport.
Parameters:
contextValue of dxl2_link_config::write_context.dataEncoded packet, including header and CRC.lengthNumber of bytes indata.
Returns:
0 on success, a negative value on failure (returned by the send call).
Public Functions Documentation¶
function dxl2_link_init¶
Initialize a link and reset its parser and counters.
Parameters:
linkLink state to initialize.configConfiguration copied into the link.
Return value:
0Link ready.DXL2_PARSE_ERRORlinkorconfigis NULL.
function dxl2_link_invalid_packets¶
Number of malformed or CRC-invalid packets rejected since dxl2_link_init .
function dxl2_link_receive¶
Feed received bytes; calls the handler for each complete valid packet.
Bytes may arrive in any chunking. Malformed or CRC-invalid packets are dropped and counted, and the parser resynchronizes on the next header.
Parameters:
linkLink state.dataReceived bytes; may be NULL only whenlengthis zero.lengthNumber of bytes indata.
function dxl2_link_rx_packets¶
Number of valid packets received since dxl2_link_init .
function dxl2_link_send¶
Encode one packet (stuffing and CRC added) and write it.
int dxl2_link_send (
struct dxl2_link * link,
uint8_t id,
uint8_t instruction,
const uint8_t * parameters,
uint16_t parameter_length
)
Parameters:
linkLink state.idDestination ID.instructionInstruction byte.parametersUnstuffed parameters, or NULL whenparameter_lengthis zero.parameter_lengthNumber of parameter bytes.
Return value:
0Packet written.DXL2_PARSE_ERRORInvalid arguments, packet too large, or no writer.Othernegative value Error returned by the write function.
function dxl2_link_send_status¶
Send a Status Packet: error byte followed by data .
int dxl2_link_send_status (
struct dxl2_link * link,
uint8_t id,
uint8_t error,
const uint8_t * data,
uint16_t data_length
)
Parameters:
linkLink state.idID of the answering device.errorStatus error byte (DXL2_ERR_*, optionally with DXL2_ERR_ALERT_BIT).dataStatus data, or NULL whendata_lengthis zero.data_lengthNumber of data bytes (at most DXL2_MAX_PARAMETER_SIZE - 1).
Returns:
Same as dxl2_link_send.