@@ -133,7 +133,7 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self, u
133
133
mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
134
134
}
135
135
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" ));
137
137
}
138
138
wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
139
139
scan -> base .type = & wifi_scannednetworks_type ;
@@ -154,35 +154,78 @@ void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
154
154
}
155
155
156
156
void common_hal_wifi_radio_stop_station (wifi_radio_obj_t * self ) {
157
+
157
158
cyw43_wifi_leave (& cyw43_state , CYW43_ITF_STA );
158
159
// This is wrong, but without this call the state of ITF_STA is still
159
160
// 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 .
162
163
cyw43_wifi_leave (& cyw43_state , CYW43_ITF_AP );
164
+
163
165
bindings_cyw43_wifi_enforce_pm ();
164
166
}
165
167
166
168
void 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
168
186
bindings_cyw43_wifi_enforce_pm ();
169
187
}
170
188
171
189
void 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
+ */
173
207
}
174
208
175
209
wifi_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 ) {
176
210
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." ));
178
216
}
179
217
218
+
180
219
size_t timeout_ms = timeout <= 0 ? 8000 : (size_t )MICROPY_FLOAT_C_FUN (ceil )(timeout * 1000 );
181
220
uint64_t start = port_get_raw_ticks (NULL );
182
221
uint64_t deadline = start + timeout_ms ;
183
222
223
+ // disconnect
224
+ common_hal_wifi_radio_stop_station (self );
225
+
184
226
// connect
185
227
cyw43_arch_wifi_connect_async ((const char * )ssid , (const char * )password , CYW43_AUTH_WPA2_AES_PSK );
228
+ // TODO: Implement authmode check like in espressif
186
229
187
230
while (port_get_raw_ticks (NULL ) < deadline ) {
188
231
RUN_BACKGROUND_TASKS ;
0 commit comments