@@ -157,11 +157,6 @@ void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
157157void common_hal_wifi_radio_stop_station (wifi_radio_obj_t * self ) {
158158
159159 cyw43_wifi_leave (& cyw43_state , CYW43_ITF_STA );
160- // This is wrong, but without this call the state of ITF_STA is still
161- // reported as CYW43_LINK_JOIN (by wifi_link_status) and CYW43_LINK_UP
162- // (by tcpip_link_status). However since ap disconnection isn't working
163- // either, this is not an issue.
164- cyw43_wifi_leave (& cyw43_state , CYW43_ITF_AP );
165160 const size_t timeout_ms = 500 ;
166161 uint64_t start = port_get_raw_ticks (NULL );
167162 uint64_t deadline = start + timeout_ms ;
@@ -179,20 +174,45 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
179174 mp_raise_RuntimeError (translate ("Wifi is not enabled" ));
180175 }
181176
182- if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_STA ) != CYW43_LINK_DOWN ) {
183- mp_raise_RuntimeError (translate ("Wifi is in station mode." ));
184- }
177+ /* TODO: If the AP is stopped once it cannot be restarted.
178+ * This means that if if the user does:
179+ *
180+ * wifi.radio.start_ap(...)
181+ * wifi.radio.stop_ap()
182+ * wifi.radio.start_ap(...)
183+ *
184+ * The second start_ap will fail.
185+ */
185186
186187 common_hal_wifi_radio_stop_ap (self );
187188
188189 // Channel can only be changed after initial powerup and config of ap.
189190 // Defaults to 1 if not set or invalid (i.e. 13)
190191 cyw43_wifi_ap_set_channel (& cyw43_state , (const uint32_t )channel );
191192
192- cyw43_arch_enable_ap_mode ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
193+ if (password_len ) {
194+ cyw43_arch_enable_ap_mode ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
195+ } else {
196+ cyw43_arch_enable_ap_mode ((const char * )ssid , NULL , CYW43_AUTH_OPEN );
197+ }
193198
194199 // TODO: Implement authmode check like in espressif
195200 bindings_cyw43_wifi_enforce_pm ();
201+
202+ const size_t timeout_ms = 500 ;
203+ uint64_t start = port_get_raw_ticks (NULL );
204+ uint64_t deadline = start + timeout_ms ;
205+ while (port_get_raw_ticks (NULL ) < deadline && (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_UP )) {
206+ RUN_BACKGROUND_TASKS ;
207+ if (mp_hal_is_interrupted ()) {
208+ break ;
209+ }
210+ }
211+ if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_UP ) {
212+ common_hal_wifi_radio_stop_ap (self );
213+ // This is needed since it leaves a broken AP up.
214+ mp_raise_RuntimeError (translate ("AP could not be started" ));
215+ }
196216}
197217
198218bool common_hal_wifi_radio_get_ap_active (wifi_radio_obj_t * self ) {
@@ -204,19 +224,19 @@ void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
204224 mp_raise_RuntimeError (translate ("wifi is not enabled" ));
205225 }
206226
207- if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_DOWN ) {
208- mp_raise_NotImplementedError (translate ("Stopping AP is not supported." ));
227+ cyw43_arch_disable_ap_mode ();
228+
229+ const size_t timeout_ms = 500 ;
230+ uint64_t start = port_get_raw_ticks (NULL );
231+ uint64_t deadline = start + timeout_ms ;
232+ while (port_get_raw_ticks (NULL ) < deadline && (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_DOWN )) {
233+ RUN_BACKGROUND_TASKS ;
234+ if (mp_hal_is_interrupted ()) {
235+ break ;
236+ }
209237 }
210238
211- /*
212- * AP cannot be disconnected. cyw43_wifi_leave is broken.
213- * This code snippet should work, but doesn't.
214- *
215- * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP);
216- * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
217- *
218- * bindings_cyw43_wifi_enforce_pm();
219- */
239+ bindings_cyw43_wifi_enforce_pm ();
220240}
221241
222242static bool connection_unchanged (wifi_radio_obj_t * self , const uint8_t * ssid , size_t ssid_len ) {
@@ -237,10 +257,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
237257 mp_raise_RuntimeError (translate ("Wifi is not enabled" ));
238258 }
239259
240- if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_DOWN ) {
241- mp_raise_RuntimeError (translate ("Wifi is in access point mode." ));
242- }
243-
244260 if (ssid_len > 32 ) {
245261 return WIFI_RADIO_ERROR_CONNECTION_FAIL ;
246262 }
0 commit comments