netutils/esp8266: Add a function for finalization.

Add a function for finalization so that it can be used repeatedly.

Signed-off-by: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com>
This commit is contained in:
SPRESENSE 2025-07-22 10:07:11 +09:00 committed by Alin Jerpelea
parent 549836600c
commit 5e704891d9
2 changed files with 65 additions and 0 deletions

View file

@ -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);

View file

@ -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
*