This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
u_wifi.c
1095 lines (976 loc) · 37.7 KB
/
u_wifi.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 "general" API for Wifi.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "u_cfg_sw.h"
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "stdio.h"
#include "string.h" // memset()
#include "u_error_common.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_debug.h"
#include "u_cfg_os_platform_specific.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_short_range_module_type.h"
#include "u_short_range.h"
#include "u_short_range_private.h"
#include "u_short_range_cfg.h"
#include "u_wifi_module_type.h"
#include "u_wifi.h"
#include "u_wifi_cfg.h"
#include "u_hex_bin_convert.h"
// THe headers below are necessary to work around an Espressif linker problem, see uWifiInit()
#include "u_network.h"
#include "u_network_config_wifi.h"
#include "u_network_private_wifi.h"
#include "u_sock.h"
#include "u_wifi_sock.h" // For uWifiSockPrivateLink()
#include "u_mqtt_common.h"
#include "u_mqtt_client.h"
#include "u_wifi_mqtt.h" // For uWifiMqttPrivateLink()
#include "u_wifi_http_private.h" // For uWifiHttpPrivateLink()
#include "u_wifi_loc_private.h" // For uWifiLocPrivateLink()
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
// Suppress picky lint "not referenced"
//lint -esym(750, U_IFACE_TYPE_UNKNOWN)
//lint -esym(750, U_IFACE_TYPE_WIFI_AP)
//lint -esym(750, U_IFACE_TYPE_ETHERNET)
//lint -esym(750, U_IFACE_TYPE_PPP)
//lint -esym(750, U_IFACE_TYPE_BRIDGE)
//lint -esym(750, U_IFACE_TYPE_BT_PAN)
#define U_IFACE_TYPE_UNKNOWN 0
#define U_IFACE_TYPE_WIFI_STA 1
#define U_IFACE_TYPE_WIFI_AP 2
#define U_IFACE_TYPE_ETHERNET 3
#define U_IFACE_TYPE_PPP 4
#define U_IFACE_TYPE_BRIDGE 5
#define U_IFACE_TYPE_BT_PAN 6
//lint -esym(767, LOG_TAG) Suppress LOG_TAG defined differently in another module
//lint -esym(750, LOG_TAG) Suppress LOG_TAG not referenced
#define LOG_TAG "U_WIFI: "
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
typedef struct {
uDeviceHandle_t devHandle;
int32_t status;
int32_t connId;
int32_t channel;
char bssid[U_WIFI_BSSID_SIZE];
int32_t reason;
} uWifiConnection_t;
typedef struct {
uDeviceHandle_t devHandle;
int32_t interfaceId;
} uWifiworkEvent_t;
typedef enum {
CFG_ACTION_RESET = 0,
CFG_ACTION_STORE = 1,
CFG_ACTION_LOAD = 2,
CFG_ACTION_ACTIVATE = 3,
CFG_ACTION_DEACTIVATE = 4
} uWifiCfgAction_t;
/* ----------------------------------------------------------------
* STATIC VARIABLES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
/** Helper function for getting the short range instance */
static inline int32_t getInstance(uDeviceHandle_t devHandle,
uShortRangePrivateInstance_t **ppInstance)
{
*ppInstance = pUShortRangePrivateGetInstance(devHandle);
if (*ppInstance == NULL) {
return (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
}
if ((*ppInstance)->mode != U_SHORT_RANGE_MODE_EDM) {
return (int32_t) U_WIFI_ERROR_INVALID_MODE;
}
return (int32_t)U_ERROR_COMMON_SUCCESS;
}
/** Helper function for reading a Wifi station config string value */
static int32_t readWifiStaConfigString(uAtClientHandle_t atHandle,
int32_t configId,
int32_t tag,
char *pString,
size_t lengthBytes)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, configId);
uAtClientWriteInt(atHandle, tag);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UWSC:");
uAtClientSkipParameters(atHandle, 2);
retValue = uAtClientReadString(atHandle, pString, lengthBytes, false);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for writing a Wifi station config integer value */
static int32_t writeWifiStaCfgInt(uAtClientHandle_t atHandle,
int32_t cfgId,
int32_t tag,
int32_t value)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, tag);
uAtClientWriteInt(atHandle, value);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for writing a Wifi station config string value */
static int32_t writeWifiStaCfgStr(uAtClientHandle_t atHandle,
int32_t cfgId,
int32_t tag,
const char *pValue)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSC=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, tag);
uAtClientWriteString(atHandle, pValue, true);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for reading a Wifi station status string value */
static int32_t readWifiStaStatusString(uAtClientHandle_t atHandle,
int32_t statusId,
char *pString,
size_t lengthBytes)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSSTAT=");
uAtClientWriteInt(atHandle, statusId);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UWSSTAT:");
// Skip status_id
uAtClientSkipParameters(atHandle, 1);
retValue = uAtClientReadString(atHandle, pString, lengthBytes, false);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for reading a Wifi station status int value */
static int32_t readWifiStaStatusInt(uAtClientHandle_t atHandle,
int32_t statusId)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSSTAT=");
uAtClientWriteInt(atHandle, statusId);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UWSSTAT:");
// Skip status_id
uAtClientSkipParameters(atHandle, 1);
retValue = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for triggering a Wifi station config action */
static int32_t writeWifiStaCfgAction(uAtClientHandle_t atHandle,
int32_t cfgId,
uWifiCfgAction_t action)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWSCA=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, (int32_t) action);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for reading a Wifi station config string value */
static int32_t readWifiApConfigString(uAtClientHandle_t atHandle,
int32_t configId,
int32_t tag,
char *pString,
size_t lengthBytes)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPC=");
uAtClientWriteInt(atHandle, configId);
uAtClientWriteInt(atHandle, tag);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UWAPC:");
uAtClientSkipParameters(atHandle, 2);
retValue = uAtClientReadString(atHandle, pString, lengthBytes, false);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for writing a Wifi access point config integer value */
static int32_t writeWifiApCfgInt(uAtClientHandle_t atHandle,
int32_t cfgId,
int32_t tag,
int32_t value)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPC=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, tag);
uAtClientWriteInt(atHandle, value);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for writing a Wifi access point config string value */
static int32_t writeWifiApCfgStr(uAtClientHandle_t atHandle,
int32_t cfgId,
int32_t tag,
const char *pValue)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPC=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, tag);
uAtClientWriteString(atHandle, pValue, true);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for reading a Wifi access point status int value */
static int32_t readWifiApStatusInt(uAtClientHandle_t atHandle,
int32_t statusId)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPSTAT=");
uAtClientWriteInt(atHandle, statusId);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UWAPSTAT:");
// Skip status_id
uAtClientSkipParameters(atHandle, 1);
retValue = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for triggering a Wifi access point config action */
static int32_t writeWifiApCfgAction(uAtClientHandle_t atHandle,
int32_t cfgId,
uWifiCfgAction_t action)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPCA=");
uAtClientWriteInt(atHandle, cfgId);
uAtClientWriteInt(atHandle, (int32_t)action);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle);
}
/** Helper function for reading an interface status integer */
static int32_t readIfaceStatusInt(uAtClientHandle_t atHandle,
int32_t interfaceId,
int32_t statusId)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UNSTAT=");
uAtClientWriteInt(atHandle, interfaceId);
uAtClientWriteInt(atHandle, statusId);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UNSTAT:");
// Skip interface_id and status_id
uAtClientSkipParameters(atHandle, 2);
retValue = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
/** Helper function for reading an interface status string */
static int32_t readIfaceStatusString(uAtClientHandle_t atHandle,
int32_t interfaceId,
int32_t statusId,
char *pString,
size_t lengthBytes)
{
int32_t retValue;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UNSTAT=");
uAtClientWriteInt(atHandle, interfaceId);
uAtClientWriteInt(atHandle, statusId);
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+UNSTAT:");
// Skip interface_id and status_id
uAtClientSkipParameters(atHandle, 2);
retValue = uAtClientReadString(atHandle, pString, lengthBytes, false);
uAtClientResponseStop(atHandle);
int32_t errorCode = uAtClientUnlock(atHandle);
if (errorCode < 0) {
retValue = errorCode;
}
return retValue;
}
static void wifiConnectCallback(uAtClientHandle_t atHandle,
void *pParameter)
{
volatile uWifiConnectionStatusCallback_t pCallback = NULL;
volatile void *pCallbackParam = NULL;
uWifiConnection_t *pStatus = (uWifiConnection_t *) pParameter;
(void) atHandle;
if (!pStatus) {
return;
}
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
const uShortRangePrivateInstance_t *pInstance;
pInstance = pUShortRangePrivateGetInstance(pStatus->devHandle);
if (pInstance && pInstance->pWifiConnectionStatusCallback) {
pCallback = pInstance->pWifiConnectionStatusCallback;
pCallbackParam = pInstance->pWifiConnectionStatusCallbackParameter;
}
uShortRangeUnlock();
if (pCallback) {
//lint -e(1773) Suppress attempt to cast away volatile
pCallback(pStatus->devHandle,
pStatus->connId,
pStatus->status,
pStatus->channel,
pStatus->bssid,
pStatus->reason,
(void *)pCallbackParam);
}
}
uPortFree(pStatus);
}
//lint -esym(818, pParameter) Suppress pParameter could be const, need to
// follow prototype
static void UUWLE_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
uDeviceHandle_t devHandle = (uDeviceHandle_t)pParameter;
char bssid[U_WIFI_BSSID_SIZE];
int32_t connId;
int32_t channel;
uWifiConnection_t *pStatus;
connId = uAtClientReadInt(atHandle);
(void)uAtClientReadString(atHandle, bssid, U_WIFI_BSSID_SIZE, false);
channel = uAtClientReadInt(atHandle);
pStatus = (uWifiConnection_t *) pUPortMalloc(sizeof(*pStatus));
if (pStatus != NULL) {
pStatus->devHandle = devHandle;
pStatus->connId = connId;
pStatus->status = U_WIFI_CON_STATUS_CONNECTED;
pStatus->channel = channel;
memcpy(pStatus->bssid, bssid, U_WIFI_BSSID_SIZE);
pStatus->reason = 0;
//lint -e(1773) Suppress attempt to cast away volatile
if (uAtClientCallback(atHandle, wifiConnectCallback, pStatus) < 0) {
uPortFree(pStatus);
}
}
}
//lint -esym(818, pParameter) Suppress pParameter could be const, need to
// follow prototype
static void UUWLD_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
uDeviceHandle_t devHandle = (uDeviceHandle_t)pParameter;
int32_t connId;
int32_t reason;
uWifiConnection_t *pStatus;
connId = uAtClientReadInt(atHandle);
reason = uAtClientReadInt(atHandle);
pStatus = (uWifiConnection_t *) pUPortMalloc(sizeof(*pStatus));
if (pStatus != NULL) {
pStatus->devHandle = devHandle;
pStatus->connId = connId;
pStatus->status = U_WIFI_CON_STATUS_DISCONNECTED;
pStatus->channel = 0;
pStatus->bssid[0] = '\0';
pStatus->reason = reason;
if (uAtClientCallback(atHandle, wifiConnectCallback, pStatus) < 0) {
uPortFree(pStatus);
}
}
}
//lint -esym(818, pParameter) Suppress pParameter could be const, need to
// follow prototype
static void networkStatusCallback(uAtClientHandle_t atHandle,
void *pParameter)
{
int32_t ifaceType = -1;
uint32_t statusMask = 0;
int32_t errorCode = -1;
volatile uWifiNetworkStatusCallback_t pCallback = NULL;
volatile void *pCallbackParam = NULL;
const uWifiworkEvent_t *pEvt = (uWifiworkEvent_t *) pParameter;
if (!pEvt) {
return;
}
if (uShortRangeLock() != (int32_t)U_ERROR_COMMON_SUCCESS) {
return;
}
const uShortRangePrivateInstance_t *pInstance;
pInstance = pUShortRangePrivateGetInstance(pEvt->devHandle);
if (pInstance && pInstance->pNetworkStatusCallback) {
pCallback = pInstance->pNetworkStatusCallback;
pCallbackParam = pInstance->pNetworkStatusCallbackParameter;
}
// Before we can call the callback we need to readout the actual network state
if (pCallback) {
// Read out the interface type
ifaceType = readIfaceStatusInt(atHandle, pEvt->interfaceId, 2);
// Normally a check for this interface type being U_IFACE_TYPE_WIFI_STA should
// be made but there is a bug in uConnect which gives the type U_IFACE_TYPE_UNKNOWN
// when the credentials have been restored from persistent memory. This although the
// wifi station has been started. So we assume that this type is also ok
if (ifaceType <= U_IFACE_TYPE_WIFI_AP) {
char ipV4Str[16] = "";
// We are only interested if the IPv6 addr is valid or not. When it's invalid
// it will just say "::". For this reason we only use a small readbuffer below:
char ipV6Str[4] = "";
errorCode = readIfaceStatusString(atHandle, pEvt->interfaceId, 103, ipV4Str, sizeof(ipV4Str));
if (errorCode >= 0) {
errorCode = readIfaceStatusString(atHandle, pEvt->interfaceId, 201, ipV6Str, sizeof(ipV6Str));
}
if (errorCode >= 0) {
static const char invalidIpV4[] = "0.0.0.0";
static const char invalidIpV6[] = "::";
if (strcmp(ipV4Str, invalidIpV4) != 0) {
statusMask |= U_WIFI_STATUS_MASK_IPV4_UP;
}
if (strcmp(ipV6Str, invalidIpV6) != 0) {
statusMask |= U_WIFI_STATUS_MASK_IPV6_UP;
}
}
}
}
// Important: Make sure we unlock the short range mutex before calling callback
uShortRangeUnlock();
if (errorCode >= 0 && pCallback) {
//lint -e(1773) Suppress attempt to cast away volatile
pCallback(pEvt->devHandle, ifaceType, statusMask, (void *)pCallbackParam);
}
uPortFree(pParameter);
}
static void UUNU_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
int32_t interfaceId;
interfaceId = uAtClientReadInt(atHandle);
if (interfaceId >= 0) {
uWifiworkEvent_t *pEvt;
pEvt = (uWifiworkEvent_t *) pUPortMalloc(sizeof(uWifiworkEvent_t));
if (pEvt != NULL) {
pEvt->devHandle = (uDeviceHandle_t)pParameter;
pEvt->interfaceId = interfaceId;
if (uAtClientCallback(atHandle, networkStatusCallback, pEvt) < 0) {
uPortFree(pEvt);
}
}
}
}
static void UUND_urc(uAtClientHandle_t atHandle,
void *pParameter)
{
int32_t interfaceId;
interfaceId = uAtClientReadInt(atHandle);
if (interfaceId >= 0) {
uWifiworkEvent_t *pEvt;
pEvt = (uWifiworkEvent_t *) pUPortMalloc(sizeof(uWifiworkEvent_t));
if (pEvt != NULL) {
pEvt->devHandle = (uDeviceHandle_t)pParameter;
pEvt->interfaceId = interfaceId;
if (uAtClientCallback(atHandle, networkStatusCallback, pEvt) < 0) {
uPortFree(pEvt);
}
}
}
}
static int32_t StaCfgAction(uDeviceHandle_t devHandle, int32_t op)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
errorCode = writeWifiStaCfgAction(atHandle, 0, op);
}
uShortRangeUnlock();
return errorCode;
}
static int32_t ApCfgAction(uDeviceHandle_t devHandle, int32_t op)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
errorCode = writeWifiApCfgAction(atHandle, 0, op);
}
uShortRangeUnlock();
return errorCode;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
// Initialise the wifi driver.
int32_t uWifiInit()
{
// Workaround for Espressif linker missing out files that
// only contain functions which also have weak alternatives
// (see https://www.esp32.com/viewtopic.php?f=13&t=8418&p=35899)
// Basically any file that might end up containing only functions
// that also have WEAK linked counterparts will be lost, so we need
// to add a dummy function in those files and call it from somewhere
// that will always be present in the build, which for Wifi we
// choose to be here
uNetworkPrivateWifiLink();
uWifiSockPrivateLink();
uWifiMqttPrivateLink();
uWifiHttpPrivateLink();
uWifiLocPrivateLink();
return uShortRangeInit();
}
// Shut-down the wifi driver.
void uWifiDeinit()
{
uShortRangeDeinit();
}
int32_t uWifiSetConnectionStatusCallback(uDeviceHandle_t devHandle,
uWifiConnectionStatusCallback_t pCallback,
void *pCallbackParameter)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t) U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientRemoveUrcHandler(pInstance->atHandle, "+UUWLE:");
uAtClientRemoveUrcHandler(pInstance->atHandle, "+UUWLD:");
if (pCallback != NULL) {
pInstance->pWifiConnectionStatusCallback = pCallback;
pInstance->pWifiConnectionStatusCallbackParameter = pCallbackParameter;
uAtClientSetUrcHandler(pInstance->atHandle, "+UUWLE:",
UUWLE_urc, (void *)devHandle);
uAtClientSetUrcHandler(pInstance->atHandle, "+UUWLD:",
UUWLD_urc, (void *)devHandle);
} else {
pInstance->pWifiConnectionStatusCallback = NULL;
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiSetNetworkStatusCallback(uDeviceHandle_t devHandle,
uWifiNetworkStatusCallback_t pCallback,
void *pCallbackParameter)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t) U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientRemoveUrcHandler(pInstance->atHandle, "+UUNU:");
uAtClientRemoveUrcHandler(pInstance->atHandle, "+UUND:");
if (pCallback != NULL) {
pInstance->pNetworkStatusCallback = pCallback;
pInstance->pNetworkStatusCallbackParameter = pCallbackParameter;
uAtClientSetUrcHandler(pInstance->atHandle, "+UUNU:",
UUNU_urc, (void *)devHandle);
uAtClientSetUrcHandler(pInstance->atHandle, "+UUND:",
UUND_urc, (void *)devHandle);
} else {
pInstance->pNetworkStatusCallback = NULL;
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiStationConnect(uDeviceHandle_t devHandle, const char *pSsid,
uWifiAuth_t authentication,
const char *pPassPhrase)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t) U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
// Read connection status
int32_t conStatus = readWifiStaStatusInt(atHandle, 3);
if (conStatus == 2) {
// Wifi already connected. Check if the SSID is the same
char ssid[32 + 1];
errorCode = (int32_t) U_WIFI_ERROR_ALREADY_CONNECTED;
int32_t tmp = readWifiStaStatusString(atHandle, 0, ssid, sizeof(ssid));
if (tmp >= 0) {
// Always accept the current connection if no SSID specified
if ((pSsid == NULL) || (strcmp(ssid, pSsid) == 0)) {
errorCode = (int32_t) U_WIFI_ERROR_ALREADY_CONNECTED_TO_SSID;
}
}
}
// Configure Wifi
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Set Wifi STA inactive on start up
uPortLog(LOG_TAG "Activating wifi STA mode\n");
errorCode = writeWifiStaCfgInt(atHandle, 0, 0, 0);
}
if ((pSsid == NULL) && (conStatus != 2)) {
errorCode = writeWifiStaCfgAction(atHandle, 0, CFG_ACTION_LOAD);
} else {
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Set SSID
errorCode = writeWifiStaCfgStr(atHandle, 0, 2, pSsid);
}
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Set authentication
errorCode = writeWifiStaCfgInt(atHandle, 0, 5, (int32_t)authentication);
}
if ((errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) &&
(authentication != U_WIFI_AUTH_OPEN)) {
// Set PSK/passphrase
errorCode = writeWifiStaCfgStr(atHandle, 0, 8, pPassPhrase);
}
}
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
// Activate wifi
errorCode = writeWifiStaCfgAction(atHandle, 0, CFG_ACTION_ACTIVATE);
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiStationDisconnect(uDeviceHandle_t devHandle)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
// Read connection status
int32_t conStatus = readWifiStaStatusInt(atHandle, 3);
if (conStatus != 0) {
uPortLog(LOG_TAG "De-activating wifi STA mode\n");
errorCode = writeWifiStaCfgAction(atHandle, 0, CFG_ACTION_DEACTIVATE);
} else {
// Wifi is already disabled
errorCode = (int32_t) U_WIFI_ERROR_ALREADY_DISCONNECTED;
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiSetHostName(uDeviceHandle_t devHandle, const char *pHostName)
{
if (!pHostName) {
return U_ERROR_COMMON_INVALID_PARAMETER;
}
int32_t errorCode;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
uShortRangePrivateInstance_t *pInstance;
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UNHN=");
uAtClientWriteString(atHandle, pHostName, false);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiStationStoreConfig(uDeviceHandle_t devHandle, bool erase)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_SUCCESS;
if (erase) {
errorCode = StaCfgAction(devHandle, CFG_ACTION_RESET);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
errorCode = StaCfgAction(devHandle, CFG_ACTION_STORE);
}
return errorCode;
}
bool uWifiStationHasStoredConfig(uDeviceHandle_t devHandle)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
bool has = false;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Load saved credentials and check if there is a valid ssid
uAtClientHandle_t atHandle = pInstance->atHandle;
errorCode = writeWifiStaCfgAction(atHandle, 0, CFG_ACTION_LOAD);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
char ssid[U_WIFI_SSID_SIZE] = {0};
readWifiStaConfigString(atHandle, 0, 2, ssid, sizeof(ssid));
has = ssid[0] != 0;
}
}
uShortRangeUnlock();
return has;
}
int32_t uWifiAccessPointStart(uDeviceHandle_t devHandle,
const char *pSsid,
uWifiAuth_t authentication,
const char *pPassPhrase,
const char *pIpAddress)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
// Read AP status
int32_t conStatus = readWifiApStatusInt(atHandle, 3);
if (conStatus == 1) {
// AP already active
errorCode = writeWifiApCfgAction(atHandle, 0, CFG_ACTION_DEACTIVATE);
uPortTaskBlock(2000);
}
// Configure AP
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uPortLog(LOG_TAG "Activating wifi AP\n");
// Set AP inactive during startup
errorCode = writeWifiApCfgInt(atHandle, 0, 0, 0);
}
if (pSsid == NULL) {
errorCode = writeWifiApCfgAction(atHandle, 0, CFG_ACTION_LOAD);
} else {
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Set SSID
errorCode = writeWifiApCfgStr(atHandle, 0, 2, pSsid);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Set authentication, has two integers as parameters
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UWAPC=");
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 5);
uAtClientWriteInt(atHandle, (int32_t)authentication);
uAtClientWriteInt(atHandle, 1);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
if ((errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) &&
(authentication != U_WIFI_AUTH_OPEN)) {
// Set PSK/passphrase
errorCode = writeWifiApCfgStr(atHandle, 0, 8, pPassPhrase);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Enable DNS
errorCode = writeWifiApCfgInt(atHandle, 0, 106, 1);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Static IP
errorCode = writeWifiApCfgInt(atHandle, 0, 100, 1);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// DNS
errorCode = writeWifiApCfgStr(atHandle, 0, 101, pIpAddress);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
errorCode = writeWifiApCfgStr(atHandle, 0, 103, pIpAddress);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
errorCode = writeWifiApCfgStr(atHandle, 0, 104, pIpAddress);
}
}
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
// Activate AP
errorCode = writeWifiApCfgAction(atHandle, 0, CFG_ACTION_ACTIVATE);
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiAccessPointStop(uDeviceHandle_t devHandle)
{
int32_t errorCode;
uShortRangePrivateInstance_t *pInstance;
errorCode = uShortRangeLock();
if (errorCode != (int32_t)U_ERROR_COMMON_SUCCESS) {
return errorCode;
}
errorCode = getInstance(devHandle, &pInstance);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uAtClientHandle_t atHandle = pInstance->atHandle;
// Read connection status
int32_t conStatus = readWifiApStatusInt(atHandle, 3);
if (conStatus != 0) {
uPortLog(LOG_TAG "Stopping Wifi access point\n");
errorCode = writeWifiApCfgAction(atHandle, 0, CFG_ACTION_DEACTIVATE);
} else {
// Not started
errorCode = (int32_t)U_WIFI_ERROR_AP_NOT_STARTED;
}
}
uShortRangeUnlock();
return errorCode;
}
int32_t uWifiAccessPointStoreConfig(uDeviceHandle_t devHandle, bool erase)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_SUCCESS;
if (erase) {
errorCode = ApCfgAction(devHandle, CFG_ACTION_RESET);
}
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
errorCode = ApCfgAction(devHandle, CFG_ACTION_STORE);
}
return errorCode;
}