@@ -34,6 +34,7 @@ static const struct mrb_data_type mrb_eh_ctx_t = {
3434
3535static EventGroupHandle_t wifi_event_group ;
3636typedef struct eh_ctx_t {
37+ TaskHandle_t task ;
3738 mrb_state * mrb ;
3839 mrb_value on_connected_blk ;
3940 mrb_value on_disconnected_blk ;
@@ -51,6 +52,7 @@ event_handler(void *ctx, system_event_t *event)
5152 case SYSTEM_EVENT_STA_GOT_IP :
5253 xEventGroupSetBits (wifi_event_group , CONNECTED_BIT );
5354 if (ehc != NULL ) {
55+ vTaskSuspend (ehc -> task );
5456 int arena_index = mrb_gc_arena_save (ehc -> mrb );
5557
5658 mrb_value mrb_ip_str = mrb_str_buf_new (ehc -> mrb , 13 );
@@ -64,13 +66,15 @@ event_handler(void *ctx, system_event_t *event)
6466 }
6567
6668 mrb_gc_arena_restore (ehc -> mrb , arena_index );
69+ vTaskResume (ehc -> task );
6770 }
6871 break ;
6972 case SYSTEM_EVENT_STA_DISCONNECTED :
7073 // This is a workaround as ESP32 WiFi libs don't currently auto-reassociate.
7174 esp_wifi_connect ();
7275 xEventGroupClearBits (wifi_event_group , CONNECTED_BIT );
7376 if (ehc != NULL ) {
77+ vTaskSuspend (ehc -> task );
7478 int arena_index = mrb_gc_arena_save (ehc -> mrb );
7579
7680 if (!mrb_nil_p (ehc -> on_disconnected_blk )) {
@@ -79,6 +83,7 @@ event_handler(void *ctx, system_event_t *event)
7983 }
8084
8185 mrb_gc_arena_restore (ehc -> mrb , arena_index );
86+ vTaskResume (ehc -> task );
8287 }
8388 break ;
8489 default :
@@ -90,6 +95,7 @@ event_handler(void *ctx, system_event_t *event)
9095static mrb_value
9196mrb_esp32_wifi_init (mrb_state * mrb , mrb_value self ) {
9297 eh_ctx_t * ehc = mrb_malloc (mrb , sizeof (eh_ctx_t ));
98+ ehc -> task = xTaskGetCurrentTaskHandle ();
9399 ehc -> mrb = mrb ;
94100 ehc -> on_connected_blk = mrb_nil_value ();
95101 ehc -> on_disconnected_blk = mrb_nil_value ();
0 commit comments