Skip to content

Commit 30691ed

Browse files
committed
drivers/cyw43: Make wifi join fail if interface is not active.
Otherwise the Python network object continues to report that it is attempting to connect. Also make the return error code consistent with wifi scan. Signed-off-by: Damien George <[email protected]>
1 parent 52a78e6 commit 30691ed

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

drivers/cyw43/cyw43_ctrl.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdio.h>
2828
#include <string.h>
2929

30+
#include "py/mperrno.h"
3031
#include "py/mphal.h"
3132
#include "drivers/cyw43/cyw43.h"
3233
#include "pendsv.h"
@@ -52,6 +53,9 @@
5253
#define WIFI_JOIN_STATE_KEYED (0x0800)
5354
#define WIFI_JOIN_STATE_ALL (0x0e01)
5455

56+
#define CYW43_STA_IS_ACTIVE(self) (((self)->itf_state >> CYW43_ITF_STA) & 1)
57+
#define CYW43_AP_IS_ACTIVE(self) (((self)->itf_state >> CYW43_ITF_AP) & 1)
58+
5559
cyw43_t cyw43_state;
5660
void (*cyw43_poll)(void);
5761
uint32_t cyw43_sleep;
@@ -475,7 +479,7 @@ void cyw43_wifi_set_up(cyw43_t *self, int itf, bool up) {
475479

476480
int cyw43_wifi_scan(cyw43_t *self, cyw43_wifi_scan_options_t *opts, void *env, int (*result_cb)(void*, const cyw43_ev_scan_result_t*)) {
477481
if (self->itf_state == 0) {
478-
return -1;
482+
return -MP_EPERM;
479483
}
480484

481485
cyw43_ensure_up(self);
@@ -518,6 +522,10 @@ int cyw43_wifi_link_status(cyw43_t *self, int itf) {
518522
// WiFi STA
519523

520524
int cyw43_wifi_join(cyw43_t *self, size_t ssid_len, const uint8_t *ssid, size_t key_len, const uint8_t *key, uint32_t auth_type, const uint8_t *bssid, uint32_t channel) {
525+
if (!CYW43_STA_IS_ACTIVE(self)) {
526+
return -MP_EPERM;
527+
}
528+
521529
int ret = cyw43_ensure_up(self);
522530
if (ret) {
523531
return ret;

extmod/network_cyw43.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ STATIC mp_obj_t network_cyw43_scan(size_t n_args, const mp_obj_t *pos_args, mp_m
196196
int scan_res = cyw43_wifi_scan(self->cyw, &opts, MP_OBJ_TO_PTR(res), network_cyw43_scan_cb);
197197

198198
if (scan_res < 0) {
199-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("STA must be active"));
199+
mp_raise_OSError(-scan_res);
200200
}
201201

202202
// Wait for scan to finish, with a 10s timeout
@@ -240,7 +240,7 @@ STATIC mp_obj_t network_cyw43_connect(size_t n_args, const mp_obj_t *pos_args, m
240240
}
241241
int ret = cyw43_wifi_join(self->cyw, ssid.len, ssid.buf, key.len, key.buf, args[ARG_auth].u_int, bssid.buf, args[ARG_channel].u_int);
242242
if (ret != 0) {
243-
mp_raise_OSError(ret);
243+
mp_raise_OSError(-ret);
244244
}
245245
return mp_const_none;
246246
}

0 commit comments

Comments
 (0)