@@ -72,7 +72,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
72
72
return mp_const_none ;
73
73
}
74
74
// If we are scanning, wait and then load them.
75
- if (self -> scanning ) {
75
+ if (self -> channel_scan_in_progress ) {
76
76
// We may have to scan more than one channel to get a result.
77
77
while (!self -> done ) {
78
78
if (!wifi_scannednetworks_wait_for_scan (self )) {
@@ -81,7 +81,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
81
81
}
82
82
83
83
esp_wifi_scan_get_ap_num (& self -> total_results );
84
- self -> scanning = false;
84
+ self -> channel_scan_in_progress = false;
85
85
if (self -> total_results > 0 ) {
86
86
break ;
87
87
}
@@ -112,7 +112,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
112
112
}
113
113
}
114
114
esp_wifi_scan_get_ap_records (& self -> total_results , self -> results );
115
- self -> scanning = false;
115
+ self -> channel_scan_in_progress = false;
116
116
}
117
117
118
118
wifi_network_obj_t * entry = m_new_obj (wifi_network_obj_t );
@@ -132,40 +132,49 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
132
132
}
133
133
134
134
// We don't do a linear scan so that we look at a variety of spectrum up front.
135
- static uint8_t scan_pattern [] = {6 , 1 , 11 , 3 , 9 , 13 , 2 , 4 , 8 , 12 , 5 , 7 , 10 , 14 };
135
+ static uint8_t scan_pattern [] = {6 , 1 , 11 , 3 , 9 , 13 , 2 , 4 , 8 , 12 , 5 , 7 , 10 , 14 , 0 };
136
136
137
137
void wifi_scannednetworks_scan_next_channel (wifi_scannednetworks_obj_t * self ) {
138
- uint8_t next_channel = sizeof (scan_pattern );
138
+ // There is no channel 0, so use that as a flag to indicate we've run out of channels to scan.
139
+ uint8_t next_channel = 0 ;
139
140
while (self -> current_channel_index < sizeof (scan_pattern )) {
140
141
next_channel = scan_pattern [self -> current_channel_index ];
141
142
self -> current_channel_index ++ ;
143
+ // Scan only channels that are in the specified range.
142
144
if (self -> start_channel <= next_channel && next_channel <= self -> end_channel ) {
143
145
break ;
144
146
}
145
147
}
146
148
wifi_scan_config_t config = { 0 };
147
149
config .channel = next_channel ;
148
- if (next_channel == sizeof ( scan_pattern ) ) {
150
+ if (next_channel == 0 ) {
149
151
wifi_scannednetworks_done (self );
150
152
} else {
151
153
esp_err_t result = esp_wifi_scan_start (& config , false);
152
154
if (result != ESP_OK ) {
153
155
wifi_scannednetworks_done (self );
154
156
} else {
155
- self -> scanning = true;
157
+ self -> channel_scan_in_progress = true;
156
158
}
157
159
}
158
160
}
159
161
160
162
void wifi_scannednetworks_deinit (wifi_scannednetworks_obj_t * self ) {
161
163
// if a scan is active, make sure and clean up the idf's buffer of results.
162
- if (self -> scanning ) {
164
+ if (self -> channel_scan_in_progress ) {
163
165
esp_wifi_scan_stop ();
164
166
if (wifi_scannednetworks_wait_for_scan (self )) {
165
- // Ignore the number of records since we're throwing them away.
166
- uint16_t number = 0 ;
167
- esp_wifi_scan_get_ap_records (& number , NULL );
168
- self -> scanning = false;
167
+ // Discard the scanned records, one at a time, to avoid memory leaks.
168
+ uint16_t number ;
169
+ do {
170
+ number = 1 ;
171
+ wifi_ap_record_t record ;
172
+ esp_wifi_scan_get_ap_records (& number , & record );
173
+ } while (number > 0 );
174
+ // TODO: available in ESP-IDF v5.0; do instead of the above.
175
+ // Discard scan results.
176
+ // esp_wifi_clear_ap_list();
177
+ self -> channel_scan_in_progress = false;
169
178
}
170
179
}
171
180
wifi_scannednetworks_done (self );
0 commit comments