mikroSDK Reference Manual
mqtt_client.h
Go to the documentation of this file.
1
31#ifndef _MQTT_CLIENT_H
32#define _MQTT_CLIENT_H
33
34//Dependencies
35#include "core/net.h"
36#include "mqtt/mqtt_common.h"
37
38//MQTT client support
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
43#endif
44
45//MQTT over TLS
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
50#endif
51
52//MQTT over WebSocket
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
57#endif
58
59//Default keep-alive time interval, in seconds
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
64#endif
65
66//Default communication timeout, in milliseconds
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
71#endif
72
73//Maximum length of the hostname
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
78#endif
79
80//Maximum length of the resource name
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
85#endif
86
87//Maximum length of the client identifier
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
92#endif
93
94//Maximum length of the user name
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
99#endif
100
101//Maximum length of the password
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
106#endif
107
108//Maximum length of the will topic
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
113#endif
114
115//Maximum length of the will message payload
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
120#endif
121
122//Size of the MQTT client buffer
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
127#endif
128
129//Application specific context
130#ifndef MQTT_CLIENT_PRIVATE_CONTEXT
131 #define MQTT_CLIENT_PRIVATE_CONTEXT
132#endif
133
134//TLS supported?
135#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
136 #include "core/crypto.h"
137 #include "tls.h"
138#endif
139
140//WebSocket supported?
141#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
142 #include "web_socket/web_socket.h"
143#endif
144
145//Forward declaration of MqttClientContext structure
146struct _MqttClientContext;
147#define MqttClientContext struct _MqttClientContext
148
149//C++ guard
150#ifdef __cplusplus
151extern "C" {
152#endif
153
154
159typedef enum
160{
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
172
173
178typedef void (*MqttClientConnAckCallback)(MqttClientContext *context,
179 uint8_t connectAckFlags, uint8_t connectReturnCode);
180
181
186typedef void (*MqttClientPublishCallback)(MqttClientContext *context,
187 const char_t *topic, const uint8_t *message, size_t length,
188 bool_t dup, MqttQosLevel qos, bool_t retain, uint16_t packetId);
189
190
195typedef void (*MqttClientPubAckCallback)(MqttClientContext *context,
196 uint16_t packetId);
197
198
203typedef void (*MqttClientPubRecCallback)(MqttClientContext *context,
204 uint16_t packetId);
205
206
211typedef void (*MqttClientPubRelCallback)(MqttClientContext *context,
212 uint16_t packetId);
213
214
219typedef void (*MqttClientPubCompCallback)(MqttClientContext *context,
220 uint16_t packetId);
221
222
227typedef void (*MqttClientSubAckCallback)(MqttClientContext *context,
228 uint16_t packetId);
229
230
235typedef void (*MqttClientUnsubAckCallback)(MqttClientContext *context,
236 uint16_t packetId);
237
238
243typedef void (*MqttClientPingRespCallback)(MqttClientContext *context);
244
245
246//TLS supported?
247#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
248
253typedef error_t (*MqttClientTlsInitCallback)(MqttClientContext *context,
254 TlsContext *tlsContext);
255
256#endif
257
258
263typedef struct
264{
265 char_t topic[MQTT_CLIENT_MAX_WILL_TOPIC_LEN + 1];
266 uint8_t payload[MQTT_CLIENT_MAX_WILL_PAYLOAD_LEN];
267 size_t length;
269 bool_t retain;
271
272
292
293
298typedef struct
299{
302 uint16_t keepAlive;
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];
307#endif
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];
313
314
320{
324 NetInterface *interface;
325 Socket *socket;
326#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
327 TlsContext *tlsContext;
328 TlsSessionState tlsSession;
329#endif
330#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
331 WebSocket *webSocket;
332#endif
335 uint8_t buffer[MQTT_CLIENT_BUFFER_SIZE];
336 uint8_t *packet;
337 size_t packetPos;
338 size_t packetLen;
340 uint16_t packetId;
342 MQTT_CLIENT_PRIVATE_CONTEXT
343};
344
345
346//MQTT client related functions
347error_t mqttClientInit(MqttClientContext *context);
348void mqttClientInitCallbacks(MqttClientCallbacks *callbacks);
349
350error_t mqttClientRegisterCallbacks(MqttClientContext *context,
351 const MqttClientCallbacks *callbacks);
352
353error_t mqttClientSetVersion(MqttClientContext *context, MqttVersion version);
354
355error_t mqttClientSetTransportProtocol(MqttClientContext *context,
356 MqttTransportProtocol transportProtocol);
357
358#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
359
360error_t mqttClientRegisterTlsInitCallback(MqttClientContext *context,
362
363#endif
364
365error_t mqttClientRegisterPublishCallback(MqttClientContext *context,
367
368error_t mqttClientSetTimeout(MqttClientContext *context, systime_t timeout);
369error_t mqttClientSetKeepAlive(MqttClientContext *context, uint16_t keepAlive);
370
371error_t mqttClientSetHost(MqttClientContext *context, const char_t *host);
372error_t mqttClientSetUri(MqttClientContext *context, const char_t *uri);
373
374error_t mqttClientSetIdentifier(MqttClientContext *context,
375 const char_t *clientId);
376
377error_t mqttClientSetAuthInfo(MqttClientContext *context,
378 const char_t *username, const char_t *password);
379
380error_t mqttClientSetWillMessage(MqttClientContext *context, const char_t *topic,
381 const void *message, size_t length, MqttQosLevel qos, bool_t retain);
382
383error_t mqttClientBindToInterface(MqttClientContext *context,
384 NetInterface *interface);
385
386error_t mqttClientConnect(MqttClientContext *context,
387 const IpAddr *serverIpAddr, uint16_t serverPort, bool_t cleanSession);
388
389error_t mqttClientPublish(MqttClientContext *context,
390 const char_t *topic, const void *message, size_t length,
391 MqttQosLevel qos, bool_t retain, uint16_t *packetId);
392
393error_t mqttClientSubscribe(MqttClientContext *context,
394 const char_t *topic, MqttQosLevel qos, uint16_t *packetId);
395
396error_t mqttClientUnsubscribe(MqttClientContext *context,
397 const char_t *topic, uint16_t *packetId);
398
399error_t mqttClientPing(MqttClientContext *context, systime_t *rtt);
400
401error_t mqttClientTask(MqttClientContext *context, systime_t timeout);
402
403error_t mqttClientDisconnect(MqttClientContext *context);
404error_t mqttClientClose(MqttClientContext *context);
405
406void mqttClientDeinit(MqttClientContext *context);
407
408//Deprecated functions
409error_t mqttClientProcessEvents(MqttClientContext *context, systime_t timeout);
410
411//C++ guard
412#ifdef __cplusplus
413}
414#endif
415
416#endif
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
TCP/IP stack core.
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)