@@ -30,6 +30,7 @@ typedef struct TCP_SERVER_T_ {
30
30
struct tcp_pcb * server_pcb ;
31
31
bool complete ;
32
32
ip_addr_t gw ;
33
+ async_context_t * context ;
33
34
} TCP_SERVER_T ;
34
35
35
36
typedef struct TCP_CONNECT_STATE_T_ {
@@ -236,7 +237,7 @@ static err_t tcp_server_accept(void *arg, struct tcp_pcb *client_pcb, err_t err)
236
237
return ERR_OK ;
237
238
}
238
239
239
- static bool tcp_server_open (void * arg ) {
240
+ static bool tcp_server_open (void * arg , const char * ap_name ) {
240
241
TCP_SERVER_T * state = (TCP_SERVER_T * )arg ;
241
242
DEBUG_printf ("starting server on port %d\n" , TCP_PORT );
242
243
@@ -264,9 +265,31 @@ static bool tcp_server_open(void *arg) {
264
265
tcp_arg (state -> server_pcb , state );
265
266
tcp_accept (state -> server_pcb , tcp_server_accept );
266
267
268
+ printf ("Try connecting to '%s' (press 'd' to disable access point)\n" , ap_name );
267
269
return true;
268
270
}
269
271
272
+ // This "worker" function is called to safely perform work when instructed by key_pressed_func
273
+ void key_pressed_worker_func (async_context_t * context , async_when_pending_worker_t * worker ) {
274
+ assert (worker -> user_data );
275
+ printf ("Disabling wifi\n" );
276
+ cyw43_arch_disable_ap_mode ();
277
+ ((TCP_SERVER_T * )(worker -> user_data ))-> complete = true;
278
+ }
279
+
280
+ static async_when_pending_worker_t key_pressed_worker = {
281
+ .do_work = key_pressed_worker_func
282
+ };
283
+
284
+ void key_pressed_func (void * param ) {
285
+ assert (param );
286
+ int key = getchar_timeout_us (0 ); // get any pending key press but don't wait
287
+ if (key == 'd' || key == 'D' ) {
288
+ // We are probably in irq context so call wifi in a "worker"
289
+ async_context_set_work_pending (((TCP_SERVER_T * )param )-> context , & key_pressed_worker );
290
+ }
291
+ }
292
+
270
293
int main () {
271
294
stdio_init_all ();
272
295
@@ -280,6 +303,13 @@ int main() {
280
303
DEBUG_printf ("failed to initialise\n" );
281
304
return 1 ;
282
305
}
306
+
307
+ // Get notified if the user presses a key
308
+ state -> context = cyw43_arch_async_context ();
309
+ key_pressed_worker .user_data = state ;
310
+ async_context_add_when_pending_worker (cyw43_arch_async_context (), & key_pressed_worker );
311
+ stdio_set_chars_available_callback (key_pressed_func , state );
312
+
283
313
const char * ap_name = "picow_test" ;
284
314
#if 1
285
315
const char * password = "password" ;
@@ -301,11 +331,12 @@ int main() {
301
331
dns_server_t dns_server ;
302
332
dns_server_init (& dns_server , & state -> gw );
303
333
304
- if (!tcp_server_open (state )) {
334
+ if (!tcp_server_open (state , ap_name )) {
305
335
DEBUG_printf ("failed to open server\n" );
306
336
return 1 ;
307
337
}
308
338
339
+ state -> complete = false;
309
340
while (!state -> complete ) {
310
341
// the following #ifdef is only here so this same example can be used in multiple modes;
311
342
// you do not need it in your code
@@ -323,6 +354,7 @@ int main() {
323
354
sleep_ms (1000 );
324
355
#endif
325
356
}
357
+ tcp_server_close (state );
326
358
dns_server_deinit (& dns_server );
327
359
dhcp_server_deinit (& dhcp_server );
328
360
cyw43_arch_deinit ();
0 commit comments