1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include <sys/time.h>
19
20 struct wpa_funcs {
21 bool (*wpa_sta_init)(void);
22 bool (*wpa_sta_deinit)(void);
23 void (*wpa_sta_connect)(uint8_t *bssid);
24 void (*wpa_sta_disconnected_cb)(uint8_t reason_code);
25 int (*wpa_sta_rx_eapol)(uint8_t *src_addr, uint8_t *buf, uint32_t len);
26 bool (*wpa_sta_in_4way_handshake)(void);
27 void *(*wpa_ap_init)(void);
28 bool (*wpa_ap_deinit)(char *data);
29 bool (*wpa_ap_join)(char **sm, uint8_t *bssid, uint8_t *wpa_ie, uint8_t wpa_ie_len);
30 bool (*wpa_ap_remove)(char *sm);
31 uint8_t *(*wpa_ap_get_wpa_ie)(uint8_t *len);
32 bool (*wpa_ap_rx_eapol)(char *hapd_data, char *sm, uint8_t *data, size_t data_len);
33 void (*wpa_ap_get_peer_spp_msg)(char *sm, bool *spp_cap, bool *spp_req);
34 char *(*wpa_config_parse_string)(const char *value, size_t *len);
35 int (*wpa_parse_wpa_ie)(const uint8_t *wpa_ie, size_t wpa_ie_len, char *data);
36 int (*wpa_config_bss)(uint8_t *bssid);
37 int (*wpa_michael_mic_failure)(uint16_t is_unicast);
38 uint8_t *(*wpa3_build_sae_msg)(uint8_t *bssid, uint32_t type, size_t *len);
39 int (*wpa3_parse_sae_msg)(uint8_t *buf, size_t len, uint32_t type, uint16_t status);
40 int (*resfunc)(void);
41 void (*wpa_config_done)(void);
42 };
43
44 struct {
45 uint32_t size;
46 uint32_t version;
47 char *func[24];
48 }const g_wifi_default_wpa_crypto_funcs = {
49 .size = sizeof(g_wifi_default_wpa_crypto_funcs),
50 .version = 1,
51 {NULL}
52 };
53
54 const char *g_wifi_default_mesh_crypto_funcs[2] = { NULL, NULL};
55 int esp_wifi_register_wpa_cb_internal(struct wpa_funcs *cb);
56 int esp_wifi_unregister_wpa_cb_internal(void);
57
wpa_attach(void)58 static bool wpa_attach(void)
59 {
60 return true;
61 }
62
wpa_deattach(void)63 static bool wpa_deattach(void)
64 {
65 return true;
66 }
67
wpa_sta_connect(uint8_t * bssid)68 static void wpa_sta_connect(uint8_t *bssid)
69 {
70 }
71
wpa_sta_disconnected_cb(uint8_t reason_code)72 static void wpa_sta_disconnected_cb(uint8_t reason_code)
73 {
74 }
75
wpa_parse_wpa_ie(const uint8_t * wpa_ie,size_t wpa_ie_len,char * data)76 static int wpa_parse_wpa_ie(const uint8_t *wpa_ie, size_t wpa_ie_len, char *data)
77 {
78 return 0;
79 }
80
wpa_config_done(void)81 static void wpa_config_done(void)
82 {
83 }
84
esp_supplicant_init(void)85 int esp_supplicant_init(void)
86 {
87 struct wpa_funcs *wpa_cb;
88
89 wpa_cb = (struct wpa_funcs *)malloc(sizeof(struct wpa_funcs));
90 if (!wpa_cb) {
91 return -1;
92 }
93 (void)memset_s(wpa_cb, sizeof(struct wpa_funcs), 0, sizeof(struct wpa_funcs));
94 wpa_cb->wpa_sta_init = wpa_attach;
95 wpa_cb->wpa_sta_deinit = wpa_deattach;
96 wpa_cb->wpa_sta_connect = wpa_sta_connect;
97 wpa_cb->wpa_sta_disconnected_cb = wpa_sta_disconnected_cb;
98
99 wpa_cb->wpa_parse_wpa_ie = wpa_parse_wpa_ie;
100 wpa_cb->wpa_config_done = wpa_config_done;
101 esp_wifi_register_wpa_cb_internal(wpa_cb);
102 return 0;
103 }
104
esp_supplicant_deinit(void)105 int esp_supplicant_deinit(void)
106 {
107 return esp_wifi_unregister_wpa_cb_internal();
108 }
109
hexstr2bin(const char * hex,unsigned char * buf,size_t len)110 int hexstr2bin(const char *hex, unsigned char *buf, size_t len)
111 {
112 unsigned char t = 0;
113
114 for (size_t i = 0; i < len; i++) {
115 unsigned char c = hex[i];
116 if (c >= '0' && c <= '9') {
117 c -= '0';
118 } else if (c >= 'a' && c <= 'f') {
119 c -= 'W';
120 } else if (c >= 'A' && c <= 'F') {
121 c -= '7';
122 } else {
123 return -1;
124 }
125 if (i & 1) {
126 *buf++ = (t << '\x4') | c;
127 } else {
128 t = c;
129 }
130 }
131 return 0;
132 }
133