forked from espressif/esp-modbus
Merge branch 'bugfix/fix_modbus_examples_print_format' into 'master'
esp_modbus: Fix Wno-format issues See merge request idf/esp-modbus!39
This commit is contained in:
@ -71,4 +71,4 @@ idf_component_register(SRCS "${srcs}"
|
||||
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
|
||||
REQUIRES ${requires}
|
||||
PRIV_REQUIRES esp_netif)
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
||||
|
@ -36,7 +36,7 @@ esp_err_t mbc_master_destroy(void)
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master destroy failure, error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t*
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master get cid info failure, error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uin
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master get parameter failure, error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr)
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master send request failure error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor,
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master set descriptor failure, error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -134,7 +134,7 @@ esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uin
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master set parameter failure, error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -154,7 +154,7 @@ esp_err_t mbc_master_setup(void* comm_info)
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master setup failure, error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -174,7 +174,7 @@ esp_err_t mbc_master_start(void)
|
||||
MB_MASTER_CHECK((error == ESP_OK),
|
||||
error,
|
||||
"Master start failure, error=(0x%x) (%s).",
|
||||
error, esp_err_to_name(error));
|
||||
(int)error, esp_err_to_name(error));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ esp_err_t mbc_slave_destroy(void)
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave destroy failure error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
// Destroy all opened descriptors
|
||||
mbc_slave_free_descriptors();
|
||||
free(slave_interface_ptr);
|
||||
@ -130,7 +130,7 @@ esp_err_t mbc_slave_setup(void* comm_info)
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave setup failure error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ esp_err_t mbc_slave_start(void)
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave start failure error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -190,7 +190,7 @@ esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave get parameter info failure error=(0x%x).",
|
||||
error);
|
||||
(int)error);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -209,7 +209,7 @@ esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)
|
||||
MB_SLAVE_CHECK((error == ESP_OK),
|
||||
ESP_ERR_INVALID_STATE,
|
||||
"Slave set descriptor failure error=(0x%x).",
|
||||
(uint16_t)error);
|
||||
(int)error);
|
||||
} else {
|
||||
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
||||
// Check if the address is already in the descriptor list
|
||||
@ -252,8 +252,8 @@ static esp_err_t mbc_slave_send_param_info(mb_event_group_t par_type, uint16_t m
|
||||
par_info.mb_offset = mb_offset;
|
||||
BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
|
||||
if (pdTRUE == status) {
|
||||
ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
|
||||
par_type, (uint32_t)par_address, par_size);
|
||||
ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%" PRIx32 ", %u",
|
||||
(int)par_type, (uint32_t)par_address, (unsigned)par_size);
|
||||
error = ESP_OK;
|
||||
} else if (errQUEUE_FULL == status) {
|
||||
ESP_LOGD(TAG, "Parameter queue is overflowed.");
|
||||
@ -269,7 +269,7 @@ static esp_err_t mbc_slave_send_param_access_notification(mb_event_group_t event
|
||||
esp_err_t err = ESP_FAIL;
|
||||
mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group, (EventBits_t)event);
|
||||
if (bits & event) {
|
||||
ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
|
||||
ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (int)event);
|
||||
err = ESP_OK;
|
||||
}
|
||||
return err;
|
||||
|
@ -7,6 +7,7 @@
|
||||
#ifndef _MB_IFACE_COMMON_H
|
||||
#define _MB_IFACE_COMMON_H
|
||||
|
||||
#include <inttypes.h> // needs to be included for default system types (such as PRIxx)
|
||||
#include "driver/uart.h" // for UART types
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -23,11 +24,11 @@ extern "C" {
|
||||
|
||||
// if cannot include esp_check then use custom check macro
|
||||
|
||||
#define MB_RETURN_ON_FALSE(a, err_code, tag, format, ...) do { \
|
||||
if (!(a)) { \
|
||||
ESP_LOGE(tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
|
||||
return err_code; \
|
||||
} \
|
||||
#define MB_RETURN_ON_FALSE(a, err_code, tag, format, ...) do { \
|
||||
if (!(a)) { \
|
||||
ESP_LOGE(tag, "%s(%" PRIu32 "): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
|
||||
return err_code; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#endif
|
||||
|
@ -19,7 +19,7 @@ extern "C" {
|
||||
#define MB_MASTER_CHECK(a, err_code, format, ...) MB_RETURN_ON_FALSE(a, err_code, TAG, format __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
#define MB_MASTER_ASSERT(con) do { \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%d, errno_str: !(%s)", errno, strerror(errno)); assert(0 && #con); } \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%u, errno_str: !(%s)", (unsigned)errno, strerror(errno)); assert(0 && #con); } \
|
||||
} while (0)
|
||||
|
||||
/*!
|
||||
|
@ -22,7 +22,7 @@ extern "C" {
|
||||
#define MB_SLAVE_CHECK(a, err_code, format, ...) MB_RETURN_ON_FALSE(a, err_code, TAG, format __VA_OPT__(,) __VA_ARGS__)
|
||||
|
||||
#define MB_SLAVE_ASSERT(con) do { \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%d, errno_str: !(%s)", errno, strerror(errno)); assert(0 && #con); } \
|
||||
if (!(con)) { ESP_LOGE(TAG, "assert errno:%u, errno_str: !(%s)", (unsigned)errno, strerror(errno)); assert(0 && #con); } \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
|
@ -39,6 +39,7 @@
|
||||
#ifndef _MB_CONFIG_H
|
||||
#define _MB_CONFIG_H
|
||||
|
||||
#include <inttypes.h> // needs to be included for default system types (such as PRIxx)
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
|
||||
#if __has_include("esp_idf_version.h")
|
||||
|
@ -351,7 +351,7 @@ eMBMasterPoll( void )
|
||||
{
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
ESP_LOGE( MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus );
|
||||
ESP_LOGE( MB_PORT_TAG, "%s:Frame send error. %u", __func__, (unsigned)eStatus );
|
||||
}
|
||||
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_TRANSMIT );
|
||||
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_SENT ) ) {
|
||||
@ -368,13 +368,13 @@ eMBMasterPoll( void )
|
||||
if ( ( eStatus == MB_ENOERR ) && ( ( ucRcvAddress == ucMBMasterGetDestAddress() )
|
||||
|| ( ucRcvAddress == MB_TCP_PSEUDO_ADDRESS) ) ) {
|
||||
if ( ( ucMBRcvFrame[MB_PDU_FUNC_OFF] & ~MB_FUNC_ERROR ) == ( ucMBSendFrame[MB_PDU_FUNC_OFF] ) ) {
|
||||
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("POLL receive buffer", (void*)ucMBRcvFrame, (uint16_t)usLength, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, (unsigned)eStatus);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("POLL receive buffer", (void*)ucMBRcvFrame, (unsigned)usLength, ESP_LOG_DEBUG);
|
||||
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
||||
} else {
|
||||
ESP_LOGE( MB_PORT_TAG, "Drop incorrect frame, receive_func(%u) != send_func(%u)",
|
||||
ucMBRcvFrame[MB_PDU_FUNC_OFF], ucMBSendFrame[MB_PDU_FUNC_OFF]);
|
||||
(UCHAR)ucMBRcvFrame[MB_PDU_FUNC_OFF], (UCHAR)ucMBSendFrame[MB_PDU_FUNC_OFF]);
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
}
|
||||
@ -382,7 +382,7 @@ eMBMasterPoll( void )
|
||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||
ESP_LOGD( MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).",
|
||||
__func__, ucRcvAddress, eStatus);
|
||||
__func__, (unsigned)ucRcvAddress, (unsigned)eStatus);
|
||||
}
|
||||
} else {
|
||||
// Ignore the `EV_MASTER_FRAME_RECEIVED` event because the respond timeout occurred
|
||||
@ -469,7 +469,7 @@ eMBMasterPoll( void )
|
||||
vMBMasterCBRequestSuccess( );
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE( MB_PORT_TAG, "%s: incorrect error type = %d.", __func__, errorType);
|
||||
ESP_LOGE( MB_PORT_TAG, "%s: incorrect error type = %u.", __func__, (unsigned)errorType);
|
||||
break;
|
||||
}
|
||||
vMBMasterPortTimersDisable( );
|
||||
@ -480,7 +480,7 @@ eMBMasterPoll( void )
|
||||
}
|
||||
} else {
|
||||
// Something went wrong and task unblocked but there are no any correct events set
|
||||
ESP_LOGE( MB_PORT_TAG, "%s: Unexpected event triggered 0x%02x.", __func__, eEvent );
|
||||
ESP_LOGE( MB_PORT_TAG, "%s: Unexpected event triggered 0x%02x.", __func__, (int)eEvent );
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
|
@ -78,7 +78,7 @@ vMBPortSetMode( UCHAR ucMode )
|
||||
BOOL xMBPortSerialWaitEvent(QueueHandle_t xMbUartQueue, uart_event_t* pxEvent, ULONG xTimeout)
|
||||
{
|
||||
BOOL xResult = (BaseType_t)xQueueReceive(xMbUartQueue, (void*)pxEvent, (TickType_t) xTimeout);
|
||||
ESP_LOGD(MB_PORT_TAG, "%s, UART event: %d ", __func__, pxEvent->type);
|
||||
ESP_LOGD(MB_PORT_TAG, "%s, UART event: %u ", __func__, (unsigned)pxEvent->type);
|
||||
return xResult;
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ xMBPortEventPost( eMBEventType eEvent )
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
if (xStatus != pdTRUE) {
|
||||
ESP_EARLY_LOGV(MB_PORT_TAG, "%s: Post message failure = %d.", __func__, xStatus);
|
||||
ESP_EARLY_LOGV(MB_PORT_TAG, "%s: Post message failure = %u.", __func__, (unsigned)xStatus);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,6 @@
|
||||
#include "port.h"
|
||||
#include "mbport.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "port_serial_master.h"
|
||||
|
||||
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
@ -149,7 +148,7 @@ xMBMasterPortEventGet( eMBMasterEventType* eEvent )
|
||||
xEventGroupSetBits( xEventGroupMasterConfirmHdl, *eEvent );
|
||||
xEventHappened = TRUE;
|
||||
} else {
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered = %d.", __func__, uxBits);
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered = %u.", __func__, (unsigned)uxBits);
|
||||
*eEvent = (eMBMasterEventType)uxBits;
|
||||
xEventHappened = FALSE;
|
||||
}
|
||||
@ -180,7 +179,7 @@ BOOL xMBMasterRunResTake( LONG lTimeOut )
|
||||
pdFALSE, // Don't wait for both bits, either bit will do.
|
||||
lTimeOut); // Resource wait timeout.
|
||||
MB_PORT_CHECK((uxBits == MB_EVENT_RESOURCE), FALSE , "Take resource failure.");
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Take resource (%x) (%lu ticks).", __func__, uxBits, lTimeOut);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Take resource (%" PRIx32 ") (%" PRIu32 " ticks).", __func__, (uint32_t)uxBits, (uint32_t)lTimeOut);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@ -194,7 +193,7 @@ void vMBMasterRunResRelease( void )
|
||||
if (uxBits != MB_EVENT_RESOURCE) {
|
||||
// The returned resource mask may be = 0, if the task waiting for it is unblocked.
|
||||
// This is not an error but expected behavior.
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: Release resource (%x) fail.", __func__, uxBits);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: Release resource (%" PRIx32 ") fail.", __func__, (uint32_t)uxBits);
|
||||
}
|
||||
}
|
||||
|
||||
@ -227,7 +226,7 @@ void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData, U
|
||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_RECEIVE_DATA);
|
||||
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_ERROR_RECEIVE_DATA' failed!", __func__);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback receive data timeout failure.", __func__);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("Err rcv buf", (void*)pucPDUData, (uint16_t)ucPDULength, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("Err rcv buf", (void *)pucPDUData, (USHORT)ucPDULength, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,7 +244,7 @@ void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUDat
|
||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_EXECUTE_FUNCTION);
|
||||
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_ERROR_EXECUTE_FUNCTION' failed!", __func__);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback execute data handler failure.", __func__);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("Exec func buf", (void*)pucPDUData, (uint16_t)ucPDULength, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL("Exec func buf", (void*)pucPDUData, (USHORT)ucPDULength, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -283,10 +282,10 @@ eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
|
||||
portMAX_DELAY ); // Wait forever for either bit to be set.
|
||||
xRecvedEvent = (eMBMasterEventType)(uxBits);
|
||||
if (xRecvedEvent) {
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: returned event = 0x%x", __func__, xRecvedEvent);
|
||||
ESP_LOGD(MB_PORT_TAG,"%s: returned event = 0x%x", __func__, (int)xRecvedEvent);
|
||||
if (!(xRecvedEvent & MB_EVENT_REQ_MASK)) {
|
||||
// if we wait for certain event bits but get from poll subset
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: incorrect event set = 0x%x", __func__, xRecvedEvent);
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: incorrect event set = 0x%x", __func__, (int)xRecvedEvent);
|
||||
}
|
||||
xEventGroupSetBits( xEventGroupMasterConfirmHdl, (xRecvedEvent & MB_EVENT_REQ_MASK) );
|
||||
if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_PROCESS_SUCCESS)) {
|
||||
@ -299,7 +298,7 @@ eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
|
||||
eErrStatus = MB_MRE_EXE_FUN;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event or timeout xRecvedEvent = 0x%x", __func__, uxBits);
|
||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event or timeout xRecvedEvent = 0x%" PRIx32 "", __func__, (uint32_t)uxBits);
|
||||
// https://github.com/espressif/esp-idf/issues/5275
|
||||
// if a no event is received, that means vMBMasterPortEventClose()
|
||||
// has been closed, so event group has been deleted by FreeRTOS, which
|
||||
|
@ -96,7 +96,7 @@ static USHORT usMBPortSerialRxPoll(size_t xEventSize)
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
pxMBPortCBTimerExpired();
|
||||
#endif
|
||||
ESP_LOGD(TAG, "RX: %d bytes\n", usCnt);
|
||||
ESP_LOGD(TAG, "RX: %u bytes\n", (unsigned)usCnt);
|
||||
}
|
||||
return usCnt;
|
||||
}
|
||||
@ -112,7 +112,7 @@ BOOL xMBPortSerialTxPoll(void)
|
||||
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
||||
bNeedPoll = pxMBFrameCBTransmitterEmpty( ); // callback to transmit FSM
|
||||
}
|
||||
ESP_LOGD(TAG, "MB_TX_buffer send: (%d) bytes\n", (uint16_t)usCount);
|
||||
ESP_LOGD(TAG, "MB_TX_buffer send: (%u) bytes\n", (unsigned)usCount);
|
||||
// Waits while UART sending the packet
|
||||
esp_err_t xTxStatus = uart_wait_tx_done(ucUartNumber, MB_SERIAL_TX_TOUT_TICKS);
|
||||
vMBPortSerialEnable(TRUE, FALSE);
|
||||
@ -128,11 +128,11 @@ static void vUartTask(void *pvParameters)
|
||||
USHORT usResult = 0;
|
||||
for(;;) {
|
||||
if (xMBPortSerialWaitEvent(xMbUartQueue, (void*)&xEvent, portMAX_DELAY)) {
|
||||
ESP_LOGD(TAG, "MB_uart[%d] event:", ucUartNumber);
|
||||
ESP_LOGD(TAG, "MB_uart[%u] event:", (unsigned)ucUartNumber);
|
||||
switch(xEvent.type) {
|
||||
//Event of UART receving data
|
||||
case UART_DATA:
|
||||
ESP_LOGD(TAG,"Data event, length: %d", xEvent.size);
|
||||
ESP_LOGD(TAG,"Data event, length: %u", (unsigned)xEvent.size);
|
||||
// This flag set in the event means that no more
|
||||
// data received during configured timeout and UART TOUT feature is triggered
|
||||
if (xEvent.timeout_flag) {
|
||||
@ -140,34 +140,34 @@ static void vUartTask(void *pvParameters)
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(ucUartNumber, &xEvent.size));
|
||||
// Read received data and send it to modbus stack
|
||||
usResult = usMBPortSerialRxPoll(xEvent.size);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %d bytes", usResult);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %u bytes", (unsigned)usResult);
|
||||
}
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
ESP_LOGD(TAG, "hw fifo overflow\n");
|
||||
ESP_LOGD(TAG, "hw fifo overflow");
|
||||
xQueueReset(xMbUartQueue);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGD(TAG, "ring buffer full\n");
|
||||
ESP_LOGD(TAG, "ring buffer full");
|
||||
xQueueReset(xMbUartQueue);
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
ESP_LOGD(TAG, "uart rx break\n");
|
||||
ESP_LOGD(TAG, "uart rx break");
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGD(TAG, "uart parity error\n");
|
||||
ESP_LOGD(TAG, "uart parity error");
|
||||
break;
|
||||
//Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGD(TAG, "uart frame error\n");
|
||||
ESP_LOGD(TAG, "uart frame error");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGD(TAG, "uart event type: %d\n", xEvent.type);
|
||||
ESP_LOGD(TAG, "uart event type: %u", (unsigned)xEvent.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -195,7 +195,7 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
||||
ucParity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %u", (unsigned)eParity);
|
||||
return FALSE;
|
||||
}
|
||||
switch(ucDataBits){
|
||||
@ -231,17 +231,17 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
||||
// Set UART config
|
||||
xErr = uart_param_config(ucUartNumber, &xUartConfig);
|
||||
MB_PORT_CHECK((xErr == ESP_OK),
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", xErr);
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", (int)xErr);
|
||||
// Install UART driver, and get the queue.
|
||||
xErr = uart_driver_install(ucUartNumber, MB_SERIAL_BUF_SIZE, MB_SERIAL_BUF_SIZE,
|
||||
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", xErr);
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", (int)xErr);
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
||||
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", xErr);
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (int)xErr);
|
||||
#endif
|
||||
|
||||
// Set always timeout flag to trigger timeout interrupt even after rx fifo full
|
||||
@ -257,7 +257,7 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
||||
// Force exit from function with failure
|
||||
MB_PORT_CHECK(FALSE, FALSE,
|
||||
"mb stack serial task creation error. xTaskCreate() returned (0x%x).",
|
||||
xStatus);
|
||||
(int)xStatus);
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Suspend serial task while stack is not started
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ static BOOL xMBMasterPortRxSemaTake( LONG lTimeOut )
|
||||
BaseType_t xStatus = pdTRUE;
|
||||
xStatus = xSemaphoreTake(xMasterSemaRxHandle, lTimeOut );
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE , "%s: RX semaphore take failure.", __func__);
|
||||
ESP_LOGV(MB_PORT_TAG,"%s:Take RX semaphore (%lu ticks).", __func__, lTimeOut);
|
||||
ESP_LOGV(MB_PORT_TAG,"%s:Take RX semaphore (%" PRIu64 " ticks).", __func__, (uint64_t)lTimeOut);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@ -117,11 +117,11 @@ void vMBMasterRxFlush( void )
|
||||
esp_err_t xErr = ESP_OK;
|
||||
for (int xCount = 0; (xCount < MB_SERIAL_RX_FLUSH_RETRY) && xSize; xCount++) {
|
||||
xErr = uart_get_buffered_data_len(ucUartNumber, &xSize);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", xErr);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", (int)xErr);
|
||||
BaseType_t xStatus = xQueueReset(xMbUartQueue);
|
||||
if (xStatus) {
|
||||
xErr = uart_flush_input(ucUartNumber);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", xErr);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), ; , "mb flush serial fail, error = 0x%x.", (int)xErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -158,7 +158,7 @@ static USHORT usMBMasterPortSerialRxPoll(size_t xEventSize)
|
||||
}
|
||||
// The buffer is transferred into Modbus stack and is not needed here any more
|
||||
uart_flush_input(ucUartNumber);
|
||||
ESP_LOGD(TAG, "Received data: %d(bytes in buffer)\n", (uint32_t)usCnt);
|
||||
ESP_LOGD(TAG, "Received data: %u(bytes in buffer)", (unsigned)usCnt);
|
||||
#if !CONFIG_FMB_TIMER_PORT_ENABLED
|
||||
vMBMasterSetCurTimerMode(MB_TMODE_T35);
|
||||
xStatus = pxMBMasterPortCBTimerExpired();
|
||||
@ -168,7 +168,8 @@ static USHORT usMBMasterPortSerialRxPoll(size_t xEventSize)
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: bRxState disabled but junk data (%d bytes) received. ", __func__, xEventSize);
|
||||
ESP_LOGE(TAG, "%s: bRxState disabled but junk data (%u bytes) received. ",
|
||||
__func__, (unsigned)xEventSize);
|
||||
}
|
||||
return usCnt;
|
||||
}
|
||||
@ -184,7 +185,7 @@ BOOL xMBMasterPortSerialTxPoll(void)
|
||||
// Calls the modbus stack callback function to let it fill the UART transmit buffer.
|
||||
bNeedPoll = pxMBMasterFrameCBTransmitterEmpty( ); // callback to transmit FSM
|
||||
}
|
||||
ESP_LOGD(TAG, "MB_TX_buffer sent: (%d) bytes.", (uint16_t)(usCount - 1));
|
||||
ESP_LOGD(TAG, "MB_TX_buffer sent: (%u) bytes.", (unsigned)(usCount - 1));
|
||||
// Waits while UART sending the packet
|
||||
esp_err_t xTxStatus = uart_wait_tx_done(ucUartNumber, MB_SERIAL_TX_TOUT_TICKS);
|
||||
vMBMasterPortSerialEnable(TRUE, FALSE);
|
||||
@ -201,11 +202,11 @@ static void vUartTask(void* pvParameters)
|
||||
USHORT usResult = 0;
|
||||
for(;;) {
|
||||
if (xMBPortSerialWaitEvent(xMbUartQueue, (void*)&xEvent, portMAX_DELAY)) {
|
||||
ESP_LOGD(TAG, "MB_uart[%d] event:", ucUartNumber);
|
||||
ESP_LOGD(TAG, "MB_uart[%u] event:", (unsigned)ucUartNumber);
|
||||
switch(xEvent.type) {
|
||||
//Event of UART receiving data
|
||||
case UART_DATA:
|
||||
ESP_LOGD(TAG,"Data event, len: %d.", xEvent.size);
|
||||
ESP_LOGD(TAG,"Data event, len: %u.", (unsigned)xEvent.size);
|
||||
// This flag set in the event means that no more
|
||||
// data received during configured timeout and UART TOUT feature is triggered
|
||||
if (xEvent.timeout_flag) {
|
||||
@ -219,7 +220,7 @@ static void vUartTask(void* pvParameters)
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(ucUartNumber, &xEvent.size));
|
||||
// Read received data and send it to modbus stack
|
||||
usResult = usMBMasterPortSerialRxPoll(xEvent.size);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %d bytes", usResult);
|
||||
ESP_LOGD(TAG,"Timeout occured, processed: %u bytes", (unsigned)usResult);
|
||||
}
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
@ -250,7 +251,7 @@ static void vUartTask(void* pvParameters)
|
||||
uart_flush_input(ucUartNumber);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGD(TAG, "uart event type: %d.", xEvent.type);
|
||||
ESP_LOGD(TAG, "uart event type: %u.", (unsigned)xEvent.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -278,7 +279,7 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
||||
ucParity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
|
||||
ESP_LOGE(TAG, "Incorrect parity option: %u", (unsigned)eParity);
|
||||
return FALSE;
|
||||
}
|
||||
switch(ucDataBits){
|
||||
@ -314,16 +315,16 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
||||
// Set UART config
|
||||
xErr = uart_param_config(ucUartNumber, &xUartConfig);
|
||||
MB_PORT_CHECK((xErr == ESP_OK),
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", xErr);
|
||||
FALSE, "mb config failure, uart_param_config() returned (0x%x).", (int)xErr);
|
||||
// Install UART driver, and get the queue.
|
||||
xErr = uart_driver_install(ucUartNumber, MB_SERIAL_BUF_SIZE, MB_SERIAL_BUF_SIZE,
|
||||
MB_QUEUE_LENGTH, &xMbUartQueue, MB_PORT_SERIAL_ISR_FLAG);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", xErr);
|
||||
"mb serial driver failure, uart_driver_install() returned (0x%x).", (int)xErr);
|
||||
// Set timeout for TOUT interrupt (T3.5 modbus time)
|
||||
xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
|
||||
MB_PORT_CHECK((xErr == ESP_OK), FALSE,
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", xErr);
|
||||
"mb serial set rx timeout failure, uart_set_rx_timeout() returned (0x%x).", (int)xErr);
|
||||
|
||||
// Set always timeout flag to trigger timeout interrupt even after rx fifo full
|
||||
uart_set_always_rx_timeout(ucUartNumber, true);
|
||||
@ -338,8 +339,7 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
||||
vTaskDelete(xMbTaskHandle);
|
||||
// Force exit from function with failure
|
||||
MB_PORT_CHECK(FALSE, FALSE,
|
||||
"mb stack serial task creation error. xTaskCreate() returned (0x%x).",
|
||||
xStatus);
|
||||
"mb stack serial task creation error. xTaskCreate() returned (0x%x).", (int)xStatus);
|
||||
} else {
|
||||
vTaskSuspend(xMbTaskHandle); // Suspend serial task while stack is not started
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ static void IRAM_ATTR vTimerAlarmCBHandler(void *param)
|
||||
{
|
||||
pxMBMasterPortCBTimerExpired(); // Timer expired callback function
|
||||
pxTimerContext->xTimerState = TRUE;
|
||||
ESP_EARLY_LOGD(TAG, "Timer mode: (%d) triggered", xMBMasterGetCurTimerMode());
|
||||
ESP_EARLY_LOGD(TAG, "Timer mode: (%u) triggered", (unsigned)xMBMasterGetCurTimerMode());
|
||||
}
|
||||
|
||||
BOOL xMBMasterPortTimersInit(USHORT usTimeOut50us)
|
||||
|
@ -71,12 +71,11 @@ static esp_err_t mbc_serial_master_setup(void* comm_info)
|
||||
const mb_master_comm_info_t* comm_info_ptr = (mb_master_comm_info_t*)comm_info;
|
||||
// Check communication options
|
||||
MB_MASTER_CHECK(((comm_info_ptr->mode == MB_MODE_RTU) || (comm_info_ptr->mode == MB_MODE_ASCII)),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).",
|
||||
(uint32_t)comm_info_ptr->mode);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (%u).", (unsigned)comm_info_ptr->mode);
|
||||
MB_MASTER_CHECK((comm_info_ptr->port <= UART_NUM_MAX), ESP_ERR_INVALID_ARG,
|
||||
"mb wrong port to set = (0x%x).", (uint32_t)comm_info_ptr->port);
|
||||
"mb wrong port to set = (%u).", (unsigned)comm_info_ptr->port);
|
||||
MB_MASTER_CHECK((comm_info_ptr->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG,
|
||||
"mb wrong parity option = (0x%x).", (uint32_t)comm_info_ptr->parity);
|
||||
"mb wrong parity option = (%u).", (unsigned)comm_info_ptr->parity);
|
||||
// Save the communication options
|
||||
mbm_opts->mbm_comm = *(mb_communication_info_t*)comm_info_ptr;
|
||||
return ESP_OK;
|
||||
@ -98,10 +97,10 @@ static esp_err_t mbc_serial_master_start(void)
|
||||
MB_PORT_PARITY_GET(comm_info->parity));
|
||||
|
||||
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", (int)status);
|
||||
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);
|
||||
"mb stack set slave ID failure, eMBMasterEnable() returned (0x%x).", (int)status);
|
||||
// Set the mbcontroller start flag
|
||||
EventBits_t flag = xEventGroupSetBits(mbm_opts->mbm_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
@ -122,7 +121,7 @@ static esp_err_t mbc_serial_master_destroy(void)
|
||||
EventBits_t flag = xEventGroupClearBits(mbm_opts->mbm_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
MB_MASTER_CHECK((flag & MB_EVENT_STACK_STARTED),
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
// Desable and then destroy the Modbus stack
|
||||
mb_error = eMBMasterDisable();
|
||||
MB_MASTER_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
|
||||
@ -130,7 +129,7 @@ static esp_err_t mbc_serial_master_destroy(void)
|
||||
(void)vEventGroupDelete(mbm_opts->mbm_event_group);
|
||||
mb_error = eMBMasterClose();
|
||||
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).", (int)mb_error);
|
||||
free(mbm_interface_ptr); // free the memory allocated for options
|
||||
vMBPortSetMode((UCHAR)MB_PORT_INACTIVE);
|
||||
mbm_interface_ptr = NULL;
|
||||
@ -234,8 +233,7 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
|
||||
(USHORT)mb_size, (LONG) MB_RESPONSE_TICS );
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ",
|
||||
__FUNCTION__, mb_command);
|
||||
ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ", __FUNCTION__, (unsigned)mb_command);
|
||||
mb_error = MB_MRE_NO_REG;
|
||||
break;
|
||||
}
|
||||
@ -266,8 +264,7 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect return code (%x) ",
|
||||
__FUNCTION__, mb_error);
|
||||
ESP_LOGE(TAG, "%s: Incorrect return code (%x) ", __FUNCTION__, (int)mb_error);
|
||||
error = ESP_FAIL;
|
||||
break;
|
||||
}
|
||||
@ -321,13 +318,11 @@ static uint8_t mbc_serial_master_get_command(mb_param_type_t param_type, mb_para
|
||||
if (mode != MB_PARAM_WRITE) {
|
||||
command = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: Incorrect mode (%u)",
|
||||
__FUNCTION__, (uint8_t)mode);
|
||||
ESP_LOGE(TAG, "%s: Incorrect mode (%u)", __FUNCTION__, (unsigned)mode);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u)",
|
||||
__FUNCTION__, param_type);
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u)", __FUNCTION__, (unsigned)param_type);
|
||||
break;
|
||||
}
|
||||
return command;
|
||||
@ -368,8 +363,7 @@ static esp_err_t mbc_serial_master_set_request(char* name, mb_param_mode_t mode,
|
||||
request->reg_start = reg_ptr->mb_reg_start;
|
||||
request->reg_size = reg_ptr->mb_size;
|
||||
request->command = mbc_serial_master_get_command(reg_ptr->mb_param_type, mode);
|
||||
MB_MASTER_CHECK((request->command > 0),
|
||||
ESP_ERR_INVALID_ARG,
|
||||
MB_MASTER_CHECK((request->command > 0), ESP_ERR_INVALID_ARG,
|
||||
"mb incorrect command or parameter type.");
|
||||
if (reg_data != NULL) {
|
||||
*reg_data = *reg_ptr; // Set the cid registered parameter data
|
||||
@ -399,16 +393,16 @@ static esp_err_t mbc_serial_master_get_parameter(uint16_t cid, char* name,
|
||||
error = mbc_serial_master_send_request(&request, value_ptr);
|
||||
if (error == ESP_OK) {
|
||||
ESP_LOGD(TAG, "%s: Good response for get cid(%u) = %s",
|
||||
__FUNCTION__, (int)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
} else {
|
||||
ESP_LOGD(TAG, "%s: Bad response to get cid(%u) = %s",
|
||||
__FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
}
|
||||
// Set the type of parameter found in the table
|
||||
*type = reg_info.param_type;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.",
|
||||
__FUNCTION__, reg_info.cid);
|
||||
__FUNCTION__, (unsigned)reg_info.cid);
|
||||
error = ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return error;
|
||||
@ -434,16 +428,16 @@ static esp_err_t mbc_serial_master_set_parameter(uint16_t cid, char* name,
|
||||
error = mbc_serial_master_send_request(&request, value_ptr);
|
||||
if (error == ESP_OK) {
|
||||
ESP_LOGD(TAG, "%s: Good response for set cid(%u) = %s",
|
||||
__FUNCTION__, (int)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
} else {
|
||||
ESP_LOGD(TAG, "%s: Bad response to set cid(%u) = %s",
|
||||
__FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
}
|
||||
// Set the type of parameter found in the table
|
||||
*type = reg_info.param_type;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: The requested cid(%u) not found in the data dictionary.",
|
||||
__FUNCTION__, reg_info.cid);
|
||||
__FUNCTION__, (unsigned)reg_info.cid);
|
||||
error = ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return error;
|
||||
@ -555,9 +549,9 @@ eMBErrorCode eMBRegCoilsCBSerialMaster(UCHAR* pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
MB_MASTER_CHECK((mbm_interface_ptr != NULL),
|
||||
MB_EILLSTATE, "Master interface uninitialized.");
|
||||
MB_EILLSTATE, "Master interface uninitialized.");
|
||||
MB_MASTER_CHECK((pucRegBuffer != NULL),
|
||||
MB_EINVAL, "Master stack processing error.");
|
||||
MB_EINVAL, "Master stack processing error.");
|
||||
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
|
||||
USHORT usRegCoilNregs = (USHORT)mbm_opts->mbm_reg_buffer_size;
|
||||
UCHAR* pucRegCoilsBuf = (UCHAR*)mbm_opts->mbm_reg_buffer_ptr;
|
||||
@ -680,8 +674,7 @@ esp_err_t mbc_serial_master_create(void** handler)
|
||||
if (status != pdPASS) {
|
||||
vTaskDelete(mbm_opts->mbm_task_handle);
|
||||
MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).",
|
||||
(uint32_t)status);
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).", (int)status);
|
||||
}
|
||||
MB_MASTER_ASSERT(mbm_opts->mbm_task_handle != NULL); // The task is created but handle is incorrect
|
||||
|
||||
|
@ -59,15 +59,14 @@ static esp_err_t mbc_serial_slave_setup(void* comm_info)
|
||||
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
||||
mb_slave_comm_info_t* comm_settings = (mb_slave_comm_info_t*)comm_info;
|
||||
MB_SLAVE_CHECK(((comm_settings->mode == MB_MODE_RTU) || (comm_settings->mode == MB_MODE_ASCII)),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).",
|
||||
(uint32_t)comm_settings->mode);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (%u).",
|
||||
(unsigned)comm_settings->mode);
|
||||
MB_SLAVE_CHECK((comm_settings->slave_addr <= MB_ADDRESS_MAX),
|
||||
ESP_ERR_INVALID_ARG, "mb wrong slave address = (0x%x).",
|
||||
(uint32_t)comm_settings->slave_addr);
|
||||
ESP_ERR_INVALID_ARG, "mb wrong slave address = (%u).", (unsigned)comm_settings->slave_addr);
|
||||
MB_SLAVE_CHECK((comm_settings->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
|
||||
"mb wrong port to set = (0x%x).", (uint32_t)comm_settings->port);
|
||||
"mb wrong port to set = (%u).", (unsigned)comm_settings->port);
|
||||
MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG,
|
||||
"mb wrong parity option = (0x%x).", (uint32_t)comm_settings->parity);
|
||||
"mb wrong parity option = (%u).", (unsigned)comm_settings->parity);
|
||||
|
||||
// Set communication options of the controller
|
||||
mbs_opts->mbs_comm = *(mb_communication_info_t*)comm_settings;
|
||||
@ -92,10 +91,10 @@ static esp_err_t mbc_serial_slave_start(void)
|
||||
MB_PORT_PARITY_GET(comm_info->parity));
|
||||
|
||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", (int)status);
|
||||
status = eMBEnable();
|
||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb stack set slave ID failure, eMBEnable() returned (0x%x).", (uint32_t)status);
|
||||
"mb stack set slave ID failure, eMBEnable() returned (0x%x).", (int)status);
|
||||
// Set the mbcontroller start flag
|
||||
EventBits_t flag = xEventGroupSetBits(mbs_opts->mbs_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
@ -154,7 +153,7 @@ static esp_err_t mbc_serial_slave_destroy(void)
|
||||
EventBits_t flag = xEventGroupClearBits(mbs_opts->mbs_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
MB_SLAVE_CHECK((flag & MB_EVENT_STACK_STARTED),
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
// Disable and then destroy the Modbus stack
|
||||
mb_error = eMBDisable();
|
||||
MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
|
||||
@ -163,7 +162,7 @@ static esp_err_t mbc_serial_slave_destroy(void)
|
||||
(void)vEventGroupDelete(mbs_opts->mbs_event_group);
|
||||
mb_error = eMBClose();
|
||||
MB_SLAVE_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).", (int)mb_error);
|
||||
mbs_interface_ptr = NULL;
|
||||
vMBPortSetMode((UCHAR)MB_PORT_INACTIVE);
|
||||
return ESP_OK;
|
||||
@ -201,7 +200,7 @@ esp_err_t mbc_serial_slave_create(void** handler)
|
||||
MB_CONTROLLER_NOTIFY_QUEUE_SIZE,
|
||||
sizeof(mb_param_info_t));
|
||||
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
||||
ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
||||
ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
||||
// Create Modbus controller task
|
||||
status = xTaskCreatePinnedToCore((void*)&modbus_slave_task,
|
||||
"modbus_slave_task",
|
||||
@ -213,8 +212,7 @@ esp_err_t mbc_serial_slave_create(void** handler)
|
||||
if (status != pdPASS) {
|
||||
vTaskDelete(mbs_opts->mbs_task_handle);
|
||||
MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).",
|
||||
(uint32_t)status);
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).", (int)status);
|
||||
}
|
||||
MB_SLAVE_ASSERT(mbs_opts->mbs_task_handle != NULL); // The task is created but handle is incorrect
|
||||
|
||||
|
@ -120,12 +120,11 @@ static esp_err_t mbc_tcp_master_setup(void* comm_info)
|
||||
const mb_communication_info_t* comm_info_ptr = (mb_communication_info_t*)comm_info;
|
||||
// Check communication options
|
||||
MB_MASTER_CHECK((comm_info_ptr->ip_mode == MB_MODE_TCP),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).",
|
||||
(uint32_t)comm_info_ptr->ip_mode);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (%u).", (unsigned)comm_info_ptr->ip_mode);
|
||||
MB_MASTER_CHECK((comm_info_ptr->ip_addr != NULL),
|
||||
ESP_ERR_INVALID_ARG, "mb wrong slave ip address table.");
|
||||
MB_MASTER_CHECK(((comm_info_ptr->ip_addr_type == MB_IPV4) || (comm_info_ptr->ip_addr_type == MB_IPV6)),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect addr type = (0x%x).", (uint8_t)comm_info_ptr->ip_addr_type);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect addr type = (%u).", (unsigned)comm_info_ptr->ip_addr_type);
|
||||
MB_MASTER_CHECK((comm_info_ptr->ip_netif_ptr != NULL),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect iface address.");
|
||||
// Save the communication options
|
||||
@ -159,7 +158,7 @@ static esp_err_t mbc_tcp_master_start(void)
|
||||
|
||||
status = eMBMasterEnable();
|
||||
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb stack enable failure, eMBMasterEnable() returned (0x%x).", (uint32_t)status);
|
||||
"mb stack enable failure, eMBMasterEnable() returned (0x%x).", (int)status);
|
||||
|
||||
// Add slave IP address for each slave to initialize connection
|
||||
mb_slave_addr_entry_t *p_slave_info;
|
||||
@ -194,7 +193,7 @@ 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_error = eMBMasterClose();
|
||||
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).", (int)mb_error);
|
||||
// Stop polling by clearing correspondent bit in the event group
|
||||
xEventGroupClearBits(mbm_opts->mbm_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
@ -237,7 +236,7 @@ static esp_err_t mbc_tcp_master_set_descriptor(const mb_parameter_descriptor_t*
|
||||
// Add it to slave list if not there.
|
||||
if (!p_slave) {
|
||||
// Is the IP address correctly defined for the slave?
|
||||
MB_MASTER_CHECK((comm_ip_table[slave_cnt]), ESP_ERR_INVALID_STATE, "mb missing IP address for cid #%d.", reg_ptr->cid);
|
||||
MB_MASTER_CHECK((comm_ip_table[slave_cnt]), ESP_ERR_INVALID_STATE, "mb missing IP address for cid #%u.", (unsigned)reg_ptr->cid);
|
||||
// Add slave to the list
|
||||
MB_MASTER_ASSERT(mbc_tcp_master_add_slave(idx, reg_ptr->mb_slave_addr, comm_ip_table[slave_cnt++]) == ESP_OK);
|
||||
}
|
||||
@ -274,52 +273,51 @@ 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
|
||||
switch(mb_command)
|
||||
{
|
||||
case MB_FUNC_READ_COILS:
|
||||
mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_SINGLE_COIL:
|
||||
mb_error = eMBMasterReqWriteCoil((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
*(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_MULTIPLE_COILS:
|
||||
mb_error = eMBMasterReqWriteMultipleCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (UCHAR *)data_ptr,
|
||||
(LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_DISCRETE_INPUTS:
|
||||
mb_error = eMBMasterReqReadDiscreteInputs((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_HOLDING_REGISTER:
|
||||
mb_error = eMBMasterReqReadHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_REGISTER:
|
||||
mb_error = eMBMasterReqWriteHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
*(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_COILS:
|
||||
mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_SINGLE_COIL:
|
||||
mb_error = eMBMasterReqWriteCoil((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
*(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_MULTIPLE_COILS:
|
||||
mb_error = eMBMasterReqWriteMultipleCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (UCHAR *)data_ptr,
|
||||
(LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_DISCRETE_INPUTS:
|
||||
mb_error = eMBMasterReqReadDiscreteInputs((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_HOLDING_REGISTER:
|
||||
mb_error = eMBMasterReqReadHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_WRITE_REGISTER:
|
||||
mb_error = eMBMasterReqWriteHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
*(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
|
||||
case MB_FUNC_WRITE_MULTIPLE_REGISTERS:
|
||||
mb_error = eMBMasterReqWriteMultipleHoldingRegister((UCHAR)mb_slave_addr,
|
||||
(USHORT)mb_offset, (USHORT)mb_size,
|
||||
(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READWRITE_MULTIPLE_REGISTERS:
|
||||
mb_error = eMBMasterReqReadWriteMultipleHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (USHORT *)data_ptr,
|
||||
case MB_FUNC_WRITE_MULTIPLE_REGISTERS:
|
||||
mb_error = eMBMasterReqWriteMultipleHoldingRegister((UCHAR)mb_slave_addr,
|
||||
(USHORT)mb_offset, (USHORT)mb_size,
|
||||
(LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_INPUT_REGISTER:
|
||||
mb_error = eMBMasterReqReadInputRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ",
|
||||
__FUNCTION__, mb_command);
|
||||
mb_error = MB_MRE_NO_REG;
|
||||
break;
|
||||
(USHORT *)data_ptr, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READWRITE_MULTIPLE_REGISTERS:
|
||||
mb_error = eMBMasterReqReadWriteMultipleHoldingRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (USHORT *)data_ptr,
|
||||
(USHORT)mb_offset, (USHORT)mb_size,
|
||||
(LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
case MB_FUNC_READ_INPUT_REGISTER:
|
||||
mb_error = eMBMasterReqReadInputRegister((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||
(USHORT)mb_size, (LONG)MB_RESPONSE_TIMEOUT);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect function in request (%u) ", __FUNCTION__, (unsigned)mb_command);
|
||||
mb_error = MB_MRE_NO_REG;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -348,7 +346,7 @@ static esp_err_t mbc_tcp_master_send_request(mb_param_request_t* request, void*
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect return code (%x) ", __FUNCTION__, mb_error);
|
||||
ESP_LOGE(TAG, "%s: Incorrect return code (0x%x) ", __FUNCTION__, (unsigned)mb_error);
|
||||
error = ESP_FAIL;
|
||||
break;
|
||||
}
|
||||
@ -392,11 +390,11 @@ static uint8_t mbc_tcp_master_get_command(mb_param_type_t param_type, mb_param_m
|
||||
if (mode != MB_PARAM_WRITE) {
|
||||
command = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: Incorrect mode (%u)", __FUNCTION__, (uint8_t)mode);
|
||||
ESP_LOGE(TAG, "%s: Incorrect mode (%u)", __FUNCTION__, (unsigned)mode);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u)", __FUNCTION__, param_type);
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u)", __FUNCTION__, (unsigned)param_type);
|
||||
break;
|
||||
}
|
||||
return command;
|
||||
@ -428,7 +426,7 @@ static esp_err_t mbc_tcp_master_set_param_data(void* dest, void* src, mb_descr_t
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "%s: Incorrect param type (%u).",
|
||||
__FUNCTION__, (uint16_t)param_type);
|
||||
__FUNCTION__, (unsigned)param_type);
|
||||
err = ESP_ERR_NOT_SUPPORTED;
|
||||
break;
|
||||
}
|
||||
@ -508,15 +506,14 @@ static esp_err_t mbc_tcp_master_get_parameter(uint16_t cid, char* name, uint8_t*
|
||||
}
|
||||
} else {
|
||||
ESP_LOGD(TAG, "%s: Bad response to get cid(%u) = %s",
|
||||
__FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
error = ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
free(pdata);
|
||||
// Set the type of parameter found in the table
|
||||
*type = reg_info.param_type;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.",
|
||||
__FUNCTION__, reg_info.cid);
|
||||
ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.", __FUNCTION__, reg_info.cid);
|
||||
error = ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return error;
|
||||
@ -555,14 +552,14 @@ static esp_err_t mbc_tcp_master_set_parameter(uint16_t cid, char* name, uint8_t*
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
} else {
|
||||
ESP_LOGD(TAG, "%s: Bad response to set cid(%u) = %s",
|
||||
__FUNCTION__, reg_info.cid, (char*)esp_err_to_name(error));
|
||||
__FUNCTION__, (unsigned)reg_info.cid, (char*)esp_err_to_name(error));
|
||||
}
|
||||
free(pdata);
|
||||
// Set the type of parameter found in the table
|
||||
*type = reg_info.param_type;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "%s: The requested cid(%u) not found in the data dictionary.",
|
||||
__FUNCTION__, reg_info.cid);
|
||||
__FUNCTION__, (unsigned)reg_info.cid);
|
||||
error = ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return error;
|
||||
@ -787,8 +784,7 @@ esp_err_t mbc_tcp_master_create(void** handler)
|
||||
if (status != pdPASS) {
|
||||
vTaskDelete(mbm_opts->mbm_task_handle);
|
||||
MB_MASTER_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).",
|
||||
(uint32_t)status);
|
||||
"mb controller task creation error, xTaskCreate() returns (%u).", (unsigned)status);
|
||||
}
|
||||
MB_MASTER_ASSERT(mbm_opts->mbm_task_handle != NULL); // The task is created but handle is incorrect
|
||||
|
||||
|
@ -74,7 +74,7 @@
|
||||
#define MB_SLAVE_FMT(fmt) "Slave #%d, Socket(#%d)(%s)"fmt
|
||||
|
||||
/* ----------------------- Types & Prototypes --------------------------------*/
|
||||
void vMBPortEventClose( void );
|
||||
void vMBPortEventClose(void);
|
||||
|
||||
/* ----------------------- Static variables ---------------------------------*/
|
||||
static const char *TAG = "MB_TCP_MASTER_PORT";
|
||||
@ -94,10 +94,10 @@ BOOL xMBTCPPortMasterWaitEvent(EventGroupHandle_t xEventHandle, EventBits_t xEve
|
||||
xMasterEventHandle = xEventHandle;
|
||||
xMasterEvent = xEvent;
|
||||
BaseType_t status = xEventGroupWaitBits(xMasterEventHandle,
|
||||
(BaseType_t)(xEvent),
|
||||
pdFALSE, // do not clear start bit
|
||||
pdFALSE,
|
||||
usTimeout);
|
||||
(BaseType_t)(xEvent),
|
||||
pdFALSE, // do not clear start bit
|
||||
pdFALSE,
|
||||
usTimeout);
|
||||
return (BOOL)(status & xEvent);
|
||||
}
|
||||
|
||||
@ -129,12 +129,12 @@ xMBMasterTCPPortInit( USHORT usTCPPort )
|
||||
|
||||
// Create task for packet processing
|
||||
BaseType_t xErr = xTaskCreatePinnedToCore(vMBTCPPortMasterTask,
|
||||
"tcp_master_task",
|
||||
MB_TCP_STACK_SIZE,
|
||||
NULL,
|
||||
MB_TCP_TASK_PRIO,
|
||||
&xMbPortConfig.xMbTcpTaskHandle,
|
||||
MB_PORT_TASK_AFFINITY);
|
||||
"tcp_master_task",
|
||||
MB_TCP_STACK_SIZE,
|
||||
NULL,
|
||||
MB_TCP_TASK_PRIO,
|
||||
&xMbPortConfig.xMbTcpTaskHandle,
|
||||
MB_PORT_TASK_AFFINITY);
|
||||
if (xErr != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "TCP master task creation failure.");
|
||||
@ -148,7 +148,7 @@ xMBMasterTCPPortInit( USHORT usTCPPort )
|
||||
return bOkay;
|
||||
}
|
||||
|
||||
static MbSlaveInfo_t* vMBTCPPortMasterFindSlaveInfo(UCHAR ucSlaveAddr)
|
||||
static MbSlaveInfo_t *vMBTCPPortMasterFindSlaveInfo(UCHAR ucSlaveAddr)
|
||||
{
|
||||
int xIndex;
|
||||
BOOL xFound = false;
|
||||
@ -161,12 +161,12 @@ static MbSlaveInfo_t* vMBTCPPortMasterFindSlaveInfo(UCHAR ucSlaveAddr)
|
||||
}
|
||||
if (!xFound) {
|
||||
xMbPortConfig.pxMbSlaveCurrInfo = NULL;
|
||||
ESP_LOGE(TAG, "Slave info for short address %d not found.", ucSlaveAddr);
|
||||
ESP_LOGE(TAG, "Slave info for short address %u not found.", ucSlaveAddr);
|
||||
}
|
||||
return xMbPortConfig.pxMbSlaveCurrInfo;
|
||||
}
|
||||
|
||||
static MbSlaveInfo_t* vMBTCPPortMasterGetCurrInfo(void)
|
||||
static MbSlaveInfo_t *vMBTCPPortMasterGetCurrInfo(void)
|
||||
{
|
||||
if (!xMbPortConfig.pxMbSlaveCurrInfo) {
|
||||
ESP_LOGE(TAG, "Incorrect current slave info.");
|
||||
@ -217,7 +217,8 @@ static void vMBTCPPortMasterMStoTimeVal(USHORT usTimeoutMs, struct timeval *tv)
|
||||
tv->tv_usec = (usTimeoutMs - (tv->tv_sec * 1000)) * 1000;
|
||||
}
|
||||
|
||||
static void xMBTCPPortMasterCheckShutdown(void) {
|
||||
static void xMBTCPPortMasterCheckShutdown(void)
|
||||
{
|
||||
// First check if the task is not flagged for shutdown
|
||||
if (xShutdownSemaphore) {
|
||||
xSemaphoreGive(xShutdownSemaphore);
|
||||
@ -225,24 +226,24 @@ static void xMBTCPPortMasterCheckShutdown(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static BOOL xMBTCPPortMasterCloseConnection(MbSlaveInfo_t* pxInfo)
|
||||
static BOOL xMBTCPPortMasterCloseConnection(MbSlaveInfo_t *pxInfo)
|
||||
{
|
||||
if (!pxInfo) {
|
||||
return FALSE;
|
||||
}
|
||||
if (pxInfo->xSockId == -1) {
|
||||
ESP_LOGE(TAG, "Wrong socket info or disconnected socket: %d, skip.", pxInfo->xSockId);
|
||||
ESP_LOGE(TAG, "Wrong socket info or disconnected socket: %d, skip.", (int)pxInfo->xSockId);
|
||||
return FALSE;
|
||||
}
|
||||
if (shutdown(pxInfo->xSockId, SHUT_RDWR) == -1) {
|
||||
ESP_LOGV(TAG, "Shutdown failed sock %d, errno=%d", pxInfo->xSockId, errno);
|
||||
ESP_LOGV(TAG, "Shutdown failed sock %d, errno=%u", (int)pxInfo->xSockId, (unsigned)errno);
|
||||
}
|
||||
close(pxInfo->xSockId);
|
||||
pxInfo->xSockId = -1;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto)
|
||||
void vMBTCPPortMasterSetNetOpt(void *pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto)
|
||||
{
|
||||
xMbPortConfig.pvNetIface = pvNetIf;
|
||||
xMbPortConfig.eMbProto = xProto;
|
||||
@ -250,7 +251,7 @@ void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortPr
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
if (!pxInfo) {
|
||||
return 0;
|
||||
@ -261,7 +262,7 @@ static int64_t xMBTCPPortMasterGetRespTimeLeft(MbSlaveInfo_t* pxInfo)
|
||||
}
|
||||
|
||||
// Wait socket ready to read state
|
||||
static int vMBTCPPortMasterRxCheck(int xSd, fd_set* pxFdSet, int xTimeMs)
|
||||
static int vMBTCPPortMasterRxCheck(int xSd, fd_set *pxFdSet, int xTimeMs)
|
||||
{
|
||||
fd_set xReadSet = *pxFdSet;
|
||||
fd_set xErrorSet = *pxFdSet;
|
||||
@ -281,14 +282,14 @@ static int vMBTCPPortMasterRxCheck(int xSd, fd_set* pxFdSet, int xTimeMs)
|
||||
return xRes;
|
||||
}
|
||||
|
||||
static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHORT usLength, uint16_t xTimeMs)
|
||||
static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t *pxInfo, UCHAR *pucDstBuf, USHORT usLength, uint16_t xTimeMs)
|
||||
{
|
||||
int xLength = 0;
|
||||
UCHAR* pucBuf = pucDstBuf;
|
||||
UCHAR *pucBuf = pucDstBuf;
|
||||
USHORT usBytesLeft = usLength;
|
||||
struct timeval xTime;
|
||||
|
||||
MB_PORT_CHECK((pxInfo && pxInfo->xSockId > -1), -1, "Try to read incorrect socket = #%d.", pxInfo->xSockId);
|
||||
MB_PORT_CHECK((pxInfo && pxInfo->xSockId > -1), -1, "Try to read incorrect socket = #%d.", (int)pxInfo->xSockId);
|
||||
|
||||
// Set receive timeout for socket <= slave respond time
|
||||
xTime.tv_sec = xTimeMs / 1000;
|
||||
@ -305,12 +306,12 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR
|
||||
} else if (errno == ENOTCONN) {
|
||||
// Socket connection closed
|
||||
ESP_LOGE(TAG, "Socket(#%d)(%s) connection closed.",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr);
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr);
|
||||
return ERR_CONN;
|
||||
} else {
|
||||
// Other error occurred during receiving
|
||||
ESP_LOGE(TAG, "Socket(#%d)(%s) receive error, length=%d, errno=%d",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, xLength, errno);
|
||||
ESP_LOGE(TAG, "Socket(#%d)(%s) receive error, length=%u, errno=%u",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (unsigned)xLength, (unsigned)errno);
|
||||
return -1;
|
||||
}
|
||||
} else if (xLength) {
|
||||
@ -324,7 +325,7 @@ static int xMBTCPPortMasterGetBuf(MbSlaveInfo_t* pxInfo, UCHAR* pucDstBuf, USHOR
|
||||
return usLength;
|
||||
}
|
||||
|
||||
static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
|
||||
static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t *pxInfo)
|
||||
{
|
||||
int xLength = 0;
|
||||
int xRet = 0;
|
||||
@ -341,7 +342,7 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
|
||||
return xRet;
|
||||
} else if (xRet != MB_TCP_UID) {
|
||||
ESP_LOGD(TAG, "Socket (#%d)(%s), Fail to read modbus header. ret=%d",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, xRet);
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xRet);
|
||||
pxInfo->xRcvErr = ERR_VAL;
|
||||
return ERR_VAL;
|
||||
}
|
||||
@ -355,9 +356,9 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
|
||||
return xRet;
|
||||
} else if (xRet != xLength) {
|
||||
// Received incorrect or fragmented packet.
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s) incorrect packet, length=%d, TID=0x%02x, errno=%d(%s)",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, pxInfo->usRcvPos,
|
||||
usTidRcv, errno, strerror(errno));
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s) incorrect packet, length=%u, TID=0x%02x, errno=%u(%s)",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)pxInfo->usRcvPos,
|
||||
(int)usTidRcv, (unsigned)errno, strerror(errno));
|
||||
pxInfo->xRcvErr = ERR_VAL;
|
||||
return ERR_VAL;
|
||||
}
|
||||
@ -366,21 +367,21 @@ static int vMBTCPPortMasterReadPacket(MbSlaveInfo_t* pxInfo)
|
||||
// Check transaction identifier field in the incoming packet.
|
||||
if ((pxInfo->usTidCnt - 1) != usTidRcv) {
|
||||
ESP_LOGD(TAG, "Socket (#%d)(%s), incorrect TID(0x%02x)!=(0x%02x) received, discard data.",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, usTidRcv, (pxInfo->usTidCnt - 1));
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)usTidRcv, (int)(pxInfo->usTidCnt - 1));
|
||||
pxInfo->xRcvErr = ERR_BUF;
|
||||
return ERR_BUF;
|
||||
}
|
||||
pxInfo->usRcvPos += xRet + MB_TCP_UID;
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s) get data, length=%d, TID=0x%02x, errno=%d(%s)",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, pxInfo->usRcvPos,
|
||||
usTidRcv, errno, strerror(errno));
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s) get data, length=%u, TID=0x%02x, errno=%u(%s)",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (unsigned)pxInfo->usRcvPos,
|
||||
(unsigned)usTidRcv, (unsigned)errno, strerror(errno));
|
||||
pxInfo->xRcvErr = ERR_OK;
|
||||
return pxInfo->usRcvPos;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static err_t xMBTCPPortMasterSetNonBlocking(MbSlaveInfo_t* pxInfo)
|
||||
static err_t xMBTCPPortMasterSetNonBlocking(MbSlaveInfo_t *pxInfo)
|
||||
{
|
||||
if (!pxInfo) {
|
||||
return ERR_CONN;
|
||||
@ -388,21 +389,21 @@ static err_t xMBTCPPortMasterSetNonBlocking(MbSlaveInfo_t* pxInfo)
|
||||
// Set non blocking attribute for socket
|
||||
ULONG ulFlags = fcntl(pxInfo->xSockId, F_GETFL);
|
||||
if (fcntl(pxInfo->xSockId, F_SETFL, ulFlags | O_NONBLOCK) == -1) {
|
||||
ESP_LOGE(TAG, "Socket(#%d)(%s), fcntl() call error=%d",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, errno);
|
||||
ESP_LOGE(TAG, "Socket(#%d)(%s), fcntl() call error=%u",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (unsigned)errno);
|
||||
return ERR_WOULDBLOCK;
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static void vMBTCPPortSetKeepAlive(MbSlaveInfo_t* pxInfo)
|
||||
static void vMBTCPPortSetKeepAlive(MbSlaveInfo_t *pxInfo)
|
||||
{
|
||||
int optval = 1;
|
||||
setsockopt(pxInfo->xSockId, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
|
||||
}
|
||||
|
||||
// Check connection for timeout helper
|
||||
static err_t xMBTCPPortMasterCheckAlive(MbSlaveInfo_t* pxInfo, ULONG xTimeoutMs)
|
||||
static err_t xMBTCPPortMasterCheckAlive(MbSlaveInfo_t *pxInfo, ULONG xTimeoutMs)
|
||||
{
|
||||
fd_set xWriteSet;
|
||||
fd_set xErrorSet;
|
||||
@ -421,13 +422,13 @@ static err_t xMBTCPPortMasterCheckAlive(MbSlaveInfo_t* pxInfo, ULONG xTimeoutMs)
|
||||
if (errno == EINPROGRESS) {
|
||||
xErr = ERR_INPROGRESS;
|
||||
} else {
|
||||
ESP_LOGV(TAG, MB_SLAVE_FMT(" connection, select write err(errno) = %d(%d)."),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, xErr, errno);
|
||||
ESP_LOGV(TAG, MB_SLAVE_FMT(" connection, select write err(errno) = 0x%x(%u)."),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xErr, (unsigned)errno);
|
||||
xErr = ERR_CONN;
|
||||
}
|
||||
} else if (xErr == 0) {
|
||||
ESP_LOGV(TAG, "Socket(#%d)(%s), connection timeout occurred, err(errno) = %d(%d).",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, xErr, errno);
|
||||
ESP_LOGV(TAG, "Socket(#%d)(%s), connection timeout occurred, err(errno) = 0x%x(%u).",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xErr, (unsigned)errno);
|
||||
return ERR_INPROGRESS;
|
||||
} else {
|
||||
int xOptErr = 0;
|
||||
@ -435,12 +436,12 @@ static err_t xMBTCPPortMasterCheckAlive(MbSlaveInfo_t* pxInfo, ULONG xTimeoutMs)
|
||||
// Check socket error
|
||||
xErr = getsockopt(pxInfo->xSockId, SOL_SOCKET, SO_ERROR, (void*)&xOptErr, (socklen_t*)&ulOptLen);
|
||||
if (xOptErr != 0) {
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s), sock error occurred (%d).",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr, xOptErr);
|
||||
ESP_LOGD(TAG, "Socket(#%d)(%s), sock error occurred (%u).",
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr, (unsigned)xOptErr);
|
||||
return ERR_CONN;
|
||||
}
|
||||
ESP_LOGV(TAG, "Socket(#%d)(%s), is alive.",
|
||||
pxInfo->xSockId, pxInfo->pcIpAddr);
|
||||
(int)pxInfo->xSockId, pxInfo->pcIpAddr);
|
||||
return ERR_OK;
|
||||
}
|
||||
} else {
|
||||
@ -450,14 +451,14 @@ static err_t xMBTCPPortMasterCheckAlive(MbSlaveInfo_t* pxInfo, ULONG xTimeoutMs)
|
||||
}
|
||||
|
||||
// Resolve host name and/or fill the IP address structure
|
||||
static BOOL xMBTCPPortMasterCheckHost(const CHAR* pcHostStr, ip_addr_t* pxHostAddr)
|
||||
static BOOL xMBTCPPortMasterCheckHost(const CHAR *pcHostStr, ip_addr_t *pxHostAddr)
|
||||
{
|
||||
MB_PORT_CHECK((pcHostStr), FALSE, "Wrong host name or IP.");
|
||||
CHAR cStr[45];
|
||||
CHAR* pcStr = &cStr[0];
|
||||
CHAR *pcStr = &cStr[0];
|
||||
ip_addr_t xTargetAddr;
|
||||
struct addrinfo xHint;
|
||||
struct addrinfo* pxAddrList;
|
||||
struct addrinfo *pxAddrList;
|
||||
memset(&xHint, 0, sizeof(xHint));
|
||||
// Do name resolution for both protocols
|
||||
xHint.ai_family = AF_UNSPEC;
|
||||
@ -492,10 +493,10 @@ static BOOL xMBTCPPortMasterCheckHost(const CHAR* pcHostStr, ip_addr_t* pxHostAd
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL xMBTCPPortMasterAddSlaveIp(const USHORT usIndex, const CHAR* pcIpStr, UCHAR ucSlaveAddress)
|
||||
BOOL xMBTCPPortMasterAddSlaveIp(const USHORT usIndex, const CHAR *pcIpStr, UCHAR ucSlaveAddress)
|
||||
{
|
||||
BOOL xRes = FALSE;
|
||||
MbSlaveAddrInfo_t xSlaveAddrInfo = { 0 };
|
||||
MbSlaveAddrInfo_t xSlaveAddrInfo = {0};
|
||||
MB_PORT_CHECK(xMbPortConfig.xConnectQueue != NULL, FALSE, "Wrong slave IP address to add.");
|
||||
if (pcIpStr && (usIndex != 0xFF)) {
|
||||
xRes = xMBTCPPortMasterCheckHost(pcIpStr, NULL);
|
||||
@ -504,14 +505,14 @@ BOOL xMBTCPPortMasterAddSlaveIp(const USHORT usIndex, const CHAR* pcIpStr, UCHAR
|
||||
xSlaveAddrInfo.pcIPAddr = pcIpStr;
|
||||
xSlaveAddrInfo.usIndex = usIndex;
|
||||
xSlaveAddrInfo.ucSlaveAddr = ucSlaveAddress;
|
||||
BaseType_t xStatus = xQueueSend(xMbPortConfig.xConnectQueue, (void*)&xSlaveAddrInfo, 100);
|
||||
BaseType_t xStatus = xQueueSend(xMbPortConfig.xConnectQueue, (void *)&xSlaveAddrInfo, 100);
|
||||
MB_PORT_CHECK((xStatus == pdTRUE), FALSE, "FAIL to add slave IP address: [%s].", pcIpStr);
|
||||
}
|
||||
return xRes;
|
||||
}
|
||||
|
||||
// Unblocking connect function
|
||||
static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t *pxInfo)
|
||||
{
|
||||
if (!pxInfo) {
|
||||
return ERR_CONN;
|
||||
@ -519,15 +520,15 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
|
||||
err_t xErr = ERR_OK;
|
||||
CHAR cStr[128];
|
||||
CHAR* pcStr = NULL;
|
||||
CHAR *pcStr = NULL;
|
||||
ip_addr_t xTargetAddr;
|
||||
struct addrinfo xHint;
|
||||
struct addrinfo* pxAddrList;
|
||||
struct addrinfo* pxCurAddr;
|
||||
struct addrinfo *pxAddrList;
|
||||
struct addrinfo *pxCurAddr;
|
||||
|
||||
memset(&xHint, 0, sizeof(xHint));
|
||||
// Do name resolution for both protocols
|
||||
//xHint.ai_family = AF_UNSPEC; Todo: Find a reason why AF_UNSPEC does not work
|
||||
// xHint.ai_family = AF_UNSPEC; Todo: Find a reason why AF_UNSPEC does not work
|
||||
xHint.ai_flags = AI_ADDRCONFIG; // get IPV6 address if supported, otherwise IPV4
|
||||
xHint.ai_family = (xMbPortConfig.eMbIpVer == MB_PORT_IPV4) ? AF_INET : AF_INET6;
|
||||
xHint.ai_socktype = (pxInfo->xMbProto == MB_PROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
|
||||
@ -558,19 +559,19 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
inet6_addr_to_ip6addr(ip_2_ip6(&xTargetAddr), &addr6);
|
||||
pcStr = ip6addr_ntoa_r(ip_2_ip6(&xTargetAddr), cStr, sizeof(cStr));
|
||||
// Set scope id to fix routing issues with local address
|
||||
((struct sockaddr_in6 *) (pxCurAddr->ai_addr))->sin6_scope_id =
|
||||
esp_netif_get_netif_impl_index(xMbPortConfig.pvNetIface);
|
||||
((struct sockaddr_in6 *)(pxCurAddr->ai_addr))->sin6_scope_id =
|
||||
esp_netif_get_netif_impl_index(xMbPortConfig.pvNetIface);
|
||||
}
|
||||
#endif
|
||||
if (pxInfo->xSockId <= 0) {
|
||||
pxInfo->xSockId = socket(pxCurAddr->ai_family, pxCurAddr->ai_socktype, pxCurAddr->ai_protocol);
|
||||
if (pxInfo->xSockId < 0) {
|
||||
ESP_LOGE(TAG, "Unable to create socket: #%d, errno %d", pxInfo->xSockId, errno);
|
||||
ESP_LOGE(TAG, "Unable to create socket: #%d, errno %u", (int)pxInfo->xSockId, (unsigned)errno);
|
||||
xErr = ERR_IF;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Socket (#%d)(%s) created.", pxInfo->xSockId, cStr);
|
||||
ESP_LOGV(TAG, "Socket (#%d)(%s) created.", (int)pxInfo->xSockId, cStr);
|
||||
}
|
||||
|
||||
// Set non blocking attribute for socket
|
||||
@ -581,8 +582,8 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
xErr = connect(pxInfo->xSockId, (struct sockaddr*)pxCurAddr->ai_addr, pxCurAddr->ai_addrlen);
|
||||
if ((xErr < 0) && (errno == EINPROGRESS || errno == EALREADY)) {
|
||||
// The unblocking connect is pending (check status later) or already connected
|
||||
ESP_LOGV(TAG, "Socket(#%d)(%s) connection is pending, errno %d (%s).",
|
||||
pxInfo->xSockId, cStr, errno, strerror(errno));
|
||||
ESP_LOGV(TAG, "Socket(#%d)(%s) connection is pending, errno %u (%s).",
|
||||
(int)pxInfo->xSockId, cStr, (unsigned)errno, strerror(errno));
|
||||
|
||||
// Set keep alive flag in socket options
|
||||
vMBTCPPortSetKeepAlive(pxInfo);
|
||||
@ -594,13 +595,13 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
continue;
|
||||
} else if (xErr != ERR_OK) {
|
||||
// Other error occurred during connection
|
||||
ESP_LOGV(TAG, MB_SLAVE_FMT(" unable to connect, error=%d, errno %d (%s)"),
|
||||
pxInfo->xIndex, pxInfo->xSockId, cStr, xErr, errno, strerror(errno));
|
||||
ESP_LOGV(TAG, MB_SLAVE_FMT(" unable to connect, error=0x%x, errno %u (%s)"),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, cStr, (int)xErr, (unsigned)errno, strerror(errno));
|
||||
xMBTCPPortMasterCloseConnection(pxInfo);
|
||||
xErr = ERR_CONN;
|
||||
} else {
|
||||
ESP_LOGI(TAG, MB_SLAVE_FMT(", successfully connected."),
|
||||
pxInfo->xIndex, pxInfo->xSockId, cStr);
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, cStr);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -609,9 +610,9 @@ static err_t xMBTCPPortMasterConnect(MbSlaveInfo_t* pxInfo)
|
||||
}
|
||||
|
||||
// Find the first slave info whose descriptor is set in xFdSet
|
||||
static MbSlaveInfo_t* xMBTCPPortMasterGetSlaveReady(fd_set* pxFdSet)
|
||||
static MbSlaveInfo_t *xMBTCPPortMasterGetSlaveReady(fd_set *pxFdSet)
|
||||
{
|
||||
MbSlaveInfo_t* pxInfo = NULL;
|
||||
MbSlaveInfo_t *pxInfo = NULL;
|
||||
|
||||
// Slave connection loop
|
||||
for (int xIndex = 0; (xIndex < MB_TCP_PORT_MAX_CONN); xIndex++) {
|
||||
@ -624,13 +625,13 @@ static MbSlaveInfo_t* xMBTCPPortMasterGetSlaveReady(fd_set* pxFdSet)
|
||||
}
|
||||
}
|
||||
}
|
||||
return (MbSlaveInfo_t*)NULL;
|
||||
return (MbSlaveInfo_t *)NULL;
|
||||
}
|
||||
|
||||
static int xMBTCPPortMasterCheckConnState(fd_set* pxFdSet)
|
||||
static int xMBTCPPortMasterCheckConnState(fd_set *pxFdSet)
|
||||
{
|
||||
fd_set xConnSetCheck = *pxFdSet;
|
||||
MbSlaveInfo_t* pxInfo = NULL;
|
||||
MbSlaveInfo_t *pxInfo = NULL;
|
||||
int64_t xTime = 0;
|
||||
int xErr = 0;
|
||||
int xCount = 0;
|
||||
@ -641,12 +642,12 @@ static int xMBTCPPortMasterCheckConnState(fd_set* pxFdSet)
|
||||
xErr = xMBTCPPortMasterCheckAlive(pxInfo, 0);
|
||||
if ((xErr < 0) && (((xTime - pxInfo->xRecvTimeStamp) > MB_TCP_RECONNECT_TIMEOUT) ||
|
||||
((xTime - pxInfo->xSendTimeStamp) > MB_TCP_RECONNECT_TIMEOUT))) {
|
||||
ESP_LOGI(TAG, MB_SLAVE_FMT(", slave is down, off_time[r][w](us) = [%ju][%ju]."),
|
||||
pxInfo->xIndex,
|
||||
pxInfo->xSockId,
|
||||
pxInfo->pcIpAddr,
|
||||
(int64_t)(xTime - pxInfo->xRecvTimeStamp),
|
||||
(int64_t)(xTime - pxInfo->xSendTimeStamp));
|
||||
ESP_LOGI(TAG, MB_SLAVE_FMT(", slave is down, off_time[r][w](us) = [%" PRIu64 "][%" PRIu64 "]."),
|
||||
(int)pxInfo->xIndex,
|
||||
(int)pxInfo->xSockId,
|
||||
pxInfo->pcIpAddr,
|
||||
(int64_t)(xTime - pxInfo->xRecvTimeStamp),
|
||||
(int64_t)(xTime - pxInfo->xSendTimeStamp));
|
||||
xCount++;
|
||||
}
|
||||
}
|
||||
@ -663,8 +664,8 @@ static void xMBTCPPortMasterFsmSetError(eMBMasterErrorEventType xErrType, eMBMas
|
||||
|
||||
static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
{
|
||||
MbSlaveInfo_t* pxInfo;
|
||||
MbSlaveInfo_t* pxCurrInfo;
|
||||
MbSlaveInfo_t *pxInfo;
|
||||
MbSlaveInfo_t *pxCurrInfo;
|
||||
|
||||
fd_set xConnSet;
|
||||
fd_set xReadSet;
|
||||
@ -687,20 +688,20 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
if (xMbPortConfig.usMbSlaveInfoCount > MB_TCP_PORT_MAX_CONN) {
|
||||
ESP_LOGE(TAG, "Exceeds maximum connections limit=%d.", MB_TCP_PORT_MAX_CONN);
|
||||
ESP_LOGE(TAG, "Exceeds maximum connections limit=%u.", (unsigned)MB_TCP_PORT_MAX_CONN);
|
||||
break;
|
||||
}
|
||||
pxInfo = calloc(1, sizeof(MbSlaveInfo_t));
|
||||
if (!pxInfo) {
|
||||
ESP_LOGE(TAG, "Slave(#%d), info structure allocation fail.",
|
||||
xMbPortConfig.usMbSlaveInfoCount);
|
||||
ESP_LOGE(TAG, "Slave(#%u), info structure allocation fail.",
|
||||
(unsigned)xMbPortConfig.usMbSlaveInfoCount);
|
||||
free(pxInfo);
|
||||
break;
|
||||
}
|
||||
pxInfo->pucRcvBuf = calloc(MB_TCP_BUF_SIZE, sizeof(UCHAR));
|
||||
if (!pxInfo->pucRcvBuf) {
|
||||
ESP_LOGE(TAG, "Slave(#%d), receive buffer allocation fail.",
|
||||
xMbPortConfig.usMbSlaveInfoCount);
|
||||
ESP_LOGE(TAG, "Slave(#%u), receive buffer allocation fail.",
|
||||
(unsigned)xMbPortConfig.usMbSlaveInfoCount);
|
||||
free(pxInfo->pucRcvBuf);
|
||||
break;
|
||||
}
|
||||
@ -736,7 +737,7 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
|
||||
// if slave descriptor is NULL then it is end of list or connection closed.
|
||||
if (!pxInfo) {
|
||||
ESP_LOGV(TAG, "Index: %d is not initialized, skip.", ucCnt);
|
||||
ESP_LOGV(TAG, "Index: % is not initialized, skip.", ucCnt);
|
||||
if (xMbPortConfig.usMbSlaveInfoCount) {
|
||||
continue;
|
||||
}
|
||||
@ -751,9 +752,9 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
// In case of connection errors remove the socket from set
|
||||
if (FD_ISSET(pxInfo->xSockId, &xConnSet)) {
|
||||
FD_CLR(pxInfo->xSockId, &xConnSet);
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(" connect failed, error = %d."),
|
||||
pxInfo->xIndex, pxInfo->xSockId,
|
||||
(char*)pxInfo->pcIpAddr, xErr);
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(" connect failed, error = 0x%x."),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId,
|
||||
(char*)pxInfo->pcIpAddr, (int)xErr);
|
||||
if (usSlaveConnCnt) {
|
||||
usSlaveConnCnt--;
|
||||
}
|
||||
@ -765,20 +766,20 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
FD_SET(pxInfo->xSockId, &xConnSet);
|
||||
usSlaveConnCnt++;
|
||||
xMaxSd = (pxInfo->xSockId > xMaxSd) ? pxInfo->xSockId : xMaxSd;
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", connected %d slave(s), error = %d."),
|
||||
pxInfo->xIndex, pxInfo->xSockId,
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", connected %u slave(s), error = 0x%x."),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId,
|
||||
pxInfo->pcIpAddr,
|
||||
usSlaveConnCnt, xErr);
|
||||
(unsigned)usSlaveConnCnt, (int)xErr);
|
||||
// Update time stamp for connected slaves
|
||||
pxInfo->xRecvTimeStamp = xMBTCPGetTimeStamp();
|
||||
pxInfo->xSendTimeStamp = xMBTCPGetTimeStamp();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", unexpected error = %d."),
|
||||
pxInfo->xIndex,
|
||||
pxInfo->xSockId,
|
||||
pxInfo->pcIpAddr, xErr);
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", unexpected error = 0x%x."),
|
||||
(int)pxInfo->xIndex,
|
||||
(int)pxInfo->xSockId,
|
||||
pxInfo->pcIpAddr, (int)xErr);
|
||||
break;
|
||||
}
|
||||
if (pxInfo) {
|
||||
@ -787,7 +788,7 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
xMBTCPPortMasterCheckShutdown();
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Connected %d slaves, start polling...", usSlaveConnCnt);
|
||||
ESP_LOGI(TAG, "Connected %u slaves, start polling...", (unsigned)usSlaveConnCnt);
|
||||
|
||||
vMBTCPPortMasterStartPoll(); // Send event to start stack
|
||||
|
||||
@ -804,21 +805,21 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
pxCurrInfo = vMBTCPPortMasterGetCurrInfo();
|
||||
if (!pxCurrInfo) {
|
||||
ESP_LOGE(TAG, "Incorrect connection options for slave index: %d.",
|
||||
xMbPortConfig.ucCurSlaveIndex);
|
||||
(int)xMbPortConfig.ucCurSlaveIndex);
|
||||
vMBTCPPortMasterStopPoll();
|
||||
xMBTCPPortMasterCheckShutdown();
|
||||
break; // incorrect slave descriptor, reconnect.
|
||||
}
|
||||
xTime = xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo);
|
||||
ESP_LOGD(TAG, "Set select timeout, left time: %ju ms.",
|
||||
xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo));
|
||||
ESP_LOGD(TAG, "Set select timeout, left time: %" PRIu64 " ms.",
|
||||
xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo));
|
||||
// Wait respond from current slave during respond timeout
|
||||
int xRes = vMBTCPPortMasterRxCheck(pxCurrInfo->xSockId, &xReadSet, xTime);
|
||||
if (xRes == ERR_TIMEOUT) {
|
||||
// No respond from current slave, process timeout.
|
||||
// Need to drop response later if it is received after timeout.
|
||||
ESP_LOGD(TAG, "Select timeout, left time: %ju ms.",
|
||||
xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo));
|
||||
ESP_LOGD(TAG, "Select timeout, left time: %" PRIu64 " ms.",
|
||||
xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo));
|
||||
xTime = xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo);
|
||||
// Wait completion of last transaction
|
||||
xMBMasterPortFsmWaitConfirmation(MB_EVENT_REQ_DONE_MASK, pdMS_TO_TICKS(xTime + 1));
|
||||
@ -827,7 +828,7 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
} else if (xRes < 0) {
|
||||
// Select error (slave connection or r/w failure).
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", socket select error. Slave disconnected?"),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
xTime = xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo);
|
||||
// Wait completion of last transaction
|
||||
xMBMasterPortFsmWaitConfirmation(MB_EVENT_REQ_DONE_MASK, pdMS_TO_TICKS(xTime));
|
||||
@ -851,20 +852,20 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
// Response received correctly, send an event to stack
|
||||
xMBTCPPortMasterFsmSetError(EV_ERROR_INIT, EV_MASTER_FRAME_RECEIVED);
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", frame received."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
} else if ((xRet == ERR_TIMEOUT) || (xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo) == 0)) {
|
||||
// Timeout occurred when receiving frame, process respond timeout
|
||||
xMBTCPPortMasterFsmSetError(EV_ERROR_RESPOND_TIMEOUT, EV_MASTER_ERROR_PROCESS);
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", frame read timeout."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
} else if (xRet == ERR_BUF) {
|
||||
// After retries a response with incorrect TID received, process failure.
|
||||
xMBTCPPortMasterFsmSetError(EV_ERROR_RECEIVE_DATA, EV_MASTER_ERROR_PROCESS);
|
||||
ESP_LOGW(TAG, MB_SLAVE_FMT(", frame error."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
} else {
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", critical error=%d."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr, xRet);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr, (int)xRet);
|
||||
// Stop polling process
|
||||
vMBTCPPortMasterStopPoll();
|
||||
xMBTCPPortMasterCheckShutdown();
|
||||
@ -873,15 +874,15 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
xTime = xMBTCPPortMasterGetRespTimeLeft(pxCurrInfo);
|
||||
ESP_LOGD(TAG, "Slave #%d, data processing left time %ju [ms].", pxCurrInfo->xIndex, xTime);
|
||||
ESP_LOGD(TAG, "Slave #%d, data processing left time %" PRIu64 " [ms].", (int)pxCurrInfo->xIndex, xTime);
|
||||
// Wait completion of Modbus frame processing before start of new transaction.
|
||||
if (xMBMasterPortFsmWaitConfirmation(MB_EVENT_REQ_DONE_MASK, pdMS_TO_TICKS(xTime))) {
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", data processing completed."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr);
|
||||
}
|
||||
xTime = xMBTCPGetTimeStamp() - pxCurrInfo->xSendTimeStamp;
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", processing time[us] = %ju."),
|
||||
pxCurrInfo->xIndex, pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr, xTime);
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", processing time[us] = %" PRIu64 "."),
|
||||
(int)pxCurrInfo->xIndex, (int)pxCurrInfo->xSockId, pxCurrInfo->pcIpAddr, xTime);
|
||||
}
|
||||
}
|
||||
xMBTCPPortMasterCheckShutdown();
|
||||
@ -893,14 +894,12 @@ static void vMBTCPPortMasterTask(void *pvParameters)
|
||||
extern void vMBMasterPortEventClose(void);
|
||||
extern void vMBMasterPortTimerClose(void);
|
||||
|
||||
void
|
||||
vMBMasterTCPPortEnable(void)
|
||||
void vMBMasterTCPPortEnable(void)
|
||||
{
|
||||
vTaskResume(xMbPortConfig.xMbTcpTaskHandle);
|
||||
}
|
||||
|
||||
void
|
||||
vMBMasterTCPPortDisable(void)
|
||||
void vMBMasterTCPPortDisable(void)
|
||||
{
|
||||
// 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
|
||||
@ -928,8 +927,7 @@ vMBMasterTCPPortDisable(void)
|
||||
free(xMbPortConfig.pxMbSlaveInfo);
|
||||
}
|
||||
|
||||
void
|
||||
vMBMasterTCPPortClose(void)
|
||||
void vMBMasterTCPPortClose(void)
|
||||
{
|
||||
vQueueDelete(xMbPortConfig.xConnectQueue);
|
||||
vMBMasterPortTimerClose();
|
||||
@ -937,10 +935,9 @@ vMBMasterTCPPortClose(void)
|
||||
vMBMasterPortEventClose();
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterTCPPortGetRequest( UCHAR ** ppucMBTCPFrame, USHORT * usTCPLength )
|
||||
BOOL xMBMasterTCPPortGetRequest(UCHAR **ppucMBTCPFrame, USHORT *usTCPLength)
|
||||
{
|
||||
MbSlaveInfo_t* pxInfo = vMBTCPPortMasterGetCurrInfo();
|
||||
MbSlaveInfo_t *pxInfo = vMBTCPPortMasterGetCurrInfo();
|
||||
*ppucMBTCPFrame = pxInfo->pucRcvBuf;
|
||||
*usTCPLength = pxInfo->usRcvPos;
|
||||
|
||||
@ -954,37 +951,35 @@ xMBMasterTCPPortGetRequest( UCHAR ** ppucMBTCPFrame, USHORT * usTCPLength )
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int xMBMasterTCPPortWritePoll(MbSlaveInfo_t* pxInfo, const UCHAR * pucMBTCPFrame, USHORT usTCPLength, ULONG xTimeout)
|
||||
int xMBMasterTCPPortWritePoll(MbSlaveInfo_t *pxInfo, const UCHAR *pucMBTCPFrame, USHORT usTCPLength, ULONG xTimeout)
|
||||
{
|
||||
// Check if the socket is alive (writable and SO_ERROR == 0)
|
||||
int xRes = (int)xMBTCPPortMasterCheckAlive(pxInfo, xTimeout);
|
||||
if ((xRes < 0) && (xRes != ERR_INPROGRESS))
|
||||
{
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", is not writable, error: %d, errno %d"),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, xRes, errno);
|
||||
if ((xRes < 0) && (xRes != ERR_INPROGRESS)) {
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", is not writable, error: %d, errno %u"),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xRes, (unsigned)errno);
|
||||
return xRes;
|
||||
}
|
||||
xRes = send(pxInfo->xSockId, pucMBTCPFrame, usTCPLength, TCP_NODELAY);
|
||||
if (xRes < 0) {
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", send data error: %d, errno %d"),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, xRes, errno);
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", send data error: %d, errno %u"),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xRes, (unsigned)errno);
|
||||
}
|
||||
return xRes;
|
||||
}
|
||||
|
||||
BOOL
|
||||
xMBMasterTCPPortSendResponse( UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
BOOL xMBMasterTCPPortSendResponse(UCHAR *pucMBTCPFrame, USHORT usTCPLength)
|
||||
{
|
||||
BOOL bFrameSent = FALSE;
|
||||
USHORT ucCurSlaveIndex = ucMBMasterGetDestAddress();
|
||||
MbSlaveInfo_t* pxInfo = vMBTCPPortMasterFindSlaveInfo(ucCurSlaveIndex);
|
||||
MbSlaveInfo_t *pxInfo = vMBTCPPortMasterFindSlaveInfo(ucCurSlaveIndex);
|
||||
|
||||
// If the slave is correct and active then send data
|
||||
// otherwise treat slave as died and skip
|
||||
if (pxInfo != NULL) {
|
||||
if (pxInfo->xSockId < 0) {
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", send to died slave, error = %d"),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, pxInfo->xError);
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", send to died slave, error = %u"),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, (unsigned)pxInfo->xError);
|
||||
} else {
|
||||
// Apply TID field to the frame before send
|
||||
pucMBTCPFrame[MB_TCP_TID] = (UCHAR)(pxInfo->usTidCnt >> 8U);
|
||||
@ -992,14 +987,14 @@ xMBMasterTCPPortSendResponse( UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
|
||||
int xRes = xMBMasterTCPPortWritePoll(pxInfo, pucMBTCPFrame, usTCPLength, MB_TCP_SEND_TIMEOUT_MS);
|
||||
if (xRes < 0) {
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", send data failure, err(errno) = %d(%d)."),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, xRes, errno);
|
||||
ESP_LOGE(TAG, MB_SLAVE_FMT(", send data failure, err(errno) = %d(%u)."),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, (int)xRes, (unsigned)errno);
|
||||
bFrameSent = FALSE;
|
||||
pxInfo->xError = xRes;
|
||||
} else {
|
||||
bFrameSent = TRUE;
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", send data successful: TID=0x%02x, %d (bytes), errno %d"),
|
||||
pxInfo->xIndex, pxInfo->xSockId, pxInfo->pcIpAddr, pxInfo->usTidCnt, xRes, errno);
|
||||
ESP_LOGD(TAG, MB_SLAVE_FMT(", send data successful: TID=0x%02x, %d (bytes), errno %u"),
|
||||
(int)pxInfo->xIndex, (int)pxInfo->xSockId, pxInfo->pcIpAddr, pxInfo->usTidCnt, (int)xRes, (unsigned)errno);
|
||||
pxInfo->xError = 0;
|
||||
pxInfo->usRcvPos = 0;
|
||||
if (pxInfo->usTidCnt < (USHRT_MAX - 1)) {
|
||||
@ -1011,7 +1006,7 @@ xMBMasterTCPPortSendResponse( UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
pxInfo->xSendTimeStamp = xMBTCPGetTimeStamp();
|
||||
}
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Send data to died slave, address = %d", ucCurSlaveIndex);
|
||||
ESP_LOGD(TAG, "Send data to died slave, address = %u", (unsigned)ucCurSlaveIndex);
|
||||
}
|
||||
vMBMasterPortTimersRespondTimeoutEnable();
|
||||
xMBMasterPortEventPost(EV_MASTER_FRAME_SENT);
|
||||
@ -1036,4 +1031,4 @@ xMBMasterTCPTimerExpired(void)
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
#endif //#if MB_MASTER_TCP_ENABLED
|
||||
#endif // #if MB_MASTER_TCP_ENABLED
|
||||
|
@ -54,9 +54,9 @@ static esp_err_t mbc_tcp_slave_setup(void* comm_info)
|
||||
"mb wrong communication settings.");
|
||||
mb_communication_info_t* comm_settings = (mb_communication_info_t*)comm_info;
|
||||
MB_SLAVE_CHECK((comm_settings->ip_mode == MB_MODE_TCP),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (0x%x).", (uint8_t)comm_settings->ip_mode);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect mode = (%u).", (unsigned)comm_settings->ip_mode);
|
||||
MB_SLAVE_CHECK(((comm_settings->ip_addr_type == MB_IPV4) || (comm_settings->ip_addr_type == MB_IPV6)),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect addr type = (0x%x).", (uint8_t)comm_settings->ip_addr_type);
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect addr type = (%u).", (unsigned)comm_settings->ip_addr_type);
|
||||
MB_SLAVE_CHECK((comm_settings->ip_netif_ptr != NULL),
|
||||
ESP_ERR_INVALID_ARG, "mb incorrect iface address.");
|
||||
// Set communication options of the controller
|
||||
@ -74,7 +74,7 @@ static esp_err_t mbc_tcp_slave_start(void)
|
||||
// Initialize Modbus stack using mbcontroller parameters
|
||||
status = eMBTCPInit((UCHAR)mbs_opts->mbs_comm.slave_uid, (USHORT)mbs_opts->mbs_comm.ip_port);
|
||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
||||
"mb stack initialization failure, eMBInit() returns (0x%x).", (int)status);
|
||||
|
||||
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;
|
||||
@ -82,12 +82,12 @@ static esp_err_t mbc_tcp_slave_start(void)
|
||||
|
||||
status = eMBEnable();
|
||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||
"mb TCP stack start failure, eMBEnable() returned (0x%x).", (uint32_t)status);
|
||||
"mb TCP stack start failure, eMBEnable() returned (0x%x).", (int)status);
|
||||
// Set the mbcontroller start flag
|
||||
EventBits_t flag = xEventGroupSetBits(mbs_opts->mbs_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
MB_SLAVE_CHECK((flag & MB_EVENT_STACK_STARTED),
|
||||
ESP_ERR_INVALID_STATE, "mb stack start event set error.");
|
||||
ESP_ERR_INVALID_STATE, "mb stack start event set error.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@ static esp_err_t mbc_tcp_slave_destroy(void)
|
||||
EventBits_t flag = xEventGroupClearBits(mbs_opts->mbs_event_group,
|
||||
(EventBits_t)MB_EVENT_STACK_STARTED);
|
||||
MB_SLAVE_CHECK((flag & MB_EVENT_STACK_STARTED),
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
ESP_ERR_INVALID_STATE, "mb stack stop event failure.");
|
||||
// Disable and then destroy the Modbus stack
|
||||
mb_error = eMBDisable();
|
||||
MB_SLAVE_CHECK((mb_error == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack disable failure.");
|
||||
@ -132,7 +132,7 @@ static esp_err_t mbc_tcp_slave_get_param_info(mb_param_info_t* reg_info, uint32_
|
||||
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
||||
esp_err_t err = ESP_ERR_TIMEOUT;
|
||||
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
||||
ESP_ERR_INVALID_ARG, "mb queue handle is invalid.");
|
||||
ESP_ERR_INVALID_ARG, "mb queue handle is invalid.");
|
||||
MB_SLAVE_CHECK((reg_info != NULL), ESP_ERR_INVALID_ARG, "mb register information is invalid.");
|
||||
BaseType_t status = xQueueReceive(mbs_opts->mbs_notification_queue_handle,
|
||||
reg_info, pdMS_TO_TICKS(timeout));
|
||||
@ -170,13 +170,13 @@ esp_err_t mbc_tcp_slave_create(void** handler)
|
||||
// Parameter change notification queue
|
||||
mbs_opts->mbs_event_group = xEventGroupCreate();
|
||||
MB_SLAVE_CHECK((mbs_opts->mbs_event_group != NULL),
|
||||
ESP_ERR_NO_MEM, "mb event group error.");
|
||||
ESP_ERR_NO_MEM, "mb event group error.");
|
||||
// Parameter change notification queue
|
||||
mbs_opts->mbs_notification_queue_handle = xQueueCreate(
|
||||
MB_CONTROLLER_NOTIFY_QUEUE_SIZE,
|
||||
sizeof(mb_param_info_t));
|
||||
MB_SLAVE_CHECK((mbs_opts->mbs_notification_queue_handle != NULL),
|
||||
ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
||||
ESP_ERR_NO_MEM, "mb notify queue creation error.");
|
||||
// Create Modbus controller task
|
||||
status = xTaskCreatePinnedToCore((void*)&modbus_tcp_slave_task,
|
||||
"modbus_tcp_slave_task",
|
||||
@ -188,8 +188,7 @@ esp_err_t mbc_tcp_slave_create(void** handler)
|
||||
if (status != pdPASS) {
|
||||
vTaskDelete(mbs_opts->mbs_task_handle);
|
||||
MB_SLAVE_CHECK((status == pdPASS), ESP_ERR_NO_MEM,
|
||||
"mb controller task creation error, xTaskCreate() returns (0x%x).",
|
||||
(uint32_t)status);
|
||||
"mb controller task creation error, xTaskCreate() returns (%u).", (unsigned)status);
|
||||
}
|
||||
|
||||
// The task is created but handle is incorrect
|
||||
|
@ -194,7 +194,7 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
|
||||
// Accept new socket connection if not active
|
||||
xSockId = accept(xListenSockId, (struct sockaddr *)&xSrcAddr, &xSize);
|
||||
if (xSockId < 0) {
|
||||
ESP_LOGE(TAG, "Unable to accept connection: errno=%d", errno);
|
||||
ESP_LOGE(TAG, "Unable to accept connection: errno=%u", (unsigned)errno);
|
||||
close(xSockId);
|
||||
} else {
|
||||
// Get the sender's ip address as string
|
||||
@ -210,7 +210,7 @@ static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
|
||||
// Make sure ss_family is valid
|
||||
abort();
|
||||
}
|
||||
ESP_LOGI(TAG, "Socket (#%d), accept client connection from address: %s", xSockId, cAddrStr);
|
||||
ESP_LOGI(TAG, "Socket (#%d), accept client connection from address: %s", (int)xSockId, cAddrStr);
|
||||
pcStr = calloc(1, strlen(cAddrStr) + 1);
|
||||
if (pcStr && pcIPAddr) {
|
||||
memcpy(pcStr, cAddrStr, strlen(cAddrStr));
|
||||
@ -226,11 +226,11 @@ static BOOL xMBTCPPortCloseConnection(MbClientInfo_t* pxInfo)
|
||||
MB_PORT_CHECK(pxInfo, FALSE, "Client info is NULL.");
|
||||
|
||||
if (pxInfo->xSockId == -1) {
|
||||
ESP_LOGE(TAG, "Wrong socket info or disconnected socket: %d.", pxInfo->xSockId);
|
||||
ESP_LOGE(TAG, "Wrong socket info or disconnected socket: %d.", (int)pxInfo->xSockId);
|
||||
return FALSE;
|
||||
}
|
||||
if (shutdown(pxInfo->xSockId, SHUT_RDWR) == -1) {
|
||||
ESP_LOGE(TAG, "Socket (#%d), shutdown failed: errno %d", pxInfo->xSockId, errno);
|
||||
ESP_LOGE(TAG, "Socket (#%d), shutdown failed: errno %u", (int)pxInfo->xSockId, (unsigned)errno);
|
||||
}
|
||||
close(pxInfo->xSockId);
|
||||
pxInfo->xSockId = -1;
|
||||
@ -267,7 +267,7 @@ static int xMBTCPPortRxPoll(MbClientInfo_t* pxClientInfo, ULONG xTimeoutMs)
|
||||
} else if (xRet == 0) {
|
||||
// timeout occurred
|
||||
if ((xStartTimeStamp + xTimeoutMs * 1000) > xMBTCPGetTimeStamp()) {
|
||||
ESP_LOGD(TAG, "Socket (#%d) Read timeout.", pxClientInfo->xSockId);
|
||||
ESP_LOGD(TAG, "Socket (#%d) Read timeout.", (int)pxClientInfo->xSockId);
|
||||
xRet = ERR_TIMEOUT;
|
||||
break;
|
||||
}
|
||||
@ -275,20 +275,20 @@ static int xMBTCPPortRxPoll(MbClientInfo_t* pxClientInfo, ULONG xTimeoutMs)
|
||||
if (FD_ISSET(pxClientInfo->xSockId, &xReadSet)) {
|
||||
// If new buffer received then read Modbus packet into buffer
|
||||
MB_PORT_CHECK((pxClientInfo->usTCPBufPos + pxClientInfo->usTCPFrameBytesLeft < MB_TCP_BUF_SIZE),
|
||||
ERR_BUF, "Socket (#%d), incorrect request buffer size = %d, ignore.",
|
||||
pxClientInfo->xSockId,
|
||||
(pxClientInfo->usTCPBufPos + pxClientInfo->usTCPFrameBytesLeft));
|
||||
ERR_BUF, "Socket (#%d), incorrect request buffer size = %u, ignore.",
|
||||
(int)pxClientInfo->xSockId,
|
||||
(unsigned)(pxClientInfo->usTCPBufPos + pxClientInfo->usTCPFrameBytesLeft));
|
||||
int xLength = recv(pxClientInfo->xSockId, &pxClientInfo->pucTCPBuf[pxClientInfo->usTCPBufPos],
|
||||
pxClientInfo->usTCPFrameBytesLeft, MSG_DONTWAIT);
|
||||
if (xLength < 0) {
|
||||
// If an error occurred during receiving
|
||||
ESP_LOGE(TAG, "Receive failed: length=%d, errno=%d", xLength, errno);
|
||||
ESP_LOGE(TAG, "Receive failed: length=%u, errno=%u", (unsigned)xLength, (unsigned)errno);
|
||||
xRet = (err_t)xLength;
|
||||
break;
|
||||
} else if (xLength == 0) {
|
||||
// Socket connection closed
|
||||
ESP_LOGD(TAG, "Socket (#%d)(%s), connection closed.",
|
||||
pxClientInfo->xSockId, pxClientInfo->pcIpAddr);
|
||||
(int)pxClientInfo->xSockId, pxClientInfo->pcIpAddr);
|
||||
xRet = ERR_CLSD;
|
||||
break;
|
||||
} else {
|
||||
@ -312,7 +312,7 @@ static int xMBTCPPortRxPoll(MbClientInfo_t* pxClientInfo, ULONG xTimeoutMs)
|
||||
xRet = pxClientInfo->usTCPBufPos;
|
||||
break;
|
||||
} else if ((pxClientInfo->usTCPBufPos + xLength) >= MB_TCP_BUF_SIZE) {
|
||||
ESP_LOGE(TAG, "Incorrect buffer received (%u) bytes.", xLength);
|
||||
ESP_LOGE(TAG, "Incorrect buffer received (%u) bytes.", (unsigned)xLength);
|
||||
// This should not happen. We can't deal with such a client and
|
||||
// drop the connection for security reasons.
|
||||
xRet = ERR_BUF;
|
||||
@ -397,7 +397,7 @@ vMBTCPPortBindAddr(const CHAR* pcBindIp)
|
||||
{
|
||||
if (listen(xListenSockFd, MB_TCP_NET_LISTEN_BACKLOG) != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error occurred during listen: errno=%d", errno);
|
||||
ESP_LOGE(TAG, "Error occurred during listen: errno=%u", (unsigned)errno);
|
||||
close(xListenSockFd);
|
||||
xListenSockFd = -1;
|
||||
continue;
|
||||
@ -405,8 +405,8 @@ vMBTCPPortBindAddr(const CHAR* pcBindIp)
|
||||
}
|
||||
// Bind was successful
|
||||
pcStr = (pxCurAddr->ai_canonname == NULL) ? (CHAR*)"\0" : pxCurAddr->ai_canonname;
|
||||
ESP_LOGI(TAG, "Socket (#%d), listener %s on port: %d, errno=%d",
|
||||
xListenSockFd, pcStr, xConfig.usPort, errno);
|
||||
ESP_LOGI(TAG, "Socket (#%d), listener %s on port: %u, errno=%u",
|
||||
(int)xListenSockFd, pcStr, (unsigned)xConfig.usPort, (unsigned)errno);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -476,11 +476,11 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
// error occurred during wait for read
|
||||
ESP_LOGE(TAG, "select() errno = %d.", errno);
|
||||
ESP_LOGE(TAG, "select() errno = %u.", (unsigned)errno);
|
||||
continue;
|
||||
} else if (xErr == 0) {
|
||||
// If timeout happened, something is wrong
|
||||
ESP_LOGE(TAG, "select() timeout, errno = %d.", errno);
|
||||
ESP_LOGE(TAG, "select() timeout, errno = %u.", (unsigned)errno);
|
||||
}
|
||||
|
||||
// If something happened on the master socket, then its an incoming connection.
|
||||
@ -496,7 +496,7 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
// if request for new connection but no space left
|
||||
if (pxClientInfo != NULL) {
|
||||
if (xConfig.pxMbClientInfo[MB_TCP_PORT_MAX_CONN] == NULL) {
|
||||
ESP_LOGE(TAG, "Fail to accept connection %d, only %d connections supported.", i + 1, MB_TCP_PORT_MAX_CONN);
|
||||
ESP_LOGE(TAG, "Fail to accept connection %u, only %u connections supported.", (unsigned)(i + 1), (unsigned)MB_TCP_PORT_MAX_CONN);
|
||||
}
|
||||
xConfig.pxMbClientInfo[MB_TCP_PORT_MAX_CONN] = pxClientInfo; // set last connection info
|
||||
} else {
|
||||
@ -510,7 +510,7 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
// Accept new client connection
|
||||
pxClientInfo->xSockId = xMBTCPPortAcceptConnection(xListenSock, &pcClientIp);
|
||||
if (pxClientInfo->xSockId < 0) {
|
||||
ESP_LOGE(TAG, "Fail to accept connection for client %d.", (xConfig.usClientCount - 1));
|
||||
ESP_LOGE(TAG, "Fail to accept connection for client %u.", (unsigned)(xConfig.usClientCount - 1));
|
||||
// Accept connection fail, then free client info and continue polling.
|
||||
vMBTCPPortFreeClientInfo(pxClientInfo);
|
||||
pxClientInfo = NULL;
|
||||
@ -518,7 +518,7 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
}
|
||||
pxClientInfo->pucTCPBuf = calloc(MB_TCP_BUF_SIZE, sizeof(UCHAR));
|
||||
if (!pxClientInfo->pucTCPBuf) {
|
||||
ESP_LOGE(TAG, "Fail to allocate buffer for client %d.", (xConfig.usClientCount - 1));
|
||||
ESP_LOGE(TAG, "Fail to allocate buffer for client %u.", (unsigned)(xConfig.usClientCount - 1));
|
||||
vMBTCPPortFreeClientInfo(pxClientInfo);
|
||||
pxClientInfo = NULL;
|
||||
continue;
|
||||
@ -552,18 +552,18 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
switch(xErr)
|
||||
{
|
||||
case ERR_TIMEOUT:
|
||||
ESP_LOGE(TAG, "Socket (#%d)(%s), data receive timeout, time[us]: %d, close active connection.",
|
||||
pxClientInfo->xSockId, pxClientInfo->pcIpAddr,
|
||||
(int)(xTimeStamp - pxClientInfo->xRecvTimeStamp));
|
||||
ESP_LOGE(TAG, "Socket (#%d)(%s), data receive timeout, time[us]: %" PRIu64 ", close active connection.",
|
||||
(int)pxClientInfo->xSockId, pxClientInfo->pcIpAddr,
|
||||
(uint64_t)(xTimeStamp - pxClientInfo->xRecvTimeStamp));
|
||||
break;
|
||||
case ERR_CLSD:
|
||||
ESP_LOGE(TAG, "Socket (#%d)(%s), connection closed by peer.",
|
||||
pxClientInfo->xSockId, pxClientInfo->pcIpAddr);
|
||||
(int)pxClientInfo->xSockId, pxClientInfo->pcIpAddr);
|
||||
break;
|
||||
case ERR_BUF:
|
||||
default:
|
||||
ESP_LOGE(TAG, "Socket (#%d)(%s), read data error: %d",
|
||||
pxClientInfo->xSockId, pxClientInfo->pcIpAddr, xErr);
|
||||
ESP_LOGE(TAG, "Socket (#%d)(%s), read data error: 0x%x",
|
||||
(int)pxClientInfo->xSockId, pxClientInfo->pcIpAddr, (int)xErr);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -594,35 +594,35 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
xMBPortEventPost(EV_FRAME_RECEIVED);
|
||||
|
||||
ESP_LOGD(TAG, "Socket (#%d)(%s), get packet TID=0x%X, %d bytes.",
|
||||
pxClientInfo->xSockId, pxClientInfo->pcIpAddr,
|
||||
pxClientInfo->usTidCnt, xErr);
|
||||
(int)pxClientInfo->xSockId, pxClientInfo->pcIpAddr,
|
||||
(int)pxClientInfo->usTidCnt, (int)xErr);
|
||||
|
||||
// Wait while response is not processed by stack by timeout
|
||||
UCHAR* pucSentBuffer = vxMBTCPPortRespQueueRecv(xConfig.xRespQueueHandle);
|
||||
if (pucSentBuffer == NULL) {
|
||||
ESP_LOGD(TAG, "Response is ignored, time exceeds configured %d [ms].",
|
||||
MB_TCP_RESP_TIMEOUT_MS);
|
||||
(unsigned)MB_TCP_RESP_TIMEOUT_MS);
|
||||
} else {
|
||||
USHORT usSentTid = MB_TCP_GET_FIELD(pucSentBuffer, MB_TCP_TID);
|
||||
if (usSentTid != pxClientInfo->usTidCnt) {
|
||||
ESP_LOGE(TAG, "Sent TID(%x) != Recv TID(%x), ignore packet.",
|
||||
usSentTid, pxClientInfo->usTidCnt);
|
||||
(int)usSentTid, (int)pxClientInfo->usTidCnt);
|
||||
}
|
||||
}
|
||||
|
||||
// Get time stamp of last data update
|
||||
pxClientInfo->xSendTimeStamp = xMBTCPGetTimeStamp();
|
||||
ESP_LOGD(TAG, "Client %d, Socket(#%d), processing time = %d (us).",
|
||||
pxClientInfo->xIndex, pxClientInfo->xSockId,
|
||||
(int)(pxClientInfo->xSendTimeStamp - pxClientInfo->xRecvTimeStamp));
|
||||
ESP_LOGD(TAG, "Client %d, Socket(#%d), processing time = %" PRIu64 "(us).",
|
||||
(int)pxClientInfo->xIndex, (int)pxClientInfo->xSockId,
|
||||
(uint64_t)(pxClientInfo->xSendTimeStamp - pxClientInfo->xRecvTimeStamp));
|
||||
}
|
||||
} else {
|
||||
if (pxClientInfo) {
|
||||
// client is not ready to be read
|
||||
int64_t xTime = xMBTCPGetTimeStamp() - pxClientInfo->xRecvTimeStamp;
|
||||
if (xTime > MB_TCP_DISCONNECT_TIMEOUT) {
|
||||
ESP_LOGE(TAG, "Client %d, Socket(#%d) do not answer for %d (us). Drop connection...",
|
||||
pxClientInfo->xIndex, pxClientInfo->xSockId, (int)(xTime));
|
||||
ESP_LOGE(TAG, "Client %d, Socket(#%d) do not answer for %" PRIu64 " (us). Drop connection...",
|
||||
(int)pxClientInfo->xIndex, (int)pxClientInfo->xSockId, (uint64_t)xTime);
|
||||
xMBTCPPortCloseConnection(pxClientInfo);
|
||||
|
||||
// This client does not respond, then delete registered data
|
||||
@ -630,7 +630,7 @@ static void vMBTCPPortServerTask(void *pvParameters)
|
||||
xConfig.pxMbClientInfo[i] = NULL;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Client %d is disconnected.", i);
|
||||
ESP_LOGE(TAG, "Client %d is disconnected.", (int)i);
|
||||
}
|
||||
}
|
||||
} // if ((pxClientInfo != NULL)
|
||||
@ -720,8 +720,8 @@ xMBTCPPortSendResponse( UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
// Check if socket writable
|
||||
xErr = select(xConfig.pxCurClientInfo->xSockId + 1, NULL, &xWriteSet, &xErrorSet, &xTimeVal);
|
||||
if ((xErr == -1) || FD_ISSET(xConfig.pxCurClientInfo->xSockId, &xErrorSet)) {
|
||||
ESP_LOGE(TAG, "Socket(#%d) , send select() error = %d.",
|
||||
xConfig.pxCurClientInfo->xSockId, errno);
|
||||
ESP_LOGE(TAG, "Socket(#%d) , send select() error = %u.",
|
||||
(int)xConfig.pxCurClientInfo->xSockId, (unsigned)errno);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@ -732,8 +732,8 @@ xMBTCPPortSendResponse( UCHAR * pucMBTCPFrame, USHORT usTCPLength )
|
||||
// Write message into socket and disable Nagle's algorithm
|
||||
xErr = send(xConfig.pxCurClientInfo->xSockId, pucMBTCPFrame, usTCPLength, TCP_NODELAY);
|
||||
if (xErr < 0) {
|
||||
ESP_LOGE(TAG, "Socket(#%d), fail to send data, errno = %d",
|
||||
xConfig.pxCurClientInfo->xSockId, errno);
|
||||
ESP_LOGE(TAG, "Socket(#%d), fail to send data, errno = %u",
|
||||
(int)xConfig.pxCurClientInfo->xSockId, (unsigned)errno);
|
||||
xConfig.pxCurClientInfo->xError = xErr;
|
||||
} else {
|
||||
bFrameSent = TRUE;
|
||||
|
@ -2,4 +2,3 @@ set(PROJECT_NAME "modbus_master")
|
||||
|
||||
idf_component_register(SRCS "master.c"
|
||||
INCLUDE_DIRS ".")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
@ -121,7 +121,7 @@ static void* master_get_param_data(const mb_parameter_descriptor_t* param_descri
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Wrong parameter offset for CID #%d", param_descriptor->cid);
|
||||
ESP_LOGE(TAG, "Wrong parameter offset for CID #%d", (int)param_descriptor->cid);
|
||||
assert(instance_ptr != NULL);
|
||||
}
|
||||
return instance_ptr;
|
||||
@ -155,11 +155,11 @@ static void master_operation_func(void *arg)
|
||||
err = mbc_master_get_parameter(cid, (char*)param_descriptor->param_key,
|
||||
(uint8_t*)temp_data_ptr, &type);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%08x) read successful.",
|
||||
param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%" PRIx32 ") read successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
// Initialize data of test array and write to slave
|
||||
if (*(uint32_t*)temp_data_ptr != 0xAAAAAAAA) {
|
||||
memset((void*)temp_data_ptr, 0xAA, param_descriptor->param_size);
|
||||
@ -167,14 +167,14 @@ static void master_operation_func(void *arg)
|
||||
err = mbc_master_set_parameter(cid, (char*)param_descriptor->param_key,
|
||||
(uint8_t*)temp_data_ptr, &type);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%08x), write successful.",
|
||||
param_descriptor->cid,
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%" PRIx32 "), write successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) write fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -182,7 +182,7 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) read fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -194,8 +194,8 @@ static void master_operation_func(void *arg)
|
||||
*(float*)temp_data_ptr = value;
|
||||
if ((param_descriptor->mb_param_type == MB_PARAM_HOLDING) ||
|
||||
(param_descriptor->mb_param_type == MB_PARAM_INPUT)) {
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%x) read successful.",
|
||||
param_descriptor->cid,
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%" PRIx32 ") read successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
value,
|
||||
@ -209,7 +209,7 @@ static void master_operation_func(void *arg)
|
||||
uint16_t state = *(uint16_t*)temp_data_ptr;
|
||||
const char* rw_str = (state & param_descriptor->param_opts.opt1) ? "ON" : "OFF";
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %s (0x%x) read successful.",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
(const char*)rw_str,
|
||||
@ -221,7 +221,7 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) read fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -235,7 +235,7 @@ static void master_operation_func(void *arg)
|
||||
|
||||
if (alarm_state) {
|
||||
ESP_LOGI(TAG, "Alarm triggered by cid #%d.",
|
||||
param_descriptor->cid);
|
||||
(int)param_descriptor->cid);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Alarm is not triggered after %d retries.",
|
||||
MASTER_MAX_RETRY);
|
||||
@ -264,12 +264,10 @@ static esp_err_t master_init(void)
|
||||
MB_RETURN_ON_FALSE((master_handler != NULL), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller initialization fail.");
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller initialization fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
"mb controller initialization fail, returns(0x%x).", (int)err);
|
||||
err = mbc_master_setup((void*)&comm);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller setup fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
"mb controller setup fail, returns(0x%x).", (int)err);
|
||||
|
||||
// Set UART pin numbers
|
||||
err = uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
|
||||
@ -277,21 +275,19 @@ static esp_err_t master_init(void)
|
||||
|
||||
err = mbc_master_start();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller start fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
"mb controller start fail, returned (0x%x).", (int)err);
|
||||
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb serial set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)err);
|
||||
"mb serial set pin failure, uart_set_pin() returned (0x%x).", (int)err);
|
||||
// Set driver mode to Half Duplex
|
||||
err = uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)err);
|
||||
"mb serial set mode failure, uart_set_mode() returned (0x%x).", (int)err);
|
||||
|
||||
vTaskDelay(5);
|
||||
err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
|
||||
"mb controller set descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
"mb controller set descriptor fail, returns(0x%x).", (int16_t)err);
|
||||
ESP_LOGI(TAG, "Modbus master stack initialized...");
|
||||
return err;
|
||||
}
|
||||
|
@ -2,4 +2,4 @@ set(PROJECT_NAME "modbus_slave")
|
||||
|
||||
idf_component_register(SRCS "slave.c"
|
||||
INCLUDE_DIRS ".")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
|
||||
|
@ -182,13 +182,13 @@ void app_main(void)
|
||||
if(event & (MB_EVENT_HOLDING_REG_WR | MB_EVENT_HOLDING_REG_RD)) {
|
||||
// Get parameter information from parameter queue
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "HOLDING %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
ESP_LOGI(TAG, "HOLDING %s (%" PRIu32 " us), ADDR:%u, TYPE:%u, INST_ADDR:0x%" PRIx32 ", SIZE:%u",
|
||||
rw_str,
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.size);
|
||||
if (reg_info.address == (uint8_t*)&holding_reg_params.holding_data0)
|
||||
{
|
||||
portENTER_CRITICAL(¶m_lock);
|
||||
@ -200,29 +200,29 @@ void app_main(void)
|
||||
}
|
||||
} else if (event & MB_EVENT_INPUT_REG_RD) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "INPUT READ (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
ESP_LOGI(TAG, "INPUT READ (%" PRIu32 " us), ADDR:%u, TYPE:%u, INST_ADDR:0x%" PRIx32 ", SIZE:%u",
|
||||
reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.size);
|
||||
} else if (event & MB_EVENT_DISCRETE_RD) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "DISCRETE READ (%u us): ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
ESP_LOGI(TAG, "DISCRETE READ (%" PRIu32 " us): ADDR:%u, TYPE:%u, INST_ADDR:0x%" PRIx32 ", SIZE:%u",
|
||||
reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.size);
|
||||
} else if (event & (MB_EVENT_COILS_RD | MB_EVENT_COILS_WR)) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "COILS %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
ESP_LOGI(TAG, "COILS %s (%" PRIu32 " us), ADDR:%u, TYPE:%u, INST_ADDR:0x%" PRIx32 ", SIZE:%u",
|
||||
rw_str,
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.size);
|
||||
if (coil_reg_params.coils_port1 == 0xFF) break;
|
||||
}
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ static int master_get_slave_ip_stdin(char** addr_table)
|
||||
ESP_ERROR_CHECK(example_configure_stdin_stdout());
|
||||
while(1) {
|
||||
if (addr_table[ip_cnt] && strcmp(addr_table[ip_cnt], "FROM_STDIN") == 0) {
|
||||
printf("Waiting IP%d from stdin:\r\n", ip_cnt);
|
||||
printf("Waiting IP%d from stdin:\r\n", (int)ip_cnt);
|
||||
while (fgets(buf, sizeof(buf), stdin) == NULL) {
|
||||
fputs(buf, stdout);
|
||||
}
|
||||
@ -194,7 +194,7 @@ static int master_get_slave_ip_stdin(char** addr_table)
|
||||
fputc('\n', stdout);
|
||||
ip_str = master_scan_addr(&index, buf);
|
||||
if (ip_str != NULL) {
|
||||
ESP_LOGI(TAG, "IP(%d) = [%s] set from stdin.", ip_cnt, ip_str);
|
||||
ESP_LOGI(TAG, "IP(%d) = [%s] set from stdin.", (int)ip_cnt, ip_str);
|
||||
if ((ip_cnt >= ip_table_sz) || (index != ip_cnt)) {
|
||||
addr_table[ip_cnt] = NULL;
|
||||
break;
|
||||
@ -207,10 +207,10 @@ static int master_get_slave_ip_stdin(char** addr_table)
|
||||
}
|
||||
} else {
|
||||
if (addr_table[ip_cnt]) {
|
||||
ESP_LOGI(TAG, "Leave IP(%d) = [%s] set manually.", ip_cnt, addr_table[ip_cnt]);
|
||||
ESP_LOGI(TAG, "Leave IP(%d) = [%s] set manually.", (int)ip_cnt, addr_table[ip_cnt]);
|
||||
ip_cnt++;
|
||||
} else {
|
||||
ESP_LOGI(TAG, "IP(%d) is not set in the table.", ip_cnt);
|
||||
ESP_LOGI(TAG, "IP(%d) is not set in the table.", (int)ip_cnt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -306,7 +306,7 @@ static esp_err_t master_resolve_slave(uint8_t short_addr, mdns_result_t* result,
|
||||
char slave_name[22] = {0};
|
||||
|
||||
if (sprintf(slave_name, "mb_slave_tcp_%02X", short_addr) < 0) {
|
||||
ESP_LOGE(TAG, "Fail to create instance name for index: %d", short_addr);
|
||||
ESP_LOGE(TAG, "Fail to create instance name for index: %d", (int)short_addr);
|
||||
abort();
|
||||
}
|
||||
for (; r ; r = r->next) {
|
||||
@ -328,7 +328,7 @@ static esp_err_t master_resolve_slave(uint8_t short_addr, mdns_result_t* result,
|
||||
}
|
||||
slave_ip = master_get_slave_ip_str(r->addr, addr_type);
|
||||
if (slave_ip) {
|
||||
ESP_LOGI(TAG, "Resolved slave %s[%s]:%u", r->hostname, slave_ip, r->port);
|
||||
ESP_LOGI(TAG, "Resolved slave %s[%s]:%u", r->hostname, slave_ip, (unsigned)r->port);
|
||||
*resolved_ip = slave_ip;
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -367,7 +367,7 @@ static int master_create_slave_list(mdns_result_t* results, char** addr_table,
|
||||
// Resolve new slave IP address using its short address
|
||||
esp_err_t err = master_resolve_slave(slave_addr, results, &slave_ip, addr_type);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Index: %d, sl_addr: %d, failed to resolve!", i, slave_addr);
|
||||
ESP_LOGE(TAG, "Index: %d, sl_addr: %d, failed to resolve!", (int)i, (int)slave_addr);
|
||||
// Set correspond index to NULL indicate host not resolved
|
||||
ip_table[ip_index] = NULL;
|
||||
continue;
|
||||
@ -384,7 +384,7 @@ static int master_create_slave_list(mdns_result_t* results, char** addr_table,
|
||||
LIST_INSERT_HEAD(&slave_addr_list, new_slave_entry, entries);
|
||||
ip_table[ip_index] = slave_ip;
|
||||
ESP_LOGI(TAG, "Index: %d, sl_addr: %d, resolved to IP: [%s]",
|
||||
i, slave_addr, slave_ip);
|
||||
(int)i, (int)slave_addr, slave_ip);
|
||||
cid_resolve_cnt++;
|
||||
if (ip_index < addr_table_size) {
|
||||
ip_index++;
|
||||
@ -392,11 +392,11 @@ static int master_create_slave_list(mdns_result_t* results, char** addr_table,
|
||||
} else {
|
||||
ip_table[ip_index] = it ? it->ip_address : ip_table[ip_index];
|
||||
ESP_LOGI(TAG, "Index: %d, sl_addr: %d, set to IP: [%s]",
|
||||
i, slave_addr, ip_table[ip_index]);
|
||||
(int)i, (int)slave_addr, ip_table[ip_index]);
|
||||
cid_resolve_cnt++;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Resolved %d cids, with %d IP addresses", cid_resolve_cnt, ip_index);
|
||||
ESP_LOGI(TAG, "Resolved %d cids, with %d IP addresses", (int)cid_resolve_cnt, (int)ip_index);
|
||||
return cid_resolve_cnt;
|
||||
}
|
||||
|
||||
@ -471,7 +471,7 @@ static void* master_get_param_data(const mb_parameter_descriptor_t* param_descri
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Wrong parameter offset for CID #%d", param_descriptor->cid);
|
||||
ESP_LOGE(TAG, "Wrong parameter offset for CID #%d", (int)param_descriptor->cid);
|
||||
assert(instance_ptr != NULL);
|
||||
}
|
||||
return instance_ptr;
|
||||
@ -505,11 +505,11 @@ static void master_operation_func(void *arg)
|
||||
err = mbc_master_get_parameter(cid, (char*)param_descriptor->param_key,
|
||||
(uint8_t*)temp_data_ptr, &type);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%08x) read successful.",
|
||||
param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%" PRIx32 ") read successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
// Initialize data of test array and write to slave
|
||||
if (*(uint32_t*)temp_data_ptr != 0xAAAAAAAA) {
|
||||
memset((void*)temp_data_ptr, 0xAA, param_descriptor->param_size);
|
||||
@ -517,14 +517,14 @@ static void master_operation_func(void *arg)
|
||||
err = mbc_master_set_parameter(cid, (char*)param_descriptor->param_key,
|
||||
(uint8_t*)temp_data_ptr, &type);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x%08x), write successful.",
|
||||
param_descriptor->cid,
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = (0x" PRIx32 "), write successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
*(uint32_t*)temp_data_ptr);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) write fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -532,7 +532,7 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) read fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -544,8 +544,8 @@ static void master_operation_func(void *arg)
|
||||
if ((param_descriptor->mb_param_type == MB_PARAM_HOLDING) ||
|
||||
(param_descriptor->mb_param_type == MB_PARAM_INPUT)) {
|
||||
value = *(float*)temp_data_ptr;
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%x) read successful.",
|
||||
param_descriptor->cid,
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%" PRIx32 ") read successful.",
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
value,
|
||||
@ -559,7 +559,7 @@ static void master_operation_func(void *arg)
|
||||
uint8_t state = *(uint8_t*)temp_data_ptr;
|
||||
const char* rw_str = (state & param_descriptor->param_opts.opt1) ? "ON" : "OFF";
|
||||
ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %s (0x%x) read successful.",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(char*)param_descriptor->param_units,
|
||||
(const char*)rw_str,
|
||||
@ -571,7 +571,7 @@ static void master_operation_func(void *arg)
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Characteristic #%d (%s) read fail, err = 0x%x (%s).",
|
||||
param_descriptor->cid,
|
||||
(int)param_descriptor->cid,
|
||||
(char*)param_descriptor->param_key,
|
||||
(int)err,
|
||||
(char*)esp_err_to_name(err));
|
||||
@ -585,7 +585,7 @@ static void master_operation_func(void *arg)
|
||||
|
||||
if (alarm_state) {
|
||||
ESP_LOGI(TAG, "Alarm triggered by cid #%d.",
|
||||
param_descriptor->cid);
|
||||
(int)param_descriptor->cid);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Alarm is not triggered after %d retries.",
|
||||
MASTER_MAX_RETRY);
|
||||
@ -604,17 +604,17 @@ static esp_err_t init_services(mb_tcp_addr_type_t ip_addr_type)
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"nvs_flash_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
result = esp_netif_init();
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_netif_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
result = esp_event_loop_create_default();
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_event_loop_create_default fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
// Start mdns service and register device
|
||||
master_start_mdns_service();
|
||||
@ -626,13 +626,13 @@ static esp_err_t init_services(mb_tcp_addr_type_t ip_addr_type)
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"example_connect fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
||||
result = esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_wifi_set_ps fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#endif
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
int res = 0;
|
||||
@ -640,7 +640,7 @@ static esp_err_t init_services(mb_tcp_addr_type_t ip_addr_type)
|
||||
res = master_query_slave_service("_modbus", "_tcp", ip_addr_type);
|
||||
}
|
||||
if (res < num_device_parameters) {
|
||||
ESP_LOGE(TAG, "Could not resolve one or more slave IP addresses, resolved: %d out of %d.", res, num_device_parameters );
|
||||
ESP_LOGE(TAG, "Could not resolve one or more slave IP addresses, resolved: %d out of %d.", (uint16_t)res, (uint16_t)num_device_parameters );
|
||||
ESP_LOGE(TAG, "Make sure you configured all slaves according to device parameter table and they alive in the network.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
@ -648,7 +648,7 @@ static esp_err_t init_services(mb_tcp_addr_type_t ip_addr_type)
|
||||
#elif CONFIG_MB_SLAVE_IP_FROM_STDIN
|
||||
int ip_cnt = master_get_slave_ip_stdin(slave_ip_address_table);
|
||||
if (ip_cnt) {
|
||||
ESP_LOGI(TAG, "Configured %d IP addresse(s).", ip_cnt);
|
||||
ESP_LOGI(TAG, "Configured %d IP addresse(s).", (int)ip_cnt);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Fail to get IP address from stdin. Continue.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
@ -666,22 +666,22 @@ static esp_err_t destroy_services(void)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"example_disconnect fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = esp_event_loop_delete_default();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_event_loop_delete_default fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = esp_netif_deinit();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_netif_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = nvs_flash_deinit();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"nvs_flash_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -697,26 +697,26 @@ static esp_err_t master_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mb controller initialization fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
err = mbc_master_setup((void*)comm_info);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mb controller setup fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
err = mbc_master_set_descriptor(&device_parameters[0], num_device_parameters);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mb controller set descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
ESP_LOGI(TAG, "Modbus master stack initialized...");
|
||||
|
||||
err = mbc_master_start();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mb controller start fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
vTaskDelay(5);
|
||||
return err;
|
||||
}
|
||||
@ -727,7 +727,7 @@ static esp_err_t master_destroy(void)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_master_destroy fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
ESP_LOGI(TAG, "Modbus master stack destroy...");
|
||||
return err;
|
||||
}
|
||||
|
@ -180,11 +180,11 @@ static void slave_operation_func(void *arg)
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "HOLDING %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
rw_str,
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(int)reg_info.address,
|
||||
(unsigned)reg_info.size);
|
||||
if (reg_info.address == (uint8_t*)&holding_reg_params.holding_data0)
|
||||
{
|
||||
portENTER_CRITICAL(¶m_lock);
|
||||
@ -197,28 +197,28 @@ static void slave_operation_func(void *arg)
|
||||
} else if (event & MB_EVENT_INPUT_REG_RD) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "INPUT READ (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(int)reg_info.address,
|
||||
(unsigned)reg_info.size);
|
||||
} else if (event & MB_EVENT_DISCRETE_RD) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "DISCRETE READ (%u us): ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(int)reg_info.address,
|
||||
(unsigned)reg_info.size);
|
||||
} else if (event & (MB_EVENT_COILS_RD | MB_EVENT_COILS_WR)) {
|
||||
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
||||
ESP_LOGI(TAG, "COILS %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
||||
rw_str,
|
||||
(uint32_t)reg_info.time_stamp,
|
||||
(uint32_t)reg_info.mb_offset,
|
||||
(uint32_t)reg_info.type,
|
||||
(uint32_t)reg_info.address,
|
||||
(uint32_t)reg_info.size);
|
||||
(unsigned)reg_info.time_stamp,
|
||||
(unsigned)reg_info.mb_offset,
|
||||
(unsigned)reg_info.type,
|
||||
(int)reg_info.address,
|
||||
(unsigned)reg_info.size);
|
||||
if (coil_reg_params.coils_port1 == 0xFF) break;
|
||||
}
|
||||
}
|
||||
@ -237,17 +237,17 @@ static esp_err_t init_services(void)
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"nvs_flash_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
result = esp_netif_init();
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_netif_init fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
result = esp_event_loop_create_default();
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_event_loop_create_default fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
// Start mdns service and register device
|
||||
start_mdns_service();
|
||||
@ -259,13 +259,13 @@ static esp_err_t init_services(void)
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"example_connect fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
||||
result = esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
MB_RETURN_ON_FALSE((result == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_wifi_set_ps fail, returns(0x%x).",
|
||||
(uint32_t)result);
|
||||
(int)result);
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -278,22 +278,22 @@ static esp_err_t destroy_services(void)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"example_disconnect fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = esp_event_loop_delete_default();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_event_loop_delete_default fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = esp_netif_deinit();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"esp_netif_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
err = nvs_flash_deinit();
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"nvs_flash_deinit fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
#if CONFIG_MB_MDNS_IP_RESOLVER
|
||||
stop_mdns_service();
|
||||
#endif
|
||||
@ -322,7 +322,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_setup fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
// The code below initializes Modbus register area descriptors
|
||||
// for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
|
||||
@ -338,7 +338,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
reg_area.type = MB_PARAM_HOLDING; // Set type of register area
|
||||
reg_area.start_offset = MB_REG_HOLDING_START_AREA1; // Offset of register area in Modbus protocol
|
||||
@ -348,7 +348,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
// Initialization of Input Registers area
|
||||
reg_area.type = MB_PARAM_INPUT;
|
||||
@ -359,7 +359,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
reg_area.type = MB_PARAM_INPUT;
|
||||
reg_area.start_offset = MB_REG_INPUT_START_AREA1;
|
||||
reg_area.address = (void*)&input_reg_params.input_data4;
|
||||
@ -368,7 +368,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
// Initialization of Coils register area
|
||||
reg_area.type = MB_PARAM_COIL;
|
||||
@ -379,7 +379,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
// Initialization of Discrete Inputs register area
|
||||
reg_area.type = MB_PARAM_DISCRETE;
|
||||
@ -390,7 +390,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_set_descriptor fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
|
||||
// Set values into known state
|
||||
setup_reg_data();
|
||||
@ -400,7 +400,7 @@ static esp_err_t slave_init(mb_communication_info_t* comm_info)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_start fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
vTaskDelay(5);
|
||||
return err;
|
||||
}
|
||||
@ -411,7 +411,7 @@ static esp_err_t slave_destroy(void)
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
|
||||
TAG,
|
||||
"mbc_slave_destroy fail, returns(0x%x).",
|
||||
(uint32_t)err);
|
||||
(int)err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user