Skip to content

Commit 9475cc5

Browse files
dpgeorgepfalcon
authored andcommitted
esp8266: Support synchronous wifi scanning.
That is: aps = if0.scan() TODO: make sure that returned list has tuple with values in "standard" order (whatever that standard is).
1 parent 2599672 commit 9475cc5

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

esp8266/esp_mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ uint32_t mp_hal_get_cpu_freq(void);
4444
void uart_task_init();
4545

4646
void ets_event_poll(void);
47+
#define ETS_POLL_WHILE(cond) { while (cond) ets_event_poll(); }
4748

4849
#endif // _INCLUDED_MPHAL_H_

esp8266/modnetwork.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
#include "py/nlr.h"
3333
#include "py/objlist.h"
3434
#include "py/runtime.h"
35+
#include "py/mphal.h"
3536
#include "netutils.h"
3637
#include "queue.h"
3738
#include "user_interface.h"
3839
#include "espconn.h"
3940
#include "spi_flash.h"
40-
#include "utils.h"
41+
#include "ets_alt_task.h"
4142

4243
#define MODNETWORK_INCLUDE_CONSTANTS (1)
4344

@@ -127,9 +128,15 @@ STATIC mp_obj_t esp_status(mp_obj_t self_in) {
127128
}
128129
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_status_obj, esp_status);
129130

131+
STATIC mp_obj_t *esp_scan_list = NULL;
132+
130133
STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
131-
struct bss_info *bs;
132-
if (si->pbss) {
134+
if (esp_scan_list == NULL) {
135+
// called unexpectedly
136+
return;
137+
}
138+
if (si->pbss && status == 0) {
139+
struct bss_info *bs;
133140
STAILQ_FOREACH(bs, si->pbss, next) {
134141
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
135142
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
@@ -138,21 +145,30 @@ STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
138145
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
139146
t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
140147
t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
141-
call_function_1_protected(MP_STATE_PORT(scan_cb_obj), t);
148+
mp_obj_list_append(*esp_scan_list, MP_OBJ_FROM_PTR(t));
142149
}
150+
} else {
151+
// indicate error
152+
*esp_scan_list = MP_OBJ_NULL;
143153
}
154+
esp_scan_list = NULL;
144155
}
145156

146-
STATIC mp_obj_t esp_scan(mp_obj_t self_in, mp_obj_t cb_in) {
147-
MP_STATE_PORT(scan_cb_obj) = cb_in;
157+
STATIC mp_obj_t esp_scan(mp_obj_t self_in) {
148158
if (wifi_get_opmode() == SOFTAP_MODE) {
149-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
150-
"Scan not supported in AP mode"));
159+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
160+
"scan unsupported in AP mode"));
151161
}
162+
mp_obj_t list = mp_obj_new_list(0, NULL);
163+
esp_scan_list = &list;
152164
wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
153-
return mp_const_none;
165+
ETS_POLL_WHILE(esp_scan_list != NULL);
166+
if (list == MP_OBJ_NULL) {
167+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "scan failed"));
168+
}
169+
return list;
154170
}
155-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_scan_obj, esp_scan);
171+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
156172

157173
/// \method isconnected()
158174
/// Return True if connected to an AP and an IP address has been assigned,

esp8266/mpconfigport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ extern const struct _mp_obj_module_t onewire_module;
127127
#define MICROPY_PORT_ROOT_POINTERS \
128128
const char *readline_hist[8]; \
129129
mp_obj_t mp_kbd_exception; \
130-
\
131-
/* Singleton instance of scan callback, meaning that there can
132-
be only one concurrent AP scan. */ \
133-
mp_obj_t scan_cb_obj; \
134130

135131
// We need to provide a declaration/definition of alloca()
136132
#include <alloca.h>

0 commit comments

Comments
 (0)