@@ -348,7 +348,15 @@ static void esp_rmaker_task(void *param)
348348 esp_rmaker_priv_data -> state = ESP_RMAKER_STATE_STARTING ;
349349 esp_err_t err ;
350350 wifi_event_group = xEventGroupCreate ();
351- ESP_ERROR_CHECK (esp_event_handler_register (IP_EVENT , IP_EVENT_STA_GOT_IP , & esp_rmaker_event_handler , esp_rmaker_priv_data ));
351+ if (!wifi_event_group ) {
352+ ESP_LOGE (TAG , "Failed to create event group. Aborting" );
353+ goto rmaker_err ;
354+ }
355+ err = esp_event_handler_register (IP_EVENT , IP_EVENT_STA_GOT_IP , & esp_rmaker_event_handler , esp_rmaker_priv_data );
356+ if (err != ESP_OK ) {
357+ ESP_LOGE (TAG , "Failed to register event handler. Error: %d. Aborting" , err );
358+ goto rmaker_err ;
359+ }
352360 /* Assisted claiming needs to be done before Wi-Fi connection */
353361#ifdef CONFIG_ESP_RMAKER_ASSISTED_CLAIM
354362 if (esp_rmaker_priv_data -> need_claim ) {
@@ -357,7 +365,8 @@ static void esp_rmaker_task(void *param)
357365 if (err != ESP_OK ) {
358366 esp_rmaker_post_event (RMAKER_EVENT_CLAIM_FAILED , NULL , 0 );
359367 ESP_LOGE (TAG , "esp_rmaker_self_claim_perform() returned %d. Aborting" , err );
360- vTaskDelete (NULL );
368+ esp_event_handler_unregister (IP_EVENT , IP_EVENT_STA_GOT_IP , & esp_rmaker_event_handler );
369+ goto rmaker_err ;
361370 }
362371 esp_rmaker_priv_data -> claim_data = NULL ;
363372 esp_rmaker_post_event (RMAKER_EVENT_CLAIM_SUCCESSFUL , NULL , 0 );
@@ -366,6 +375,8 @@ static void esp_rmaker_task(void *param)
366375 /* Wait for Wi-Fi connection */
367376 xEventGroupWaitBits (wifi_event_group , WIFI_CONNECTED_EVENT , false, true, portMAX_DELAY );
368377 vEventGroupDelete (wifi_event_group );
378+ wifi_event_group = NULL ;
379+ esp_event_handler_unregister (IP_EVENT , IP_EVENT_STA_GOT_IP , & esp_rmaker_event_handler );
369380
370381 if (esp_rmaker_priv_data -> enable_time_sync ) {
371382#ifdef CONFIG_MBEDTLS_HAVE_TIME_DATE
@@ -380,7 +391,7 @@ static void esp_rmaker_task(void *param)
380391 if (err != ESP_OK ) {
381392 esp_rmaker_post_event (RMAKER_EVENT_CLAIM_FAILED , NULL , 0 );
382393 ESP_LOGE (TAG , "esp_rmaker_self_claim_perform() returned %d. Aborting" , err );
383- vTaskDelete ( NULL ) ;
394+ goto rmaker_err ;
384395 }
385396 esp_rmaker_priv_data -> claim_data = NULL ;
386397 esp_rmaker_post_event (RMAKER_EVENT_CLAIM_SUCCESSFUL , NULL , 0 );
@@ -391,20 +402,20 @@ static void esp_rmaker_task(void *param)
391402 esp_rmaker_priv_data -> mqtt_config = esp_rmaker_get_mqtt_config ();
392403 if (!esp_rmaker_priv_data -> mqtt_config ) {
393404 ESP_LOGE (TAG , "Failed to initialise MQTT Config after claiming. Aborting" );
394- vTaskDelete ( NULL ) ;
405+ goto rmaker_err ;
395406 }
396407 err = esp_rmaker_mqtt_init (esp_rmaker_priv_data -> mqtt_config );
397408 if (err != ESP_OK ) {
398409 ESP_LOGE (TAG , "esp_rmaker_mqtt_init() returned %d. Aborting" , err );
399- vTaskDelete ( NULL ) ;
410+ goto rmaker_err ;
400411 }
401412 esp_rmaker_priv_data -> need_claim = false;
402413 }
403414#endif /* ESP_RMAKER_CLAIM_ENABLED */
404415 err = esp_rmaker_mqtt_connect ();
405416 if (err != ESP_OK ) {
406417 ESP_LOGE (TAG , "esp_rmaker_mqtt_connect() returned %d. Aborting" , err );
407- vTaskDelete ( NULL ) ;
418+ goto rmaker_err ;
408419 }
409420 esp_rmaker_priv_data -> mqtt_connected = true;
410421 esp_rmaker_priv_data -> state = ESP_RMAKER_STATE_STARTED ;
@@ -424,6 +435,11 @@ static void esp_rmaker_task(void *param)
424435rmaker_end :
425436 esp_rmaker_mqtt_disconnect ();
426437 esp_rmaker_priv_data -> mqtt_connected = false;
438+ rmaker_err :
439+ if (wifi_event_group ) {
440+ vEventGroupDelete (wifi_event_group );
441+ }
442+ wifi_event_group = NULL ;
427443 esp_rmaker_priv_data -> state = ESP_RMAKER_STATE_INIT_DONE ;
428444 vTaskDelete (NULL );
429445}
0 commit comments