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