From f81ef8229e48e84628df78e66e4512e468c94850 Mon Sep 17 00:00:00 2001 From: aleks Date: Thu, 6 Jun 2024 12:06:02 +0200 Subject: [PATCH] tests: add transaction time handling --- freemodbus/modbus/mb_m.c | 4 ++-- test/serial/mb_serial_master/main/master.c | 17 ++++++++++++++--- test/tcp/mb_tcp_master/main/tcp_master.c | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/freemodbus/modbus/mb_m.c b/freemodbus/modbus/mb_m.c index 456381e..573dfd7 100644 --- a/freemodbus/modbus/mb_m.c +++ b/freemodbus/modbus/mb_m.c @@ -272,7 +272,7 @@ eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP atomic_init(&usMasterSendPDULength, 0); atomic_init(&eMBMasterCurErrorType, EV_ERROR_INIT); atomic_init(&xMBRunInMasterMode, FALSE); - atomic_init(&ucMBMasterDestAddress, MB_TCP_PSEUDO_ADDRESS); + atomic_init(&ucMBMasterDestAddress, 0); } /* initialize the OS resource for modbus master. */ vMBMasterOsResInit(); @@ -514,7 +514,7 @@ eMBMasterPoll( void ) ESP_LOGD( MB_PORT_TAG, "Transaction (%" PRIu64 "), processing time(us) = %" PRId64, xCurTransactionId, xProcTime ); MB_ATOMIC_SECTION { xTransactionInfo.xTransId = xCurTransactionId; - xTransactionInfo.ucDestAddr = atomic_load(&ucMBMasterDestAddress); + xTransactionInfo.ucDestAddr = ucMBMasterGetDestAddress(); xTransactionInfo.ucFuncCode = ucFunctionCode; xTransactionInfo.eException = eException; xTransactionInfo.ucFrameError = errorType; diff --git a/test/serial/mb_serial_master/main/master.c b/test/serial/mb_serial_master/main/master.c index 63d3e0a..2559525 100644 --- a/test/serial/mb_serial_master/main/master.c +++ b/test/serial/mb_serial_master/main/master.c @@ -6,6 +6,7 @@ #include "string.h" #include "esp_log.h" +#include "esp_timer.h" #include "modbus_params.h" // for modbus parameters structures #include "mbcontroller.h" #include "sdkconfig.h" @@ -378,6 +379,7 @@ static void master_operation_func(void *arg) } #endif } else if (cid <= CID_HOLD_DATA_2) { + uint64_t start_timestamp = esp_timer_get_time(); // Get current timestamp in microseconds if (TEST_VERIFY_VALUES(param_descriptor, (float *)temp_data_ptr) == ESP_OK) { ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%" PRIx32 ") read successful.", (int)param_descriptor->cid, @@ -394,10 +396,19 @@ static void master_operation_func(void *arg) } mb_trans_info_t tinfo = {0}; if (mbc_master_get_transaction_info(&tinfo) == ESP_OK) { - ESP_LOGI("TRANS_INFO", "Id: %" PRIu64 ", Addr: %x, FC: %x, Exp: %u, Err: %x", + bool trans_is_expired = (tinfo.trans_id >= (start_timestamp + (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND * 1000))); + ESP_LOGW("TRANS_INFO", "Id: %" PRIu64 ", Addr: %x, FC: 0x%x, Exception: %u, Err: %u %s", (uint64_t)tinfo.trans_id, (int)tinfo.dest_addr, - (unsigned)tinfo.func_code, (unsigned)tinfo.exception, - (int)tinfo.err_type); + (int)tinfo.func_code, (unsigned)tinfo.exception, + (int)tinfo.err_type, + trans_is_expired ? "(EXPIRED)" : ""); + // Check if the response time is expired sinse start of transaction, + // or the other IO is performed from different thread. + if (trans_is_expired) { + ESP_LOGE("TRANS_INFO", "Transaction Id: %" PRIu64 ", is expired.", tinfo.trans_id); + alarm_state = true; + break; + } } } else if ((cid >= CID_RELAY_P1) && (cid <= CID_DISCR_P1)) { if (TEST_VERIFY_VALUES(param_descriptor, (uint8_t *)temp_data_ptr) == ESP_OK) { diff --git a/test/tcp/mb_tcp_master/main/tcp_master.c b/test/tcp/mb_tcp_master/main/tcp_master.c index b47c622..83bef2c 100644 --- a/test/tcp/mb_tcp_master/main/tcp_master.c +++ b/test/tcp/mb_tcp_master/main/tcp_master.c @@ -9,6 +9,7 @@ #include #include #include "esp_log.h" +#include "esp_timer.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" @@ -729,6 +730,7 @@ static void master_operation_func(void *arg) } #endif } else if (cid <= CID_HOLD_DATA_2) { + uint64_t start_timestamp = esp_timer_get_time(); // Get current timestamp in microseconds if (TEST_VERIFY_VALUES(param_descriptor, (float *)temp_data_ptr) == ESP_OK) { ESP_LOGI(TAG, "Characteristic #%d %s (%s) value = %f (0x%" PRIx32 ") read successful.", (int)param_descriptor->cid, @@ -743,6 +745,22 @@ static void master_operation_func(void *arg) alarm_state = true; break; } + mb_trans_info_t tinfo = {0}; // The transaction information structure + if (mbc_master_get_transaction_info(&tinfo) == ESP_OK) { + bool trans_is_expired = (tinfo.trans_id >= (start_timestamp + (CONFIG_FMB_MASTER_TIMEOUT_MS_RESPOND * 1000))); + ESP_LOGW("TRANS_INFO", "Id: %" PRIu64 ", Addr: %x, FC: 0x%x, Exception: %u, Err: %u %s", + (uint64_t)tinfo.trans_id, (int)tinfo.dest_addr, + (int)tinfo.func_code, (unsigned)tinfo.exception, + (int)tinfo.err_type, + trans_is_expired ? "(EXPIRED)" : ""); + // Check if the response time is expired sinse start of transaction, + // or the other IO is performed from different thread. + if (trans_is_expired) { + ESP_LOGE("TRANS_INFO", "Transaction Id: %" PRIu64 ", is expired.", tinfo.trans_id); + alarm_state = true; + break; + } + } } else if ((cid >= CID_RELAY_P1) && (cid <= CID_DISCR_P1)) { if (TEST_VERIFY_VALUES(param_descriptor, (uint8_t *)temp_data_ptr) == ESP_OK) { uint8_t state = *(uint8_t *)temp_data_ptr;