From d9d66989d7fe96c3b8afa86291e284a5da22cd0f Mon Sep 17 00:00:00 2001 From: Valerio De Benedetto Date: Sat, 22 Jan 2022 12:18:32 +0100 Subject: [PATCH] Byte spacing tests --- modbusino.h | 2 ++ tests/modbusino_tests.c | 22 +++++++++++++++++++++- tests/modbusino_tests.h | 27 +++++++++++++-------------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/modbusino.h b/modbusino.h index b010526..8727766 100644 --- a/modbusino.h +++ b/modbusino.h @@ -93,6 +93,8 @@ void mbsn_set_read_timeout(mbsn_t* mbsn, int32_t timeout_ms); void mbsn_set_byte_timeout(mbsn_t* mbsn, int32_t timeout_ms); +void mbsn_set_byte_spacing(mbsn_t* mbsn, uint32_t spacing_ms); + void mbsn_set_destination_rtu_address(mbsn_t* mbsn, uint8_t address); mbsn_error mbsn_server_receive(mbsn_t* mbsn); diff --git a/tests/modbusino_tests.c b/tests/modbusino_tests.c index 06488d4..786f71f 100644 --- a/tests/modbusino_tests.c +++ b/tests/modbusino_tests.c @@ -86,7 +86,7 @@ int read_byte_timeout_third(uint8_t* b, int32_t timeout) { void test_server_receive_base(mbsn_transport transport) { - mbsn_t server; + mbsn_t server, client; mbsn_error err; mbsn_platform_conf platform_conf; @@ -134,6 +134,26 @@ void test_server_receive_base(mbsn_transport transport) { err = mbsn_server_receive(&server); expect(err == MBSN_ERROR_TIMEOUT); + + + should("honor byte spacing on RTU"); + if (transport == MBSN_TRANSPORT_RTU) { + reset(client); + platform_conf.transport = transport; + platform_conf.read_byte = read_byte_socket_client; + platform_conf.write_byte = write_byte_socket_client; + + reset_sockets(); + + check(mbsn_client_create(&client, &platform_conf)); + + mbsn_set_byte_spacing(&client, 200); + + uint64_t start = now_ms(); + check(mbsn_send_raw_pdu(&client, 1, (uint16_t[]){htons(1), htons(1)}, 4)); + uint64_t diff = now_ms() - start; + assert(diff >= 200 * 8); + } } diff --git a/tests/modbusino_tests.h b/tests/modbusino_tests.h index 2aaf5d5..d6812e1 100644 --- a/tests/modbusino_tests.h +++ b/tests/modbusino_tests.h @@ -124,41 +124,41 @@ int write_byte_fd(int fd, uint8_t b, int32_t timeout_ms) { } -int read_byte_pipe_server(uint8_t* b, int32_t timeout_ms) { +int read_byte_socket_server(uint8_t* b, int32_t timeout_ms) { return read_byte_fd(sockets[0], b, timeout_ms); } -int write_byte_pipe_server(uint8_t b, int32_t timeout_ms) { +int write_byte_socket_server(uint8_t b, int32_t timeout_ms) { return write_byte_fd(sockets[0], b, timeout_ms); } -int read_byte_pipe_client(uint8_t* b, int32_t timeout_ms) { +int read_byte_socket_client(uint8_t* b, int32_t timeout_ms) { return read_byte_fd(sockets[1], b, timeout_ms); } -int write_byte_pipe_client(uint8_t b, int32_t timeout_ms) { +int write_byte_socket_client(uint8_t b, int32_t timeout_ms) { return write_byte_fd(sockets[1], b, timeout_ms); } mbsn_platform_conf mbsn_platform_conf_server; -mbsn_platform_conf* platform_conf_pipe_server(mbsn_transport transport) { +mbsn_platform_conf* platform_conf_socket_server(mbsn_transport transport) { mbsn_platform_conf_server.transport = transport; - mbsn_platform_conf_server.read_byte = read_byte_pipe_server; - mbsn_platform_conf_server.write_byte = write_byte_pipe_server; + mbsn_platform_conf_server.read_byte = read_byte_socket_server; + mbsn_platform_conf_server.write_byte = write_byte_socket_server; mbsn_platform_conf_server.sleep = platform_sleep; return &mbsn_platform_conf_server; } mbsn_platform_conf mbsn_platform_conf_client; -mbsn_platform_conf* platform_conf_pipe_client(mbsn_transport transport) { +mbsn_platform_conf* platform_conf_socket_client(mbsn_transport transport) { mbsn_platform_conf_client.transport = transport; - mbsn_platform_conf_client.read_byte = read_byte_pipe_client; - mbsn_platform_conf_client.write_byte = write_byte_pipe_client; + mbsn_platform_conf_client.read_byte = read_byte_socket_client; + mbsn_platform_conf_client.write_byte = write_byte_socket_client; mbsn_platform_conf_client.sleep = platform_sleep; return &mbsn_platform_conf_client; } @@ -178,8 +178,7 @@ void* server_listen_thread() { if (is_server_listen_thread_stopped()) break; - mbsn_error err = mbsn_server_receive(&SERVER); - check(err); + check(mbsn_server_receive(&SERVER)); } return NULL; @@ -205,8 +204,8 @@ void start_client_and_server(mbsn_transport transport, mbsn_callbacks server_cal reset(SERVER); reset(CLIENT); - check(mbsn_server_create(&SERVER, TEST_SERVER_ADDR, platform_conf_pipe_server(transport), server_callbacks)); - check(mbsn_client_create(&CLIENT, platform_conf_pipe_client(transport))); + check(mbsn_server_create(&SERVER, TEST_SERVER_ADDR, platform_conf_socket_server(transport), server_callbacks)); + check(mbsn_client_create(&CLIENT, platform_conf_socket_client(transport))); mbsn_set_destination_rtu_address(&CLIENT, TEST_SERVER_ADDR); mbsn_set_read_timeout(&SERVER, 500);