2022-08-31 11:13:39 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "supervisor/port.h"
|
|
|
|
#include "shared-bindings/wifi/Radio.h"
|
|
|
|
#include "shared-bindings/wifi/Network.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "common-hal/wifi/__init__.h"
|
|
|
|
#include "shared/runtime/interrupt_char.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/runtime.h"
|
2022-09-30 15:21:37 -04:00
|
|
|
#include "bindings/cyw43/__init__.h"
|
2022-08-31 11:13:39 -04:00
|
|
|
#include "shared-bindings/ipaddress/IPv4Address.h"
|
|
|
|
#include "shared-bindings/wifi/ScannedNetworks.h"
|
|
|
|
#include "shared-bindings/wifi/AuthMode.h"
|
|
|
|
#include "shared-bindings/time/__init__.h"
|
|
|
|
#include "shared-module/ipaddress/__init__.h"
|
|
|
|
|
2022-11-11 07:17:57 -05:00
|
|
|
#include "lwip/sys.h"
|
2022-08-31 11:13:39 -04:00
|
|
|
#include "lwip/dns.h"
|
|
|
|
#include "lwip/icmp.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip_src/ping.h"
|
|
|
|
|
2023-05-14 18:44:27 -04:00
|
|
|
#include "shared/netutils/dhcpserver.h"
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
#define MAC_ADDRESS_LENGTH 6
|
|
|
|
|
|
|
|
#define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA])
|
|
|
|
#define NETIF_AP (&cyw43_state.netif[CYW43_ITF_AP])
|
|
|
|
|
2022-09-29 10:09:14 -04:00
|
|
|
static inline uint32_t nw_get_le32(const uint8_t *buf) {
|
|
|
|
return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void nw_put_le32(uint8_t *buf, uint32_t x) {
|
|
|
|
buf[0] = x;
|
|
|
|
buf[1] = x >> 8;
|
|
|
|
buf[2] = x >> 16;
|
|
|
|
buf[3] = x >> 24;
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:44:06 -05:00
|
|
|
NORETURN static void ro_attribute(qstr attr) {
|
|
|
|
mp_raise_NotImplementedError_varg(translate("%q is read-only for this board"), attr);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self) {
|
2022-11-18 14:21:02 -05:00
|
|
|
return self->enabled;
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
|
2022-11-18 14:21:02 -05:00
|
|
|
// TODO: Actually enable and disable the WiFi module at this point.
|
2023-09-28 10:53:40 -04:00
|
|
|
if (self->enabled && !enabled) {
|
|
|
|
common_hal_wifi_radio_stop_station(self);
|
|
|
|
common_hal_wifi_radio_stop_ap(self);
|
|
|
|
}
|
|
|
|
self->enabled = enabled;
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
|
|
|
|
if (!NETIF_STA->hostname) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return mp_obj_new_str(NETIF_STA->hostname, strlen(NETIF_STA->hostname));
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
|
2022-09-30 11:05:11 -04:00
|
|
|
assert(strlen(hostname) < MP_ARRAY_SIZE(self->hostname));
|
2023-07-09 11:55:37 -04:00
|
|
|
strncpy(self->hostname, hostname, MP_ARRAY_SIZE(self->hostname) - 1);
|
2022-09-30 11:05:11 -04:00
|
|
|
netif_set_hostname(NETIF_STA, self->hostname);
|
|
|
|
netif_set_hostname(NETIF_AP, self->hostname);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2022-11-22 20:10:54 -05:00
|
|
|
void wifi_radio_get_mac_address(wifi_radio_obj_t *self, uint8_t *mac) {
|
|
|
|
memcpy(mac, cyw43_state.mac, MAC_ADDRESS_LENGTH);
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
|
|
|
|
return mp_obj_new_bytes(cyw43_state.mac, MAC_ADDRESS_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac) {
|
|
|
|
ro_attribute(MP_QSTR_mac_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_float_t common_hal_wifi_radio_get_tx_power(wifi_radio_obj_t *self) {
|
2022-09-29 10:09:14 -04:00
|
|
|
uint8_t buf[13];
|
|
|
|
memcpy(buf, "qtxpower\x00\x00\x00\x00\x00", 13);
|
|
|
|
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_VAR, 13, buf, CYW43_ITF_STA);
|
|
|
|
return nw_get_le32(buf) * MICROPY_FLOAT_CONST(0.25);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_tx_power(wifi_radio_obj_t *self, const mp_float_t tx_power) {
|
2022-09-29 10:09:14 -04:00
|
|
|
mp_int_t dbm_times_four = (int)(4 * tx_power);
|
|
|
|
uint8_t buf[9 + 4];
|
|
|
|
memcpy(buf, "qtxpower\x00", 9);
|
|
|
|
nw_put_le32(buf + 9, dbm_times_four);
|
|
|
|
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_STA);
|
|
|
|
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_AP);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) {
|
|
|
|
return common_hal_wifi_radio_get_mac_address(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_mac_address_ap(wifi_radio_obj_t *self, const uint8_t *mac) {
|
|
|
|
ro_attribute(MP_QSTR_mac_address_ap);
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:22:17 -04:00
|
|
|
mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self, uint8_t start_channel, uint8_t stop_channel) {
|
|
|
|
// channel bounds are ignored; not implemented in driver
|
2022-08-31 11:13:39 -04:00
|
|
|
if (self->current_scan) {
|
|
|
|
mp_raise_RuntimeError(translate("Already scanning for wifi networks"));
|
|
|
|
}
|
|
|
|
if (!common_hal_wifi_radio_get_enabled(self)) {
|
2022-11-21 18:00:34 -05:00
|
|
|
mp_raise_RuntimeError(translate("Wifi is not enabled"));
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
2023-08-07 20:45:57 -04:00
|
|
|
wifi_scannednetworks_obj_t *scan = mp_obj_malloc(wifi_scannednetworks_obj_t, &wifi_scannednetworks_type);
|
2022-08-31 11:13:39 -04:00
|
|
|
mp_obj_t args[] = { mp_const_empty_tuple, MP_OBJ_NEW_SMALL_INT(16) };
|
|
|
|
scan->results = mp_type_deque.make_new(&mp_type_deque, 2, 0, args);
|
|
|
|
self->current_scan = scan;
|
|
|
|
wifi_scannednetworks_start_scan(scan);
|
|
|
|
return scan;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
|
|
|
|
self->current_scan = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
|
|
|
|
cyw43_arch_enable_sta_mode();
|
2022-09-30 15:21:37 -04:00
|
|
|
bindings_cyw43_wifi_enforce_pm();
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
|
2022-11-17 14:47:39 -05:00
|
|
|
|
2022-10-17 20:14:20 -04:00
|
|
|
cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
|
2023-05-15 21:47:44 -04:00
|
|
|
const size_t timeout_ms = 500;
|
2023-05-15 15:38:59 -04:00
|
|
|
uint64_t start = port_get_raw_ticks(NULL);
|
|
|
|
uint64_t deadline = start + timeout_ms;
|
|
|
|
while (port_get_raw_ticks(NULL) < deadline && (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN)) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 20:14:20 -04:00
|
|
|
bindings_cyw43_wifi_enforce_pm();
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:17:47 -04:00
|
|
|
void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint32_t authmode, uint8_t max_connections) {
|
2022-10-21 06:20:16 -04:00
|
|
|
if (!common_hal_wifi_radio_get_enabled(self)) {
|
2022-11-21 18:00:34 -05:00
|
|
|
mp_raise_RuntimeError(translate("Wifi is not enabled"));
|
2022-10-21 06:20:16 -04:00
|
|
|
}
|
2022-11-17 14:47:39 -05:00
|
|
|
|
2023-08-23 13:04:51 -04:00
|
|
|
/* TODO: If the AP is stopped once it cannot be restarted.
|
|
|
|
* This means that if if the user does:
|
|
|
|
*
|
|
|
|
* wifi.radio.start_ap(...)
|
|
|
|
* wifi.radio.stop_ap()
|
|
|
|
* wifi.radio.start_ap(...)
|
|
|
|
*
|
|
|
|
* The second start_ap will fail.
|
|
|
|
*/
|
2022-11-17 14:47:39 -05:00
|
|
|
|
|
|
|
common_hal_wifi_radio_stop_ap(self);
|
2022-10-22 04:58:44 -04:00
|
|
|
|
2023-03-18 11:17:02 -04:00
|
|
|
// Channel can only be changed after initial powerup and config of ap.
|
2022-10-29 15:20:10 -04:00
|
|
|
// Defaults to 1 if not set or invalid (i.e. 13)
|
|
|
|
cyw43_wifi_ap_set_channel(&cyw43_state, (const uint32_t)channel);
|
|
|
|
|
2023-08-23 13:04:51 -04:00
|
|
|
if (password_len) {
|
|
|
|
cyw43_arch_enable_ap_mode((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK);
|
|
|
|
} else {
|
|
|
|
cyw43_arch_enable_ap_mode((const char *)ssid, NULL, CYW43_AUTH_OPEN);
|
|
|
|
}
|
2022-11-17 14:47:39 -05:00
|
|
|
|
2022-10-22 04:58:44 -04:00
|
|
|
// TODO: Implement authmode check like in espressif
|
2022-09-30 15:21:37 -04:00
|
|
|
bindings_cyw43_wifi_enforce_pm();
|
2023-08-23 13:04:51 -04:00
|
|
|
|
|
|
|
const size_t timeout_ms = 500;
|
|
|
|
uint64_t start = port_get_raw_ticks(NULL);
|
|
|
|
uint64_t deadline = start + timeout_ms;
|
|
|
|
while (port_get_raw_ticks(NULL) < deadline && (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP)) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP) {
|
|
|
|
common_hal_wifi_radio_stop_ap(self);
|
|
|
|
// This is needed since it leaves a broken AP up.
|
|
|
|
mp_raise_RuntimeError(translate("AP could not be started"));
|
|
|
|
}
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2022-12-26 12:28:44 -05:00
|
|
|
bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self) {
|
|
|
|
return cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) == CYW43_LINK_UP;
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
|
2022-11-17 14:47:39 -05:00
|
|
|
if (!common_hal_wifi_radio_get_enabled(self)) {
|
|
|
|
mp_raise_RuntimeError(translate("wifi is not enabled"));
|
|
|
|
}
|
|
|
|
|
2023-08-23 13:04:51 -04:00
|
|
|
cyw43_arch_disable_ap_mode();
|
|
|
|
|
|
|
|
const size_t timeout_ms = 500;
|
|
|
|
uint64_t start = port_get_raw_ticks(NULL);
|
|
|
|
uint64_t deadline = start + timeout_ms;
|
|
|
|
while (port_get_raw_ticks(NULL) < deadline && (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN)) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
2022-11-17 14:47:39 -05:00
|
|
|
}
|
|
|
|
|
2023-08-23 13:04:51 -04:00
|
|
|
bindings_cyw43_wifi_enforce_pm();
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:44:34 -05:00
|
|
|
static bool connection_unchanged(wifi_radio_obj_t *self, const uint8_t *ssid, size_t ssid_len) {
|
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ssid_len != self->connected_ssid_len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (memcmp(ssid, self->connected_ssid, self->connected_ssid_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len) {
|
|
|
|
if (!common_hal_wifi_radio_get_enabled(self)) {
|
2022-11-21 18:00:34 -05:00
|
|
|
mp_raise_RuntimeError(translate("Wifi is not enabled"));
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
2022-10-17 11:17:14 -04:00
|
|
|
|
2022-12-08 13:44:34 -05:00
|
|
|
if (ssid_len > 32) {
|
|
|
|
return WIFI_RADIO_ERROR_CONNECTION_FAIL;
|
|
|
|
}
|
2022-11-17 14:47:39 -05:00
|
|
|
|
2022-10-17 11:17:14 -04:00
|
|
|
size_t timeout_ms = timeout <= 0 ? 8000 : (size_t)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000);
|
|
|
|
uint64_t start = port_get_raw_ticks(NULL);
|
|
|
|
uint64_t deadline = start + timeout_ms;
|
|
|
|
|
2022-12-08 13:44:34 -05:00
|
|
|
if (connection_unchanged(self, ssid, ssid_len)) {
|
|
|
|
return WIFI_RADIO_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2022-10-22 04:58:44 -04:00
|
|
|
// disconnect
|
|
|
|
common_hal_wifi_radio_stop_station(self);
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
// connect
|
2023-01-10 16:03:06 -05:00
|
|
|
int auth_mode = password_len ? CYW43_AUTH_WPA2_AES_PSK : CYW43_AUTH_OPEN;
|
|
|
|
cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode);
|
2022-10-22 04:58:44 -04:00
|
|
|
// TODO: Implement authmode check like in espressif
|
2022-10-17 11:17:14 -04:00
|
|
|
|
|
|
|
while (port_get_raw_ticks(NULL) < deadline) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:33:32 -04:00
|
|
|
int result = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
2022-10-17 11:17:14 -04:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case CYW43_LINK_UP:
|
2022-12-08 13:44:34 -05:00
|
|
|
memcpy(self->connected_ssid, ssid, ssid_len);
|
|
|
|
self->connected_ssid_len = ssid_len;
|
2022-10-17 11:33:32 -04:00
|
|
|
bindings_cyw43_wifi_enforce_pm();
|
|
|
|
return WIFI_RADIO_ERROR_NONE;
|
|
|
|
case CYW43_LINK_FAIL:
|
2022-10-17 11:17:14 -04:00
|
|
|
return WIFI_RADIO_ERROR_CONNECTION_FAIL;
|
2022-10-17 11:33:32 -04:00
|
|
|
case CYW43_LINK_NONET:
|
2022-10-17 11:17:14 -04:00
|
|
|
return WIFI_RADIO_ERROR_NO_AP_FOUND;
|
2022-10-17 11:33:32 -04:00
|
|
|
case CYW43_LINK_BADAUTH:
|
2022-10-17 11:17:14 -04:00
|
|
|
return WIFI_RADIO_ERROR_AUTH_FAIL;
|
|
|
|
}
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
2022-10-17 11:17:14 -04:00
|
|
|
|
|
|
|
// Being here means we either timed out or got interrupted.
|
2022-10-17 11:33:32 -04:00
|
|
|
return WIFI_RADIO_ERROR_UNSPECIFIED;
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2022-12-26 12:28:44 -05:00
|
|
|
bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self) {
|
|
|
|
return cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) == CYW43_LINK_UP;
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
|
|
|
|
mp_raise_NotImplementedError(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_STA->gw.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_AP->gw.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_STA->netmask.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_AP->netmask.addr);
|
|
|
|
}
|
|
|
|
|
2022-11-18 14:21:02 -05:00
|
|
|
uint32_t wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
|
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return NETIF_STA->ip_addr.addr;
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_STA->ip_addr.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self) {
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(NETIF_AP->ip_addr.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) {
|
|
|
|
uint32_t addr = dns_getserver(0)->addr;
|
2022-10-17 11:08:50 -04:00
|
|
|
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_UP || addr == 0) {
|
2022-08-31 11:13:39 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return common_hal_ipaddress_new_ipv4address(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_set_ipv4_dns(wifi_radio_obj_t *self, mp_obj_t ipv4_dns_addr) {
|
2022-10-19 21:54:53 -04:00
|
|
|
ip4_addr_t addr;
|
|
|
|
ipaddress_ipaddress_to_lwip(ipv4_dns_addr, &addr);
|
|
|
|
dns_setserver(0, &addr);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_start_dhcp_client(wifi_radio_obj_t *self) {
|
2022-10-19 21:54:53 -04:00
|
|
|
dhcp_start(NETIF_STA);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self) {
|
2022-10-19 21:54:53 -04:00
|
|
|
dhcp_stop(NETIF_STA);
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 23:41:33 -04:00
|
|
|
void common_hal_wifi_radio_start_dhcp_server(wifi_radio_obj_t *self) {
|
2023-05-14 18:44:27 -04:00
|
|
|
ip4_addr_t ipv4_addr, netmask_addr;
|
|
|
|
ipaddress_ipaddress_to_lwip(common_hal_wifi_radio_get_ipv4_address_ap(self), &ipv4_addr);
|
|
|
|
ipaddress_ipaddress_to_lwip(common_hal_wifi_radio_get_ipv4_subnet_ap(self), &netmask_addr);
|
|
|
|
dhcp_server_init(&cyw43_state.dhcp_server, &ipv4_addr, &netmask_addr);
|
2023-05-08 23:41:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_stop_dhcp_server(wifi_radio_obj_t *self) {
|
2023-05-14 18:44:27 -04:00
|
|
|
dhcp_server_deinit(&cyw43_state.dhcp_server);
|
2023-05-08 23:41:33 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway, mp_obj_t ipv4_dns) {
|
2022-10-19 21:54:53 -04:00
|
|
|
common_hal_wifi_radio_stop_dhcp_client(self);
|
|
|
|
|
|
|
|
ip4_addr_t ipv4_addr, netmask_addr, gateway_addr;
|
|
|
|
ipaddress_ipaddress_to_lwip(ipv4, &ipv4_addr);
|
|
|
|
ipaddress_ipaddress_to_lwip(netmask, &netmask_addr);
|
|
|
|
ipaddress_ipaddress_to_lwip(gateway, &gateway_addr);
|
|
|
|
netif_set_addr(NETIF_STA, &ipv4_addr, &netmask_addr, &gateway_addr);
|
|
|
|
if (ipv4_dns != MP_OBJ_NULL) {
|
|
|
|
common_hal_wifi_radio_set_ipv4_dns(self, ipv4_dns);
|
|
|
|
}
|
2022-08-31 11:13:39 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 23:41:33 -04:00
|
|
|
void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway) {
|
2023-05-13 23:06:16 -04:00
|
|
|
common_hal_wifi_radio_stop_dhcp_server(self);
|
|
|
|
|
|
|
|
ip4_addr_t ipv4_addr, netmask_addr, gateway_addr;
|
|
|
|
ipaddress_ipaddress_to_lwip(ipv4, &ipv4_addr);
|
|
|
|
ipaddress_ipaddress_to_lwip(netmask, &netmask_addr);
|
|
|
|
ipaddress_ipaddress_to_lwip(gateway, &gateway_addr);
|
|
|
|
netif_set_addr(NETIF_AP, &ipv4_addr, &netmask_addr, &gateway_addr);
|
|
|
|
|
|
|
|
common_hal_wifi_radio_start_dhcp_server(self);
|
2023-05-08 23:41:33 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
volatile bool ping_received;
|
2022-11-14 12:31:18 -05:00
|
|
|
uint32_t ping_time;
|
2022-08-31 11:13:39 -04:00
|
|
|
|
|
|
|
static u8_t
|
|
|
|
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) {
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
LWIP_ASSERT("p != NULL", p != NULL);
|
|
|
|
|
|
|
|
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
|
|
|
|
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
|
2022-11-14 12:31:18 -05:00
|
|
|
|
2022-08-31 11:13:39 -04:00
|
|
|
iecho = (struct icmp_echo_hdr *)p->payload;
|
|
|
|
|
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
|
|
|
|
LWIP_DEBUGF(PING_DEBUG, ("ping: recv "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, addr);
|
|
|
|
LWIP_DEBUGF(PING_DEBUG, (" %"U32_F " ms\n", (sys_now() - ping_time)));
|
|
|
|
|
|
|
|
/* do some ping result processing */
|
|
|
|
ping_received = true;
|
|
|
|
pbuf_free(p);
|
|
|
|
return 1; /* eat the packet */
|
|
|
|
}
|
|
|
|
/* not eaten, restore original packet */
|
|
|
|
pbuf_add_header(p, PBUF_IP_HLEN);
|
|
|
|
}
|
|
|
|
return 0; /* don't eat the packet */
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {
|
2022-11-14 12:31:18 -05:00
|
|
|
ping_time = sys_now();
|
2022-08-31 11:13:39 -04:00
|
|
|
ip_addr_t ping_addr;
|
|
|
|
ipaddress_ipaddress_to_lwip(ip_address, &ping_addr);
|
|
|
|
|
|
|
|
struct raw_pcb *ping_pcb;
|
2022-09-17 10:01:31 -04:00
|
|
|
MICROPY_PY_LWIP_ENTER
|
|
|
|
ping_pcb = raw_new(IP_PROTO_ICMP);
|
2022-08-31 11:13:39 -04:00
|
|
|
if (!ping_pcb) {
|
2022-09-17 10:01:31 -04:00
|
|
|
MICROPY_PY_LWIP_EXIT
|
2022-08-31 11:13:39 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
raw_recv(ping_pcb, ping_recv, NULL);
|
|
|
|
raw_bind(ping_pcb, IP_ADDR_ANY);
|
2022-09-17 10:01:31 -04:00
|
|
|
MICROPY_PY_LWIP_EXIT
|
2022-08-31 11:13:39 -04:00
|
|
|
|
2022-09-17 10:01:31 -04:00
|
|
|
ping_received = false;
|
2022-08-31 11:13:39 -04:00
|
|
|
ping_send(ping_pcb, &ping_addr);
|
|
|
|
size_t timeout_ms = (size_t)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000);
|
|
|
|
uint64_t start = port_get_raw_ticks(NULL);
|
|
|
|
uint64_t deadline = start + timeout_ms;
|
|
|
|
while (port_get_raw_ticks(NULL) < deadline && !ping_received) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_int_t result = -1;
|
|
|
|
if (ping_received) {
|
|
|
|
uint64_t now = port_get_raw_ticks(NULL);
|
|
|
|
result = now - start;
|
|
|
|
}
|
|
|
|
|
2022-09-17 10:01:31 -04:00
|
|
|
MICROPY_PY_LWIP_ENTER;
|
2022-08-31 11:13:39 -04:00
|
|
|
raw_remove(ping_pcb);
|
2022-09-17 10:01:31 -04:00
|
|
|
MICROPY_PY_LWIP_EXIT;
|
2022-08-31 11:13:39 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self) {
|
|
|
|
// Only bother to scan the actual object references.
|
|
|
|
gc_collect_ptr(self->current_scan);
|
|
|
|
}
|