Skip to content

File syn_mqttsn.c

FileList > proto > syn_mqttsn.c

Go to the source code of this file

MQTT for Sensor Networks (MQTT-SN v1.2) Protocol Client Engine Implementation.

  • #include "../port/syn_port_system.h"
  • #include "syn_mqttsn.h"
  • #include <string.h>

Public Functions

Type Name
SYN_Status syn_mqttsn_client_connect (SYN_MQTTSN_Client * client)
Send CONNECT packet to MQTT-SN gateway.
SYN_Status syn_mqttsn_client_disconnect (SYN_MQTTSN_Client * client)
Disconnect client from gateway.
SYN_Status syn_mqttsn_client_init (SYN_MQTTSN_Client * client, const SYN_MQTTSN_Config * cfg)
Initialize MQTT-SN client.
SYN_PT_Status syn_mqttsn_client_pt (SYN_PT * pt, SYN_Task * task)
Protothread continuation function for syn_sched .
SYN_Status syn_mqttsn_client_publish (SYN_MQTTSN_Client * client, uint16_t topic_id, int8_t qos, bool retain, const uint8_t * payload, size_t len)
Publish data payload using a registered 16-bit Topic ID.
SYN_Status syn_mqttsn_client_publish_predefined (SYN_MQTTSN_Client * client, uint16_t predef_id, int8_t qos, bool retain, const uint8_t * payload, size_t len)
Publish data payload using a predefined 16-bit Topic ID.
SYN_Status syn_mqttsn_client_publish_short (SYN_MQTTSN_Client * client, const char short_topic, int8_t qos, bool retain, const uint8_t * payload, size_t len)
Publish data payload using a short 2-character topic name.
SYN_Status syn_mqttsn_client_register_topic (SYN_MQTTSN_Client * client, const char * topic_name, uint16_t * out_msg_id)
Register a topic name with gateway to obtain a 16-bit Topic ID.
SYN_Status syn_mqttsn_client_searchgw (SYN_MQTTSN_Client * client, uint8_t radius)
Broadcast SEARCHGW to discover active MQTT-SN gateways.
SYN_Status syn_mqttsn_client_sleep (SYN_MQTTSN_Client * client, uint16_t sleep_duration_s)
Put client into low-power SLEEP state with specified sleep duration.
SYN_Status syn_mqttsn_client_step (SYN_MQTTSN_Client * client, uint32_t now_ms)
Non-blocking poll step processing received frames and keep-alive ping timers.
SYN_Status syn_mqttsn_client_subscribe (SYN_MQTTSN_Client * client, const char * topic_name, uint16_t predef_id, int8_t qos)
Subscribe to a topic name or predefined ID.
SYN_Status syn_mqttsn_client_unsubscribe (SYN_MQTTSN_Client * client, const char * topic_name, uint16_t predef_id)
Unsubscribe from a topic.
SYN_Status syn_mqttsn_client_wake (SYN_MQTTSN_Client * client)
Wake up from sleep to flush messages or return to connected state.

Public Static Functions

Type Name
bool mqttsn_decode_header (const uint8_t * buf, size_t len, uint8_t * out_msg_type, size_t * out_payload_offset, size_t * out_payload_len)
Decode MQTT-SN packet header from buffer.
size_t mqttsn_encode_header (uint8_t * buf, size_t max_buf, uint8_t msg_type, size_t payload_len)
Encode MQTT-SN packet header into buffer.
SYN_MQTTSN_TopicEntry * mqttsn_find_or_add_topic (SYN_MQTTSN_Client * client, const char * topic_name)
Find or allocate topic entry in client table.
void mqttsn_process_rx_frame (SYN_MQTTSN_Client * client, uint8_t msg_type, const uint8_t * payload, size_t payload_len)
Handle incoming decoded MQTT-SN message frame.
SYN_Status mqttsn_publish_internal (SYN_MQTTSN_Client * client, uint16_t topic_id, uint8_t topic_type_flag, int8_t qos, bool retain, const uint8_t * payload, size_t len)
Internal helper to publish with configurable topic ID type flag.

Public Functions Documentation

function syn_mqttsn_client_connect

