@@ -128,16 +128,16 @@ STATIC const wlan_if_obj_t wlan_ap_obj = {{&wlan_if_type}, WIFI_IF_AP};
128
128
// Set to "true" if esp_wifi_start() was called
129
129
static bool wifi_started = false;
130
130
131
- // Set to "true" if the STA interface completed a scan for APs.
132
- static bool wifi_sta_scan_done = false;
133
-
134
131
// Set to "true" if the STA interface is requested to be connected by the
135
132
// user, used for automatic reassociation.
136
133
static bool wifi_sta_connect_requested = false;
137
134
138
135
// Set to "true" if the STA interface is connected to wifi and has IP address.
139
136
static bool wifi_sta_connected = false;
140
137
138
+ // Set to "true" to run a blocking wifi scan, false for non-blocking
139
+ static bool wifi_sta_scan_blocking = true;
140
+
141
141
// Store the current status. 0 means None here, safe to do so as first enum value is WIFI_REASON_UNSPECIFIED=1.
142
142
static uint8_t wifi_sta_disconn_reason = 0 ;
143
143
@@ -149,6 +149,21 @@ static bool mdns_initialised = false;
149
149
static uint8_t conf_wifi_sta_reconnects = 0 ;
150
150
static uint8_t wifi_sta_reconnects ;
151
151
152
+ STATIC mp_obj_t esp_get_scan_results (void );
153
+
154
+ // This function is scheduled when a SCAN_DONE event is emitted after a non-blocking scan.
155
+ // It allows for retrieving AP records in a list in the context of the scheduler so the
156
+ // list can be passed to a user-supplied callback in esp_scan.
157
+ STATIC mp_obj_t esp_wifi_scan_cb (mp_obj_t arg ) {
158
+ ESP_LOGI ("wifi" , "Scan callback ran" );
159
+ mp_obj_t handler = MP_STATE_PORT (esp_wifi_scan_cb_handler );
160
+ mp_obj_t scan_results = esp_get_scan_results ();
161
+ mp_sched_schedule (handler , scan_results );
162
+ return mp_const_none ;
163
+ }
164
+
165
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_wifi_scan_cb_obj , esp_wifi_scan_cb );
166
+
152
167
// This function is called by the system-event task and so runs in a different
153
168
// thread to the main MicroPython task. It must not raise any Python exceptions.
154
169
static esp_err_t event_handler (void * ctx , system_event_t * event ) {
@@ -159,7 +174,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
159
174
break ;
160
175
case SYSTEM_EVENT_SCAN_DONE :
161
176
ESP_LOGI ("wifi" , "SCAN_DONE" );
162
- wifi_sta_scan_done = true;
177
+ if (!wifi_sta_scan_blocking ) {
178
+ mp_sched_schedule (MP_OBJ_FROM_PTR (& esp_wifi_scan_cb_obj ), mp_const_none );
179
+ }
163
180
break ;
164
181
case SYSTEM_EVENT_STA_CONNECTED :
165
182
ESP_LOGI ("network" , "CONNECTED" );
@@ -300,6 +317,7 @@ STATIC mp_obj_t esp_initialize() {
300
317
ESP_LOGD ("modnetwork" , "Initializing Event Loop" );
301
318
ESP_EXCEPTIONS (esp_event_loop_init (event_handler , NULL ));
302
319
ESP_LOGD ("modnetwork" , "esp_event_loop_init done" );
320
+ memset (& MP_STATE_PORT (esp_wifi_scan_cb_handler ), 0 , sizeof (MP_STATE_PORT (esp_wifi_scan_cb_handler )));
303
321
initialized = 1 ;
304
322
}
305
323
return mp_const_none ;
@@ -465,17 +483,12 @@ STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
465
483
466
484
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_status_obj , 1 , 2 , esp_status );
467
485
468
- STATIC mp_obj_t esp_get_scan_results (mp_obj_t self_in ) {
469
- if (!wifi_sta_scan_done ) {
470
- ESP_LOGI ("wifi" , "Scan not complete" );
471
- return mp_const_none ;
472
- }
486
+ STATIC mp_obj_t esp_get_scan_results () {
473
487
mp_obj_t list = mp_obj_new_list (0 , NULL );
474
488
uint16_t count = 0 ;
475
489
ESP_EXCEPTIONS (esp_wifi_scan_get_ap_num (& count ));
476
490
if (count == 0 ) {
477
491
ESP_LOGI ("wifi" , "No AP records found" );
478
- wifi_sta_scan_done = false;
479
492
return list ;
480
493
}
481
494
wifi_ap_record_t * wifi_ap_records = calloc (count , sizeof (wifi_ap_record_t ));
@@ -493,19 +506,20 @@ STATIC mp_obj_t esp_get_scan_results(mp_obj_t self_in) {
493
506
mp_obj_list_append (list , MP_OBJ_FROM_PTR (t ));
494
507
}
495
508
free (wifi_ap_records );
496
- wifi_sta_scan_done = false;
497
509
return list ;
498
510
}
499
511
500
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_get_scan_results_obj , esp_get_scan_results );
501
-
502
512
STATIC mp_obj_t esp_scan (size_t n_args , const mp_obj_t * args ) {
503
- mp_obj_t * self_in = MP_OBJ_TO_PTR (args [0 ]);
504
- bool blocking = true;
513
+ wifi_sta_scan_blocking = true;
505
514
506
515
// If 1 arg or 2nd arg is True, blocking scan
507
516
if (n_args > 1 ) {
508
- blocking = mp_obj_is_true (args [1 ]);
517
+ if (!mp_obj_is_callable (args [1 ])) {
518
+ mp_raise_ValueError (MP_ERROR_TEXT ("Invalid callback, must be callable" ));
519
+ }
520
+ MP_STATE_PORT (esp_wifi_scan_cb_handler ) = args [1 ];
521
+ wifi_sta_scan_blocking = false;
522
+ ESP_LOGI ("wifi" , "Non-blocking scan" );
509
523
}
510
524
511
525
// check that STA mode is active
@@ -518,21 +532,17 @@ STATIC mp_obj_t esp_scan(size_t n_args, const mp_obj_t *args) {
518
532
wifi_scan_config_t config = { 0 };
519
533
// XXX how do we scan hidden APs (and if we can scan them, are they really hidden?)
520
534
521
- if ( blocking ) {
522
- MP_THREAD_GIL_EXIT ( );
523
- esp_err_t status = esp_wifi_scan_start ( & config , 1 );
524
- MP_THREAD_GIL_ENTER ( );
525
-
526
- if (status == 0 ) {
527
- return esp_get_scan_results ( self_in );
528
- }
535
+ MP_THREAD_GIL_EXIT ();
536
+ esp_err_t status = esp_wifi_scan_start ( & config , wifi_sta_scan_blocking );
537
+ MP_THREAD_GIL_ENTER ( );
538
+ ESP_EXCEPTIONS ( status );
539
+
540
+ if (wifi_sta_scan_blocking ) {
541
+ ESP_LOGI ( "wifi" , "blocking scan calling get_results" );
542
+ return esp_get_scan_results ();
529
543
} else {
530
- ESP_LOGI ("wifi" , "Attempting non-blocking scan" );
531
- ESP_EXCEPTIONS (esp_wifi_scan_start (& config , 0 ));
544
+ return mp_const_none ;
532
545
}
533
-
534
- wifi_sta_scan_done = false;
535
- return mp_const_none ;
536
546
}
537
547
538
548
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_scan_obj , 1 , 2 , esp_scan );
@@ -802,7 +812,6 @@ STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
802
812
{ MP_ROM_QSTR (MP_QSTR_isconnected ), MP_ROM_PTR (& esp_isconnected_obj ) },
803
813
{ MP_ROM_QSTR (MP_QSTR_config ), MP_ROM_PTR (& esp_config_obj ) },
804
814
{ MP_ROM_QSTR (MP_QSTR_ifconfig ), MP_ROM_PTR (& esp_ifconfig_obj ) },
805
- { MP_ROM_QSTR (MP_QSTR_get_scan_results ), MP_ROM_PTR (& esp_get_scan_results_obj ) },
806
815
};
807
816
808
817
STATIC MP_DEFINE_CONST_DICT (wlan_if_locals_dict , wlan_if_locals_dict_table );
0 commit comments