mqtt_sn_client.c
Go to the documentation of this file.
1 /**
2  * @file mqtt_sn_client.c
3  * @brief MQTT-SN client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL MQTT_SN_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "mqtt_sn/mqtt_sn_client.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (MQTT_SN_CLIENT_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Initialize MQTT-SN client context
48  * @param[in] context Pointer to the MQTT-SN client context
49  * @return Error code
50  **/
51 
53 {
54 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
55  error_t error;
56 #endif
57 
58  //Make sure the MQTT-SN client context is valid
59  if(context == NULL)
61 
62  //Clear MQTT-SN client context
63  osMemset(context, 0, sizeof(MqttSnClientContext));
64 
65  //Attach TCP/IP stack context
66  context->netContext = netGetDefaultContext();
67 
68 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
69  //Initialize DTLS session state
70  error = tlsInitSessionState(&context->dtlsSession);
71  //Any error to report?
72  if(error)
73  return error;
74 #endif
75 
76  //Initialize MQTT-SN client state
77  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
78 
79  //Default transport protocol
80  context->transportProtocol = MQTT_SN_TRANSPORT_PROTOCOL_UDP;
81  //Default timeout
82  context->timeout = MQTT_SN_CLIENT_DEFAULT_TIMEOUT;
83  //Default keep-alive time interval
84  context->keepAlive = MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE;
85 
86  //Initialize message identifier
87  context->msgId = 0;
88 
89  //Successful initialization
90  return NO_ERROR;
91 }
92 
93 
94 /**
95  * @brief Set the transport protocol to be used
96  * @param[in] context Pointer to the MQTT-SN client context
97  * @param[in] transportProtocol Transport protocol to be used (UDP or DTLS)
98  * @return Error code
99  **/
100 
102  MqttSnTransportProtocol transportProtocol)
103 {
104  //Make sure the MQTT-SN client context is valid
105  if(context == NULL)
107 
108  //Save the transport protocol to be used
109  context->transportProtocol = transportProtocol;
110 
111  //Successful processing
112  return NO_ERROR;
113 }
114 
115 
116 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
117 
118 /**
119  * @brief Register DTLS initialization callback function
120  * @param[in] context Pointer to the MQTT-SN client context
121  * @param[in] callback DTLS initialization callback function
122  * @return Error code
123  **/
124 
127 {
128  //Check parameters
129  if(context == NULL || callback == NULL)
131 
132  //Save callback function
133  context->dtlsInitCallback = callback;
134 
135  //Successful processing
136  return NO_ERROR;
137 }
138 
139 #endif
140 
141 
142 /**
143  * @brief Register publish callback function
144  * @param[in] context Pointer to the MQTT-SN client context
145  * @param[in] callback Callback function to be called when a PUBLISH message
146  * is received
147  * @return Error code
148  **/
149 
152 {
153  //Make sure the MQTT-SN client context is valid
154  if(context == NULL)
156 
157  //Save callback function
158  context->publishCallback = callback;
159 
160  //Successful processing
161  return NO_ERROR;
162 }
163 
164 
165 /**
166  * @brief Set the list of predefined topics
167  * @param[in] context Pointer to the MQTT-SN client context
168  * @param[in] predefinedTopics List of predefined topics
169  * @param[in] size Number of predefined topics
170  * @return Error code
171  **/
172 
174  MqttSnPredefinedTopic *predefinedTopics, uint_t size)
175 {
176  //Make sure the MQTT-SN client context is valid
177  if(context == NULL)
179 
180  //Check parameters
181  if(predefinedTopics == NULL && size != 0)
183 
184  //Save the list of predefined topics
185  context->predefinedTopicTable = predefinedTopics;
186  context->predefinedTopicTableSize = size;
187 
188  //Successful processing
189  return NO_ERROR;
190 }
191 
192 
193 /**
194  * @brief Set communication timeout
195  * @param[in] context Pointer to the MQTT-SN client context
196  * @param[in] timeout Timeout value, in milliseconds
197  * @return Error code
198  **/
199 
201 {
202  //Make sure the MQTT-SN client context is valid
203  if(context == NULL)
205 
206  //Save timeout value
207  context->timeout = timeout;
208 
209  //Successful processing
210  return NO_ERROR;
211 }
212 
213 
214 /**
215  * @brief Set keep-alive value
216  * @param[in] context Pointer to the MQTT-SN client context
217  * @param[in] keepAlive Keep-alive interval, in milliseconds
218  * @return Error code
219  **/
220 
222 {
223  //Make sure the MQTT-SN client context is valid
224  if(context == NULL)
226 
227  //Save keep-alive value
228  context->keepAlive = keepAlive;
229 
230  //Successful processing
231  return NO_ERROR;
232 }
233 
234 
235 /**
236  * @brief Set client identifier
237  * @param[in] context Pointer to the MQTT-SN client context
238  * @param[in] clientId NULL-terminated string containing the client identifier
239  * @return Error code
240  **/
241 
243  const char_t *clientId)
244 {
245  //Check parameters
246  if(context == NULL || clientId == NULL)
248 
249  //Make sure the length of the client identifier is acceptable
251  return ERROR_INVALID_LENGTH;
252 
253  //Save client identifier
254  osStrcpy(context->clientId, clientId);
255 
256  //Successful processing
257  return NO_ERROR;
258 }
259 
260 
261 /**
262  * @brief Specify the Will message
263  * @param[in] context Pointer to the MQTT-SN client context
264  * @param[in] topic Will topic name
265  * @param[in] message Will message
266  * @param[in] length Length of the Will message
267  * @param[in] qos QoS level to be used when publishing the Will message
268  * @param[in] retain This flag specifies if the Will message is to be retained
269  * @return Error code
270  **/
271 
273  const char_t *topic, const void *message, size_t length,
274  MqttSnQosLevel qos, bool_t retain)
275 {
276  //Check parameters
277  if(context == NULL || topic == NULL)
279 
280  //Make sure the length of the Will topic is acceptable
282  return ERROR_INVALID_LENGTH;
283 
284  //Save Will topic
285  osStrcpy(context->willMessage.topic, topic);
286 
287  //Any message payload
288  if(length > 0)
289  {
290  //Sanity check
291  if(message == NULL)
293 
294  //Make sure the length of the Will message payload is acceptable
296  return ERROR_INVALID_LENGTH;
297 
298  //Save Will message payload
299  osMemcpy(context->willMessage.payload, message, length);
300  }
301 
302  //Length of the Will message payload
303  context->willMessage.length = length;
304 
305  //QoS level to be used when publishing the Will message
306  context->willMessage.flags.qos = qos;
307  //This flag specifies if the Will message is to be retained
308  context->willMessage.flags.retain = retain;
309 
310  //Successful processing
311  return NO_ERROR;
312 }
313 
314 
315 /**
316  * @brief Bind the MQTT-SN client to a particular network interface
317  * @param[in] context Pointer to the MQTT-SN client context
318  * @param[in] interface Network interface to be used
319  * @return Error code
320  **/
321 
323  NetInterface *interface)
324 {
325  //Make sure the MQTT-SN client context is valid
326  if(context == NULL)
328 
329  //Explicitly associate the MQTT client with the specified interface
330  context->interface = interface;
331 
332  //Successful processing
333  return NO_ERROR;
334 }
335 
336 
337 /**
338  * @brief Specify the address of the gateway
339  * @param[in] context Pointer to the MQTT-SN client context
340  * @param[in] gwIpAddr Gateway IP address
341  * @param[in] gwPort Gateway port number
342  * @return Error code
343  **/
344 
346  const IpAddr *gwIpAddr, uint16_t gwPort)
347 {
348  //Check parameters
349  if(context == NULL || gwIpAddr == NULL)
351 
352  //Save the IP address and the port number of the MQTT-SN gateway
353  context->gwIpAddr = *gwIpAddr;
354  context->gwPort = gwPort;
355 
356  //Successful processing
357  return NO_ERROR;
358 }
359 
360 
361 /**
362  * @brief Search for a gateway
363  * @param[in] context Pointer to the MQTT-SN client context
364  * @param[in] destIpAddr Destination IP address
365  * @param[in] destPort Destination port number
366  * @return Error code
367  **/
368 
370  const IpAddr *destIpAddr, uint16_t destPort)
371 {
372  error_t error;
373  systime_t time;
374 
375  //Check parameters
376  if(context == NULL || destIpAddr == NULL)
378 
379  //Initialize status code
380  error = NO_ERROR;
381 
382  //Gateway discovery procedure
383  while(!error)
384  {
385  //Get current time
386  time = osGetSystemTime();
387 
388  //Check current state
389  if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTED)
390  {
391  //Open network connection
392  error = mqttSnClientOpenConnection(context, FALSE);
393 
394  //Check status code
395  if(!error)
396  {
397  //Save current time
398  context->startTime = time;
399  context->retransmitStartTime = time;
400 
401  //To prevent broadcast storms when multiple clients start searching
402  //for GW almost at the same time, the sending of the SEARCHGW
403  //message is delayed by a random time between 0 and TSEARCHGW
404  context->retransmitTimeout = netGetRandRange(context->netContext,
406 
407  //Start searching for gateways
408  context->state = MQTT_SN_CLIENT_STATE_SEARCHING;
409  }
410  }
411  else if(context->state == MQTT_SN_CLIENT_STATE_SEARCHING)
412  {
413  //Check whether the timeout has elapsed
414  if(timeCompare(time, context->startTime + context->timeout) >= 0)
415  {
416  //Abort the retransmission procedure
417  error = ERROR_TIMEOUT;
418  }
419  else if(timeCompare(time, context->retransmitStartTime +
420  context->retransmitTimeout) >= 0)
421  {
422  //Set retransmission timeout
423  context->retransmitTimeout = MQTT_SN_CLIENT_RETRY_TIMEOUT;
424 
425  //If the retry timer times out and the expected gateway's reply
426  //is not received, the client retransmits the message
427  error = mqttSnClientSendSearchGw(context, 0, destIpAddr, destPort);
428  }
429  else
430  {
431  //Wait for the gateway's reply
433  }
434  }
435  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
436  {
437  //Check the type of the received message
438  if(context->msgType == MQTT_SN_MSG_TYPE_GWINFO)
439  {
440  //Close network connection
442 
443  //A MQTT-SN gateway has been found
444  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
445  break;
446  }
447  else
448  {
449  //Report an error
451  }
452  }
453  else
454  {
455  //Invalid state
456  error = ERROR_WRONG_STATE;
457  }
458  }
459 
460  //Any error to report?
461  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
462  {
463  //Clean up side effects
465  //Update MQTT-SN client state
466  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
467  }
468 
469  //Return status code
470  return error;
471 }
472 
473 
474 /**
475  * @brief Establish connection with the MQTT-SN gateway
476  * @param[in] context Pointer to the MQTT-SN client context
477  * @param[in] cleanSession If this flag is set, then the client and server
478  * must discard any previous session and start a new one
479  * @return Error code
480  **/
481 
483 {
484  error_t error;
485  systime_t time;
486 
487  //Make sure the MQTT-SN client context is valid
488  if(context == NULL)
490 
491  //Initialize status code
492  error = NO_ERROR;
493 
494  //Establish connection with the MQTT-SN gateway
495  while(!error)
496  {
497  //Get current time
498  time = osGetSystemTime();
499 
500  //Check current state
501  if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTED)
502  {
503  //Open network connection
504  error = mqttSnClientOpenConnection(context, TRUE);
505 
506  //Check status code
507  if(!error)
508  {
509  //Save current time
510  context->startTime = time;
511  //Update MQTT-SN client state
512  context->state = MQTT_SN_CLIENT_STATE_CONNECTING;
513  }
514  }
515  else if(context->state == MQTT_SN_CLIENT_STATE_CONNECTING)
516  {
517  //Establish DTLS connection
518  error = mqttSnClientEstablishConnection(context);
519 
520  //Check status code
521  if(error == NO_ERROR)
522  {
523  //Check whether the CleanSession flag is set
524  if(cleanSession)
525  {
526  //Discard previous session state
527  osMemset(context->topicTable, 0, sizeof(context->topicTable));
528  osMemset(context->msgIdTable, 0, sizeof(context->msgIdTable));
529  }
530 
531  //The CONNECT message is sent by a client to setup a connection
532  error = mqttSnClientSendConnect(context, cleanSession);
533  }
534  else if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
535  {
536  //Check whether the timeout has elapsed
537  if(timeCompare(time, context->startTime + context->timeout) >= 0)
538  {
539  //Report an error
540  error = ERROR_TIMEOUT;
541  }
542  }
543  else
544  {
545  //Failed to establish DTLS connection
546  }
547  }
548  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
549  {
550  //Check whether the timeout has elapsed
551  if(timeCompare(time, context->startTime + context->timeout) >= 0)
552  {
553  //Abort the retransmission procedure
554  error = ERROR_TIMEOUT;
555  }
556  else if(timeCompare(time, context->retransmitStartTime +
558  {
559  //If the retry timer times out and the expected gateway's reply
560  //is not received, the client retransmits the message
561  error = mqttSnClientSendConnect(context, cleanSession);
562  }
563  else
564  {
565  //Wait for the gateway's reply
567  }
568  }
569  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
570  {
571  //Check the type of the received message
572  if(context->msgType == MQTT_SN_MSG_TYPE_CONNACK)
573  {
574  //If the connection request has not been accepted, the failure reason
575  //is encoded in the return code field of the CONNACK message
576  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
577  {
578  //The connection request has been accepted by the gateway
579  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
580  }
581  else
582  {
583  //Terminate DTLS connection
585 
586  //The connection request has been rejected by the gateway
587  error = ERROR_REQUEST_REJECTED;
588  }
589  }
590  else
591  {
592  //Report an error
594  }
595  }
596  else if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
597  {
598  //The MQTT-SN client is connected
599  break;
600  }
601  else
602  {
603  //Invalid state
604  error = ERROR_WRONG_STATE;
605  }
606  }
607 
608  //Any error to report?
609  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
610  {
611  //Clean up side effects
613  //Update MQTT-SN client state
614  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
615  }
616 
617  //Return status code
618  return error;
619 }
620 
621 
622 /**
623  * @brief Publish message
624  * @param[in] context Pointer to the MQTT-SN client context
625  * @param[in] topicName Topic name
626  * @param[in] message Message payload
627  * @param[in] length Length of the message payload
628  * @param[in] qos QoS level to be used when publishing the message
629  * @param[in] retain This flag specifies if the message is to be retained
630  * @param[in] dup This flag specifies if the message is sent for the first
631  * time or if the message is retransmitted
632  * @param[in,out] msgId Message identifier used to send the PUBLISH message
633  * @return Error code
634  **/
635 
637  const char_t *topicName, const void *message, size_t length,
638  MqttSnQosLevel qos, bool_t retain, bool_t dup, uint16_t *msgId)
639 {
640  error_t error;
641  systime_t time;
642  uint16_t publishMsgId;
643 
644  //Check parameters
645  if(context == NULL || topicName == NULL)
647  if(message == NULL && length != 0)
649  if(dup && msgId == NULL)
651 
652  //Initialize status code
653  error = NO_ERROR;
654 
655  //Initialize message identifier
656  if(dup)
657  {
658  publishMsgId = *msgId;
659  }
660  else
661  {
662  publishMsgId = 0;
663  }
664 
665  //Publish procedure
666  while(!error)
667  {
668  //Get current time
669  time = osGetSystemTime();
670 
671  //Check current state
672  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
673  {
674  //Save current time
675  context->startTime = time;
676 
677  //Check whether the register procedure is needed
679  mqttSnClientFindTopicName(context, topicName) == 0 &&
681  {
682  //The message identifier allows the sender to match a message with
683  //its corresponding acknowledgment
685 
686  //To register a topic name a client sends a REGISTER message to
687  //the gateway
688  error = mqttSnClientSendRegister(context, topicName);
689  }
690  else
691  {
692  //The message ID is only relevant in case of QoS levels 1 and 2
694  {
695  //The message identifier allows the sender to match a message with
696  //its corresponding acknowledgment
697  if(!dup)
698  publishMsgId = mqttSnClientGenerateMessageId(context);
699  }
700  else
701  {
702  //For QoS level 0, the message identifier is coded 0x0000
703  publishMsgId = 0;
704  }
705 
706  //The client can start publishing data relating to the registered
707  //topic name by sending PUBLISH messages to the gateway
708  error = mqttSnClientSendPublish(context, publishMsgId, topicName,
709  message, length, qos, retain, dup);
710 
711  //In the QoS 0, no response is sent by the receiver and no retry
712  //is performed by the sender
714  break;
715  }
716  }
717  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
718  {
719  //Check whether the transmission of the PUBLISH message has started
720  if(context->msgType == MQTT_SN_MSG_TYPE_PUBLISH ||
721  context->msgType == MQTT_SN_MSG_TYPE_PUBREL)
722  {
723  //Restore the message identifier that was used to send the first
724  //PUBLISH message
725  if(!dup)
726  publishMsgId = context->msgId;
727  }
728 
729  //Check whether the timeout has elapsed
730  if(timeCompare(time, context->startTime + context->timeout) >= 0)
731  {
732  //Abort the retransmission procedure
733  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
734  //Report a timeout error
735  error = ERROR_TIMEOUT;
736  }
737  else if(timeCompare(time, context->retransmitStartTime +
739  {
740  //If the retry timer times out and the expected gateway's reply
741  //is not received, the client retransmits the message
742  if(context->msgType == MQTT_SN_MSG_TYPE_REGISTER)
743  {
744  //Retransmit REGISTER message
745  error = mqttSnClientSendRegister(context, topicName);
746  }
747  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBLISH)
748  {
749  //Retransmit PUBLISH message
750  error = mqttSnClientSendPublish(context, publishMsgId,
751  topicName, message, length, qos, retain, TRUE);
752  }
753  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBREL)
754  {
755  //Retransmit PUBREL message
756  error = mqttSnClientSendPubRel(context, context->msgId);
757  }
758  else
759  {
760  //Report an error
761  error = ERROR_INVALID_TYPE;
762  }
763  }
764  else
765  {
766  //Wait for the gateway's reply
768  }
769  }
770  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
771  {
772  //Update MQTT-SN client state
773  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
774 
775  //Check whether the transmission of the PUBLISH message has started
776  if(context->msgType == MQTT_SN_MSG_TYPE_PUBACK ||
777  context->msgType == MQTT_SN_MSG_TYPE_PUBREC ||
778  context->msgType == MQTT_SN_MSG_TYPE_PUBCOMP)
779  {
780  //Restore the message identifier that was used to send the first
781  //PUBLISH message
782  if(!dup)
783  publishMsgId = context->msgId;
784  }
785 
786  //Check the type of the received message
787  if(context->msgType == MQTT_SN_MSG_TYPE_REGACK)
788  {
789  //If the registration has not been accepted, the failure reason is
790  //encoded in the return code field of the REGACK message
791  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
792  {
793  //Save the topic ID assigned by the gateway
794  error = mqttSnClientAddTopic(context, topicName, context->topicId);
795  }
796  else
797  {
798  //The registration request has been rejected by the gateway
799  error = ERROR_REQUEST_REJECTED;
800  }
801  }
802  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBACK)
803  {
804  //If the publish request has not been accepted, the failure reason
805  //is encoded in the return code field of the PUBACK message
806  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
807  {
808  //Check QoS level
809  if(qos == MQTT_SN_QOS_LEVEL_2)
810  {
811  //Unexpected PUBREC message received
812  error = ERROR_UNEXPECTED_MESSAGE;
813  }
814  else
815  {
816  //A PUBACK message has been received
817  break;
818  }
819  }
820  else
821  {
822  //The publish request has been rejected by the gateway
823  error = ERROR_REQUEST_REJECTED;
824  }
825  }
826  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBREC)
827  {
828  //Check QoS level
829  if(qos == MQTT_SN_QOS_LEVEL_2)
830  {
831  //A PUBREL packet is the response to a PUBREC packet. It is the
832  //third packet of the QoS 2 protocol exchange
833  error = mqttSnClientSendPubRel(context, context->msgId);
834  }
835  else
836  {
837  //Unexpected PUBREC message received
838  error = ERROR_UNEXPECTED_MESSAGE;
839  }
840  }
841  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBCOMP)
842  {
843  //A PUBCOMP message has been received
844  break;
845  }
846  else
847  {
848  //Report an error
850  }
851  }
852  else
853  {
854  //Invalid state
855  error = ERROR_NOT_CONNECTED;
856  }
857  }
858 
859  //Return the message identifier that was used to send the PUBLISH message
860  if(msgId != NULL)
861  *msgId = publishMsgId;
862 
863  //Return status code
864  return error;
865 }
866 
867 
868 /**
869  * @brief Subscribe to topic
870  * @param[in] context Pointer to the MQTT-SN client context
871  * @param[in] topicName Topic filter
872  * @param[in] qos Maximum QoS level at which the server can send application
873  * messages to the client
874  * @return Error code
875  **/
876 
879 {
880  error_t error;
881  systime_t time;
882 
883  //Check parameters
884  if(context == NULL || topicName == NULL)
886 
887  //Initialize status code
888  error = NO_ERROR;
889 
890  //Topic subscribe procedure
891  while(!error)
892  {
893  //Get current time
894  time = osGetSystemTime();
895 
896  //Check current state
897  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
898  {
899  //The message identifier allows the sender to match a message with
900  //its corresponding acknowledgment
902 
903  //Save current time
904  context->startTime = time;
905 
906  //Send SUBSCRIBE message
907  error = mqttSnClientSendSubscribe(context, topicName, qos);
908  }
909  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
910  {
911  //Check whether the timeout has elapsed
912  if(timeCompare(time, context->startTime + context->timeout) >= 0)
913  {
914  //Abort the retransmission procedure
915  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
916  //Report a timeout error
917  error = ERROR_TIMEOUT;
918  }
919  else if(timeCompare(time, context->retransmitStartTime +
921  {
922  //If the retry timer times out and the expected gateway's reply
923  //is not received, the client retransmits the message
924  error = mqttSnClientSendSubscribe(context, topicName, qos);
925  }
926  else
927  {
928  //Wait for the gateway's reply
930  }
931  }
932  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
933  {
934  //Update MQTT-SN client state
935  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
936 
937  //Check the type of the received message
938  if(context->msgType == MQTT_SN_MSG_TYPE_SUBACK)
939  {
940  //If the subscribe request has not been accepted, the failure reason
941  //is encoded in the return code field of the SUBACK message
942  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
943  {
944  //The topic ID field is not relevant in case of subscriptions to a
945  //topic name which contains wildcard characters
946  if(osStrchr(topicName, '#') == NULL && osStrchr(topicName, '+') == NULL)
947  {
948  //Save the topic ID assigned by the gateway
949  error = mqttSnClientAddTopic(context, topicName, context->topicId);
950  }
951 
952  //A SUBACK message has been received
953  break;
954  }
955  else
956  {
957  //The subscribe request has been rejected by the gateway
958  error = ERROR_REQUEST_REJECTED;
959  }
960  }
961  else
962  {
963  //Report an error
965  }
966  }
967  else
968  {
969  //Invalid state
970  error = ERROR_NOT_CONNECTED;
971  }
972  }
973 
974  //Return status code
975  return error;
976 }
977 
978 
979 /**
980  * @brief Unsubscribe from topic
981  * @param[in] context Pointer to the MQTT-SN client context
982  * @param[in] topicName Topic filter
983  * @return Error code
984  **/
985 
987  const char_t *topicName)
988 {
989  error_t error;
990  systime_t time;
991 
992  //Check parameters
993  if(context == NULL || topicName == NULL)
995 
996  //Initialize status code
997  error = NO_ERROR;
998 
999  //Topic unsubscribe procedure
1000  while(!error)
1001  {
1002  //Get current time
1003  time = osGetSystemTime();
1004 
1005  //Check current state
1006  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1007  {
1008  //The message identifier allows the sender to match a message with
1009  //its corresponding acknowledgment
1011 
1012  //Save current time
1013  context->startTime = time;
1014 
1015  //Send UNSUBSCRIBE message
1016  error = mqttSnClientSendUnsubscribe(context, topicName);
1017  }
1018  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1019  {
1020  //Check whether the timeout has elapsed
1021  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1022  {
1023  //Abort the retransmission procedure
1024  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1025  //Report a timeout error
1026  error = ERROR_TIMEOUT;
1027  }
1028  else if(timeCompare(time, context->retransmitStartTime +
1030  {
1031  //If the retry timer times out and the expected gateway's reply
1032  //is not received, the client retransmits the message
1033  error = mqttSnClientSendUnsubscribe(context, topicName);
1034  }
1035  else
1036  {
1037  //Wait for the gateway's reply
1039  }
1040  }
1041  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1042  {
1043  //Update MQTT-SN client state
1044  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1045 
1046  //Check the type of the received message
1047  if(context->msgType == MQTT_SN_MSG_TYPE_UNSUBACK)
1048  {
1049  //An UNSUBACK message has been received
1050  break;
1051  }
1052  else
1053  {
1054  //Report an error
1055  error = ERROR_UNEXPECTED_RESPONSE;
1056  }
1057  }
1058  else
1059  {
1060  //Invalid state
1061  error = ERROR_NOT_CONNECTED;
1062  }
1063  }
1064 
1065  //Return status code
1066  return error;
1067 }
1068 
1069 
1070 /**
1071  * @brief Send ping request
1072  * @param[in] context Pointer to the MQTT-SN client context
1073  * @return Error code
1074  **/
1075 
1077 {
1078  error_t error;
1079  systime_t time;
1080 
1081  //Make sure the MQTT-SN client context is valid
1082  if(context == NULL)
1083  return ERROR_INVALID_PARAMETER;
1084 
1085  //Initialize status code
1086  error = NO_ERROR;
1087 
1088  //Send PINGREQ packet and wait for PINGRESP packet to be received
1089  while(!error)
1090  {
1091  //Get current time
1092  time = osGetSystemTime();
1093 
1094  //Check current state
1095  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1096  {
1097  //Save current time
1098  context->startTime = time;
1099  context->retransmitStartTime = time;
1100 
1101  //Send PINGREQ message
1102  error = mqttSnClientSendPingReq(context);
1103 
1104  //Update MQTT-SN client state
1105  context->state = MQTT_SN_CLIENT_STATE_SENDING_REQ;
1106  context->msgType = MQTT_SN_MSG_TYPE_PINGREQ;
1107  }
1108  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1109  {
1110  //Check whether the timeout has elapsed
1111  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1112  {
1113  //Abort the retransmission procedure
1114  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1115  //Report a timeout error
1116  error = ERROR_TIMEOUT;
1117  }
1118  else if(timeCompare(time, context->retransmitStartTime +
1120  {
1121  //If the retry timer times out and the expected gateway's reply
1122  //is not received, the client retransmits the message
1123  error = mqttSnClientSendPingReq(context);
1124 
1125  //Save the time at which the message was sent
1126  context->retransmitStartTime = time;
1127  }
1128  else
1129  {
1130  //Wait for the gateway's reply
1132  }
1133  }
1134  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1135  {
1136  //Update MQTT-SN client state
1137  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1138 
1139  //Check the type of the received message
1140  if(context->msgType == MQTT_SN_MSG_TYPE_PINGRESP)
1141  {
1142  //A PINGRESP message has been received
1143  break;
1144  }
1145  else
1146  {
1147  //Report an error
1148  error = ERROR_UNEXPECTED_RESPONSE;
1149  }
1150  }
1151  else
1152  {
1153  //Invalid state
1154  error = ERROR_NOT_CONNECTED;
1155  }
1156  }
1157 
1158  //Return status code
1159  return error;
1160 }
1161 
1162 
1163 /**
1164  * @brief Update the Will message
1165  * @param[in] context Pointer to the MQTT-SN client context
1166  * @param[in] topic Will topic name
1167  * @param[in] message Will message
1168  * @param[in] length Length of the Will message
1169  * @param[in] qos QoS level to be used when publishing the Will message
1170  * @param[in] retain This flag specifies if the Will message is to be retained
1171  * @return Error code
1172  **/
1173 
1175  const char_t *topic, const void *message, size_t length,
1176  MqttSnQosLevel qos, bool_t retain)
1177 {
1178  error_t error;
1179  systime_t time;
1180 
1181  //Check parameters
1182  if(context == NULL || topic == NULL)
1183  return ERROR_INVALID_PARAMETER;
1184  if(message == NULL && length != 0)
1185  return ERROR_INVALID_PARAMETER;
1186 
1187  //Initialize status code
1188  error = NO_ERROR;
1189 
1190  //Publish procedure
1191  while(!error)
1192  {
1193  //Get current time
1194  time = osGetSystemTime();
1195 
1196  //Check current state
1197  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1198  {
1199  //Update the Will message
1200  error = mqttSnClientSetWillMessage(context, topic, message, length,
1201  qos, retain);
1202 
1203  //Check status code
1204  if(!error)
1205  {
1206  //Save current time
1207  context->startTime = time;
1208 
1209  //Send WILLTOPICUPD message
1210  error = mqttSnClientSendWillTopicUpd(context);
1211  }
1212  }
1213  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1214  {
1215  //Check whether the timeout has elapsed
1216  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1217  {
1218  //Abort the retransmission procedure
1219  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1220  //Report a timeout error
1221  error = ERROR_TIMEOUT;
1222  }
1223  else if(timeCompare(time, context->retransmitStartTime +
1225  {
1226  //If the retry timer times out and the expected gateway's reply
1227  //is not received, the client retransmits the message
1228  if(context->msgType == MQTT_SN_MSG_TYPE_WILLTOPICUPD)
1229  {
1230  //Retransmit WILLTOPICUPD message
1231  error = mqttSnClientSendWillTopicUpd(context);
1232  }
1233  else if(context->msgType == MQTT_SN_MSG_TYPE_WILLMSGUPD)
1234  {
1235  //Retransmit WILLMSGUPD message
1236  error = mqttSnClientSendWillMsgUpd(context);
1237  }
1238  else
1239  {
1240  //Report an error
1241  error = ERROR_INVALID_TYPE;
1242  }
1243  }
1244  else
1245  {
1246  //Wait for the gateway's reply
1248  }
1249  }
1250  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1251  {
1252  //Update MQTT-SN client state
1253  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1254 
1255  //Check the type of the received message
1256  if(context->msgType == MQTT_SN_MSG_TYPE_WILLTOPICRESP)
1257  {
1258  //If the WILLTOPICUPD has not been accepted, the failure reason
1259  //is encoded in the return code field of the WILLTOPICRESP
1260  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
1261  {
1262  //Valid Will topic?
1263  if(context->willMessage.topic[0] != '\0')
1264  {
1265  //Send WILLMSGUPD message
1266  error = mqttSnClientSendWillMsgUpd(context);
1267  }
1268  else
1269  {
1270  //An empty WILLTOPIC message is used by a client to delete
1271  //the Will topic and the Will message stored in the server
1272  break;
1273  }
1274  }
1275  else
1276  {
1277  //The WILLTOPICUPD request has been rejected by the gateway
1278  error = ERROR_REQUEST_REJECTED;
1279  }
1280  }
1281  else if(context->msgType == MQTT_SN_MSG_TYPE_WILLMSGRESP)
1282  {
1283  //If the WILLMSGUPD has not been accepted, the failure reason
1284  //is encoded in the return code field of the WILLMSGRESP
1285  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
1286  {
1287  //The WILLMSGUPD request has been accepted by the gateway
1288  break;
1289  }
1290  else
1291  {
1292  //The WILLMSGUPD request has been rejected by the gateway
1293  error = ERROR_REQUEST_REJECTED;
1294  }
1295  }
1296  else
1297  {
1298  //Report an error
1299  error = ERROR_UNEXPECTED_RESPONSE;
1300  }
1301  }
1302  else
1303  {
1304  //Invalid state
1305  error = ERROR_NOT_CONNECTED;
1306  }
1307  }
1308 
1309  //Return status code
1310  return error;
1311 }
1312 
1313 
1314 /**
1315  * @brief Retrieve return code
1316  * @param[in] context Pointer to the MQTT-SN client context
1317  * @param[out] returnCode Return code
1318  * @return Error code
1319  **/
1320 
1323 {
1324  //Check parameters
1325  if(context == NULL || returnCode == NULL)
1326  return ERROR_INVALID_PARAMETER;
1327 
1328  //Retrieve return code
1329  *returnCode = context->returnCode;
1330 
1331  //Successful processing
1332  return NO_ERROR;
1333 }
1334 
1335 
1336 /**
1337  * @brief Process MQTT-SN client events
1338  * @param[in] context Pointer to the MQTT-SN client context
1339  * @param[in] timeout Maximum time to wait before returning
1340  * @return Error code
1341  **/
1342 
1344 {
1345  error_t error;
1346 
1347  //Make sure the MQTT-SN client context is valid
1348  if(context == NULL)
1349  return ERROR_INVALID_PARAMETER;
1350 
1351  //Make sure the MQTT-SN client is connected
1352  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE ||
1353  context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ ||
1354  context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1355  {
1356  //Process MQTT-SN client events
1357  error = mqttSnClientProcessEvents(context, timeout);
1358  }
1359  else
1360  {
1361  //Invalid state
1362  error = ERROR_NOT_CONNECTED;
1363  }
1364 
1365  //Return status code
1366  return error;
1367 }
1368 
1369 
1370 /**
1371  * @brief Disconnect from the MQTT-SN gateway
1372  * @param[in] context Pointer to the MQTT-SN client context
1373  * @param[in] duration Sleep duration, in milliseconds
1374  * @return Error code
1375  **/
1376 
1379 {
1380  error_t error;
1381  systime_t time;
1382 
1383  //Make sure the MQTT-SN client context is valid
1384  if(context == NULL)
1385  return ERROR_INVALID_PARAMETER;
1386 
1387  //Initialize status code
1388  error = NO_ERROR;
1389 
1390  //Disconnect procedure
1391  while(!error)
1392  {
1393  //Get current time
1394  time = osGetSystemTime();
1395 
1396  //Check current state
1397  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1398  {
1399  //Save current time
1400  context->startTime = time;
1401 
1402  //The DISCONNECT message is sent by a client to indicate that it
1403  //wants to close the connection
1404  error = mqttSnClientSendDisconnect(context, duration / 1000);
1405  }
1406  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1407  {
1408  //Check whether the timeout has elapsed
1409  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1410  {
1411  //Terminate DTLS connection
1413 
1414  //Report a timeout error
1415  error = ERROR_TIMEOUT;
1416  }
1417  else if(timeCompare(time, context->retransmitStartTime +
1419  {
1420  //If the retry timer times out and the expected gateway's reply
1421  //is not received, the client retransmits the message
1422  error = mqttSnClientSendDisconnect(context, duration / 1000);
1423  }
1424  else
1425  {
1426  //Wait for the gateway's reply
1428  }
1429  }
1430  else if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTING)
1431  {
1432  //Terminate DTLS connection
1433  error = mqttSnClientShutdownConnection(context);
1434  //Close network connection
1435  mqttSnClientCloseConnection(context);
1436 
1437  //The connection is closed
1438  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
1439  }
1440  else if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTED)
1441  {
1442  //The MQTT-SN client is disconnected
1443  break;
1444  }
1445  else
1446  {
1447  //Invalid state
1448  error = ERROR_WRONG_STATE;
1449  }
1450  }
1451 
1452  //Any error to report?
1453  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
1454  {
1455  //Close network connection
1456  mqttSnClientCloseConnection(context);
1457  //Update MQTT-SN client state
1458  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
1459  }
1460 
1461  //Return status code
1462  return error;
1463 }
1464 
1465 
1466 /**
1467  * @brief Release MQTT-SN client context
1468  * @param[in] context Pointer to the MQTT-SN client context
1469  **/
1470 
1472 {
1473  //Make sure the MQTT-SN client context is valid
1474  if(context != NULL)
1475  {
1476  //Close connection
1477  mqttSnClientCloseConnection(context);
1478 
1479 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
1480  //Release DTLS session state
1481  tlsFreeSessionState(&context->dtlsSession);
1482 #endif
1483 
1484  //Clear MQTT-SN client context
1485  osMemset(context, 0, sizeof(MqttSnClientContext));
1486  }
1487 }
1488 
1489 #endif
error_t mqttSnClientDisconnect(MqttSnClientContext *context, systime_t duration)
Disconnect from the MQTT-SN gateway.
error_t mqttSnClientUpdateWillMessage(MqttSnClientContext *context, const char_t *topic, const void *message, size_t length, MqttSnQosLevel qos, bool_t retain)
Update the Will message.
error_t mqttSnClientProcessEvents(MqttSnClientContext *context, systime_t timeout)
Process MQTT-SN client events.
void mqttSnClientDeinit(MqttSnClientContext *context)
Release MQTT-SN client context.
#define osStrchr(s, c)
Definition: os_port.h:198
error_t mqttSnClientSendPubRel(MqttSnClientContext *context, uint16_t msgId)
Send PUBREL message.
int bool_t
Definition: compiler_port.h:63
@ MQTT_SN_MSG_TYPE_PINGRESP
@ MQTT_SN_MSG_TYPE_REGISTER
@ ERROR_WOULD_BLOCK
Definition: error.h:96
Predefined topic.
IP network address.
Definition: ip.h:90
@ MQTT_SN_MSG_TYPE_PINGREQ
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
error_t mqttSnClientSendDisconnect(MqttSnClientContext *context, uint16_t duration)
Send DISCONNECT message.
uint32_t netGetRandRange(NetContext *context, uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:473
error_t(* MqttSnClientDtlsInitCallback)(MqttSnClientContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
uint8_t message[]
Definition: chap.h:154
error_t mqttSnClientSendRegister(MqttSnClientContext *context, const char_t *topicName)
Send REGISTER message.
#define MqttSnClientContext
#define TRUE
Definition: os_port.h:50
error_t mqttSnClientTask(MqttSnClientContext *context, systime_t timeout)
Process MQTT-SN client events.
@ MQTT_SN_QOS_LEVEL_1
At least once delivery.
MqttSnReturnCode
MQTT-SN return codes.
error_t mqttSnClientSendSubscribe(MqttSnClientContext *context, const char_t *topicName, MqttSnQosLevel qos)
Send SUBSCRIBE message.
MqttSnQosLevel
Quality of service level.
error_t mqttSnClientGetReturnCode(MqttSnClientContext *context, MqttSnReturnCode *returnCode)
Retrieve return code.
error_t mqttSnClientSendSearchGw(MqttSnClientContext *context, uint8_t radius, const IpAddr *destIpAddr, uint16_t destPort)
Send SEARCHGW message.
@ MQTT_SN_MSG_TYPE_GWINFO
error_t mqttSnClientSendUnsubscribe(MqttSnClientContext *context, const char_t *topicName)
Send UNSUBSCRIBE message.
error_t mqttSnClientSubscribe(MqttSnClientContext *context, const char_t *topicName, MqttSnQosLevel qos)
Subscribe to topic.
uint16_t msgId
@ MQTT_SN_MSG_TYPE_WILLTOPICRESP
uint16_t destPort
Definition: tcp.h:347
uint8_t qos
Definition: mqtt_common.h:181
#define osStrlen(s)
Definition: os_port.h:168
MQTT-SN message formatting and parsing.
error_t mqttSnClientSetPredefinedTopics(MqttSnClientContext *context, MqttSnPredefinedTopic *predefinedTopics, uint_t size)
Set the list of predefined topics.
@ MQTT_SN_MSG_TYPE_PUBREL
#define MQTT_SN_CLIENT_SEARCH_DELAY
uint8_t returnCode
#define MQTT_SN_CLIENT_MAX_WILL_PAYLOAD_LEN
error_t mqttSnClientSetKeepAlive(MqttSnClientContext *context, systime_t keepAlive)
Set keep-alive value.
@ MQTT_SN_CLIENT_STATE_SENDING_REQ
error_t mqttSnClientSetIdentifier(MqttSnClientContext *context, const char_t *clientId)
Set client identifier.
#define timeCompare(t1, t2)
Definition: os_port.h:40
void tlsFreeSessionState(TlsSessionState *session)
Properly dispose a session state.
Definition: tls.c:3065
#define MQTT_SN_CLIENT_MAX_ID_LEN
error_t mqttSnClientSendPingReq(MqttSnClientContext *context)
Send PINGREQ message.
uint16_t mqttSnClientFindTopicName(MqttSnClientContext *context, const char_t *topicName)
Retrieve the topic ID associated with a given topic name.
@ MQTT_SN_CLIENT_STATE_SEARCHING
@ ERROR_WRONG_STATE
Definition: error.h:210
error_t mqttSnClientOpenConnection(MqttSnClientContext *context, bool_t secure)
Open network connection.
error_t mqttSnClientAddTopic(MqttSnClientContext *context, const char_t *topicName, uint16_t topicId)
Add a new entry to the topic table.
#define MQTT_SN_CLIENT_TICK_INTERVAL
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.
#define FALSE
Definition: os_port.h:46
error_t mqttSnClientSetWillMessage(MqttSnClientContext *context, const char_t *topic, const void *message, size_t length, MqttSnQosLevel qos, bool_t retain)
Specify the Will message.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define MQTT_SN_CLIENT_RETRY_TIMEOUT
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
error_t mqttSnClientBindToInterface(MqttSnClientContext *context, NetInterface *interface)
Bind the MQTT-SN client to a particular network interface.
@ MQTT_SN_MSG_TYPE_PUBREC
uint16_t mqttSnClientGenerateMessageId(MqttSnClientContext *context)
Generate a new message identifier.
@ MQTT_SN_TRANSPORT_PROTOCOL_UDP
UDP protocol.
@ MQTT_SN_MSG_TYPE_WILLTOPICUPD
error_t mqttSnClientSendWillMsgUpd(MqttSnClientContext *context)
Send WILLMSGUPD message.
#define NetInterface
Definition: net.h:40
@ MQTT_SN_MSG_TYPE_PUBLISH
@ MQTT_SN_MSG_TYPE_WILLMSGRESP
bool_t mqttSnClientIsShortTopicName(const char_t *topicName)
Check whether a topic name is a short topic name.
@ MQTT_SN_MSG_TYPE_PUBCOMP
@ ERROR_INVALID_LENGTH
Definition: error.h:111
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
error_t mqttSnClientEstablishConnection(MqttSnClientContext *context)
Establish network connection.
error_t mqttSnClientUnsubscribe(MqttSnClientContext *context, const char_t *topicName)
Unsubscribe from topic.
error_t mqttSnClientSetTimeout(MqttSnClientContext *context, systime_t timeout)
Set communication timeout.
#define MQTT_SN_CLIENT_DEFAULT_KEEP_ALIVE
@ MQTT_SN_CLIENT_STATE_DISCONNECTED
error_t mqttSnClientSendPublish(MqttSnClientContext *context, uint16_t msgId, const char_t *topicName, const uint8_t *data, size_t length, MqttSnQosLevel qos, bool_t retain, bool_t dup)
Send PUBLISH message.
@ ERROR_INVALID_TYPE
Definition: error.h:115
@ ERROR_UNEXPECTED_RESPONSE
Definition: error.h:70
uint8_t length
Definition: tcp.h:375
@ MQTT_SN_CLIENT_STATE_RESP_RECEIVED
error_t mqttSnClientSetTransportProtocol(MqttSnClientContext *context, MqttSnTransportProtocol transportProtocol)
Set the transport protocol to be used.
@ MQTT_SN_CLIENT_STATE_DISCONNECTING
void mqttSnClientCloseConnection(MqttSnClientContext *context)
Close network connection.
Helper functions for MQTT-SN client.
@ MQTT_SN_MSG_TYPE_REGACK
error_t mqttSnClientShutdownConnection(MqttSnClientContext *context)
Shutdown network connection.
uint32_t systime_t
System time.
uint16_t duration
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
uint32_t time
Transport protocol abstraction layer.
@ MQTT_SN_MSG_TYPE_WILLMSGUPD
error_t mqttSnClientSetGateway(MqttSnClientContext *context, const IpAddr *gwIpAddr, uint16_t gwPort)
Specify the address of the gateway.
@ ERROR_NOT_CONNECTED
Definition: error.h:80
uint16_t mqttSnClientFindPredefTopicName(MqttSnClientContext *context, const char_t *topicName)
Retrieve the topic ID associated with a predefined topic name.
error_t mqttSnClientPing(MqttSnClientContext *context)
Send ping request.
char_t topicName[]
@ MQTT_SN_RETURN_CODE_ACCEPTED
#define MQTT_SN_CLIENT_MAX_WILL_TOPIC_LEN
#define MQTT_SN_CLIENT_DEFAULT_TIMEOUT
error_t mqttSnClientSearchGateway(MqttSnClientContext *context, const IpAddr *destIpAddr, uint16_t destPort)
Search for a gateway.
error_t mqttSnClientInit(MqttSnClientContext *context)
Initialize MQTT-SN client context.
@ MQTT_SN_MSG_TYPE_UNSUBACK
uint8_t dup
Definition: mqtt_common.h:182
@ MQTT_SN_CLIENT_STATE_ACTIVE
@ MQTT_SN_CLIENT_STATE_CONNECTING
MqttSnTransportProtocol
MQTT-SN transport protocols.
MQTT-SN client.
char_t clientId[]
error_t mqttSnClientSendWillTopicUpd(MqttSnClientContext *context)
Send WILLTOPICUPD message.
@ ERROR_REQUEST_REJECTED
Definition: error.h:273
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
error_t tlsInitSessionState(TlsSessionState *session)
Initialize session state.
Definition: tls.c:2922
#define osStrcpy(s1, s2)
Definition: os_port.h:210
error_t mqttSnClientRegisterDtlsInitCallback(MqttSnClientContext *context, MqttSnClientDtlsInitCallback callback)
Register DTLS initialization callback function.
@ MQTT_SN_MSG_TYPE_CONNACK
error_t mqttSnClientConnect(MqttSnClientContext *context, bool_t cleanSession)
Establish connection with the MQTT-SN gateway.
@ MQTT_SN_QOS_LEVEL_2
Exactly once delivery.
@ NO_ERROR
Success.
Definition: error.h:44
void(* MqttSnClientPublishCallback)(MqttSnClientContext *context, const char_t *topic, const uint8_t *message, size_t length, MqttSnQosLevel qos, bool_t retain)
PUBLISH message received callback.
@ MQTT_SN_MSG_TYPE_PUBACK
Debugging facilities.
error_t mqttSnClientRegisterPublishCallback(MqttSnClientContext *context, MqttSnClientPublishCallback callback)
Register publish callback function.
@ MQTT_SN_MSG_TYPE_SUBACK
error_t mqttSnClientSendConnect(MqttSnClientContext *context, bool_t cleanSession)
Send CONNECT message.
systime_t osGetSystemTime(void)
Retrieve system time.
Ipv4Addr destIpAddr
Definition: ipcp.h:80