|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Arduino SA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + * |
| 26 | + * ESP-Hosted network interface. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "py/mphal.h" |
| 30 | + |
| 31 | +#if MICROPY_PY_NETWORK && MICROPY_PY_NETWORK_ESP_HOSTED |
| 32 | + |
| 33 | +#include <string.h> |
| 34 | +#include <stdio.h> |
| 35 | +#include <stdarg.h> |
| 36 | +#include <stdint.h> |
| 37 | + |
| 38 | +#include "py/objtuple.h" |
| 39 | +#include "py/objlist.h" |
| 40 | +#include "py/stream.h" |
| 41 | +#include "py/runtime.h" |
| 42 | +#include "py/misc.h" |
| 43 | +#include "py/mperrno.h" |
| 44 | +#include "shared/netutils/netutils.h" |
| 45 | +#include "extmod/modnetwork.h" |
| 46 | +#include "modmachine.h" |
| 47 | + |
| 48 | +#include "esp_hosted_wifi.h" |
| 49 | +#include "esp_hosted_hal.h" |
| 50 | + |
| 51 | +typedef struct _esp_hosted_obj_t { |
| 52 | + mp_obj_base_t base; |
| 53 | + uint32_t itf; |
| 54 | +} esp_hosted_obj_t; |
| 55 | + |
| 56 | +static esp_hosted_obj_t esp_hosted_sta_if = {{(mp_obj_type_t *)&mod_network_esp_hosted_type}, ESP_HOSTED_STA_IF}; |
| 57 | +static esp_hosted_obj_t esp_hosted_ap_if = {{(mp_obj_type_t *)&mod_network_esp_hosted_type}, ESP_HOSTED_AP_IF}; |
| 58 | + |
| 59 | +STATIC mp_obj_t network_esp_hosted_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 60 | + mp_arg_check_num(n_args, n_kw, 0, 1, false); |
| 61 | + mp_obj_t esp_hosted_obj; |
| 62 | + // TODO fix |
| 63 | + if (n_args == 0 || mp_obj_get_int(args[0]) == ESP_HOSTED_STA_IF) { |
| 64 | + esp_hosted_obj = MP_OBJ_FROM_PTR(&esp_hosted_sta_if); |
| 65 | + } else { |
| 66 | + esp_hosted_obj = MP_OBJ_FROM_PTR(&esp_hosted_ap_if); |
| 67 | + } |
| 68 | + // Register with network module |
| 69 | + mod_network_register_nic(esp_hosted_obj); |
| 70 | + return esp_hosted_obj; |
| 71 | +} |
| 72 | + |
| 73 | +STATIC mp_obj_t network_esp_hosted_active(size_t n_args, const mp_obj_t *args) { |
| 74 | + esp_hosted_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 75 | + |
| 76 | + if (n_args == 2) { |
| 77 | + bool active = mp_obj_is_true(args[1]); |
| 78 | + if (active) { |
| 79 | + // If the active NIC is changing disable the active one first. |
| 80 | + // Note the host driver, firmware and ESP all support simultaneous AP/STA, |
| 81 | + // however modnetwork.c doesn't support routing traffic to different NICs |
| 82 | + // at the moment. |
| 83 | + if (self->itf == ESP_HOSTED_STA_IF && esp_hosted_wifi_link_status(ESP_HOSTED_AP_IF)) { |
| 84 | + esp_hosted_wifi_disable(ESP_HOSTED_AP_IF); |
| 85 | + } else if (self->itf == ESP_HOSTED_AP_IF && esp_hosted_wifi_link_status(ESP_HOSTED_STA_IF)) { |
| 86 | + esp_hosted_wifi_disable(ESP_HOSTED_STA_IF); |
| 87 | + } |
| 88 | + if (esp_hosted_wifi_init(self->itf) != 0) { |
| 89 | + mp_raise_msg_varg(&mp_type_OSError, MP_ERROR_TEXT("Failed to initialize ESP32 module")); |
| 90 | + } |
| 91 | + } else { |
| 92 | + esp_hosted_wifi_disable(self->itf); |
| 93 | + } |
| 94 | + return mp_const_none; |
| 95 | + } |
| 96 | + return mp_obj_new_bool(esp_hosted_wifi_link_status(self->itf)); |
| 97 | +} |
| 98 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_esp_hosted_active_obj, 1, 2, network_esp_hosted_active); |
| 99 | + |
| 100 | +STATIC int esp_hosted_scan_callback(esp_hosted_scan_result_t *scan_result, void *arg) { |
| 101 | + mp_obj_t scan_list = (mp_obj_t)arg; |
| 102 | + mp_obj_t ap[6] = { |
| 103 | + mp_obj_new_bytes((uint8_t *)scan_result->ssid, strlen(scan_result->ssid)), |
| 104 | + mp_obj_new_bytes(scan_result->bssid, sizeof(scan_result->bssid)), |
| 105 | + mp_obj_new_int(scan_result->channel), |
| 106 | + mp_obj_new_int(scan_result->rssi), |
| 107 | + mp_obj_new_int(scan_result->security), |
| 108 | + MP_OBJ_NEW_SMALL_INT(1), |
| 109 | + }; |
| 110 | + mp_obj_list_append(scan_list, mp_obj_new_tuple(MP_ARRAY_SIZE(ap), ap)); |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +STATIC mp_obj_t network_esp_hosted_scan(mp_obj_t self_in) { |
| 115 | + mp_obj_t scan_list; |
| 116 | + scan_list = mp_obj_new_list(0, NULL); |
| 117 | + esp_hosted_wifi_scan(esp_hosted_scan_callback, scan_list, 10000); |
| 118 | + return scan_list; |
| 119 | +} |
| 120 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_esp_hosted_scan_obj, network_esp_hosted_scan); |
| 121 | + |
| 122 | +STATIC mp_obj_t network_esp_hosted_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 123 | + enum { ARG_ssid, ARG_key, ARG_security, ARG_bssid, ARG_channel }; |
| 124 | + static const mp_arg_t allowed_args[] = { |
| 125 | + { MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 126 | + { MP_QSTR_key, MP_ARG_OBJ, {.u_obj = MP_ROM_NONE} }, |
| 127 | + { MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ESP_HOSTED_SEC_WPA_WPA2_PSK} }, |
| 128 | + { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 129 | + { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 130 | + }; |
| 131 | + |
| 132 | + // Extract args. |
| 133 | + esp_hosted_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
| 134 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 135 | + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 136 | + |
| 137 | + // get ssid |
| 138 | + const char *ssid = NULL; |
| 139 | + if (args[ARG_ssid].u_obj != mp_const_none) { |
| 140 | + ssid = mp_obj_str_get_str(args[ARG_ssid].u_obj); |
| 141 | + } else { |
| 142 | + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SSID can't be empty!")); |
| 143 | + } |
| 144 | + |
| 145 | + // get bssid (if any) |
| 146 | + const char *bssid = NULL; |
| 147 | + if (args[ARG_bssid].u_obj != mp_const_none) { |
| 148 | + bssid = mp_obj_str_get_str(args[ARG_bssid].u_obj); |
| 149 | + } |
| 150 | + |
| 151 | + // get key and security |
| 152 | + const char *key = NULL; |
| 153 | + mp_uint_t security = ESP_HOSTED_SEC_OPEN; |
| 154 | + if (args[ARG_key].u_obj != mp_const_none) { |
| 155 | + key = mp_obj_str_get_str(args[ARG_key].u_obj); |
| 156 | + security = args[ARG_security].u_int; |
| 157 | + if (security != ESP_HOSTED_SEC_OPEN && strlen(key) == 0) { |
| 158 | + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Key can't be empty!")); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // get channel |
| 163 | + mp_uint_t channel = args[ARG_channel].u_int; |
| 164 | + |
| 165 | + // If connected or AP active disconnect/stop AP first. |
| 166 | + if (esp_hosted_wifi_is_connected(self->itf)) { |
| 167 | + esp_hosted_wifi_disconnect(self->itf); |
| 168 | + } |
| 169 | + |
| 170 | + if (self->itf == ESP_HOSTED_STA_IF) { |
| 171 | + // Initialize WiFi in Station mode. |
| 172 | + if (esp_hosted_wifi_connect(ssid, bssid, security, key, channel) != 0) { |
| 173 | + mp_raise_msg_varg(&mp_type_OSError, |
| 174 | + MP_ERROR_TEXT("could not connect to ssid=%s, sec=%d, key=%s\n"), ssid, security, key); |
| 175 | + } |
| 176 | + } else { |
| 177 | + // Initialize WiFi in AP mode. |
| 178 | + if (esp_hosted_wifi_start_ap(ssid, security, key, channel) != 0) { |
| 179 | + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed to start in AP mode")); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return mp_const_none; |
| 184 | +} |
| 185 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_esp_hosted_connect_obj, 1, network_esp_hosted_connect); |
| 186 | + |
| 187 | +STATIC mp_obj_t network_esp_hosted_disconnect(mp_obj_t self_in) { |
| 188 | + esp_hosted_obj_t *self = self_in; |
| 189 | + esp_hosted_wifi_disconnect(self->itf); |
| 190 | + return mp_const_none; |
| 191 | +} |
| 192 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_esp_hosted_disconnect_obj, network_esp_hosted_disconnect); |
| 193 | + |
| 194 | +STATIC mp_obj_t network_esp_hosted_isconnected(mp_obj_t self_in) { |
| 195 | + esp_hosted_obj_t *self = self_in; |
| 196 | + return mp_obj_new_bool(esp_hosted_wifi_is_connected(self->itf)); |
| 197 | +} |
| 198 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_esp_hosted_isconnected_obj, network_esp_hosted_isconnected); |
| 199 | + |
| 200 | +STATIC mp_obj_t network_esp_hosted_ifconfig(size_t n_args, const mp_obj_t *args) { |
| 201 | + esp_hosted_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 202 | + void *netif = esp_hosted_wifi_get_netif(self->itf); |
| 203 | + return mod_network_nic_ifconfig(netif, n_args - 1, args + 1); |
| 204 | +} |
| 205 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_esp_hosted_ifconfig_obj, 1, 2, network_esp_hosted_ifconfig); |
| 206 | + |
| 207 | +STATIC mp_obj_t network_esp_hosted_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { |
| 208 | + esp_hosted_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 209 | + |
| 210 | + if (kwargs->used == 0) { |
| 211 | + // Get config value |
| 212 | + if (n_args != 2) { |
| 213 | + mp_raise_TypeError(MP_ERROR_TEXT("must query one param")); |
| 214 | + } |
| 215 | + |
| 216 | + switch (mp_obj_str_get_qstr(args[1])) { |
| 217 | + case MP_QSTR_mac: { |
| 218 | + uint8_t buf[6]; |
| 219 | + esp_hosted_wifi_get_mac(self->itf, buf); |
| 220 | + return mp_obj_new_bytes(buf, 6); |
| 221 | + } |
| 222 | + case MP_QSTR_ssid: |
| 223 | + case MP_QSTR_essid: { |
| 224 | + esp_hosted_netinfo_t netinfo; |
| 225 | + esp_hosted_wifi_netinfo(&netinfo); |
| 226 | + return mp_obj_new_str(netinfo.ssid, strlen(netinfo.ssid)); |
| 227 | + } |
| 228 | + case MP_QSTR_security: { |
| 229 | + esp_hosted_netinfo_t netinfo; |
| 230 | + esp_hosted_wifi_netinfo(&netinfo); |
| 231 | + return mp_obj_new_int(netinfo.security); |
| 232 | + } |
| 233 | + case MP_QSTR_bssid: { |
| 234 | + esp_hosted_netinfo_t netinfo; |
| 235 | + esp_hosted_wifi_netinfo(&netinfo); |
| 236 | + return mp_obj_new_bytes(netinfo.bssid, 6); |
| 237 | + } |
| 238 | + case MP_QSTR_channel: { |
| 239 | + esp_hosted_netinfo_t netinfo; |
| 240 | + esp_hosted_wifi_netinfo(&netinfo); |
| 241 | + return mp_obj_new_int(netinfo.channel); |
| 242 | + } |
| 243 | + default: |
| 244 | + mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); |
| 245 | + } |
| 246 | + } else { |
| 247 | + if (self->itf != MOD_NETWORK_AP_IF) { |
| 248 | + mp_raise_ValueError(MP_ERROR_TEXT("AP required")); |
| 249 | + } |
| 250 | + // Call connect to set WiFi access point. |
| 251 | + return network_esp_hosted_connect(n_args, args, kwargs); |
| 252 | + } |
| 253 | + |
| 254 | + return mp_const_none; |
| 255 | +} |
| 256 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(network_esp_hosted_config_obj, 1, network_esp_hosted_config); |
| 257 | + |
| 258 | +STATIC mp_obj_t network_esp_hosted_status(size_t n_args, const mp_obj_t *args) { |
| 259 | + esp_hosted_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 260 | + |
| 261 | + if (n_args == 1) { |
| 262 | + // no arguments: return link status |
| 263 | + return mp_obj_new_bool(esp_hosted_wifi_link_status(self->itf)); |
| 264 | + } |
| 265 | + |
| 266 | + // Query parameter. |
| 267 | + switch (mp_obj_str_get_qstr(args[1])) { |
| 268 | + case MP_QSTR_rssi: { |
| 269 | + esp_hosted_netinfo_t netinfo; |
| 270 | + esp_hosted_wifi_netinfo(&netinfo); |
| 271 | + return mp_obj_new_int(netinfo.rssi); |
| 272 | + } |
| 273 | + case MP_QSTR_stations: { |
| 274 | + if (self->itf != MOD_NETWORK_AP_IF) { |
| 275 | + mp_raise_ValueError(MP_ERROR_TEXT("AP required")); |
| 276 | + } |
| 277 | + |
| 278 | + size_t sta_count = 0; |
| 279 | + uint8_t sta_macs[ESP_HOSTED_MAX_AP_CLIENTS * 6]; |
| 280 | + |
| 281 | + mp_obj_t sta_list = mp_obj_new_list(0, NULL); |
| 282 | + if (esp_hosted_wifi_get_stations(sta_macs, &sta_count) == 0) { |
| 283 | + for (int i = 0; i < sta_count; i++) { |
| 284 | + mp_obj_list_append(sta_list, mp_obj_new_bytes(&sta_macs[i * 6], 6)); |
| 285 | + } |
| 286 | + } |
| 287 | + return sta_list; |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + mp_raise_ValueError(MP_ERROR_TEXT("unknown status param")); |
| 292 | +} |
| 293 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_esp_hosted_status_obj, 1, 2, network_esp_hosted_status); |
| 294 | + |
| 295 | +STATIC const mp_rom_map_elem_t network_esp_hosted_locals_dict_table[] = { |
| 296 | + { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_esp_hosted_active_obj) }, |
| 297 | + { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&network_esp_hosted_scan_obj) }, |
| 298 | + { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_esp_hosted_connect_obj) }, |
| 299 | + { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_esp_hosted_disconnect_obj) }, |
| 300 | + { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_esp_hosted_isconnected_obj) }, |
| 301 | + { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_esp_hosted_ifconfig_obj) }, |
| 302 | + { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_esp_hosted_config_obj) }, |
| 303 | + { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_esp_hosted_status_obj) }, |
| 304 | + { MP_ROM_QSTR(MP_QSTR_OPEN), MP_ROM_INT(ESP_HOSTED_SEC_OPEN) }, |
| 305 | + { MP_ROM_QSTR(MP_QSTR_WEP), MP_ROM_INT(ESP_HOSTED_SEC_WEP) }, |
| 306 | + { MP_ROM_QSTR(MP_QSTR_WPA_PSK), MP_ROM_INT(ESP_HOSTED_SEC_WPA_WPA2_PSK) }, |
| 307 | +}; |
| 308 | +STATIC MP_DEFINE_CONST_DICT(network_esp_hosted_locals_dict, network_esp_hosted_locals_dict_table); |
| 309 | + |
| 310 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 311 | + mod_network_esp_hosted_type, |
| 312 | + MP_QSTR_ESPHOSTED, |
| 313 | + MP_TYPE_FLAG_NONE, |
| 314 | + make_new, network_esp_hosted_make_new, |
| 315 | + locals_dict, &network_esp_hosted_locals_dict |
| 316 | + ); |
| 317 | + |
| 318 | +MP_REGISTER_ROOT_POINTER(struct _machine_spi_obj_t *mp_wifi_spi); |
| 319 | + |
| 320 | +#endif // #if MICROPY_PY_BLUETOOTH && MICROPY_PY_NETWORK_ESP_HOSTED |
0 commit comments