blob: 3af7225440c433fc43a04f5bb82a62bc0d63ccde [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"
38#include "modnetwork.h"
39
40#include "esp_wifi.h"
41#include "esp_log.h"
42#include "mdns.h"
43
44#if MICROPY_PY_NETWORK_WLAN
45
46#if (WIFI_MODE_STA & WIFI_MODE_AP != WIFI_MODE_NULL || WIFI_MODE_STA | WIFI_MODE_AP != WIFI_MODE_APSTA)
47#error WIFI_MODE_STA and WIFI_MODE_AP are supposed to be bitfields!
48#endif
49
50STATIC const mp_obj_type_t wlan_if_type;
51STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA};
52STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP};
53
54// Set to "true" if esp_wifi_start() was called
55static bool wifi_started = false;
56
57// Set to "true" if the STA interface is requested to be connected by the
58// user, used for automatic reassociation.
59static bool wifi_sta_connect_requested = false;
60
61// Set to "true" if the STA interface is connected to wifi and has IP address.
62static bool wifi_sta_connected = false;
63
64// Store the current status. 0 means None here, safe to do so as first enum value is WIFI_REASON_UNSPECIFIED=1.
65static uint8_t wifi_sta_disconn_reason = 0;
66
67#if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER
68// Whether mDNS has been initialised or not
69static bool mdns_initialised = false;
70#endif
71
72static uint8_t conf_wifi_sta_reconnects = 0;
73static uint8_t wifi_sta_reconnects;
74
75// This function is called by the system-event task and so runs in a different
76// thread to the main MicroPython task. It must not raise any Python exceptions.
77void network_wlan_event_handler(system_event_t *event) {
78 switch (event->event_id) {
79 case SYSTEM_EVENT_STA_START:
80 ESP_LOGI("wifi", "STA_START");
81 wifi_sta_reconnects = 0;
82 break;
83 case SYSTEM_EVENT_STA_CONNECTED:
84 ESP_LOGI("network", "CONNECTED");
85 break;
86 case SYSTEM_EVENT_STA_GOT_IP:
87 ESP_LOGI("network", "GOT_IP");
88 wifi_sta_connected = true;
89 wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway)
90 #if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER
91 if (!mdns_initialised) {
92 mdns_init();
93 #if MICROPY_HW_ENABLE_MDNS_RESPONDER
94 const char *hostname = NULL;
95 if (tcpip_adapter_get_hostname(WIFI_IF_STA, &hostname) != ESP_OK || hostname == NULL) {
96 hostname = "esp32";
97 }
98 mdns_hostname_set(hostname);
99 mdns_instance_name_set(hostname);
100 #endif
101 mdns_initialised = true;
102 }
103 #endif
104 break;
105 case SYSTEM_EVENT_STA_DISCONNECTED: {
106 // This is a workaround as ESP32 WiFi libs don't currently
107 // auto-reassociate.
108 system_event_sta_disconnected_t *disconn = &event->event_info.disconnected;
109 char *message = "";
110 wifi_sta_disconn_reason = disconn->reason;
111 switch (disconn->reason) {
112 case WIFI_REASON_BEACON_TIMEOUT:
113 // AP has dropped out; try to reconnect.
114 message = "\nbeacon timeout";
115 break;
116 case WIFI_REASON_NO_AP_FOUND:
117 // AP may not exist, or it may have momentarily dropped out; try to reconnect.
118 message = "\nno AP found";
119 break;
120 case WIFI_REASON_AUTH_FAIL:
121 // Password may be wrong, or it just failed to connect; try to reconnect.
122 message = "\nauthentication failed";
123 break;
124 default:
125 // Let other errors through and try to reconnect.
126 break;
127 }
128 ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d%s", disconn->reason, message);
129
130 wifi_sta_connected = false;
131 if (wifi_sta_connect_requested) {
132 wifi_mode_t mode;
133 if (esp_wifi_get_mode(&mode) != ESP_OK) {
134 break;
135 }
136 if (!(mode & WIFI_MODE_STA)) {
137 break;
138 }
139 if (conf_wifi_sta_reconnects) {
140 ESP_LOGI("wifi", "reconnect counter=%d, max=%d",
141 wifi_sta_reconnects, conf_wifi_sta_reconnects);
142 if (++wifi_sta_reconnects >= conf_wifi_sta_reconnects) {
143 break;
144 }
145 }
146 esp_err_t e = esp_wifi_connect();
147 if (e != ESP_OK) {
148 ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
149 }
150 }
151 break;
152 }
153 default:
154 break;
155 }
156}
157
158STATIC void require_if(mp_obj_t wlan_if, int if_no) {
159 wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if);
160 if (self->if_id != if_no) {
161 mp_raise_msg(&mp_type_OSError, if_no == WIFI_IF_STA ? MP_ERROR_TEXT("STA required") : MP_ERROR_TEXT("AP required"));
162 }
163}
164
165STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
166 static int initialized = 0;
167 if (!initialized) {
168 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
169 ESP_LOGD("modnetwork", "Initializing WiFi");
170 esp_exceptions(esp_wifi_init(&cfg));
171 esp_exceptions(esp_wifi_set_storage(WIFI_STORAGE_RAM));
172 ESP_LOGD("modnetwork", "Initialized");
173 initialized = 1;
174 }
175
176 int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA;
177 if (idx == WIFI_IF_STA) {
178 return MP_OBJ_FROM_PTR(&wlan_sta_obj);
179 } else if (idx == WIFI_IF_AP) {
180 return MP_OBJ_FROM_PTR(&wlan_ap_obj);
181 } else {
182 mp_raise_ValueError(MP_ERROR_TEXT("invalid WLAN interface identifier"));
183 }
184}
185MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
186
187STATIC mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) {
188 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
189
190 wifi_mode_t mode;
191 if (!wifi_started) {
192 mode = WIFI_MODE_NULL;
193 } else {
194 esp_exceptions(esp_wifi_get_mode(&mode));
195 }
196
197 int bit = (self->if_id == WIFI_IF_STA) ? WIFI_MODE_STA : WIFI_MODE_AP;
198
199 if (n_args > 1) {
200 bool active = mp_obj_is_true(args[1]);
201 mode = active ? (mode | bit) : (mode & ~bit);
202 if (mode == WIFI_MODE_NULL) {
203 if (wifi_started) {
204 esp_exceptions(esp_wifi_stop());
205 wifi_started = false;
206 }
207 } else {
208 esp_exceptions(esp_wifi_set_mode(mode));
209 if (!wifi_started) {
210 esp_exceptions(esp_wifi_start());
211 wifi_started = true;
212 }
213 }
214 }
215
216 return (mode & bit) ? mp_const_true : mp_const_false;
217}
218STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_active_obj, 1, 2, network_wlan_active);
219
220STATIC mp_obj_t network_wlan_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
221 enum { ARG_ssid, ARG_password, ARG_bssid };
222 static const mp_arg_t allowed_args[] = {
223 { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} },
224 { MP_QSTR_, MP_ARG_OBJ, {.u_obj = mp_const_none} },
225 { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
226 };
227
228 // parse args
229 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
230 mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
231
232 wifi_config_t wifi_sta_config = {0};
233
234 // configure any parameters that are given
235 if (n_args > 1) {
236 size_t len;
237 const char *p;
238 if (args[ARG_ssid].u_obj != mp_const_none) {
239 p = mp_obj_str_get_data(args[ARG_ssid].u_obj, &len);
240 memcpy(wifi_sta_config.sta.ssid, p, MIN(len, sizeof(wifi_sta_config.sta.ssid)));
241 }
242 if (args[ARG_password].u_obj != mp_const_none) {
243 p = mp_obj_str_get_data(args[ARG_password].u_obj, &len);
244 memcpy(wifi_sta_config.sta.password, p, MIN(len, sizeof(wifi_sta_config.sta.password)));
245 }
246 if (args[ARG_bssid].u_obj != mp_const_none) {
247 p = mp_obj_str_get_data(args[ARG_bssid].u_obj, &len);
248 if (len != sizeof(wifi_sta_config.sta.bssid)) {
249 mp_raise_ValueError(NULL);
250 }
251 wifi_sta_config.sta.bssid_set = 1;
252 memcpy(wifi_sta_config.sta.bssid, p, sizeof(wifi_sta_config.sta.bssid));
253 }
254 esp_exceptions(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
255 }
256
257 wifi_sta_reconnects = 0;
258 // connect to the WiFi AP
259 MP_THREAD_GIL_EXIT();
260 esp_exceptions(esp_wifi_connect());
261 MP_THREAD_GIL_ENTER();
262 wifi_sta_connect_requested = true;
263
264 return mp_const_none;
265}
266STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_connect_obj, 1, network_wlan_connect);
267
268STATIC mp_obj_t network_wlan_disconnect(mp_obj_t self_in) {
269 wifi_sta_connect_requested = false;
270 esp_exceptions(esp_wifi_disconnect());
271 return mp_const_none;
272}
273STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_disconnect_obj, network_wlan_disconnect);
274
275STATIC mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) {
276 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
277 if (n_args == 1) {
278 if (self->if_id == WIFI_IF_STA) {
279 // Case of no arg is only for the STA interface
280 if (wifi_sta_connected) {
281 // Happy path, connected with IP
282 return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
283 } else if (wifi_sta_connect_requested
284 && (conf_wifi_sta_reconnects == 0
285 || wifi_sta_reconnects < conf_wifi_sta_reconnects)) {
286 // No connection or error, but is requested = Still connecting
287 return MP_OBJ_NEW_SMALL_INT(STAT_CONNECTING);
288 } else if (wifi_sta_disconn_reason == 0) {
289 // No activity, No error = Idle
290 return MP_OBJ_NEW_SMALL_INT(STAT_IDLE);
291 } else {
292 // Simply pass the error through from ESP-identifier
293 return MP_OBJ_NEW_SMALL_INT(wifi_sta_disconn_reason);
294 }
295 }
296 return mp_const_none;
297 }
298
299 // one argument: return status based on query parameter
300 switch ((uintptr_t)args[1]) {
301 case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_stations): {
302 // return list of connected stations, only if in soft-AP mode
303 require_if(args[0], WIFI_IF_AP);
304 wifi_sta_list_t station_list;
305 esp_exceptions(esp_wifi_ap_get_sta_list(&station_list));
306 wifi_sta_info_t *stations = (wifi_sta_info_t *)station_list.sta;
307 mp_obj_t list = mp_obj_new_list(0, NULL);
308 for (int i = 0; i < station_list.num; ++i) {
309 mp_obj_tuple_t *t = mp_obj_new_tuple(1, NULL);
310 t->items[0] = mp_obj_new_bytes(stations[i].mac, sizeof(stations[i].mac));
311 mp_obj_list_append(list, t);
312 }
313 return list;
314 }
315 case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_rssi): {
316 // return signal of AP, only in STA mode
317 require_if(args[0], WIFI_IF_STA);
318
319 wifi_ap_record_t info;
320 esp_exceptions(esp_wifi_sta_get_ap_info(&info));
321 return MP_OBJ_NEW_SMALL_INT(info.rssi);
322 }
323 default:
324 mp_raise_ValueError(MP_ERROR_TEXT("unknown status param"));
325 }
326
327 return mp_const_none;
328}
329STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_status_obj, 1, 2, network_wlan_status);
330
331STATIC mp_obj_t network_wlan_scan(mp_obj_t self_in) {
332 // check that STA mode is active
333 wifi_mode_t mode;
334 esp_exceptions(esp_wifi_get_mode(&mode));
335 if ((mode & WIFI_MODE_STA) == 0) {
336 mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("STA must be active"));
337 }
338
339 mp_obj_t list = mp_obj_new_list(0, NULL);
340 wifi_scan_config_t config = { 0 };
341 config.show_hidden = true;
342 MP_THREAD_GIL_EXIT();
343 esp_err_t status = esp_wifi_scan_start(&config, 1);
344 MP_THREAD_GIL_ENTER();
345 if (status == 0) {
346 uint16_t count = 0;
347 esp_exceptions(esp_wifi_scan_get_ap_num(&count));
348 wifi_ap_record_t *wifi_ap_records = calloc(count, sizeof(wifi_ap_record_t));
349 esp_exceptions(esp_wifi_scan_get_ap_records(&count, wifi_ap_records));
350 for (uint16_t i = 0; i < count; i++) {
351 mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
352 uint8_t *x = memchr(wifi_ap_records[i].ssid, 0, sizeof(wifi_ap_records[i].ssid));
353 int ssid_len = x ? x - wifi_ap_records[i].ssid : sizeof(wifi_ap_records[i].ssid);
354 t->items[0] = mp_obj_new_bytes(wifi_ap_records[i].ssid, ssid_len);
355 t->items[1] = mp_obj_new_bytes(wifi_ap_records[i].bssid, sizeof(wifi_ap_records[i].bssid));
356 t->items[2] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].primary);
357 t->items[3] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].rssi);
358 t->items[4] = MP_OBJ_NEW_SMALL_INT(wifi_ap_records[i].authmode);
359 t->items[5] = mp_const_false; // XXX hidden?
360 mp_obj_list_append(list, MP_OBJ_FROM_PTR(t));
361 }
362 free(wifi_ap_records);
363 }
364 return list;
365}
366STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_scan_obj, network_wlan_scan);
367
368STATIC mp_obj_t network_wlan_isconnected(mp_obj_t self_in) {
369 wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
370 if (self->if_id == WIFI_IF_STA) {
371 return mp_obj_new_bool(wifi_sta_connected);
372 } else {
373 wifi_sta_list_t sta;
374 esp_wifi_ap_get_sta_list(&sta);
375 return mp_obj_new_bool(sta.num != 0);
376 }
377}
378STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_isconnected_obj, network_wlan_isconnected);
379
380STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
381 if (n_args != 1 && kwargs->used != 0) {
382 mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
383 }
384
385 wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
386
387 bool is_wifi = self->if_id == WIFI_IF_AP || self->if_id == WIFI_IF_STA;
388
389 wifi_config_t cfg;
390 if (is_wifi) {
391 esp_exceptions(esp_wifi_get_config(self->if_id, &cfg));
392 }
393
394 #define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
395
396 if (kwargs->used != 0) {
397 if (!is_wifi) {
398 goto unknown;
399 }
400
401 for (size_t i = 0; i < kwargs->alloc; i++) {
402 if (mp_map_slot_is_filled(kwargs, i)) {
403 int req_if = -1;
404
405 switch ((uintptr_t)kwargs->table[i].key) {
406 case QS(MP_QSTR_mac): {
407 mp_buffer_info_t bufinfo;
408 mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
409 if (bufinfo.len != 6) {
410 mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
411 }
412 esp_exceptions(esp_wifi_set_mac(self->if_id, bufinfo.buf));
413 break;
414 }
415 case QS(MP_QSTR_essid): {
416 req_if = WIFI_IF_AP;
417 size_t len;
418 const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
419 len = MIN(len, sizeof(cfg.ap.ssid));
420 memcpy(cfg.ap.ssid, s, len);
421 cfg.ap.ssid_len = len;
422 break;
423 }
424 case QS(MP_QSTR_hidden): {
425 req_if = WIFI_IF_AP;
426 cfg.ap.ssid_hidden = mp_obj_is_true(kwargs->table[i].value);
427 break;
428 }
429 case QS(MP_QSTR_authmode): {
430 req_if = WIFI_IF_AP;
431 cfg.ap.authmode = mp_obj_get_int(kwargs->table[i].value);
432 break;
433 }
434 case QS(MP_QSTR_password): {
435 req_if = WIFI_IF_AP;
436 size_t len;
437 const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
438 len = MIN(len, sizeof(cfg.ap.password) - 1);
439 memcpy(cfg.ap.password, s, len);
440 cfg.ap.password[len] = 0;
441 break;
442 }
443 case QS(MP_QSTR_channel): {
444 req_if = WIFI_IF_AP;
445 cfg.ap.channel = mp_obj_get_int(kwargs->table[i].value);
446 break;
447 }
448 case QS(MP_QSTR_dhcp_hostname): {
449 const char *s = mp_obj_str_get_str(kwargs->table[i].value);
450 esp_exceptions(tcpip_adapter_set_hostname(self->if_id, s));
451 break;
452 }
453 case QS(MP_QSTR_max_clients): {
454 req_if = WIFI_IF_AP;
455 cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value);
456 break;
457 }
458 case QS(MP_QSTR_reconnects): {
459 int reconnects = mp_obj_get_int(kwargs->table[i].value);
460 req_if = WIFI_IF_STA;
461 // parameter reconnects == -1 means to retry forever.
462 // here means conf_wifi_sta_reconnects == 0 to retry forever.
463 conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1;
464 break;
465 }
466 default:
467 goto unknown;
468 }
469
470 // We post-check interface requirements to save on code size
471 if (req_if >= 0) {
472 require_if(args[0], req_if);
473 }
474 }
475 }
476
477 esp_exceptions(esp_wifi_set_config(self->if_id, &cfg));
478
479 return mp_const_none;
480 }
481
482 // Get config
483
484 if (n_args != 2) {
485 mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
486 }
487
488 int req_if = -1;
489 mp_obj_t val = mp_const_none;
490
491 switch ((uintptr_t)args[1]) {
492 case QS(MP_QSTR_mac): {
493 uint8_t mac[6];
494 switch (self->if_id) {
495 case WIFI_IF_AP: // fallthrough intentional
496 case WIFI_IF_STA:
497 esp_exceptions(esp_wifi_get_mac(self->if_id, mac));
498 return mp_obj_new_bytes(mac, sizeof(mac));
499 default:
500 goto unknown;
501 }
502 }
503 case QS(MP_QSTR_essid):
504 switch (self->if_id) {
505 case WIFI_IF_STA:
506 val = mp_obj_new_str((char *)cfg.sta.ssid, strlen((char *)cfg.sta.ssid));
507 break;
508 case WIFI_IF_AP:
509 val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len);
510 break;
511 default:
512 req_if = WIFI_IF_AP;
513 }
514 break;
515 case QS(MP_QSTR_hidden):
516 req_if = WIFI_IF_AP;
517 val = mp_obj_new_bool(cfg.ap.ssid_hidden);
518 break;
519 case QS(MP_QSTR_authmode):
520 req_if = WIFI_IF_AP;
521 val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode);
522 break;
523 case QS(MP_QSTR_channel):
524 req_if = WIFI_IF_AP;
525 val = MP_OBJ_NEW_SMALL_INT(cfg.ap.channel);
526 break;
527 case QS(MP_QSTR_dhcp_hostname): {
528 const char *s;
529 esp_exceptions(tcpip_adapter_get_hostname(self->if_id, &s));
530 val = mp_obj_new_str(s, strlen(s));
531 break;
532 }
533 case QS(MP_QSTR_max_clients): {
534 val = MP_OBJ_NEW_SMALL_INT(cfg.ap.max_connection);
535 break;
536 }
537 case QS(MP_QSTR_reconnects):
538 req_if = WIFI_IF_STA;
539 int rec = conf_wifi_sta_reconnects - 1;
540 val = MP_OBJ_NEW_SMALL_INT(rec);
541 break;
542 default:
543 goto unknown;
544 }
545
546#undef QS
547
548 // We post-check interface requirements to save on code size
549 if (req_if >= 0) {
550 require_if(args[0], req_if);
551 }
552
553 return val;
554
555unknown:
556 mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
557}
558MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_config_obj, 1, network_wlan_config);
559
560STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
561 { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_wlan_active_obj) },
562 { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_wlan_connect_obj) },
563 { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_wlan_disconnect_obj) },
564 { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_wlan_status_obj) },
565 { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&network_wlan_scan_obj) },
566 { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) },
567 { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) },
568 { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
569};
570STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table);
571
572STATIC const mp_obj_type_t wlan_if_type = {
573 { &mp_type_type },
574 .name = MP_QSTR_WLAN,
575 .locals_dict = (mp_obj_t)&wlan_if_locals_dict,
576};
577
578#endif // MICROPY_PY_NETWORK_WLAN