This repository was archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathu_wifi_mqtt.c
1321 lines (1063 loc) · 43.8 KB
/
u_wifi_mqtt.c
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 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of the u-blox MQTT client API for WiFi.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "ctype.h" // isdigit()
#include "string.h" // memset(), strncpy(), strtok_r()
#include "stdio.h" // snprintf()
#include "u_cfg_sw.h"
#include "u_cfg_os_platform_specific.h"
#include "u_error_common.h"
#include "u_port_clib_platform_specific.h" /* strtok_r and integer stdio, must
be included before the other port
files if any print or scan function
is used. */
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_debug.h"
#include "u_port_event_queue.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_mqtt_common.h"
#include "u_mqtt_client.h"
#include "u_wifi_mqtt.h"
#include "u_short_range_module_type.h"
#include "u_short_range_pbuf.h"
#include "u_short_range.h"
#include "u_short_range_private.h"
#include "u_short_range_edm_stream.h"
#include "u_short_range_sec_tls.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* ------------------------------------------------------------- */
#ifndef U_WIFI_MQTT_DATA_EVENT_STACK_SIZE
/* The stack size for the event queue task, limiting factor being
* ESP32 when it has been pre-built for use with PlatformIO.
*/
# define U_WIFI_MQTT_DATA_EVENT_STACK_SIZE 1600
#endif
#ifndef U_WIFI_MQTT_DATA_EVENT_PRIORITY
# define U_WIFI_MQTT_DATA_EVENT_PRIORITY (U_CFG_OS_PRIORITY_MAX - 5)
#endif
typedef struct uWifiMqttTopic_t {
char *pTopicStr;
int32_t edmChannel;
int32_t peerHandle;
bool isTopicUnsubscribed;
bool isPublish;
uMqttQos_t qos;
bool retain;
struct uWifiMqttTopic_t *pNext;
} uWifiMqttTopic_t;
typedef struct uWifiMqttTopicList_t {
uWifiMqttTopic_t *pHead;
uWifiMqttTopic_t *pTail;
} uWifiMqttTopicList_t;
typedef struct uWifiMqttSession_t {
char *pBrokerNameStr;
char *pClientIdStr;
char *pUserNameStr;
char *pPasswordStr;
bool isConnected;
bool keepAlive;
uShortRangePktList_t rxPkt;
uWifiMqttTopicList_t topicList;
int32_t sessionHandle;
uAtClientHandle_t atHandle;
int32_t localPort;
int32_t unreadMsgsCount;
uPortSemaphoreHandle_t semaphore;
void *pCbParam;
void (*pDataCb)(int32_t unreadMsgsCount, void *pCbParam);
void (*pDisconnectCb)(int32_t status, void *pCbParam);
} uWifiMqttSession_t;
typedef struct {
uWifiMqttSession_t *pMqttSession;
void *pCbParam;
void (*pDataCb)(int32_t unreadMsgsCount, void *pCbParam);
int32_t disconnStatus;
void (*pDisconnectCb)(int32_t disconnStatus, void *pCbParam);
} uCallbackEvent_t;
static uWifiMqttSession_t gMqttSessions[U_WIFI_MQTT_MAX_NUM_CONNECTIONS];
static uPortMutexHandle_t gMqttSessionMutex = NULL;
static int32_t gCallbackQueue = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
static int32_t gEdmChannel = -1;
/**
* Fetch the topic string in a given MQTT session associated to particular EDM channel
*/
static char *getTopicStrForEdmChannel(uWifiMqttSession_t *pMqttSession, int32_t edmChannel)
{
uWifiMqttTopic_t *pTopic;
char *pTopicNameStr = NULL;
for (pTopic = pMqttSession->topicList.pHead; pTopic != NULL; pTopic = pTopic->pNext) {
if (pTopic->edmChannel == edmChannel) {
pTopicNameStr = pTopic->pTopicStr;
}
}
return pTopicNameStr;
}
/**
* Fetch the topic object in a given MQTT session
*/
static uWifiMqttTopic_t *findTopic (uWifiMqttSession_t *pMqttSession, const char *pTopicStr,
bool isPublish)
{
uWifiMqttTopic_t *pTemp;
uWifiMqttTopic_t *pFoundTopic = NULL;
for (pTemp = pMqttSession->topicList.pHead; pTemp != NULL; pTemp = pTemp->pNext) {
//lint -save -e731
if ((strcmp(pTemp->pTopicStr, pTopicStr) == 0) && (isPublish == pTemp->isPublish)) {
pFoundTopic = pTemp;
}
//lint -restore
}
return pFoundTopic;
}
/**
* Allocate topic object and associate it to a given MQTT session
*/
static uWifiMqttTopic_t *pAllocateMqttTopic (uWifiMqttSession_t *pMqttSession, bool isPublish)
{
uWifiMqttTopic_t *pTopic = NULL;
if (pMqttSession != NULL) {
pTopic = (uWifiMqttTopic_t *)pUPortMalloc(sizeof(uWifiMqttTopic_t));
if (pTopic != NULL) {
pTopic->pNext = NULL;
if (pMqttSession->topicList.pHead == NULL) {
pMqttSession->topicList.pHead = pTopic;
} else {
pMqttSession->topicList.pTail->pNext = pTopic;
}
pMqttSession->topicList.pTail = pTopic;
pTopic->peerHandle = -1;
pTopic->edmChannel = -1;
pTopic->isTopicUnsubscribed = false;
pTopic->isPublish = isPublish;
}
}
return pTopic;
}
/**
* Free a specific topic object associated to given MQTT session
*/
static void freeMqttTopic(uWifiMqttSession_t *pMqttSession, uWifiMqttTopic_t *pTopic)
{
uWifiMqttTopic_t *pPrev;
uWifiMqttTopic_t *pCurr;
for (pPrev = NULL, pCurr = pMqttSession->topicList.pHead; pCurr != NULL;
pPrev = pCurr, pCurr = pCurr->pNext) {
if (strcmp(pCurr->pTopicStr, pTopic->pTopicStr) == 0) {
if (pPrev == NULL) {
pMqttSession->topicList.pHead = pCurr->pNext;
} else {
pPrev->pNext = pCurr->pNext;
}
uPortFree(pCurr->pTopicStr);
uPortFree(pCurr);
break;
}
}
}
/**
* Free all topic objects associated to given MQTT session
*/
static void freeAllMqttTopics(uWifiMqttSession_t *pMqttSession)
{
uWifiMqttTopic_t *pTemp;
uWifiMqttTopic_t *pNext;
for (pTemp = pMqttSession->topicList.pHead; pTemp != NULL; pTemp = pNext) {
pNext = pTemp->pNext;
uPortFree(pTemp->pTopicStr);
uPortFree(pTemp);
}
pMqttSession->topicList.pHead = NULL;
pMqttSession->topicList.pTail = NULL;
}
static int32_t copyConnectionParams(char **ppMqttSessionParams,
const char *pConnectionParams)
{
size_t len;
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
if (pConnectionParams != NULL) {
len = strlen(pConnectionParams) + 1;
if (ppMqttSessionParams != NULL) {
*ppMqttSessionParams = (char *)pUPortMalloc(len);
if (*ppMqttSessionParams != NULL) {
memset(*ppMqttSessionParams, 0, len);
strncpy(*ppMqttSessionParams, pConnectionParams, len);
err = (int32_t)U_ERROR_COMMON_SUCCESS;
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
}
return err;
}
static uShortRangeSecTlsContext_t *getShoTlsContext(const uMqttClientContext_t *pContext)
{
uShortRangeSecTlsContext_t *pMqttTlsContext = NULL;
if (pContext != NULL) {
if (pContext->pSecurityContext != NULL) {
if (pContext->pSecurityContext->pNetworkSpecific != NULL) {
pMqttTlsContext = (uShortRangeSecTlsContext_t *)pContext->pSecurityContext->pNetworkSpecific;
}
}
}
uPortLog("MQTT SHO TLS context %p\n", pMqttTlsContext);
return pMqttTlsContext;
}
/**
* Establish connection to a given broker. Report the disconnection
* to the user via a callback in case of connection failure
*/
static int32_t establishMqttConnectionToBroker(const uMqttClientContext_t *pContext,
uWifiMqttSession_t *pMqttSession,
uWifiMqttTopic_t *pTopic,
bool isPublish)
{
char url[200];
uAtClientHandle_t atHandle;
int32_t err = (int32_t)U_ERROR_COMMON_SUCCESS;
int32_t len;
uShortRangeSecTlsContext_t *pMqttTlsContext;
memset(url, 0, sizeof(url));
// Add the port number unless it is already present
char port[10] = {0};
if (!strstr(pMqttSession->pBrokerNameStr, ":")) {
snprintf(port, sizeof(port), ":%d", (int)pMqttSession->localPort);
}
if (isPublish) {
len = snprintf(url, sizeof(url), "mqtt://%s%s/?pt=%s&retain=%d&qos=%d",
pMqttSession->pBrokerNameStr,
port,
pTopic->pTopicStr,
pTopic->retain,
pTopic->qos);
} else {
len = snprintf(url, sizeof(url), "mqtt://%s%s/?st=%s&qos=%d",
pMqttSession->pBrokerNameStr,
port,
pTopic->pTopicStr,
pTopic->qos);
}
if (pMqttSession->pClientIdStr) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&client=%s", pMqttSession->pClientIdStr);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
if (pMqttSession->pUserNameStr) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&user=%s", pMqttSession->pUserNameStr);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
if (pMqttSession->pPasswordStr) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&passwd=%s", pMqttSession->pPasswordStr);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
// TBD: KeepAlive parameter in uMqttClientConnection_t is a bool and it needs to
// be changed to uint16_t so that user try out different value acceptable by the broker.
if (pMqttSession->keepAlive) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&keepAlive=%d", 60);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
pMqttTlsContext = getShoTlsContext(pContext);
if (pMqttTlsContext != NULL) {
if (pMqttTlsContext->pRootCaCertificateName) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&ca=%s", pMqttTlsContext->pRootCaCertificateName);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
if (pMqttTlsContext->pClientCertificateName) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&cert=%s", pMqttTlsContext->pClientCertificateName);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
if (pMqttTlsContext->pClientPrivateKeyName) {
if (len < (int32_t)sizeof(url)) {
len += snprintf(&url[len], sizeof(url), "&privKey=%s", pMqttTlsContext->pClientPrivateKeyName);
} else {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
}
if (len >= (int32_t)sizeof(url)) {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
atHandle = pMqttSession->atHandle;
uPortLog("U_WIFI_MQTT: Sending AT+UDCP\n");
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDCP=");
uAtClientWriteString(atHandle, (char *)&url[0], false);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UDCP:");
pTopic->peerHandle = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
err = uAtClientUnlock(atHandle);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (uPortSemaphoreTryTake(pMqttSession->semaphore, 5000) != (int32_t)U_ERROR_COMMON_SUCCESS) {
err = (int32_t)U_ERROR_COMMON_TIMEOUT;
}
}
// Report to user that we are disconnected
if (err == (int32_t)U_ERROR_COMMON_TIMEOUT) {
pMqttSession->isConnected = false;
// Remove the topic from the mqtt session
freeMqttTopic(pMqttSession, pTopic);
if (pMqttSession->pDisconnectCb) {
//lint -save -e785
uCallbackEvent_t event = {
.pDataCb = NULL,
.pDisconnectCb = pMqttSession->pDisconnectCb,
.pCbParam = pMqttSession->pCbParam,
.pMqttSession = pMqttSession,
.disconnStatus = err
};
//lint -restore
uPortEventQueueSend(gCallbackQueue, &event, sizeof(event));
}
}
}
uPortLog("U_WIFI_MQTT: MQTT connection err = %d\n", err);
return err;
}
/**
* Disconnect from a given broker.
*/
static int32_t disconnectMqttConnectionToBroker(uWifiMqttSession_t *pMqttSession)
{
uAtClientHandle_t atHandle;
uWifiMqttTopic_t *topic;
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
atHandle = pMqttSession->atHandle;
// Possible bug in u-connect S/W requires a
// delay between EDM data write and disconnect
uPortTaskBlock(1000);
for (topic = pMqttSession->topicList.pHead; topic != NULL; topic = topic->pNext) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDCPC=");
uAtClientWriteInt(atHandle, topic->peerHandle);
uAtClientCommandStopReadResponse(atHandle);
err = uAtClientUnlock(atHandle);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (uPortSemaphoreTryTake(pMqttSession->semaphore, 5000) != (int32_t)U_ERROR_COMMON_SUCCESS) {
err = (int32_t)U_ERROR_COMMON_TIMEOUT;
}
}
uPortLog("U_WIFI_MQTT: MQTT disconnection err = %d\n", err);
}
return err;
}
/**
* Callback to handle both data available and disconnection events
*/
static void onCallbackEvent(void *pParam, size_t eventSize)
{
uCallbackEvent_t *pCbEvent = (uCallbackEvent_t *)pParam;
uWifiMqttSession_t *pMqttSession = (uWifiMqttSession_t *)pCbEvent->pMqttSession;
int unreadMsgsCount = 0;
(void) eventSize;
if (pCbEvent->pDataCb) {
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
if (pMqttSession != NULL) {
unreadMsgsCount = pMqttSession->unreadMsgsCount;
}
U_PORT_MUTEX_UNLOCK(gMqttSessionMutex);
pCbEvent->pDataCb(unreadMsgsCount, pCbEvent->pCbParam);
} else if (pCbEvent->pDisconnectCb) {
pCbEvent->pDisconnectCb(pCbEvent->disconnStatus, pCbEvent->pCbParam);
}
}
static void edmMqttDataCallback(int32_t edmHandle, int32_t edmChannel,
uShortRangePbufList_t *pBufList,
void *pCallbackParameter)
{
uWifiMqttSession_t *pMqttSession = NULL;
uWifiMqttTopic_t *pTopic;
int32_t i;
(void) edmHandle;
(void)pCallbackParameter;
bool doCallback;
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
for (i = 0; i < U_WIFI_MQTT_MAX_NUM_CONNECTIONS; i++) {
pMqttSession = &gMqttSessions[i];
for (pTopic = pMqttSession->topicList.pHead; pTopic != NULL; pTopic = pTopic->pNext) {
if ((pTopic->edmChannel == edmChannel) && (!pTopic->isTopicUnsubscribed)) {
uPortLog("U_WIFI_MQTT: EDM data event for channel %d\n", edmChannel);
if (uShortRangePktListAppend(&pMqttSession->rxPkt,
pBufList) == (int32_t)U_ERROR_COMMON_SUCCESS) {
doCallback = (pMqttSession->rxPkt.pktCount > pMqttSession->unreadMsgsCount);
pMqttSession->unreadMsgsCount = pMqttSession->rxPkt.pktCount;
// Schedule user data pDataCb
if ((pMqttSession->pDataCb) && doCallback) {
//lint -save -e785
uCallbackEvent_t event = {
.pDataCb = pMqttSession->pDataCb,
.pDisconnectCb = NULL,
.pCbParam = pMqttSession->pCbParam,
.pMqttSession = pMqttSession
};
//lint -restore
uPortEventQueueSend(gCallbackQueue, &event, sizeof(event));
}
} else {
uPortLog("U_WIFI_MQTT: Pkt insert failed\n");
uShortRangePbufListFree(pBufList);
}
}
}
}
U_PORT_MUTEX_UNLOCK(gMqttSessionMutex);
}
static void edmIpConnectionCallback(int32_t edmHandle,
int32_t edmChannel,
uShortRangeConnectionEventType_t eventType,
const uShortRangeConnectDataIp_t *pConnectData,
void *pCallbackParameter)
{
(void)pConnectData;
(void)edmHandle;
(void)pCallbackParameter;
switch (eventType) {
case U_SHORT_RANGE_EVENT_CONNECTED:
uPortLog("U_WIFI_MQTT: EDM connect event for channel %d\n", edmChannel);
gEdmChannel = edmChannel;
break;
case U_SHORT_RANGE_EVENT_DISCONNECTED:
uPortLog("U_WIFI_MQTT: EDM disconnect event for channel %d\n", edmChannel);
gEdmChannel = -1;
break;
}
}
static void atMqttConnectionCallback(uDeviceHandle_t devHandle,
int32_t connHandle,
uShortRangeConnectionEventType_t eventType,
uShortRangeConnectDataIp_t *pConnectData,
void *pCallbackParameter)
{
uWifiMqttSession_t *pMqttSession;
uWifiMqttTopic_t *pTopic;
int32_t i;
bool topicFound = false;
(void)devHandle;
(void)pConnectData;
(void)pCallbackParameter;
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
for (i = 0; i < U_WIFI_MQTT_MAX_NUM_CONNECTIONS; i++) {
pMqttSession = &gMqttSessions[i];
for (pTopic = pMqttSession->topicList.pHead; pTopic != NULL; pTopic = pTopic->pNext) {
if (pTopic->peerHandle == connHandle) {
switch (eventType) {
case U_SHORT_RANGE_EVENT_CONNECTED:
uPortLog("U_WIFI_MQTT: AT+UUDCPC connect event for connHandle %d\n", connHandle);
pTopic->edmChannel = gEdmChannel;
pTopic->peerHandle = connHandle;
topicFound = true;
break;
case U_SHORT_RANGE_EVENT_DISCONNECTED:
uPortLog("U_WIFI_MQTT: AT+UUDCPC disconnect event for connHandle %d\n", connHandle);
pTopic->peerHandle = -1;
pTopic->edmChannel = -1;
topicFound = true;
pMqttSession->isConnected = false;
// Report to user that we are disconnected
if (pMqttSession->pDisconnectCb) {
//lint -save -e785
uCallbackEvent_t event = {0}; // Constructed this way to keep Valgrind happy
event.pDisconnectCb = pMqttSession->pDisconnectCb;
event.pCbParam = pMqttSession->pCbParam;
event.pMqttSession = pMqttSession;
event.disconnStatus = (int32_t)U_ERROR_COMMON_SUCCESS;
//lint -restore
uPortEventQueueSend(gCallbackQueue, &event, sizeof(event));
}
break;
}
}
if (topicFound) {
break;
}
}
if (topicFound) {
break;
}
}
U_PORT_MUTEX_UNLOCK(gMqttSessionMutex);
uPortSemaphoreGive(pMqttSession->semaphore);
}
static int32_t getInstance(uDeviceHandle_t devHandle, uShortRangePrivateInstance_t **ppInstance)
{
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
if (ppInstance != NULL) {
*ppInstance = pUShortRangePrivateGetInstance(devHandle);
if (*ppInstance != NULL) {
if ((*ppInstance)->mode == U_SHORT_RANGE_MODE_EDM) {
err = (int32_t)U_ERROR_COMMON_SUCCESS;
}
} else {
uPortLog("U_WIFI_MQTT: sho instance failed err = %d\n", err);
}
}
return err;
}
static int32_t getMqttInstance(const uMqttClientContext_t *pContext,
uShortRangePrivateInstance_t **ppInstance,
uWifiMqttSession_t **ppMqttSession)
{
int32_t err;
err = getInstance(pContext->devHandle, ppInstance);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
if (ppMqttSession != NULL) {
err = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
*ppMqttSession = (uWifiMqttSession_t *)pContext->pPriv;
if (*ppMqttSession != NULL) {
err = (int32_t)U_ERROR_COMMON_SUCCESS;
}
}
}
return err;
}
static void freeMqttSession(uWifiMqttSession_t *pMqttSession)
{
if (pMqttSession != NULL) {
if (pMqttSession->pClientIdStr) {
uPortFree(pMqttSession->pClientIdStr);
}
if (pMqttSession->pPasswordStr) {
uPortFree(pMqttSession->pPasswordStr);
}
if (pMqttSession->pUserNameStr) {
uPortFree(pMqttSession->pUserNameStr);
}
if (pMqttSession->pBrokerNameStr) {
uPortFree(pMqttSession->pBrokerNameStr);
}
if (pMqttSession->semaphore) {
uPortSemaphoreDelete(pMqttSession->semaphore);
}
if (pMqttSession->topicList.pHead) {
freeAllMqttTopics(pMqttSession);
}
memset(pMqttSession, 0, sizeof(uWifiMqttSession_t));
pMqttSession->sessionHandle = -1;
}
}
static int32_t initMqttSessions(void)
{
int32_t err;
err = uPortMutexCreate(&gMqttSessionMutex);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
for (int32_t i = 0; i < U_WIFI_MQTT_MAX_NUM_CONNECTIONS; i++) {
freeMqttSession(&gMqttSessions[i]);
}
}
uPortLog("U_WIFI_MQTT: init MQTT session err = %d\n", err);
return err;
}
static void freeMqtt(uMqttClientContext_t *pContext)
{
int32_t count = 0;
uShortRangePrivateInstance_t *pInstance;
for (int32_t i = 0; i < U_WIFI_MQTT_MAX_NUM_CONNECTIONS; i++) {
if (gMqttSessions[i].sessionHandle == -1) {
count++;
}
}
if (count == U_WIFI_MQTT_MAX_NUM_CONNECTIONS) {
uPortMutexDelete(gMqttSessionMutex);
gMqttSessionMutex = NULL;
if (getInstance(pContext->devHandle, &pInstance) == (int32_t)U_ERROR_COMMON_SUCCESS) {
uShortRangeSetMqttConnectionStatusCallback(pContext->devHandle, NULL, NULL);
uShortRangeEdmStreamMqttEventCallbackSet(pInstance->streamHandle, NULL, NULL);
uShortRangeEdmStreamDataEventCallbackSet(pInstance->streamHandle,
U_SHORT_RANGE_CONNECTION_TYPE_MQTT,
NULL,
NULL);
uPortEventQueueClose(gCallbackQueue);
gCallbackQueue = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
}
}
}
static uWifiMqttSession_t *allocateMqttSession(void)
{
uWifiMqttSession_t *pMqttSession = NULL;
for (int32_t i = 0; i < U_WIFI_MQTT_MAX_NUM_CONNECTIONS; i++) {
if (gMqttSessions[i].sessionHandle == -1) {
pMqttSession = &gMqttSessions[i];
pMqttSession->sessionHandle = i;
break;
}
}
return pMqttSession;
}
/**
* Allocate MQTT session based on the given connection params
*/
static int32_t configureMqttSessionConnection(uWifiMqttSession_t *pMqttSession,
const uMqttClientConnection_t *pConnection)
{
int32_t err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
if ((pMqttSession != NULL) && (pConnection != NULL)) {
err = copyConnectionParams(&pMqttSession->pBrokerNameStr,
pConnection->pBrokerNameStr);
if (err == 0) {
if (pConnection->pClientIdStr) {
err = copyConnectionParams(&pMqttSession->pClientIdStr,
pConnection->pClientIdStr);
}
}
if (err == 0) {
if (pConnection->pUserNameStr) {
err = copyConnectionParams(&pMqttSession->pUserNameStr,
pConnection->pUserNameStr);
}
}
if (err == 0) {
if (pConnection->pPasswordStr) {
err = copyConnectionParams(&pMqttSession->pPasswordStr,
pConnection->pPasswordStr);
}
}
if (err == 0) {
pMqttSession->localPort = pConnection->localPort;
pMqttSession->keepAlive = pConnection->keepAlive;
err = uPortSemaphoreCreate(&(pMqttSession->semaphore), 0, 1);
}
memset((void *)(&pMqttSession->rxPkt), 0, sizeof(uShortRangePktList_t));
}
if (err != 0) {
if (err == (int32_t)U_ERROR_COMMON_NO_MEMORY) {
uPortLog("U_WIFI_MQTT: %s Out of memory\n", __func__);
}
if (pMqttSession) {
freeMqttSession(pMqttSession);
}
}
return err;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: WORKAROUND FOR LINKER ISSUE
* -------------------------------------------------------------- */
void uWifiMqttPrivateLink()
{
//dummy
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
int32_t uWifiMqttInit(uDeviceHandle_t devHandle, void **ppMqttSession)
{
uShortRangePrivateInstance_t *pInstance;
int32_t err = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (gMqttSessionMutex == NULL) {
if (initMqttSessions() == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (getInstance(devHandle, &pInstance) == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (pInstance->devHandle == NULL) {
pInstance->devHandle = devHandle;
}
err = uShortRangeSetMqttConnectionStatusCallback(devHandle,
atMqttConnectionCallback,
pInstance);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
err = uShortRangeEdmStreamMqttEventCallbackSet(pInstance->streamHandle,
edmIpConnectionCallback,
pInstance);
if (err != (int32_t)U_ERROR_COMMON_SUCCESS) {
uPortLog("U_WIFI_MQTT: EDM IP event cb register failed err = %d\n", err);
}
} else {
uPortLog("U_WIFI_MQTT: MQTT conn status cb register failed err = %d\n", err);
}
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
err = uShortRangeEdmStreamDataEventCallbackSet(pInstance->streamHandle,
U_SHORT_RANGE_CONNECTION_TYPE_MQTT,
edmMqttDataCallback,
pInstance);
if (err != (int32_t)U_ERROR_COMMON_SUCCESS) {
uPortLog("U_WIFI_MQTT: EDM stream event cb register failed err = %d\n", err);
}
}
}
}
} else {
err = (int32_t)U_ERROR_COMMON_SUCCESS;
}
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
*ppMqttSession = (void *)allocateMqttSession();
if (*ppMqttSession == NULL) {
err = (int32_t)U_ERROR_COMMON_NO_MEMORY;
}
}
uShortRangeUnlock();
} else {
uPortLog("U_WIFI_MQTT: sho lock failed err = %d\n", err);
}
return err;
}
int32_t uWifiMqttConnect(const uMqttClientContext_t *pContext,
const uMqttClientConnection_t *pConnection)
{
uShortRangePrivateInstance_t *pInstance;
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
uWifiMqttSession_t *pMqttSession = (uWifiMqttSession_t *)pContext->pPriv;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (getInstance(pContext->devHandle, &pInstance) == (int32_t)U_ERROR_COMMON_SUCCESS) {
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
err = configureMqttSessionConnection(pMqttSession, pConnection);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
if (pConnection->localPort == -1) {
if (pContext->pSecurityContext != NULL) {
pMqttSession->localPort = U_MQTT_BROKER_PORT_SECURE;
} else {
pMqttSession->localPort = U_MQTT_BROKER_PORT_UNSECURE;
}
}
pMqttSession->atHandle = pInstance->atHandle;
pMqttSession->isConnected = true;
}
U_PORT_MUTEX_UNLOCK(gMqttSessionMutex);
}
uShortRangeUnlock();
} else {
uPortLog("U_WIFI_MQTT: sho lock failed err = %d\n", err);
}
return err;
}
int32_t uWifiMqttSetMessageCallback(const uMqttClientContext_t *pContext,
void (*pCallback) (int32_t, void *),
void *pCallbackParam)
{
uWifiMqttSession_t *pMqttSession;
uShortRangePrivateInstance_t *pInstance;
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
err = getMqttInstance(pContext, &pInstance, &pMqttSession);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
pMqttSession->pDataCb = pCallback;
pMqttSession->pCbParam = pCallbackParam;
if (gCallbackQueue == (int32_t)U_ERROR_COMMON_NOT_INITIALISED) {
gCallbackQueue = uPortEventQueueOpen(onCallbackEvent,
"uWifiMqttCallbackQueue",
sizeof(uCallbackEvent_t),
U_WIFI_MQTT_DATA_EVENT_STACK_SIZE,
U_WIFI_MQTT_DATA_EVENT_PRIORITY,
2 * U_WIFI_MQTT_MAX_NUM_CONNECTIONS);
}
err = (gCallbackQueue >= 0) ? (int32_t)U_ERROR_COMMON_SUCCESS : (int32_t)
U_ERROR_COMMON_NOT_INITIALISED;
U_PORT_MUTEX_UNLOCK(gMqttSessionMutex);
}
uShortRangeUnlock();
}
return err;
}
int32_t uWifiMqttSetDisconnectCallback(const uMqttClientContext_t *pContext,
void (*pCallback) (int32_t, void *),
void *pCallbackParam)
{
uWifiMqttSession_t *pMqttSession;
uShortRangePrivateInstance_t *pInstance;
int32_t err = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
err = getMqttInstance(pContext, &pInstance, &pMqttSession);
if (err == (int32_t)U_ERROR_COMMON_SUCCESS) {
U_PORT_MUTEX_LOCK(gMqttSessionMutex);
pMqttSession->pDisconnectCb = pCallback;
pMqttSession->pCbParam = pCallbackParam;
if (gCallbackQueue == (int32_t)U_ERROR_COMMON_NOT_INITIALISED) {
gCallbackQueue = uPortEventQueueOpen(onCallbackEvent,
"uWifiMqttCallbackQueue",
sizeof(uCallbackEvent_t),
U_WIFI_MQTT_DATA_EVENT_STACK_SIZE,