mirror of
https://github.com/espressif/esp-modbus.git
synced 2025-07-30 10:27:16 +02:00
Merge branch 'bugfix/fix_modbus_tcp_slave_close_connections_on_destroy_v1' into 'master'
modbus tcp slave port fix close connections on destroy See merge request idf/esp-modbus!80
This commit is contained in:
@ -235,8 +235,6 @@ static BOOL xMBTCPPortMasterCloseConnection(MbSlaveInfo_t *pxInfo)
|
|||||||
|
|
||||||
static void xMBTCPPortMasterShutdown(void)
|
static void xMBTCPPortMasterShutdown(void)
|
||||||
{
|
{
|
||||||
xSemaphoreGive(xShutdownSema);
|
|
||||||
|
|
||||||
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
|
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
|
||||||
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
|
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
|
||||||
if (pxInfo) {
|
if (pxInfo) {
|
||||||
@ -245,12 +243,15 @@ static void xMBTCPPortMasterShutdown(void)
|
|||||||
free(pxInfo->pucRcvBuf);
|
free(pxInfo->pucRcvBuf);
|
||||||
}
|
}
|
||||||
free(pxInfo);
|
free(pxInfo);
|
||||||
|
ESP_LOGD(TAG,"Close slave instance: %p", xMbPortConfig.pxMbSlaveInfo[ucCnt]);
|
||||||
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
|
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(xMbPortConfig.pxMbSlaveInfo);
|
free(xMbPortConfig.pxMbSlaveInfo);
|
||||||
vTaskDelete(NULL);
|
xMbPortConfig.pxMbSlaveInfo = NULL;
|
||||||
xMbPortConfig.xMbTcpTaskHandle = NULL;
|
xSemaphoreGive(xShutdownSema);
|
||||||
|
ESP_LOGD(TAG,"Shutdown the port task.");
|
||||||
|
vTaskSuspend(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBTCPPortMasterSetNetOpt(void *pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto)
|
void vMBTCPPortMasterSetNetOpt(void *pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto)
|
||||||
@ -918,14 +919,16 @@ void vMBMasterTCPPortDisable(void)
|
|||||||
// that were allocated on the stack of the task we're going to delete
|
// that were allocated on the stack of the task we're going to delete
|
||||||
xShutdownSema = xSemaphoreCreateBinary();
|
xShutdownSema = xSemaphoreCreateBinary();
|
||||||
// if no semaphore (alloc issues) or couldn't acquire it, just delete the task
|
// if no semaphore (alloc issues) or couldn't acquire it, just delete the task
|
||||||
if (xShutdownSema == NULL || xSemaphoreTake(xShutdownSema, pdMS_TO_TICKS(MB_SHDN_WAIT_TOUT_MS)) != pdTRUE) {
|
if (!xShutdownSema || xSemaphoreTake(xShutdownSema, pdMS_TO_TICKS(MB_SHDN_WAIT_TOUT_MS)) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "Modbus port task couldn't exit gracefully within timeout -> abruptly deleting the task.");
|
ESP_LOGW(TAG, "Modbus port task couldn't exit gracefully within timeout -> abruptly deleting the task.");
|
||||||
vTaskDelete(xMbPortConfig.xMbTcpTaskHandle);
|
|
||||||
}
|
}
|
||||||
|
vTaskDelete(xMbPortConfig.xMbTcpTaskHandle);
|
||||||
|
xMbPortConfig.xMbTcpTaskHandle = NULL;
|
||||||
if (xShutdownSema) {
|
if (xShutdownSema) {
|
||||||
vSemaphoreDelete(xShutdownSema);
|
vSemaphoreDelete(xShutdownSema);
|
||||||
xShutdownSema = NULL;
|
xShutdownSema = NULL;
|
||||||
}
|
}
|
||||||
|
ESP_LOGD(TAG,"Master port is closed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBMasterTCPPortClose(void)
|
void vMBMasterTCPPortClose(void)
|
||||||
|
@ -261,19 +261,21 @@ static void vMBTCPPortFreeClientInfo(MbClientInfo_t *pxClientInfo)
|
|||||||
|
|
||||||
static void vMBTCPPortShutdown(void)
|
static void vMBTCPPortShutdown(void)
|
||||||
{
|
{
|
||||||
xSemaphoreGive(xShutdownSema);
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
xConfig.xMbTcpTaskHandle = NULL;
|
|
||||||
|
|
||||||
for (int i = 0; i < MB_TCP_PORT_MAX_CONN; i++) {
|
for (int i = 0; i < MB_TCP_PORT_MAX_CONN; i++) {
|
||||||
MbClientInfo_t *pxClientInfo = xConfig.pxMbClientInfo[i];
|
MbClientInfo_t *pxClientInfo = xConfig.pxMbClientInfo[i];
|
||||||
if ((pxClientInfo != NULL) && (pxClientInfo->xSockId > 0)) {
|
if (pxClientInfo != NULL) {
|
||||||
xMBTCPPortCloseConnection(pxClientInfo);
|
if (pxClientInfo->xSockId > 0) {
|
||||||
|
xMBTCPPortCloseConnection(pxClientInfo);
|
||||||
|
}
|
||||||
|
ESP_LOGD(TAG,"Close port instance: %p.", pxClientInfo);
|
||||||
vMBTCPPortFreeClientInfo(pxClientInfo);
|
vMBTCPPortFreeClientInfo(pxClientInfo);
|
||||||
xConfig.pxMbClientInfo[i] = NULL;
|
xConfig.pxMbClientInfo[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ESP_LOGD(TAG,"Shutdown port task.");
|
||||||
free(xConfig.pxMbClientInfo);
|
free(xConfig.pxMbClientInfo);
|
||||||
|
xSemaphoreGive(xShutdownSema);
|
||||||
|
vTaskSuspend(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int xMBTCPPortRxPoll(MbClientInfo_t *pxClientInfo, ULONG xTimeoutMs)
|
static int xMBTCPPortRxPoll(MbClientInfo_t *pxClientInfo, ULONG xTimeoutMs)
|
||||||
@ -669,19 +671,19 @@ vMBTCPPortClose( )
|
|||||||
if (xShutdownSema == NULL || // if no semaphore (alloc issues) or couldn't acquire it, just delete the task
|
if (xShutdownSema == NULL || // if no semaphore (alloc issues) or couldn't acquire it, just delete the task
|
||||||
xSemaphoreTake(xShutdownSema, 2 * pdMS_TO_TICKS(CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND)) != pdTRUE) {
|
xSemaphoreTake(xShutdownSema, 2 * pdMS_TO_TICKS(CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND)) != pdTRUE) {
|
||||||
ESP_LOGE(TAG, "Task couldn't exit gracefully within timeout -> abruptly deleting the task");
|
ESP_LOGE(TAG, "Task couldn't exit gracefully within timeout -> abruptly deleting the task");
|
||||||
vTaskDelete(xConfig.xMbTcpTaskHandle);
|
|
||||||
}
|
}
|
||||||
|
vTaskDelete(xConfig.xMbTcpTaskHandle);
|
||||||
|
xConfig.xMbTcpTaskHandle = NULL;
|
||||||
close(xListenSock);
|
close(xListenSock);
|
||||||
xListenSock = -1;
|
xListenSock = -1;
|
||||||
|
|
||||||
vMBTCPPortRespQueueDelete(xConfig.xRespQueueHandle);
|
vMBTCPPortRespQueueDelete(xConfig.xRespQueueHandle);
|
||||||
|
|
||||||
if (xShutdownSema) {
|
if (xShutdownSema) {
|
||||||
vSemaphoreDelete(xShutdownSema);
|
vSemaphoreDelete(xShutdownSema);
|
||||||
xShutdownSema = NULL;
|
xShutdownSema = NULL;
|
||||||
}
|
}
|
||||||
vMBPortEventClose();
|
vMBPortEventClose();
|
||||||
|
ESP_LOGD(TAG,"Port is closed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBTCPPortEnable( void )
|
void vMBTCPPortEnable( void )
|
||||||
|
Reference in New Issue
Block a user