forked from ARMmbed/mbed-os
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwhd_wifi_api.c
executable file
·4404 lines (3707 loc) · 153 KB
/
whd_wifi_api.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 2021, Cypress Semiconductor Corporation (an Infineon company)
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
/** @file
* Implements user functions for controlling the Wi-Fi system
*
* This file provides end-user functions which allow actions such as scanning for
* Wi-Fi networks, joining Wi-Fi networks, getting the MAC address, etc
*
*/
#include <stdlib.h>
#include "whd_version.h"
#include "whd_chip_constants.h"
#include "whd_cdc_bdc.h"
#include "whd_thread_internal.h"
#include "whd_debug.h"
#include "whd_utils.h"
#include "whd_wifi_api.h"
#include "whd_buffer_api.h"
#include "whd_wlioctl.h"
#include "whd_types.h"
#include "whd_types_int.h"
/******************************************************
* Constants
******************************************************/
#define WL_CHANSPEC_CHAN_MASK (0x00ff)
#define CHSPEC_CHANNEL(chspec) ( (uint8_t)( (chspec) & WL_CHANSPEC_CHAN_MASK ) )
#define CH20MHZ_CHSPEC(channel) (chanspec_t)( (chanspec_t)(channel) | GET_C_VAR(whd_driver, CHANSPEC_BW_20) | \
GET_C_VAR(whd_driver, CHANSPEC_CTL_SB_NONE) | \
( ( (channel) <= CH_MAX_2G_CHANNEL ) ? GET_C_VAR(whd_driver, \
CHANSPEC_BAND_2G) \
: GET_C_VAR(whd_driver, CHANSPEC_BAND_5G) ) )
#define MAX_SUPPORTED_MCAST_ENTRIES (10)
#define WLC_EVENT_MSG_LINK (0x01)
#define JOIN_ASSOCIATED (uint32_t)(1 << 0)
#define JOIN_AUTHENTICATED (uint32_t)(1 << 1)
#define JOIN_LINK_READY (uint32_t)(1 << 2)
#define JOIN_SECURITY_COMPLETE (uint32_t)(1 << 3)
#define JOIN_SSID_SET (uint32_t)(1 << 4)
#define JOIN_NO_NETWORKS (uint32_t)(1 << 5)
#define JOIN_EAPOL_KEY_M1_TIMEOUT (uint32_t)(1 << 6)
#define JOIN_EAPOL_KEY_M3_TIMEOUT (uint32_t)(1 << 7)
#define JOIN_EAPOL_KEY_G1_TIMEOUT (uint32_t)(1 << 8)
#define JOIN_EAPOL_KEY_FAILURE (uint32_t)(1 << 9)
#define JOIN_SECURITY_FLAGS_MASK (JOIN_SECURITY_COMPLETE | JOIN_EAPOL_KEY_M1_TIMEOUT | JOIN_EAPOL_KEY_M3_TIMEOUT | \
JOIN_EAPOL_KEY_G1_TIMEOUT | JOIN_EAPOL_KEY_FAILURE)
#define DEFAULT_JOIN_ATTEMPT_TIMEOUT (7000) /* Overall join attempt timeout in milliseconds. */
#define DEFAULT_EAPOL_KEY_PACKET_TIMEOUT (5000) /* Timeout when waiting for EAPOL key packet M1 or M3 in milliseconds.*/
/* Some APs may be slow to provide M1 and 1000 ms is not long enough for edge of cell. */
#ifndef DEFAULT_PM2_SLEEP_RET_TIME
#define DEFAULT_PM2_SLEEP_RET_TIME (200)
#endif
#define PM2_SLEEP_RET_TIME_MIN (10) /* Minimum return-to-sleep in milliseconds */
#define PM2_SLEEP_RET_TIME_MAX (2000) /* Maximum return-to-sleep in milliseconds */
#define NULL_FRAMES_WITH_PM_SET_LIMIT (100) /* NULL_FRAMES_WITH_PM_SET_LIMIT */
#define RSPEC_KBPS_MASK (0x7f)
#define RSPEC_500KBPS(rate) ( (rate) & RSPEC_KBPS_MASK )
#define RSPEC_TO_KBPS(rate) (RSPEC_500KBPS( (rate) ) * (unsigned int)500)
#define UNSIGNED_CHAR_TO_CHAR(uch) ( (uch) & 0x7f )
#define KEY_MAX_LEN (64) /* Maximum key length */
#define KEY_MIN_LEN (8) /* Minimum key length */
/******************************************************
* Local Structures
******************************************************/
#pragma pack(1)
typedef struct
{
uint32_t entry_count;
whd_mac_t macs[1];
} mcast_list_t;
typedef struct
{
int32_t rssi;
whd_mac_t macs;
} client_rssi_t;
typedef struct
{
whd_sync_scan_result_t *aps;
uint32_t count;
uint32_t offset;
cy_semaphore_t scan_semaphore;
} whd_scan_userdata_t;
#pragma pack()
/******************************************************
* Static Variables
******************************************************/
/* LOOK: !!!When adding events below, please modify whd_event_to_string!!! */
const whd_event_num_t join_events[] =
{
WLC_E_SET_SSID, WLC_E_LINK, WLC_E_AUTH, WLC_E_DEAUTH_IND, WLC_E_DISASSOC_IND, WLC_E_PSK_SUP, WLC_E_CSA_COMPLETE_IND,
WLC_E_NONE
};
static const whd_event_num_t scan_events[] = { WLC_E_ESCAN_RESULT, WLC_E_NONE };
/* Values are in 100's of Kbit/sec (1 = 100Kbit/s). Arranged as:
* [Bit index]
* [0] = 20Mhz only
* [0] = Long GI
* [1] = Short GI
* [1] = 40MHz support
* [0] = Long GI
* [1] = Short GI
*/
static const uint16_t mcs_data_rate_lookup_table[32][2][2] =
{
[0] =
{
{ 65, 72},
{ 135, 150}
},
[1] =
{
{ 130, 144},
{ 270, 300}
},
[2] =
{
{ 195, 217},
{ 405, 450}
},
[3] =
{
{ 260, 289},
{ 540, 600}
},
[4] =
{
{ 390, 433},
{ 810, 900}
},
[5] =
{
{ 520, 578},
{ 1080, 1200}
},
[6] =
{
{ 585, 650},
{ 1215, 1350}
},
[7] =
{
{ 650, 722},
{ 1350, 1500}
},
[8] =
{
{ 130, 144},
{ 270, 300}
},
[9] =
{
{ 260, 289},
{ 540, 600}
},
[10] =
{
{ 390, 433},
{ 810, 900}
},
[11] =
{
{ 520, 578},
{ 1080, 1200}
},
[12] =
{
{ 780, 867},
{ 1620, 1800}
},
[13] =
{
{ 1040, 1156},
{ 2160, 2400}
},
[14] =
{
{ 1170, 1300},
{ 2430, 2700}
},
[15] =
{
{ 1300, 1444},
{ 2700, 3000}
},
[16] =
{
{ 195, 217},
{ 405, 450}
},
[17] =
{
{ 390, 433},
{ 810, 900}
},
[18] =
{
{ 585, 650},
{ 1215, 1350}
},
[19] =
{
{ 780, 867},
{ 1620, 1800}
},
[20] =
{
{ 1170, 1300},
{ 2430, 2700}
},
[21] =
{
{ 1560, 1733},
{ 3240, 3600}
},
[22] =
{
{ 1755, 1950},
{ 3645, 4050}
},
[23] =
{
{ 1950, 2167},
{ 4050, 4500}
},
[24] =
{
{ 260, 288},
{ 540, 600}
},
[25] =
{
{ 520, 576},
{ 1080, 1200}
},
[26] =
{
{ 780, 868},
{ 1620, 1800}
},
[27] =
{
{ 1040, 1156},
{ 2160, 2400}
},
[28] =
{
{ 1560, 1732},
{ 3240, 3600}
},
[29] =
{
{ 2080, 2312},
{ 4320, 4800}
},
[30] =
{
{ 2340, 2600},
{ 4860, 5400}
},
[31] =
{
{ 2600, 2888},
{ 5400, 6000}
},
};
static whd_wifi_join_timeout = DEFAULT_JOIN_ATTEMPT_TIMEOUT;
/******************************************************
* Static Function prototypes
******************************************************/
static void *whd_wifi_join_events_handler(whd_interface_t ifp, const whd_event_header_t *event_header,
const uint8_t *event_data, void *handler_user_data);
static void *whd_wifi_scan_events_handler(whd_interface_t ifp, const whd_event_header_t *event_header,
const uint8_t *event_data,
void *handler_user_data);
static uint32_t whd_wifi_prepare_join(whd_interface_t ifp,
whd_security_t security,
const uint8_t *security_key,
uint8_t key_length,
cy_semaphore_t *semaphore);
static uint32_t whd_wifi_check_join_status(whd_interface_t ifp);
static void whd_wifi_active_join_deinit(whd_interface_t ifp, cy_semaphore_t *stack_semaphore,
whd_result_t result);
static uint32_t whd_wifi_active_join_init(whd_interface_t ifp, whd_security_t auth_type,
const uint8_t *security_key, uint8_t key_length,
cy_semaphore_t *semaphore);
/** Sets the current EAPOL key timeout for the given interface
*
* @param interface : the interface for which we want to set the EAPOL key timeout
* eapol_key_timeout : EAPOL key timeout value
*
* @return WHD_SUCCESS : if success
* Error code : error code to indicate the type of error
*/
static uint32_t whd_wifi_set_supplicant_key_timeout(whd_interface_t ifp, int32_t eapol_key_timeout);
/******************************************************
* Function definitions
******************************************************/
inline wl_chanspec_t whd_channel_to_wl_band(whd_driver_t whd_driver, uint32_t channel)
{
return ( ( (channel) <= CH_MAX_2G_CHANNEL ) ? (uint16_t)GET_C_VAR(whd_driver, CHANSPEC_BAND_2G) :
(uint16_t)GET_C_VAR(whd_driver, CHANSPEC_BAND_5G) );
}
uint32_t whd_wifi_set_up(whd_interface_t ifp)
{
whd_mac_t mac;
char version[200];
whd_driver_t whd_driver;
CHECK_IFP_NULL(ifp);
whd_driver = ifp->whd_driver;
if (whd_driver->internal_info.whd_wlan_status.state == WLAN_UP)
{
WPRINT_WHD_INFO( ("whd_wifi_set_up: already up.\n") );
return WHD_SUCCESS;
}
/* Send UP command */
CHECK_RETURN(whd_wifi_set_ioctl_buffer(ifp, WLC_UP, NULL, 0) );
if (whd_wifi_get_mac_address(ifp, &mac) == WHD_SUCCESS)
{
WPRINT_MACRO( ("WLAN MAC Address : %02X:%02X:%02X:%02X:%02X:%02X\n", mac.octet[0], mac.octet[1], mac.octet[2],
mac.octet[3], mac.octet[4], mac.octet[5]) );
}
if (whd_wifi_get_wifi_version(ifp, version, sizeof(version) ) == WHD_SUCCESS)
{
WPRINT_MACRO( ("WLAN Firmware : %s", version) );
}
/* minimize bootloader usage and start time from UART output */
if (whd_wifi_get_clm_version(ifp, version, sizeof(version) ) == WHD_SUCCESS)
{
WPRINT_MACRO( ("WLAN CLM : %s\n", version) );
}
WPRINT_MACRO( ("WHD VERSION : " WHD_VERSION) );
WPRINT_MACRO( (" : " WHD_BRANCH) );
#if defined(__ARMCC_VERSION)
WPRINT_MACRO( (" : ARM CLANG %u", __ARMCC_VERSION) );
#elif defined(__ICCARM__)
WPRINT_MACRO( (" : IAR %u", __VER__) );
#elif defined(__GNUC__)
WPRINT_MACRO( (" : GCC %u.%u", __GNUC__, __GNUC_MINOR__) );
#else
WPRINT_MACRO( (" : UNKNOWN CC") );
#endif
WPRINT_MACRO( (" : " WHD_DATE "\n") );
/* Update wlan status */
whd_driver->internal_info.whd_wlan_status.state = WLAN_UP;
return WHD_SUCCESS;
}
uint32_t whd_wifi_set_down(whd_interface_t ifp)
{
whd_driver_t whd_driver = ifp->whd_driver;
if (whd_driver->internal_info.whd_wlan_status.state != WLAN_UP)
{
WPRINT_WHD_INFO( ("whd_wifi_set_down: already down.\n") );
return WHD_INTERFACE_NOT_UP;
}
/* Send DOWN command */
CHECK_RETURN(whd_wifi_set_ioctl_buffer(ifp, WLC_DOWN, NULL, 0) );
/* Update wlan status */
whd_driver->internal_info.whd_wlan_status.state = WLAN_DOWN;
return WHD_SUCCESS;
}
uint32_t whd_wifi_set_channel(whd_interface_t ifp, uint32_t channel)
{
whd_buffer_t buffer;
uint32_t *data;
wl_chan_switch_t *chan_switch;
whd_driver_t whd_driver;
CHECK_IFP_NULL(ifp);
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
/* Map P2P interface to either STA or AP interface depending if it's running as group owner or client */
if (ifp->role == WHD_P2P_ROLE)
{
if (whd_driver->internal_info.whd_wifi_p2p_go_is_up == WHD_TRUE)
{
ifp->role = WHD_AP_ROLE;
}
else
{
ifp->role = WHD_STA_ROLE;
}
}
switch (ifp->role)
{
case WHD_STA_ROLE:
data = (uint32_t *)whd_cdc_get_ioctl_buffer(whd_driver, &buffer, sizeof(uint32_t) );
CHECK_IOCTL_BUFFER(data);
*data = htod32(channel);
CHECK_RETURN(whd_cdc_send_ioctl(ifp, CDC_GET, WLC_SET_CHANNEL, buffer, NULL) );
break;
case WHD_AP_ROLE:
chan_switch = (wl_chan_switch_t *)whd_cdc_get_iovar_buffer(whd_driver, &buffer, sizeof(wl_chan_switch_t),
IOVAR_STR_CSA);
CHECK_IOCTL_BUFFER(chan_switch);
chan_switch->chspec =
( wl_chanspec_t )(GET_C_VAR(whd_driver,
CHANSPEC_BW_20) | GET_C_VAR(whd_driver, CHANSPEC_CTL_SB_NONE) | channel);
chan_switch->chspec |= whd_channel_to_wl_band(whd_driver, channel);
chan_switch->chspec = htod16(chan_switch->chspec);
chan_switch->count = 1;
chan_switch->mode = 1;
chan_switch->reg = 0;
CHECK_RETURN(whd_cdc_send_iovar(ifp, CDC_SET, buffer, 0) );
break;
case WHD_P2P_ROLE:
case WHD_INVALID_ROLE:
default:
whd_assert("Bad interface", 0 != 0);
return WHD_UNKNOWN_INTERFACE;
}
return WHD_SUCCESS;
}
uint32_t whd_wifi_get_channel(whd_interface_t ifp, uint32_t *channel)
{
whd_buffer_t buffer;
whd_buffer_t response;
channel_info_t *channel_info;
whd_driver_t whd_driver;
CHECK_IFP_NULL(ifp);
if (channel == NULL)
return WHD_BADARG;
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
CHECK_IOCTL_BUFFER(whd_cdc_get_ioctl_buffer(whd_driver, &buffer, sizeof(channel_info_t) ) );
CHECK_RETURN(whd_cdc_send_ioctl(ifp, CDC_GET, WLC_GET_CHANNEL, buffer, &response) );
channel_info = (channel_info_t *)whd_buffer_get_current_piece_data_pointer(whd_driver, response);
*channel = (uint32_t)channel_info->hw_channel;
CHECK_RETURN(whd_buffer_release(whd_driver, response, WHD_NETWORK_RX) );
return WHD_SUCCESS;
}
uint32_t whd_wifi_enable_supplicant(whd_interface_t ifp, whd_security_t auth_type)
{
whd_buffer_t buffer;
uint32_t *data;
uint32_t bss_index = 0;
whd_driver_t whd_driver;
CHECK_IFP_NULL(ifp);
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
/* Map the interface to a BSS index */
bss_index = ifp->bsscfgidx;
/* Set supplicant variable - mfg app doesn't support these iovars, so don't care if return fails */
data = whd_cdc_get_iovar_buffer(whd_driver, &buffer, (uint16_t)8, "bsscfg:" IOVAR_STR_SUP_WPA);
CHECK_IOCTL_BUFFER(data);
data[0] = bss_index;
data[1] = (uint32_t)( ( ( (auth_type & WPA_SECURITY) != 0 ) ||
( (auth_type & WPA2_SECURITY) != 0 ) ||
(auth_type & WPA3_SECURITY) != 0 ) ? 1 : 0 );
(void)whd_cdc_send_iovar(ifp, CDC_SET, buffer, 0);
return WHD_SUCCESS;
}
uint32_t whd_wifi_set_supplicant_key_timeout(whd_interface_t ifp, int32_t eapol_key_timeout)
{
whd_buffer_t buffer;
int32_t *data;
uint32_t bss_index = 0;
whd_driver_t whd_driver = (whd_driver_t)ifp->whd_driver;
/* Map the interface to a BSS index */
bss_index = ifp->bsscfgidx;
data = whd_cdc_get_iovar_buffer(whd_driver, &buffer, (uint16_t)8, "bsscfg:" IOVAR_STR_SUP_WPA_TMO);
CHECK_IOCTL_BUFFER(data);
data[0] = (int32_t)bss_index;
data[1] = eapol_key_timeout;
CHECK_RETURN(whd_cdc_send_iovar(ifp, CDC_SET, buffer, 0) );
return WHD_SUCCESS;
}
uint32_t whd_wifi_set_passphrase(whd_interface_t ifp, const uint8_t *security_key, uint8_t key_length)
{
whd_buffer_t buffer;
whd_driver_t whd_driver;
wsec_pmk_t *psk;
if (!ifp || !security_key || (key_length < KEY_MIN_LEN) || (key_length > KEY_MAX_LEN) )
{
WPRINT_WHD_ERROR( ("Invalid param in func %s at line %d \n",
__func__, __LINE__) );
return WHD_WLAN_BADARG;
}
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
psk = (wsec_pmk_t *)whd_cdc_get_ioctl_buffer(whd_driver, &buffer, sizeof(wsec_pmk_t) );
CHECK_IOCTL_BUFFER(psk);
memset(psk, 0, sizeof(wsec_pmk_t) );
memcpy(psk->key, security_key, key_length);
psk->key_len = htod16(key_length);
psk->flags = htod16( (uint16_t)WSEC_PASSPHRASE );
/* Delay required to allow radio firmware to be ready to receive PMK and avoid intermittent failure */
CHECK_RETURN(cy_rtos_delay_milliseconds(1) );
CHECK_RETURN(whd_cdc_send_ioctl(ifp, CDC_SET, WLC_SET_WSEC_PMK, buffer, 0) );
return WHD_SUCCESS;
}
uint32_t whd_wifi_sae_password(whd_interface_t ifp, const uint8_t *security_key, uint8_t key_length)
{
whd_buffer_t buffer;
whd_driver_t whd_driver;
wsec_sae_password_t *sae_password;
if (!ifp || !security_key || (key_length < KEY_MIN_LEN) || (key_length > KEY_MAX_LEN) )
{
WPRINT_WHD_ERROR( ("Invalid param in func %s at line %d \n",
__func__, __LINE__) );
return WHD_WLAN_BADARG;
}
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
sae_password = (wsec_sae_password_t *)whd_cdc_get_iovar_buffer(whd_driver, &buffer,
sizeof(wsec_sae_password_t),
IOVAR_STR_SAE_PASSWORD);
CHECK_IOCTL_BUFFER(sae_password);
memset(sae_password, 0, sizeof(wsec_sae_password_t) );
memcpy(sae_password->password, security_key, key_length);
sae_password->password_len = htod16(key_length);
/* Delay required to allow radio firmware to be ready to receive PMK and avoid intermittent failure */
cy_rtos_delay_milliseconds(1);
CHECK_RETURN(whd_cdc_send_iovar(ifp, CDC_SET, buffer, 0) );
return WHD_SUCCESS;
}
uint32_t whd_wifi_enable_sup_set_passphrase(whd_interface_t ifp, const uint8_t *security_key_psk, uint8_t psk_length,
whd_security_t auth_type)
{
whd_buffer_t buffer;
uint32_t *data;
uint32_t bss_index = 0;
whd_driver_t whd_driver;
CHECK_IFP_NULL(ifp);
whd_driver = ifp->whd_driver;
CHECK_DRIVER_NULL(whd_driver);
if ( (psk_length > (uint8_t)WSEC_MAX_PSK_LEN) ||
(psk_length < (uint8_t)WSEC_MIN_PSK_LEN) )
{
return WHD_INVALID_KEY;
}
/* Map the interface to a BSS index */
bss_index = ifp->bsscfgidx;
/* Set supplicant variable - mfg app doesn't support these iovars, so don't care if return fails */
data = whd_cdc_get_iovar_buffer(whd_driver, &buffer, (uint16_t)8, "bsscfg:" IOVAR_STR_SUP_WPA);
CHECK_IOCTL_BUFFER(data);
data[0] = bss_index;
data[1] = (uint32_t)( ( ( (auth_type & WPA_SECURITY) != 0 ) ||
( (auth_type & WPA2_SECURITY) != 0 ) ||
(auth_type & WPA3_SECURITY) != 0 ) ? 1 : 0 );
(void)whd_cdc_send_iovar(ifp, CDC_SET, buffer, 0);
CHECK_RETURN(whd_wifi_set_passphrase(ifp, security_key_psk, psk_length) );
return WHD_SUCCESS;
}
uint32_t whd_wifi_get_rssi(whd_interface_t ifp, int32_t *rssi)
{
CHECK_IFP_NULL(ifp);
if (rssi == NULL)
return WHD_BADARG;
if (ifp->role == WHD_STA_ROLE)
{
return whd_wifi_get_ioctl_buffer(ifp, WLC_GET_RSSI, (uint8_t *)rssi, sizeof(*rssi) );
}
return WHD_BADARG;
}
uint32_t whd_wifi_get_ap_client_rssi(whd_interface_t ifp, int32_t *rssi, const whd_mac_t *client_mac)
{
whd_buffer_t buffer;
whd_buffer_t response;
client_rssi_t *client_rssi;
whd_driver_t whd_driver = ifp->whd_driver;
/* WLAN expects buffer size to be 4-byte aligned */
client_rssi =
(client_rssi_t *)whd_cdc_get_ioctl_buffer(whd_driver, &buffer, ROUND_UP(sizeof(client_rssi_t),
sizeof(uint32_t) ) );
CHECK_IOCTL_BUFFER(client_rssi);
memcpy(&client_rssi->macs, client_mac, sizeof(*client_mac) );
client_rssi->rssi = 0;
CHECK_RETURN_UNSUPPORTED_OK(whd_cdc_send_ioctl(ifp, CDC_GET, WLC_GET_RSSI, buffer, &response) );
memcpy(rssi, whd_buffer_get_current_piece_data_pointer(whd_driver, response), sizeof(int32_t) );
CHECK_RETURN(whd_buffer_release(whd_driver, response, WHD_NETWORK_RX) );
return WHD_SUCCESS;
}
/** Callback for join events
* This is called when the WLC_E_SET_SSID event is received,
* indicating that the system has joined successfully.
* Wakes the thread which was doing the join, allowing it to resume.
*/
static void *whd_wifi_join_events_handler(whd_interface_t ifp, const whd_event_header_t *event_header,
const uint8_t *event_data,
void *handler_user_data)
{
cy_semaphore_t *semaphore = (cy_semaphore_t *)handler_user_data;
whd_bool_t join_attempt_complete = WHD_FALSE;
whd_driver_t whd_driver = ifp->whd_driver;
whd_result_t result;
UNUSED_PARAMETER(event_data);
if (event_header->bsscfgidx >= WHD_INTERFACE_MAX)
{
WPRINT_WHD_DEBUG( ("%s: event_header: Bad interface\n", __FUNCTION__) );
return NULL;
}
switch (event_header->event_type)
{
case WLC_E_PSK_SUP:
/* Ignore WLC_E_PSK_SUP event if link is not up */
if ( (whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] & JOIN_LINK_READY) != 0 )
{
if (event_header->status == WLC_SUP_KEYED)
{
/* Successful WPA key exchange */
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] &= ~JOIN_SECURITY_FLAGS_MASK;
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_SECURITY_COMPLETE;
}
else
{
/* join has completed with an error */
join_attempt_complete = WHD_TRUE;
if ( (event_header->status == WLC_SUP_KEYXCHANGE_WAIT_M1) &&
(event_header->reason == WLC_E_SUP_WPA_PSK_TMO) )
{
/* A timeout waiting for M1 may occur at the edge of the cell or if the AP is particularly slow. */
WPRINT_WHD_DEBUG( ("Supplicant M1 timeout event\n") );
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_EAPOL_KEY_M1_TIMEOUT;
}
else if ( (event_header->status == WLC_SUP_KEYXCHANGE_WAIT_M3) &&
(event_header->reason == WLC_E_SUP_WPA_PSK_TMO) )
{
/* A timeout waiting for M3 is an indicator that the passphrase may be incorrect. */
WPRINT_WHD_DEBUG( ("Supplicant M3 timeout event\n") );
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_EAPOL_KEY_M3_TIMEOUT;
}
else if ( (event_header->status == WLC_SUP_KEYXCHANGE_WAIT_G1) &&
(event_header->reason == WLC_E_SUP_WPA_PSK_TMO) )
{
/* A timeout waiting for G1 (group key) may occur at the edge of the cell. */
WPRINT_WHD_DEBUG( ("Supplicant G1 timeout event\n") );
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_EAPOL_KEY_G1_TIMEOUT;
}
else
{
WPRINT_WHD_DEBUG( ("Unsuccessful supplicant event; status=0x%" PRIu32 "\n",
event_header->status) );
/* Unknown failure during EAPOL key handshake */
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_EAPOL_KEY_FAILURE;
}
}
}
break;
case WLC_E_SET_SSID:
if (event_header->status == WLC_E_STATUS_SUCCESS)
{
/* SSID has been successfully set. */
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_SSID_SET;
}
/* We don't bail out on this event or things like WPS won't work if the AP is rebooting after configuration */
else if (event_header->status == WLC_E_STATUS_NO_NETWORKS)
{
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_NO_NETWORKS;
}
else
{
join_attempt_complete = WHD_TRUE;
}
break;
case WLC_E_LINK:
if ( (event_header->flags & WLC_EVENT_MSG_LINK) != 0 )
{
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_LINK_READY;
}
else
{
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] &= ~JOIN_LINK_READY;
}
break;
case WLC_E_DEAUTH_IND:
case WLC_E_DISASSOC_IND:
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] &=
~(JOIN_AUTHENTICATED | JOIN_LINK_READY);
break;
case WLC_E_AUTH:
if (event_header->status == WLC_E_STATUS_SUCCESS)
{
whd_driver->internal_info.whd_join_status[event_header->bsscfgidx] |= JOIN_AUTHENTICATED;
}
else if (event_header->status == WLC_E_STATUS_UNSOLICITED)
{
WPRINT_WHD_DEBUG( ("Ignore UNSOLICITED pkt event\n") );
}
else
{
/* We cannot authenticate. Perhaps we're blocked or at the edge of a cell. */
join_attempt_complete = WHD_TRUE;
}
break;
case WLC_E_CSA_COMPLETE_IND:
if (event_header->datalen >= sizeof(wl_chan_switch_t) )
{
wl_chan_switch_t *wl_csa = (wl_chan_switch_t *)event_data;
UNUSED_PARAMETER(wl_csa);
WPRINT_WHD_INFO( ("CSA event => chan %d\n", (dtoh16(wl_csa->chspec) & 0xff) ) );
}
break;
/* Note - These are listed to keep gcc pedantic checking happy */
case WLC_E_RRM:
case WLC_E_NONE:
case WLC_E_ROAM:
case WLC_E_JOIN:
case WLC_E_START:
case WLC_E_AUTH_IND:
case WLC_E_DEAUTH:
case WLC_E_ASSOC:
case WLC_E_ASSOC_IND:
case WLC_E_REASSOC:
case WLC_E_REASSOC_IND:
case WLC_E_DISASSOC:
case WLC_E_QUIET_START:
case WLC_E_QUIET_END:
case WLC_E_BEACON_RX:
case WLC_E_MIC_ERROR:
case WLC_E_NDIS_LINK:
case WLC_E_TXFAIL:
case WLC_E_PMKID_CACHE:
case WLC_E_RETROGRADE_TSF:
case WLC_E_PRUNE:
case WLC_E_AUTOAUTH:
case WLC_E_EAPOL_MSG:
case WLC_E_SCAN_COMPLETE:
case WLC_E_ADDTS_IND:
case WLC_E_DELTS_IND:
case WLC_E_BCNSENT_IND:
case WLC_E_BCNRX_MSG:
case WLC_E_BCNLOST_MSG:
case WLC_E_ROAM_PREP:
case WLC_E_PFN_NET_FOUND:
case WLC_E_PFN_NET_LOST:
case WLC_E_RESET_COMPLETE:
case WLC_E_JOIN_START:
case WLC_E_ROAM_START:
case WLC_E_ASSOC_START:
case WLC_E_IBSS_ASSOC:
case WLC_E_RADIO:
case WLC_E_PSM_WATCHDOG:
case WLC_E_CCX_ASSOC_START:
case WLC_E_CCX_ASSOC_ABORT:
case WLC_E_PROBREQ_MSG:
case WLC_E_SCAN_CONFIRM_IND:
case WLC_E_COUNTRY_CODE_CHANGED:
case WLC_E_EXCEEDED_MEDIUM_TIME:
case WLC_E_ICV_ERROR:
case WLC_E_UNICAST_DECODE_ERROR:
case WLC_E_MULTICAST_DECODE_ERROR:
case WLC_E_TRACE:
case WLC_E_BTA_HCI_EVENT:
case WLC_E_IF:
case WLC_E_PFN_BEST_BATCHING:
case WLC_E_RSSI:
case WLC_E_EXTLOG_MSG:
case WLC_E_ACTION_FRAME:
case WLC_E_ACTION_FRAME_COMPLETE:
case WLC_E_PRE_ASSOC_IND:
case WLC_E_PRE_REASSOC_IND:
case WLC_E_CHANNEL_ADOPTED:
case WLC_E_AP_STARTED:
case WLC_E_DFS_AP_STOP:
case WLC_E_DFS_AP_RESUME:
case WLC_E_WAI_STA_EVENT:
case WLC_E_WAI_MSG:
case WLC_E_ESCAN_RESULT:
case WLC_E_ACTION_FRAME_OFF_CHAN_COMPLETE:
case WLC_E_PROBRESP_MSG:
case WLC_E_P2P_PROBREQ_MSG:
case WLC_E_DCS_REQUEST:
case WLC_E_FIFO_CREDIT_MAP:
case WLC_E_ACTION_FRAME_RX:
case WLC_E_WAKE_EVENT:
case WLC_E_RM_COMPLETE:
case WLC_E_HTSFSYNC:
case WLC_E_OVERLAY_REQ:
case WLC_E_EXCESS_PM_WAKE_EVENT:
case WLC_E_PFN_SCAN_NONE:
case WLC_E_PFN_SCAN_ALLGONE:
case WLC_E_GTK_PLUMBED:
case WLC_E_ASSOC_IND_NDIS:
case WLC_E_REASSOC_IND_NDIS:
case WLC_E_ASSOC_REQ_IE:
case WLC_E_ASSOC_RESP_IE:
case WLC_E_ASSOC_RECREATED:
case WLC_E_ACTION_FRAME_RX_NDIS:
case WLC_E_AUTH_REQ:
case WLC_E_TDLS_PEER_EVENT:
case WLC_E_SPEEDY_RECREATE_FAIL:
case WLC_E_NATIVE:
case WLC_E_PKTDELAY_IND:
case WLC_E_AWDL_AW:
case WLC_E_AWDL_ROLE:
case WLC_E_AWDL_EVENT:
case WLC_E_NIC_AF_TXS:
case WLC_E_NAN:
case WLC_E_BEACON_FRAME_RX:
case WLC_E_SERVICE_FOUND:
case WLC_E_GAS_FRAGMENT_RX:
case WLC_E_GAS_COMPLETE:
case WLC_E_P2PO_ADD_DEVICE:
case WLC_E_P2PO_DEL_DEVICE:
case WLC_E_WNM_STA_SLEEP:
case WLC_E_TXFAIL_THRESH:
case WLC_E_PROXD:
case WLC_E_IBSS_COALESCE:
case WLC_E_AWDL_RX_PRB_RESP:
case WLC_E_AWDL_RX_ACT_FRAME:
case WLC_E_AWDL_WOWL_NULLPKT:
case WLC_E_AWDL_PHYCAL_STATUS:
case WLC_E_AWDL_OOB_AF_STATUS:
case WLC_E_AWDL_SCAN_STATUS:
case WLC_E_AWDL_AW_START:
case WLC_E_AWDL_AW_END:
case WLC_E_AWDL_AW_EXT:
case WLC_E_AWDL_PEER_CACHE_CONTROL:
case WLC_E_CSA_START_IND:
case WLC_E_CSA_DONE_IND:
case WLC_E_CSA_FAILURE_IND:
case WLC_E_CCA_CHAN_QUAL:
case WLC_E_BSSID:
case WLC_E_TX_STAT_ERROR:
case WLC_E_BCMC_CREDIT_SUPPORT:
case WLC_E_PSTA_PRIMARY_INTF_IND:
case WLC_E_P2P_DISC_LISTEN_COMPLETE:
case WLC_E_BT_WIFI_HANDOVER_REQ:
case WLC_E_SPW_TXINHIBIT:
case WLC_E_FBT_AUTH_REQ_IND:
case WLC_E_RSSI_LQM:
case WLC_E_PFN_GSCAN_FULL_RESULT:
case WLC_E_PFN_SWC:
case WLC_E_AUTHORIZED:
case WLC_E_PROBREQ_MSG_RX:
case WLC_E_PFN_SCAN_COMPLETE:
case WLC_E_RMC_EVENT:
case WLC_E_DPSTA_INTF_IND:
case WLC_E_ULP:
case WLC_E_LAST:
default:
whd_assert("Received event which was not registered\n", 0 != 0);
break;
}
if (whd_wifi_is_ready_to_transceive(ifp) == WHD_SUCCESS)
{
join_attempt_complete = WHD_TRUE;
}
if (join_attempt_complete == WHD_TRUE)
{
if (semaphore != NULL)
{
result = cy_rtos_get_semaphore(&whd_driver->internal_info.active_join_mutex, CY_RTOS_NEVER_TIMEOUT,
WHD_FALSE);
if (result != WHD_SUCCESS)
{
WPRINT_WHD_ERROR( ("Get semaphore failed in %s at %d \n", __func__, __LINE__) );
}
if (whd_driver->internal_info.active_join_semaphore != NULL)
{
whd_assert("Unexpected semaphore\n", whd_driver->internal_info.active_join_semaphore == semaphore);
result = cy_rtos_set_semaphore(whd_driver->internal_info.active_join_semaphore, WHD_FALSE);
if (result != WHD_SUCCESS)
{
WPRINT_WHD_ERROR( ("Set semaphore failed in %s at %d \n", __func__, __LINE__) );
}
}
result = cy_rtos_set_semaphore(&whd_driver->internal_info.active_join_mutex, WHD_FALSE);
if (result != WHD_SUCCESS)
{
WPRINT_WHD_ERROR( ("Set semaphore failed in %s at %d \n", __func__, __LINE__) );
}
}
return NULL;
}
else
{
return handler_user_data;
}
}
/* Do any needed preparation prior to launching a join */
static uint32_t whd_wifi_active_join_init(whd_interface_t ifp, whd_security_t auth_type, const uint8_t *security_key,
uint8_t key_length, cy_semaphore_t *semaphore)
{
whd_driver_t whd_driver = ifp->whd_driver;
if (whd_driver->internal_info.active_join_mutex_initted == WHD_FALSE)
{
CHECK_RETURN(cy_rtos_init_semaphore(&whd_driver->internal_info.active_join_mutex, 1, 0) );
whd_driver->internal_info.active_join_mutex_initted = WHD_TRUE;
CHECK_RETURN(cy_rtos_set_semaphore(&whd_driver->internal_info.active_join_mutex, WHD_FALSE) );