Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_modbus_tcp_slave_close_connections_on_destro…
Browse files Browse the repository at this point in the history
…y_v1' into 'master'

modbus tcp slave port fix close connections on destroy

See merge request idf/esp-modbus!80
  • Loading branch information
alisitsyn committed Sep 23, 2024
2 parents e829315 + 3689bc6 commit 2601e34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
15 changes: 9 additions & 6 deletions freemodbus/tcp_master/port/port_tcp_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions freemodbus/tcp_slave/port/port_tcp_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 2601e34

Please sign in to comment.