mqtt_sn_client.h
Go to the documentation of this file.
1 /**
2  * @file mqtt_sn_client.h
3  * @brief MQTT-SN client
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MQTT_SN_CLIENT_H
30 #define _MQTT_SN_CLIENT_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "mqtt_sn/mqtt_sn_common.h"
36 
37 //MQTT-SN client support
38 #ifndef MQTT_SN_CLIENT_SUPPORT
39  #define MQTT_SN_CLIENT_SUPPORT ENABLED
40 #elif (MQTT_SN_CLIENT_SUPPORT != ENABLED && MQTT_SN_CLIENT_SUPPORT != DISABLED)
41  #error MQTT_SN_CLIENT_SUPPORT parameter is not valid
42 #endif
43 
44 //MQTT-SN over DTLS
45 #ifndef MQTT_SN_CLIENT_DTLS_SUPPORT
46  #define MQTT_SN_CLIENT_DTLS_SUPPORT DISABLED
47 #elif (MQTT_SN_CLIENT_DTLS_SUPPORT != ENABLED && MQTT_SN_CLIENT_DTLS_SUPPORT != DISABLED)
48  #error MQTT_SN_CLIENT_DTLS_SUPPORT parameter is not valid
49 #endif
50 
51 //Maximum number of topics that can be registered
52 #ifndef MQTT_SN_CLIENT_TOPIC_TABLE_SIZE
53  #define MQTT_SN_CLIENT_TOPIC_TABLE_SIZE 10
54 #elif (MQTT_SN_CLIENT_TOPIC_TABLE_SIZE < 0)
55  #error MQTT_SN_CLIENT_TOPIC_TABLE_SIZE parameter is not valid
56 #endif
57 
58 //Maximum number of QoS 2 messages that can be accepted
59 #ifndef MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE
60  #define MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE 10
61 #elif (MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE < 0)
62  #error MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE parameter is not valid
63 #endif
64 
65 //Maximum length of the client identifier
66 #ifndef MQTT_SN_CLIENT_MAX_ID_LEN
67  #define MQTT_SN_CLIENT_MAX_ID_LEN 23
68 #elif (MQTT_SN_CLIENT_MAX_ID_LEN < 0)
69  #error MQTT_SN_CLIENT_MAX_ID_LEN parameter is not valid
70 #endif
71 
72 //Maximum length of topic names
73 #ifndef MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN
74  #define MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN 32
75 #elif (MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN < 0)
76  #error MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN parameter is not valid
77 #endif
78 
79 //Maximum length of the will topic
80 #ifndef MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN
81  #define MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN 32
82 #elif (MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN < 0)
83  #error MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN parameter is not valid
84 #endif
85 
86 //Maximum length of the will message payload
87 #ifndef MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN
88  #define MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN 32
89 #elif (MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN < 0)
90  #error MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN parameter is not valid
91 #endif
92 
93 //MQTT-SN client tick interval
94 #ifndef MQTT_SN_CLIENT_TICK_INTERVAL
95  #define MQTT_SN_CLIENT_TICK_INTERVAL 100
96 #elif (MQTT_SN_CLIENT_TICK_INTERVAL < 10)
97  #error MQTT_SN_CLIENT_TICK_INTERVAL parameter is not valid
98 #endif
99 
100 //Default timeout
101 #ifndef MQTT_SN_CLIENT_DEFAULT_TIMEOUT
102  #define MQTT_SN_CLIENT_DEFAULT_TIMEOUT 30000
103 #elif (MQTT_SN_CLIENT_DEFAULT_TIMEOUT < 0)
104  #error MQTT_SN_CLIENT_DEFAULT_TIMEOUT parameter is not valid
105 #endif
106 
107 //Default keep-alive time interval
108 #ifndef MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE
109  #define MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE 0
110 #elif (MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE < 0)
111  #error MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE parameter is not valid
112 #endif
113 
114 //Number of retransmissions of keep-alive probes
115 #ifndef MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES
116  #define MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES 5
117 #elif (MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES < 0)
118  #error MQTT_SN_CLIENT_MAX_KEEP_ALIVE_PROBES parameter is not valid
119 #endif
120 
121 //Initial delay before the client starts searching for gateways
122 #ifndef MQTT_SN_CLIENT_SEARCH_DELAY
123  #define MQTT_SN_CLIENT_SEARCH_DELAY 0
124 #elif (MQTT_SN_CLIENT_SEARCH_DELAY < 0)
125  #error MQTT_SN_CLIENT_SEARCH_DELAY parameter is not valid
126 #endif
127 
128 //Retransmission timeout
129 #ifndef MQTT_SN_CLIENT_RETRY_TIMEOUT
130  #define MQTT_SN_CLIENT_RETRY_TIMEOUT 5000
131 #elif (MQTT_SN_CLIENT_RETRY_TIMEOUT < 1000)
132  #error MQTT_SN_CLIENT_RETRY_TIMEOUT parameter is not valid
133 #endif
134 
135 //DTLS supported?
136 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
137  #include "core/crypto.h"
138  #include "tls.h"
139 #endif
140 
141 //Forward declaration of MqttSnClientContext structure
142 struct _MqttSnClientContext;
143 #define MqttSnClientContext struct _MqttSnClientContext
144 
145 //C++ guard
146 #ifdef __cplusplus
147  extern "C" {
148 #endif
149 
150 
151 /**
152  * @brief MQTT-SN client states
153  **/
154 
155 typedef enum
156 {
168 
169 
170 //DTLS supported?
171 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
172 
173 /**
174  * @brief DTLS initialization callback
175  **/
176 
179 
180 #endif
181 
182 
183 /**
184  * @brief PUBLISH message received callback
185  **/
186 
188  const char_t *topic, const uint8_t *message, size_t length,
190 
191 
192 /**
193  * @brief Will message
194  **/
195 
196 typedef struct
197 {
198  char_t topic[MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN + 1]; ///<Will topic name
199  uint8_t payload[MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN]; ///<Will message payload
200  size_t length; ///<Length of the Will message payload
201  MqttSnFlags flags; ///<Flags
203 
204 
205 /**
206  * @brief Mapping between a topic name and a topic ID
207  **/
208 
209 typedef struct
210 {
212  uint16_t topicId; ///<Topic identifier
214 
215 
216 /**
217  * @brief QoS 2 message state
218  **/
219 
220 typedef struct
221 {
222  bool_t ownership; ///<Ownership of message accepted by the client
223  uint16_t msgId; ///<Message identifier
225 
226 
227 /**
228  * @brief MQTT-SN client context
229  **/
230 
232 {
233  MqttSnClientState state; ///<MQTT-SN client state
234  MqttSnTransportProtocol transportProtocol; ///<Transport protocol (UDP or DTLS)
235  const MqttSnPredefinedTopic *predefinedTopicTable; ///<List of predefined topics
236  uint_t predefinedTopicTableSize; ///<Number of predefined topics
237  systime_t timeout; ///<Timeout value
238  systime_t keepAlive; ///<Keep-alive interval
239  char_t clientId[MQTT_SN_CLIENT_MAX_ID_LEN + 1]; ///<Client identifier
241  NetInterface *interface; ///<Underlying network interface
242  Socket *socket; ///<Underlying TCP socket
243 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
244  TlsContext *dtlsContext; ///<DTLS context
245  TlsSessionState dtlsSession; ///<DTLS session state
246  MqttSnClientDtlsInitCallback dtlsInitCallback; ///<DTLS initialization callback
247 #endif
249  IpAddr gwIpAddr; ///<Gateway IP address
250  uint16_t gwPort; ///<Gateway port number
251  systime_t startTime; ///<Start time
252  systime_t retransmitStartTime; ///<Time at which the last message was sent
253  systime_t retransmitTimeout; ///<Retransmission timeout
254  systime_t keepAliveTimestamp; ///<Timestamp used to manage keep-alive
255  uint_t keepAliveCounter; ///<PINGREQ retransmission counter
256  MqttSnMessage message; ///<MQTT-SN message
257  MqttSnMsgType msgType; ///<Message type
258  uint16_t msgId; ///<Message identifier
259  uint16_t topicId; ///<Topic identifier returned by the gateway (REGACK/SUBACK)
260  MqttSnReturnCode returnCode; ///<Status code returned by the gateway
263 };
264 
265 
266 //MQTT-SN client related functions
268 
270  MqttSnTransportProtocol transportProtocol);
271 
272 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
273 
276 
277 #endif
278 
280  MqttSnClientPublishCallback callback);
281 
283  MqttSnPredefinedTopic *predefinedTopics, uint_t size);
284 
286  systime_t timeout);
287 
289  systime_t keepAlive);
290 
292  const char_t *clientId);
293 
295  const char_t *topic, const void *message, size_t length,
297 
299  NetInterface *interface);
300 
302  const IpAddr *gwIpAddr, uint16_t gwPort);
303 
305  const IpAddr *destIpAddr, uint16_t destPort);
306 
308 
310  const char_t *topicName, const void *message, size_t length,
312 
315 
317  const char_t *topicName);
318 
320 
322  const char_t *topic, const void *message, size_t length,
324 
327 
329 
332 
333 //C++ guard
334 #ifdef __cplusplus
335  }
336 #endif
337 
338 #endif
TLS (Transport Layer Security)
uint8_t returnCode
uint_t predefinedTopicTableSize
Number of predefined topics.
MQTT-SN message formatting and parsing.
Mapping between a topic name and a topic ID.
#define MQTT_SN_CLIENT_TOPIC_TABLE_SIZE
Socket * socket
Underlying TCP socket.
error_t mqttSnClientPublish(MqttSnClientContext *context, const char_t *topicName, const void *message, size_t length, MqttSnQosLevel qos, bool_t retain, bool_t dup, uint16_t *msgId)
Publish message.
uint32_t systime_t
Definition: compiler_port.h:44
uint16_t destPort
Definition: tcp.h:301
char char_t
Definition: compiler_port.h:41
MQTT-SN client context.
systime_t keepAliveTimestamp
Timestamp used to manage keep-alive.
systime_t keepAlive
Keep-alive interval.
TCP/IP stack core.
#define MQTT_SN_CLIENT_MAX_TOPIC_NAME_LEN
void mqttSnClientDeinit(MqttSnClientContext *context)
Release MQTT-SN client context.
MqttSnFlags flags
Flags.
systime_t retransmitTimeout
Retransmission timeout.
MqttSnTransportProtocol transportProtocol
Transport protocol (UDP or DTLS)
error_t mqttSnClientInit(MqttSnClientContext *context)
Initialize MQTT-SN client context.
error_t mqttSnClientSetIdentifier(MqttSnClientContext *context, const char_t *clientId)
Set client identifier.
uint8_t message[]
Definition: chap.h:150
error_t mqttSnClientSetWillMessage(MqttSnClientContext *context, const char_t *topic, const void *message, size_t length, MqttSnQosLevel qos, bool_t retain)
Specify the Will message.
MqttSnMsgType msgType
Message type.
const MqttSnPredefinedTopic * predefinedTopicTable
List of predefined topics.
MqttSnClientDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
General definitions for cryptographic algorithms.
size_t length
Length of the Will message payload.
error_t mqttSnClientSetGateway(MqttSnClientContext *context, const IpAddr *gwIpAddr, uint16_t gwPort)
Specify the address of the gateway.
error_t mqttSnClientSetTimeout(MqttSnClientContext *context, systime_t timeout)
Set communication timeout.
uint16_t msgId
Message identifier.
MQTT-SN message.
IP network address.
Definition: ip.h:57
MqttSnQosLevel
Quality of service level.
#define MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN
uint16_t msgId
TLS session state.
Definition: tls.h:1820
MqttSnClientMsgIdEntry msgIdTable[MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE]
MqttSnReturnCode returnCode
Status code returned by the gateway.
MqttSnMsgType
MQTT-SN message types.
uint8_t qos
Definition: mqtt_common.h:177
error_t mqttSnClientUnsubscribe(MqttSnClientContext *context, const char_t *topicName)
Unsubscribe from topic.
MqttSnClientState state
MQTT-SN client state.
#define Socket
Definition: socket.h:34
#define MqttSnClientContext
TlsContext * dtlsContext
DTLS context.
error_t mqttSnClientSetPredefinedTopics(MqttSnClientContext *context, MqttSnPredefinedTopic *predefinedTopics, uint_t size)
Set the list of predefined topics.
MqttSnMessage message
MQTT-SN message.
systime_t retransmitStartTime
Time at which the last message was sent.
uint8_t cleanSession
error_t mqttSnClientConnect(MqttSnClientContext *context, bool_t cleanSession)
Establish connection with the MQTT-SN gateway.
error_t mqttSnClientDisconnect(MqttSnClientContext *context)
Disconnect from the MQTT-SN gateway.
systime_t timeout
Timeout value.
char_t clientId[]
void(* MqttSnClientPublishCallback)(MqttSnClientContext *context, const char_t *topic, const uint8_t *message, size_t length, MqttSnQosLevel qos, bool_t retain)
PUBLISH message received callback.
Predefined topic.
error_t(* MqttSnClientDtlsInitCallback)(MqttSnClientContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
MqttSnClientState
MQTT-SN client states.
Definitions common to MQTT-SN client and gateway.
char_t topicName[]
error_t mqttSnClientSetTransportProtocol(MqttSnClientContext *context, MqttSnTransportProtocol transportProtocol)
Set the transport protocol to be used.
error_t mqttSnClientRegisterDtlsInitCallback(MqttSnClientContext *context, MqttSnClientDtlsInitCallback callback)
Register DTLS initialization callback function.
MqttSnTransportProtocol
MQTT-SN transport protocols.
error_t mqttSnClientSearchGateway(MqttSnClientContext *context, const IpAddr *destIpAddr, uint16_t destPort)
Search for a gateway.
MqttSnClientTopicEntry topicTable[MQTT_SN_CLIENT_TOPIC_TABLE_SIZE]
#define MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN
uint16_t topicId
Topic identifier returned by the gateway (REGACK/SUBACK)
__start_packed struct @235 MqttSnFlags
MQTT-SN flags.
error_t
Error codes.
Definition: error.h:40
uint8_t retain
Definition: mqtt_common.h:176
systime_t startTime
Start time.
Ipv4Addr destIpAddr
Definition: ipcp.h:76
MqttSnClientPublishCallback publishCallback
uint16_t gwPort
Gateway port number.
unsigned int uint_t
Definition: compiler_port.h:43
MqttSnClientWillMessage willMessage
Will message.
#define NetInterface
Definition: net.h:34
TlsSessionState dtlsSession
DTLS session state.
error_t mqttSnClientRegisterPublishCallback(MqttSnClientContext *context, MqttSnClientPublishCallback callback)
Register publish callback function.
MqttSnReturnCode
MQTT-SN return codes.
error_t mqttSnClientSetKeepAlive(MqttSnClientContext *context, systime_t keepAlive)
Set keep-alive value.
#define MQTT_SN_CLIENT_MAX_ID_LEN
uint8_t dup
Definition: mqtt_common.h:178
error_t mqttSnClientPing(MqttSnClientContext *context)
Send ping request.
uint16_t msgId
Message identifier.
QoS 2 message state.
IpAddr gwIpAddr
Gateway IP address.
error_t mqttSnClientUpdateWillMessage(MqttSnClientContext *context, const char_t *topic, const void *message, size_t length, MqttSnQosLevel qos, bool_t retain)
Update the Will message.
uint_t keepAliveCounter
PINGREQ retransmission counter.
error_t mqttSnClientTask(MqttSnClientContext *context, systime_t timeout)
Process MQTT-SN client events.
char_t clientId[MQTT_SN_CLIENT_MAX_ID_LEN+1]
Client identifier.
error_t mqttSnClientGetReturnCode(MqttSnClientContext *context, MqttSnReturnCode *returnCode)
Retrieve return code.
uint8_t length
Definition: dtls_misc.h:140
error_t mqttSnClientBindToInterface(MqttSnClientContext *context, NetInterface *interface)
Bind the MQTT-SN client to a particular network interface.
bool_t ownership
Ownership of message accepted by the client.
error_t mqttSnClientSubscribe(MqttSnClientContext *context, const char_t *topicName, MqttSnQosLevel qos)
Subscribe to topic.
NetInterface * interface
Underlying network interface.
#define TlsContext
Definition: tls.h:34
int bool_t
Definition: compiler_port.h:47
#define MQTT_SN_CLIENT_MSG_ID_TABLE_SIZE
uint16_t topicId
Topic identifier.