Merge branch 'bugfix/modbus_fix_start_after_destroy_issues' into 'master'

modbus: fix tcp slave destroy issues

Closes IDFGH-6568

See merge request espressif/esp-idf!16856
This commit is contained in:
Alex Lisitsyn 2022-03-03 16:34:33 +08:00
commit dcaac79380
10 changed files with 132 additions and 133 deletions

View File

@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
* *
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
*/ */
/* /*
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
@ -229,6 +229,8 @@ BOOL xMBTCPPortInit( USHORT usTCPPort );
void vMBTCPPortClose( void ); void vMBTCPPortClose( void );
void vMBTCPPortEnable( void );
void vMBTCPPortDisable( void ); void vMBTCPPortDisable( void );
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength ); BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
@ -242,6 +244,8 @@ BOOL xMBMasterTCPPortInit( USHORT usTCPPort );
void vMBMasterTCPPortClose( void ); void vMBMasterTCPPortClose( void );
void vMBMasterTCPPortEnable( void );
void vMBMasterTCPPortDisable( void ); void vMBMasterTCPPortDisable( void );
BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength ); BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );

View File

@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
* *
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
*/ */
/* /*
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
@ -93,12 +93,15 @@ eMBTCPDoInit( USHORT ucTCPPort )
void void
eMBTCPStart( void ) eMBTCPStart( void )
{ {
ESP_LOGD(MB_PORT_TAG, "TCP Slave port enable.");
vMBTCPPortEnable( );
} }
void void
eMBTCPStop( void ) eMBTCPStop( void )
{ {
/* Make sure that no more clients are connected. */ /* Make sure that no more clients are connected. */
ESP_LOGD(MB_PORT_TAG, "TCP Slave port disable.");
vMBTCPPortDisable( ); vMBTCPPortDisable( );
} }

View File

