mikroSDK Reference Manual
mqtt_sn_client.h
Go to the documentation of this file.
1
31#ifndef _MQTT_SN_CLIENT_H
32#define _MQTT_SN_CLIENT_H
33
34//Dependencies
35#include "core/net.h"
38
39//MQTT-SN client support
40#ifndef MQTT_SN_CLIENT_SUPPORT
41 #define MQTT_SN_CLIENT_SUPPORT ENABLED
42#elif (MQTT_SN_CLIENT_SUPPORT != ENABLED && MQTT_SN_CLIENT_SUPPORT != DISABLED)
43 #error MQTT_SN_CLIENT_SUPPORT parameter is not valid
44#endif
45
46//MQTT-SN over DTLS
47#ifndef MQTT_SN_CLIENT_DTLS_SUPPORT
48 #define MQTT_SN_CLIENT_DTLS_SUPPORT DISABLED
49#elif (MQTT_SN_CLIENT_DTLS_SUPPORT != ENABLED && MQTT_SN_CLIENT_DTLS_SUPPORT != DISABLED)
50 #error MQTT_SN_CLIENT_DTLS_SUPPORT parameter is not valid
51#endif
52
53//Maximum number of topics that can be registered
54#ifndef MQTT_SN_CLIENT_TOPIC_TABLE_SIZE
55 #define MQTT_SN_CLIENT_TOPIC_TABLE_SIZE 10
56#elif (MQTT_SN_CLIENT_TOPIC_TABLE_SIZE < 0)
57 #error MQTT_SN_CLIENT_TOPIC_TABLE_SIZE parameter is not valid
58#endif
59
60//Maximum number of QoS 2 messages that can be accepted
61#ifndef MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE
62 #define MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE 10
63#elif (MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE < 0)
64 #error MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE parameter is not valid
65#endif
66
67//Maximum length of the client identifier
68#ifndef MQTT_SN_CLIENT_MAX_ID_LEN
69 #define MQTT_SN_CLIENT_MAX_ID_LEN 23
70#elif (MQTT_SN_CLIENT_MAX_ID_LEN < 0)
71 #error MQTT_SN_CLIENT_MAX_ID_LEN parameter is not valid
72#endif
73
74//Maximum length of topic names
75#ifndef MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN
76 #define MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN 32
77#elif (MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN < 0)
78 #error MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN parameter is not valid
79#endif
80
81//Maximum length of the will topic
82#ifndef MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN
83 #define MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN 32
84#elif (MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN < 0)
85 #error MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN parameter is not valid
86#endif
87
88//Maximum length of the will message payload
89#ifndef MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN
90 #define MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN 32
91#elif (MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN < 0)
92 #error MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN parameter is not valid
93#endif
94
95//MQTT-SN client tick interval
96#ifndef MQTT_SN_CLIENT_TICK_INTERVAL
97 #define MQTT_SN_CLIENT_TICK_INTERVAL 100
98#elif (MQTT_SN_CLIENT_TICK_INTERVAL < 10)
99 #error MQTT_SN_CLIENT_TICK_INTERVAL parameter is not valid
100#endif
101
102//Default timeout
103#ifndef MQTT_SN_CLIENT_DEFAULT_TIMEOUT
104 #define MQTT_SN_CLIENT_DEFAULT_TIMEOUT 30000
105#elif (MQTT_SN_CLIENT_DEFAULT_TIMEOUT < 0)
106 #error MQTT_SN_CLIENT_DEFAULT_TIMEOUT parameter is not valid
107#endif
108
109//Default keep-alive time interval
110#ifndef MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE
111 #define MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE 0
112#elif (MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE < 0)
113 #error MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE parameter is not valid
114#endif
115
116//Number of retransmissions of keep-alive probes
117#ifndef MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES
118 #define MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES 5
119#elif (MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES < 0)
120 #error MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES parameter is not valid
121#endif
122
123//Initial delay before the client starts searching for gateways
124#ifndef MQTT_SN_CLIENT_SEARCH_DELAY
125 #define MQTT_SN_CLIENT_SEARCH_DELAY 0
126#elif (MQTT_SN_CLIENT_SEARCH_DELAY < 0)
127 #error MQTT_SN_CLIENT_SEARCH_DELAY parameter is not valid
128#endif
129
130//Retransmission timeout
131#ifndef MQTT_SN_CLIENT_RETRY_TIMEOUT
132 #define MQTT_SN_CLIENT_RETRY_TIMEOUT 5000
133#elif (MQTT_SN_CLIENT_RETRY_TIMEOUT < 1000)
134 #error MQTT_SN_CLIENT_RETRY_TIMEOUT parameter is not valid
135#endif
136
137//Application specific context
138#ifndef MQTT_SN_CLIENT_PRIVATE_CONTEXT
139 #define MQTT_SN_CLIENT_PRIVATE_CONTEXT
140#endif
141
142//DTLS supported?
143#if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
144 #include "core/crypto.h"
145 #include "tls.h"
146#endif
147
148//Forward declaration of MqttSnClientContext structure
150#define MqttSnClientContext struct _MqttSnClientContext
151
152//C++ guard
153#ifdef __cplusplus
154extern "C" {
155#endif
156
157
162typedef enum
163{
164 MQTT_SN_CLIENT_STATE_DISCONNECTED = 0,
165 MQTT_SN_CLIENT_STATE_SEARCHING = 1,
166 MQTT_SN_CLIENT_STATE_CONNECTING = 2,
167 MQTT_SN_CLIENT_STATE_ACTIVE = 3,
168 MQTT_SN_CLIENT_STATE_SENDING_REQ = 4,
169 MQTT_SN_CLIENT_STATE_RESP_RECEIVED = 5,
170 MQTT_SN_CLIENT_STATE_LOST = 6,
171 MQTT_SN_CLIENT_STATE_ASLEEP = 7,
172 MQTT_SN_CLIENT_STATE_AWAKE = 8,
173 MQTT_SN_CLIENT_STATE_DISCONNECTING = 9
175
176
177//DTLS supported?
178#if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
179
184typedef error_t (*MqttSnClientDtlsInitCallback)(MqttSnClientContext *context,
185 TlsContext *dtlsContext);
186
187#endif
188
189
194typedef void (*MqttSnClientPublishCallback)(MqttSnClientContext *context,
195 const char_t *topic, const uint8_t *message, size_t length,
196 MqttSnQosLevel qos, bool_t retain);
197
198
203typedef struct
204{
205 char_t topic[MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN + 1];
206 uint8_t payload[MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN];
207 size_t length;
208 MqttSnFlags flags;
210
211
216typedef struct
217{
218 char_t topicName[MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN + 1];
219 uint16_t topicId;
221
222
227typedef struct
228{
229 bool_t ownership;
230 uint16_t msgId;
232
233
272
273
274//MQTT-SN client related functions
275error_t mqttSnClientInit(MqttSnClientContext *context);
276
277error_t mqttSnClientSetTransportProtocol(MqttSnClientContext *context,
278 MqttSnTransportProtocol transportProtocol);
279
280#if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
281
282error_t mqttSnClientRegisterDtlsInitCallback(MqttSnClientContext *context,
284
285#endif
286
287error_t mqttSnClientRegisterPublishCallback(MqttSnClientContext *context,
289
290error_t mqttSnClientSetPredefinedTopics(MqttSnClientContext *context,
291 MqttSnPredefinedTopic *predefinedTopics, uint_t size);
292
293error_t mqttSnClientSetTimeout(MqttSnClientContext *context,
294 systime_t timeout);
295
296error_t mqttSnClientSetKeepAlive(MqttSnClientContext *context,
297 systime_t keepAlive);
298
299error_t mqttSnClientSetIdentifier(MqttSnClientContext *context,
300 const char_t *clientId);
301
302error_t mqttSnClientSetWillMessage(MqttSnClientContext *context,
303 const char_t *topic, const void *message, size_t length,
304 MqttSnQosLevel qos, bool_t retain);
305
306error_t mqttSnClientBindToInterface(MqttSnClientContext *context,
307 NetInterface *interface);
308
309error_t mqttSnClientSetGateway(MqttSnClientContext *context,
310 const IpAddr *gwIpAddr, uint16_t gwPort);
311
312error_t mqttSnClientSearchGateway(MqttSnClientContext *context,
313 const IpAddr *destIpAddr, uint16_t destPort);
314
315error_t mqttSnClientConnect(MqttSnClientContext *context, bool_t cleanSession);
316
317error_t mqttSnClientPublish(MqttSnClientContext *context,
318 const char_t *topicName, const void *message, size_t length,
319 MqttSnQosLevel qos, bool_t retain, bool_t dup, uint16_t *msgId);
320
321error_t mqttSnClientSubscribe(MqttSnClientContext *context,
322 const char_t *topicName, MqttSnQosLevel qos);
323
324error_t mqttSnClientUnsubscribe(MqttSnClientContext *context,
325 const char_t *topicName);
326
327error_t mqttSnClientPing(MqttSnClientContext *context);
328
329error_t mqttSnClientUpdateWillMessage(MqttSnClientContext *context,
330 const char_t *topic, const void *message, size_t length,
331 MqttSnQosLevel qos, bool_t retain);
332
333error_t mqttSnClientGetReturnCode(MqttSnClientContext *context,
334 MqttSnReturnCode *returnCode);
335
336error_t mqttSnClientTask(MqttSnClientContext *context, systime_t timeout);
337
338error_t mqttSnClientDisconnect(MqttSnClientContext *context,
339 systime_t duration);
340
341void mqttSnClientDeinit(MqttSnClientContext *context);
342
343//C++ guard
344#ifdef __cplusplus
345}
346#endif
347
348#endif
error_t
Error codes.
Definition error.h:43
error_t(* MqttSnClientDtlsInitCallback)(MqttSnClientContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition mqtt_sn_client.h:184
void(* MqttSnClientPublishCallback)(MqttSnClientContext *context, const char_t *topic, const uint8_t *message, size_t length, MqttSnQosLevel qos, bool_t retain)
PUBLISH message received callback.
Definition mqtt_sn_client.h:194
MqttSnClientState
MQTT-SN client states.
Definition mqtt_sn_client.h:163
Definitions common to MQTT-SN client and gateway.
MqttSnMsgType
MQTT-SN message types.
Definition mqtt_sn_common.h:92
MqttSnTransportProtocol
MQTT-SN transport protocols.
Definition mqtt_sn_common.h:55
MqttSnQosLevel
Quality of service level.
Definition mqtt_sn_common.h:66
MqttSnReturnCode
MQTT-SN return codes.
Definition mqtt_sn_common.h:129
MQTT-SN message formatting and parsing.
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
IP network address.
Definition ip.h:72
QoS 2 message state.
Definition mqtt_sn_client.h:228
uint16_t msgId
Message identifier.
Definition mqtt_sn_client.h:230
bool_t ownership
Ownership of message accepted by the client.
Definition mqtt_sn_client.h:229
Mapping between a topic name and a topic ID.
Definition mqtt_sn_client.h:217
uint16_t topicId
Topic identifier.
Definition mqtt_sn_client.h:219
Will message.
Definition mqtt_sn_client.h:204
size_t length
Length of the Will message payload.
Definition mqtt_sn_client.h:207
MqttSnFlags flags
Flags.
Definition mqtt_sn_client.h:208
MQTT-SN message.
Definition mqtt_sn_message.h:56
Predefined topic.
Definition mqtt_sn_common.h:485
MQTT-SN client context.
Definition mqtt_sn_client.h:239
const MqttSnPredefinedTopic * predefinedTopicTable
List of predefined topics.
Definition mqtt_sn_client.h:242
IpAddr gwIpAddr
Gateway IP address.
Definition mqtt_sn_client.h:256
uint16_t msgId
Message identifier.
Definition mqtt_sn_client.h:265
MqttSnReturnCode returnCode
Status code returned by the gateway.
Definition mqtt_sn_client.h:267
uint16_t gwPort
Gateway port number.
Definition mqtt_sn_client.h:257
TlsSessionState dtlsSession
DTLS session state.
Definition mqtt_sn_client.h:252
uint_t keepAliveCounter
PINGREQ retransmission counter.
Definition mqtt_sn_client.h:262
systime_t timeout
Timeout value.
Definition mqtt_sn_client.h:244
MqttSnClientState state
MQTT-SN client state.
Definition mqtt_sn_client.h:240
char_t clientId[MQTT_SN_CLIENT_MAX_ID_LEN+1]
Client identifier.
Definition mqtt_sn_client.h:246
systime_t startTime
Start time.
Definition mqtt_sn_client.h:258
systime_t keepAliveTimestamp
Timestamp used to manage keep-alive.
Definition mqtt_sn_client.h:261
MqttSnClientPublishCallback publishCallback
PUBLISH message received callback.
Definition mqtt_sn_client.h:255
MqttSnClientWillMessage willMessage
Will message.
Definition mqtt_sn_client.h:247
MqttSnTransportProtocol transportProtocol
Transport protocol (UDP or DTLS)
Definition mqtt_sn_client.h:241
NetInterface * interface
Underlying network interface.
Definition mqtt_sn_client.h:248
systime_t retransmitStartTime
Time at which the last message was sent.
Definition mqtt_sn_client.h:259
MqttSnClientDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition mqtt_sn_client.h:253
TlsContext * dtlsContext
DTLS context.
Definition mqtt_sn_client.h:251
MqttSnMsgType msgType
Message type.
Definition mqtt_sn_client.h:264
systime_t keepAlive
Keep-alive interval.
Definition mqtt_sn_client.h:245
MqttSnMessage message
MQTT-SN message.
Definition mqtt_sn_client.h:263
Socket * socket
Underlying TCP socket.
Definition mqtt_sn_client.h:249
uint16_t topicId
Topic identifier returned by the gateway (REGACK/SUBACK)
Definition mqtt_sn_client.h:266
systime_t retransmitTimeout
Retransmission timeout.
Definition mqtt_sn_client.h:260
uint_t predefinedTopicTableSize
Number of predefined topics.
Definition mqtt_sn_client.h:243