forked from espressif/esp-modbus
feature add custom fc handlers for master and slave v2
This commit is contained in:
1 parent
439c767aed
commit
8215e3d0d2
51 files changed
+1345
-300
No files matched your search
@@ -18,6 +18,6 @@ CONFIG_MB_PORT_ADAPTER_EN=y
|
||||
CONFIG_MB_TEST_SLAVE_TASK_PRIO=4
|
||||
CONFIG_MB_TEST_MASTER_TASK_PRIO=4
|
||||
CONFIG_MB_TEST_COMM_CYCLE_COUNTER=10
|
||||
CONFIG_MB_TEST_LEAK_CRITICAL_LEVEL=128
|
||||
CONFIG_MB_TEST_LEAK_WARN_LEVEL=128
|
||||
CONFIG_MB_TEST_LEAK_CRITICAL_LEVEL=256
|
||||
CONFIG_MB_TEST_LEAK_WARN_LEVEL=256
|
||||
|
||||
@@ -18,6 +18,6 @@ CONFIG_MB_PORT_ADAPTER_EN=y
|
||||
CONFIG_MB_TEST_SLAVE_TASK_PRIO=4
|
||||
CONFIG_MB_TEST_MASTER_TASK_PRIO=4
|
||||
CONFIG_MB_TEST_COMM_CYCLE_COUNTER=10
|
||||
CONFIG_MB_TEST_LEAK_CRITICAL_LEVEL=128
|
||||
CONFIG_MB_TEST_LEAK_WARN_LEVEL=128
|
||||
CONFIG_MB_TEST_LEAK_CRITICAL_LEVEL=256
|
||||
CONFIG_MB_TEST_LEAK_WARN_LEVEL=256
|
||||
|
||||
@@ -105,6 +105,44 @@ static esp_err_t slave_serial_init(void **pinst)
|
||||
return err;
|
||||
}
|
||||
|
||||
mb_exception_t test_handler(void *pinst, uint8_t *frame_ptr, uint16_t *plen)
|
||||
{
|
||||
return MB_EX_CRITICAL; // Set the exception code for slave appropriately
|
||||
}
|
||||
|
||||
static int check_custom_handlers(void *pinst)
|
||||
{
|
||||
mb_fn_handler_fp phandler = NULL;
|
||||
int entry;
|
||||
uint16_t count = 0;
|
||||
esp_err_t err = ESP_FAIL;
|
||||
err = mbc_get_handler_count(pinst, &count);
|
||||
MB_RETURN_ON_FALSE((err == ESP_OK), 0, TAG,
|
||||
"mbc slave get handler count, returns(0x%x).", (int)err);
|
||||
ESP_LOGI(TAG,"Object %p, custom handler test, (registered:max) handlers: %d:%d.", pinst, count, CONFIG_FMB_FUNC_HANDLERS_MAX);
|
||||
for (entry = 0x01; entry < CONFIG_FMB_FUNC_HANDLERS_MAX; entry++) {
|
||||
// Try to remove the handler
|
||||
err = mbc_delete_handler(pinst, (uint8_t)entry);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Could not remove handler for command: (0x%x), returned (0x%x), already empty?", entry, (int)err);
|
||||
}
|
||||
err = mbc_set_handler(pinst, (uint8_t)entry, test_handler);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG,"Could not set handler for command 0x%x, returned (0x%x).", entry, (int)err);
|
||||
break;
|
||||
} else {
|
||||
ESP_LOGI(TAG,"Set handler for command 0x%x, returned (0x%x).", entry, (int)err);
|
||||
}
|
||||
err = mbc_get_handler(pinst, (uint8_t)entry, &phandler);
|
||||
if (err != ESP_OK || phandler != test_handler) {
|
||||
ESP_LOGE(TAG, "Could not get handler for command (0x%x) = (%p), returned (0x%x).", entry, phandler, (int)err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Last entry processed: %d.", entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
// Intentionally verify that atomic values are layout compatible with original types
|
||||
static_assert(
|
||||
sizeof(std::atomic<int>) == sizeof(int),
|
||||
@@ -117,12 +155,18 @@ extern "C" void app_main(void)
|
||||
ESP_LOGI(TAG, "Setup master cpp....");
|
||||
ESP_ERROR_CHECK(master_serial_init(&pmaster_handle));
|
||||
ESP_ERROR_CHECK(mbc_master_stop(pmaster_handle));
|
||||
int last_entry = check_custom_handlers(pmaster_handle);
|
||||
MB_RETURN_ON_FALSE((last_entry >= CONFIG_FMB_FUNC_HANDLERS_MAX), ;, TAG,
|
||||
"Incorrect number of command entries for master: %d.", (int)last_entry);
|
||||
ESP_ERROR_CHECK(mbc_master_delete(pmaster_handle));
|
||||
ESP_LOGI(TAG, "Master test passed successfully.");
|
||||
ESP_LOGI(TAG, "Setup slave cpp....");
|
||||
ESP_ERROR_CHECK(slave_serial_init(&pslave_handle));
|
||||
last_entry = check_custom_handlers(pslave_handle);
|
||||
// explicitly check stop method before delete
|
||||
ESP_ERROR_CHECK(mbc_slave_stop(pslave_handle));
|
||||
ESP_ERROR_CHECK(mbc_slave_delete(pslave_handle));
|
||||
MB_RETURN_ON_FALSE((last_entry >= CONFIG_FMB_FUNC_HANDLERS_MAX), ;, TAG,
|
||||
"Incorrect number of command entries for slave: %d.", (int)last_entry);
|
||||
ESP_LOGI(TAG, "Slave test passed successfully.");
|
||||
}
|
||||
@@ -15,7 +15,7 @@ def test_modbus_comm_multi_dev_serial(case_tester) -> None: # typ
|
||||
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.multi_dut_modbus_tcp
|
||||
@pytest.mark.parametrize('count, config', [(2, 'wifi'), (2, 'ethernet')], indirect=True)
|
||||
@pytest.mark.parametrize('count, config', [(2, 'ethernet')], indirect=True)
|
||||
def test_modbus_comm_multi_dev_tcp(case_tester) -> None: # type: ignore
|
||||
for case in case_tester.test_menu:
|
||||
if case.attributes.get('test_env', 'multi_dut_modbus_tcp') == 'multi_dut_modbus_tcp':
|
||||
|
||||
@@ -11,6 +11,7 @@ target_include_directories(mb_ut_lib PUBLIC
|
||||
"${dir}/modbus/mb_controller/common/include"
|
||||
"${dir}/modbus/mb_controller/serial"
|
||||
"${dir}/modbus/mb_controller/tcp"
|
||||
"${dir}/modbus/mb_objects/common"
|
||||
"${dir}/modbus/mb_objects/include"
|
||||
"${dir}/modbus/mb_ports/common"
|
||||
"${dir}/modbus/mb_ports/serial"
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ mb_err_enum_t mb_port_event_wait_req_finish(mb_port_base_t *inst);
|
||||
|
||||
#if (CONFIG_FMB_COMM_MODE_ASCII_EN || CONFIG_FMB_COMM_MODE_RTU_EN)
|
||||
|
||||
typedef struct _port_serial_opts mb_serial_opts_t;
|
||||
typedef struct port_serial_opts_s mb_serial_opts_t;
|
||||
|
||||
mb_err_enum_t mbs_rtu_create(mb_serial_opts_t *ser_opts, void **in_out_obj);
|
||||
mb_err_enum_t mbs_ascii_create(mb_serial_opts_t *ser_opts, void **in_out_obj);
|
||||
@@ -93,7 +93,7 @@ mb_exception_t mbs_fn_rw_multi_holding_reg(mb_base_t *inst, uint8_t *frame_ptr,u
|
||||
|
||||
#if (CONFIG_FMB_COMM_MODE_TCP_EN)
|
||||
|
||||
typedef struct _port_tcp_opts mb_tcp_opts_t;
|
||||
typedef struct port_tcp_opts_s mb_tcp_opts_t;
|
||||
|
||||
mb_err_enum_t mbs_tcp_create(mb_tcp_opts_t *tcp_opts, void **in_out_obj);
|
||||
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ mb_err_enum_t mb_port_event_wait_req_finish(mb_port_base_t *inst);
|
||||
|
||||
#if (CONFIG_FMB_COMM_MODE_ASCII_EN || CONFIG_FMB_COMM_MODE_RTU_EN)
|
||||
|
||||
typedef struct _port_serial_opts mb_serial_opts_t;
|
||||
typedef struct port_serial_opts_s mb_serial_opts_t;
|
||||
|
||||
mb_err_enum_t mbs_rtu_create(mb_serial_opts_t *ser_opts, void **in_out_obj);
|
||||
mb_err_enum_t mbs_ascii_create(mb_serial_opts_t *ser_opts, void **in_out_obj);
|
||||
@@ -102,7 +102,7 @@ mb_err_enum_t mbs_set_slv_id(mb_base_t *inst, uint8_t slv_id, bool is_running, u
|
||||
|
||||
#if (CONFIG_FMB_COMM_MODE_TCP_EN)
|
||||
|
||||
typedef struct _port_tcp_opts mb_tcp_opts_t;
|
||||
typedef struct port_tcp_opts_s mb_tcp_opts_t;
|
||||
|
||||
mb_err_enum_t mbm_tcp_create(mb_tcp_opts_t *tcp_opts, void **in_out_obj);
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ error:
|
||||
|
||||
#if (MB_MASTER_ASCII_ENABLED || MB_MASTER_RTU_ENABLED)
|
||||
|
||||
typedef struct _port_serial_opts mb_serial_opts_t;
|
||||
typedef struct port_serial_opts_s mb_serial_opts_t;
|
||||
|
||||
mb_err_enum_t mb_stub_serial_create(mb_serial_opts_t *ser_opts, void **in_out_obj)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user