@@ -173,24 +173,50 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
173
173
if (!common_hal_wifi_radio_get_enabled (self )) {
174
174
mp_raise_RuntimeError (translate ("wifi is not enabled" ));
175
175
}
176
- unsigned timeout_ms = timeout <= 0 ? 8000 : (unsigned )MAX (0 , MICROPY_FLOAT_C_FUN (ceil )(timeout * 1000 ));
177
- // TODO use connect_async so we can service bg tasks & check for ctrl-c during
176
+
177
+ size_t timeout_ms = timeout <= 0 ? 8000 : (size_t )MICROPY_FLOAT_C_FUN (ceil )(timeout * 1000 );
178
+ uint64_t start = port_get_raw_ticks (NULL );
179
+ uint64_t deadline = start + timeout_ms ;
180
+
178
181
// connect
179
- int result = cyw43_arch_wifi_connect_timeout_ms ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK , timeout_ms );
180
- bindings_cyw43_wifi_enforce_pm ();
182
+ cyw43_arch_wifi_connect_async ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
183
+
184
+ while (port_get_raw_ticks (NULL ) < deadline ) {
185
+ RUN_BACKGROUND_TASKS ;
186
+ if (mp_hal_is_interrupted ()) {
187
+ break ;
188
+ }
189
+
190
+ int result = cyw43_wifi_link_status (& cyw43_state , CYW43_ITF_STA );
191
+
192
+ // While the async connection is running, it will return CYW43_LINK_JOIN.
193
+
194
+ int done = 0 ;
195
+ switch (result ) {
196
+ case CYW43_LINK_UP :
197
+ done = 1 ;
198
+ break ;
199
+ case (unsigned int )CYW43_LINK_FAIL :
200
+ return WIFI_RADIO_ERROR_CONNECTION_FAIL ;
201
+ case (unsigned int )CYW43_LINK_NONET :
202
+ return WIFI_RADIO_ERROR_NO_AP_FOUND ;
203
+ case (unsigned int )CYW43_LINK_BADAUTH :
204
+ return WIFI_RADIO_ERROR_AUTH_FAIL ;
205
+ }
206
+
207
+ if (done == 1 ) {
208
+ break ;
209
+ }
210
+ }
211
+
212
+ // Being here means we either timed out or got interrupted.
213
+ int result = cyw43_wifi_link_status (& cyw43_state , CYW43_ITF_STA );
181
214
switch (result ) {
182
- case 0 :
215
+ case CYW43_LINK_UP :
216
+ case CYW43_LINK_JOIN :
217
+ // If CYW43_LINK_JOIN wasn't here, it wouldn't work even though we have ip.
218
+ bindings_cyw43_wifi_enforce_pm ();
183
219
return WIFI_RADIO_ERROR_NONE ;
184
- // case CYW43_LINK_DOWN:
185
- // case CYW43_LINK_JOIN:
186
- // case CYW43_LINK_NOIP:
187
- // case CYW43_LINK_UP:
188
- case CYW43_LINK_FAIL :
189
- return WIFI_RADIO_ERROR_CONNECTION_FAIL ;
190
- case CYW43_LINK_NONET :
191
- return WIFI_RADIO_ERROR_NO_AP_FOUND ;
192
- case CYW43_LINK_BADAUTH :
193
- return WIFI_RADIO_ERROR_AUTH_FAIL ;
194
220
195
221
default :
196
222
return WIFI_RADIO_ERROR_UNSPECIFIED ;
0 commit comments