Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@
[submodule "ports/raspberrypi/lib/cyw43-driver"]
path = ports/raspberrypi/lib/cyw43-driver
url = https://github.com/adafruit/cyw43-driver
branch = main
branch = circuitpython11
[submodule "ports/raspberrypi/lib/lwip"]
path = ports/raspberrypi/lib/lwip
url = https://github.com/adafruit/lwip.git
Expand Down
2 changes: 2 additions & 0 deletions ports/raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ SRC_SDK := \

SRC_SDK := $(addprefix sdk/, $(SRC_SDK))
$(patsubst %.c,$(BUILD)/%.o,$(SRC_SDK) $(SRC_CYW43)): CFLAGS += -Wno-missing-prototypes -Wno-undef -Wno-unused-function -Wno-nested-externs -Wno-strict-prototypes -Wno-double-promotion -Wno-sign-compare -Wno-unused-variable -Wno-strict-overflow -Ilib/cyw43-driver
# cyw43_ll.c assigns string literals to a non-const pointer in its scan IE parser.
$(BUILD)/lib/cyw43-driver/src/cyw43_ll.o: CFLAGS += -Wno-discarded-qualifiers

SRC_C += \
boards/$(BOARD)/board.c \
Expand Down
23 changes: 14 additions & 9 deletions ports/raspberrypi/common-hal/wifi/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,27 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) {
}

mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) {
uint32_t auth = self->record.auth_mode;
uint32_t authmode_mask = 0;
if (self->record.auth_mode == 0) {
if (auth == 0) {
authmode_mask = AUTHMODE_OPEN;
}
if (self->record.auth_mode & 1) {
authmode_mask |= AUTHMODE_PSK;
}
;
if (self->record.auth_mode & 2) {
if (auth & CYW43_AUTH_FLAG_WPA_SECURITY) {
authmode_mask |= AUTHMODE_WPA;
}
;
if (self->record.auth_mode & 4) {
if (auth & CYW43_AUTH_FLAG_WPA2_SECURITY) {
authmode_mask |= AUTHMODE_WPA2;
}
;
if (auth & CYW43_AUTH_FLAG_WPA3_SECURITY) {
authmode_mask |= AUTHMODE_WPA3;
}
if (auth & CYW43_AUTH_FLAG_ENTERPRISE_ENABLED) {
authmode_mask |= AUTHMODE_ENTERPRISE;
} else if (authmode_mask & (AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_WPA3)) {
authmode_mask |= AUTHMODE_PSK;
} else if (auth & CYW43_AUTH_FLAG_WEP_ENABLED) {
authmode_mask |= AUTHMODE_WEP;
}
mp_obj_t authmode_list = mp_obj_new_list(0, NULL);
if (authmode_mask != 0) {
for (uint32_t i = 0; i < 32; i++) {
Expand Down
53 changes: 47 additions & 6 deletions ports/raspberrypi/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,41 @@ static bool connection_unchanged(wifi_radio_obj_t *self, const uint8_t *ssid, si
return true;
}

// Static, not on the stack: results can arrive after scan_for_auth() gives up.
static uint32_t scan_auth;
static uint8_t scan_ssid[32];
static size_t scan_ssid_len;

static int scan_for_auth_cb(void *env, const cyw43_ev_scan_result_t *result) {
// Only count results for the SSID being joined.
if (result->ssid_len == scan_ssid_len && memcmp(result->ssid, scan_ssid, scan_ssid_len) == 0) {
scan_auth |= result->auth_mode;
}
return 0;
}

// Scan for one SSID. Returns its scan result auth flags, or 0 if not seen.
static uint32_t scan_for_auth(wifi_radio_obj_t *self, const uint8_t *ssid, size_t ssid_len, uint64_t deadline) {
// A user scan owns the driver's scan callback.
if (self->current_scan != NULL) {
return 0;
}
cyw43_wifi_scan_options_t opts = {0};
opts.ssid_len = ssid_len;
memcpy(opts.ssid, ssid, ssid_len);
memcpy(scan_ssid, ssid, ssid_len);
scan_ssid_len = ssid_len;
scan_auth = 0;
if (cyw43_wifi_scan(&cyw43_state, &opts, NULL, scan_for_auth_cb) != 0) {
return 0;
}
while (cyw43_wifi_scan_active(&cyw43_state) && port_get_raw_ticks(NULL) < deadline &&
!mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
}
return scan_auth;
}

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)) {
mp_raise_RuntimeError(MP_ERROR_TEXT("WiFi is not enabled"));
Expand All @@ -358,13 +393,20 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
// disconnect
common_hal_wifi_radio_stop_station(self);

// Joining with WPA3 SAE fails on a WPA2-only network, so scan for the
// network first and use WPA3 only if it advertises it.
uint32_t auth_mode = CYW43_AUTH_OPEN;
if (password_len) {
uint32_t ap_auth = scan_for_auth(self, ssid, ssid_len, deadline);
auth_mode = (ap_auth & CYW43_AUTH_FLAG_WPA3_SECURITY) ?
CYW43_AUTH_WPA3_SAE_AES_PSK : CYW43_AUTH_WPA2_AES_PSK;
}

// connect. Record the network now: the join carries on after a timeout.
memcpy(self->connected_ssid, ssid, ssid_len);
self->connected_ssid_len = ssid_len;
self->connected_with_password = password_len != 0;
int auth_mode = password_len ? CYW43_AUTH_WPA2_AES_PSK : CYW43_AUTH_OPEN;
self->connected_auth_mode = auth_mode;
cyw43_arch_wifi_connect_async((const char *)ssid, (const char *)password, auth_mode);
// TODO: Implement authmode check like in espressif

while (port_get_raw_ticks(NULL) < deadline) {
RUN_BACKGROUND_TASKS;
Expand Down Expand Up @@ -417,9 +459,8 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {

cyw43_wifi_get_bssid(&cyw43_state, record.bssid);

// connect() joins with either CYW43_AUTH_OPEN or CYW43_AUTH_WPA2_AES_PSK.
// Encode that with the scan result bits from cyw43_ll.c: 1 is privacy, 4 is WPA2.
record.auth_mode = self->connected_with_password ? (1 | 4) : 0;
// CYW43_AUTH_* values use the same flags as a scan result's auth_mode.
record.auth_mode = self->connected_auth_mode;

wifi_network_obj_t *ap_info = mp_obj_malloc(wifi_network_obj_t, &wifi_network_type);
ap_info->record = record;
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct {
wifi_scannednetworks_obj_t *current_scan;
uint8_t connected_ssid[32];
uint8_t connected_ssid_len;
bool connected_with_password;
uint32_t connected_auth_mode; // CYW43_AUTH_* passed to the last join
bool enabled;
} wifi_radio_obj_t;

Expand Down
4 changes: 4 additions & 0 deletions ports/raspberrypi/cyw43_configport.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#define CYW43_NETUTILS (1)

// Keep driver warnings off the serial console. Every scan warns once per network
// that offers a cipher the driver does not know, such as GCMP on WPA3 routers.
#define CYW43_WARN(...) ((void)0)

#if CIRCUITPY_USB_DEVICE
#include "supervisor/usb.h"
#define CYW43_EVENT_POLL_HOOK usb_background();
Expand Down
Loading