@@ -133,7 +133,7 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self, u
133133 mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
134134 }
135135 if (!common_hal_wifi_radio_get_enabled (self )) {
136- mp_raise_RuntimeError (translate ("wifi is not enabled" ));
136+ mp_raise_RuntimeError (translate ("Wifi is not enabled" ));
137137 }
138138 wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
139139 scan -> base .type = & wifi_scannednetworks_type ;
@@ -154,35 +154,78 @@ void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
154154}
155155
156156void common_hal_wifi_radio_stop_station (wifi_radio_obj_t * self ) {
157+
157158 cyw43_wifi_leave (& cyw43_state , CYW43_ITF_STA );
158159 // This is wrong, but without this call the state of ITF_STA is still
159160 // reported as CYW43_LINK_JOIN (by wifi_link_status) and CYW43_LINK_UP
160- // (by tcpip_link_status). Until AP support is added, we can ignore the
161- // problem .
161+ // (by tcpip_link_status). However since ap disconnection isn't working
162+ // either, this is not an issue .
162163 cyw43_wifi_leave (& cyw43_state , CYW43_ITF_AP );
164+
163165 bindings_cyw43_wifi_enforce_pm ();
164166}
165167
166168void common_hal_wifi_radio_start_ap (wifi_radio_obj_t * self , uint8_t * ssid , size_t ssid_len , uint8_t * password , size_t password_len , uint8_t channel , uint32_t authmodes , uint8_t max_connections ) {
167- mp_raise_NotImplementedError (NULL );
169+ if (!common_hal_wifi_radio_get_enabled (self )) {
170+ mp_raise_RuntimeError (translate ("Wifi is not enabled" ));
171+ }
172+
173+ if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_STA ) != CYW43_LINK_DOWN ) {
174+ mp_raise_RuntimeError (translate ("Wifi is in station mode." ));
175+ }
176+
177+ common_hal_wifi_radio_stop_ap (self );
178+
179+ // Channel can only be changed after inital powerup and config of ap.
180+ // Defaults to 1 if not set or invalid (i.e. 13)
181+ cyw43_wifi_ap_set_channel (& cyw43_state , (const uint32_t )channel );
182+
183+ cyw43_arch_enable_ap_mode ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
184+
185+ // TODO: Implement authmode check like in espressif
168186 bindings_cyw43_wifi_enforce_pm ();
169187}
170188
171189void common_hal_wifi_radio_stop_ap (wifi_radio_obj_t * self ) {
172- mp_raise_NotImplementedError (NULL );
190+ if (!common_hal_wifi_radio_get_enabled (self )) {
191+ mp_raise_RuntimeError (translate ("wifi is not enabled" ));
192+ }
193+
194+ if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_DOWN ) {
195+ mp_raise_NotImplementedError (translate ("Stopping AP is not supported." ));
196+ }
197+
198+ /*
199+ * AP cannot be disconnected. cyw43_wifi_leave is broken.
200+ * This code snippet should work, but doesn't.
201+ *
202+ * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP);
203+ * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
204+ *
205+ * bindings_cyw43_wifi_enforce_pm();
206+ */
173207}
174208
175209wifi_radio_error_t common_hal_wifi_radio_connect (wifi_radio_obj_t * self , uint8_t * ssid , size_t ssid_len , uint8_t * password , size_t password_len , uint8_t channel , mp_float_t timeout , uint8_t * bssid , size_t bssid_len ) {
176210 if (!common_hal_wifi_radio_get_enabled (self )) {
177- mp_raise_RuntimeError (translate ("wifi is not enabled" ));
211+ mp_raise_RuntimeError (translate ("Wifi is not enabled" ));
212+ }
213+
214+ if (cyw43_tcpip_link_status (& cyw43_state , CYW43_ITF_AP ) != CYW43_LINK_DOWN ) {
215+ mp_raise_RuntimeError (translate ("Wifi is in access point mode." ));
178216 }
179217
218+
180219 size_t timeout_ms = timeout <= 0 ? 8000 : (size_t )MICROPY_FLOAT_C_FUN (ceil )(timeout * 1000 );
181220 uint64_t start = port_get_raw_ticks (NULL );
182221 uint64_t deadline = start + timeout_ms ;
183222
223+ // disconnect
224+ common_hal_wifi_radio_stop_station (self );
225+
184226 // connect
185227 cyw43_arch_wifi_connect_async ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
228+ // TODO: Implement authmode check like in espressif
186229
187230 while (port_get_raw_ticks (NULL ) < deadline ) {
188231 RUN_BACKGROUND_TASKS ;
0 commit comments