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:
Alex Lisitsyn
2024-09-23 18:59:08 +08:00
2 changed files with 20 additions and 15 deletions

View File

@ -235,8 +235,6 @@ static BOOL xMBTCPPortMasterCloseConnection(MbSlaveInfo_t *pxInfo)
static void xMBTCPPortMasterShutdown(void)
{
xSemaphoreGive(xShutdownSema);
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
if (pxInfo) {
@ -245,12 +243,15 @@ static void xMBTCPPortMasterShutdown(void)
free(pxInfo->pucRcvBuf);
}
free(pxInfo);
ESP_LOGD(TAG,"Close slave instance: %p", xMbPortConfig.pxMbSlaveInfo[ucCnt]);
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
}
}
free(xMbPortConfig.pxMbSlaveInfo);
vTaskDelete(NULL);
xMbPortConfig.xMbTcpTaskHandle = NULL;
xMbPortConfig.pxMbSlaveInfo = NULL;
xSemaphoreGive(xShutdownSema);
ESP_LOGD(TAG,"Shutdown the port task.");
vTaskSuspend(NULL);
}
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
xShutdownSema = xSemaphoreCreateBinary();
// 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.");
vTaskDelete(xMbPortConfig.xMbTcpTaskHandle);
}
vTaskDelete(xMbPortConfig.xMbTcpTaskHandle);
xMbPortConfig.xMbTcpTaskHandle = NULL;
if (xShutdownSema) {
vSemaphoreDelete(xShutdownSema);
xShutdownSema = NULL;
}
ESP_LOGD(TAG,"Master port is closed.");
}
void vMBMasterTCPPortClose(void)

View File

@ -261,19 +261,21 @@ static void vMBTCPPortFreeClientInfo(MbClientInfo_t *pxClientInfo)
static void vMBTCPPortShutdown(void)
{
xSemaphoreGive(xShutdownSema);
vTaskDelete(NULL);
xConfig.xMbTcpTaskHandle = NULL;
for (int i = 0; i < MB_TCP_PORT_MAX_CONN; i++) {
MbClientInfo_t *pxClientInfo = xConfig.pxMbClientInfo[i];
if ((pxClientInfo != NULL) && (pxClientInfo->xSockId > 0)) {
xMBTCPPortCloseConnection(pxClientInfo);
if (pxClientInfo != NULL) {
if (pxClientInfo->xSockId > 0) {
xMBTCPPortCloseConnection(pxClientInfo);
}
ESP_LOGD(TAG,"Close port instance: %p.", pxClientInfo);
vMBTCPPortFreeClientInfo(pxClientInfo);
xConfig.pxMbClientInfo[i] = NULL;
}
}
ESP_LOGD(TAG,"Shutdown port task.");
free(xConfig.pxMbClientInfo);
xSemaphoreGive(xShutdownSema);
vTaskSuspend(NULL);
}
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
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");
vTaskDelete(xConfig.xMbTcpTaskHandle);
}
vTaskDelete(xConfig.xMbTcpTaskHandle);
xConfig.xMbTcpTaskHandle = NULL;
close(xListenSock);
xListenSock = -1;
vMBTCPPortRespQueueDelete(xConfig.xRespQueueHandle);
if (xShutdownSema) {
vSemaphoreDelete(xShutdownSema);
xShutdownSema = NULL;
}
vMBPortEventClose();
ESP_LOGD(TAG,"Port is closed.");
}
void vMBTCPPortEnable( void )