Send CONNECT packet to MQTT-SN gateway.

SYN_Status syn_mqttsn_client_connect (
    SYN_MQTTSN_Client * client
) 

Parameters:

  • client Client instance.

Returns:

SYN_OK on success.


function syn_mqttsn_client_disconnect

Disconnect client from gateway.

SYN_Status syn_mqttsn_client_disconnect (
    SYN_MQTTSN_Client * client
) 

Parameters:

  • client Client instance.

Returns:

SYN_OK on success.


function syn_mqttsn_client_init

Initialize MQTT-SN client.

SYN_Status syn_mqttsn_client_init (
    SYN_MQTTSN_Client * client,
    const SYN_MQTTSN_Config * cfg
) 

Parameters:

  • client Client instance pointer.
  • cfg Configuration descriptor.

Returns:

SYN_OK on success, SYN_INVALID_PARAM on invalid argument.


function syn_mqttsn_client_pt

Protothread continuation function for syn_sched .

SYN_PT_Status syn_mqttsn_client_pt (
    SYN_PT * pt,
    SYN_Task * task
) 

Parameters:

  • pt Protothread continuation structure.
  • task Task handle containing SYN_MQTTSN_Client in user_data.

Returns:

SYN_PT_Status continuation status.


function syn_mqttsn_client_publish

Publish data payload using a registered 16-bit Topic ID.

SYN_Status syn_mqttsn_client_publish (
    SYN_MQTTSN_Client * client,
    uint16_t topic_id,
    int8_t qos,
    bool retain,
    const uint8_t * payload,
    size_t len
) 

Parameters:

  • client Client instance.
  • topic_id 16-bit Topic ID.
  • qos Quality of Service (0, 1, or -1).
  • retain Retain flag.
  • payload Payload buffer.
  • len Payload length.

Returns:

SYN_OK on success.


function syn_mqttsn_client_publish_predefined

Publish data payload using a predefined 16-bit Topic ID.

SYN_Status syn_mqttsn_client_publish_predefined (
    SYN_MQTTSN_Client * client,
    uint16_t predef_id,
    int8_t qos,
    bool retain,
    const uint8_t * payload,
    size_t len
) 

Parameters:

  • client Client instance.
  • predef_id Predefined 16-bit Topic ID.
  • qos Quality of Service (0, 1, or -1).
  • retain Retain flag.
  • payload Payload buffer.
  • len Payload length.

Returns:

SYN_OK on success.


function syn_mqttsn_client_publish_short

Publish data payload using a short 2-character topic name.

SYN_Status syn_mqttsn_client_publish_short (
    SYN_MQTTSN_Client * client,
    const char short_topic,
    int8_t qos,
    bool retain,
    const uint8_t * payload,
    size_t len
) 

Parameters:

  • client Client instance.
  • short_topic 2-character topic string.
  • qos Quality of Service (0, 1, or -1).
  • retain Retain flag.
  • payload Payload buffer.
  • len Payload length.

Returns:

SYN_OK on success.


function syn_mqttsn_client_register_topic

Register a topic name with gateway to obtain a 16-bit Topic ID.

SYN_Status syn_mqttsn_client_register_topic (
    SYN_MQTTSN_Client * client,
    const char * topic_name,
    uint16_t * out_msg_id
) 

Parameters:

  • client Client instance.
  • topic_name String topic name.
  • out_msg_id Optional pointer to receive allocated MsgId.

Returns:

SYN_OK on success.


function syn_mqttsn_client_searchgw

Broadcast SEARCHGW to discover active MQTT-SN gateways.

SYN_Status syn_mqttsn_client_searchgw (
    SYN_MQTTSN_Client * client,
    uint8_t radius
) 

Parameters:

  • client Client instance.
  • radius Search radius hop count.

Returns:

SYN_OK on success.


function syn_mqttsn_client_sleep

Put client into low-power SLEEP state with specified sleep duration.

SYN_Status syn_mqttsn_client_sleep (
    SYN_MQTTSN_Client * client,
    uint16_t sleep_duration_s
) 

Parameters:

  • client Client instance.
  • sleep_duration_s Sleep duration in seconds.

Returns:

