diff --git a/tests/unit-test-client.c b/tests/unit-test-client.c index cfb2c18..911605c 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -146,8 +146,7 @@ int main(int argc, char *argv[]) uint8_t tab_value[UT_BITS_NB]; modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB); - rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, - UT_BITS_NB, tab_value); + rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_value); printf("1/2 modbus_write_bits: "); ASSERT_TRUE(rc == UT_BITS_NB, ""); } @@ -325,7 +324,7 @@ int main(int argc, char *argv[]) ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, - UT_REGISTERS_NB + 1, tab_rp_registers); + UT_REGISTERS_NB_MAX + 1, tab_rp_registers); printf("* modbus_read_registers: "); ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); @@ -344,12 +343,12 @@ int main(int argc, char *argv[]) printf("* modbus_write_coils: "); ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); - rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, + rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, tab_rp_registers[0]); printf("* modbus_write_register: "); ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); - rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, + rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, UT_REGISTERS_NB, tab_rp_registers); printf("* modbus_write_registers: "); ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); @@ -639,8 +638,10 @@ int test_server(modbus_t *ctx, int use_backend) uint8_t read_raw_req[] = { /* slave */ (use_backend == RTU) ? SERVER_ID : 0xFF, - /* function, addr 1, 5 values */ - MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05 + /* function, address, 5 values */ + MODBUS_FC_READ_HOLDING_REGISTERS, + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, + 0x0, 0x05 }; /* Write and read registers request */ const int RW_RAW_REQ_LEN = 13; @@ -650,7 +651,7 @@ int test_server(modbus_t *ctx, int use_backend) /* function, addr to read, nb to read */ MODBUS_FC_WRITE_AND_READ_REGISTERS, /* Read */ - 0, 0, + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8, (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF, /* Write */ @@ -668,8 +669,7 @@ int test_server(modbus_t *ctx, int use_backend) /* function will be set in the loop */ MODBUS_FC_WRITE_MULTIPLE_REGISTERS, /* Address */ - UT_REGISTERS_ADDRESS >> 8, - UT_REGISTERS_ADDRESS & 0xFF, + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, /* 3 values, 6 bytes */ 0x00, 0x03, 0x06, /* Dummy data to write */ @@ -711,8 +711,7 @@ int test_server(modbus_t *ctx, int use_backend) ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc); /* Try to read more values than a response could hold for all data - * types. - */ + types. */ for (i=0; i<4; i++) { rc = send_crafted_request(ctx, tab_read_function[i], read_raw_req, READ_RAW_REQ_LEN, diff --git a/tests/unit-test-server.c b/tests/unit-test-server.c index ebcfdb9..fe2fc76 100644 --- a/tests/unit-test-server.c +++ b/tests/unit-test-server.c @@ -71,11 +71,11 @@ int main(int argc, char*argv[]) modbus_set_debug(ctx, TRUE); - mb_mapping = modbus_mapping_new( - UT_BITS_ADDRESS + UT_BITS_NB, - UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB, - UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, - UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB); + mb_mapping = modbus_mapping_new_start_address( + UT_BITS_ADDRESS, UT_BITS_NB, + UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, + UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX, + UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB); if (mb_mapping == NULL) { fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno)); @@ -83,44 +83,16 @@ int main(int argc, char*argv[]) return -1; } - /* Unit tests of modbus_mapping_new (tests would not be sufficient if two - nb_* were identical) */ - if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) { - printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits); - modbus_free(ctx); - return -1; - } - - if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) { - printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB); - modbus_free(ctx); - return -1; - } - - if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) { - printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB); - modbus_free(ctx); - return -1; - } - - if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) { - printf("Invalid nb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB); - modbus_free(ctx); - return -1; - } - /* Examples from PI_MODBUS_300.pdf. Only the read-only input values are assigned. */ - /** INPUT STATUS **/ - modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, - UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, + /* Initialize input values that's can be only done server side. */ + modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB); - /** INPUT REGISTERS **/ + /* Initialize values of INPUT REGISTERS */ for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { - mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] = - UT_INPUT_REGISTERS_TAB[i];; + mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];; } if (use_backend == TCP) { diff --git a/tests/unit-test.h.in b/tests/unit-test.h.in index b753ad3..dca826f 100644 --- a/tests/unit-test.h.in +++ b/tests/unit-test.h.in @@ -25,7 +25,6 @@ #define SERVER_ID 17 #define INVALID_SERVER_ID 18 -/* Server allocates address + nb */ const uint16_t UT_BITS_ADDRESS = 0x130; const uint16_t UT_BITS_NB = 0x25; const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B }; @@ -34,18 +33,20 @@ const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4; const uint16_t UT_INPUT_BITS_NB = 0x16; const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 }; -const uint16_t UT_REGISTERS_ADDRESS = 0x16B; +const uint16_t UT_REGISTERS_ADDRESS = 0x160; +const uint16_t UT_REGISTERS_NB = 0x3; +const uint16_t UT_REGISTERS_NB_MAX = 0x20; +const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; + /* Raise a manual exception when this address is used for the first byte */ -const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x6C; +const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x170; /* The response of the server will contains an invalid TID or slave */ -const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x6D; +const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171; /* The server will wait for 1 second before replying to test timeout */ -const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x6E; +const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172; /* The server will wait for 5 ms before sending each byte */ -const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x6F; +const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173; -const uint16_t UT_REGISTERS_NB = 0x3; -const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; /* If the following value is used, a bad response is sent. It's better to test with a lower value than UT_REGISTERS_NB_POINTS to try to raise a segfault. */