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}
128129STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_status_obj , esp_status );
129130
131+ STATIC mp_obj_t * esp_scan_list = NULL ;
132+
130133STATIC 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,
0 commit comments