diff --git a/include/netutils/esp8266.h b/include/netutils/esp8266.h index 2c4a612b9..d36bd73c8 100644 --- a/include/netutils/esp8266.h +++ b/include/netutils/esp8266.h @@ -77,6 +77,7 @@ typedef struct ****************************************************************************/ int lesp_initialize(void); +int lesp_finalize(void); int lesp_soft_reset(void); const char *lesp_security_to_str(lesp_security_t security); diff --git a/netutils/esp8266/esp8266.c b/netutils/esp8266/esp8266.c index e2368df95..51de514b7 100644 --- a/netutils/esp8266/esp8266.c +++ b/netutils/esp8266/esp8266.c @@ -1531,6 +1531,70 @@ int lesp_initialize(void) return 0; } +/**************************************************************************** + * Name: lesp_finalize + * + * Description: + * finalize Esp8266 class. + * - destroy worker thread + * - close port + * + * Input Parameters: + * None + * + * Returned Value: + * 0 on success, -1 in case of error. + * + ****************************************************************************/ + +int lesp_finalize(void) +{ + int i; + + ninfo("Finalizing Esp8266...\n"); + + pthread_mutex_lock(&g_lesp_state.mutex); + + if (!g_lesp_state.is_initialized) + { + pthread_mutex_unlock(&g_lesp_state.mutex); + ninfo("Esp8266 already finalized\n"); + return 0; + } + + pthread_mutex_lock(&g_lesp_state.worker.mutex); + + for (i = 0; i < SOCKET_NBR; i++) + { + if ((g_lesp_state.sockets[i].flags & FLAGS_SOCK_USED) != 0) + { + nerr("ERROR: Exist opened socket\n"); + pthread_mutex_unlock(&g_lesp_state.worker.mutex); + pthread_mutex_unlock(&g_lesp_state.mutex); + return -1; + } + } + + /* Destroy worker thread */ + + g_lesp_state.worker.running = false; + pthread_kill(g_lesp_state.worker.thread, SIGTERM); + pthread_join(g_lesp_state.worker.thread, NULL); + + if (g_lesp_state.fd > 0) + { + close(g_lesp_state.fd); + g_lesp_state.fd = -1; + } + + g_lesp_state.is_initialized = false; + + pthread_mutex_unlock(&g_lesp_state.worker.mutex); + pthread_mutex_unlock(&g_lesp_state.mutex); + + return 0; +} + /**************************************************************************** * Name: lesp_soft_reset *