Skip to content

File syn_router.c

FileList > net > syn_router.c

Go to the source code of this file

Packet router implementation.

  • #include "syn_router.h"
  • #include "syntropic/util/syn_assert.h"
  • #include "syntropic/util/syn_pack.h"
  • #include <string.h>

Public Functions

Type Name
void syn_router_enable_ack (SYN_Router * r, SYN_PendingAck * pending, uint8_t cap, uint16_t timeout_ms, uint8_t max_retries)
Enable reliable delivery (ACK/retry).
void syn_router_init (SYN_Router * r, uint8_t node_id, SYN_Transport * transport, SYN_RouterHandler * handlers, uint8_t handler_cap)
Initialize packet router.
void syn_router_poll (SYN_Router * r)
Poll for incoming packets and dispatch handlers.
bool syn_router_register (SYN_Router * r, uint8_t type, SYN_RouterHandlerFn handler, void * ctx)
Register a handler for a message type.
bool syn_router_send (SYN_Router * r, uint8_t dst, uint8_t type, const uint8_t * data, uint8_t len, bool reliable)
Send a message.

Public Static Functions

Type Name
void check_retries (SYN_Router * r)
Retry pending packets that have timed out.
bool deserialize_packet (const uint8_t * buf, size_t len, SYN_Packet * pkt)
Deserialize a wire-format buffer into a packet.
void handle_ack (SYN_Router * r, uint8_t src, uint8_t seq)
Handle an incoming ACK by clearing the pending entry.
bool queue_pending (SYN_Router * r, uint8_t dst, uint8_t type, uint8_t seq, const uint8_t * data, uint8_t len)
Queue a pending ACK entry for reliable delivery.
void send_ack (SYN_Router * r, uint8_t dst, uint8_t seq)
Send an ACK packet to the source.
size_t serialize_packet (const SYN_Packet * pkt, uint8_t * buf)
Serialize a packet into a wire-format buffer.

Public Functions Documentation

function syn_router_enable_ack

Enable reliable delivery (ACK/retry).

void syn_router_enable_ack (
    SYN_Router * r,
    SYN_PendingAck * pending,
    uint8_t cap,
    uint16_t timeout_ms,
    uint8_t max_retries
) 

Parameters:

  • r Router.
  • pending Caller-owned pending ACK array.
  • cap Array capacity.
  • timeout_ms ACK timeout per attempt.
  • max_retries Max retransmissions before giving up.

function syn_router_init

Initialize packet router.

void syn_router_init (
    SYN_Router * r,
    uint8_t node_id,
    SYN_Transport * transport,
    SYN_RouterHandler * handlers,
    uint8_t handler_cap
) 

Parameters:

  • r Router instance.
  • node_id This node's ID (0-254).
  • transport Transport to send/receive over.
  • handlers Caller-owned handler array.
  • handler_cap Array capacity.

function syn_router_poll

Poll for incoming packets and dispatch handlers.

void syn_router_poll (
    SYN_Router * r
) 

Also checks for ACK timeouts and retransmits if needed. Call from your main loop.

Parameters:

  • r Router.

function syn_router_register

Register a handler for a message type.

bool syn_router_register (
    SYN_Router * r,
    uint8_t type,
    SYN_RouterHandlerFn handler,
    void * ctx
) 

Parameters:

  • r Router.
  • type Message type to handle.
  • handler Handler function.
  • ctx User context.

Returns:

true if registered, false if handler table is full.


function syn_router_send

Send a message.

bool syn_router_send (
    SYN_Router * r,
    uint8_t dst,
    uint8_t type,
    const uint8_t * data,
    uint8_t len,
    bool reliable
) 

Parameters:

  • r Router.
  • dst Destination node ID (0xFF = broadcast).
  • type Message type.
  • data Payload.
  • len Payload length.
  • reliable If true and ACK is enabled, wait for ACK with retry.

Returns:

true if sent (or queued for retry).


Public Static Functions Documentation

function check_retries

Retry pending packets that have timed out.

static void check_retries (
    SYN_Router * r
) 

Parameters:

  • r Router instance.

function deserialize_packet

Deserialize a wire-format buffer into a packet.

static bool deserialize_packet (
    const uint8_t * buf,
    size_t len,
    SYN_Packet * pkt
) 

Parameters:

  • buf Input buffer.
  • len Buffer length.
  • pkt [out] Parsed packet.

Returns:

true on success.


function handle_ack

Handle an incoming ACK by clearing the pending entry.

static void handle_ack (
    SYN_Router * r,
    uint8_t src,
    uint8_t seq
) 

Parameters:

  • r Router instance.
  • src Source node ID.
  • seq Sequence number.

function queue_pending

Queue a pending ACK entry for reliable delivery.

static bool queue_pending (
    SYN_Router * r,
    uint8_t dst,
    uint8_t type,
    uint8_t seq,
    const uint8_t * data,
    uint8_t len
) 

Parameters:

  • r Router instance.
  • dst Destination node ID.
  • type Message type.
  • seq Sequence number.
  • data Payload data.
  • len Payload length.

Returns:

true if queued successfully.


function send_ack

Send an ACK packet to the source.

static void send_ack (
    SYN_Router * r,
    uint8_t dst,
    uint8_t seq
) 

Parameters:

  • r Router instance.
  • dst Destination node to acknowledge.
  • seq Sequence number being acknowledged.

function serialize_packet

Serialize a packet into a wire-format buffer.

static size_t serialize_packet (
    const SYN_Packet * pkt,
    uint8_t * buf
) 

Parameters:

  • pkt Packet to serialize.
  • buf [out] Output buffer (header + payload).

Returns:

Total bytes written.



The documentation for this class was generated from the following file src/syntropic/net/syn_router.c