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.4
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  //Save DTLS session
575  error = mqttSnClientSaveSession(context);
576 
577  //Check status code
578  if(!error)
579  {
580  //If the connection request has not been accepted, the failure
581  //reason is encoded in the return code field of the CONNACK
582  //message
583  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
584  {
585  //The connection request has been accepted by the gateway
586  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
587  }
588  else
589  {
590  //Terminate DTLS connection
592 
593  //The connection request has been rejected by the gateway
594  error = ERROR_REQUEST_REJECTED;
595  }
596  }
597  }
598  else
599  {
600  //Report an error
602  }
603  }
604  else if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
605  {
606  //The MQTT-SN client is connected
607  break;
608  }
609  else
610  {
611  //Invalid state
612  error = ERROR_WRONG_STATE;
613  }
614  }
615 
616  //Any error to report?
617  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
618  {
619  //Clean up side effects
621  //Update MQTT-SN client state
622  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
623  }
624 
625  //Return status code
626  return error;
627 }
628 
629 
630 /**
631  * @brief Publish message
632  * @param[in] context Pointer to the MQTT-SN client context
633  * @param[in] topicName Topic name
634  * @param[in] message Message payload
635  * @param[in] length Length of the message payload
636  * @param[in] qos QoS level to be used when publishing the message
637  * @param[in] retain This flag specifies if the message is to be retained
638  * @param[in] dup This flag specifies if the message is sent for the first
639  * time or if the message is retransmitted
640  * @param[in,out] msgId Message identifier used to send the PUBLISH message
641  * @return Error code
642  **/
643 
645  const char_t *topicName, const void *message, size_t length,
646  MqttSnQosLevel qos, bool_t retain, bool_t dup, uint16_t *msgId)
647 {
648  error_t error;
649  systime_t time;
650  uint16_t publishMsgId;
651 
652  //Check parameters
653  if(context == NULL || topicName == NULL)
655  if(message == NULL && length != 0)
657  if(dup && msgId == NULL)
659 
660  //Initialize status code
661  error = NO_ERROR;
662 
663  //Initialize message identifier
664  if(dup)
665  {
666  publishMsgId = *msgId;
667  }
668  else
669  {
670  publishMsgId = 0;
671  }
672 
673  //Publish procedure
674  while(!error)
675  {
676  //Get current time
677  time = osGetSystemTime();
678 
679  //Check current state
680  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
681  {
682  //Save current time
683  context->startTime = time;
684 
685  //Check whether the register procedure is needed
687  mqttSnClientFindTopicName(context, topicName) == 0 &&
689  {
690  //The message identifier allows the sender to match a message with
691  //its corresponding acknowledgment
693 
694  //To register a topic name a client sends a REGISTER message to
695  //the gateway
696  error = mqttSnClientSendRegister(context, topicName);
697  }
698  else
699  {
700  //The message ID is only relevant in case of QoS levels 1 and 2
702  {
703  //The message identifier allows the sender to match a message with
704  //its corresponding acknowledgment
705  if(!dup)
706  {
707  publishMsgId = mqttSnClientGenerateMessageId(context);
708  }
709  }
710  else
711  {
712  //For QoS level 0, the message identifier is coded 0x0000
713  publishMsgId = 0;
714  }
715 
716  //The client can start publishing data relating to the registered
717  //topic name by sending PUBLISH messages to the gateway
718  error = mqttSnClientSendPublish(context, publishMsgId, topicName,
719  message, length, qos, retain, dup);
720 
721  //In the QoS 0, no response is sent by the receiver and no retry
722  //is performed by the sender
724  break;
725  }
726  }
727  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
728  {
729  //Check whether the transmission of the PUBLISH message has started
730  if(context->msgType == MQTT_SN_MSG_TYPE_PUBLISH ||
731  context->msgType == MQTT_SN_MSG_TYPE_PUBREL)
732  {
733  //Restore the message identifier that was used to send the first
734  //PUBLISH message
735  if(!dup)
736  {
737  publishMsgId = context->msgId;
738  }
739  }
740 
741  //Check whether the timeout has elapsed
742  if(timeCompare(time, context->startTime + context->timeout) >= 0)
743  {
744  //Abort the retransmission procedure
745  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
746  //Report a timeout error
747  error = ERROR_TIMEOUT;
748  }
749  else if(timeCompare(time, context->retransmitStartTime +
751  {
752  //If the retry timer times out and the expected gateway's reply
753  //is not received, the client retransmits the message
754  if(context->msgType == MQTT_SN_MSG_TYPE_REGISTER)
755  {
756  //Retransmit REGISTER message
757  error = mqttSnClientSendRegister(context, topicName);
758  }
759  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBLISH)
760  {
761  //Retransmit PUBLISH message
762  error = mqttSnClientSendPublish(context, publishMsgId,
763  topicName, message, length, qos, retain, TRUE);
764  }
765  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBREL)
766  {
767  //Retransmit PUBREL message
768  error = mqttSnClientSendPubRel(context, context->msgId);
769  }
770  else
771  {
772  //Report an error
773  error = ERROR_INVALID_TYPE;
774  }
775  }
776  else
777  {
778  //Wait for the gateway's reply
780  }
781  }
782  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
783  {
784  //Update MQTT-SN client state
785  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
786 
787  //Check whether the transmission of the PUBLISH message has started
788  if(context->msgType == MQTT_SN_MSG_TYPE_PUBACK ||
789  context->msgType == MQTT_SN_MSG_TYPE_PUBREC ||
790  context->msgType == MQTT_SN_MSG_TYPE_PUBCOMP)
791  {
792  //Restore the message identifier that was used to send the first
793  //PUBLISH message
794  if(!dup)
795  {
796  publishMsgId = context->msgId;
797  }
798  }
799 
800  //Check the type of the received message
801  if(context->msgType == MQTT_SN_MSG_TYPE_REGACK)
802  {
803  //If the registration has not been accepted, the failure reason is
804  //encoded in the return code field of the REGACK message
805  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
806  {
807  //Save the topic ID assigned by the gateway
808  error = mqttSnClientAddTopic(context, topicName, context->topicId);
809  }
810  else
811  {
812  //The registration request has been rejected by the gateway
813  error = ERROR_REQUEST_REJECTED;
814  }
815  }
816  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBACK)
817  {
818  //If the publish request has not been accepted, the failure reason
819  //is encoded in the return code field of the PUBACK message
820  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
821  {
822  //Check QoS level
823  if(qos == MQTT_SN_QOS_LEVEL_2)
824  {
825  //Unexpected PUBREC message received
826  error = ERROR_UNEXPECTED_MESSAGE;
827  }
828  else
829  {
830  //A PUBACK message has been received
831  break;
832  }
833  }
834  else
835  {
836  //The publish request has been rejected by the gateway
837  error = ERROR_REQUEST_REJECTED;
838  }
839  }
840  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBREC)
841  {
842  //Check QoS level
843  if(qos == MQTT_SN_QOS_LEVEL_2)
844  {
845  //A PUBREL packet is the response to a PUBREC packet. It is the
846  //third packet of the QoS 2 protocol exchange
847  error = mqttSnClientSendPubRel(context, context->msgId);
848  }
849  else
850  {
851  //Unexpected PUBREC message received
852  error = ERROR_UNEXPECTED_MESSAGE;
853  }
854  }
855  else if(context->msgType == MQTT_SN_MSG_TYPE_PUBCOMP)
856  {
857  //A PUBCOMP message has been received
858  break;
859  }
860  else
861  {
862  //Report an error
864  }
865  }
866  else
867  {
868  //Invalid state
869  error = ERROR_NOT_CONNECTED;
870  }
871  }
872 
873  //Return the message identifier that was used to send the PUBLISH message
874  if(msgId != NULL)
875  *msgId = publishMsgId;
876 
877  //Return status code
878  return error;
879 }
880 
881 
882 /**
883  * @brief Subscribe to topic
884  * @param[in] context Pointer to the MQTT-SN client context
885  * @param[in] topicName Topic filter
886  * @param[in] qos Maximum QoS level at which the server can send application
887  * messages to the client
888  * @return Error code
889  **/
890 
893 {
894  error_t error;
895  systime_t time;
896 
897  //Check parameters
898  if(context == NULL || topicName == NULL)
900 
901  //Initialize status code
902  error = NO_ERROR;
903 
904  //Topic subscribe procedure
905  while(!error)
906  {
907  //Get current time
908  time = osGetSystemTime();
909 
910  //Check current state
911  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
912  {
913  //The message identifier allows the sender to match a message with
914  //its corresponding acknowledgment
916 
917  //Save current time
918  context->startTime = time;
919 
920  //Send SUBSCRIBE message
921  error = mqttSnClientSendSubscribe(context, topicName, qos);
922  }
923  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
924  {
925  //Check whether the timeout has elapsed
926  if(timeCompare(time, context->startTime + context->timeout) >= 0)
927  {
928  //Abort the retransmission procedure
929  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
930  //Report a timeout error
931  error = ERROR_TIMEOUT;
932  }
933  else if(timeCompare(time, context->retransmitStartTime +
935  {
936  //If the retry timer times out and the expected gateway's reply
937  //is not received, the client retransmits the message
938  error = mqttSnClientSendSubscribe(context, topicName, qos);
939  }
940  else
941  {
942  //Wait for the gateway's reply
944  }
945  }
946  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
947  {
948  //Update MQTT-SN client state
949  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
950 
951  //Check the type of the received message
952  if(context->msgType == MQTT_SN_MSG_TYPE_SUBACK)
953  {
954  //If the subscribe request has not been accepted, the failure reason
955  //is encoded in the return code field of the SUBACK message
956  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
957  {
958  //The topic ID field is not relevant in case of subscriptions to a
959  //topic name which contains wildcard characters
960  if(osStrchr(topicName, '#') == NULL && osStrchr(topicName, '+') == NULL)
961  {
962  //Save the topic ID assigned by the gateway
963  error = mqttSnClientAddTopic(context, topicName, context->topicId);
964  }
965 
966  //A SUBACK message has been received
967  break;
968  }
969  else
970  {
971  //The subscribe request has been rejected by the gateway
972  error = ERROR_REQUEST_REJECTED;
973  }
974  }
975  else
976  {
977  //Report an error
979  }
980  }
981  else
982  {
983  //Invalid state
984  error = ERROR_NOT_CONNECTED;
985  }
986  }
987 
988  //Return status code
989  return error;
990 }
991 
992 
993 /**
994  * @brief Unsubscribe from topic
995  * @param[in] context Pointer to the MQTT-SN client context
996  * @param[in] topicName Topic filter
997  * @return Error code
998  **/
999 
1001  const char_t *topicName)
1002 {
1003  error_t error;
1004  systime_t time;
1005 
1006  //Check parameters
1007  if(context == NULL || topicName == NULL)
1008  return ERROR_INVALID_PARAMETER;
1009 
1010  //Initialize status code
1011  error = NO_ERROR;
1012 
1013  //Topic unsubscribe procedure
1014  while(!error)
1015  {
1016  //Get current time
1017  time = osGetSystemTime();
1018 
1019  //Check current state
1020  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1021  {
1022  //The message identifier allows the sender to match a message with
1023  //its corresponding acknowledgment
1025 
1026  //Save current time
1027  context->startTime = time;
1028 
1029  //Send UNSUBSCRIBE message
1030  error = mqttSnClientSendUnsubscribe(context, topicName);
1031  }
1032  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1033  {
1034  //Check whether the timeout has elapsed
1035  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1036  {
1037  //Abort the retransmission procedure
1038  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1039  //Report a timeout error
1040  error = ERROR_TIMEOUT;
1041  }
1042  else if(timeCompare(time, context->retransmitStartTime +
1044  {
1045  //If the retry timer times out and the expected gateway's reply
1046  //is not received, the client retransmits the message
1047  error = mqttSnClientSendUnsubscribe(context, topicName);
1048  }
1049  else
1050  {
1051  //Wait for the gateway's reply
1053  }
1054  }
1055  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1056  {
1057  //Update MQTT-SN client state
1058  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1059 
1060  //Check the type of the received message
1061  if(context->msgType == MQTT_SN_MSG_TYPE_UNSUBACK)
1062  {
1063  //An UNSUBACK message has been received
1064  break;
1065  }
1066  else
1067  {
1068  //Report an error
1069  error = ERROR_UNEXPECTED_RESPONSE;
1070  }
1071  }
1072  else
1073  {
1074  //Invalid state
1075  error = ERROR_NOT_CONNECTED;
1076  }
1077  }
1078 
1079  //Return status code
1080  return error;
1081 }
1082 
1083 
1084 /**
1085  * @brief Send ping request
1086  * @param[in] context Pointer to the MQTT-SN client context
1087  * @return Error code
1088  **/
1089 
1091 {
1092  error_t error;
1093  systime_t time;
1094 
1095  //Make sure the MQTT-SN client context is valid
1096  if(context == NULL)
1097  return ERROR_INVALID_PARAMETER;
1098 
1099  //Initialize status code
1100  error = NO_ERROR;
1101 
1102  //Send PINGREQ packet and wait for PINGRESP packet to be received
1103  while(!error)
1104  {
1105  //Get current time
1106  time = osGetSystemTime();
1107 
1108  //Check current state
1109  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1110  {
1111  //Save current time
1112  context->startTime = time;
1113  context->retransmitStartTime = time;
1114 
1115  //Send PINGREQ message
1116  error = mqttSnClientSendPingReq(context);
1117 
1118  //Update MQTT-SN client state
1119  context->state = MQTT_SN_CLIENT_STATE_SENDING_REQ;
1120  context->msgType = MQTT_SN_MSG_TYPE_PINGREQ;
1121  }
1122  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1123  {
1124  //Check whether the timeout has elapsed
1125  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1126  {
1127  //Abort the retransmission procedure
1128  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1129  //Report a timeout error
1130  error = ERROR_TIMEOUT;
1131  }
1132  else if(timeCompare(time, context->retransmitStartTime +
1134  {
1135  //If the retry timer times out and the expected gateway's reply
1136  //is not received, the client retransmits the message
1137  error = mqttSnClientSendPingReq(context);
1138 
1139  //Save the time at which the message was sent
1140  context->retransmitStartTime = time;
1141  }
1142  else
1143  {
1144  //Wait for the gateway's reply
1146  }
1147  }
1148  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1149  {
1150  //Update MQTT-SN client state
1151  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1152 
1153  //Check the type of the received message
1154  if(context->msgType == MQTT_SN_MSG_TYPE_PINGRESP)
1155  {
1156  //A PINGRESP message has been received
1157  break;
1158  }
1159  else
1160  {
1161  //Report an error
1162  error = ERROR_UNEXPECTED_RESPONSE;
1163  }
1164  }
1165  else
1166  {
1167  //Invalid state
1168  error = ERROR_NOT_CONNECTED;
1169  }
1170  }
1171 
1172  //Return status code
1173  return error;
1174 }
1175 
1176 
1177 /**
1178  * @brief Update the Will message
1179  * @param[in] context Pointer to the MQTT-SN client context
1180  * @param[in] topic Will topic name
1181  * @param[in] message Will message
1182  * @param[in] length Length of the Will message
1183  * @param[in] qos QoS level to be used when publishing the Will message
1184  * @param[in] retain This flag specifies if the Will message is to be retained
1185  * @return Error code
1186  **/
1187 
1189  const char_t *topic, const void *message, size_t length,
1190  MqttSnQosLevel qos, bool_t retain)
1191 {
1192  error_t error;
1193  systime_t time;
1194 
1195  //Check parameters
1196  if(context == NULL || topic == NULL)
1197  return ERROR_INVALID_PARAMETER;
1198  if(message == NULL && length != 0)
1199  return ERROR_INVALID_PARAMETER;
1200 
1201  //Initialize status code
1202  error = NO_ERROR;
1203 
1204  //Publish procedure
1205  while(!error)
1206  {
1207  //Get current time
1208  time = osGetSystemTime();
1209 
1210  //Check current state
1211  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1212  {
1213  //Update the Will message
1214  error = mqttSnClientSetWillMessage(context, topic, message, length,
1215  qos, retain);
1216 
1217  //Check status code
1218  if(!error)
1219  {
1220  //Save current time
1221  context->startTime = time;
1222 
1223  //Send WILLTOPICUPD message
1224  error = mqttSnClientSendWillTopicUpd(context);
1225  }
1226  }
1227  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1228  {
1229  //Check whether the timeout has elapsed
1230  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1231  {
1232  //Abort the retransmission procedure
1233  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTING;
1234  //Report a timeout error
1235  error = ERROR_TIMEOUT;
1236  }
1237  else if(timeCompare(time, context->retransmitStartTime +
1239  {
1240  //If the retry timer times out and the expected gateway's reply
1241  //is not received, the client retransmits the message
1242  if(context->msgType == MQTT_SN_MSG_TYPE_WILLTOPICUPD)
1243  {
1244  //Retransmit WILLTOPICUPD message
1245  error = mqttSnClientSendWillTopicUpd(context);
1246  }
1247  else if(context->msgType == MQTT_SN_MSG_TYPE_WILLMSGUPD)
1248  {
1249  //Retransmit WILLMSGUPD message
1250  error = mqttSnClientSendWillMsgUpd(context);
1251  }
1252  else
1253  {
1254  //Report an error
1255  error = ERROR_INVALID_TYPE;
1256  }
1257  }
1258  else
1259  {
1260  //Wait for the gateway's reply
1262  }
1263  }
1264  else if(context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1265  {
1266  //Update MQTT-SN client state
1267  context->state = MQTT_SN_CLIENT_STATE_ACTIVE;
1268 
1269  //Check the type of the received message
1270  if(context->msgType == MQTT_SN_MSG_TYPE_WILLTOPICRESP)
1271  {
1272  //If the WILLTOPICUPD has not been accepted, the failure reason
1273  //is encoded in the return code field of the WILLTOPICRESP
1274  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
1275  {
1276  //Valid Will topic?
1277  if(context->willMessage.topic[0] != '\0')
1278  {
1279  //Send WILLMSGUPD message
1280  error = mqttSnClientSendWillMsgUpd(context);
1281  }
1282  else
1283  {
1284  //An empty WILLTOPIC message is used by a client to delete
1285  //the Will topic and the Will message stored in the server
1286  break;
1287  }
1288  }
1289  else
1290  {
1291  //The WILLTOPICUPD request has been rejected by the gateway
1292  error = ERROR_REQUEST_REJECTED;
1293  }
1294  }
1295  else if(context->msgType == MQTT_SN_MSG_TYPE_WILLMSGRESP)
1296  {
1297  //If the WILLMSGUPD has not been accepted, the failure reason
1298  //is encoded in the return code field of the WILLMSGRESP
1299  if(context->returnCode == MQTT_SN_RETURN_CODE_ACCEPTED)
1300  {
1301  //The WILLMSGUPD request has been accepted by the gateway
1302  break;
1303  }
1304  else
1305  {
1306  //The WILLMSGUPD request has been rejected by the gateway
1307  error = ERROR_REQUEST_REJECTED;
1308  }
1309  }
1310  else
1311  {
1312  //Report an error
1313  error = ERROR_UNEXPECTED_RESPONSE;
1314  }
1315  }
1316  else
1317  {
1318  //Invalid state
1319  error = ERROR_NOT_CONNECTED;
1320  }
1321  }
1322 
1323  //Return status code
1324  return error;
1325 }
1326 
1327 
1328 /**
1329  * @brief Retrieve return code
1330  * @param[in] context Pointer to the MQTT-SN client context
1331  * @param[out] returnCode Return code
1332  * @return Error code
1333  **/
1334 
1337 {
1338  //Check parameters
1339  if(context == NULL || returnCode == NULL)
1340  return ERROR_INVALID_PARAMETER;
1341 
1342  //Retrieve return code
1343  *returnCode = context->returnCode;
1344 
1345  //Successful processing
1346  return NO_ERROR;
1347 }
1348 
1349 
1350 /**
1351  * @brief Process MQTT-SN client events
1352  * @param[in] context Pointer to the MQTT-SN client context
1353  * @param[in] timeout Maximum time to wait before returning
1354  * @return Error code
1355  **/
1356 
1358 {
1359  error_t error;
1360 
1361  //Make sure the MQTT-SN client context is valid
1362  if(context == NULL)
1363  return ERROR_INVALID_PARAMETER;
1364 
1365  //Make sure the MQTT-SN client is connected
1366  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE ||
1367  context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ ||
1368  context->state == MQTT_SN_CLIENT_STATE_RESP_RECEIVED)
1369  {
1370  //Process MQTT-SN client events
1371  error = mqttSnClientProcessEvents(context, timeout);
1372  }
1373  else
1374  {
1375  //Invalid state
1376  error = ERROR_NOT_CONNECTED;
1377  }
1378 
1379  //Return status code
1380  return error;
1381 }
1382 
1383 
1384 /**
1385  * @brief Disconnect from the MQTT-SN gateway
1386  * @param[in] context Pointer to the MQTT-SN client context
1387  * @param[in] duration Sleep duration, in milliseconds
1388  * @return Error code
1389  **/
1390 
1393 {
1394  error_t error;
1395  systime_t time;
1396 
1397  //Make sure the MQTT-SN client context is valid
1398  if(context == NULL)
1399  return ERROR_INVALID_PARAMETER;
1400 
1401  //Initialize status code
1402  error = NO_ERROR;
1403 
1404  //Disconnect procedure
1405  while(!error)
1406  {
1407  //Get current time
1408  time = osGetSystemTime();
1409 
1410  //Check current state
1411  if(context->state == MQTT_SN_CLIENT_STATE_ACTIVE)
1412  {
1413  //Save current time
1414  context->startTime = time;
1415 
1416  //The DISCONNECT message is sent by a client to indicate that it
1417  //wants to close the connection
1418  error = mqttSnClientSendDisconnect(context, duration / 1000);
1419  }
1420  else if(context->state == MQTT_SN_CLIENT_STATE_SENDING_REQ)
1421  {
1422  //Check whether the timeout has elapsed
1423  if(timeCompare(time, context->startTime + context->timeout) >= 0)
1424  {
1425  //Terminate DTLS connection
1427 
1428  //Report a timeout error
1429  error = ERROR_TIMEOUT;
1430  }
1431  else if(timeCompare(time, context->retransmitStartTime +
1433  {
1434  //If the retry timer times out and the expected gateway's reply
1435  //is not received, the client retransmits the message
1436  error = mqttSnClientSendDisconnect(context, duration / 1000);
1437  }
1438  else
1439  {
1440  //Wait for the gateway's reply
1442  }
1443  }
1444  else if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTING)
1445  {
1446  //Terminate DTLS connection
1447  error = mqttSnClientShutdownConnection(context);
1448  //Close network connection
1449  mqttSnClientCloseConnection(context);
1450 
1451  //The connection is closed
1452  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
1453  }
1454  else if(context->state == MQTT_SN_CLIENT_STATE_DISCONNECTED)
1455  {
1456  //The MQTT-SN client is disconnected
1457  break;
1458  }
1459  else
1460  {
1461  //Invalid state
1462  error = ERROR_WRONG_STATE;
1463  }
1464  }
1465 
1466  //Any error to report?
1467  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
1468  {
1469  //Close network connection
1470  mqttSnClientCloseConnection(context);
1471  //Update MQTT-SN client state
1472  context->state = MQTT_SN_CLIENT_STATE_DISCONNECTED;
1473  }
1474 
1475  //Return status code
1476  return error;
1477 }
1478 
1479 
1480 /**
1481  * @brief Release MQTT-SN client context
1482  * @param[in] context Pointer to the MQTT-SN client context
1483  **/
1484 
1486 {
1487  //Make sure the MQTT-SN client context is valid
1488  if(context != NULL)
1489  {
1490  //Close connection
1491  mqttSnClientCloseConnection(context);
1492 
1493 #if (MQTT_SN_CLIENT_DTLS_SUPPORT == ENABLED)
1494  //Release DTLS session state
1495  tlsFreeSessionState(&context->dtlsSession);
1496 #endif
1497 
1498  //Clear MQTT-SN client context
1499  osMemset(context, 0, sizeof(MqttSnClientContext));
1500  }
1501 }
1502 
1503 #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:201
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:171
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:3126
#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:147
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:141
TCP/IP stack core.
error_t tlsInitSessionState(TlsSessionState *session)
Initialize session state.
Definition: tls.c:2983
#define osStrcpy(s1, s2)
Definition: os_port.h:213
error_t mqttSnClientRegisterDtlsInitCallback(MqttSnClientContext *context, MqttSnClientDtlsInitCallback callback)
Register DTLS initialization callback function.
error_t mqttSnClientSaveSession(MqttSnClientContext *context)
Save DTLS session.
@ 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