Skip to content

Commit 5e70489

Browse files
SPRESENSEjerpelea
authored andcommitted
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>
1 parent 5498366 commit 5e70489

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

include/netutils/esp8266.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ typedef struct
7777
****************************************************************************/
7878

7979
int lesp_initialize(void);
80+
int lesp_finalize(void);
8081
int lesp_soft_reset(void);
8182

8283
const char *lesp_security_to_str(lesp_security_t security);

netutils/esp8266/esp8266.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,70 @@ int lesp_initialize(void)
15311531
return 0;
15321532
}
15331533

1534+
/****************************************************************************
1535+
* Name: lesp_finalize
1536+
*
1537+
* Description:
1538+
* finalize Esp8266 class.
1539+
* - destroy worker thread
1540+
* - close port
1541+
*
1542+
* Input Parameters:
1543+
* None
1544+
*
1545+
* Returned Value:
1546+
* 0 on success, -1 in case of error.
1547+
*
1548+
****************************************************************************/
1549+
1550+
int lesp_finalize(void)
1551+
{
1552+
int i;
1553+
1554+
ninfo("Finalizing Esp8266...\n");
1555+
1556+
pthread_mutex_lock(&g_lesp_state.mutex);
1557+
1558+
if (!g_lesp_state.is_initialized)
1559+
{
1560+
pthread_mutex_unlock(&g_lesp_state.mutex);
1561+
ninfo("Esp8266 already finalized\n");
1562+
return 0;
1563+
}
1564+
1565+
pthread_mutex_lock(&g_lesp_state.worker.mutex);
1566+
1567+
for (i = 0; i < SOCKET_NBR; i++)
1568+
{
1569+
if ((g_lesp_state.sockets[i].flags & FLAGS_SOCK_USED) != 0)
1570+
{
1571+
nerr("ERROR: Exist opened socket\n");
1572+
pthread_mutex_unlock(&g_lesp_state.worker.mutex);
1573+
pthread_mutex_unlock(&g_lesp_state.mutex);
1574+
return -1;
1575+
}
1576+
}
1577+
1578+
/* Destroy worker thread */
1579+
1580+
g_lesp_state.worker.running = false;
1581+
pthread_kill(g_lesp_state.worker.thread, SIGTERM);
1582+
pthread_join(g_lesp_state.worker.thread, NULL);
1583+
1584+
if (g_lesp_state.fd > 0)
1585+
{
1586+
close(g_lesp_state.fd);
1587+
g_lesp_state.fd = -1;
1588+
}
1589+
1590+
g_lesp_state.is_initialized = false;
1591+
1592+
pthread_mutex_unlock(&g_lesp_state.worker.mutex);
1593+
pthread_mutex_unlock(&g_lesp_state.mutex);
1594+
1595+
return 0;
1596+
}
1597+
15341598
/****************************************************************************
15351599
* Name: lesp_soft_reset
15361600
*

0 commit comments

Comments
 (0)