@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
* *
* SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD
*/ */
/* /*
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
@ -93,11 +93,15 @@ eMBMasterTCPDoInit( USHORT ucTCPPort )
void void
eMBMasterTCPStart( void ) eMBMasterTCPStart( void )
{ {
ESP_LOGD(MB_PORT_TAG, "TCP Master port enable.");
vMBMasterTCPPortEnable( );
} }
void void
eMBMasterTCPStop( void ) eMBMasterTCPStop( void )
{ {
ESP_LOGD(MB_PORT_TAG, "TCP Master port disable.");
vMBMasterTCPPortDisable( );
} }
eMBErrorCode eMBErrorCode

View File

@ -156,7 +156,10 @@ static esp_err_t mbc_tcp_master_start(void)
eMBPortProto proto = (comm_info->ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP; eMBPortProto proto = (comm_info->ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
eMBPortIpVer ip_ver = (comm_info->ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6; eMBPortIpVer ip_ver = (comm_info->ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
vMBTCPPortMasterSetNetOpt(comm_info->ip_netif_ptr, ip_ver, proto); vMBTCPPortMasterSetNetOpt(comm_info->ip_netif_ptr, ip_ver, proto);
vMBTCPPortMasterTaskStart();
status = eMBMasterEnable();
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
"mb stack set slave ID failure, eMBMasterEnable() returned (0x%x).", (uint32_t)status);
// Add slave IP address for each slave to initialize connection // Add slave IP address for each slave to initialize connection
mb_slave_addr_entry_t *p_slave_info; mb_slave_addr_entry_t *p_slave_info;
@ -169,17 +172,13 @@ static esp_err_t mbc_tcp_master_start(void)
// Add end of list condition // Add end of list condition
(void)xMBTCPPortMasterAddSlaveIp(0xFF, NULL, 0xFF); (void)xMBTCPPortMasterAddSlaveIp(0xFF, NULL, 0xFF);
status = eMBMasterEnable();
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
"mb stack set slave ID failure, eMBMasterEnable() returned (0x%x).", (uint32_t)status);
// Wait for connection done event // Wait for connection done event
bool start = (bool)xMBTCPPortMasterWaitEvent(mbm_opts->mbm_event_group, bool start = (bool)xMBTCPPortMasterWaitEvent(mbm_opts->mbm_event_group,
(EventBits_t)MB_EVENT_STACK_STARTED, MB_TCP_CONNECTION_TOUT); (EventBits_t)MB_EVENT_STACK_STARTED, MB_TCP_CONNECTION_TOUT);
MB_MASTER_CHECK((start), ESP_ERR_INVALID_STATE, MB_MASTER_CHECK((start), ESP_ERR_INVALID_STATE,
"mb stack could not connect to slaves for %d seconds.", "mb stack could not connect to slaves for %d seconds.",
CONFIG_FMB_TCP_CONNECTION_TOUT_SEC); CONFIG_FMB_TCP_CONNECTION_TOUT_SEC);
return ESP_OK; return ESP_OK;
} }
@ -196,10 +195,10 @@ static esp_err_t mbc_tcp_master_destroy(void)
MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure."); MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
mb_error = eMBMasterClose(); mb_error = eMBMasterClose();
MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE,
"mb stack close failure returned (0x%x).", (uint32_t)mb_error); "mb stack close failure returned (0x%x).", (uint32_t)mb_error);
// Stop polling by clearing correspondent bit in the event group // Stop polling by clearing correspondent bit in the event group
xEventGroupClearBits(mbm_opts->mbm_event_group, xEventGroupClearBits(mbm_opts->mbm_event_group,
(EventBits_t)MB_EVENT_STACK_STARTED); (EventBits_t)MB_EVENT_STACK_STARTED);
(void)vTaskDelete(mbm_opts->mbm_task_handle); (void)vTaskDelete(mbm_opts->mbm_task_handle);
mbm_opts->mbm_task_handle = NULL; mbm_opts->mbm_task_handle = NULL;
(void)vEventGroupDelete(mbm_opts->mbm_event_group); (void)vEventGroupDelete(mbm_opts->mbm_event_group);
@ -272,52 +271,52 @@ static esp_err_t mbc_tcp_master_send_request(mb_param_request_t* request, void*
// Calls appropriate request function to send request and waits response // Calls appropriate request function to send request and waits response
switch(mb_command) switch(mb_command)
{ {
case MB_FUNC_READ_COILS: case MB_FUNC_READ_COILS:
mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size , (LONG)MB_RESPONSE_TIMEOUT ); (USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_WRITE_SINGLE_COIL: case MB_FUNC_WRITE_SINGLE_COIL:
mb_error = eMBMasterReqWriteCoil((UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqWriteCoil((UCHAR)mb_slave_addr, (USHORT)mb_offset,
*(USHORT*)data_ptr, (LONG)MB_RESPONSE_TIMEOUT ); *(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_WRITE_MULTIPLE_COILS: case MB_FUNC_WRITE_MULTIPLE_COILS:
mb_error = eMBMasterReqWriteMultipleCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqWriteMultipleCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size, (UCHAR*)data_ptr, (USHORT)mb_size, (UCHAR *)data_ptr,
(LONG)MB_RESPONSE_TIMEOUT); (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_READ_DISCRETE_INPUTS: case MB_FUNC_READ_DISCRETE_INPUTS:
mb_error = eMBMasterReqReadDiscreteInputs((UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqReadDiscreteInputs((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT ); (USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_READ_HOLDING_REGISTER: case MB_FUNC_READ_HOLDING_REGISTER:
mb_error = eMBMasterReqReadHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqReadHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT ); (USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_WRITE_REGISTER: case MB_FUNC_WRITE_REGISTER:
mb_error = eMBMasterReqWriteHoldingRegister( (UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqWriteHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
*(USHORT*)data_ptr, (LONG)MB_RESPONSE_TIMEOUT ); *(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_WRITE_MULTIPLE_REGISTERS: case MB_FUNC_WRITE_MULTIPLE_REGISTERS:
mb_error = eMBMasterReqWriteMultipleHoldingRegister( (UCHAR)mb_slave_addr, mb_error = eMBMasterReqWriteMultipleHoldingRegister((UCHAR)mb_slave_addr,
(USHORT)mb_offset, (USHORT)mb_size, (USHORT)mb_offset, (USHORT)mb_size,
(USHORT*)data_ptr, (LONG)MB_RESPONSE_TIMEOUT ); (USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_READWRITE_MULTIPLE_REGISTERS: case MB_FUNC_READWRITE_MULTIPLE_REGISTERS:
mb_error = eMBMasterReqReadWriteMultipleHoldingRegister( (UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqReadWriteMultipleHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size, (USHORT*)data_ptr, (USHORT)mb_size, (USHORT *)data_ptr,
(USHORT)mb_offset, (USHORT)mb_size, (USHORT)mb_offset, (USHORT)mb_size,
(LONG)MB_RESPONSE_TIMEOUT ); (LONG)MB_RESPONSE_TIMEOUT);
break; break;
case MB_FUNC_READ_INPUT_REGISTER: case MB_FUNC_READ_INPUT_REGISTER:
mb_error = eMBMasterReqReadInputRegister( (UCHAR)mb_slave_addr, (USHORT)mb_offset, mb_error = eMBMasterReqReadInputRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
(USHORT)mb_size, (LONG) MB_RESPONSE_TIMEOUT ); (USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
break; break;
default: default:
ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ", ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ",
__FUNCTION__, mb_command); __FUNCTION__, mb_command);
mb_error = MB_MRE_NO_REG; mb_error = MB_MRE_NO_REG;
break; break;
} }
// Propagate the Modbus errors to higher level // Propagate the Modbus errors to higher level
@ -500,12 +499,12 @@ static esp_err_t mbc_tcp_master_get_parameter(uint16_t cid, char* name, uint8_t*
error = ESP_ERR_INVALID_STATE; error = ESP_ERR_INVALID_STATE;
} else { } else {
ESP_LOGD(TAG, "%s: Good response for get cid(%u) = %s", ESP_LOGD(TAG, "%s: Good response for get cid(%u) = %s",
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error)); __FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
} }
} }
} else { } else {
ESP_LOGD(TAG, "%s: Bad response to get cid(%u) = %s", ESP_LOGD(TAG, "%s: Bad response to get cid(%u) = %s",
__FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error)); __FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error));
error = ESP_ERR_INVALID_RESPONSE; error = ESP_ERR_INVALID_RESPONSE;
} }
free(pdata); free(pdata);
@ -513,7 +512,7 @@ static esp_err_t mbc_tcp_master_get_parameter(uint16_t cid, char* name, uint8_t*
*type = reg_info.param_type; *type = reg_info.param_type;
} else { } else {
ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.", ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.",
__FUNCTION__, reg_info.cid); __FUNCTION__, reg_info.cid);
error = ESP_ERR_INVALID_ARG; error = ESP_ERR_INVALID_ARG;
} }
return error; return error;
@ -539,7 +538,7 @@ static esp_err_t mbc_tcp_master_set_parameter(uint16_t cid, char* name, uint8_t*
} }
// Transfer value of characteristic into parameter buffer // Transfer value of characteristic into parameter buffer
error = mbc_tcp_master_set_param_data((void*)pdata, (void*)value, error = mbc_tcp_master_set_param_data((void*)pdata, (void*)value,
reg_info.param_type, reg_info.param_size); reg_info.param_type, reg_info.param_size);
if (error != ESP_OK) { if (error != ESP_OK) {
ESP_LOGE(TAG, "fail to set parameter data."); ESP_LOGE(TAG, "fail to set parameter data.");
free(pdata); free(pdata);
@ -578,8 +577,7 @@ static esp_err_t mbc_tcp_master_set_parameter(uint16_t cid, char* name, uint8_t*
* @return result * @return result
*/ */
// Callback function for reading of MB Input Registers // Callback function for reading of MB Input Registers
eMBErrorCode eMBRegInputCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBRegInputCBTcpMaster(UCHAR* pucRegBuffer, USHORT usAddress, USHORT usNRegs)
USHORT usNRegs)
{ {
MB_MASTER_ASSERT(mbm_interface_ptr != NULL); MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts; mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
@ -614,36 +612,40 @@ eMBErrorCode eMBRegInputCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
*/ */
// Callback function for reading of MB Holding Registers // Callback function for reading of MB Holding Registers
// Executed by stack when request to read/write holding registers is received // Executed by stack when request to read/write holding registers is received
eMBErrorCode eMBRegHoldingCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBRegHoldingCBTcpMaster(UCHAR *pucRegBuffer, USHORT usAddress,
USHORT usNRegs, eMBRegisterMode eMode) USHORT usNRegs, eMBRegisterMode eMode)
{ {
MB_MASTER_ASSERT(mbm_interface_ptr != NULL); MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts; mb_master_options_t *mbm_opts = &mbm_interface_ptr->opts;
MB_MASTER_ASSERT(pucRegBuffer != NULL); MB_MASTER_ASSERT(pucRegBuffer != NULL);
USHORT usRegHoldingNregs = (USHORT)mbm_opts->mbm_reg_buffer_size; USHORT usRegHoldingNregs = (USHORT)mbm_opts->mbm_reg_buffer_size;
UCHAR* pucHoldingBuffer = (UCHAR*)mbm_opts->mbm_reg_buffer_ptr; UCHAR *pucHoldingBuffer = (UCHAR *)mbm_opts->mbm_reg_buffer_ptr;
eMBErrorCode eStatus = MB_ENOERR; eMBErrorCode eStatus = MB_ENOERR;
USHORT usRegs = usNRegs; USHORT usRegs = usNRegs;
// Check input and configuration parameters for correctness // Check input and configuration parameters for correctness
if ((pucHoldingBuffer != NULL) if ((pucHoldingBuffer != NULL) && (usRegHoldingNregs == usNRegs) && (usNRegs >= 1))
&& (usRegHoldingNregs == usNRegs) {
&& (usNRegs >= 1)) { switch (eMode)
switch (eMode) { {
case MB_REG_WRITE: case MB_REG_WRITE:
while (usRegs > 0) { while (usRegs > 0)
_XFER_2_RD(pucRegBuffer, pucHoldingBuffer); {
usRegs -= 1; _XFER_2_RD(pucRegBuffer, pucHoldingBuffer);
}; usRegs -= 1;
break; };
case MB_REG_READ: break;
while (usRegs > 0) { case MB_REG_READ:
_XFER_2_WR(pucHoldingBuffer, pucRegBuffer); while (usRegs > 0)
pucHoldingBuffer += 2; {
usRegs -= 1; _XFER_2_WR(pucHoldingBuffer, pucRegBuffer);
}; pucHoldingBuffer += 2;
break; usRegs -= 1;
};
break;
} }
} else { }
else
{
eStatus = MB_ENOREG; eStatus = MB_ENOREG;
} }
return eStatus; return eStatus;
@ -660,8 +662,8 @@ eMBErrorCode eMBRegHoldingCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
* @return result * @return result
*/ */
// Callback function for reading of MB Coils Registers // Callback function for reading of MB Coils Registers
eMBErrorCode eMBRegCoilsCBTcpMaster(UCHAR* pucRegBuffer, USHORT usAddress, eMBErrorCode eMBRegCoilsCBTcpMaster(UCHAR *pucRegBuffer, USHORT usAddress,
USHORT usNCoils, eMBRegisterMode eMode) USHORT usNCoils, eMBRegisterMode eMode)
{ {
MB_MASTER_ASSERT(mbm_interface_ptr != NULL); MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts; mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
@ -712,7 +714,7 @@ eMBErrorCode eMBRegCoilsCBTcpMaster(UCHAR* pucRegBuffer, USHORT usAddress,
*/ */
// Callback function for reading of MB Discrete Input Registers // Callback function for reading of MB Discrete Input Registers
eMBErrorCode eMBRegDiscreteCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBRegDiscreteCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNDiscrete) USHORT usNDiscrete)
{ {
MB_MASTER_ASSERT(mbm_interface_ptr != NULL); MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts; mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
@ -775,14 +777,14 @@ esp_err_t mbc_tcp_master_create(void** handler)
status = xTaskCreate((void*)&modbus_tcp_master_task, status = xTaskCreate((void*)&modbus_tcp_master_task,
"modbus_tcp_master_task", "modbus_tcp_master_task",
MB_CONTROLLER_STACK_SIZE, MB_CONTROLLER_STACK_SIZE,
NULL, // No parameters NULL, // No parameters
MB_CONTROLLER_PRIORITY, MB_CONTROLLER_PRIORITY,
&mbm_opts->mbm_task_handle); &mbm_opts->mbm_task_handle);
if (status != pdPASS) { if (status != pdPASS) {
vTaskDelete(mbm_opts->mbm_task_handle); vTaskDelete(mbm_opts->mbm_task_handle);
MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM, MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
"mb controller task creation error, xTaskCreate() returns (0x%x).", "mb controller task creation error, xTaskCreate() returns (0x%x).",
(uint32_t)status); (uint32_t)status);
} }
MB_MASTER_ASSERT(mbm_opts->mbm_task_handle != NULL); // The task is created but handle is incorrect MB_MASTER_ASSERT(mbm_opts->mbm_task_handle != NULL); // The task is created but handle is incorrect

View File

@ -249,11 +249,6 @@ void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortPr
xMbPortConfig.eMbIpVer = xIpVersion; xMbPortConfig.eMbIpVer = xIpVersion;
} }
void vMBTCPPortMasterTaskStart(void)
{
vTaskResume(xMbPortConfig.xMbTcpTaskHandle);
}
// Function returns time left for response processing according to response timeout // Function returns time left for response processing according to response timeout
static int64_t xMBTCPPortMasterGetRespTimeLeft(MbSlaveInfo_t* pxInfo) static int64_t xMBTCPPortMasterGetRespTimeLeft(MbSlaveInfo_t* pxInfo)
{ {
@ -891,23 +886,13 @@ extern void vMBMasterPortEventClose(void);
extern void vMBMasterPortTimerClose(void); extern void vMBMasterPortTimerClose(void);
void void
vMBMasterTCPPortDisable(void) vMBMasterTCPPortEnable(void)
{ {
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) { vTaskResume(xMbPortConfig.xMbTcpTaskHandle);
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
if (pxInfo) {
xMBTCPPortMasterCloseConnection(pxInfo);
if (pxInfo->pucRcvBuf) {
free(pxInfo->pucRcvBuf);
}
free(pxInfo);
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
}
}
} }
void void
vMBMasterTCPPortClose(void) vMBMasterTCPPortDisable(void)
{ {
// Try to exit the task gracefully, so select could release its internal callbacks // Try to exit the task gracefully, so select could release its internal callbacks
// 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
@ -921,8 +906,23 @@ vMBMasterTCPPortClose(void)
vSemaphoreDelete(xShutdownSemaphore); vSemaphoreDelete(xShutdownSemaphore);
xShutdownSemaphore = NULL; xShutdownSemaphore = NULL;
} }
vMBMasterTCPPortDisable(); for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
if (pxInfo) {
xMBTCPPortMasterCloseConnection(pxInfo);
if (pxInfo->pucRcvBuf) {
free(pxInfo->pucRcvBuf);
}
free(pxInfo);
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
}
}
free(xMbPortConfig.pxMbSlaveInfo); free(xMbPortConfig.pxMbSlaveInfo);
}
void
vMBMasterTCPPortClose(void)
{
vQueueDelete(xMbPortConfig.xConnectQueue); vQueueDelete(xMbPortConfig.xConnectQueue);
vMBMasterPortTimerClose(); vMBMasterPortTimerClose();
// Release resources for the event queue. // Release resources for the event queue.

View File

@ -132,13 +132,6 @@ BOOL xMBTCPPortMasterWaitEvent(EventGroupHandle_t xEventHandle, EventBits_t xEve
*/ */
void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto); void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto);
/**
* Resume TCP/UDP Master processing task
*
* @return None
*/
void vMBTCPPortMasterTaskStart(void);
#ifdef __cplusplus #ifdef __cplusplus
PR_END_EXTERN_C PR_END_EXTERN_C
#endif #endif

View File

@ -79,7 +79,6 @@ static esp_err_t mbc_tcp_slave_start(void)
eMBPortProto proto = (mbs_opts->mbs_comm.ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP; eMBPortProto proto = (mbs_opts->mbs_comm.ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
eMBPortIpVer ip_ver = (mbs_opts->mbs_comm.ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6; eMBPortIpVer ip_ver = (mbs_opts->mbs_comm.ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
vMBTCPPortSlaveSetNetOpt(mbs_opts->mbs_comm.ip_netif_ptr, ip_ver, proto, (char*)mbs_opts->mbs_comm.ip_addr); vMBTCPPortSlaveSetNetOpt(mbs_opts->mbs_comm.ip_netif_ptr, ip_ver, proto, (char*)mbs_opts->mbs_comm.ip_addr);
vMBTCPPortSlaveStartServerTask();
status = eMBEnable(); status = eMBEnable();
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
@ -110,7 +109,7 @@ static esp_err_t mbc_tcp_slave_destroy(void)
(void)vQueueDelete(mbs_opts->mbs_notification_queue_handle); (void)vQueueDelete(mbs_opts->mbs_notification_queue_handle);
(void)vEventGroupDelete(mbs_opts->mbs_event_group); (void)vEventGroupDelete(mbs_opts->mbs_event_group);
(void)vMBTCPPortClose(); (void)vMBTCPPortClose();
mbs_interface_ptr = NULL;
vMBPortSetMode((UCHAR)MB_PORT_INACTIVE); vMBPortSetMode((UCHAR)MB_PORT_INACTIVE);
return ESP_OK; return ESP_OK;
} }

View File

@ -177,11 +177,6 @@ void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortPro
xConfig.pcBindAddr = pcBindAddrStr; xConfig.pcBindAddr = pcBindAddrStr;
} }
void vMBTCPPortSlaveStartServerTask(void)
{
vTaskResume(xConfig.xMbTcpTaskHandle);
}
static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr) static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
{ {
MB_PORT_CHECK(pcIPAddr, -1, "Wrong IP address pointer."); MB_PORT_CHECK(pcIPAddr, -1, "Wrong IP address pointer.");
@ -656,6 +651,11 @@ vMBTCPPortClose( )
vMBPortEventClose( ); vMBPortEventClose( );
} }
void vMBTCPPortEnable( void )
{
vTaskResume(xConfig.xMbTcpTaskHandle);
}
void void
vMBTCPPortDisable( void ) vMBTCPPortDisable( void )
{ {
@ -668,6 +668,7 @@ vMBTCPPortDisable( void )
xConfig.pxMbClientInfo[i] = NULL; xConfig.pxMbClientInfo[i] = NULL;
} }
} }
free(xConfig.pxMbClientInfo);
close(xListenSock); close(xListenSock);
xListenSock = -1; xListenSock = -1;
vMBTCPPortRespQueueDelete(xConfig.xRespQueueHandle); vMBTCPPortRespQueueDelete(xConfig.xRespQueueHandle);

View File

@ -101,13 +101,6 @@ typedef struct {
*/ */
void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto, CHAR* pcBindAddr); void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto, CHAR* pcBindAddr);
/**
* Resume TCP Slave processing task
*
* @return None
*/
void vMBTCPPortSlaveStartServerTask(void);
#ifdef __cplusplus #ifdef __cplusplus
PR_END_EXTERN_C PR_END_EXTERN_C
#endif #endif

View File

@ -98,7 +98,7 @@ static void start_mdns_service(void)
ESP_ERROR_CHECK(mdns_init()); ESP_ERROR_CHECK(mdns_init());
//set mDNS hostname (required if you want to advertise services) //set mDNS hostname (required if you want to advertise services)
ESP_ERROR_CHECK(mdns_hostname_set(hostname)); ESP_ERROR_CHECK(mdns_hostname_set(hostname));
ESP_LOGI(SLAVE_TAG, "mdns hostname set to: [%s]", hostname); ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
//set default mDNS instance name //set default mDNS instance name
ESP_ERROR_CHECK(mdns_instance_name_set(MB_MDNS_INSTANCE("esp32_"))); ESP_ERROR_CHECK(mdns_instance_name_set(MB_MDNS_INSTANCE("esp32_")));