MQTT Client
MQTT Client library.
Example Usage
#include <proto/mqttclient.h>
mqtt_client_t *client = NULL;
char client_id[32];
/* set a client ID */
strcpy(client_id, "1234567890");
/* init new mqtt client */
client = mqtt_lease();
/* configure host */
mqtt_set_host(client, "broker.example.com");
/* configure port */
mqtt_set_port(client, "1883");
/* set client id */
mqtt_set_client_id(client, client_id);
/* configure session clean */
mqtt_set_clean_session(client, 1);
/* connect to broker */
mqtt_connect(client);
/* topic subscribe */
mqtt_subscribe(client, "topic1", QOS0, topic1_handler);
/* publish message */
mqtt_message_t msg;
memset(&msg, 0, sizeof(msg));
/* set payload */
msg.payload = "Test";
/* set QOS value */
msg.qos = 0;
/* publish */
mqtt_publish(client, "topic2", &msg);
API Reference
Header File
Source: include/proto/mqttclient.h
#include <proto/mqttclient.h>
Functions
-
const char *mqtt_set_client_id(mqtt_client_t *client, const char *id)
Set MQTT broker client id.
- Parameters:
client – MQTT client handle
id – Client ID to set
- Returns:
client id or NULL when error
-
const char *mqtt_set_user_name(mqtt_client_t *client, const char *username)
Set MQTT broker username.
- Parameters:
client – MQTT client handle
username – Username to set
- Returns:
username or NULL when error
-
const char *mqtt_set_password(mqtt_client_t *client, const char *password)
Set MQTT broker client ID.
- Parameters:
client – MQTT client handle
password – Password to set
- Returns:
password or NULL when error
-
const char *mqtt_set_host(mqtt_client_t *client, const char *host)
Set MQTT broker host name.
- Parameters:
client – MQTT client handle
host – hostname to set
- Returns:
hostname on success or NULL
-
const char *mqtt_set_port(mqtt_client_t *client, const char *port)
Set MQTT broker port.
- Parameters:
client – MQTT client handle
port – Port to set
- Returns:
Port value or NULL when error
-
const char *mqtt_set_ca(mqtt_client_t *client, const char *ca)
Set CA certificate.
- Parameters:
client – MQTT client handle
ca – CA certificate in PEM format
- Returns:
pointer to CA or NULL when error
-
int mqtt_set_client_cert(mqtt_client_t *client, char *cert)
Set client certificate.
- Parameters:
client – MQTT client handle
cert – client certificate in PEM format
- Returns:
0 on success, error code otherwise
-
int mqtt_set_client_prvkey(mqtt_client_t *client, char *key)
Set client private key.
- Parameters:
client – MQTT client handle
key – client private key
- Returns:
0 on success, error code otherwise
-
void *mqtt_set_reconnect_data(mqtt_client_t *client, void *arg)
Set custom user argument passed to reconnect hook handler.
- Parameters:
client – MQTT client handle
arg – User argument
- Returns:
user argument on success or NULL for error
-
uint16_t mqtt_set_keep_alive_interval(mqtt_client_t *client, uint16_t ka)
Set keep alive interval, default 30s.
- Parameters:
client – MQTT client handle
ka – Keep alive interval in seconds
- Returns:
current set value on success or 0 otherwise
-
uint32_t mqtt_set_will_flag(mqtt_client_t *client, uint32_t will_flag)
Enable or disable LWT message, default 0.
- Parameters:
client – MQTT client handle
will_flag – Will flag, 1 to enable, 0 to disable
- Returns:
current value of will flag
-
uint32_t mqtt_set_clean_session(mqtt_client_t *client, uint32_t clean_session)
Enable or disable MQTT clean session flag, default 0.
- Parameters:
client – MQTT client handle
clean_session – clean session flag value, 0 - persistent session, 1 - non-persistent session
- Returns:
current value of clean sesson flag
-
uint32_t mqtt_set_version(mqtt_client_t *client, uint32_t version)
Change MQTT version, default 4 (for MQTT 3.1.1)
- Parameters:
client – MQTT client handle
version – version value
- Returns:
current version value
-
uint32_t mqtt_set_cmd_timeout(mqtt_client_t *client, uint32_t timeout)
Set MQTT command timeout value, default 20000ms.
- Parameters:
client – MQTT client handle
timeout – Timeout value in milliseconds
- Returns:
current set timeout value
-
uint32_t mqtt_set_read_buf_size(mqtt_client_t *client, uint32_t bufsz)
set MQTT read payload buffer size, default 1024 bytes
- Parameters:
client – MQTT client handle
bufsz – size of buffer in bytes
- Returns:
current size on success, 0 on error
-
uint32_t mqtt_set_write_buf_size(mqtt_client_t *client, uint32_t bufsz)
Set MQTT write payload buffer size, default 1024 bytes.
- Parameters:
client – MQTT client handle
bufsz – size of buffer in bytes
- Returns:
current size on success, 0 on error
-
uint32_t mqtt_set_reconnect_try_duration(mqtt_client_t *client, uint32_t duration)
Set MQTT reconnect duration, default 1000ms.
- Parameters:
client – MQTT client handle
duration – reconnect duration in millisecond
- Returns:
current duration on success or 0 on error
-
reconnect_handler_t mqtt_set_reconnect_handler(mqtt_client_t *client, reconnect_handler_t fn)
Set MQTT socket reconnect hook.
- Parameters:
client – MQTT client handle
fn – handler function to set
- Returns:
handler function value on success, NULL on error
-
interceptor_handler_t mqtt_set_interceptor_handler(mqtt_client_t *client, interceptor_handler_t fn)
Set MQTT incoming message interceptor hook.
- Parameters:
client – MQTT client handle
fn – handler function to set
- Returns:
handler function value on success, NULL on error
-
mqtt_client_t *mqtt_lease(void)
Allocate and init a new MQTT client handle.
Note
only single MQTT connection is allowed on MT6261/MT2503 SoC
- Returns:
MQTT client handle
-
int mqtt_release(mqtt_client_t *c)
release previously allocated MQTT client by mqtt_lease()
- Parameters:
c – MQTT client handle
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_connect(mqtt_client_t *c)
Connect to MQTT broker.
- Parameters:
c – MQTT client handle
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_disconnect(mqtt_client_t *c)
Disconnect from MQTT broker.
- Parameters:
c – MQTT client handle
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_subscribe(mqtt_client_t *c, const char *topic_filter, mqtt_qos_t qos, message_handler_t msg_handler)
Subscribe to a topic.
- Parameters:
c – MQTT client handle
topic_filter – Topic name
qos – Quality of service mqtt_qos_t
msg_handler – incoming message handler
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_unsubscribe(mqtt_client_t *c, const char *topic_filter)
Unsubscribe a topic.
- Parameters:
c – MQTT client handle
topic_filter – topic name
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_publish(mqtt_client_t *c, const char *topic_filter, mqtt_message_t *msg)
Publish a message.
- Parameters:
c – MQTT client handle
topic_filter – topic name
msg – Message to send mqtt_message_t
- Returns:
0 on success, error code otherwise mqtt_error_t
-
int mqtt_set_will_options(mqtt_client_t *c, char *topic, mqtt_qos_t qos, uint8_t retained, char *message)
Set/change LWT options, this call will enable will flag if disabled.
- Parameters:
c – MQTT client handle
topic – topic name
qos – Quality of service mqtt_qos_t
retained – retained flag
message – LWT message
- Returns:
0 on success, error code otherwise mqtt_error_t
Structures
-
struct mqtt_message
MQTT Message payload information structure.
Public Members
-
mqtt_qos_t qos
Quality of service see mqtt_qos_t
-
uint8_t retained
flag for retained message
-
uint8_t dup
flag indicating duplicate message
-
uint16_t id
Message identifier
-
size_t payloadlen
Payload length
-
void *payload
Payload data
-
mqtt_qos_t qos
-
struct message_data
MQTT Message data structure.
Public Members
-
char topic_name[MQTT_TOPIC_LEN_MAX]
Topic name
-
mqtt_message_t *message
pointer to message payload
-
char topic_name[MQTT_TOPIC_LEN_MAX]
Macros
-
MQTT_TOPIC_LEN_MAX
mqtt.h
This file has been derived from mqttclient library by jiejie.
Type Definitions
-
typedef enum mqtt_error mqtt_error_t
MQTT error codes.
-
typedef struct mqtt_message mqtt_message_t
MQTT Message payload information structure.
-
typedef struct message_data message_data_t
MQTT Message data structure.
-
typedef struct mqtt_client mqtt_client_t
mqtt_client forward definition
-
typedef void (*interceptor_handler_t)(void *client, message_data_t *msg)
incoming message intercept handler
-
typedef void (*message_handler_t)(void *client, message_data_t *msg)
Incoming message handler for assigned topic.
-
typedef void (*reconnect_handler_t)(void *client, void *reconnect_date)
Reconnection hook, this function is called before reconnection.
Enumerations
-
enum mqtt_error
MQTT error codes.
Values:
-
enumerator MQTT_SSL_CERT_ERROR
-
enumerator MQTT_SOCKET_FAILED_ERROR
-
enumerator MQTT_SOCKET_UNKNOWN_HOST_ERROR
-
enumerator MQTT_SET_PUBLISH_DUP_FAILED_ERROR
-
enumerator MQTT_CLEAN_SESSION_ERROR
-
enumerator MQTT_ACK_NODE_IS_EXIST_ERROR
-
enumerator MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR
-
enumerator MQTT_RESUBSCRIBE_ERROR
-
enumerator MQTT_SUBSCRIBE_ERROR
-
enumerator MQTT_SEND_PACKET_ERROR
-
enumerator MQTT_SERIALIZE_PUBLISH_ACK_PACKET_ERROR
-
enumerator MQTT_PUBLISH_PACKET_ERROR
-
enumerator MQTT_RECONNECT_TIMEOUT_ERROR
-
enumerator MQTT_SUBSCRIBE_NOT_ACK_ERROR
-
enumerator MQTT_NOT_CONNECT_ERROR
-
enumerator MQTT_SUBSCRIBE_ACK_PACKET_ERROR
-
enumerator MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR
-
enumerator MQTT_PUBLISH_ACK_PACKET_ERROR
-
enumerator MQTT_PUBLISH_ACK_TYPE_ERROR
-
enumerator MQTT_PUBREC_PACKET_ERROR
-
enumerator MQTT_BUFFER_TOO_SHORT_ERROR
-
enumerator MQTT_NOTHING_TO_READ_ERROR
-
enumerator MQTT_SUBSCRIBE_QOS_ERROR
-
enumerator MQTT_BUFFER_OVERFLOW_ERROR
-
enumerator MQTT_CONNECT_FAILED_ERROR
-
enumerator MQTT_MEM_NOT_ENOUGH_ERROR
-
enumerator MQTT_NULL_VALUE_ERROR
-
enumerator MQTT_FAILED_ERROR
-
enumerator MQTT_SUCCESS_ERROR
-
enumerator MQTT_SSL_CERT_ERROR
