-
Notifications
You must be signed in to change notification settings - Fork 15
/
BasicClient.h
2983 lines (2423 loc) · 84.6 KB
/
BasicClient.h
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 2016 - 2024 (C). Alex Robenko. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <type_traits>
#include <vector>
#include <algorithm>
#include <iterator>
#include <limits>
#include <cstring>
#include "comms/comms.h"
#include "comms/util/ScopeGuard.h"
#include "cc_mqttsn/Version.h"
#include "cc_mqttsn/Message.h"
#include "cc_mqttsn/frame/Frame.h"
#include "cc_mqttsn/input/ClientInputMessages.h"
#include "cc_mqttsn/options/ClientDefaultOptions.h"
#include "cc_mqttsn_client/common.h"
#include "details/WriteBufStorageType.h"
static_assert(COMMS_MAKE_VERSION(2, 7, 0) <= cc_mqttsn::version(),
"The version of cc.mqttsn.generated library is too old");
namespace cc_mqttsn_client
{
namespace details
{
template <typename TInfo, typename TOpts, bool THasTrackedGatewaysLimit>
struct GwInfoStorageType;
template <typename TInfo, typename TOpts>
struct GwInfoStorageType<TInfo, TOpts, true>
{
typedef comms::util::StaticVector<TInfo, TOpts::TrackedGatewaysLimit> Type;
};
template <typename TInfo, typename TOpts>
struct GwInfoStorageType<TInfo, TOpts, false>
{
typedef std::vector<TInfo> Type;
};
template <typename TInfo, typename TOpts>
using GwInfoStorageTypeT =
typename GwInfoStorageType<TInfo, TOpts, TOpts::HasTrackedGatewaysLimit>::Type;
//-----------------------------------------------------------
template <typename TInfo, typename TOpts, bool THasRegisteredTopicsLimit>
struct RegInfoStorageType;
template <typename TInfo, typename TOpts>
struct RegInfoStorageType<TInfo, TOpts, true>
{
typedef comms::util::StaticVector<TInfo, TOpts::RegisteredTopicsLimit> Type;
};
template <typename TInfo, typename TOpts>
struct RegInfoStorageType<TInfo, TOpts, false>
{
typedef std::vector<TInfo> Type;
};
template <typename TInfo, typename TOpts>
using RegInfoStorageTypeT =
typename RegInfoStorageType<TInfo, TOpts, TOpts::HasRegisteredTopicsLimit>::Type;
//-----------------------------------------------------------
template <typename TOpts, bool THasStaticSize>
struct TopicNameStorageOpt;
template <typename TOpts>
struct TopicNameStorageOpt<TOpts, true>
{
typedef comms::option::FixedSizeStorage<TOpts::TopicNameStaticStorageSize> Type;
};
template <typename TOpts>
struct TopicNameStorageOpt<TOpts, false>
{
typedef comms::option::EmptyOption Type;
};
template <typename TOpts>
using TopicNameStorageOptT =
typename TopicNameStorageOpt<TOpts, TOpts::HasTopicNameStaticStorageSize>::Type;
//-----------------------------------------------------------
template <typename TOpts, bool THasStaticSize>
struct DataStorageOpt;
template <typename TOpts>
struct DataStorageOpt<TOpts, true>
{
typedef comms::option::FixedSizeStorage<TOpts::MessageDataStaticStorageSize> Type;
};
template <typename TOpts>
struct DataStorageOpt<TOpts, false>
{
typedef comms::option::EmptyOption Type;
};
template <typename TOpts>
using DataStorageOptT =
typename DataStorageOpt<TOpts, TOpts::HasMessageDataStaticStorageSize>::Type;
//-----------------------------------------------------------
template <typename TOpts, bool THasStaticSize>
struct ClientIdStorageOpt;
template <typename TOpts>
struct ClientIdStorageOpt<TOpts, true>
{
typedef comms::option::FixedSizeStorage<TOpts::TopicNameStaticStorageSize> Type;
};
template <typename TOpts>
struct ClientIdStorageOpt<TOpts, false>
{
typedef comms::option::EmptyOption Type;
};
template <typename TOpts>
using ClientIdStorageOptT =
typename ClientIdStorageOpt<TOpts, TOpts::HasTopicNameStaticStorageSize>::Type;
//-----------------------------------------------------------
cc_mqttsn::field::QosVal translateQosValue(CC_MqttsnQoS val)
{
static_assert(
static_cast<int>(cc_mqttsn::field::QosVal::AtMostOnceDelivery) == CC_MqttsnQoS_AtMostOnceDelivery,
"Invalid mapping");
static_assert(
static_cast<int>(cc_mqttsn::field::QosVal::AtLeastOnceDelivery) == CC_MqttsnQoS_AtLeastOnceDelivery,
"Invalid mapping");
static_assert(
static_cast<int>(cc_mqttsn::field::QosVal::ExactlyOnceDelivery) == CC_MqttsnQoS_ExactlyOnceDelivery,
"Invalid mapping");
if (val == CC_MqttsnQoS_NoGwPublish) {
return cc_mqttsn::field::QosVal::NoGwPublish;
}
return static_cast<cc_mqttsn::field::QosVal>(val);
}
CC_MqttsnQoS translateQosValue(cc_mqttsn::field::QosVal val)
{
if (val == cc_mqttsn::field::QosVal::NoGwPublish) {
return CC_MqttsnQoS_NoGwPublish;
}
return static_cast<CC_MqttsnQoS>(val);
}
} // namespace details
template <typename TClientOpts>
class BasicClient
{
typedef details::WriteBufStorageTypeT<TClientOpts> WriteBufStorage;
typedef cc_mqttsn::Message<
comms::option::IdInfoInterface,
comms::option::ReadIterator<const std::uint8_t*>,
comms::option::WriteIterator<std::uint8_t*>,
comms::option::Handler<BasicClient<TClientOpts> >,
comms::option::LengthInfoInterface
> Message;
typedef unsigned long long Timestamp;
enum class Op
{
None,
Connect,
Disconnect,
PublishId,
Publish,
SubscribeId,
Subscribe,
UnsubscribeId,
Unsubscribe,
WillTopicUpdate,
WillMsgUpdate,
Sleep,
CheckMessages,
NumOfValues // must be last
};
struct OpBase
{
Timestamp m_lastMsgTimestamp = 0;
unsigned m_attempt = 0;
};
struct AsyncOpBase : public OpBase
{
CC_MqttsnAsyncOpCompleteReportFn m_cb = nullptr;
void* m_cbData = nullptr;
};
struct ConnectOp : public AsyncOpBase
{
CC_MqttsnWillInfo m_willInfo = CC_MqttsnWillInfo();
const char* m_clientId = nullptr;
std::uint16_t m_keepAlivePeriod = 0;
bool m_hasWill = false;
bool m_cleanSession = false;
bool m_willTopicSent = false;
bool m_willMsgSent = false;
};
typedef AsyncOpBase DisconnectOp;
struct PublishOpBase : public AsyncOpBase
{
CC_MqttsnTopicId m_topicId = 0U;
std::uint16_t m_msgId = 0U;
const std::uint8_t* m_msg = nullptr;
std::size_t m_msgLen = 0;
CC_MqttsnQoS m_qos = CC_MqttsnQoS_AtMostOnceDelivery;
bool m_retain = false;
bool m_ackReceived = false;
};
struct PublishIdOp : public PublishOpBase
{
};
struct PublishOp : public PublishOpBase
{
const char* m_topic = nullptr;
bool m_registered = false;
bool m_didRegistration = false;
bool m_shortName = false;
};
struct SubscribeOpBase : public OpBase
{
CC_MqttsnQoS m_qos = CC_MqttsnQoS_AtMostOnceDelivery;
CC_MqttsnSubscribeCompleteReportFn m_cb = nullptr;
void* m_cbData = nullptr;
std::uint16_t m_msgId = 0U;
CC_MqttsnTopicId m_topicId = 0U;
};
struct SubscribeIdOp : public SubscribeOpBase
{
};
struct SubscribeOp : public SubscribeOpBase
{
const char* m_topic = nullptr;
bool m_usingShortTopicName = false;
};
struct UnsubscribeOpBase : public AsyncOpBase
{
std::uint16_t m_msgId = 0U;
CC_MqttsnTopicId m_topicId = 0U;
};
struct UnsubscribeIdOp : public UnsubscribeOpBase
{
};
struct UnsubscribeOp : public UnsubscribeOpBase
{
const char* m_topic = nullptr;
bool m_usingShortTopicName = false;
};
struct WillTopicUpdateOp : public AsyncOpBase
{
const char* m_topic = nullptr;
CC_MqttsnQoS m_qos;
bool m_retain;
};
struct WillMsgUpdateOp : public AsyncOpBase
{
const unsigned char* m_msg = nullptr;
unsigned m_msgLen = 0;
};
struct SleepOp : public AsyncOpBase
{
std::uint16_t m_duration = 0;
};
typedef AsyncOpBase CheckMessagesOp;
enum class ConnectionStatus
{
Disconnected,
Connected,
Asleep
};
typedef void (BasicClient<TClientOpts>::*FinaliseFunc)(CC_MqttsnAsyncOpStatus);
using TopicIdTypeVal = cc_mqttsn::field::TopicIdTypeVal;
using ReturnCodeVal = cc_mqttsn::field::ReturnCodeVal;
// struct NoOrigDataViewProtOpts : TProtOpts
// {
// static const bool HasOrigDataView = false;
// };
class ProtOpts : public cc_mqttsn::options::ClientDefaultOptions
{
using Base = cc_mqttsn::options::ClientDefaultOptions;
public:
struct field : public Base::field
{
using ClientId = comms::option::app::OrigDataView;
using Data = comms::option::app::OrigDataView;
using GwAdd = comms::option::app::OrigDataView;
using TopicName = comms::option::app::OrigDataView;
using WillMsg = comms::option::app::OrigDataView;
using WillTopic = comms::option::app::OrigDataView;
};
struct frame : public Base::frame
{
struct FrameLayers : public Base::frame::FrameLayers
{
using Id = comms::option::app::InPlaceAllocation;
}; // struct FrameLayers
}; // struct frame
};
class StorageOptions : public cc_mqttsn::options::ClientDefaultOptions
{
using Base = cc_mqttsn::options::ClientDefaultOptions;
public:
struct field : public Base::field
{
using ClientId = details::ClientIdStorageOptT<TClientOpts>;
using Data = details::DataStorageOptT<TClientOpts>;
using TopicName = details::TopicNameStorageOptT<TClientOpts>;
};
};
public:
typedef typename Message::Field FieldBase;
typedef typename cc_mqttsn::field::GwId<ProtOpts>::ValueType GwIdValueType;
typedef typename cc_mqttsn::field::TopicName<StorageOptions>::ValueType TopicNameType;
typedef typename cc_mqttsn::field::Data<StorageOptions>::ValueType DataType;
typedef typename cc_mqttsn::field::TopicId<StorageOptions>::ValueType TopicIdType;
typedef typename cc_mqttsn::field::ClientId<StorageOptions>::ValueType ClientIdType;
struct GwInfo
{
GwInfo() = default;
Timestamp m_timestamp = 0;
GwIdValueType m_id = 0;
//GwAddValueType m_addr;
unsigned m_duration = 0;
};
typedef details::GwInfoStorageTypeT<GwInfo, TClientOpts> GwInfoStorage;
typedef cc_mqttsn::message::Advertise<Message, ProtOpts> AdvertiseMsg;
typedef cc_mqttsn::message::Searchgw<Message, ProtOpts> SearchgwMsg;
typedef cc_mqttsn::message::Gwinfo<Message, ProtOpts> GwinfoMsg;
typedef cc_mqttsn::message::Connect<Message, ProtOpts> ConnectMsg;
typedef cc_mqttsn::message::Connack<Message, ProtOpts> ConnackMsg;
typedef cc_mqttsn::message::Willtopicreq<Message, ProtOpts> WilltopicreqMsg;
typedef cc_mqttsn::message::Willtopic<Message, ProtOpts> WilltopicMsg;
typedef cc_mqttsn::message::Willmsgreq<Message, ProtOpts> WillmsgreqMsg;
typedef cc_mqttsn::message::Willmsg<Message, ProtOpts> WillmsgMsg;
typedef cc_mqttsn::message::Register<Message, ProtOpts> RegisterMsg;
typedef cc_mqttsn::message::Regack<Message, ProtOpts> RegackMsg;
typedef cc_mqttsn::message::Publish<Message, ProtOpts> PublishMsg;
typedef cc_mqttsn::message::Puback<Message, ProtOpts> PubackMsg;
typedef cc_mqttsn::message::Pubrec<Message, ProtOpts> PubrecMsg;
typedef cc_mqttsn::message::Pubrel<Message, ProtOpts> PubrelMsg;
typedef cc_mqttsn::message::Pubcomp<Message, ProtOpts> PubcompMsg;
typedef cc_mqttsn::message::Subscribe<Message, ProtOpts> SubscribeMsg;
typedef cc_mqttsn::message::Suback<Message, ProtOpts> SubackMsg;
typedef cc_mqttsn::message::Unsubscribe<Message, ProtOpts> UnsubscribeMsg;
typedef cc_mqttsn::message::Unsuback<Message, ProtOpts> UnsubackMsg;
typedef cc_mqttsn::message::Pingreq<Message, ProtOpts> PingreqMsg;
typedef cc_mqttsn::message::Pingresp<Message, ProtOpts> PingrespMsg;
typedef cc_mqttsn::message::Disconnect<Message, ProtOpts> DisconnectMsg;
typedef cc_mqttsn::message::Willtopicupd<Message, ProtOpts> WilltopicupdMsg;
typedef cc_mqttsn::message::Willtopicresp<Message, ProtOpts> WilltopicrespMsg;
typedef cc_mqttsn::message::Willmsgupd<Message, ProtOpts> WillmsgupdMsg;
typedef cc_mqttsn::message::Willmsgresp<Message, ProtOpts> WillmsgrespMsg;
BasicClient() = default;
~BasicClient() noexcept = default;
typedef typename Message::ReadIterator ReadIterator;
const GwInfoStorage& gwInfos() const
{
return m_gwInfos;
}
void setRetryPeriod(unsigned val)
{
static const auto MaxVal =
std::numeric_limits<decltype(m_retryPeriod)>::max() / 1000;
m_retryPeriod = std::min(val * 1000, MaxVal);
}
void setRetryCount(unsigned val)
{
m_retryCount = std::max(1U, val);
}
void setBroadcastRadius(std::uint8_t val)
{
m_broadcastRadius = val;
}
void setNextTickProgramCallback(CC_MqttsnNextTickProgramFn cb, void* data)
{
if (cb != nullptr) {
m_nextTickProgramFn = cb;
m_nextTickProgramData = data;
}
}
void setCancelNextTickWaitCallback(CC_MqttsnCancelNextTickWaitFn cb, void* data)
{
if (cb != nullptr) {
m_cancelNextTickWaitFn = cb;
m_cancelNextTickWaitData = data;
}
}
void setSendOutputDataCallback(CC_MqttsnSendOutputDataFn cb, void* data)
{
if (cb != nullptr) {
m_sendOutputDataFn = cb;
m_sendOutputDataData = data;
}
}
void setGwStatusReportCallback(CC_MqttsnGwStatusReportFn cb, void* data)
{
m_gwStatusReportFn = cb;
m_gwStatusReportData = data;
}
void setGwDisconnectReportCallback(CC_MqttsnGwDisconnectReportFn cb, void* data)
{
m_gwDisconnectReportFn = cb;
m_gwDisconnectReportData = data;
}
void setMessageReportCallback(CC_MqttsnMessageReportFn cb, void* data)
{
m_msgReportFn = cb;
m_msgReportData = data;
}
void setSearchgwEnabled(bool value)
{
m_searchgwEnabled = value;
}
void sendSearchGw()
{
sendGwSearchReq();
}
void discardGw(std::uint8_t gwId)
{
auto guard = apiCall();
auto iter =
std::find_if(
m_gwInfos.begin(), m_gwInfos.end(),
[gwId](typename GwInfoStorage::const_reference elem) -> bool
{
return elem.m_id == gwId;
});
if (iter == m_gwInfos.end()) {
return;
}
m_gwInfos.erase(iter);
reportGwStatus(gwId, CC_MqttsnGwStatus_Discarded);
}
void discardAllGw()
{
auto guard = apiCall();
if (m_gwStatusReportFn == nullptr) {
m_gwInfos.clear();
return;
}
typedef details::GwInfoStorageTypeT<std::uint8_t, TClientOpts> GwIdStorage;
GwIdStorage ids;
ids.reserve(m_gwInfos.size());
std::transform(
m_gwInfos.begin(), m_gwInfos.end(), std::back_inserter(ids),
[](typename GwInfoStorage::const_reference elem) -> std::uint8_t
{
return elem.m_id;
});
m_gwInfos.clear();
for (auto gwId : ids) {
reportGwStatus(gwId, CC_MqttsnGwStatus_Discarded);
}
}
CC_MqttsnErrorCode start()
{
if (m_running) {
return CC_MqttsnErrorCode_AlreadyStarted;
}
if ((m_nextTickProgramFn == nullptr) ||
(m_cancelNextTickWaitFn == nullptr) ||
(m_sendOutputDataFn == nullptr) ||
(m_msgReportFn == nullptr)) {
return CC_MqttsnErrorCode_BadParam;
}
m_running = true;
m_gwInfos.clear();
m_regInfos.clear();
m_nextTimeoutTimestamp = 0;
m_lastGwSearchTimestamp = 0;
m_lastRecvMsgTimestamp = 0;
m_lastSentMsgTimestamp = 0;
m_lastPingTimestamp = 0;
m_pingCount = 0;
m_connectionStatus = ConnectionStatus::Disconnected;
m_currOp = Op::None;
m_tickDelay = 0U;
checkGwSearchReq();
programNextTimeout();
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode stop()
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
auto guard = apiCall();
m_running = false;
return CC_MqttsnErrorCode_Success;
}
void tick()
{
if (!m_running) {
return;
}
COMMS_ASSERT(m_callStackCount == 0U);
m_timestamp += m_tickDelay;
m_tickDelay = 0U;
checkTimeouts();
programNextTimeout();
}
std::size_t processData(ReadIterator& iter, std::size_t len)
{
if (!m_running) {
return 0U;
}
auto guard = apiCall();
std::size_t consumed = 0;
while (true) {
auto iterTmp = iter;
MsgPtr msg;
auto es = m_stack.read(msg, iterTmp, len - consumed);
if (es == comms::ErrorStatus::NotEnoughData) {
break;
}
if (es == comms::ErrorStatus::ProtocolError) {
++iter;
continue;
}
if (es == comms::ErrorStatus::Success) {
COMMS_ASSERT(msg);
m_lastRecvMsgTimestamp = m_timestamp;
msg->dispatch(*this);
}
consumed += static_cast<std::size_t>(std::distance(iter, iterTmp));
iter = iterTmp;
}
return consumed;
}
bool cancel()
{
if (m_currOp == Op::None) {
return false;
}
COMMS_ASSERT(m_running);
auto guard = apiCall();
auto fn = getFinaliseFunc();
COMMS_ASSERT(fn != nullptr);
(this->*(fn))(CC_MqttsnAsyncOpStatus_Aborted);
return true;
}
CC_MqttsnErrorCode connect(
const char* clientId,
unsigned short keepAlivePeriod,
bool cleanSession,
const CC_MqttsnWillInfo* willInfo,
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus != ConnectionStatus::Disconnected) {
return CC_MqttsnErrorCode_AlreadyConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
auto guard = apiCall();
m_currOp = Op::Connect;
auto* connectOp = newAsyncOp<ConnectOp>(callback, data);
if (willInfo != nullptr) {
connectOp->m_willInfo = *willInfo;
connectOp->m_hasWill = true;
}
connectOp->m_clientId = clientId;
connectOp->m_keepAlivePeriod = keepAlivePeriod;
connectOp->m_cleanSession = cleanSession;
bool result = doConnect();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode reconnect(
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus == ConnectionStatus::Disconnected) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if (callback == nullptr) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
m_currOp = Op::Connect;
auto* op = newAsyncOp<ConnectOp>(callback, data);
op->m_clientId = m_clientId.c_str();
op->m_keepAlivePeriod = static_cast<decltype(op->m_keepAlivePeriod)>(m_keepAlivePeriod / 1000);
op->m_hasWill = false;
op->m_cleanSession = false;
bool result = doConnect();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode disconnect(
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus == ConnectionStatus::Disconnected) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
auto guard = apiCall();
m_currOp = Op::Disconnect;
auto* op = newAsyncOp<DisconnectOp>(callback, data);
static_cast<void>(op);
bool result = doDisconnect();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode publish(
CC_MqttsnTopicId topicId,
const std::uint8_t* msg,
std::size_t msgLen,
CC_MqttsnQoS qos,
bool retain,
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if ((m_connectionStatus != ConnectionStatus::Connected) && (qos != CC_MqttsnQoS_NoGwPublish)) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if ((qos < CC_MqttsnQoS_NoGwPublish) ||
(CC_MqttsnQoS_ExactlyOnceDelivery < qos)) {
return CC_MqttsnErrorCode_BadParam;
}
if ((callback == nullptr) && (CC_MqttsnQoS_AtLeastOnceDelivery <= qos)) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
if (CC_MqttsnQoS_AtLeastOnceDelivery <= qos) {
m_currOp = Op::PublishId;
auto* pubOp = newAsyncOp<PublishIdOp>(callback, data);
pubOp->m_topicId = topicId;
pubOp->m_msg = msg;
pubOp->m_msgLen = msgLen;
pubOp->m_qos = qos;
pubOp->m_retain = retain;
bool result = doPublishId();
static_cast<void>(result);
COMMS_ASSERT(result);
}
else {
sendPublish(
topicId,
0,
msg,
msgLen,
TopicIdTypeVal::PredefinedTopicId,
details::translateQosValue(qos),
retain,
false);
if (callback != nullptr) {
callback(data, CC_MqttsnAsyncOpStatus_Successful);
}
}
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode publish(
const char* topic,
const std::uint8_t* msg,
std::size_t msgLen,
CC_MqttsnQoS qos,
bool retain,
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if ((m_connectionStatus != ConnectionStatus::Connected) && (qos != CC_MqttsnQoS_NoGwPublish)) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if ((qos < CC_MqttsnQoS_AtMostOnceDelivery) ||
(CC_MqttsnQoS_ExactlyOnceDelivery < qos) ||
(callback == nullptr)) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
m_currOp = Op::Publish;
auto* pubOp = newOp<PublishOp>();
pubOp->m_topic = topic;
pubOp->m_msg = msg;
pubOp->m_msgLen = msgLen;
pubOp->m_qos = qos;
pubOp->m_retain = retain;
pubOp->m_cb = callback;
pubOp->m_cbData = data;
pubOp->m_shortName = isShortTopicName(topic);
if (pubOp->m_shortName) {
pubOp->m_topicId = shortTopicToTopicId(topic);
}
bool result = doPublish();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode subscribe(
CC_MqttsnTopicId topicId,
CC_MqttsnQoS qos,
CC_MqttsnSubscribeCompleteReportFn callback,
void* data
)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus != ConnectionStatus::Connected) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if ((qos < CC_MqttsnQoS_AtMostOnceDelivery) ||
(CC_MqttsnQoS_ExactlyOnceDelivery < qos) ||
(callback == nullptr)) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
m_currOp = Op::SubscribeId;
auto* op = newOp<SubscribeIdOp>();
op->m_topicId = topicId;
op->m_qos = qos;
op->m_cb = callback;
op->m_cbData = data;
op->m_msgId = allocMsgId();
bool result = doSubscribeId();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode subscribe(
const char* topic,
CC_MqttsnQoS qos,
CC_MqttsnSubscribeCompleteReportFn callback,
void* data
)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus != ConnectionStatus::Connected) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if ((qos < CC_MqttsnQoS_AtMostOnceDelivery) ||
(CC_MqttsnQoS_ExactlyOnceDelivery < qos) ||
(topic == nullptr) ||
(callback == nullptr)) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
m_currOp = Op::Subscribe;
auto* op = newOp<SubscribeOp>();
op->m_topic = topic;
op->m_qos = qos;
op->m_cb = callback;
op->m_cbData = data;
op->m_msgId = allocMsgId();
op->m_usingShortTopicName = isShortTopicName(topic);
if (op->m_usingShortTopicName) {
op->m_topicId = shortTopicToTopicId(topic);
}
bool result = doSubscribe();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode unsubscribe(
CC_MqttsnTopicId topicId,
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data
)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus != ConnectionStatus::Connected) {
return CC_MqttsnErrorCode_NotConnected;
}
if (m_currOp != Op::None) {
return CC_MqttsnErrorCode_Busy;
}
if (callback == nullptr) {
return CC_MqttsnErrorCode_BadParam;
}
auto guard = apiCall();
m_currOp = Op::UnsubscribeId;
auto* op = newAsyncOp<UnsubscribeIdOp>(callback, data);
op->m_topicId = topicId;
op->m_msgId = allocMsgId();
bool result = doUnsubscribeId();
static_cast<void>(result);
COMMS_ASSERT(result);
return CC_MqttsnErrorCode_Success;
}
CC_MqttsnErrorCode unsubscribe(
const char* topic,
CC_MqttsnAsyncOpCompleteReportFn callback,
void* data)
{
if (!m_running) {
return CC_MqttsnErrorCode_NotStarted;
}
if (m_connectionStatus != ConnectionStatus::Connected) {
return CC_MqttsnErrorCode_NotConnected;
}