forked from openthread/openthread
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mqttsn_client.hpp
1156 lines (1040 loc) · 37.8 KB
/
mqttsn_client.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018, Vit Holasek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MQTTSN_CLIENT_HPP_
#define MQTTSN_CLIENT_HPP_
#include "mqttsn/mqttsn_gateway_list.hpp"
#include "common/locator.hpp"
#include "common/tasklet.hpp"
#include "net/ip6_address.hpp"
#include "net/udp6.hpp"
#include <openthread/mqttsn.h>
/**
* @file
* This file includes interface for MQTT-SN protocol v1.2 client.
*
*/
namespace ot {
/**
* @namespace ot::Mqttsn
* @brief
* This namespace includes definitions for MQTT-SN.
*
*/
namespace Mqttsn {
/**
* @addtogroup core-mqttsn
*
* @brief
* This module includes definitions for MQTT-SN client library. Client provides functionality
* for MQTT-SN connection established and full messaging capabilities over UDP.
*
* @{
*
*/
/**
* MQTT-SN message return code.
*
*/
typedef otMqttsnReturnCode ReturnCode;
/**
* MQTT-SN quality of service level.
*
*/
typedef otMqttsnQos Qos;
/**
* Disconnected state reason.
*
*/
typedef otMqttsnDisconnectType DisconnectType;
/**
* Client lifecycle states.
*
*/
typedef otMqttsnClientState ClientState;
enum
{
/**
* Client ID maximal length.
*
*/
kCliendIdStringMax = 24,
/**
* Long topic name maximal length (with null terminator).
*
*/
kMaxTopicNameLength = 50,
/**
* Short topic maximal length (with null terminator).
*
*/
kShortTopicNameLength = 3
};
/**
* MQTT-SN topic identificator type.
*/
typedef otMqttsnTopicIdType TopicIdType;
/**
* Numeric topic ID.
*
*/
typedef otMqttsnTopicId TopicId;
/**
* Short topic name string.
*
*/
typedef String<kShortTopicNameLength> ShortTopicNameString;
/**
* Long topic name string.
*
*/
typedef String<kMaxTopicNameLength> TopicNameString;
/**
* Client ID string.
*
*/
typedef String<kCliendIdStringMax> ClientIdString;
template <typename CallbackType>
class WaitingMessagesQueue;
/**
* This class represents topic. Topic is used for subscribing and publishing
* messages in following situations:
* - normal topic name string for subscribing and unsubscribing messages
* - short topic name (length <= 2) for subscribing, unsubscribing and publishing messages
* - normal topic ID for publishing messages returned by REGACK or SUBACK message
* - predefined topic ID for subscribing, unsubscribing and publishing messages
*
*/
class Topic: public otMqttsnTopic
{
public:
/**
* Default constructor.
*/
Topic(void) { }
/**
* Get topic type.
*
*/
TopicIdType GetType(void) const { return mType; }
/**
* This topic type is defined as a topic name string.
*
*/
bool HasTopicName(void) const;
/**
* Get topic name string.
*
*/
const char* GetTopicName(void) const;
/**
* This topic type is defined as a topic ID number.
*
*/
bool HasTopicId(void) const;
/**
* Get topic ID number.
*
*/
TopicId GetTopicId(void) const;
/**
* Create and initialize topic with topic name string. If is of length 1 or 2 then short topic name is sent.
*
* @note It can be used only for MQTT-SN subscribe or unsubscribe.
*
* @param[in] aName A pointer full topic name string.
*
* @return Topic of type kTopicName initialized with topic name string.
*
*/
static Topic FromTopicName(const char *aName);
/**
* Create and initialize topic with short topic name string. Must be of length 1 or 2.
*
* @note It can be used for MQTT-SN subscribe, unsubscribe and publish and QoS -1 publish.
*
* @param[in] aTopicName A pointer full topic name string.
*
* @return Topic of type kTopicName initialized with topic name string.
*
*/
static Topic FromShortTopicName(const char *aShortName);
/**
* Create and initialize topic with numeric topic ID.
*
* @note It should be used only for MQTT-SN publish.
*
* @param[in] aTopicId Numeric topic ID. Must not be 0.
*
* @return Topic of type kTopicId initialized with numeric topic ID.
*
*/
static Topic FromTopicId(TopicId aTopicId);
/**
* Create and initialize topic with predefined topic ID.
*
* @note It can be used only for MQTT-SN subscribe, unsubscribe, publish and QoS -1 publish.
*
* @param[in] aTopicId Numeric topic ID. Must not be 0.
*
* @return Topic of type kTopicId initialized with numeric topic ID.
*
*/
static Topic FromPredefinedTopicId(TopicId aTopicId);
};
/**
* Message metadata which are stored in waiting messages queue.
*
*/
template <typename CallbackType>
class MessageMetadata
{
friend class MqttsnClient;
friend class WaitingMessagesQueue<CallbackType>;
public:
/**
* Default constructor for the object.
*
*/
MessageMetadata(void);
/**
* This constructor initializes the object with specific values.
*
* @param[in] aDestinationAddress A reference of message destination IPv6 address.
* @param[in] aDestinationPort Message destination port.
* @param[in] aMessageId MQTT-SN Message ID.
* @param[in] aTimestamp Time stamp of message in milliseconds for timeout evaluation.
* @param[in] aRetransmissionTimeout Time in milliseconds after which message is message timeout invoked.
* @param[in] aRetransmissionCount Number of retransmission attempts.
* @param[in] aCallback A function pointer for handling message timeout.
* @param[in] aContext Pointer to callback passed to timeout callback.
*
*/
MessageMetadata(const Ip6::Address &aDestinationAddress, uint16_t aDestinationPort, uint16_t aMessageId, uint32_t aTimestamp, uint32_t aRetransmissionTimeout, uint8_t aRetransmissionCount, CallbackType aCallback, void* aContext);
/**
* Append metadata to the message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully appended the bytes.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
otError AppendTo(Message &aMessage) const;
/**
* Update metadata of the message.
*
* @param[in] aMessage A reference to the message.
*
* @retval OT_ERROR_NONE Successfully updated the message.
*
*/
otError UpdateIn(Message &aMessage) const;
/**
* Read metadata from the message.
*
* @param[in] aMessage A reference to the message.
*
* @returns The number of bytes read.
*
*/
uint16_t ReadFrom(Message &aMessage);
/**
* Get message without metadata bytes.
*
* @param[in] aMessage A reference to the message.
*
* @returns Returns a pointer to the message or null if invalid input message.
*
*/
Message* GetRawMessage(const Message &aMessage) const;
/**
* Get metadata length in bytes.
*
* @returns The number of bytes.
*
*/
uint16_t GetLength(void) const;
private:
/**
* A reference of message destination IPv6 address.
*
*/
Ip6::Address mDestinationAddress;
/**
* Message destination port.
*
*/
uint16_t mDestinationPort;
/**
* MQTT-SN Message ID.
*
*/
uint16_t mMessageId;
/**
* Time stamp of message in milliseconds for timeout evaluation.
*
*/
uint32_t mTimestamp;
/**
* Time in millisecond after which message is message timeout invoked.
*
*/
uint32_t mRetransmissionTimeout;
/**
* Message retransmission count.
*
*/
uint8_t mRetransmissionCount;
/**
* A function pointer for handling message timeout.
*
*/
CallbackType mCallback;
/**
* A pointer to callback passed to timeout callback.
*
*/
void* mContext;
};
/**
* The class represents the queue which contains messages waiting for acknowledgments from gateway.
*
*/
template <typename CallbackType>
class WaitingMessagesQueue
{
public:
/**
* Declaration of a function pointer which is used as timeout callback.
*
* @param[in] aMetadata A reference to message metadata.
* @param[in] aContext A poter to timeout callback context object.
*
*/
typedef void (*TimeoutCallbackFunc)(const MessageMetadata<CallbackType> &aMetadata, void* aContext);
/**
* Declaration of a function pointer for handling message retransmission.
*
* @param[in] aMessage A reference to message to be resend.
* @param[in] aAddress A reference to destination gateway address.
* @param[in] aPort Destination port.
* @param[in] aContext A pointer to retransmission callback context object.
*/
typedef void (*RetransmissionFunc)(const Message &aMessage, const Ip6::Address &aAddress, uint16_t aPort, void* aContext);
/**
* This constructor initializes the object with specific values.
*
* @param[in] aTimeoutCallback A function pointer to callback which is invoked on message timeout.
* @param[in] aTimeoutContext A pointer to context passed to timeout callback.
* @param[in] aRetransmissionFunc A function pointer to retransmission function.
* @param[in] aRetransmissionContext A pointer to context passed to retransmission function.
*
*/
WaitingMessagesQueue(TimeoutCallbackFunc aTimeoutCallback, void* aTimeoutContext, RetransmissionFunc aRetransmissionFunc, void* aRetransmissionContext);
/**
* Default object destructor.
*
*/
~WaitingMessagesQueue(void);
/**
* Copy message data and enqueue message to waiting queue.
*
* @param[in] aMessage A reference to message object to be enqueued.
* @param[in] aLength Message length.
* @param[in] aMetadata A reference to message metadata.
*
* @retval OT_ERROR_NONE Successfully enqueued the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to copy or enqueue the message.
*
*/
otError EnqueueCopy(const Message &aMessage, uint16_t aLength, const MessageMetadata<CallbackType> &aMetadata);
/**
* Dequeue specific message from waiting queue.
*
* @param[in] aMessage A reference to message object to be dequeued.
*
* @retval OT_ERROR_NONE Successfully dequeued the message.
* @retval OT_ERROR_NOT_FOUND Message was not found in waiting queue.
*
*/
otError Dequeue(Message &aMessage);
/**
* Find message by message ID and read message metadata.
*
* @param[in] aMessageId Message ID.
* @param[out] aMetadata A reference to initialized metadata object.
*
* @returns If the message is found the message ID is returned or null otherwise.
*
*/
Message* Find(uint16_t aMessageId, MessageMetadata<CallbackType> &aMetadata);
/**
* Evaluate queued messages timeout and retransmission.
*
* @retval OT_ERROR_NONE Timeouts were successfully processed.
*
*/
otError HandleTimer(void);
/**
* Force waiting messages timeout, invoke callback and empty queue.
*
*/
void ForceTimeout(void);
/**
* Determine if is the queue empty.
*
* @returns Returns true if the queue is empty or false otherwise.
*/
bool IsEmpty(void);
private:
MessageQueue mQueue;
TimeoutCallbackFunc mTimeoutCallback;
void* mTimeoutContext;
RetransmissionFunc mRetransmissionFunc;
void* mRetransmissionContext;
};
/**
* This class contains MQTT-SN connection parameters.
*
*/
class MqttsnConfig
{
public:
/**
* Default constructor for the object.
*
*/
MqttsnConfig(void)
: mAddress()
, mPort(OPENTHREAD_CONFIG_MQTTSN_DEFAULT_PORT)
, mClientId()
, mKeepAlive(30)
, mCleanSession()
, mRetransmissionTimeout(10)
, mRetransmissionCount(3)
{
;
}
/**
* Get gateway IPv6 address.
*
* @returns A reference to IPv6 address.
*
*/
const Ip6::Address &GetAddress() const
{
return mAddress;
}
/**
* Set gateway IPv6 address.
*
* @param[in] aAddress A reference to gateway IPv6 address.
*
*/
void SetAddress(const Ip6::Address &aAddress)
{
mAddress = aAddress;
}
/**
* Get gateway interface port number.
*
* @returns Gateway port number.
*
*/
uint16_t GetPort() const
{
return mPort;
}
/**
* Set gateway interface port number.
*
* @param[in] aPort Gateway port number.
*
*/
void SetPort(uint16_t aPort)
{
mPort = aPort;
}
/**
* Get client ID.
*
* @returns Cliend ID string.
*
*/
const ClientIdString &GetClientId() const
{
return mClientId;
}
/**
* Set client ID.
*
* @param[in] aClientId A pointer to client ID string.
*
*/
void SetClientId(const char* aClientId)
{
mClientId.Clear().Append("%s", aClientId);
}
/**
* Get keepalive period in seconds.
*
* @returns Keepalive time in seconds.
*
*/
int16_t GetKeepAlive() const
{
return mKeepAlive;
}
/**
* Set keepalive period in seconds.
*
* @param[in] Keepalive time in seconds.
*
*/
void SetKeepAlive(int16_t aDuration)
{
mKeepAlive = aDuration;
}
/**
* Get clean session flag.
*
* @returns Clean session flag.
*
*/
bool GetCleanSession() const
{
return mCleanSession;
}
/**
* Set clean session flag.
*
* @param[in] aCleanSession Clean session flag.
*
*/
void SetCleanSession(bool aCleanSession)
{
mCleanSession = aCleanSession;
}
/**
* Get retransmission timeout in milliseconds.
*
* @returns Retransmission timeout in milliseconds.
*
*/
uint32_t GetRetransmissionTimeout() const
{
return mRetransmissionTimeout;
}
/**
* Set retransmission timeout in milliseconds.
*
* @param[in] aTimeout Retransmission timeout value in milliseconds.
*
*/
void SetRetransmissionTimeout(uint32_t aTimeout)
{
mRetransmissionTimeout = aTimeout;
}
/**
* Get retransmission count.
*
* @returns Retransmission count.
*
*/
uint8_t GetRetransmissionCount() const
{
return mRetransmissionCount;
}
/**
* Set retransmission count.
*
* @param[in] aTimeout Retransmission count.
*
*/
void SetRetransmissionCount(uint8_t aCount)
{
mRetransmissionCount = aCount;
}
/**
* This method overloads assignment `=` operator to copy elements from another config into this config.
*
* @param[in] aOtherConfig Another config to copy from.
*
*/
MqttsnConfig &operator=(const MqttsnConfig &aOtherConfig)
{
SetAddress(aOtherConfig.GetAddress());
SetPort(aOtherConfig.GetPort());
SetClientId(aOtherConfig.GetClientId().AsCString());
SetKeepAlive(aOtherConfig.GetKeepAlive());
SetCleanSession(aOtherConfig.GetCleanSession());
SetRetransmissionTimeout(aOtherConfig.GetRetransmissionTimeout());
SetRetransmissionCount(aOtherConfig.GetRetransmissionCount());
return *this;
}
private:
Ip6::Address mAddress;
uint16_t mPort;
ClientIdString mClientId;
uint16_t mKeepAlive;
bool mCleanSession;
uint32_t mRetransmissionTimeout;
uint8_t mRetransmissionCount;
};
/**
* The class representing MQTT-SN protocol client.
*
*/
class MqttsnClient: public InstanceLocator
{
public:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit MqttsnClient(Instance &aInstance);
/**
* Default object destructor.
*
*/
~MqttsnClient(void);
/**
* Start MQTT-SN service and start connection and listening.
*
* @param[in] aPort MQTT-SN client listening port.
*
* @retval OT_ERROR_NONE Successfully started the service.
* @retval OT_ERROR_INVALID_STATE MQTT-SN client is already running.
*
*/
otError Start(uint16_t aPort);
/**
* Stop MQTT-SN service.
*
* @retval OT_ERROR_NONE Successfully stopped the service.
*
*/
otError Stop(void);
/**
* Process service workers.
*
* @retval OT_ERROR_NONE Successfully processed.
*
*/
otError Process(void);
/**
* Establish MQTT-SN connection with gateway.
*
* @param[in] aConfig A reference to configuration object with connection parameters.
*
* @retval OT_ERROR_NONE Connection message successfully queued.
* @retval OT_ERROR_INVALID_ARGS Invalid connection parameters.
* @retval OT_ERROR_INVALID_STATE The client is in invalid state. It must be disconnected before new connection establishment.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Connect(const MqttsnConfig &aConfig);
/**
* Reconnect MQTT-SN client with current connection settings. This is method is useful
* e.g. for returning from sleep mode to active mode.
*
* @retval OT_ERROR_NONE Connection message successfully queued.
* @retval OT_ERROR_INVALID_STATE Previous connection is still pending.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Reconnect(void);
/**
* Subscribe to the topic.
*
* @param[in] aTopic A reference to the topic to be subscribed. Long topic name, short topic name and topic ID are supported.
* @param[in] aQos Quality of service level to be subscribed.
* @param[in] aCallback A function pointer to callback which is invoked when subscription is acknowledged.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Subscription message successfully queued.
* @retval OT_ERROR_INVALID_ARGS Invalid subscription parameters.
* @retval OT_ERROR_INVALID_STATE The client cannot connect in active state.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Subscribe(const Topic &aTopic, Qos aQos, otMqttsnSubscribedHandler aCallback, void* aContext);
/**
* Register to topic with long topic name and obtain related topic ID.
*
* @param[in] aTopicName A pointer to long topic name string.
* @param[in] aCallback A function pointer to callback invoked when registration is acknowledged.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Registration message successfully queued.
* @retval OT_ERROR_INVALID_STATE The client is not in active state.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Register(const char* aTopicName, otMqttsnRegisteredHandler aCallback, void* aContext);
/**
* Publish message to the topic.
*
* @param[in] aData A pointer to byte array to be send as message payload.
* @param[in] aLength Length of message payload data.
* @param[in] aQos Message quality of service level.
* @param[in] aRetained Set retained flag of MQTT-SN publish message.
* @param[in] aTopic A reference to the topic to publish to. Only short topic name, topic ID and predefined topic ID are allowed.
* @param[in] aCallback A function pointer to callback invoked when publish is acknowledged.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Publish message successfully queued.
* @retval OT_ERROR_INVALID_ARGS Invalid publish parameters. Short topic name must have one or two characters.
* @retval OT_ERROR_INVALID_STATE The client is not in active state.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Publish(const uint8_t* aData, int32_t aLength, Qos aQos, bool aRetained, const Topic &aTopic, otMqttsnPublishedHandler aCallback, void* aContext);
/**
* Publish message to the topic with QoS level -1. No connection or subscription is required.
*
* @param[in] aData A pointer to byte array to be send as message payload.
* @param[in] aLength Length of message payload data.
* @param[in] aRetained Set retained flag of MQTT-SN publish message.
* @param[in] aTopic A reference to the topic to publish to. Only short topic name and predefined topic ID are allowed.
* @param[in] aAddress Gateway address.
* @param[in] aPort Gateway port.
*
* @retval OT_ERROR_NONE Publish message successfully queued.
* @retval OT_ERROR_INVALID_ARGS Invalid publish parameters. Short topic name must have one or two characters.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError PublishQosm1(const uint8_t* aData, int32_t aLength, bool aRetained, const Topic &aTopic, const Ip6::Address &aAddress, uint16_t aPort);
/**
* Unsubscribe from the topic.
*
* @param[in] aTopic A reference to the topic to be unsubscribed. Long topic name, short topic name and topic ID are supported.
* @param[in] aCallback A function pointer to callback invoked when unsubscription is acknowledged.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Unsubscribe message successfully queued.
* @retval OT_ERROR_INVALID_STATE The client is not in active state.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to unsubscribe.
*
*/
otError Unsubscribe(const Topic &aTopic, otMqttsnUnsubscribedHandler aCallback, void* aContext);
/**
* Disconnect MQTT-SN client from gateway.
*
* @retval OT_ERROR_NONE Disconnection message successfully queued.
* @retval OT_ERROR_INVALID_STATE The client is not in relevant state. It must be asleep, awake or active.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Disconnect(void);
/**
* Put the client into asleep state or change sleep duration. Client must be awaken or reconnected before duration time passes.
*
* @param[in] aDuration Duration time in seconds for which will the client stay in asleep state.
*
* @retval OT_ERROR_NONE Sleep request successfully queued.
* @retval OT_ERROR_INVALID_STATE The client is not in relevant state. It must be asleep, awake or active.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Sleep(uint16_t aDuration);
/**
* Awake the client and receive pending messages.
*
* @note Configuration retransmission count is still applied.
*
* @param[in] aTimeout Timeout in milliseconds for staying in awake state. PINGRESP message must be received before timeout time passes.
*
* @retval OT_ERROR_NONE Awake request successfully queued.
* @retval OT_ERROR_INVALID_STATE The client is not in relevant state. It must be asleep or awake.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError Awake(uint32_t aTimeout);
/**
* Search for gateway with multicast message.
*
* @param[in] aMulticastAddress A reference to multicast IPv6 address.
* @param[in] aPort Gateway port number.
* @param[in] aRadius Message hop limit (time to live)
*
* @retval OT_ERROR_NONE Search gateway request successfully queued.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to process.
*
*/
otError SearchGateway(const Ip6::Address &aMulticastAddress, uint16_t aPort, uint8_t aRadius);
/**
* Get current MQTT-SN client state.
*
* @returns Current client state.
*
*/
ClientState GetState(void) const;
/**
* Get list of active MQTT-SN gateways. Gateways are periodically advertised
* or obtained with gwinfo message.
*
* @returns A reference to list with active gateways information.
*/
const StaticArrayList<GatewayInfo> &GetActiveGateways(void) const;
/**
* Set callback function invoked when connection acknowledged or timed out.
*
* @param[in] aCallback A function pointer to connect callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetConnectedCallback(otMqttsnConnectedHandler aCallback, void* aContext);
/**
* Set callback function invoked when publish message received from the topic.
*
* @param[in] aCallback A function pointer to publish received callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetPublishReceivedCallback(otMqttsnPublishReceivedHandler aCallback, void* aContext);
/**
* Set callback function invoked when advertise message received from the gateway.
*
* @param[in] aCallback A function pointer to advertise callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetAdvertiseCallback(otMqttsnAdvertiseHandler aCallback, void* aContext);
/**
* Set callback function invoked when gateway info received from gateway.
*
* @param[in] aCallback A function pointer to gateway info received callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetSearchGwCallback(otMqttsnSearchgwHandler aCallback, void* aContext);
/**
* Set callback function invoked when disconnect acknowledged or timed out.
*
* @param[in] aCallback A function pointer to disconnect callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetDisconnectedCallback(otMqttsnDisconnectedHandler aCallback, void* aContext);
/**
* Set callback function invoked when register message received.
*
* @param[in] aCallback A function pointer to register callback function.
* @param[in] aContext A pointer to context object passed to callback.
*
* @retval OT_ERROR_NONE Callback function successfully set.
*
*/
otError SetRegisterReceivedCallback(otMqttsnRegisterReceivedHandler aCallback, void* aContext);
protected:
/**
* Allocate new message with payload.
*
* @param[out] aMessage A pointer to message pointer.
* @param[in] aBuffer A pointer to payload byte array.
* @param[in] aLength Payload length in bytes.
*
* @retval OT_ERROR_NONE New message successfully created.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to allocate new message.
*
*/
otError NewMessage(Message **aMessage, unsigned char* aBuffer, int32_t aLength);
/**
* Send OT message to configured gateway address.
*
* @param[in] aMessage A reference to message instance.
*
* @retval OT_ERROR_NONE Message successfully enqueued.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to enqueue message.
*
*/
otError SendMessage(Message &aMessage);
/**
* Send OT message to configured gateway address and queue message to retransmission on timeout.
*
* @param[in] aMessage A reference to message instance.
* @param[in] aQueue A waiting messages queue where will be message enqueued to retransmission on timeout.
* @param[in] aMessageId MQTT-SN Message ID. Or zero if the message does not contain ID.
* @param[in] aCallback A function pointer for handling message timeout.
* @param[in] aContext A pointer to callback passed to timeout callback.
*
* @retval OT_ERROR_NONE Message successfully enqueued.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to enqueue message.
*
*/
template <typename CallbackType>
otError SendMessageWithRetransmission(Message &aMessage, WaitingMessagesQueue<CallbackType> &aQueue, uint16_t aMessageId, CallbackType aCallback, void* aContext);
/**
* Send OT message to specific gateway address.