SYN_OK on success.


function syn_mqttsn_client_step

Non-blocking poll step processing received frames and keep-alive ping timers.

SYN_Status syn_mqttsn_client_step (
    SYN_MQTTSN_Client * client,
    uint32_t now_ms
) 

Parameters:

  • client Client instance.
  • now_ms Current timestamp in milliseconds.

Returns:

SYN_OK on success.


function syn_mqttsn_client_subscribe

Subscribe to a topic name or predefined ID.

SYN_Status syn_mqttsn_client_subscribe (
    SYN_MQTTSN_Client * client,
    const char * topic_name,
    uint16_t predef_id,
    int8_t qos
) 

Parameters:

  • client Client instance.
  • topic_name Topic name string (or NULL if subscribing by predef_id).
  • predef_id Predefined Topic ID (if topic_name is NULL).
  • qos Requested QoS level.

Returns:

SYN_OK on success.


function syn_mqttsn_client_unsubscribe

Unsubscribe from a topic.

SYN_Status syn_mqttsn_client_unsubscribe (
    SYN_MQTTSN_Client * client,
    const char * topic_name,
    uint16_t predef_id
) 

Parameters:

  • client Client instance.
  • topic_name Topic name string.
  • predef_id Predefined Topic ID.

Returns:

SYN_OK on success.


function syn_mqttsn_client_wake

Wake up from sleep to flush messages or return to connected state.

SYN_Status syn_mqttsn_client_wake (
    SYN_MQTTSN_Client * client
) 

Parameters:

  • client Client instance.

Returns:

SYN_OK on success.


Public Static Functions Documentation

function mqttsn_decode_header

Decode MQTT-SN packet header from buffer.

static bool mqttsn_decode_header (
    const uint8_t * buf,
    size_t len,
    uint8_t * out_msg_type,
    size_t * out_payload_offset,
    size_t * out_payload_len
) 

Parameters:

  • buf Input buffer.
  • len Buffer length.
  • out_msg_type Output message type.
  • out_payload_offset Output byte offset to payload.
  • out_payload_len Output payload length.

Returns:

true if valid header, false if incomplete or malformed.


function mqttsn_encode_header

Encode MQTT-SN packet header into buffer.

static size_t mqttsn_encode_header (
    uint8_t * buf,
    size_t max_buf,
    uint8_t msg_type,
    size_t payload_len
) 

Parameters:

  • buf Output buffer.
  • max_buf Maximum buffer capacity.
  • msg_type MQTT-SN message type.
  • payload_len Length of payload that follows the header.

Returns:

Header length in bytes (2 or 4), or 0 on buffer overflow.


function mqttsn_find_or_add_topic

Find or allocate topic entry in client table.

static SYN_MQTTSN_TopicEntry * mqttsn_find_or_add_topic (
    SYN_MQTTSN_Client * client,
    const char * topic_name
) 

Parameters:

  • client Client instance.
  • topic_name Topic string.

Returns:

Pointer to topic entry, or NULL if table is full.


function mqttsn_process_rx_frame

Handle incoming decoded MQTT-SN message frame.

static void mqttsn_process_rx_frame (
    SYN_MQTTSN_Client * client,
    uint8_t msg_type,
    const uint8_t * payload,
    size_t payload_len
) 

Parameters:

  • client Client instance pointer.
  • msg_type Decoded MQTT-SN message type.
  • payload Pointer to payload buffer.
  • payload_len Length of payload in bytes.

function mqttsn_publish_internal

Internal helper to publish with configurable topic ID type flag.

static SYN_Status mqttsn_publish_internal (
    SYN_MQTTSN_Client * client,
    uint16_t topic_id,
    uint8_t topic_type_flag,
    int8_t qos,
    bool retain,
    const uint8_t * payload,
    size_t len
) 

Parameters:

  • client Client instance pointer.
  • topic_id Topic ID or 2-byte short topic name.
  • topic_type_flag Topic ID type flag (Normal, Predefined, Short).
  • qos QoS level (0, 1, -1).
  • retain Retain flag.
  • payload Payload data buffer.
  • len Payload length in bytes.

Returns:

SYN_OK on success.



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