Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 1 | /* |
| 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 | |
| 50 | STATIC const mp_obj_type_t wlan_if_type; |
| 51 | STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA}; |
| 52 | STATIC 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 |
| 55 | static 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. |
| 59 | static bool wifi_sta_connect_requested = false; |
| 60 | |
| 61 | // Set to "true" if the STA interface is connected to wifi and has IP address. |
| 62 | static 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. |
| 65 | static 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 |
| 69 | static bool mdns_initialised = false; |
| 70 | #endif |
| 71 | |
| 72 | static uint8_t conf_wifi_sta_reconnects = 0; |
| 73 | static 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. |
| 77 | void 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 | |
| 158 | STATIC 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 | |
| 165 | STATIC 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 | } |
| 185 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan); |
| 186 | |
| 187 | STATIC 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 | } |
| 218 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_active_obj, 1, 2, network_wlan_active); |
| 219 | |
| 220 | STATIC 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 | } |
| 266 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_connect_obj, 1, network_wlan_connect); |
| 267 | |
| 268 | STATIC 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 | } |
| 273 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_disconnect_obj, network_wlan_disconnect); |
| 274 | |
| 275 | STATIC 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 | } |
| 329 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_wlan_status_obj, 1, 2, network_wlan_status); |
| 330 | |
| 331 | STATIC 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 | } |
| 366 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_scan_obj, network_wlan_scan); |
| 367 | |
| 368 | STATIC 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 | } |
| 378 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_isconnected_obj, network_wlan_isconnected); |
| 379 | |
| 380 | STATIC 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 | |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 394 | if (kwargs->used != 0) { |
| 395 | if (!is_wifi) { |
| 396 | goto unknown; |
| 397 | } |
| 398 | |
| 399 | for (size_t i = 0; i < kwargs->alloc; i++) { |
| 400 | if (mp_map_slot_is_filled(kwargs, i)) { |
| 401 | int req_if = -1; |
| 402 | |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 403 | switch (mp_obj_str_get_qstr(kwargs->table[i].key)) { |
| 404 | case MP_QSTR_mac: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 405 | mp_buffer_info_t bufinfo; |
| 406 | mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ); |
| 407 | if (bufinfo.len != 6) { |
| 408 | mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length")); |
| 409 | } |
| 410 | esp_exceptions(esp_wifi_set_mac(self->if_id, bufinfo.buf)); |
| 411 | break; |
| 412 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 413 | case MP_QSTR_essid: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 414 | req_if = WIFI_IF_AP; |
| 415 | size_t len; |
| 416 | const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len); |
| 417 | len = MIN(len, sizeof(cfg.ap.ssid)); |
| 418 | memcpy(cfg.ap.ssid, s, len); |
| 419 | cfg.ap.ssid_len = len; |
| 420 | break; |
| 421 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 422 | case MP_QSTR_hidden: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 423 | req_if = WIFI_IF_AP; |
| 424 | cfg.ap.ssid_hidden = mp_obj_is_true(kwargs->table[i].value); |
| 425 | break; |
| 426 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 427 | case MP_QSTR_authmode: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 428 | req_if = WIFI_IF_AP; |
| 429 | cfg.ap.authmode = mp_obj_get_int(kwargs->table[i].value); |
| 430 | break; |
| 431 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 432 | case MP_QSTR_password: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 433 | req_if = WIFI_IF_AP; |
| 434 | size_t len; |
| 435 | const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len); |
| 436 | len = MIN(len, sizeof(cfg.ap.password) - 1); |
| 437 | memcpy(cfg.ap.password, s, len); |
| 438 | cfg.ap.password[len] = 0; |
| 439 | break; |
| 440 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 441 | case MP_QSTR_channel: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 442 | req_if = WIFI_IF_AP; |
| 443 | cfg.ap.channel = mp_obj_get_int(kwargs->table[i].value); |
| 444 | break; |
| 445 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 446 | case MP_QSTR_dhcp_hostname: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 447 | const char *s = mp_obj_str_get_str(kwargs->table[i].value); |
| 448 | esp_exceptions(tcpip_adapter_set_hostname(self->if_id, s)); |
| 449 | break; |
| 450 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 451 | case MP_QSTR_max_clients: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 452 | req_if = WIFI_IF_AP; |
| 453 | cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value); |
| 454 | break; |
| 455 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 456 | case MP_QSTR_reconnects: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 457 | int reconnects = mp_obj_get_int(kwargs->table[i].value); |
| 458 | req_if = WIFI_IF_STA; |
| 459 | // parameter reconnects == -1 means to retry forever. |
| 460 | // here means conf_wifi_sta_reconnects == 0 to retry forever. |
| 461 | conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1; |
| 462 | break; |
| 463 | } |
wemos | ff28d2e | 2022-04-11 20:12:57 +0800 | [diff] [blame^] | 464 | case MP_QSTR_txpower: { |
| 465 | int8_t power = (mp_obj_get_float(kwargs->table[i].value) * 4); |
| 466 | esp_exceptions(esp_wifi_set_max_tx_power(power)); |
| 467 | break; |
| 468 | } |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 469 | default: |
| 470 | goto unknown; |
| 471 | } |
| 472 | |
| 473 | // We post-check interface requirements to save on code size |
| 474 | if (req_if >= 0) { |
| 475 | require_if(args[0], req_if); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | esp_exceptions(esp_wifi_set_config(self->if_id, &cfg)); |
| 481 | |
| 482 | return mp_const_none; |
| 483 | } |
| 484 | |
| 485 | // Get config |
| 486 | |
| 487 | if (n_args != 2) { |
| 488 | mp_raise_TypeError(MP_ERROR_TEXT("can query only one param")); |
| 489 | } |
| 490 | |
| 491 | int req_if = -1; |
| 492 | mp_obj_t val = mp_const_none; |
| 493 | |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 494 | switch (mp_obj_str_get_qstr(args[1])) { |
| 495 | case MP_QSTR_mac: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 496 | uint8_t mac[6]; |
| 497 | switch (self->if_id) { |
| 498 | case WIFI_IF_AP: // fallthrough intentional |
| 499 | case WIFI_IF_STA: |
| 500 | esp_exceptions(esp_wifi_get_mac(self->if_id, mac)); |
| 501 | return mp_obj_new_bytes(mac, sizeof(mac)); |
| 502 | default: |
| 503 | goto unknown; |
| 504 | } |
| 505 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 506 | case MP_QSTR_essid: |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 507 | switch (self->if_id) { |
| 508 | case WIFI_IF_STA: |
| 509 | val = mp_obj_new_str((char *)cfg.sta.ssid, strlen((char *)cfg.sta.ssid)); |
| 510 | break; |
| 511 | case WIFI_IF_AP: |
| 512 | val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len); |
| 513 | break; |
| 514 | default: |
| 515 | req_if = WIFI_IF_AP; |
| 516 | } |
| 517 | break; |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 518 | case MP_QSTR_hidden: |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 519 | req_if = WIFI_IF_AP; |
| 520 | val = mp_obj_new_bool(cfg.ap.ssid_hidden); |
| 521 | break; |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 522 | case MP_QSTR_authmode: |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 523 | req_if = WIFI_IF_AP; |
| 524 | val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode); |
| 525 | break; |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 526 | case MP_QSTR_channel: |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 527 | req_if = WIFI_IF_AP; |
| 528 | val = MP_OBJ_NEW_SMALL_INT(cfg.ap.channel); |
| 529 | break; |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 530 | case MP_QSTR_dhcp_hostname: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 531 | const char *s; |
| 532 | esp_exceptions(tcpip_adapter_get_hostname(self->if_id, &s)); |
| 533 | val = mp_obj_new_str(s, strlen(s)); |
| 534 | break; |
| 535 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 536 | case MP_QSTR_max_clients: { |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 537 | val = MP_OBJ_NEW_SMALL_INT(cfg.ap.max_connection); |
| 538 | break; |
| 539 | } |
Damien George | 5adb1fa | 2021-12-10 23:28:46 +1100 | [diff] [blame] | 540 | case MP_QSTR_reconnects: |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 541 | req_if = WIFI_IF_STA; |
| 542 | int rec = conf_wifi_sta_reconnects - 1; |
| 543 | val = MP_OBJ_NEW_SMALL_INT(rec); |
| 544 | break; |
wemos | ff28d2e | 2022-04-11 20:12:57 +0800 | [diff] [blame^] | 545 | case MP_QSTR_txpower: { |
| 546 | int8_t power; |
| 547 | esp_exceptions(esp_wifi_get_max_tx_power(&power)); |
| 548 | val = mp_obj_new_float(power * 0.25); |
| 549 | break; |
| 550 | } |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 551 | default: |
| 552 | goto unknown; |
| 553 | } |
| 554 | |
Damien George | ea186de | 2021-09-22 00:35:46 +1000 | [diff] [blame] | 555 | // We post-check interface requirements to save on code size |
| 556 | if (req_if >= 0) { |
| 557 | require_if(args[0], req_if); |
| 558 | } |
| 559 | |
| 560 | return val; |
| 561 | |
| 562 | unknown: |
| 563 | mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); |
| 564 | } |
| 565 | MP_DEFINE_CONST_FUN_OBJ_KW(network_wlan_config_obj, 1, network_wlan_config); |
| 566 | |
| 567 | STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = { |
| 568 | { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_wlan_active_obj) }, |
| 569 | { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_wlan_connect_obj) }, |
| 570 | { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_wlan_disconnect_obj) }, |
| 571 | { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_wlan_status_obj) }, |
| 572 | { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&network_wlan_scan_obj) }, |
| 573 | { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) }, |
| 574 | { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) }, |
| 575 | { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) }, |
| 576 | }; |
| 577 | STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table); |
| 578 | |
| 579 | STATIC const mp_obj_type_t wlan_if_type = { |
| 580 | { &mp_type_type }, |
| 581 | .name = MP_QSTR_WLAN, |
| 582 | .locals_dict = (mp_obj_t)&wlan_if_locals_dict, |
| 583 | }; |
| 584 | |
| 585 | #endif // MICROPY_PY_NETWORK_WLAN |