39#ifndef MQTT_CLIENT_SUPPORT
40 #define MQTT_CLIENT_SUPPORT ENABLED
41#elif (MQTT_CLIENT_SUPPORT != ENABLED && MQTT_CLIENT_SUPPORT != DISABLED)
42 #error MQTT_CLIENT_SUPPORT parameter is not valid
46#ifndef MQTT_CLIENT_TLS_SUPPORT
47 #define MQTT_CLIENT_TLS_SUPPORT DISABLED
48#elif (MQTT_CLIENT_TLS_SUPPORT != ENABLED && MQTT_CLIENT_TLS_SUPPORT != DISABLED)
49 #error MQTT_CLIENT_TLS_SUPPORT parameter is not valid
53#ifndef MQTT_CLIENT_WS_SUPPORT
54 #define MQTT_CLIENT_WS_SUPPORT DISABLED
55#elif (MQTT_CLIENT_WS_SUPPORT != ENABLED && MQTT_CLIENT_WS_SUPPORT != DISABLED)
56 #error MQTT_CLIENT_WS_SUPPORT parameter is not valid
60#ifndef MQTT_CLIENT_DEFAULT_KEEP_ALIVE
61 #define MQTT_CLIENT_DEFAULT_KEEP_ALIVE 0
62#elif (MQTT_CLIENT_DEFAULT_KEEP_ALIVE < 0)
63 #error MQTT_CLIENT_DEFAULT_KEEP_ALIVE parameter is not valid
67#ifndef MQTT_CLIENT_DEFAULT_TIMEOUT
68 #define MQTT_CLIENT_DEFAULT_TIMEOUT 20000
69#elif (MQTT_CLIENT_DEFAULT_TIMEOUT < 0)
70 #error MQTT_CLIENT_DEFAULT_TIMEOUT parameter is not valid
74#ifndef MQTT_CLIENT_MAX_HOST_LEN
75 #define MQTT_CLIENT_MAX_HOST_LEN 32
76#elif (MQTT_CLIENT_MAX_HOST_LEN < 1)
77 #error MQTT_CLIENT_MAX_HOST_LEN parameter is not valid
81#ifndef MQTT_CLIENT_MAX_URI_LEN
82 #define MQTT_CLIENT_MAX_URI_LEN 16
83#elif (MQTT_CLIENT_MAX_URI_LEN < 1)
84 #error MQTT_CLIENT_MAX_URI_LEN parameter is not valid
88#ifndef MQTT_CLIENT_MAX_ID_LEN
89 #define MQTT_CLIENT_MAX_ID_LEN 23
90#elif (MQTT_CLIENT_MAX_ID_LEN < 0)
91 #error MQTT_CLIENT_MAX_ID_LEN parameter is not valid
95#ifndef MQTT_CLIENT_MAX_USERNAME_LEN
96 #define MQTT_CLIENT_MAX_USERNAME_LEN 16
97#elif (MQTT_CLIENT_MAX_USERNAME_LEN < 0)
98 #error MQTT_CLIENT_MAX_USERNAME_LEN parameter is not valid
102#ifndef MQTT_CLIENT_MAX_PASSWORD_LEN
103 #define MQTT_CLIENT_MAX_PASSWORD_LEN 16
104#elif (MQTT_CLIENT_MAX_PASSWORD_LEN < 0)
105 #error MQTT_CLIENT_MAX_PASSWORD_LEN parameter is not valid
109#ifndef MQTT_CLIENT_MAX_WILL_TOPIC_LEN
110 #define MQTT_CLIENT_MAX_WILL_TOPIC_LEN 16
111#elif (MQTT_CLIENT_MAX_WILL_TOPIC_LEN < 0)
112 #error MQTT_CLIENT_MAX_WILL_TOPIC_LEN parameter is not valid
116#ifndef MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN
117 #define MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN 16
118#elif (MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN < 0)
119 #error MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN parameter is not valid
123#ifndef MQTT_CLIENT_BUFFER_SIZE
124 #define MQTT_CLIENT_BUFFER_SIZE 1024
125#elif (MQTT_CLIENT_BUFFER_SIZE < 1)
126 #error MQTT_CLIENT_BUFFER_SIZE parameter is not valid
130#ifndef MQTT_CLIENT_PRIVATE_CONTEXT
131 #define MQTT_CLIENT_PRIVATE_CONTEXT
135#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
136 #include "core/crypto.h"
141#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
147#define MqttClientContext struct _MqttClientContext
161 MQTT_CLIENT_STATE_DISCONNECTED = 0,
162 MQTT_CLIENT_STATE_CONNECTING = 1,
163 MQTT_CLIENT_STATE_CONNECTED = 2,
164 MQTT_CLIENT_STATE_IDLE = 3,
165 MQTT_CLIENT_STATE_SENDING_PACKET = 4,
166 MQTT_CLIENT_STATE_PACKET_SENT = 5,
167 MQTT_CLIENT_STATE_WAITING_PACKET = 6,
168 MQTT_CLIENT_STATE_RECEIVING_PACKET = 7,
169 MQTT_CLIENT_STATE_PACKET_RECEIVED = 8,
170 MQTT_CLIENT_STATE_DISCONNECTING = 9
179 uint8_t connectAckFlags, uint8_t connectReturnCode);
187 const char_t *topic,
const uint8_t *message,
size_t length,
247#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
265 char_t topic[MQTT_CLIENT_MAX_WILL_TOPIC_LEN + 1];
266 uint8_t payload[MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN];
288#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
304#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
305 char_t host[MQTT_CLIENT_MAX_HOST_LEN + 1];
306 char_t uri[MQTT_CLIENT_MAX_URI_LEN + 1];
308 char_t clientId[MQTT_CLIENT_MAX_ID_LEN + 1];
309 char_t username[MQTT_CLIENT_MAX_USERNAME_LEN + 1];
310 char_t password[MQTT_CLIENT_MAX_PASSWORD_LEN + 1];
326#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
330#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
342 MQTT_CLIENT_PRIVATE_CONTEXT
347error_t mqttClientInit(MqttClientContext *context);
350error_t mqttClientRegisterCallbacks(MqttClientContext *context,
355error_t mqttClientSetTransportProtocol(MqttClientContext *context,
358#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
360error_t mqttClientRegisterTlsInitCallback(MqttClientContext *context,
365error_t mqttClientRegisterPublishCallback(MqttClientContext *context,
369error_t mqttClientSetKeepAlive(MqttClientContext *context, uint16_t keepAlive);
371error_t mqttClientSetHost(MqttClientContext *context,
const char_t *host);
372error_t mqttClientSetUri(MqttClientContext *context,
const char_t *uri);
374error_t mqttClientSetIdentifier(MqttClientContext *context,
375 const char_t *clientId);
377error_t mqttClientSetAuthInfo(MqttClientContext *context,
378 const char_t *username,
const char_t *password);
380error_t mqttClientSetWillMessage(MqttClientContext *context,
const char_t *topic,
381 const void *message,
size_t length,
MqttQosLevel qos, bool_t retain);
383error_t mqttClientBindToInterface(MqttClientContext *context,
384 NetInterface *interface);
386error_t mqttClientConnect(MqttClientContext *context,
387 const IpAddr *serverIpAddr, uint16_t serverPort, bool_t cleanSession);
389error_t mqttClientPublish(MqttClientContext *context,
390 const char_t *topic,
const void *message,
size_t length,
393error_t mqttClientSubscribe(MqttClientContext *context,
394 const char_t *topic,
MqttQosLevel qos, uint16_t *packetId);
396error_t mqttClientUnsubscribe(MqttClientContext *context,
397 const char_t *topic, uint16_t *packetId);
403error_t mqttClientDisconnect(MqttClientContext *context);
404error_t mqttClientClose(MqttClientContext *context);
406void mqttClientDeinit(MqttClientContext *context);
409error_t mqttClientProcessEvents(MqttClientContext *context,
systime_t timeout);
error_t
Error codes.
Definition error.h:43
void(* MqttClientSubAckCallback)(MqttClientContext *context, uint16_t packetId)
SUBACK message received callback.
Definition mqtt_client.h:227
void(* MqttClientConnAckCallback)(MqttClientContext *context, uint8_t connectAckFlags, uint8_t connectReturnCode)
CONNACK message received callback.
Definition mqtt_client.h:178
void(* MqttClientPublishCallback)(MqttClientContext *context, const char_t *topic, const uint8_t *message, size_t length, bool_t dup, MqttQosLevel qos, bool_t retain, uint16_t packetId)
PUBLISH message received callback.
Definition mqtt_client.h:186
MqttClientState
MQTT client states.
Definition mqtt_client.h:160
void(* MqttClientPubRecCallback)(MqttClientContext *context, uint16_t packetId)
PUBREC message received callback.
Definition mqtt_client.h:203
void(* MqttClientPingRespCallback)(MqttClientContext *context)
PINGRESP message received callback.
Definition mqtt_client.h:243
void(* MqttClientPubAckCallback)(MqttClientContext *context, uint16_t packetId)
PUBACK message received callback.
Definition mqtt_client.h:195
void(* MqttClientUnsubAckCallback)(MqttClientContext *context, uint16_t packetId)
UNSUBACK message received callback.
Definition mqtt_client.h:235
void(* MqttClientPubCompCallback)(MqttClientContext *context, uint16_t packetId)
PUBCOMP message received callback.
Definition mqtt_client.h:219
error_t(* MqttClientTlsInitCallback)(MqttClientContext *context, TlsContext *tlsContext)
TLS initialization callback.
Definition mqtt_client.h:253
void(* MqttClientPubRelCallback)(MqttClientContext *context, uint16_t packetId)
PUBREL message received callback.
Definition mqtt_client.h:211
Definitions common to MQTT client and server.
MqttQosLevel
Quality of service level.
Definition mqtt_common.h:87
MqttTransportProtocol
Transport protocol.
Definition mqtt_common.h:74
MqttVersion
MQTT protocol level.
Definition mqtt_common.h:63
MqttPacketType
MQTT control packet type.
Definition mqtt_common.h:99
uint32_t systime_t
System time.
Definition os_port_none.h:90
IP network address.
Definition ip.h:72
MQTT client callback functions.
Definition mqtt_client.h:278
MqttClientPubAckCallback pubRecCallback
PUBREC message received callback.
Definition mqtt_client.h:282
MqttClientPubAckCallback unsubAckCallback
UNSUBACK message received callback.
Definition mqtt_client.h:286
MqttClientPublishCallback publishCallback
PUBLISH message received callback.
Definition mqtt_client.h:280
MqttClientPubAckCallback pubCompCallback
PUBCOMP message received callback.
Definition mqtt_client.h:284
MqttClientPubAckCallback pubAckCallback
PUBACK message received callback.
Definition mqtt_client.h:281
MqttClientPubAckCallback pubRelCallback
PUBREL message received callback.
Definition mqtt_client.h:283
MqttClientPingRespCallback pingRespCallback
PINGRESP message received callback.
Definition mqtt_client.h:287
MqttClientPubAckCallback subAckCallback
SUBACK message received callback.
Definition mqtt_client.h:285
MqttClientTlsInitCallback tlsInitCallback
TLS initialization callback.
Definition mqtt_client.h:289
MqttClientConnAckCallback connAckCallback
CONNACK message received callback.
Definition mqtt_client.h:279
MQTT client settings.
Definition mqtt_client.h:299
uint16_t keepAlive
Keep-alive time interval.
Definition mqtt_client.h:302
MqttClientWillMessage willMessage
Will message.
Definition mqtt_client.h:311
MqttTransportProtocol transportProtocol
Transport protocol.
Definition mqtt_client.h:301
systime_t timeout
Communication timeout.
Definition mqtt_client.h:303
MqttVersion version
MQTT protocol version.
Definition mqtt_client.h:300
Will message.
Definition mqtt_client.h:264
bool_t retain
Specifies if the Will message is to be retained.
Definition mqtt_client.h:269
size_t length
Length of the Will message payload.
Definition mqtt_client.h:267
MqttQosLevel qos
QoS level to be used when publishing the Will message.
Definition mqtt_client.h:268
MQTT client context.
Definition mqtt_client.h:320
TlsSessionState tlsSession
TLS session state.
Definition mqtt_client.h:328
size_t packetPos
Current position.
Definition mqtt_client.h:337
systime_t startTime
Start time.
Definition mqtt_client.h:333
uint8_t buffer[MQTT_CLIENT_BUFFER_SIZE]
Internal buffer.
Definition mqtt_client.h:335
MqttPacketType packetType
Control packet type.
Definition mqtt_client.h:339
NetInterface * interface
Underlying network interface.
Definition mqtt_client.h:324
MqttClientCallbacks callbacks
MQTT client callback functions.
Definition mqtt_client.h:322
TlsContext * tlsContext
TLS context.
Definition mqtt_client.h:327
size_t remainingLen
Length of the variable header and payload.
Definition mqtt_client.h:341
MqttClientSettings settings
MQTT client settings.
Definition mqtt_client.h:321
uint16_t packetId
Packet identifier.
Definition mqtt_client.h:340
uint8_t * packet
Pointer to the incoming/outgoing MQTT packet.
Definition mqtt_client.h:336
systime_t keepAliveTimestamp
Timestamp used to manage keep-alive.
Definition mqtt_client.h:334
MqttClientState state
MQTT client state.
Definition mqtt_client.h:323
WebSocket * webSocket
Underlying WebSocket.
Definition mqtt_client.h:331
Socket * socket
Underlying TCP socket.
Definition mqtt_client.h:325
size_t packetLen
Length of the entire MQTT packet.
Definition mqtt_client.h:338
WebSocket API (client and server)