Commit 79c1bd3dace8fb7bd44d98a08f0ebd6341446f00
1 parent
933ed4d3
Use new modbus_mapping_new_start_address in unit tests
Showing
3 changed files
with
29 additions
and
57 deletions
tests/unit-test-client.c
| @@ -146,8 +146,7 @@ int main(int argc, char *argv[]) | @@ -146,8 +146,7 @@ int main(int argc, char *argv[]) | ||
| 146 | uint8_t tab_value[UT_BITS_NB]; | 146 | uint8_t tab_value[UT_BITS_NB]; |
| 147 | 147 | ||
| 148 | modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB); | 148 | modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB); |
| 149 | - rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, | ||
| 150 | - UT_BITS_NB, tab_value); | 149 | + rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_value); |
| 151 | printf("1/2 modbus_write_bits: "); | 150 | printf("1/2 modbus_write_bits: "); |
| 152 | ASSERT_TRUE(rc == UT_BITS_NB, ""); | 151 | ASSERT_TRUE(rc == UT_BITS_NB, ""); |
| 153 | } | 152 | } |
| @@ -325,7 +324,7 @@ int main(int argc, char *argv[]) | @@ -325,7 +324,7 @@ int main(int argc, char *argv[]) | ||
| 325 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); | 324 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 326 | 325 | ||
| 327 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 326 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 328 | - UT_REGISTERS_NB + 1, tab_rp_registers); | 327 | + UT_REGISTERS_NB_MAX + 1, tab_rp_registers); |
| 329 | printf("* modbus_read_registers: "); | 328 | printf("* modbus_read_registers: "); |
| 330 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); | 329 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 331 | 330 | ||
| @@ -344,12 +343,12 @@ int main(int argc, char *argv[]) | @@ -344,12 +343,12 @@ int main(int argc, char *argv[]) | ||
| 344 | printf("* modbus_write_coils: "); | 343 | printf("* modbus_write_coils: "); |
| 345 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); | 344 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 346 | 345 | ||
| 347 | - rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, | 346 | + rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, |
| 348 | tab_rp_registers[0]); | 347 | tab_rp_registers[0]); |
| 349 | printf("* modbus_write_register: "); | 348 | printf("* modbus_write_register: "); |
| 350 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); | 349 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 351 | 350 | ||
| 352 | - rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, | 351 | + rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, |
| 353 | UT_REGISTERS_NB, tab_rp_registers); | 352 | UT_REGISTERS_NB, tab_rp_registers); |
| 354 | printf("* modbus_write_registers: "); | 353 | printf("* modbus_write_registers: "); |
| 355 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); | 354 | ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| @@ -639,8 +638,10 @@ int test_server(modbus_t *ctx, int use_backend) | @@ -639,8 +638,10 @@ int test_server(modbus_t *ctx, int use_backend) | ||
| 639 | uint8_t read_raw_req[] = { | 638 | uint8_t read_raw_req[] = { |
| 640 | /* slave */ | 639 | /* slave */ |
| 641 | (use_backend == RTU) ? SERVER_ID : 0xFF, | 640 | (use_backend == RTU) ? SERVER_ID : 0xFF, |
| 642 | - /* function, addr 1, 5 values */ | ||
| 643 | - MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05 | 641 | + /* function, address, 5 values */ |
| 642 | + MODBUS_FC_READ_HOLDING_REGISTERS, | ||
| 643 | + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, | ||
| 644 | + 0x0, 0x05 | ||
| 644 | }; | 645 | }; |
| 645 | /* Write and read registers request */ | 646 | /* Write and read registers request */ |
| 646 | const int RW_RAW_REQ_LEN = 13; | 647 | const int RW_RAW_REQ_LEN = 13; |
| @@ -650,7 +651,7 @@ int test_server(modbus_t *ctx, int use_backend) | @@ -650,7 +651,7 @@ int test_server(modbus_t *ctx, int use_backend) | ||
| 650 | /* function, addr to read, nb to read */ | 651 | /* function, addr to read, nb to read */ |
| 651 | MODBUS_FC_WRITE_AND_READ_REGISTERS, | 652 | MODBUS_FC_WRITE_AND_READ_REGISTERS, |
| 652 | /* Read */ | 653 | /* Read */ |
| 653 | - 0, 0, | 654 | + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, |
| 654 | (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8, | 655 | (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8, |
| 655 | (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF, | 656 | (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF, |
| 656 | /* Write */ | 657 | /* Write */ |
| @@ -668,8 +669,7 @@ int test_server(modbus_t *ctx, int use_backend) | @@ -668,8 +669,7 @@ int test_server(modbus_t *ctx, int use_backend) | ||
| 668 | /* function will be set in the loop */ | 669 | /* function will be set in the loop */ |
| 669 | MODBUS_FC_WRITE_MULTIPLE_REGISTERS, | 670 | MODBUS_FC_WRITE_MULTIPLE_REGISTERS, |
| 670 | /* Address */ | 671 | /* Address */ |
| 671 | - UT_REGISTERS_ADDRESS >> 8, | ||
| 672 | - UT_REGISTERS_ADDRESS & 0xFF, | 672 | + UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF, |
| 673 | /* 3 values, 6 bytes */ | 673 | /* 3 values, 6 bytes */ |
| 674 | 0x00, 0x03, 0x06, | 674 | 0x00, 0x03, 0x06, |
| 675 | /* Dummy data to write */ | 675 | /* Dummy data to write */ |
| @@ -711,8 +711,7 @@ int test_server(modbus_t *ctx, int use_backend) | @@ -711,8 +711,7 @@ int test_server(modbus_t *ctx, int use_backend) | ||
| 711 | ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc); | 711 | ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc); |
| 712 | 712 | ||
| 713 | /* Try to read more values than a response could hold for all data | 713 | /* Try to read more values than a response could hold for all data |
| 714 | - * types. | ||
| 715 | - */ | 714 | + types. */ |
| 716 | for (i=0; i<4; i++) { | 715 | for (i=0; i<4; i++) { |
| 717 | rc = send_crafted_request(ctx, tab_read_function[i], | 716 | rc = send_crafted_request(ctx, tab_read_function[i], |
| 718 | read_raw_req, READ_RAW_REQ_LEN, | 717 | read_raw_req, READ_RAW_REQ_LEN, |
tests/unit-test-server.c
| @@ -71,11 +71,11 @@ int main(int argc, char*argv[]) | @@ -71,11 +71,11 @@ int main(int argc, char*argv[]) | ||
| 71 | 71 | ||
| 72 | modbus_set_debug(ctx, TRUE); | 72 | modbus_set_debug(ctx, TRUE); |
| 73 | 73 | ||
| 74 | - mb_mapping = modbus_mapping_new( | ||
| 75 | - UT_BITS_ADDRESS + UT_BITS_NB, | ||
| 76 | - UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB, | ||
| 77 | - UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, | ||
| 78 | - UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB); | 74 | + mb_mapping = modbus_mapping_new_start_address( |
| 75 | + UT_BITS_ADDRESS, UT_BITS_NB, | ||
| 76 | + UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, | ||
| 77 | + UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX, | ||
| 78 | + UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB); | ||
| 79 | if (mb_mapping == NULL) { | 79 | if (mb_mapping == NULL) { |
| 80 | fprintf(stderr, "Failed to allocate the mapping: %s\n", | 80 | fprintf(stderr, "Failed to allocate the mapping: %s\n", |
| 81 | modbus_strerror(errno)); | 81 | modbus_strerror(errno)); |
| @@ -83,44 +83,16 @@ int main(int argc, char*argv[]) | @@ -83,44 +83,16 @@ int main(int argc, char*argv[]) | ||
| 83 | return -1; | 83 | return -1; |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | - /* Unit tests of modbus_mapping_new (tests would not be sufficient if two | ||
| 87 | - nb_* were identical) */ | ||
| 88 | - if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) { | ||
| 89 | - printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits); | ||
| 90 | - modbus_free(ctx); | ||
| 91 | - return -1; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) { | ||
| 95 | - printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB); | ||
| 96 | - modbus_free(ctx); | ||
| 97 | - return -1; | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) { | ||
| 101 | - printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB); | ||
| 102 | - modbus_free(ctx); | ||
| 103 | - return -1; | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) { | ||
| 107 | - printf("Invalid nb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB); | ||
| 108 | - modbus_free(ctx); | ||
| 109 | - return -1; | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | /* Examples from PI_MODBUS_300.pdf. | 86 | /* Examples from PI_MODBUS_300.pdf. |
| 113 | Only the read-only input values are assigned. */ | 87 | Only the read-only input values are assigned. */ |
| 114 | 88 | ||
| 115 | - /** INPUT STATUS **/ | ||
| 116 | - modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, | ||
| 117 | - UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, | 89 | + /* Initialize input values that's can be only done server side. */ |
| 90 | + modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, | ||
| 118 | UT_INPUT_BITS_TAB); | 91 | UT_INPUT_BITS_TAB); |
| 119 | 92 | ||
| 120 | - /** INPUT REGISTERS **/ | 93 | + /* Initialize values of INPUT REGISTERS */ |
| 121 | for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { | 94 | for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { |
| 122 | - mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] = | ||
| 123 | - UT_INPUT_REGISTERS_TAB[i];; | 95 | + mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];; |
| 124 | } | 96 | } |
| 125 | 97 | ||
| 126 | if (use_backend == TCP) { | 98 | if (use_backend == TCP) { |
tests/unit-test.h.in
| @@ -25,7 +25,6 @@ | @@ -25,7 +25,6 @@ | ||
| 25 | #define SERVER_ID 17 | 25 | #define SERVER_ID 17 |
| 26 | #define INVALID_SERVER_ID 18 | 26 | #define INVALID_SERVER_ID 18 |
| 27 | 27 | ||
| 28 | -/* Server allocates address + nb */ | ||
| 29 | const uint16_t UT_BITS_ADDRESS = 0x130; | 28 | const uint16_t UT_BITS_ADDRESS = 0x130; |
| 30 | const uint16_t UT_BITS_NB = 0x25; | 29 | const uint16_t UT_BITS_NB = 0x25; |
| 31 | const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B }; | 30 | const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B }; |
| @@ -34,18 +33,20 @@ const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4; | @@ -34,18 +33,20 @@ const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4; | ||
| 34 | const uint16_t UT_INPUT_BITS_NB = 0x16; | 33 | const uint16_t UT_INPUT_BITS_NB = 0x16; |
| 35 | const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 }; | 34 | const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 }; |
| 36 | 35 | ||
| 37 | -const uint16_t UT_REGISTERS_ADDRESS = 0x16B; | 36 | +const uint16_t UT_REGISTERS_ADDRESS = 0x160; |
| 37 | +const uint16_t UT_REGISTERS_NB = 0x3; | ||
| 38 | +const uint16_t UT_REGISTERS_NB_MAX = 0x20; | ||
| 39 | +const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; | ||
| 40 | + | ||
| 38 | /* Raise a manual exception when this address is used for the first byte */ | 41 | /* Raise a manual exception when this address is used for the first byte */ |
| 39 | -const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x6C; | 42 | +const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x170; |
| 40 | /* The response of the server will contains an invalid TID or slave */ | 43 | /* The response of the server will contains an invalid TID or slave */ |
| 41 | -const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x6D; | 44 | +const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171; |
| 42 | /* The server will wait for 1 second before replying to test timeout */ | 45 | /* The server will wait for 1 second before replying to test timeout */ |
| 43 | -const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x6E; | 46 | +const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172; |
| 44 | /* The server will wait for 5 ms before sending each byte */ | 47 | /* The server will wait for 5 ms before sending each byte */ |
| 45 | -const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x6F; | 48 | +const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173; |
| 46 | 49 | ||
| 47 | -const uint16_t UT_REGISTERS_NB = 0x3; | ||
| 48 | -const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; | ||
| 49 | /* If the following value is used, a bad response is sent. | 50 | /* If the following value is used, a bad response is sent. |
| 50 | It's better to test with a lower value than | 51 | It's better to test with a lower value than |
| 51 | UT_REGISTERS_NB_POINTS to try to raise a segfault. */ | 52 | UT_REGISTERS_NB_POINTS to try to raise a segfault. */ |