blob: e85d1328fdc2b809609542f04b5e0c1676fd3c3f [file] [log] [blame]
Damien Georgeea186de2021-09-22 00:35:46 +10001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * Development of the code in this file was sponsored by Microbric Pty Ltd
5 * and Mnemote Pty Ltd
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2016, 2017 Nick Moore @mnemote
10 * Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
11 *
12 * Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky
13 * And the ESP IDF example code which is Public Domain / CC0
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this software and associated documentation files (the "Software"), to deal
17 * in the Software without restriction, including without limitation the rights
18 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 * copies of the Software, and to permit persons to whom the Software is
20 * furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 * THE SOFTWARE.
32 */
33
34#include <string.h>
35
36#include "py/objlist.h"
37#include "py/runtime.h"
robert-hhd6bc34a2023-01-19 12:42:09 +010038#include "py/mphal.h"
Jim Mussaredeb51ca42023-02-01 14:20:45 +110039#include "extmod/modnetwork.h"
Damien Georgeea186de2021-09-22 00:35:46 +100040#include "modnetwork.h"
41
42#include "esp_wifi.h"
43#include "esp_log.h"
Angus Gratton960eef72023-11-14 16:46:55 +110044#include "esp_psram.h"
Damien Georgeea186de2021-09-22 00:35:46 +100045
Carlosgg1f355762023-06-25 22:48:39 +010046#ifndef NO_QSTR
47#include "mdns.h"
48#endif
49
Damien Georgeea186de2021-09-22 00:35:46 +100050#if MICROPY_PY_NETWORK_WLAN
51
52#if (WIFI_MODE_STA & WIFI_MODE_AP != WIFI_MODE_NULL || WIFI_MODE_STA | WIFI_MODE_AP != WIFI_MODE_APSTA)
53#error WIFI_MODE_STA and WIFI_MODE_AP are supposed to be bitfields!
54#endif
55
Damien Georgee4650122023-05-09 09:52:54 +100056typedef base_if_obj_t wlan_if_obj_t;
57
Angus Grattondecf8e62024-02-27 15:32:29 +110058static wlan_if_obj_t wlan_sta_obj;
59static wlan_if_obj_t wlan_ap_obj;
Damien Georgeea186de2021-09-22 00:35:46 +100060
61// Set to "true" if esp_wifi_start() was called
62static bool wifi_started = false;
63
64// Set to "true" if the STA interface is requested to be connected by the
65// user, used for automatic reassociation.
66static bool wifi_sta_connect_requested = false;
67
68// Set to "true" if the STA interface is connected to wifi and has IP address.
69static bool wifi_sta_connected = false;
70
71// Store the current status. 0 means None here, safe to do so as first enum value is WIFI_REASON_UNSPECIFIED=1.
72static uint8_t wifi_sta_disconn_reason = 0;
73
74#if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER
75// Whether mDNS has been initialised or not
76static bool mdns_initialised = false;
77#endif
78
79static uint8_t conf_wifi_sta_reconnects = 0;
Glenn Moloneydbced752023-07-01 13:54:44 +100080static uint8_t wifi_sta_reconnects;
Damien Georgeea186de2021-09-22 00:35:46 +100081
82// This function is called by the system-event task and so runs in a different
83// thread to the main MicroPython task. It must not raise any Python exceptions.
Damien Georgee4650122023-05-09 09:52:54 +100084static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
85 switch (event_id) {
86 case WIFI_EVENT_STA_START:
Damien Georgeea186de2021-09-22 00:35:46 +100087 ESP_LOGI("wifi", "STA_START");
Glenn Moloneydbced752023-07-01 13:54:44 +100088 wlan_sta_obj.active = true;
Damien Georgeea186de2021-09-22 00:35:46 +100089 wifi_sta_reconnects = 0;
90 break;
Damien Georgee4650122023-05-09 09:52:54 +100091
Glenn Moloneydbced752023-07-01 13:54:44 +100092 case WIFI_EVENT_STA_STOP:
93 wlan_sta_obj.active = false;
94 break;
95
Damien Georgee4650122023-05-09 09:52:54 +100096 case WIFI_EVENT_STA_CONNECTED:
Damien Georgeea186de2021-09-22 00:35:46 +100097 ESP_LOGI("network", "CONNECTED");
98 break;
Damien Georgee4650122023-05-09 09:52:54 +100099
100 case WIFI_EVENT_STA_DISCONNECTED: {
Damien Georgeea186de2021-09-22 00:35:46 +1000101 // This is a workaround as ESP32 WiFi libs don't currently
102 // auto-reassociate.
Damien Georgee4650122023-05-09 09:52:54 +1000103
104 wifi_event_sta_disconnected_t *disconn = event_data;
Damien Georgeea186de2021-09-22 00:35:46 +1000105 char *message = "";
106 wifi_sta_disconn_reason = disconn->reason;
107 switch (disconn->reason) {
108 case WIFI_REASON_BEACON_TIMEOUT:
109 // AP has dropped out; try to reconnect.
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200110 message = "beacon timeout";
Damien Georgeea186de2021-09-22 00:35:46 +1000111 break;
112 case WIFI_REASON_NO_AP_FOUND:
113 // AP may not exist, or it may have momentarily dropped out; try to reconnect.
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200114 message = "no AP found";
Damien Georgeea186de2021-09-22 00:35:46 +1000115 break;
Daniël van de Giessend6176c12024-02-29 14:05:01 +0100116 case WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD:
117 // No AP with RSSI within given threshold exists, or it may have momentarily dropped out; try to reconnect.
118 message = "no AP with RSSI within threshold found";
119 break;
120 case WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD:
121 // No AP with authmode within given threshold exists, or it may have momentarily dropped out; try to reconnect.
122 message = "no AP with authmode within threshold found";
123 break;
124 case WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY:
125 // No AP with compatible security exists, or it may have momentarily dropped out; try to reconnect.
126 message = "no AP with compatible security found";
127 break;
Damien Georgeea186de2021-09-22 00:35:46 +1000128 case WIFI_REASON_AUTH_FAIL:
129 // Password may be wrong, or it just failed to connect; try to reconnect.
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200130 message = "authentication failed";
Damien Georgeea186de2021-09-22 00:35:46 +1000131 break;
132 default:
133 // Let other errors through and try to reconnect.
134 break;
135 }
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200136 ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d:%s", disconn->reason, message);
Damien Georgeea186de2021-09-22 00:35:46 +1000137
138 wifi_sta_connected = false;
139 if (wifi_sta_connect_requested) {
140 wifi_mode_t mode;
141 if (esp_wifi_get_mode(&mode) != ESP_OK) {
142 break;
143 }
144 if (!(mode & WIFI_MODE_STA)) {
145 break;
146 }
147 if (conf_wifi_sta_reconnects) {
148 ESP_LOGI("wifi", "reconnect counter=%d, max=%d",
149 wifi_sta_reconnects, conf_wifi_sta_reconnects);
150 if (++wifi_sta_reconnects >= conf_wifi_sta_reconnects) {
151 break;
152 }
153 }
154 esp_err_t e = esp_wifi_connect();
155 if (e != ESP_OK) {
156 ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
157 }
158 }
159 break;
160 }
Glenn Moloneydbced752023-07-01 13:54:44 +1000161
162 case WIFI_EVENT_AP_START:
163 wlan_ap_obj.active = true;
164 break;
165
166 case WIFI_EVENT_AP_STOP:
167 wlan_ap_obj.active = false;
168 break;
169
Damien Georgeea186de2021-09-22 00:35:46 +1000170 default:
171 break;
172 }
173}
174
Damien Georgee4650122023-05-09 09:52:54 +1000175static void network_wlan_ip_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
176 switch (event_id) {
177 case IP_EVENT_STA_GOT_IP:
178 ESP_LOGI("network", "GOT_IP");
179 wifi_sta_connected = true;
180 wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway)
181 #if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER
182 if (!mdns_initialised) {
183 mdns_init();
184 #if MICROPY_HW_ENABLE_MDNS_RESPONDER
Jim Mussared65a3ce32023-10-03 13:32:48 +1100185 mdns_hostname_set(mod_network_hostname_data);
186 mdns_instance_name_set(mod_network_hostname_data);
Damien Georgee4650122023-05-09 09:52:54 +1000187 #endif
188 mdns_initialised = true;
189 }
190 #endif
191 break;
192
193 default:
194 break;
Damien Georgeea186de2021-09-22 00:35:46 +1000195 }
196}
197
Angus Grattondecf8e62024-02-27 15:32:29 +1100198static void require_if(mp_obj_t wlan_if, int if_no) {
Damien Georgee4650122023-05-09 09:52:54 +1000199 wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if);
200 if (self->if_id != if_no) {
201 mp_raise_msg(&mp_type_OSError, if_no == ESP_IF_WIFI_STA ? MP_ERROR_TEXT("STA required") : MP_ERROR_TEXT("AP required"));
202 }
203}
204
205void esp_initialise_wifi(void) {
Glenn Moloney7fa322a2020-09-24 15:37:04 +1000206 static int wifi_initialized = 0;
207 if (!wifi_initialized) {
Damien Georgee4650122023-05-09 09:52:54 +1000208 esp_exceptions(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, network_wlan_wifi_event_handler, NULL, NULL));
209 esp_exceptions(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, network_wlan_ip_event_handler, NULL, NULL));
210
211 wlan_sta_obj.base.type = &esp_network_wlan_type;
212 wlan_sta_obj.if_id = ESP_IF_WIFI_STA;
213 wlan_sta_obj.netif = esp_netif_create_default_wifi_sta();
Glenn Moloneydbced752023-07-01 13:54:44 +1000214 wlan_sta_obj.active = false;
Damien Georgee4650122023-05-09 09:52:54 +1000215
216 wlan_ap_obj.base.type = &esp_network_wlan_type;
217 wlan_ap_obj.if_id = ESP_IF_WIFI_AP;
218 wlan_ap_obj.netif = esp_netif_create_default_wifi_ap();
Glenn Moloneydbced752023-07-01 13:54:44 +1000219 wlan_ap_obj.active = false;
Damien Georgee4650122023-05-09 09:52:54 +1000220
Damien Georgeea186de2021-09-22 00:35:46 +1000221 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
Angus Gratton960eef72023-11-14 16:46:55 +1100222 #if CONFIG_SPIRAM_IGNORE_NOTFOUND
223 if (!esp_psram_is_initialized()) {
224 // If PSRAM failed to initialize, disable "Wi-Fi Cache TX Buffers"
225 // (default SPIRAM config ESP32_WIFI_CACHE_TX_BUFFER_NUM==32, this is 54,400 bytes of heap)
226 cfg.cache_tx_buf_num = 0;
227 cfg.feature_caps &= ~CONFIG_FEATURE_CACHE_TX_BUF_BIT;
228
229 // Set some other options back to the non-SPIRAM default values
230 // to save more RAM.
231 //
232 // These can be determined from ESP-IDF components/esp_wifi/Kconfig and the
233 // WIFI_INIT_CONFIG_DEFAULT macro
234 cfg.tx_buf_type = 1; // Dynamic, this "magic number" is defined in IDF KConfig
235 cfg.static_tx_buf_num = 0; // Probably don't need, due to tx_buf_type
236 cfg.dynamic_tx_buf_num = 32; // ESP-IDF default value (maximum)
237 }
238 #endif
Damien Georgeea186de2021-09-22 00:35:46 +1000239 ESP_LOGD("modnetwork", "Initializing WiFi");
240 esp_exceptions(esp_wifi_init(&cfg));
241 esp_exceptions(esp_wifi_set_storage(WIFI_STORAGE_RAM));
Damien Georgee4650122023-05-09 09:52:54 +1000242
Damien Georgeea186de2021-09-22 00:35:46 +1000243 ESP_LOGD("modnetwork", "Initialized");
Glenn Moloney7fa322a2020-09-24 15:37:04 +1000244 wifi_initialized = 1;
Damien Georgeea186de2021-09-22 00:35:46 +1000245 }
Glenn Moloney7fa322a2020-09-24 15:37:04 +1000246}
247
Angus Grattondecf8e62024-02-27 15:32:29 +1100248static mp_obj_t network_wlan_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George53cb0732023-05-11 11:51:02 +1000249 mp_arg_check_num(n_args, n_kw, 0, 1, false);
250
Glenn Moloney7fa322a2020-09-24 15:37:04 +1000251 esp_initialise_wifi();
Damien Georgeea186de2021-09-22 00:35:46 +1000252
Damien Georgee4650122023-05-09 09:52:54 +1000253 int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : ESP_IF_WIFI_STA;
254 if (idx == ESP_IF_WIFI_STA) {
Damien Georgeea186de2021-09-22 00:35:46 +1000255 return MP_OBJ_FROM_PTR(&wlan_sta_obj);
Damien Georgee4650122023-05-09 09:52:54 +1000256 } else if (idx == ESP_IF_WIFI_AP) {
Damien Georgeea186de2021-09-22 00:35:46 +1000257 return MP_OBJ_FROM_PTR(&wlan_ap_obj);
258 } else {
259 mp_raise_ValueError(MP_ERROR_TEXT("invalid WLAN interface identifier"));
260 }
261}
Damien Georgeea186de2021-09-22 00:35:46 +1000262
Angus Grattondecf8e62024-02-27 15:32:29 +1100263static mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) {
Damien Georgeea186de2021-09-22 00:35:46 +1000264 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
265
266 wifi_mode_t mode;
267 if (!wifi_started) {
268 mode = WIFI_MODE_NULL;
269 } else {
270 esp_exceptions(esp_wifi_get_mode(&mode));
271 }
272
Damien Georgee4650122023-05-09 09:52:54 +1000273 int bit = (self->if_id == ESP_IF_WIFI_STA) ? WIFI_MODE_STA : WIFI_MODE_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000274
275 if (n_args > 1) {
276 bool active = mp_obj_is_true(args[1]);
277 mode = active ? (mode | bit) : (mode & ~bit);
278 if (mode == WIFI_MODE_NULL) {
279 if (wifi_started) {
280 esp_exceptions(esp_wifi_stop());
281 wifi_started = false;
282 }
283 } else {
284 esp_exceptions(esp_wifi_set_mode(mode));
285 if (!wifi_started) {
286 esp_exceptions(esp_wifi_start());
287 wifi_started = true;
288 }
289 }
Glenn Moloneydbced752023-07-01 13:54:44 +1000290
291 // Wait for the interface to be in the correct state.
292 while (self->active != active) {
293 MICROPY_EVENT_POLL_HOOK;
294 }
Damien Georgeea186de2021-09-22 00:35:46 +1000295 }
296
297 return (mode & bit) ? mp_const_true : mp_const_false;
298}
Angus Grattondecf8e62024-02-27 15:32:29 +1100299static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_active_obj, 1, 2, network_wlan_active);
Damien Georgeea186de2021-09-22 00:35:46 +1000300
Angus Grattondecf8e62024-02-27 15:32:29 +1100301static mp_obj_t network_wlan_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
iabdalkadera7c7feb2022-06-04 12:22:55 +0200302 enum { ARG_ssid, ARG_key, ARG_bssid };
Damien Georgeea186de2021-09-22 00:35:46 +1000303 static const mp_arg_t allowed_args[] = {
304 { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} },
305 { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} },
306 { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
307 };
308
309 // parse args
310 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
311 mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
312
313 wifi_config_t wifi_sta_config = {0};
314
315 // configure any parameters that are given
316 if (n_args > 1) {
317 size_t len;
318 const char *p;
319 if (args[ARG_ssid].u_obj != mp_const_none) {
320 p = mp_obj_str_get_data(args[ARG_ssid].u_obj, &len);
321 memcpy(wifi_sta_config.sta.ssid, p, MIN(len, sizeof(wifi_sta_config.sta.ssid)));
322 }
iabdalkadera7c7feb2022-06-04 12:22:55 +0200323 if (args[ARG_key].u_obj != mp_const_none) {
324 p = mp_obj_str_get_data(args[ARG_key].u_obj, &len);
Damien Georgeea186de2021-09-22 00:35:46 +1000325 memcpy(wifi_sta_config.sta.password, p, MIN(len, sizeof(wifi_sta_config.sta.password)));
326 }
327 if (args[ARG_bssid].u_obj != mp_const_none) {
328 p = mp_obj_str_get_data(args[ARG_bssid].u_obj, &len);
329 if (len != sizeof(wifi_sta_config.sta.bssid)) {
330 mp_raise_ValueError(NULL);
331 }
332 wifi_sta_config.sta.bssid_set = 1;
333 memcpy(wifi_sta_config.sta.bssid, p, sizeof(wifi_sta_config.sta.bssid));
334 }
335 esp_exceptions(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
336 }
337
Jim Mussared65a3ce32023-10-03 13:32:48 +1100338 esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname_data));
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100339
Damien Georgeea186de2021-09-22 00:35:46 +1000340 wifi_sta_reconnects = 0;
341 // connect to the WiFi AP
342 MP_THREAD_GIL_EXIT();
343 esp_exceptions(esp_wifi_connect());
344 MP_THREAD_GIL_ENTER();
345 wifi_sta_connect_requested = true;
346
347 return mp_const_none;
348}
Angus Grattondecf8e62024-02-27 15:32:29 +1100349static MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_connect_obj, 1, network_wlan_connect);
Damien Georgeea186de2021-09-22 00:35:46 +1000350
Angus Grattondecf8e62024-02-27 15:32:29 +1100351static mp_obj_t network_wlan_disconnect(mp_obj_t self_in) {
Damien Georgeea186de2021-09-22 00:35:46 +1000352 wifi_sta_connect_requested = false;
353 esp_exceptions(esp_wifi_disconnect());
354 return mp_const_none;
355}
Angus Grattondecf8e62024-02-27 15:32:29 +1100356static MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_disconnect_obj, network_wlan_disconnect);
Damien Georgeea186de2021-09-22 00:35:46 +1000357
Angus Grattondecf8e62024-02-27 15:32:29 +1100358static mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) {
Damien Georgeea186de2021-09-22 00:35:46 +1000359 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
360 if (n_args == 1) {
Damien Georgee4650122023-05-09 09:52:54 +1000361 if (self->if_id == ESP_IF_WIFI_STA) {
Damien Georgeea186de2021-09-22 00:35:46 +1000362 // Case of no arg is only for the STA interface
363 if (wifi_sta_connected) {
364 // Happy path, connected with IP
365 return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200366 } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND) {
367 return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND);
Daniël van de Giessend6176c12024-02-29 14:05:01 +0100368 } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD) {
369 return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD);
370 } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD) {
371 return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD);
372 } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY) {
373 return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY);
Ihor Nehrutsad6154922023-11-09 16:18:39 +0200374 } else if ((wifi_sta_disconn_reason == WIFI_REASON_AUTH_FAIL) || (wifi_sta_disconn_reason == WIFI_REASON_CONNECTION_FAIL)) {
375 // wrong password
376 return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_AUTH_FAIL);
377 } else if (wifi_sta_disconn_reason == WIFI_REASON_ASSOC_LEAVE) {
378 // After wlan.disconnect()
379 return MP_OBJ_NEW_SMALL_INT(STAT_IDLE);
Damien Georgeea186de2021-09-22 00:35:46 +1000380 } else if (wifi_sta_connect_requested
381 && (conf_wifi_sta_reconnects == 0
382 || wifi_sta_reconnects < conf_wifi_sta_reconnects)) {
383 // No connection or error, but is requested = Still connecting
384 return MP_OBJ_NEW_SMALL_INT(STAT_CONNECTING);
385 } else if (wifi_sta_disconn_reason == 0) {
386 // No activity, No error = Idle
387 return MP_OBJ_NEW_SMALL_INT(STAT_IDLE);
388 } else {
389 // Simply pass the error through from ESP-identifier
390 return MP_OBJ_NEW_SMALL_INT(wifi_sta_disconn_reason);
391 }
392 }
393 return mp_const_none;
394 }
395
396 // one argument: return status based on query parameter
397 switch ((uintptr_t)args[1]) {
398 case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_stations): {
399 // return list of connected stations, only if in soft-AP mode
Damien Georgee4650122023-05-09 09:52:54 +1000400 require_if(args[0], ESP_IF_WIFI_AP);
Damien Georgeea186de2021-09-22 00:35:46 +1000401 wifi_sta_list_t station_list;
402 esp_exceptions(esp_wifi_ap_get_sta_list(&station_list));
403 wifi_sta_info_t *stations = (wifi_sta_info_t *)station_list.sta;
404 mp_obj_t list = mp_obj_new_list(0, NULL);
405 for (int i = 0; i < station_list.num; ++i) {
406 mp_obj_tuple_t *t = mp_obj_new_tuple(1, NULL);
407 t->items[0] = mp_obj_new_bytes(stations[i].mac, sizeof(stations[i].mac));
408 mp_obj_list_append(list, t);
409 }
410 return list;
411 }
412 case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_rssi): {
413 // return signal of AP, only in STA mode
Damien Georgee4650122023-05-09 09:52:54 +1000414 require_if(args[0], ESP_IF_WIFI_STA);
Damien Georgeea186de2021-09-22 00:35:46 +1000415
416 wifi_ap_record_t info;
417 esp_exceptions(esp_wifi_sta_get_ap_info(&info));
418 return MP_OBJ_NEW_SMALL_INT(info.rssi);
419 }
420 default:
421 mp_raise_ValueError(MP_ERROR_TEXT("unknown status param"));
422 }
423
424 return mp_const_none;
425}
Angus Grattondecf8e62024-02-27 15:32:29 +1100426static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_status_obj, 1, 2, network_wlan_status);
Damien Georgeea186de2021-09-22 00:35:46 +1000427
Angus Grattondecf8e62024-02-27 15:32:29 +1100428static mp_obj_t network_wlan_scan(mp_obj_t self_in) {
Damien Georgeea186de2021-09-22 00:35:46 +1000429 // check that STA mode is active
430 wifi_mode_t mode;
431 esp_exceptions(esp_wifi_get_mode(&mode));
432 if ((mode & WIFI_MODE_STA) == 0) {
433 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("STA must be active"));
434 }
435
436 mp_obj_t list = mp_obj_new_list(0, NULL);
437 wifi_scan_config_t config = { 0 };
438 config.show_hidden = true;
439 MP_THREAD_GIL_EXIT();
440 esp_err_t status = esp_wifi_scan_start(&config, 1);
441 MP_THREAD_GIL_ENTER();
442 if (status == 0) {
443 uint16_t count = 0;
444 esp_exceptions(esp_wifi_scan_get_ap_num(&count));
Damien Georgeccaf1972022-06-28 13:15:10 +1000445 if (count == 0) {
446 // esp_wifi_scan_get_ap_records must be called to free internal buffers from the scan.
447 // But it returns an error if wifi_ap_records==NULL. So allocate at least 1 AP entry.
448 // esp_wifi_scan_get_ap_records will then return the actual number of APs in count.
449 count = 1;
450 }
Damien Georgeea186de2021-09-22 00:35:46 +1000451 wifi_ap_record_t *wifi_ap_records = calloc(count, sizeof(wifi_ap_record_t));
452 esp_exceptions(esp_wifi_scan_get_ap_records(&count, wifi_ap_records));
453 for (uint16_t i = 0; i < count; i++) {
454 mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
455 uint8_t *x = memchr(wifi_ap_records[i].ssid, 0, sizeof(wifi_ap_records[i].ssid));
456 int ssid_len = x ? x - wifi_ap_records[i].ssid : sizeof(wifi_ap_records[i].ssid);
457 t->items[0] = mp_obj_new_bytes(wifi_ap_records[i].ssid, ssid_len);
458 t->items[1] = mp_obj_new_bytes(wifi_ap_records[i].bssid, sizeof(wifi_ap_records[i].bssid));
459 t->items[2] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].primary);
460 t->items[3] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].rssi);
461 t->items[4] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].authmode);
462 t->items[5] = mp_const_false; // XXX hidden?
463 mp_obj_list_append(list, MP_OBJ_FROM_PTR(t));
464 }
465 free(wifi_ap_records);
466 }
467 return list;
468}
Angus Grattondecf8e62024-02-27 15:32:29 +1100469static MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_scan_obj, network_wlan_scan);
Damien Georgeea186de2021-09-22 00:35:46 +1000470
Angus Grattondecf8e62024-02-27 15:32:29 +1100471static mp_obj_t network_wlan_isconnected(mp_obj_t self_in) {
Damien Georgeea186de2021-09-22 00:35:46 +1000472 wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgee4650122023-05-09 09:52:54 +1000473 if (self->if_id == ESP_IF_WIFI_STA) {
Damien Georgeea186de2021-09-22 00:35:46 +1000474 return mp_obj_new_bool(wifi_sta_connected);
475 } else {
476 wifi_sta_list_t sta;
477 esp_wifi_ap_get_sta_list(&sta);
478 return mp_obj_new_bool(sta.num != 0);
479 }
480}
Angus Grattondecf8e62024-02-27 15:32:29 +1100481static MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_isconnected_obj, network_wlan_isconnected);
Damien Georgeea186de2021-09-22 00:35:46 +1000482
Angus Grattondecf8e62024-02-27 15:32:29 +1100483static mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Damien Georgeea186de2021-09-22 00:35:46 +1000484 if (n_args != 1 && kwargs->used != 0) {
485 mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
486 }
487
488 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
489
Damien Georgee4650122023-05-09 09:52:54 +1000490 bool is_wifi = self->if_id == ESP_IF_WIFI_AP || self->if_id == ESP_IF_WIFI_STA;
Damien Georgeea186de2021-09-22 00:35:46 +1000491
492 wifi_config_t cfg;
493 if (is_wifi) {
494 esp_exceptions(esp_wifi_get_config(self->if_id, &cfg));
495 }
496
Damien Georgeea186de2021-09-22 00:35:46 +1000497 if (kwargs->used != 0) {
498 if (!is_wifi) {
499 goto unknown;
500 }
501
502 for (size_t i = 0; i < kwargs->alloc; i++) {
503 if (mp_map_slot_is_filled(kwargs, i)) {
504 int req_if = -1;
505
Damien George5adb1fa2021-12-10 23:28:46 +1100506 switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
507 case MP_QSTR_mac: {
Damien Georgeea186de2021-09-22 00:35:46 +1000508 mp_buffer_info_t bufinfo;
509 mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
510 if (bufinfo.len != 6) {
511 mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
512 }
513 esp_exceptions(esp_wifi_set_mac(self->if_id, bufinfo.buf));
514 break;
515 }
iabdalkadera7c7feb2022-06-04 12:22:55 +0200516 case MP_QSTR_ssid:
Damien George5adb1fa2021-12-10 23:28:46 +1100517 case MP_QSTR_essid: {
Damien Georgee4650122023-05-09 09:52:54 +1000518 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000519 size_t len;
520 const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
521 len = MIN(len, sizeof(cfg.ap.ssid));
522 memcpy(cfg.ap.ssid, s, len);
523 cfg.ap.ssid_len = len;
524 break;
525 }
Damien George5adb1fa2021-12-10 23:28:46 +1100526 case MP_QSTR_hidden: {
Damien Georgee4650122023-05-09 09:52:54 +1000527 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000528 cfg.ap.ssid_hidden = mp_obj_is_true(kwargs->table[i].value);
529 break;
530 }
iabdalkadera7c7feb2022-06-04 12:22:55 +0200531 case MP_QSTR_security:
Damien George5adb1fa2021-12-10 23:28:46 +1100532 case MP_QSTR_authmode: {
Damien Georgee4650122023-05-09 09:52:54 +1000533 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000534 cfg.ap.authmode = mp_obj_get_int(kwargs->table[i].value);
535 break;
536 }
iabdalkadera7c7feb2022-06-04 12:22:55 +0200537 case MP_QSTR_key:
Damien George5adb1fa2021-12-10 23:28:46 +1100538 case MP_QSTR_password: {
Damien Georgee4650122023-05-09 09:52:54 +1000539 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000540 size_t len;
541 const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
542 len = MIN(len, sizeof(cfg.ap.password) - 1);
543 memcpy(cfg.ap.password, s, len);
544 cfg.ap.password[len] = 0;
545 break;
546 }
Damien George5adb1fa2021-12-10 23:28:46 +1100547 case MP_QSTR_channel: {
Angus Gratton951a10e2024-11-13 10:16:25 +1100548 uint8_t channel = mp_obj_get_int(kwargs->table[i].value);
549 if (self->if_id == ESP_IF_WIFI_AP) {
550 cfg.ap.channel = channel;
551 } else {
552 // This setting is only used to determine the
553 // starting channel for a scan, so it can result in
554 // slightly faster connection times.
555 cfg.sta.channel = channel;
556
557 // This additional code to directly set the channel
558 // on the STA interface is only relevant for ESP-NOW
559 // (when there is no STA connection attempt.)
560 uint8_t old_primary;
561 wifi_second_chan_t secondary;
562 // Get the current value of secondary
563 esp_exceptions(esp_wifi_get_channel(&old_primary, &secondary));
564 esp_err_t err = esp_wifi_set_channel(channel, secondary);
565 if (err == ESP_ERR_INVALID_ARG) {
566 // May need to swap secondary channel above to below or below to above
567 secondary = (
568 (secondary == WIFI_SECOND_CHAN_ABOVE)
569 ? WIFI_SECOND_CHAN_BELOW
570 : (secondary == WIFI_SECOND_CHAN_BELOW)
glenn2098d1c502022-07-31 16:36:10 +1000571 ? WIFI_SECOND_CHAN_ABOVE
572 : WIFI_SECOND_CHAN_NONE);
Angus Gratton951a10e2024-11-13 10:16:25 +1100573 err = esp_wifi_set_channel(channel, secondary);
574 }
575 esp_exceptions(err);
576 if (channel != old_primary) {
577 // Workaround the ESP-IDF Wi-Fi stack sometimes taking a moment to change channels
578 mp_hal_delay_ms(1);
579 }
glenn2098d1c502022-07-31 16:36:10 +1000580 }
Damien Georgeea186de2021-09-22 00:35:46 +1000581 break;
582 }
IhorNehrutsa1ea82b62022-06-28 16:38:45 +0300583 case MP_QSTR_hostname:
Damien George5adb1fa2021-12-10 23:28:46 +1100584 case MP_QSTR_dhcp_hostname: {
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100585 // TODO: Deprecated. Use network.hostname(name) instead.
Jim Mussared65a3ce32023-10-03 13:32:48 +1100586 mod_network_hostname(1, &kwargs->table[i].value);
Damien Georgeea186de2021-09-22 00:35:46 +1000587 break;
588 }
Damien George5adb1fa2021-12-10 23:28:46 +1100589 case MP_QSTR_max_clients: {
Damien Georgee4650122023-05-09 09:52:54 +1000590 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000591 cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value);
592 break;
593 }
Damien George5adb1fa2021-12-10 23:28:46 +1100594 case MP_QSTR_reconnects: {
Damien Georgeea186de2021-09-22 00:35:46 +1000595 int reconnects = mp_obj_get_int(kwargs->table[i].value);
Damien Georgee4650122023-05-09 09:52:54 +1000596 req_if = ESP_IF_WIFI_STA;
Damien Georgeea186de2021-09-22 00:35:46 +1000597 // parameter reconnects == -1 means to retry forever.
598 // here means conf_wifi_sta_reconnects == 0 to retry forever.
599 conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1;
600 break;
601 }
wemosff28d2e2022-04-11 20:12:57 +0800602 case MP_QSTR_txpower: {
603 int8_t power = (mp_obj_get_float(kwargs->table[i].value) * 4);
604 esp_exceptions(esp_wifi_set_max_tx_power(power));
605 break;
606 }
glenn2076f2e3e2022-07-31 18:17:40 +1000607 case MP_QSTR_protocol: {
608 esp_exceptions(esp_wifi_set_protocol(self->if_id, mp_obj_get_int(kwargs->table[i].value)));
609 break;
610 }
glenn201093dea2022-08-25 16:55:57 +1000611 case MP_QSTR_pm: {
612 esp_exceptions(esp_wifi_set_ps(mp_obj_get_int(kwargs->table[i].value)));
613 break;
614 }
Damien Georgeea186de2021-09-22 00:35:46 +1000615 default:
616 goto unknown;
617 }
618
619 // We post-check interface requirements to save on code size
620 if (req_if >= 0) {
621 require_if(args[0], req_if);
622 }
623 }
624 }
625
626 esp_exceptions(esp_wifi_set_config(self->if_id, &cfg));
627
628 return mp_const_none;
629 }
630
631 // Get config
632
633 if (n_args != 2) {
634 mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
635 }
636
637 int req_if = -1;
638 mp_obj_t val = mp_const_none;
639
Damien George5adb1fa2021-12-10 23:28:46 +1100640 switch (mp_obj_str_get_qstr(args[1])) {
641 case MP_QSTR_mac: {
Damien Georgeea186de2021-09-22 00:35:46 +1000642 uint8_t mac[6];
643 switch (self->if_id) {
Damien Georgee4650122023-05-09 09:52:54 +1000644 case ESP_IF_WIFI_AP: // fallthrough intentional
645 case ESP_IF_WIFI_STA:
Damien Georgeea186de2021-09-22 00:35:46 +1000646 esp_exceptions(esp_wifi_get_mac(self->if_id, mac));
647 return mp_obj_new_bytes(mac, sizeof(mac));
648 default:
649 goto unknown;
650 }
651 }
iabdalkadera7c7feb2022-06-04 12:22:55 +0200652 case MP_QSTR_ssid:
Damien George5adb1fa2021-12-10 23:28:46 +1100653 case MP_QSTR_essid:
Damien Georgeea186de2021-09-22 00:35:46 +1000654 switch (self->if_id) {
Damien Georgee4650122023-05-09 09:52:54 +1000655 case ESP_IF_WIFI_STA:
Jon Foster92484d82024-04-01 19:23:49 +0100656 val = mp_obj_new_str_from_cstr((char *)cfg.sta.ssid);
Damien Georgeea186de2021-09-22 00:35:46 +1000657 break;
Damien Georgee4650122023-05-09 09:52:54 +1000658 case ESP_IF_WIFI_AP:
Damien Georgeea186de2021-09-22 00:35:46 +1000659 val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len);
660 break;
661 default:
Damien Georgee4650122023-05-09 09:52:54 +1000662 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000663 }
664 break;
Damien George5adb1fa2021-12-10 23:28:46 +1100665 case MP_QSTR_hidden:
Damien Georgee4650122023-05-09 09:52:54 +1000666 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000667 val = mp_obj_new_bool(cfg.ap.ssid_hidden);
668 break;
iabdalkadera7c7feb2022-06-04 12:22:55 +0200669 case MP_QSTR_security:
Damien George5adb1fa2021-12-10 23:28:46 +1100670 case MP_QSTR_authmode:
Damien Georgee4650122023-05-09 09:52:54 +1000671 req_if = ESP_IF_WIFI_AP;
Damien Georgeea186de2021-09-22 00:35:46 +1000672 val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode);
673 break;
glenn2098d1c502022-07-31 16:36:10 +1000674 case MP_QSTR_channel: {
675 uint8_t channel;
676 wifi_second_chan_t second;
677 esp_exceptions(esp_wifi_get_channel(&channel, &second));
678 val = MP_OBJ_NEW_SMALL_INT(channel);
Damien Georgeea186de2021-09-22 00:35:46 +1000679 break;
glenn2098d1c502022-07-31 16:36:10 +1000680 }
Daniël van de Giessenba8aad32023-07-04 15:36:37 +0200681 case MP_QSTR_ifname: {
682 val = esp_ifname(self->netif);
683 break;
684 }
IhorNehrutsa1ea82b62022-06-28 16:38:45 +0300685 case MP_QSTR_hostname:
Damien George5adb1fa2021-12-10 23:28:46 +1100686 case MP_QSTR_dhcp_hostname: {
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100687 // TODO: Deprecated. Use network.hostname() instead.
Damien Georgee4650122023-05-09 09:52:54 +1000688 req_if = ESP_IF_WIFI_STA;
Jim Mussared65a3ce32023-10-03 13:32:48 +1100689 val = mod_network_hostname(0, NULL);
Damien Georgeea186de2021-09-22 00:35:46 +1000690 break;
691 }
Damien George5adb1fa2021-12-10 23:28:46 +1100692 case MP_QSTR_max_clients: {
Damien Georgeea186de2021-09-22 00:35:46 +1000693 val = MP_OBJ_NEW_SMALL_INT(cfg.ap.max_connection);
694 break;
695 }
Damien George5adb1fa2021-12-10 23:28:46 +1100696 case MP_QSTR_reconnects:
Damien Georgee4650122023-05-09 09:52:54 +1000697 req_if = ESP_IF_WIFI_STA;
Damien Georgeea186de2021-09-22 00:35:46 +1000698 int rec = conf_wifi_sta_reconnects - 1;
699 val = MP_OBJ_NEW_SMALL_INT(rec);
700 break;
wemosff28d2e2022-04-11 20:12:57 +0800701 case MP_QSTR_txpower: {
702 int8_t power;
703 esp_exceptions(esp_wifi_get_max_tx_power(&power));
704 val = mp_obj_new_float(power * 0.25);
705 break;
706 }
glenn2076f2e3e2022-07-31 18:17:40 +1000707 case MP_QSTR_protocol: {
708 uint8_t protocol_bitmap;
709 esp_exceptions(esp_wifi_get_protocol(self->if_id, &protocol_bitmap));
710 val = MP_OBJ_NEW_SMALL_INT(protocol_bitmap);
711 break;
712 }
glenn201093dea2022-08-25 16:55:57 +1000713 case MP_QSTR_pm: {
714 wifi_ps_type_t ps_type;
715 esp_exceptions(esp_wifi_get_ps(&ps_type));
716 val = MP_OBJ_NEW_SMALL_INT(ps_type);
717 break;
718 }
Damien Georgeea186de2021-09-22 00:35:46 +1000719 default:
720 goto unknown;
721 }
722
Damien Georgeea186de2021-09-22 00:35:46 +1000723 // We post-check interface requirements to save on code size
724 if (req_if >= 0) {
725 require_if(args[0], req_if);
726 }
727
728 return val;
729
730unknown:
731 mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
732}
733MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_config_obj, 1, network_wlan_config);
734
Angus Grattondecf8e62024-02-27 15:32:29 +1100735static const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
Damien Georgeea186de2021-09-22 00:35:46 +1000736 { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_wlan_active_obj) },
737 { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_wlan_connect_obj) },
738 { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_wlan_disconnect_obj) },
739 { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_wlan_status_obj) },
740 { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&network_wlan_scan_obj) },
741 { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) },
742 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) },
Jim Mussaredeb51ca42023-02-01 14:20:45 +1100743 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
Felix Dörre1f23ab12024-03-27 21:53:34 +0000744 { MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
glenn201093dea2022-08-25 16:55:57 +1000745
746 // Constants
iabdalkadere8dd5192024-03-14 14:39:53 +0100747 { MP_ROM_QSTR(MP_QSTR_IF_STA), MP_ROM_INT(WIFI_IF_STA)},
748 { MP_ROM_QSTR(MP_QSTR_IF_AP), MP_ROM_INT(WIFI_IF_AP)},
749
750 { MP_ROM_QSTR(MP_QSTR_SEC_OPEN), MP_ROM_INT(WIFI_AUTH_OPEN) },
751 { MP_ROM_QSTR(MP_QSTR_SEC_WEP), MP_ROM_INT(WIFI_AUTH_WEP) },
752 { MP_ROM_QSTR(MP_QSTR_SEC_WPA), MP_ROM_INT(WIFI_AUTH_WPA_PSK) },
753 { MP_ROM_QSTR(MP_QSTR_SEC_WPA2), MP_ROM_INT(WIFI_AUTH_WPA2_PSK) },
754 { MP_ROM_QSTR(MP_QSTR_SEC_WPA_WPA2), MP_ROM_INT(WIFI_AUTH_WPA_WPA2_PSK) },
755 { MP_ROM_QSTR(MP_QSTR_SEC_WPA2_ENT), MP_ROM_INT(WIFI_AUTH_WPA2_ENTERPRISE) },
756 { MP_ROM_QSTR(MP_QSTR_SEC_WPA3), MP_ROM_INT(WIFI_AUTH_WPA3_PSK) },
757 { MP_ROM_QSTR(MP_QSTR_SEC_WPA2_WPA3), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_PSK) },
758 { MP_ROM_QSTR(MP_QSTR_SEC_WAPI), MP_ROM_INT(WIFI_AUTH_WAPI_PSK) },
759 { MP_ROM_QSTR(MP_QSTR_SEC_OWE), MP_ROM_INT(WIFI_AUTH_OWE) },
Damien George275b3af2024-10-31 11:10:46 +1100760 { MP_ROM_QSTR(MP_QSTR_SEC_WPA3_ENT_192), MP_ROM_INT(WIFI_AUTH_WPA3_ENT_192) },
Damien George275b3af2024-10-31 11:10:46 +1100761 { MP_ROM_QSTR(MP_QSTR_SEC_WPA3_EXT_PSK), MP_ROM_INT(WIFI_AUTH_WPA3_EXT_PSK) },
762 { MP_ROM_QSTR(MP_QSTR_SEC_WPA3_EXT_PSK_MIXED_MODE), MP_ROM_INT(WIFI_AUTH_WPA3_EXT_PSK_MIXED_MODE) },
Andrew Leech5564f302024-08-28 09:21:34 +1000763 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
764 { MP_ROM_QSTR(MP_QSTR_SEC_DPP), MP_ROM_INT(WIFI_AUTH_DPP) },
765 #endif
IhorNehrutsa865a4c82025-01-21 08:48:25 +0200766 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
767 { MP_ROM_QSTR(MP_QSTR_SEC_WPA3_ENT), MP_ROM_INT(WIFI_AUTH_WPA3_ENTERPRISE) },
768 { MP_ROM_QSTR(MP_QSTR_SEC_WPA2_WPA3_ENT), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_ENTERPRISE) },
769 #endif
iabdalkadere8dd5192024-03-14 14:39:53 +0100770
glenn201093dea2022-08-25 16:55:57 +1000771 { MP_ROM_QSTR(MP_QSTR_PM_NONE), MP_ROM_INT(WIFI_PS_NONE) },
772 { MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(WIFI_PS_MIN_MODEM) },
773 { MP_ROM_QSTR(MP_QSTR_PM_POWERSAVE), MP_ROM_INT(WIFI_PS_MAX_MODEM) },
Damien Georgeea186de2021-09-22 00:35:46 +1000774};
Angus Grattondecf8e62024-02-27 15:32:29 +1100775static MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table);
Damien Georgeea186de2021-09-22 00:35:46 +1000776
IhorNehrutsa865a4c82025-01-21 08:48:25 +0200777#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
778_Static_assert(WIFI_AUTH_MAX == 16, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types_generic.h");
779#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
Andrew Leech5564f302024-08-28 09:21:34 +1000780_Static_assert(WIFI_AUTH_MAX == 14, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types_generic.h");
781#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
Damien George275b3af2024-10-31 11:10:46 +1100782_Static_assert(WIFI_AUTH_MAX == 13, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h");
Damien George275b3af2024-10-31 11:10:46 +1100783#else
Angus Gratton6e5d8d02024-11-01 16:32:48 +1100784#error "Error in macro logic, all supported versions should be covered."
Damien George275b3af2024-10-31 11:10:46 +1100785#endif
786
Jim Mussared662b9762021-07-14 14:38:38 +1000787MP_DEFINE_CONST_OBJ_TYPE(
Damien George53cb0732023-05-11 11:51:02 +1000788 esp_network_wlan_type,
Jim Mussared662b9762021-07-14 14:38:38 +1000789 MP_QSTR_WLAN,
790 MP_TYPE_FLAG_NONE,
Damien George53cb0732023-05-11 11:51:02 +1000791 make_new, network_wlan_make_new,
Jim Mussared9dce8272022-06-24 16:27:46 +1000792 locals_dict, &wlan_if_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000793 );
794
Damien Georgeea186de2021-09-22 00:35:46 +1000795#endif // MICROPY_PY_NETWORK_WLAN