1
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
86
87
93
94
105
106
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
141
142
143
144
145
146
147
148
149
154
155
166
167
168
169
170
180
184
188
192
196
200
203
204
205
209
210
211
212
213
214
217
218
219
220
221
222
226
227
232
233
234
235
236
237
238
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
267
268
269
270
271
272
273
277
278
282
283
284
285
286
287
288
289
290
291
292
293
298
299
300
301
304
305
311
312
313
314
315
316
317
318
319
320
321
322
323
324
328
329
330
331
332
333
334
337
338
339
340
343
344
345
346
347
348
349
350
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
377
378
379
380
381
384
385
386
387
388
389
390
393
394
398
399
403
404
405
406
407
408
409
410
411
417
427
428
429
433
434
435
436
437
438
439
440
441
442
443
444
445
452
453
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
489
490
491
492
493
498
499
500
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
525
526
527
531
532
533
537
538
539
540
544
545
546
547
548
549
550
551
552
553
554
555
558
559
560
561
565
566
567
571
572
573
576
577
578
579
580
581
585
586
587
588
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
609
610
611
612
613
626
627
628
638
639
664
665
668
669
670
671
672
673
676
677
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
699
700
701
705
706
707
711
712
713
714
715
716
717
718
719
723
724
725
726
730
731
732
733
734
735
736
737
738
739
740
741
742
745
746
747
748
751
752
753
754
755
756
757
758
759
760
769
770
776
777
778
779
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
841
842
843
844
845
846
847
848
854
855
856
857
858
859
860
861
862
863
864
865
876
877
878
879
880
886
887
888
889
890
891
892
893
896
897
905
906
907
908
909
910
911
912
913
914
915
918
919
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
957
958
959
960
961
962
965
966
967
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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1021
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1056
1060
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1086
1098
1099
1101
1102
1103
1105
1106
1107
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1150
1151
1154
1155
1158
1159
1162
1163
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1184
1185
1186
1187
1188
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1208
1209
1210
1214
1215
1216
1217
1221
1222
1223
1224
1228
1229
1230
1231
1235
1236
1237
1238
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1264
1265
1266
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1307
1308
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1348
1360
1361
1362
1363
1364
1365
1367
1368
1369
1370
1371
1372
1377
1378
1379
1380
1381
1383
1384
1385
1386
1387
1388
1389
1390
1391
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1412
1413
1414
1415
1416
1417
1418
1419
1420
1423
1424
1425
1426
1427
1428
1429
1430
1434
1435
1436
1437
1438
1439
1440
1441
1444
1445
1446
1447
1448
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1464
1465
1468
1469
1470
1471
1472
1475
1476
1477
1478
1481
1482
1483
1488
1489
1490
1491
1492
1495
1496
1499
1500
1501
1502
1503
1504
1505
1506
1507
1510
1511
1512
1513
1514
1515
1516
1517
1520
1521
1522
1523
1524
1525
1526
1527
1528
1532
1539
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1575
1576
1577
1578
1579
1580
1584
1591
1595
1599
1600
1601
1602
1603
1604
1605
1606
1610
1611
1612
1613
1614
1615
1616
1617
1618
1621
1622
1625
1626
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1692
1693
1694
1695
1696
1697
1701
1702
1703
1704
1705
1706
1710
1711
1712
1713
1714
1715
1716
1717
1718
1721
1722
1723
1724
1725
1726
1727
1728
1733
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1751
1752
1757
1758
1763
1764
1767
1768
1772
1773
1774
1775
1776
1777
1778
1779
1780
1782
1783
1784
1791
1792
1793
1797
1798
1799
1803
1804
1805
1806
1810
1811
1812
1813
1814
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1841
1842
1847
1848
1849
1850
1851
1852
1853
1854
1855
1859
1860
1861
1862
1863
1864
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1881
1882
1885
1886
1890
1891
1892
1902
1903
1904
1905
1909
1910
1911
1916
1917
1918
1919
1920
1921
1922
1925
1926
1927
1928
1932
1933
1934
1935
1936
1937
1938
1939
1943
1944
1945
1948
1949
1950
1951
1952
1953
1954
1955
1956
1961
1962
1963
1964
1965
1966
1967
1968
1969
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1987
1988
1989
1990
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2009
2010
2011
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2027
2028
2029
2030
2035
2036
2040
2041
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
/* ... */
#include <string.h>
#include <inttypes.h>
#include "utils/includes.h"
#include "common.h"
#include "eloop.h"
#include "rsn_supp/wpa.h"
#include "utils/common.h"
#include "common/eapol_common.h"
#include "utils/wpa_debug.h"
#include "common/ieee802_11_defs.h"
#include "crypto/dh_group5.h"
#include "wps/wps_i.h"
#include "wps/wps_dev_attr.h"
#include "eap_peer/eap_defs.h"
#include "eap_peer/eap_common.h"
#include "esp_wifi_driver.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_err.h"
#include "esp_private/wifi.h"
#include "esp_wps_i.h"
#include "esp_dpp_i.h"
#include "esp_wps.h"
#include "eap_common/eap_wsc_common.h"
#include "esp_wpas_glue.h"25 includes
const char *wps_model_number = CONFIG_IDF_TARGET;
void *s_wps_api_lock = NULL;
void *s_wps_api_sem = NULL;
bool s_wps_enabled = false;
#ifdef USE_WPS_TASK
struct wps_rx_param {
u8 sa[ETH_ALEN];
u8 *buf;
int len;
STAILQ_ENTRY(wps_rx_param) bqentry;
}{ ... };
static STAILQ_HEAD(, wps_rx_param) s_wps_rxq;
static struct wps_sm_funcs *s_wps_sm_cb = NULL;
static void *s_wps_task_hdl = NULL;
static void *s_wps_queue = NULL;
static void *s_wps_data_lock = NULL;
static void *s_wps_task_create_sem = NULL;
static uint8_t s_wps_sig_cnt[SIG_WPS_NUM] = {0};
/* ... */
#endif
void wifi_wps_scan_done(void *arg, ETS_STATUS status);
void wifi_wps_scan(void *data, void *user_ctx);
int wifi_station_wps_start(void);
int wps_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len);
void wifi_wps_start_internal(void);
int wifi_wps_enable_internal(const esp_wps_config_t *config);
int wifi_wps_disable_internal(void);
void wifi_station_wps_timeout_internal(void);
void wifi_station_wps_msg_timeout_internal(void);
void wifi_station_wps_success_internal(void);
void wifi_wps_scan_internal(void);
void wifi_station_wps_eapol_start_handle_internal(void);
void wps_add_discard_ap(u8 *bssid);
void wifi_station_wps_msg_timeout(void *data, void *user_ctx);
void wifi_station_wps_eapol_start_handle(void *data, void *user_ctx);
void wifi_station_wps_success(void *data, void *user_ctx);
void wifi_station_wps_timeout(void *data, void *user_ctx);
int wps_delete_timer(void);
struct wps_sm *gWpsSm = NULL;
static wps_factory_information_t *s_factory_info = NULL;
static void wps_rxq_init(void)
{
DATA_MUTEX_TAKE();
STAILQ_INIT(&s_wps_rxq);
DATA_MUTEX_GIVE();
}{ ... }
static void wps_rxq_enqueue(struct wps_rx_param *param)
{
DATA_MUTEX_TAKE();
STAILQ_INSERT_TAIL(&s_wps_rxq, param, bqentry);
DATA_MUTEX_GIVE();
}{ ... }
static struct wps_rx_param * wps_rxq_dequeue(void)
{
struct wps_rx_param *param = NULL;
DATA_MUTEX_TAKE();
if ((param = STAILQ_FIRST(&s_wps_rxq)) != NULL) {
STAILQ_REMOVE_HEAD(&s_wps_rxq, bqentry);
STAILQ_NEXT(param, bqentry) = NULL;
}{...}
DATA_MUTEX_GIVE();
return param;
}{ ... }
static void wps_rxq_deinit(void)
{
struct wps_rx_param *param = NULL;
DATA_MUTEX_TAKE();
while ((param = STAILQ_FIRST(&s_wps_rxq)) != NULL) {
STAILQ_REMOVE_HEAD(&s_wps_rxq, bqentry);
STAILQ_NEXT(param, bqentry) = NULL;
os_free(param->buf);
os_free(param);
}{...}
DATA_MUTEX_GIVE();
}{ ... }
#ifdef USE_WPS_TASK
void wps_task(void *pvParameters)
{
ETSEvent e;
wps_ioctl_param_t *param;
bool del_task = false;
os_semphr_give(s_wps_task_create_sem);
wpa_printf(MSG_DEBUG, "wps_Task enter");
for (;;) {
if (TRUE == os_queue_recv(s_wps_queue, &e, OS_BLOCK)) {
if ((e.sig >= SIG_WPS_ENABLE) && (e.sig < SIG_WPS_NUM)) {
DATA_MUTEX_TAKE();
if (s_wps_sig_cnt[e.sig]) {
s_wps_sig_cnt[e.sig]--;
}{...} else {
wpa_printf(MSG_ERROR, "wpsT: invalid sig cnt, sig=%" PRId32 " cnt=%d", e.sig, s_wps_sig_cnt[e.sig]);
}{...}
DATA_MUTEX_GIVE();
}{...}
wpa_printf(MSG_DEBUG, "wpsT: rx sig=%" PRId32 "", e.sig);
switch (e.sig) {
case SIG_WPS_ENABLE:
case SIG_WPS_DISABLE:
case SIG_WPS_START:
param = (wps_ioctl_param_t *)e.par;
if (!param) {
wpa_printf(MSG_ERROR, "wpsT: invalid param sig=%" PRId32 "", e.sig);
os_semphr_give(s_wps_api_sem);
break;
}{...}
if (e.sig == SIG_WPS_ENABLE) {
param->ret = wifi_wps_enable_internal((esp_wps_config_t *)(param->arg));
}{...} else if (e.sig == SIG_WPS_DISABLE) {
DATA_MUTEX_TAKE();
param->ret = wifi_wps_disable_internal();
del_task = true;
s_wps_task_hdl = NULL;
DATA_MUTEX_GIVE();
}{...} else {
param->ret = wifi_station_wps_start();
}{...}
os_semphr_give(s_wps_api_sem);
break;
...
case SIG_WPS_RX: {
struct wps_rx_param *param = NULL;
while ((param = wps_rxq_dequeue()) != NULL) {
wps_sm_rx_eapol_internal(param->sa, param->buf, param->len);
os_free(param->buf);
os_free(param);
}{...}
break;
}{...}
...
case SIG_WPS_TIMER_TIMEOUT:
wifi_station_wps_timeout_internal();
break;
...
case SIG_WPS_TIMER_MSG_TIMEOUT:
wifi_station_wps_msg_timeout_internal();
break;
...
case SIG_WPS_TIMER_SUCCESS_CB:
wifi_station_wps_success_internal();
break;
...
case SIG_WPS_TIMER_SCAN:
wifi_wps_scan_internal();
break;
...
case SIG_WPS_TIMER_EAPOL_START:
wifi_station_wps_eapol_start_handle_internal();
break;
...
default:
wpa_printf(MSG_ERROR, "wpsT: invalid sig=%" PRId32 "", e.sig);
break;...
}{...}
if (del_task) {
wpa_printf(MSG_DEBUG, "wpsT: delete task");
break;
}{...}
}{...}
}{...}
os_task_delete(NULL);
}{ ... }
/* ... */
int wps_post(uint32_t sig, uint32_t par)
{
wpa_printf(MSG_DEBUG, "wps post: sig=%" PRId32 " cnt=%d", sig, s_wps_sig_cnt[sig]);
ETSEvent evt;
if (!s_wps_task_hdl) {
wpa_printf(MSG_DEBUG, "wps post: sig=%" PRId32 " failed as wps task has been deinited", sig);
return ESP_FAIL;
}{...}
DATA_MUTEX_TAKE();
if (s_wps_sig_cnt[sig]) {
wpa_printf(MSG_DEBUG, "wps post: sig=%" PRId32 " processing", sig);
DATA_MUTEX_GIVE();
return ESP_OK;
}{...}
s_wps_sig_cnt[sig]++;
evt.sig = sig;
evt.par = par;
DATA_MUTEX_GIVE();
if (os_queue_send(s_wps_queue, &evt, os_task_ms_to_tick(10)) != TRUE) {
wpa_printf(MSG_ERROR, "WPS: Q S E");
DATA_MUTEX_TAKE();
s_wps_sig_cnt[sig]--;
DATA_MUTEX_GIVE();
return ESP_FAIL;
}{...}
return ESP_OK;
}{ ... }
#endif/* ... */
/* ... */
static inline int wps_sm_ether_send(struct wps_sm *sm, u16 proto,
const u8 *data, size_t data_len)
{
u8 bssid[ETH_ALEN];
int ret = esp_wifi_get_assoc_bssid_internal(bssid);
if (ret != 0) {
wpa_printf(MSG_ERROR, "bssid is empty!");
return -1;
}{...}
return wpa_ether_send(sm, bssid, proto, data, data_len);
}{ ... }
u8 *wps_sm_alloc_eapol(struct wps_sm *sm, u8 type,
const void *data, u16 data_len,
size_t *msg_len, void **data_pos)
{
return wpa_alloc_eapol(sm, type, data, data_len, msg_len, data_pos);
}{ ... }
void wps_sm_free_eapol(u8 *buffer)
{
return wpa_free_eapol(buffer);
}{ ... }
static void
wps_build_ic_appie_wps_pr(void)
{
struct wpabuf *extra_ie = NULL;
struct wpabuf *wps_ie;
struct wps_sm *sm = gWpsSm;
u16 pw_id;
wpa_printf(MSG_DEBUG, "wps build: wps pr");
if (wps_get_type() == WPS_TYPE_PBC) {
pw_id = DEV_PW_PUSHBUTTON;
}{...} else {
pw_id = DEV_PW_DEFAULT;
}{...}
wps_ie = wps_build_probe_req_ie(pw_id, sm->dev, sm->uuid,
WPS_REQ_ENROLLEE, 0, NULL);
if (!wps_ie) {
return;
}{...}
if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0) {
wpabuf_put_buf(extra_ie, wps_ie);
}{...} else {
wpabuf_free(wps_ie);
return;
}{...}
wpabuf_free(wps_ie);
esp_wifi_set_appie_internal(WIFI_APPIE_WPS_PR, (uint8_t *)wpabuf_head(extra_ie), extra_ie->used, 0);
wpabuf_free(extra_ie);
}{ ... }
static void
wps_build_ic_appie_wps_ar(void)
{
struct wpabuf *buf = wps_build_assoc_req_ie(WPS_REQ_ENROLLEE);
wpa_printf(MSG_DEBUG, "wps build: wps ar");
if (buf) {
esp_wifi_set_appie_internal(WIFI_APPIE_WPS_AR, (uint8_t *)wpabuf_head(buf), buf->used, 0);
wpabuf_free(buf);
}{...}
}{ ... }
static bool ap_supports_sae(struct wps_scan_ie *scan)
{
struct wpa_ie_data rsn_info;
if (!scan->rsn) {
return false;
}{...}
wpa_parse_wpa_ie_rsn(scan->rsn, scan->rsn[1] + 2, &rsn_info);
if (rsn_info.key_mgmt & WPA_KEY_MGMT_SAE) {
return true;
}{...}
return false;
}{ ... }
static bool
is_wps_pbc_overlap(struct wps_sm *sm, const u8 *sel_uuid)
{
if (!sel_uuid) {
wpa_printf(MSG_DEBUG, "WPS: null uuid field");
return false;
}{...}
if (os_memcmp(sel_uuid, sm->uuid, WPS_UUID_LEN) != 0) {
wpa_printf(MSG_DEBUG, "uuid is not same");
wpa_hexdump(MSG_DEBUG, "WPS: UUID of scanned BSS is",
sel_uuid, WPS_UUID_LEN);
wpa_hexdump(MSG_DEBUG, "WPS: UUID of sm BSS is",
sm->uuid, WPS_UUID_LEN);
return true;
}{...}
return false;
}{ ... }
static bool
wps_parse_scan_result(struct wps_scan_ie *scan)
{
struct wps_sm *sm = gWpsSm;
wifi_mode_t op_mode = 0;
sm->wps_pbc_overlap = false;
if (!sm->is_wps_scan || !scan->bssid) {
return false;
}{...}
if (wps_get_type() == WPS_TYPE_DISABLE
|| (wps_get_status() != WPS_STATUS_DISABLE
&& wps_get_status() != WPS_STATUS_SCANNING)
) {
return false;
}{...}
esp_wifi_get_mode(&op_mode);
if ((op_mode != WIFI_MODE_STA)
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
&& (op_mode != WIFI_MODE_APSTA)
#endif
) {
return false;
}{...}
if (!scan->rsn && !scan->wpa && (scan->capinfo & WLAN_CAPABILITY_PRIVACY)) {
wpa_printf(MSG_DEBUG, "WEP not suppported in WPS");
return false;
}{...}
if (sm->ignore_sel_reg && !is_zero_ether_addr(sm->bssid)) {
return false;
}{...}
if (scan->wps) {
bool ap_found = false;
struct wpabuf *buf = wpabuf_alloc_copy(scan->wps + 6, scan->wps[1] - 4);
int count;
const u8 *scan_uuid;
if ((wps_get_type() == WPS_TYPE_PBC && wps_is_selected_pbc_registrar(buf)) ||
(wps_get_type() == WPS_TYPE_PIN && wps_is_addr_authorized(buf, sm->ownaddr, 1))) {
sm->ignore_sel_reg = false;
sm->discard_ap_cnt = 0;
ap_found = true;
}{...}
if (wps_get_type() == WPS_TYPE_PIN && sm->ignore_sel_reg) {
for (count = 0; count < WPS_MAX_DIS_AP_NUM; count++) {
if (os_memcmp(sm->dis_ap_list[count].bssid, scan->bssid, ETH_ALEN) == 0) {
wpa_printf(MSG_INFO, "discard ap bssid "MACSTR, MAC2STR(scan->bssid));
wpabuf_free(buf);
return false;
}{...}
}{...}
}{...}
if (ap_found || sm->ignore_sel_reg) {
if (scan->ssid[1] > SSID_MAX_LEN) {
wpabuf_free(buf);
return false;
}{...}
esp_wifi_enable_sta_privacy_internal();
os_memset(sm->creds[0].ssid, 0, SSID_MAX_LEN);
os_memcpy(sm->creds[0].ssid, (char *)&scan->ssid[2], (int)scan->ssid[1]);
sm->creds[0].ssid_len = scan->ssid[1];
if (scan->bssid && memcmp(sm->bssid, scan->bssid, ETH_ALEN) != 0) {
wpa_printf(MSG_INFO, "sm BSSid: "MACSTR " scan BSSID " MACSTR,
MAC2STR(sm->bssid), MAC2STR(scan->bssid));
sm->discover_ssid_cnt++;
wpa_printf(MSG_INFO, "discoverd cnt is %d and chan is %d ", sm->discover_ssid_cnt, scan->chan);
os_memcpy(sm->bssid, scan->bssid, ETH_ALEN);
scan_uuid = wps_get_uuid_e(buf);
if (scan_uuid) {
if (wps_get_type() == WPS_TYPE_PBC && is_wps_pbc_overlap(sm, scan_uuid) == true) {
wpa_printf(MSG_INFO, "pbc_overlap flag is true");
sm->wps_pbc_overlap = true;
}{...}
os_memcpy(sm->uuid, scan_uuid, WPS_UUID_LEN);
}{...}
if (ap_supports_sae(scan)) {
wpa_printf(MSG_INFO, "AP supports SAE, get password in passphrase");
sm->dev->config_methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY;
sm->wps->wps->config_methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY;
wps_build_ic_appie_wps_ar();
}{...}
}{...}
wpabuf_free(buf);
wpa_printf(MSG_DEBUG, "wps discover [%s] ", (char *)sm->creds[0].ssid);
sm->channel = scan->chan;
return true;
}{...}
wpabuf_free(buf);
}{...}
return false;
}{ ... }
int wps_send_eap_identity_rsp(u8 id)
{
struct wps_sm *sm = gWpsSm;
struct wpabuf *eap_buf = NULL;
u8 *buf = NULL;
int len;
int ret = ESP_OK;
wpa_printf(MSG_DEBUG, "wps send eapol id rsp");
eap_buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, sm->identity_len,
EAP_CODE_RESPONSE, id);
if (!eap_buf) {
wpa_printf(MSG_ERROR, "eap buf allocation failed");
ret = ESP_FAIL;
goto _err;
}{...}
wpabuf_put_data(eap_buf, sm->identity, sm->identity_len);
buf = wps_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAP_PACKET, wpabuf_head_u8(eap_buf), wpabuf_len(eap_buf), (size_t *)&len, NULL);
if (!buf) {
wpa_printf(MSG_ERROR, "buf allocation failed");
ret = ESP_ERR_NO_MEM;
goto _err;
}{...}
ret = wps_sm_ether_send(sm, ETH_P_EAPOL, buf, len);
if (ret) {
wpa_printf(MSG_ERROR, "wps sm ether send failed ret=%d", ret);
ret = ESP_FAIL;
goto _err;
}{...}
_err:
wps_sm_free_eapol(buf);
wpabuf_free(eap_buf);
return ret;
}{ ... }
int wps_send_frag_ack(u8 id)
{
struct wps_sm *sm = gWpsSm;
struct wpabuf *eap_buf = NULL;
u8 *buf;
int len;
int ret = 0;
wpa_printf(MSG_DEBUG, "send frag ack id:%d", id);
if (!sm) {
return ESP_FAIL;
}{...}
eap_buf = eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
if (!eap_buf) {
ret = ESP_ERR_NO_MEM;
goto _err;
}{...}
buf = wps_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAP_PACKET, wpabuf_head_u8(eap_buf), wpabuf_len(eap_buf), (size_t *)&len, NULL);
if (!buf) {
ret = ESP_ERR_NO_MEM;
goto _err;
}{...}
ret = wps_sm_ether_send(sm, ETH_P_EAPOL, buf, len);
wps_sm_free_eapol(buf);
if (ret) {
ret = ESP_ERR_NO_MEM;
goto _err;
}{...}
_err:
wpabuf_free(eap_buf);
return ret;
}{ ... }
int wps_enrollee_process_msg_frag(struct wpabuf **buf, int tot_len, u8 *frag_data, int frag_len, u8 flag)
{
struct wps_sm *sm = gWpsSm;
u8 identifier;
if (!sm) {
return ESP_FAIL;
}{...}
identifier = sm->current_identifier;
if (buf == NULL || frag_data == NULL) {
wpa_printf(MSG_ERROR, "fun:%s. line:%d, frag buf or frag data is null", __FUNCTION__, __LINE__);
return ESP_FAIL;
}{...}
if (*buf == NULL) {
if (0 == (flag & WPS_MSG_FLAG_LEN) || tot_len < frag_len) {
wpa_printf(MSG_ERROR, "fun:%s. line:%d, flag error:%02x", __FUNCTION__, __LINE__, flag);
return ESP_FAIL;
}{...}
*buf = wpabuf_alloc(tot_len);
if (*buf == NULL) {
return ESP_ERR_NO_MEM;
}{...}
wpabuf_put_data(*buf, frag_data, frag_len);
return wps_send_frag_ack(identifier);
}{...}
if (flag & WPS_MSG_FLAG_LEN) {
wpa_printf(MSG_ERROR, "fun:%s. line:%d, flag error:%02x", __FUNCTION__, __LINE__, flag);
return ESP_FAIL;
}{...}
wpabuf_put_data(*buf, frag_data, frag_len);
if (flag & WPS_MSG_FLAG_MORE) {
return wps_send_frag_ack(identifier);
}{...}
return ESP_OK;
}{ ... }
int wps_process_wps_mX_req(u8 *ubuf, int len, enum wps_process_res *res)
{
struct wps_sm *sm = gWpsSm;
static struct wpabuf *wps_buf = NULL;
struct eap_expand *expd;
int tlen = 0;
u8 *tbuf;
u8 flag;
int frag_len;
u16 be_tot_len = 0;
if (!sm || !res) {
return ESP_FAIL;
}{...}
expd = (struct eap_expand *) ubuf;
wpa_printf(MSG_DEBUG, "wps process mX req: len %d, tlen %d", len, tlen);
if (sm->state == WAIT_START) {
if (expd->opcode != WSC_Start) {
wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
"in WAIT_START state", expd->opcode);
return ESP_ERR_INVALID_STATE;
}{...}
wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
sm->state = WPA_MESG;
}{...} else if (expd->opcode == WSC_Start) {
wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
expd->opcode);
return ESP_ERR_INVALID_STATE;
}{...}
flag = *(u8 *)(ubuf + sizeof(struct eap_expand));
if (flag & WPS_MSG_FLAG_LEN) {
tbuf = ubuf + sizeof(struct eap_expand) + 1 + 2;
frag_len = len - (sizeof(struct eap_expand) + 1 + 2);
be_tot_len = *(u16 *)(ubuf + sizeof(struct eap_expand) + 1);
tlen = ((be_tot_len & 0xff) << 8) | ((be_tot_len >> 8) & 0xff);
}{...} else {
tbuf = ubuf + sizeof(struct eap_expand) + 1;
frag_len = len - (sizeof(struct eap_expand) + 1);
tlen = frag_len;
}{...}
if ((flag & WPS_MSG_FLAG_MORE) || wps_buf != NULL) {
if (tlen > 50000) {
wpa_printf(MSG_ERROR, "EAP-WSC: Invalid Message Length");
return ESP_FAIL;
}{...}
wpa_printf(MSG_DEBUG, "rx frag msg id:%d, flag:%d, frag_len: %d, tot_len: %d, be_tot_len:%d", sm->current_identifier, flag, frag_len, tlen, be_tot_len);
if (ESP_OK != wps_enrollee_process_msg_frag(&wps_buf, tlen, tbuf, frag_len, flag)) {
if (wps_buf) {
wpabuf_free(wps_buf);
wps_buf = NULL;
}{...}
return ESP_FAIL;
}{...}
if (flag & WPS_MSG_FLAG_MORE) {
*res = WPS_FRAGMENT;
return ESP_OK;
}{...}
}{...} else {
if (wps_buf) {
wpa_printf(MSG_ERROR, "something is wrong, frag buf is not freed");
wpabuf_free(wps_buf);
wps_buf = NULL;
}{...}
wps_buf = wpabuf_alloc_copy(tbuf, tlen);
}{...}
if (!wps_buf) {
return ESP_FAIL;
}{...}
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
*res = wps_enrollee_process_msg(sm->wps, expd->opcode, wps_buf);
if (*res == WPS_FAILURE) {
sm->state = WPA_FAIL;
}{...}
if (wps_buf) {
wpabuf_free(wps_buf);
wps_buf = NULL;
}{...}
return ESP_OK;
}{ ... }
int wps_send_wps_mX_rsp(u8 id)
{
struct wps_sm *sm = gWpsSm;
struct wpabuf *eap_buf = NULL;
struct wpabuf *wps_buf = NULL;
u8 *buf;
int len;
int ret = 0;
enum wsc_op_code opcode;
wpa_printf(MSG_DEBUG, "wps send wps mX rsp");
if (!sm) {
return ESP_FAIL;
}{...}
wps_buf = wps_enrollee_get_msg(sm->wps, &opcode);
if (!wps_buf) {
ret = ESP_FAIL;
goto _err;
}{...}
eap_buf = eap_msg_alloc(EAP_VENDOR_WFA, 0x00000001, wpabuf_len(wps_buf) + 2, EAP_CODE_RESPONSE, id);
if (!eap_buf) {
ret = ESP_FAIL;
goto _err;
}{...}
wpabuf_put_u8(eap_buf, opcode);
wpabuf_put_u8(eap_buf, 0x00);
wpabuf_put_data(eap_buf, wpabuf_head_u8(wps_buf), wpabuf_len(wps_buf));
wpabuf_free(wps_buf);
buf = wps_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAP_PACKET, wpabuf_head_u8(eap_buf), wpabuf_len(eap_buf), (size_t *)&len, NULL);
if (!buf) {
ret = ESP_FAIL;
goto _err;
}{...}
ret = wps_sm_ether_send(sm, ETH_P_EAPOL, buf, len);
wps_sm_free_eapol(buf);
if (ret) {
ret = ESP_FAIL;
goto _err;
}{...}
_err:
wpabuf_free(eap_buf);
return ret;
}{ ... }
int wps_tx_start(void)
{
struct wps_sm *sm = gWpsSm;
u8 *buf;
int len;
if (!sm) {
return ESP_FAIL;
}{...}
wpa_printf(MSG_DEBUG, "WPS: Send EAPOL START.");
buf = wps_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START, (u8 *)"", 0, (size_t *)&len, NULL);
if (!buf) {
return ESP_ERR_NO_MEM;
}{...}
wps_sm_ether_send(sm, ETH_P_EAPOL, buf, len);
wps_sm_free_eapol(buf);
eloop_register_timeout(3, 0, wifi_station_wps_eapol_start_handle, NULL, NULL);
return ESP_OK;
}{ ... }
int wps_start_pending(void)
{
if (!gWpsSm) {
return ESP_FAIL;
}{...}
wpa_printf(MSG_DEBUG, "wps start pending");
return wps_tx_start();
}{ ... }
static void wps_stop_connection_timers(struct wps_sm *sm)
{
esp_wifi_disarm_sta_connection_timer_internal();
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_success, NULL, NULL);
}{ ... }
static int wps_sm_init(struct wps_sm *sm)
{
if (!sm) {
return -1;
}{...}
sm->ignore_sel_reg = false;
sm->discard_ap_cnt = 0;
sm->scan_cnt = 0;
sm->discover_ssid_cnt = 0;
os_bzero(sm->bssid, ETH_ALEN);
os_bzero(sm->creds, sizeof(sm->creds));
sm->ap_cred_cnt = 0;
return 0;
}{ ... }
int wps_stop_process(wifi_event_sta_wps_fail_reason_t reason_code)
{
struct wps_sm *sm = gWpsSm;
if (!gWpsSm) {
return ESP_FAIL;
}{...}
wps_set_status(WPS_STATUS_DISABLE);
wps_sm_init(sm);
sm->wps->state = SEND_M1;
wps_stop_connection_timers(sm);
esp_wifi_disconnect();
sm->state = WPA_FAIL;
wpa_printf(MSG_DEBUG, "Write wps_fail_information");
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &reason_code, sizeof(reason_code), OS_BLOCK);
return ESP_OK;
}{ ... }
int wps_finish(void)
{
struct wps_sm *sm = gWpsSm;
int ret = ESP_FAIL;
if (!gWpsSm) {
return ESP_FAIL;
}{...}
if (sm->wps->state == WPS_FINISHED) {
wpa_printf(MSG_DEBUG, "wps finished------>");
wps_set_status(WPS_STATUS_SUCCESS);
wps_delete_timer();
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_PR);
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_AR);
esp_wifi_set_wps_cb_internal(NULL);
if (sm->ap_cred_cnt == 1) {
wifi_config_t *config = os_zalloc(sizeof(wifi_config_t));
if (config == NULL) {
return ESP_FAIL;
}{...}
esp_wifi_get_config(WIFI_IF_STA, config);
esp_wifi_disconnect();
os_memcpy(config->sta.ssid, sm->creds[0].ssid, sm->creds[0].ssid_len);
os_memcpy(config->sta.password, sm->creds[0].key, sm->creds[0].key_len);
os_memcpy(config->sta.bssid, sm->bssid, ETH_ALEN);
#ifndef CONFIG_WPS_STRICT
if (sm->creds[0].auth_type == WPS_AUTH_WPAPSK &&
(sm->creds[0].encr_type & WPS_ENCR_AES)) {
config->sta.threshold.authmode = WIFI_AUTH_WPA_PSK;
}{...}
#endif/* ... */
config->sta.bssid_set = 0;
config->sta.sae_pwe_h2e = 0;
esp_wifi_set_config(WIFI_IF_STA, config);
esp_wifi_connect();
os_free(config);
}{...}
eloop_cancel_timeout(wifi_station_wps_success, NULL, NULL);
eloop_register_timeout(1, 0, wifi_station_wps_success, NULL, NULL);
ret = 0;
}{...} else {
wpa_printf(MSG_ERROR, "wps failed-----> ignore_sel_reg=%d", sm->ignore_sel_reg);
if (sm->ignore_sel_reg) {
sm->discover_ssid_cnt = 0;
esp_wifi_disconnect();
os_bzero(sm->creds, sizeof(sm->creds));
wps_add_discard_ap(sm->bssid);
}{...} else {
ret = wps_stop_process(WPS_FAIL_REASON_NORMAL);
}{...}
}{...}
return ret;
}{ ... }
static void wps_sm_notify_deauth(void)
{
if (gWpsSm && gWpsSm->wps->state != WPS_FINISHED) {
wps_stop_process(WPS_FAIL_REASON_RECV_DEAUTH);
}{...}
}{ ... }
void wps_add_discard_ap(u8 *bssid)
{
struct wps_sm *sm = gWpsSm;
u8 cnt = sm->discard_ap_cnt;
if (!gWpsSm || !bssid) {
return;
}{...}
if (sm->discard_ap_cnt < WPS_MAX_DIS_AP_NUM) {
sm->discard_ap_cnt++;
}{...} else {
for (cnt = 0; cnt < WPS_MAX_DIS_AP_NUM - 2; cnt++) {
os_memcpy(sm->dis_ap_list[cnt].bssid, sm->dis_ap_list[cnt + 1].bssid, 6);
}{...}
sm->discard_ap_cnt = WPS_MAX_DIS_AP_NUM;
}{...}
os_memcpy(sm->dis_ap_list[cnt].bssid, bssid, ETH_ALEN);
wpa_printf(MSG_INFO, "Added BSSID:"MACSTR" to discard list cnt=%d" , MAC2STR(bssid), sm->discard_ap_cnt);
}{ ... }
int wps_start_msg_timer(void)
{
struct wps_sm *sm = gWpsSm;
uint32_t msg_timeout;
int ret = ESP_FAIL;
if (!gWpsSm) {
return ESP_FAIL;
}{...}
if (sm->wps->state == WPS_FINISHED) {
msg_timeout = 100;
wpa_printf(MSG_DEBUG, "start msg timer WPS_FINISHED %" PRId32 " ms", msg_timeout);
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_register_timeout(0, msg_timeout*1000, wifi_station_wps_msg_timeout, NULL, NULL);
ret = 0;
}{...} else if (sm->wps->state == RECV_M2) {
msg_timeout = 5;
wpa_printf(MSG_DEBUG, "start msg timer RECV_M2 %" PRId32 " s", msg_timeout);
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_register_timeout(msg_timeout, 0, wifi_station_wps_msg_timeout, NULL, NULL);
ret = 0;
}{...}
return ret;
}{ ... }
/* ... */
int wps_sm_rx_eapol(u8 *src_addr, u8 *buf, u32 len)
{
if (!gWpsSm) {
return ESP_FAIL;
}{...}
#ifdef USE_WPS_TASK
{
struct wps_rx_param *param = os_zalloc(sizeof(struct wps_rx_param));
if (!param) {
return ESP_ERR_NO_MEM;
}{...}
param->buf = os_zalloc(len);
if (!param->buf) {
os_free(param);
return ESP_ERR_NO_MEM;
}{...}
os_memcpy(param->buf, buf, len);
param->len = len;
os_memcpy(param->sa, src_addr, ETH_ALEN);
wps_rxq_enqueue(param);
return wps_post(SIG_WPS_RX, 0);
}/* ... */
{...}#else
return wps_sm_rx_eapol_internal(src_addr, buf, len);
#endif
}{ ... }
int wps_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len)
{
struct wps_sm *sm = gWpsSm;
u32 plen, data_len, eap_len;
struct ieee802_1x_hdr *hdr;
struct eap_hdr *ehdr;
u8 *tmp;
u8 eap_code;
u8 eap_type;
int ret = ESP_FAIL;
enum wps_process_res res = WPS_DONE;
if (!gWpsSm) {
return ESP_FAIL;
}{...}
if (len < sizeof(*hdr) + sizeof(*ehdr)) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
"EAPOL-Key (len %lu, expecting at least %lu)",
(unsigned long) len,
(unsigned long) sizeof(*hdr) + sizeof(*ehdr));
return ESP_OK;
}{...}
tmp = buf;
hdr = (struct ieee802_1x_hdr *) tmp;
ehdr = (struct eap_hdr *) (hdr + 1);
plen = be_to_host16(hdr->length);
data_len = plen + sizeof(*hdr);
eap_len = be_to_host16(ehdr->length);
wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%" PRId32 "",
hdr->version, hdr->type, plen);
if (hdr->version < EAPOL_VERSION) {
}{...}
if (hdr->type != IEEE802_1X_TYPE_EAP_PACKET) {
wpa_printf(MSG_DEBUG, "WPS: EAP frame (type %u) discarded, "
"not a EAP PACKET frame", hdr->type);
ret = 0;
goto out;
}{...}
if (plen > len - sizeof(*hdr) || plen < sizeof(*ehdr)) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
"invalid (frame size %lu)",
(unsigned long) plen, (unsigned long) len);
ret = 0;
goto out;
}{...}
wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-EAP PACKET", tmp, len);
if (data_len < len) {
wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
"802.1X data", (unsigned long) len - data_len);
}{...}
if (eap_len != plen) {
wpa_printf(MSG_DEBUG, "WPA: EAPOL length %lu "
"invalid (eapol length %lu)",
(unsigned long) eap_len, (unsigned long) plen);
ret = 0;
goto out;
}{...}
eap_code = ehdr->code;
switch (eap_code) {
case EAP_CODE_SUCCESS:
wpa_printf(MSG_DEBUG, "error: receive eapol success frame!");
ret = 0;
break;...
case EAP_CODE_FAILURE:
wpa_printf(MSG_DEBUG, "receive eap code failure!");
ret = wps_finish();
break;...
case EAP_CODE_RESPONSE:
wpa_printf(MSG_DEBUG, "error: receive eapol response frame!");
ret = 0;
break;...
case EAP_CODE_REQUEST:
eap_type = ((u8 *)ehdr)[sizeof(*ehdr)];
switch (eap_type) {
case EAP_TYPE_IDENTITY:
wpa_printf(MSG_DEBUG, "=========identity===========");
sm->current_identifier = ehdr->identifier;
eloop_cancel_timeout(wifi_station_wps_eapol_start_handle, NULL, NULL);
wpa_printf(MSG_DEBUG, "WPS: Build EAP Identity.");
ret = wps_send_eap_identity_rsp(ehdr->identifier);
eloop_register_timeout(3, 0, wifi_station_wps_eapol_start_handle, NULL, NULL);
break;...
case EAP_TYPE_EXPANDED:
wpa_printf(MSG_DEBUG, "=========expanded plen[%" PRId32 "], %d===========", plen, sizeof(*ehdr));
sm->current_identifier = ehdr->identifier;
tmp = (u8 *)(ehdr + 1) + 1;
ret = wps_process_wps_mX_req(tmp, plen - sizeof(*ehdr) - 1, &res);
if (res == WPS_FRAGMENT) {
wpa_printf(MSG_DEBUG, "wps frag, silently exit", res);
ret = ESP_OK;
break;
}{...}
if (ret == ESP_OK && res != WPS_FAILURE) {
ret = wps_send_wps_mX_rsp(ehdr->identifier);
if (ret == ESP_OK) {
wpa_printf(MSG_DEBUG, "sm->wps->state = %d", sm->wps->state);
wps_start_msg_timer();
}{...}
}{...} else if (ret == ESP_ERR_INVALID_STATE) {
ret = ESP_OK;
}{...} else {
ret = ESP_FAIL;
}{...}
break;...
default:
break;...
}{...}
break;...
default:
break;...
}{...}
out:
if (ret != 0 && sm->ignore_sel_reg) {
wifi_wps_scan(NULL, NULL);
}{...} else if ((ret != 0 || res == WPS_FAILURE)) {
wifi_event_sta_wps_fail_reason_t reason_code = WPS_FAIL_REASON_NORMAL;
wpa_printf(MSG_DEBUG, "wpa rx eapol internal: fail ret=%d", ret);
wps_set_status(WPS_STATUS_DISABLE);
esp_wifi_disarm_sta_connection_timer_internal();
eloop_cancel_timeout(wifi_station_wps_timeout, NULL, NULL);
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &reason_code, sizeof(reason_code), OS_BLOCK);
return ret;
}{...}
return ret;
}{ ... }
int wps_set_default_factory(void)
{
if (!s_factory_info) {
s_factory_info = os_zalloc(sizeof(wps_factory_information_t));
if (!s_factory_info) {
wpa_printf(MSG_ERROR, "wps factory info malloc failed");
return ESP_ERR_NO_MEM;
}{...}
}{...}
os_snprintf(s_factory_info->manufacturer, WPS_MAX_MANUFACTURER_LEN, "ESPRESSIF");
os_snprintf(s_factory_info->model_name, WPS_MAX_MODEL_NUMBER_LEN, "ESPRESSIF IOT");
os_snprintf(s_factory_info->model_number, WPS_MAX_MODEL_NAME_LEN, wps_model_number);
os_snprintf(s_factory_info->device_name, WPS_MAX_DEVICE_NAME_LEN, "%s STATION", wps_model_number);
return ESP_OK;
}{ ... }
int wps_set_factory_info(const esp_wps_config_t *config)
{
int ret;
ret = wps_set_default_factory();
if (ret != 0) {
return ret;
}{...}
if (config->factory_info.manufacturer[0] != 0) {
os_memcpy(s_factory_info->manufacturer, config->factory_info.manufacturer, WPS_MAX_MANUFACTURER_LEN - 1);
}{...}
if (config->factory_info.model_number[0] != 0) {
os_memcpy(s_factory_info->model_number, config->factory_info.model_number, WPS_MAX_MODEL_NUMBER_LEN - 1);
}{...}
if (config->factory_info.model_name[0] != 0) {
os_memcpy(s_factory_info->model_name, config->factory_info.model_name, WPS_MAX_MODEL_NAME_LEN - 1);
}{...}
if (config->factory_info.device_name[0] != 0) {
os_memcpy(s_factory_info->device_name, config->factory_info.device_name, WPS_MAX_DEVICE_NAME_LEN - 1);
}{...}
wpa_printf(MSG_INFO, "manufacturer: %s, model number: %s, model name: %s, device name: %s", s_factory_info->manufacturer,
s_factory_info->model_number, s_factory_info->model_name, s_factory_info->device_name);
return ESP_OK;
}{ ... }
int wps_dev_init(void)
{
int ret = 0;
struct wps_sm *sm = gWpsSm;
struct wps_device_data *dev = NULL;
if (!sm) {
ret = ESP_FAIL;
goto _out;
}{...}
dev = &sm->wps_ctx->dev;
sm->dev = dev;
if (!dev) {
ret = ESP_FAIL;
goto _out;
}{...}
dev->config_methods = WPS_CONFIG_VIRT_PUSHBUTTON;
#ifdef CONFIG_ESP_WIFI_WPS_PASSPHRASE
dev->config_methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY;
#endif
dev->rf_bands = WPS_RF_24GHZ;
WPA_PUT_BE16(dev->pri_dev_type, WPS_DEV_PHONE);
WPA_PUT_BE32(dev->pri_dev_type + 2, WPS_DEV_OUI_WFA);
WPA_PUT_BE16(dev->pri_dev_type + 6, WPS_DEV_PHONE_SINGLE_MODE);
if (!s_factory_info) {
ret = wps_set_default_factory();
if (ret != 0) {
goto _out;
}{...}
}{...}
dev->manufacturer = os_zalloc(WPS_MAX_MANUFACTURER_LEN);
if (!dev->manufacturer) {
ret = ESP_FAIL;
goto _out;
}{...}
os_snprintf(dev->manufacturer, WPS_MAX_MANUFACTURER_LEN, s_factory_info->manufacturer);
dev->model_name = os_zalloc(WPS_MAX_MODEL_NAME_LEN);
if (!dev->model_name) {
ret = ESP_FAIL;
goto _out;
}{...}
os_snprintf(dev->model_name, WPS_MAX_MODEL_NAME_LEN, s_factory_info->model_name);
dev->model_number = os_zalloc(WPS_MAX_MODEL_NAME_LEN);
if (!dev->model_number) {
ret = ESP_FAIL;
goto _out;
}{...}
os_snprintf(dev->model_number, WPS_MAX_MODEL_NAME_LEN, s_factory_info->model_number);
dev->device_name = os_zalloc(WPS_MAX_DEVICE_NAME_LEN);
if (!dev->device_name) {
ret = ESP_FAIL;
goto _out;
}{...}
os_snprintf(dev->device_name, WPS_MAX_DEVICE_NAME_LEN, s_factory_info->device_name);
dev->serial_number = os_zalloc(16);
if (!dev->serial_number) {
ret = ESP_FAIL;
goto _out;
}{...}
os_snprintf(dev->serial_number, 16, "%02x%02x%02x%02x%02x%02x",
sm->ownaddr[0], sm->ownaddr[1], sm->ownaddr[2],
sm->ownaddr[3], sm->ownaddr[4], sm->ownaddr[5]);
uuid_gen_mac_addr(sm->ownaddr, sm->uuid);
os_memcpy(dev->mac_addr, sm->ownaddr, ETH_ALEN);
return ESP_OK;
_out:
wps_dev_deinit(dev);
return ret;
}{ ... }
int wps_dev_deinit(struct wps_device_data *dev)
{
int ret = 0;
if (!dev) {
return ESP_FAIL;
}{...}
wps_device_data_free(dev);
if (s_factory_info) {
os_free(s_factory_info);
s_factory_info = NULL;
}{...}
return ret;
}{ ... }
void
wifi_station_wps_timeout_internal(void)
{
struct wps_sm *sm = gWpsSm;
if (!sm) {
return;
}{...}
esp_wifi_disarm_sta_connection_timer_internal();
wps_set_status(WPS_STATUS_DISABLE);
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_TIMEOUT, 0, 0, OS_BLOCK);
}{ ... }
void wifi_station_wps_timeout(void *data, void *user_ctx)
{
#ifdef USE_WPS_TASK
wps_post(SIG_WPS_TIMER_TIMEOUT, 0);
return;/* ... */
#else
wifi_station_wps_timeout_internal();
#endif
}{ ... }
void
wifi_station_wps_msg_timeout_internal(void)
{
struct wps_sm *sm = gWpsSm;
if (!sm) {
return;
}{...}
if (sm->wps->state == WPS_FINISHED) {
wpa_printf(MSG_DEBUG, "wps msg timeout WPS_FINISHED");
wps_finish();
return;
}{...} else if (sm->wps->state == RECV_M2) {
wpa_printf(MSG_DEBUG, "wps msg timeout RECV_M2");
if (!sm->ignore_sel_reg) {
wps_stop_process(WPS_FAIL_REASON_RECV_M2D);
}{...}
}{...}
if (sm->ignore_sel_reg) {
esp_wifi_disconnect();
wps_add_discard_ap(sm->bssid);
os_bzero(sm->creds, sizeof(sm->creds));
os_bzero(sm->bssid, ETH_ALEN);
sm->discover_ssid_cnt = 0;
wifi_wps_scan(NULL, NULL);
}{...}
}{ ... }
void wifi_station_wps_msg_timeout(void *data, void *user_ctx)
{
#ifdef USE_WPS_TASK
wps_post(SIG_WPS_TIMER_MSG_TIMEOUT, 0);
return;/* ... */
#else
wifi_station_wps_msg_timeout_internal();
#endif
}{ ... }
void wifi_station_wps_success_internal(void)
{
wifi_event_sta_wps_er_success_t evt = {0};
struct wps_sm *sm = gWpsSm;
int i;
/* ... */
if (sm->ap_cred_cnt > 1) {
evt.ap_cred_cnt = sm->ap_cred_cnt;
for (i = 0; i < MAX_WPS_AP_CRED; i++) {
os_memcpy(evt.ap_cred[i].ssid, sm->creds[i].ssid, sm->creds[i].ssid_len);
os_memcpy(evt.ap_cred[i].passphrase, sm->creds[i].key, sm->creds[i].key_len);
}{...}
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS, &evt,
sizeof(evt), OS_BLOCK);
}{...} else {
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS,
0, 0, OS_BLOCK);
}{...}
}{ ... }
void wifi_station_wps_success(void *data, void *user_ctx)
{
#ifdef USE_WPS_TASK
wps_post(SIG_WPS_TIMER_SUCCESS_CB, 0);
return;/* ... */
#else
wifi_station_wps_success_internal();
#endif
}{ ... }
void wifi_station_wps_eapol_start_handle_internal(void)
{
wpa_printf(MSG_DEBUG, "Resend EAPOL-Start.");
wps_tx_start();
}{ ... }
void wifi_station_wps_eapol_start_handle(void *data, void *user_ctx)
{
#ifdef USE_WPS_TASK
wps_post(SIG_WPS_TIMER_EAPOL_START, 0);
return;/* ... */
#else
wifi_station_wps_eapol_start_handle_internal();
#endif
}{ ... }
static int save_credentials_cb(void *ctx, const struct wps_credential *cred)
{
struct wps_credential *creds;
if (!gWpsSm || !cred || gWpsSm->ap_cred_cnt >= MAX_CRED_COUNT) {
return ESP_FAIL;
}{...}
creds = &gWpsSm->creds[gWpsSm->ap_cred_cnt];
memcpy(creds, cred, sizeof(*creds));
gWpsSm->ap_cred_cnt++;
wpa_hexdump_ascii(MSG_DEBUG, "ssid ", cred->ssid, cred->ssid_len);
wpa_hexdump_ascii(MSG_DEBUG, "key ", cred->key, cred->key_len);
return ESP_OK;
}{ ... }
int wps_init_cfg_pin(struct wps_config *cfg)
{
if (wps_get_type() != WPS_TYPE_PIN) {
cfg->pbc = 1;
return 0;
}{...}
cfg->pbc = 0;
if ((os_strncmp((char *)cfg->pin, "00000000", 8) == 0) || !wps_pin_str_valid((char *)cfg->pin)) {
unsigned int spin = 0;
cfg->dev_pw_id = DEV_PW_DEFAULT;
cfg->pin_len = 8;
if (wps_generate_pin(&spin) < 0) {
return -1;
}{...}
wpa_printf(MSG_INFO, "Provided PIN %s is not valid, generated a new PIN %08d", (char *)cfg->pin, spin);
os_snprintf((char *)cfg->pin, 9, "%08d", spin);
}{...}
return 0;
}{ ... }
struct wps_sm_funcs* wps_get_wps_sm_cb(void)
{
return s_wps_sm_cb;
}{ ... }
static int wifi_station_wps_init(const esp_wps_config_t *config)
{
struct wps_funcs *wps_cb;
struct wps_sm *sm = NULL;
struct wps_config cfg = {0};
if (gWpsSm) {
goto _out;
}{...}
wpa_printf(MSG_DEBUG, "wifi sta wps init");
gWpsSm = os_zalloc(sizeof(struct wps_sm));
if (!gWpsSm) {
goto _out;
}{...}
sm = gWpsSm;
esp_wifi_get_macaddr_internal(WIFI_IF_STA, sm->ownaddr);
os_memcpy(gWpaSm.own_addr, sm->ownaddr, ETH_ALEN);
sm->identity_len = WSC_ID_ENROLLEE_LEN;
os_memcpy(sm->identity, WSC_ID_ENROLLEE, sm->identity_len);
sm->wps_ctx = os_zalloc(sizeof(struct wps_context));
if (!sm->wps_ctx) {
goto _err;
}{...}
if (wps_dev_init() != 0) {
goto _err;
}{...}
cfg.wps = sm->wps_ctx;
os_memcpy((void *)cfg.pin, config->pin, 8);
if (wps_init_cfg_pin(&cfg) < 0) {
goto _err;
}{...}
os_memcpy(cfg.wps->uuid, sm->uuid, WPS_UUID_LEN);
cfg.wps->config_methods = sm->wps_ctx->dev.config_methods;
if ((sm->wps = wps_init(&cfg)) == NULL) {
goto _err;
}{...}
if (wps_get_type() == WPS_TYPE_PIN) {
wifi_event_sta_wps_er_pin_t evt;
os_memcpy(evt.pin_code, sm->wps->dev_password, 8);
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PIN, &evt, sizeof(evt), OS_BLOCK);
}{...}
sm->wps->wps->cred_cb = save_credentials_cb;
if (esp_wifi_get_appie_internal(WIFI_APPIE_WPS_PR) == NULL) {
wps_build_ic_appie_wps_pr();
}{...}
if (esp_wifi_get_appie_internal(WIFI_APPIE_WPS_AR) == NULL) {
wps_build_ic_appie_wps_ar();
}{...}
eloop_cancel_timeout(wifi_station_wps_timeout, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_success, NULL, NULL);
eloop_cancel_timeout(wifi_wps_scan, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_eapol_start_handle, NULL, NULL);
wps_cb = os_malloc(sizeof(struct wps_funcs));
if (wps_cb == NULL) {
goto _err;
}{...}
wps_cb->wps_parse_scan_result = wps_parse_scan_result;
wps_cb->wifi_station_wps_start = wifi_station_wps_start;
wps_cb->wps_sm_rx_eapol = wps_sm_rx_eapol;
wps_cb->wps_start_pending = wps_start_pending;
esp_wifi_set_wps_cb_internal(wps_cb);
s_wps_sm_cb = os_malloc(sizeof(struct wps_sm_funcs));
if (s_wps_sm_cb == NULL) {
goto _err;
}{...}
s_wps_sm_cb->wps_sm_notify_deauth = wps_sm_notify_deauth;
return ESP_OK;
_err:
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_PR);
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_AR);
if (sm->dev) {
wps_dev_deinit(sm->dev);
sm->dev = NULL;
}{...}
if (sm->wps_ctx) {
if (sm->wps_ctx->dh_privkey) {
wpabuf_free(sm->wps_ctx->dh_privkey);
}{...}
os_free(sm->wps_ctx);
sm->wps_ctx = NULL;
}{...}
if (sm->wps) {
wps_deinit(sm->wps);
sm->wps = NULL;
}{...}
os_free(gWpsSm);
gWpsSm = NULL;
return ESP_FAIL;
_out:
return ESP_FAIL;
}{ ... }
int wps_delete_timer(void)
{
struct wps_sm *sm = gWpsSm;
if (!sm) {
return ESP_OK;
}{...}
eloop_cancel_timeout(wifi_station_wps_success, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_timeout, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_cancel_timeout(wifi_wps_scan, NULL, NULL);
eloop_cancel_timeout(wifi_station_wps_eapol_start_handle, NULL, NULL);
esp_wifi_disarm_sta_connection_timer_internal();
return ESP_OK;
}{ ... }
int
wifi_station_wps_deinit(void)
{
struct wps_sm *sm = gWpsSm;
if (gWpsSm == NULL) {
return ESP_FAIL;
}{...}
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_PR);
esp_wifi_unset_appie_internal(WIFI_APPIE_WPS_AR);
esp_wifi_set_wps_cb_internal(NULL);
if (sm->dev) {
wps_dev_deinit(sm->dev);
sm->dev = NULL;
}{...}
if (sm->wps_ctx) {
if (sm->wps_ctx->dh_privkey) {
wpabuf_free(sm->wps_ctx->dh_privkey);
}{...}
os_free(sm->wps_ctx);
sm->wps_ctx = NULL;
}{...}
if (sm->wps) {
wps_deinit(sm->wps);
sm->wps = NULL;
}{...}
if (s_wps_sm_cb) {
os_free(s_wps_sm_cb);
s_wps_sm_cb = NULL;
}{...}
os_free(gWpsSm);
gWpsSm = NULL;
return ESP_OK;
}{ ... }
struct wps_sm *
wps_sm_get(void)
{
return gWpsSm;
}{ ... }
void
wifi_wps_scan_done(void *arg, ETS_STATUS status)
{
struct wps_sm *sm = gWpsSm;
wifi_config_t wifi_config = {0};
wpa_printf(MSG_INFO, "WPS: scan done");
if (wps_get_type() == WPS_TYPE_DISABLE) {
return;
}{...}
if (!sm) {
return;
}{...}
if (sm->discover_ssid_cnt == 1) {
wps_set_status(WPS_STATUS_PENDING);
}{...} else if (sm->discover_ssid_cnt == 0) {
wps_set_status(WPS_STATUS_SCANNING);
}{...} else {
if (sm->wps_pbc_overlap) {
sm->wps_pbc_overlap = false;
wpa_printf(MSG_INFO, "PBC session overlap!");
wps_set_status(WPS_STATUS_DISABLE);
esp_event_post(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP, 0, 0, OS_BLOCK);
}{...} else {
wps_set_status(WPS_STATUS_PENDING);
}{...}
}{...}
wpa_printf(MSG_DEBUG, "wps scan_done discover_ssid_cnt = %d", sm->discover_ssid_cnt);
sm->discover_ssid_cnt = 0;
if (wps_get_status() == WPS_STATUS_PENDING) {
esp_wifi_disconnect();
os_memcpy(wifi_config.sta.bssid, sm->bssid, ETH_ALEN);
os_memcpy(wifi_config.sta.ssid, (char *)sm->creds[0].ssid, sm->creds[0].ssid_len);
wifi_config.sta.bssid_set = 1;
wifi_config.sta.channel = sm->channel;
wpa_printf(MSG_INFO, "WPS: connecting to %s, bssid=" MACSTR,
(char *)sm->creds[0].ssid, MAC2STR(wifi_config.sta.bssid));
esp_wifi_set_config(0, &wifi_config);
wpa_printf(MSG_DEBUG, "WPS: neg start");
wifi_config.sta.failure_retry_cnt = 2;
esp_wifi_connect();
sm->state = WAIT_START;
eloop_cancel_timeout(wifi_station_wps_msg_timeout, NULL, NULL);
eloop_register_timeout(2, 0, wifi_station_wps_msg_timeout, NULL, NULL);
}{...} else if (wps_get_status() == WPS_STATUS_SCANNING) {
if (wps_get_type() == WPS_TYPE_PIN && sm->scan_cnt > WPS_IGNORE_SEL_REG_MAX_CNT) {
wpa_printf(MSG_INFO, "WPS: ignore selected registrar after %d scans", sm->scan_cnt);
sm->ignore_sel_reg = true;
}{...}
eloop_cancel_timeout(wifi_wps_scan, NULL, NULL);
eloop_register_timeout(2, 0, wifi_wps_scan, NULL, NULL);
}{...} else {
return;
}{...}
}{ ... }
void
wifi_wps_scan_internal(void)
{
struct wps_sm *sm = gWpsSm;
sm->scan_cnt++;
wpa_printf(MSG_DEBUG, "wifi_wps_scan : %d", sm->scan_cnt);
typedef void (* scan_done_cb_t)(void *arg, ETS_STATUS status);
extern int esp_wifi_promiscuous_scan_start(wifi_scan_config_t *config, scan_done_cb_t cb);
esp_wifi_promiscuous_scan_start(NULL, wifi_wps_scan_done);
}{ ... }
void wifi_wps_scan(void *data, void *user_ctx)
{
#ifdef USE_WPS_TASK
wps_post(SIG_WPS_TIMER_SCAN, 0);
return;/* ... */
#else
wifi_wps_scan_internal();
#endif
}{ ... }
static int wps_rf_band_cb(void *ctx)
{
return WPS_RF_24GHZ;
}{ ... }
int wifi_station_wps_start(void)
{
struct wps_sm *sm = wps_sm_get();
if (!sm) {
wpa_printf(MSG_ERROR, "WPS: wps is not initialized");
return ESP_FAIL;
}{...}
eloop_register_timeout(120, 0, wifi_station_wps_timeout, NULL, NULL);
switch (wps_get_status()) {
case WPS_STATUS_DISABLE:
sm->is_wps_scan = true;
wps_build_public_key(sm->wps, NULL);
if (sm->wps->wps->dh_privkey) {
wpabuf_free(sm->wps->wps->dh_privkey);
}{...}
sm->wps->wps->dh_privkey = sm->wps->dh_privkey;
sm->wps->wps->dh_ctx = sm->wps->dh_ctx;
sm->wps->wps->dh_pubkey = sm->wps->dh_pubkey_e;
sm->wps->wps->rf_band_cb = wps_rf_band_cb;
sm->wps->dh_privkey = NULL;
wifi_wps_scan(NULL, NULL);
break;...
case WPS_STATUS_SCANNING:
sm->scan_cnt = 0;
eloop_cancel_timeout(wifi_station_wps_timeout, NULL, NULL);
eloop_register_timeout(120, 0, wifi_station_wps_timeout, NULL, NULL);
break;...
default:
break;...
}{...}
sm->discard_ap_cnt = 0;
os_memset(&sm->dis_ap_list, 0, WPS_MAX_DIS_AP_NUM * sizeof(struct discard_ap_list_t));
esp_wifi_set_wps_start_flag_internal(true);
return ESP_OK;
}{ ... }
int wps_task_deinit(void)
{
wpa_printf(MSG_DEBUG, "wps task deinit");
if (s_wps_api_sem) {
os_semphr_delete(s_wps_api_sem);
s_wps_api_sem = NULL;
wpa_printf(MSG_DEBUG, "wps task deinit: free api sem");
}{...}
if (s_wps_task_create_sem) {
os_semphr_delete(s_wps_task_create_sem);
s_wps_task_create_sem = NULL;
wpa_printf(MSG_DEBUG, "wps task deinit: free task create sem");
}{...}
if (s_wps_queue) {
os_queue_delete(s_wps_queue);
s_wps_queue = NULL;
wpa_printf(MSG_DEBUG, "wps task deinit: free queue");
}{...}
if (STAILQ_FIRST(&s_wps_rxq) != NULL){
wps_rxq_deinit();
}{...}
if (s_wps_data_lock) {
os_mutex_delete(s_wps_data_lock);
s_wps_data_lock = NULL;
}{...}
return ESP_OK;
}{ ... }
int wps_task_init(void)
{
int ret = 0;
/* ... */
wps_task_deinit();
if (!s_wps_data_lock) {
s_wps_data_lock = os_recursive_mutex_create();
if (!s_wps_data_lock) {
wpa_printf(MSG_ERROR, "wps task init: failed to alloc data lock");
goto _wps_no_mem;
}{...}
}{...}
s_wps_api_sem = os_semphr_create(1, 0);
if (!s_wps_api_sem) {
wpa_printf(MSG_ERROR, "wps task init: failed to create api sem");
goto _wps_no_mem;
}{...}
s_wps_task_create_sem = os_semphr_create(1, 0);
if (!s_wps_task_create_sem) {
wpa_printf(MSG_ERROR, "wps task init: failed to create task sem");
goto _wps_no_mem;
}{...}
os_bzero(s_wps_sig_cnt, SIG_WPS_NUM);
s_wps_queue = os_queue_create(SIG_WPS_NUM, sizeof(ETSEvent));
if (!s_wps_queue) {
wpa_printf(MSG_ERROR, "wps task init: failed to alloc queue");
goto _wps_no_mem;
}{...}
wps_rxq_init();
ret = os_task_create(wps_task, "wpsT", WPS_TASK_STACK_SIZE, NULL, 2, &s_wps_task_hdl);
if (TRUE != ret) {
wpa_printf(MSG_ERROR, "wps enable: failed to create task");
goto _wps_no_mem;
}{...}
os_semphr_take(s_wps_task_create_sem, OS_BLOCK);
os_semphr_delete(s_wps_task_create_sem);
s_wps_task_create_sem = NULL;
wpa_printf(MSG_DEBUG, "wifi wps enable: task prio:%d, stack:%d", 2, WPS_TASK_STACK_SIZE);
return ESP_OK;
_wps_no_mem:
wps_task_deinit();
return ESP_ERR_NO_MEM;
}{ ... }
int wps_post_block(uint32_t sig, void *arg)
{
wps_ioctl_param_t param;
param.ret = ESP_FAIL;
param.arg = arg;
if (ESP_OK != wps_post(sig, (uint32_t)¶m)) {
return ESP_FAIL;
}{...}
if (TRUE == os_semphr_take(s_wps_api_sem, OS_BLOCK)) {
return param.ret;
}{...} else {
return ESP_FAIL;
}{...}
}{ ... }
int wps_check_wifi_mode(void)
{
wifi_mode_t mode;
int ret;
ret = esp_wifi_get_mode(&mode);
if (ESP_OK != ret) {
wpa_printf(MSG_ERROR, "wps check wifi mode: failed to get wifi mode ret=%d", ret);
return ESP_FAIL;
}{...}
if (
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
mode == WIFI_MODE_AP ||
#endif
mode == WIFI_MODE_NULL) {
wpa_printf(MSG_ERROR, "wps check wifi mode: wrong wifi mode=%d ", mode);
return ESP_ERR_WIFI_MODE;
}{...}
return ESP_OK;
}{ ... }
int esp_wifi_wps_enable(const esp_wps_config_t *config)
{
int ret = ESP_OK;
struct wps_sm *sm = gWpsSm;
if (esp_wifi_get_user_init_flag_internal() == 0) {
wpa_printf(MSG_ERROR, "wps enable: wifi not started cannot disable wpsreg");
return ESP_ERR_WIFI_STATE;
}{...}
if (ESP_OK != wps_check_wifi_mode()) {
return ESP_ERR_WIFI_MODE;
}{...}
if (is_dpp_enabled()) {
wpa_printf(MSG_ERROR, "wps enabled failed since DPP is initialized");
return ESP_FAIL;
}{...}
API_MUTEX_TAKE();
if (s_wps_enabled) {
if (sm && os_memcmp(sm->identity, WSC_ID_REGISTRAR, sm->identity_len) == 0) {
wpa_printf(MSG_ERROR, "wps enable: wpsreg already enabled cannot enable wps enrollee");
ret = ESP_ERR_WIFI_MODE;
}{...} else {
wpa_printf(MSG_DEBUG, "wps enable: already enabled");
}{...}
API_MUTEX_GIVE();
return ret;
}{...}
#ifdef USE_WPS_TASK
ret = wps_task_init();
if (ESP_OK != ret) {
API_MUTEX_GIVE();
return ret;
}{...}
ret = wps_post_block(SIG_WPS_ENABLE, (esp_wps_config_t *)config);
if (ESP_OK != ret) {
wps_task_deinit();
API_MUTEX_GIVE();
return ret;
}{...}
s_wps_enabled = true;
wpa_printf(MSG_DEBUG, "wifi wps task: prio:%d, stack:%d", 2, WPS_TASK_STACK_SIZE);
API_MUTEX_GIVE();
return ret;/* ... */
#else
ret = wifi_wps_enable_internal(config);
API_MUTEX_GIVE();
return ret;/* ... */
#endif
}{ ... }
bool is_wps_enabled(void)
{
return s_wps_enabled;
}{ ... }
int wifi_wps_enable_internal(const esp_wps_config_t *config)
{
int ret = 0;
struct wpa_sm *wpa_sm = &gWpaSm;
wpa_printf(MSG_DEBUG, "ESP WPS crypto initialize!");
if (config->wps_type == WPS_TYPE_DISABLE) {
wpa_printf(MSG_ERROR, "wps enable: invalid wps type");
return ESP_ERR_WIFI_WPS_TYPE;
}{...}
wpa_printf(MSG_DEBUG, "Set factory information.");
ret = wps_set_factory_info(config);
if (ret != 0) {
return ret;
}{...}
wpa_printf(MSG_INFO, "wifi_wps_enable");
wps_set_type(config->wps_type);
wps_set_status(WPS_STATUS_DISABLE);
ret = wifi_station_wps_init(config);
if (ret != 0) {
wps_set_type(WPS_TYPE_DISABLE);
wps_set_status(WPS_STATUS_DISABLE);
return ESP_FAIL;
}{...}
wpa_sm->wpa_sm_wps_disable = esp_wifi_wps_disable;
return ESP_OK;
}{ ... }
int wifi_wps_disable_internal(void)
{
wps_set_status(WPS_STATUS_DISABLE);
/* ... */
wps_delete_timer();
wifi_station_wps_deinit();
return ESP_OK;
}{ ... }
int esp_wifi_wps_disable(void)
{
int ret = 0;
int wps_status;
struct wps_sm *wps_sm = gWpsSm;
struct wpa_sm *wpa_sm = &gWpaSm;
if (wps_sm && os_memcmp(wps_sm->identity, WSC_ID_REGISTRAR, wps_sm->identity_len) == 0) {
return ESP_ERR_WIFI_MODE;
}{...}
API_MUTEX_TAKE();
if (!s_wps_enabled) {
wpa_printf(MSG_DEBUG, "wps disable: already disabled");
API_MUTEX_GIVE();
return ESP_OK;
}{...}
wps_status = wps_get_status();
wpa_printf(MSG_INFO, "wifi_wps_disable");
wps_set_type(WPS_TYPE_DISABLE);
#ifdef USE_WPS_TASK
ret = wps_post_block(SIG_WPS_DISABLE, 0);
#else
ret = wifi_wps_disable_internal();
#endif
if (ESP_OK != ret) {
wpa_printf(MSG_ERROR, "wps disable: failed to disable wps, ret=%d", ret);
}{...}
if ((wps_status == WPS_STATUS_PENDING) || (wps_status == WPS_STATUS_SUCCESS)) {
esp_wifi_disconnect();
}{...}
esp_wifi_set_wps_start_flag_internal(false);
wps_task_deinit();
s_wps_enabled = false;
API_MUTEX_GIVE();
wpa_sm->wpa_sm_wps_disable = NULL;
return ESP_OK;
}{ ... }
int esp_wifi_wps_start(int timeout_ms)
{
if (ESP_OK != wps_check_wifi_mode()) {
return ESP_ERR_WIFI_MODE;
}{...}
API_MUTEX_TAKE();
if (!s_wps_enabled) {
wpa_printf(MSG_ERROR, "wps start: wps not enabled");
API_MUTEX_GIVE();
return ESP_ERR_WIFI_WPS_SM;
}{...}
if (wps_get_type() == WPS_TYPE_DISABLE || (wps_get_status() != WPS_STATUS_DISABLE && wps_get_status() != WPS_STATUS_SCANNING)) {
API_MUTEX_GIVE();
return ESP_ERR_WIFI_WPS_TYPE;
}{...}
if (esp_wifi_get_user_init_flag_internal() == 0) {
API_MUTEX_GIVE();
return ESP_ERR_WIFI_STATE;
}{...}
wpa_printf(MSG_DEBUG, "wps scan");
#ifdef USE_WPS_TASK
wps_post_block(SIG_WPS_START, 0);
#else
ic_pp_post(SIG_PP_WPS, 0);
#endif
API_MUTEX_GIVE();
return ESP_OK;
}{ ... }