File syn_mqtt.c¶
FileList > net > syn_mqtt.c
Go to the source code of this file
Lightweight MQTT 3.1.1 client implementation — fully non-blocking.
#include "../port/syn_port_system.h"#include "../util/syn_assert.h"#include "../util/syn_metrics.h"#include "../util/syn_pack.h"#include "syn_mqtt.h"#include <string.h>
Public Functions¶
| Type | Name |
|---|---|
| size_t | syn_mqtt5_encode_user_prop (const char * key, const char * val, uint8_t * buf, size_t max_buf_len) Encode an MQTT 5.0 User Property (Key-Value string pair). |
| bool | syn_mqtt_decode_varint (const uint8_t * buf, size_t buf_len, uint32_t * val, size_t * bytes_read) Decode a Variable Byte Integer (MQTT 3.1.1 & MQTT 5.0). |
| void | syn_mqtt_disconnect (SYN_MqttClient * client) Disconnect the MQTT client and close underlying TCP socket. |
| size_t | syn_mqtt_encode_varint (uint32_t val, uint8_t buf) Encode a Variable Byte Integer (MQTT 3.1.1 & MQTT 5.0). |
| SYN_Status | syn_mqtt_init (SYN_MqttClient * client, const char * host, uint16_t port, const char * client_id, const char * username, const char * password, uint16_t keep_alive_s, uint8_t * rx_buf, size_t rx_buf_size, uint8_t * tx_buf, size_t tx_buf_size) Initialize the MQTT client. |
| SYN_Status | syn_mqtt_ping (SYN_MqttClient * client) Transmit an explicit MQTT PINGREQ packet. |
| SYN_Status | syn_mqtt_publish (SYN_MqttClient * client, const char * topic, const void * payload, size_t len, uint8_t qos, bool retain) Publish a message to a topic. |
| SYN_Status | syn_mqtt_subscribe (SYN_MqttClient * client, const char * topic, uint8_t qos) Subscribe to a topic. |
| SYN_PT_Status | syn_mqtt_task (SYN_PT * pt, SYN_Task * task) Cooperative task for driving the MQTT client. |
Public Static Functions¶
| Type | Name |
|---|---|
| size_t | encode_remaining_len (uint8_t * buf, uint32_t len) Encode remaining length field. |
| void | handle_publish (SYN_MqttClient * c, const uint8_t * payload, uint32_t len, uint8_t qos_bits) Handle an incoming PUBLISH packet. |
| bool | mqtt_has_work (const SYN_MqttClient * c) Check if MQTT client task has actionable work. |
| void | poll_rx (SYN_MqttClient * c) Non-blocking receive state machine poll. |
| void | process_packet (SYN_MqttClient * c) Dispatch a fully received MQTT packet. |
| bool | send_mqtt_connect (SYN_MqttClient * c) Build and send an MQTT CONNECT packet. |
| bool | send_mqtt_ping (const SYN_MqttClient * c) Send an MQTT PINGREQ packet. |
Macros¶
| Type | Name |
|---|---|
| define | MQTT_ACK_TIMEOUT_MS 5000 |
| define | MQTT_RECV_TIMEOUT_MS 5000 |
Public Functions Documentation¶
function syn_mqtt5_encode_user_prop¶
Encode an MQTT 5.0 User Property (Key-Value string pair).
size_t syn_mqtt5_encode_user_prop (
const char * key,
const char * val,
uint8_t * buf,
size_t max_buf_len
)
Parameters:
keyProperty key string.valProperty value string.buf[out] Output buffer.max_buf_lenCapacity of output buffer.
Returns:
Number of bytes written, or 0 on error/overflow.
function syn_mqtt_decode_varint¶
Decode a Variable Byte Integer (MQTT 3.1.1 & MQTT 5.0).
bool syn_mqtt_decode_varint (
const uint8_t * buf,
size_t buf_len,
uint32_t * val,
size_t * bytes_read
)
Parameters:
bufBuffer containing varint bytes.buf_lenAvailable bytes in buffer.val[out] Parsed integer value.bytes_read[out] Number of bytes consumed (1..4).
Returns:
true on success, false if incomplete or malformed (> 4 bytes).
function syn_mqtt_disconnect¶
Disconnect the MQTT client and close underlying TCP socket.
Sends an MQTT DISCONNECT packet if currently connected, then closes the socket and transitions client state to DISCONNECTED.
Parameters:
clientPointer to client context.
function syn_mqtt_encode_varint¶
Encode a Variable Byte Integer (MQTT 3.1.1 & MQTT 5.0).
Parameters:
valValue to encode (0..268435455).buf[out] Output buffer (must have at least 4 bytes capacity).
Returns:
Number of bytes written (1..4).
function syn_mqtt_init¶
Initialize the MQTT client.
SYN_Status syn_mqtt_init (
SYN_MqttClient * client,
const char * host,
uint16_t port,
const char * client_id,
const char * username,
const char * password,
uint16_t keep_alive_s,
uint8_t * rx_buf,
size_t rx_buf_size,
uint8_t * tx_buf,
size_t tx_buf_size
)
Configures broker destination, client ID, authentication credentials, keep-alive timing parameters, and network packet buffers.
Parameters:
clientPointer to client context.hostBroker network address string.portBroker port number.client_idMQTT client identity string.usernameAuthentication username (or NULL).passwordAuthentication password (or NULL).keep_alive_sKeep-alive timeout parameter in seconds.rx_bufReceive buffer storage.rx_buf_sizeReceive buffer capacity.tx_bufTransmit buffer storage.tx_buf_sizeTransmit buffer capacity.
Returns:
SYN_OK on successful configuration, or error parameter code.
function syn_mqtt_ping¶
Transmit an explicit MQTT PINGREQ packet.
Note: PINGREQ packets are sent automatically by syn_mqtt_task based on the configured keep_alive_s interval. This function allows manual pinging on demand.
Parameters:
clientPointer to client context.
Returns:
SYN_OK on success, SYN_ERROR if not connected or transmit failed.
function syn_mqtt_publish¶
Publish a message to a topic.
SYN_Status syn_mqtt_publish (
SYN_MqttClient * client,
const char * topic,
const void * payload,
size_t len,
uint8_t qos,
bool retain
)
Non-blocking publish command. For QoS 0, queued directly. For QoS 1, tracks acknowledgement state.
Parameters:
clientPointer to client context.topicTopic name to target.payloadData payload to send.lenPayload size in bytes.qosQuality of service level (0 or 1).retainRetain flag on broker.
Returns:
SYN_OK on queued, or error status if payload bounds exceeded.
function syn_mqtt_subscribe¶
Subscribe to a topic.
Formats and queues a subscription request for transmission.
Parameters:
clientPointer to client context.topicTopic filter string.qosRequested quality of service.
Returns:
SYN_OK on success.
function syn_mqtt_task¶
Cooperative task for driving the MQTT client.
Yields during connection, socket polling, keep-alive pinging, and packet parsing loops. Runs within the cooperative scheduler context.
Parameters:
ptCooperative protothread handle.taskCorresponding task control block.
Returns:
PT_WAITING or PT_EXITED status.
Public Static Functions Documentation¶
function encode_remaining_len¶
Encode remaining length field.
Parameters:
bufDestination buffer.lenLength value to encode.
Returns:
Number of bytes written.
function handle_publish¶
Handle an incoming PUBLISH packet.
static void handle_publish (
SYN_MqttClient * c,
const uint8_t * payload,
uint32_t len,
uint8_t qos_bits
)
Parameters:
cMQTT client.payloadRaw payload (after fixed header).lenPayload length.qos_bitsQoS flags from the fixed header.
function mqtt_has_work¶
Check if MQTT client task has actionable work.
Parameters:
cPointer to MQTT client instance.
Returns:
true if work pending, false otherwise.
function poll_rx¶
Non-blocking receive state machine poll.
Reads available bytes with timeout=0. Advances through RX phases: IDLE → REMAINING_LEN → PAYLOAD / DISCARD → IDLE
Parameters:
cMQTT client.
function process_packet¶
Dispatch a fully received MQTT packet.
Parameters:
cMQTT client context.
function send_mqtt_connect¶
Build and send an MQTT CONNECT packet.
Parameters:
cMQTT client.
Returns:
true on success.
function send_mqtt_ping¶
Send an MQTT PINGREQ packet.
Parameters:
cMQTT client.
Returns:
true on success.
Macro Definition Documentation¶
define MQTT_ACK_TIMEOUT_MS¶
Timeout for ACK responses (ms).
define MQTT_RECV_TIMEOUT_MS¶
Timeout for incomplete packet reception (ms).
The documentation for this class was generated from the following file src/syntropic/net/syn_mqtt.c