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_sock.c
1779 lines (1530 loc) · 58.7 KB
/
u_wifi_sock.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 data API for ble.
*/
#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 "string.h" // memset()
#include "stdio.h" // snprintf()
#include "limits.h" // UINT16_MAX
#include "u_error_common.h"
#include "u_assert.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_cfg_sw.h"
#include "u_port_debug.h"
#include "u_cfg_os_platform_specific.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_sock_errno.h"
#include "u_sock.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_cfg.h"
#include "u_wifi_module_type.h"
#include "u_wifi_sock.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* ------------------------------------------------------------- */
#define U_WIFI_MAX_INSTANCE_COUNT 2
/* ----------------------------------------------------------------
* TYPES
* ------------------------------------------------------------- */
typedef enum {
WIFI_INT_OPT_INVALID = -1,
WIFI_INT_OPT_TCP_NODELAY = 0,
WIFI_INT_OPT_TCP_KEEPIDLE,
WIFI_INT_OPT_TCP_KEEPINTVL,
WIFI_INT_OPT_TCP_KEEPCNT,
/* Sentinel */
WIFI_INT_OPT_MAX
} WifiIntOptId_t;
typedef struct {
int32_t sockHandle; /**< The handle of the socket instance
-1 if this socket is not in use. */
int32_t clientHandle;
bool isClient;
uDeviceHandle_t devHandle; /**< The u-blox device handle.
-1 if this socket is not in use. */
int32_t connHandle; /**< The connection handle that the wifi module
uses for the socket instance.
-1 if this socket is not in use. */
int32_t edmChannel; /**< The EDM stream channel. */
uPortSemaphoreHandle_t semaphore;
uSockType_t type;
uSockProtocol_t protocol;
bool connected;
bool connecting;
bool closing;
uSockAddress_t remoteAddress;
int32_t localPort;
int32_t serverId;
int32_t remotePort;
uShortRangePbufList_t *pTcpRxBuff;
uShortRangePktList_t udpPktList;
int32_t intOpts[WIFI_INT_OPT_MAX];
uWifiSockCallback_t pAsyncClosedCallback; /**< Set to NULL if socket is not in use. */
uWifiSockCallback_t pDataCallback; /**< Set to NULL if socket is not in use. */
uWifiSockCallback_t pClosedCallback; /**< Set to NULL if socket is not in use. */
} uWifiSockSocket_t;
typedef enum {
U_PING_STATUS_WAITING = 0,
U_PING_STATUS_IP_RECEIVED,
U_PING_STATUS_ERROR
} uPingStatus_t;
typedef struct {
volatile uPingStatus_t status;
volatile uSockAddress_t resultSockAddress;
uPortSemaphoreHandle_t semaphore;
} uPingContext_t;
/* ----------------------------------------------------------------
* STATIC VARIABLES
* ------------------------------------------------------------- */
// Keep track of whether we're initialised or not.
static bool gInitialised = false;
/** A list of uDevice handles for the instances
* Each time uWifiSockInitInstance() is called the corresponding
* device handle will be added to this list. We need this in order
* to de-initialize each instance when user calls uWifiSockDeinit()
*/
static uDeviceHandle_t gInstanceDeviceHandleList[U_WIFI_MAX_INSTANCE_COUNT];
/** The sockets: a nice simple array, nothing fancy.
*/
uPortMutexHandle_t gSocketsMutex = NULL;
static uWifiSockSocket_t gSockets[U_WIFI_SOCK_MAX_NUM_SOCKETS];
static uPingContext_t gPingContext;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/* Workaround for WiFi captive portal. Used to control the accept()
timeout for now. Will be removed once a full select() implementation
is available.
*/
int32_t gUWifiSocketAcceptTimeoutS = -1;
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* ------------------------------------------------------------- */
static void freeSocket(uWifiSockSocket_t *pSock)
{
if (pSock != NULL) {
pSock->sockHandle = -1;
pSock->edmChannel = -1;
pSock->isClient = false;
pSock->connected = false;
if (pSock->semaphore != NULL) {
uPortSemaphoreDelete(pSock->semaphore);
pSock->semaphore = NULL;
}
}
}
static uWifiSockSocket_t *pAllocateSocket(uDeviceHandle_t devHandle)
{
bool outOfMemory = false;
uWifiSockSocket_t *pSock = NULL;
if (uPortMutexLock(gSocketsMutex) != 0) {
return NULL;
}
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
if (gSockets[index].sockHandle == -1) {
int32_t tmp;
pSock = &(gSockets[index]);
pSock->sockHandle = index;
pSock->devHandle = devHandle;
pSock->semaphore = NULL;
tmp = uPortSemaphoreCreate(&(pSock->semaphore), 0, 1);
if (tmp != (int32_t) U_ERROR_COMMON_SUCCESS) {
outOfMemory = true;
break;
}
if (pSock->protocol == U_SOCK_PROTOCOL_TCP) {
pSock->pTcpRxBuff = NULL;
} else if (pSock->protocol == U_SOCK_PROTOCOL_UDP) {
memset((void *)(&pSock->udpPktList), 0, sizeof(uShortRangePktList_t));
}
break;
}
}
if (outOfMemory) {
freeSocket(pSock);
pSock = NULL;
}
uPortMutexUnlock(gSocketsMutex);
return pSock;
}
static void freeAllSockets(void)
{
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
freeSocket(&(gSockets[index]));
}
}
static inline WifiIntOptId_t getIntOptionId(int32_t level, uint32_t option)
{
if (level == U_SOCK_OPT_LEVEL_TCP) {
switch (option) {
case U_SOCK_OPT_TCP_NODELAY:
return WIFI_INT_OPT_TCP_NODELAY;
case U_SOCK_OPT_TCP_KEEPIDLE:
return WIFI_INT_OPT_TCP_KEEPIDLE;
case U_SOCK_OPT_TCP_KEEPINTVL:
return WIFI_INT_OPT_TCP_KEEPINTVL;
case U_SOCK_OPT_TCP_KEEPCNT:
return WIFI_INT_OPT_TCP_KEEPCNT;
default:
break;
}
}
return WIFI_INT_OPT_INVALID;
}
static inline int32_t compareSockAddr(const uSockAddress_t *pAddr1,
const uSockAddress_t *pAddr2)
{
int32_t ret;
ret = memcmp(&(pAddr1->port), &(pAddr2->port), sizeof(pAddr1->port));
if (ret == 0) {
ret = memcmp(&(pAddr1->ipAddress.type),
&(pAddr2->ipAddress.type),
sizeof(pAddr1->ipAddress.type));
}
if (ret == 0) {
if (pAddr1->ipAddress.type == U_SOCK_ADDRESS_TYPE_V4) {
ret = memcmp(&(pAddr1->ipAddress.address.ipv4),
&(pAddr2->ipAddress.address.ipv4),
sizeof(pAddr1->ipAddress.address.ipv4));
} else {
ret = memcmp(&(pAddr1->ipAddress.address.ipv6[0]),
&(pAddr2->ipAddress.address.ipv6[0]),
sizeof(pAddr1->ipAddress.address.ipv6));
}
}
return ret;
}
static int32_t validateSockAddress(const uSockAddress_t *pRemoteAddress)
{
switch (pRemoteAddress->ipAddress.type) {
case U_SOCK_ADDRESS_TYPE_V4:
// Check that address is not 0.0.0.0
if (pRemoteAddress->ipAddress.address.ipv4 == 0) {
return -U_SOCK_EINVAL;
}
break;
case U_SOCK_ADDRESS_TYPE_V6: {
// Check that address is not all zeroes
bool allZero = true;
for (int i = 0; (i < 4) && allZero; i++) {
if (pRemoteAddress->ipAddress.address.ipv6[i] != 0) {
allZero = false;
}
}
if (allZero) {
return -U_SOCK_EINVAL;
}
break;
}
default:
return -U_SOCK_EINVAL;
}
if (pRemoteAddress->port == 0) {
return -U_SOCK_EINVAL;
}
return U_SOCK_ENONE;
}
static uWifiSockSocket_t *pFindConnectingSocketByRemoteAddress(uDeviceHandle_t devHandle,
const uSockAddress_t *pRemoteAddr)
{
uWifiSockSocket_t *pSock = NULL;
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
if ((gSockets[index].sockHandle == index) && // is active socket
(gSockets[index].connecting) && // is connecting
(gSockets[index].devHandle == devHandle) && // correct instance
(compareSockAddr(pRemoteAddr,
&gSockets[index].remoteAddress) == 0)) { // correct remote socket address
pSock = &(gSockets[index]);
break;
}
}
return pSock;
}
static uWifiSockSocket_t *pFindSocketByEdmChannel(uDeviceHandle_t devHandle, int32_t edmChannel)
{
uWifiSockSocket_t *pSock = NULL;
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
if (gSockets[index].sockHandle == index && // is active socket
gSockets[index].devHandle == devHandle && // correct instance
gSockets[index].edmChannel == edmChannel) { // correct edm channel
pSock = &(gSockets[index]);
break;
}
}
return pSock;
}
static uWifiSockSocket_t *pFindClientSocketByPort(uDeviceHandle_t devHandle,
int32_t port)
{
uWifiSockSocket_t *pSock;
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
pSock = &(gSockets[index]);
if (pSock->sockHandle == index &&
pSock->devHandle == devHandle &&
pSock->isClient &&
pSock->localPort == port) {
return pSock;
}
}
return NULL;
}
static uWifiSockSocket_t *pFindOrCreateClientSocket(uDeviceHandle_t devHandle,
const uShortRangeConnectDataIp_t *pConnectData)
{
int32_t localPort;
int32_t remotePort;
uShortRangeIpProtocol_t shortRangeProt;
if (pConnectData->type == U_SHORT_RANGE_CONNECTION_IPv4) {
shortRangeProt = pConnectData->ipv4.protocol;
localPort = pConnectData->ipv4.localPort;
remotePort = pConnectData->ipv4.remotePort;
} else {
shortRangeProt = pConnectData->ipv6.protocol;
localPort = pConnectData->ipv6.localPort;
remotePort = pConnectData->ipv6.remotePort;
}
uSockProtocol_t prot =
shortRangeProt == U_SHORT_RANGE_IP_PROTOCOL_UDP ? U_SOCK_PROTOCOL_UDP : U_SOCK_PROTOCOL_TCP;
uSockType_t type =
prot == U_SOCK_PROTOCOL_UDP ? U_SOCK_TYPE_DGRAM : U_SOCK_TYPE_STREAM;
uWifiSockSocket_t *pSock = NULL;
for (int32_t index = 0; index < U_WIFI_SOCK_MAX_NUM_SOCKETS; index++) {
if (gSockets[index].isClient &&
gSockets[index].devHandle == devHandle &&
gSockets[index].protocol == prot &&
gSockets[index].localPort == localPort &&
gSockets[index].remotePort == remotePort) {
pSock = &(gSockets[index]);
break;
}
}
if (!pSock) {
int32_t sockHandle = uWifiSockCreate(devHandle, type, prot);
if (sockHandle >= 0) {
pSock = &(gSockets[sockHandle]);
pSock->isClient = true;
pSock->localPort = localPort;
pSock->remotePort = remotePort;
uPortLog("U_WIFI_SOCK: Created client socket: %d - %d - %d\n", sockHandle, localPort, remotePort);
} else {
uPortLog("U_WIFI_SOCK: ERROR Failed to create client socket\n");
}
} else {
}
return pSock;
}
static inline int32_t getInstance(uDeviceHandle_t devHandle,
uShortRangePrivateInstance_t **ppInstance)
{
if (!gInitialised) {
return -U_SOCK_EFAULT;
}
*ppInstance = pUShortRangePrivateGetInstance(devHandle);
if (*ppInstance == NULL) {
return -U_SOCK_EINVAL;
}
if ((*ppInstance)->mode != U_SHORT_RANGE_MODE_EDM) {
return -U_SOCK_EIO;
}
return U_SOCK_ENONE;
}
static inline int32_t getInstanceAndSocket(uDeviceHandle_t devHandle, int32_t sockHandle,
uShortRangePrivateInstance_t **ppInstance,
uWifiSockSocket_t **ppSock)
{
int32_t errnoLocal;
*ppSock = NULL;
errnoLocal = getInstance(devHandle, ppInstance);
if (errnoLocal != U_SOCK_ENONE) {
return errnoLocal;
}
errnoLocal = -U_SOCK_EBADFD;
if ((sockHandle >= 0) &&
(sockHandle < U_WIFI_SOCK_MAX_NUM_SOCKETS) &&
(gSockets[sockHandle].sockHandle == sockHandle) &&
(gSockets[sockHandle].devHandle == devHandle)) {
*ppSock = &(gSockets[sockHandle]);
errnoLocal = U_SOCK_ENONE;
}
return errnoLocal;
}
// Get a socket option that has an integer as a
// parameter
static int32_t getOptionInt(uWifiSockSocket_t *pSock,
WifiIntOptId_t option,
void *pOptionValue,
size_t *pOptionValueLength)
{
if ((pOptionValueLength != NULL) && (pOptionValue == NULL)) {
// Caller just wants to know the length required
*pOptionValueLength = sizeof(int32_t);
return U_SOCK_ENONE;
}
if ((pOptionValueLength == NULL) || (pOptionValue == NULL) ||
(*pOptionValueLength < sizeof(int32_t))) {
return -U_SOCK_EINVAL;
}
*((int32_t *)pOptionValue) = pSock->intOpts[option];
return U_SOCK_ENONE;
}
// Set a socket option that has an integer as a parameter
static int32_t setOptionInt(uWifiSockSocket_t *pSock,
WifiIntOptId_t option,
const void *pOptionValue,
size_t optionValueLength)
{
if ((pOptionValue == NULL) || (optionValueLength < sizeof(int32_t))) {
return -U_SOCK_EINVAL;
}
pSock->intOpts[option] = *((const int32_t *)pOptionValue);
return U_SOCK_ENONE;
}
// Convert a short range IP struct to uSockAddress structs
static void convertToSockAddress(const uShortRangeConnectDataIp_t *pShoAddr,
uint16_t *pLocalPort,
uint16_t *pRemotePort,
uSockAddress_t *pRemoteSockAddr)
{
if (pShoAddr->type == U_SHORT_RANGE_CONNECTION_IPv4) {
pRemoteSockAddr->port = pShoAddr->ipv4.remotePort;
pRemoteSockAddr->ipAddress.type = U_SOCK_ADDRESS_TYPE_V4;
pRemoteSockAddr->ipAddress.address.ipv4 = 0;
*pLocalPort = pShoAddr->ipv4.localPort;
*pRemotePort = pShoAddr->ipv4.remotePort;
for (int i = 0; i < 4; i++) {
pRemoteSockAddr->ipAddress.address.ipv4 |=
((uint32_t)pShoAddr->ipv4.remoteAddress[i]) << (8 * (3 - i));
}
} else {
pRemoteSockAddr->port = pShoAddr->ipv6.remotePort;
pRemoteSockAddr->ipAddress.type = U_SOCK_ADDRESS_TYPE_V6;
*pLocalPort = pShoAddr->ipv6.localPort;
*pRemotePort = pShoAddr->ipv6.remotePort;
for (int i = 0; i < 4; i++) {
pRemoteSockAddr->ipAddress.address.ipv6[i] = 0;
for (int j = 0; j < 4; j++) {
//lint -e{679}
pRemoteSockAddr->ipAddress.address.ipv6[i] |=
((uint32_t)pShoAddr->ipv6.remoteAddress[(i * 4) + j]) << (8 * (3 - j));
}
}
}
}
static void atConnectionCallback(uDeviceHandle_t devHandle,
int32_t connHandle,
uShortRangeConnectionEventType_t eventType,
uShortRangeConnectDataIp_t *pConnectData,
void *pCallbackParameter)
{
(void)devHandle;
(void)pConnectData;
(void)devHandle;
(void)pCallbackParameter;
if (eventType == U_SHORT_RANGE_EVENT_CONNECTED) {
uSockAddress_t remoteAddr;
uint16_t localPort;
uint16_t remotePort;
convertToSockAddress(pConnectData,
&localPort,
&remotePort,
&remoteAddr);
uWifiSockSocket_t *pSock = pFindConnectingSocketByRemoteAddress(devHandle, &remoteAddr);
if (!pSock) {
// Incoming client connection
pSock = pFindOrCreateClientSocket(devHandle, pConnectData);
}
if (pSock) {
pSock->connHandle = connHandle;
} else {
uPortLog("U_WIFI_SOCK: ERROR Failed to find socket for connection: %d\n", connHandle);
}
}
}
//lint -e{818} suppress "address could be declared as pointing to const":
// need to follow function signature
static void edmIpConnectionCallback(int32_t edmHandle,
int32_t edmChannel,
uShortRangeConnectionEventType_t eventType,
const uShortRangeConnectDataIp_t *pConnectData,
void *pCallbackParameter)
{
(void)edmHandle;
volatile uDeviceHandle_t devHandle;
volatile int32_t sockHandle = -1;
volatile uWifiSockCallback_t pUserClosedCb = NULL;
volatile uWifiSockCallback_t pUserAsyncClosedCb = NULL;
uWifiSockSocket_t *pSock = NULL;
uShortRangePrivateInstance_t *pInstance = (uShortRangePrivateInstance_t *) pCallbackParameter;
// Basic validation
if (pInstance == NULL || pInstance->atHandle == NULL) {
return;
}
if (uShortRangeLock() != (int32_t) U_ERROR_COMMON_SUCCESS) {
uPortLog("U_WIFI_SOCK: ERROR failed to take lock\n");
}
devHandle = pInstance->devHandle;
switch (eventType) {
case U_SHORT_RANGE_EVENT_CONNECTED: {
uSockAddress_t remoteAddr;
uint16_t localPort;
uint16_t remotePort;
convertToSockAddress(pConnectData,
&localPort,
&remotePort,
&remoteAddr);
pSock = pFindConnectingSocketByRemoteAddress(devHandle, &remoteAddr);
if (pSock) {
pSock->edmChannel = edmChannel;
pSock->connected = true;
pSock->localPort = localPort;
uPortSemaphoreGive(pSock->semaphore);
} else {
pSock = pFindOrCreateClientSocket(devHandle, pConnectData);
if (pSock) {
pSock->edmChannel = edmChannel;
pSock->remoteAddress = remoteAddr;
pSock->connected = true;
} else {
uPortLog("U_WIFI_SOCK: ERROR Failed to find socket for edm: %d\n", edmChannel);
}
}
break;
}
case U_SHORT_RANGE_EVENT_DISCONNECTED: {
pSock = pFindSocketByEdmChannel(devHandle, edmChannel);
if (pSock && pSock->connected) {
sockHandle = pSock->sockHandle;
pSock->connected = false;
pUserClosedCb = pSock->pClosedCallback;
pUserAsyncClosedCb = pSock->pAsyncClosedCallback;
if (pSock->closing) {
// User has called close()
freeSocket(pSock);
}
}
break;
}
default:
break;
}
uShortRangeUnlock();
// Call the user callbacks after the mutex has been unlocked
if (pUserClosedCb) {
pUserClosedCb(devHandle, sockHandle);
}
if (pUserAsyncClosedCb) {
pUserAsyncClosedCb(devHandle, sockHandle);
}
}
static void edmIpDataCallback(int32_t edmHandle, int32_t edmChannel,
uShortRangePbufList_t *pBufList,
void *pCallbackParameter)
{
(void)edmHandle;
volatile uDeviceHandle_t devHandle;
volatile int32_t sockHandle = -1;
volatile uWifiSockCallback_t pUserDataCb = NULL;
uShortRangePrivateInstance_t *pInstance = (uShortRangePrivateInstance_t *) pCallbackParameter;
// Basic validation
if (pInstance == NULL || pInstance->atHandle == NULL) {
return;
}
U_ASSERT( uShortRangeLock() == (int32_t) U_ERROR_COMMON_SUCCESS );
devHandle = pInstance->devHandle;
uWifiSockSocket_t *pSock = pFindSocketByEdmChannel(devHandle, edmChannel);
if (pSock) {
sockHandle = pSock->sockHandle;
if (pSock->protocol == U_SOCK_PROTOCOL_UDP) {
if (uShortRangePktListAppend(&pSock->udpPktList,
pBufList) != (int32_t)U_ERROR_COMMON_SUCCESS) {
uPortLog("U_WIFI_SOCK: UDP pkt insert failed\n");
uShortRangePbufListFree(pBufList);
}
} else {
if (pSock->pTcpRxBuff == NULL) {
pSock->pTcpRxBuff = pBufList;
} else {
uShortRangePbufListMerge(pSock->pTcpRxBuff, pBufList);
}
}
// Schedule user data callback
pUserDataCb = pSock->pDataCallback;
}
uShortRangeUnlock();
// Call the user callback after the mutex has been unlocked
if (pUserDataCb) {
pUserDataCb(devHandle, sockHandle);
}
}
static void UUPING_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
char ipStr[64];
volatile uPingContext_t *pPingCtx = (volatile uPingContext_t *)pParameter;
// default to error
pPingCtx->status = U_PING_STATUS_ERROR;
// retry_num
uAtClientReadInt(atHandle);
// p_size
uAtClientReadInt(atHandle);
// remote_hostname
uAtClientReadString(atHandle, NULL, 256, false);
// remote_ip
if (uAtClientReadString(atHandle, ipStr, sizeof(ipStr), false) > 0) {
int32_t tmp;
// Use a temporary output variable to avoid "Attempt to cast away volatile" lint issue
uSockAddress_t tmpAddr;
tmp = uSockStringToAddress(ipStr, &tmpAddr);
pPingCtx->resultSockAddress = tmpAddr;
if (tmp == (int32_t) U_ERROR_COMMON_SUCCESS) {
pPingCtx->status = U_PING_STATUS_IP_RECEIVED;
}
}
// ttl
uAtClientReadInt(atHandle);
// rtt
uAtClientReadInt(atHandle);
uPortSemaphoreGive(pPingCtx->semaphore);
}
static void UUPINGER_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
(void)atHandle;
volatile uPingContext_t *pPingCtx = (volatile uPingContext_t *)pParameter;
// we received an error
pPingCtx->status = U_PING_STATUS_ERROR;
uPortSemaphoreGive(pPingCtx->semaphore);
}
static int32_t connectPeer(uAtClientHandle_t atHandle,
const uPortSemaphoreHandle_t semaphoreHandle,
const char *pProtocolStr,
const uSockAddress_t *pAddress,
const char *pFlagStr)
{
int32_t errnoLocal = U_SOCK_ENONE;
int32_t tmp;
char ipAddrStr[64];
tmp = uSockIpAddressToString(&pAddress->ipAddress,
ipAddrStr, sizeof(ipAddrStr));
if (tmp <= 0) {
errnoLocal = tmp;
}
if (errnoLocal == U_SOCK_ENONE) {
char portStr[16];
snprintf(portStr, sizeof(portStr), ":%d", pAddress->port);
// Make sure the semaphore is taken
// it could be given by a disconnect event earlier
uPortSemaphoreTryTake(semaphoreHandle, 0);
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDCP=");
uAtClientWritePartialString(atHandle, true, pProtocolStr);
uAtClientWritePartialString(atHandle, false, "://");
uAtClientWritePartialString(atHandle, false, ipAddrStr);
uAtClientWritePartialString(atHandle, false, portStr);
if (pFlagStr) {
uAtClientWritePartialString(atHandle, false, "/?");
uAtClientWritePartialString(atHandle, false, pFlagStr);
}
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UDCP:");
errnoLocal = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
tmp = uAtClientUnlock(atHandle);
if (tmp == (int32_t) U_ERROR_COMMON_SUCCESS) {
if (uPortSemaphoreTryTake(semaphoreHandle, 5000) != 0) {
errnoLocal = -U_SOCK_ETIMEDOUT;
}
} else {
errnoLocal = -U_SOCK_EIO;
}
}
return errnoLocal;
}
static int32_t closePeer(uAtClientHandle_t atHandle,
int32_t connHandle)
{
int32_t errnoLocal = U_SOCK_ENONE;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UDCPC=");
uAtClientWriteInt(atHandle, connHandle);
uAtClientCommandStopReadResponse(atHandle);
// Ignore possible error here as the client may have closed the socket
uAtClientUnlock(atHandle);
return errnoLocal;
}
int32_t deinitInstance(uDeviceHandle_t devHandle)
{
int32_t errnoLocal;
uShortRangePrivateInstance_t *pInstance = NULL;
// First check that uWifiSockInitInstance has been called
// and that the instance is not already de-initialized
errnoLocal = -U_SOCK_EINVAL;
for (int i = 0; i < U_WIFI_MAX_INSTANCE_COUNT; i++) {
if (gInstanceDeviceHandleList[i] == devHandle) {
gInstanceDeviceHandleList[i] = NULL;
errnoLocal = U_SOCK_ENONE;
break;
}
}
if (errnoLocal == U_SOCK_ENONE) {
errnoLocal = getInstance(devHandle, &pInstance);
}
if ((errnoLocal == U_SOCK_ENONE) && pInstance) {
int32_t shortRangeEC;
shortRangeEC = uShortRangeEdmStreamIpEventCallbackSet(pInstance->streamHandle,
NULL,
NULL);
if (shortRangeEC == (int32_t) U_ERROR_COMMON_SUCCESS) {
shortRangeEC = uShortRangeEdmStreamDataEventCallbackSet(pInstance->streamHandle,
U_SHORT_RANGE_CONNECTION_TYPE_IP,
NULL,
NULL);
}
if (shortRangeEC == (int32_t)U_ERROR_COMMON_SUCCESS) {
shortRangeEC = uShortRangeSetIpConnectionStatusCallback(
devHandle, NULL, NULL);
}
if (shortRangeEC != (int32_t)U_ERROR_COMMON_SUCCESS) {
errnoLocal = -U_SOCK_ENOSR;
}
}
return errnoLocal;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS: WORKAROUND FOR LINKER ISSUE
* -------------------------------------------------------------- */
void uWifiSockPrivateLink()
{
//dummy
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* ------------------------------------------------------------- */
// Initialise the wifi sockets layer.
int32_t uWifiSockInit(void)
{
int32_t errnoLocal = U_SOCK_ENONE;
if (uShortRangeLock() != (int32_t) U_ERROR_COMMON_SUCCESS) {
return -U_SOCK_EIO;
}
if (!gInitialised) {
int32_t tmp = uPortSemaphoreCreate(&gPingContext.semaphore, 0, 1);
if (tmp != (int32_t) U_ERROR_COMMON_SUCCESS) {
errnoLocal = -U_SOCK_ENOMEM;
}
if (errnoLocal == U_SOCK_ENONE) {
// Create mutex for protecting the socket lists
errnoLocal = uPortMutexCreate(&gSocketsMutex);
}
for (int i = 0; i < U_WIFI_MAX_INSTANCE_COUNT; i++) {
gInstanceDeviceHandleList[i] = NULL;
}
if (errnoLocal == U_SOCK_ENONE) {
freeAllSockets();
gInitialised = true;
}
}
uShortRangeUnlock();
return errnoLocal;
}
int32_t uWifiSockInitInstance(uDeviceHandle_t devHandle)
{
int32_t errnoLocal;
bool alreadyInit = false;
uShortRangePrivateInstance_t *pInstance = NULL;
if (uShortRangeLock() != (int32_t) U_ERROR_COMMON_SUCCESS) {
return -U_SOCK_EIO;
}
// Check that the instance isn't already initilized
errnoLocal = U_SOCK_ENONE;
for (int i = 0; i < U_WIFI_MAX_INSTANCE_COUNT; i++) {
if (gInstanceDeviceHandleList[i] == devHandle) {
alreadyInit = true;
break;
}
}
if (!alreadyInit) {
// Try to add the wifi handle to the instance list
errnoLocal = -U_SOCK_ENOMEM;
for (int i = 0; i < U_WIFI_MAX_INSTANCE_COUNT; i++) {
if (gInstanceDeviceHandleList[i] == NULL) {
errnoLocal = U_SOCK_ENONE;
gInstanceDeviceHandleList[i] = devHandle;
break;
}
}
if (errnoLocal == U_SOCK_ENONE) {
errnoLocal = getInstance(devHandle, &pInstance);
}
if ((errnoLocal == U_SOCK_ENONE) && pInstance) {
if (pInstance->devHandle == NULL) {
pInstance->devHandle = devHandle;
}
}
if ((errnoLocal == U_SOCK_ENONE) && pInstance) {
int32_t shortRangeEC;
shortRangeEC = uShortRangeEdmStreamIpEventCallbackSet(pInstance->streamHandle,
edmIpConnectionCallback,
pInstance);
if (shortRangeEC == (int32_t) U_ERROR_COMMON_SUCCESS) {
shortRangeEC = uShortRangeEdmStreamDataEventCallbackSet(pInstance->streamHandle,
U_SHORT_RANGE_CONNECTION_TYPE_IP,
edmIpDataCallback,
pInstance);
}
if (shortRangeEC == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Need the AT connection callback as well in order to get the connection handle
shortRangeEC = uShortRangeSetIpConnectionStatusCallback(
devHandle, atConnectionCallback, NULL);
}
if (shortRangeEC != (int32_t) U_ERROR_COMMON_SUCCESS) {
errnoLocal = -U_SOCK_ENOSR;
}
}
}
uShortRangeUnlock();
return errnoLocal;
}
int32_t uWifiSockDeinitInstance(uDeviceHandle_t devHandle)
{
int32_t errnoLocal;
if (uShortRangeLock() != (int32_t) U_ERROR_COMMON_SUCCESS) {
return -U_SOCK_EIO;
}
errnoLocal = deinitInstance(devHandle);
if (gSocketsMutex != NULL) {
uPortMutexDelete(gSocketsMutex);
gSocketsMutex = NULL;
}
uShortRangeUnlock();
return errnoLocal;
}
// Deinitialise the wifi sockets layer.
void uWifiSockDeinit()
{
if (uShortRangeLock() != (int32_t) U_ERROR_COMMON_SUCCESS) {
uPortLog("U_WIFI_SOCK: ERROR - Failed to take lock\n");
return;
}
if (gInitialised) {
for (int i = 0; i < U_WIFI_MAX_INSTANCE_COUNT; i++) {
if (gInstanceDeviceHandleList[i] != NULL) {
deinitInstance(gInstanceDeviceHandleList[i]);
}
}
freeAllSockets();
uPortSemaphoreDelete(gPingContext.semaphore);
if (gSocketsMutex != NULL) {
uPortMutexDelete(gSocketsMutex);
gSocketsMutex = NULL;
}
// Nothing more to do, URCs will have been
// removed on close
gInitialised = false;
}
uShortRangeUnlock();
}
int32_t uWifiSockCreate(uDeviceHandle_t devHandle,
uSockType_t type,
uSockProtocol_t protocol)
{
int32_t sockHandle = -U_SOCK_ENOMEM;
uShortRangePrivateInstance_t *pInstance = NULL;
uWifiSockSocket_t *pSock;
// Find the instance
sockHandle = getInstance(devHandle, &pInstance);
if (sockHandle == U_SOCK_ENONE) {
pSock = pAllocateSocket(devHandle);
if (pSock != NULL) {
pSock->isClient = false;