Commit 5665306800bbd4b3fa5277b26a93b5409f67d3b8
1 parent
bf060ff2
Fix response timeout modification on connect (closes #80)
Thanks to Perry Kundert for bug report and initial patches.
Showing
2 changed files
with
22 additions
and
8 deletions
src/modbus-tcp.c
| ... | ... | @@ -264,7 +264,7 @@ static int _modbus_tcp_set_ipv4_options(int s) |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, |
| 267 | - struct timeval *tv) | |
| 267 | + const struct timeval *ro_tv) | |
| 268 | 268 | { |
| 269 | 269 | int rc; |
| 270 | 270 | |
| ... | ... | @@ -278,11 +278,12 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, |
| 278 | 278 | fd_set wset; |
| 279 | 279 | int optval; |
| 280 | 280 | socklen_t optlen = sizeof(optval); |
| 281 | + struct timeval tv = *ro_tv; | |
| 281 | 282 | |
| 282 | 283 | /* Wait to be available in writing */ |
| 283 | 284 | FD_ZERO(&wset); |
| 284 | 285 | FD_SET(sockfd, &wset); |
| 285 | - rc = select(sockfd + 1, NULL, &wset, NULL, tv); | |
| 286 | + rc = select(sockfd + 1, NULL, &wset, NULL, &tv); | |
| 286 | 287 | if (rc <= 0) { |
| 287 | 288 | /* Timeout or fail */ |
| 288 | 289 | return -1; | ... | ... |
tests/unit-test-client.c
| ... | ... | @@ -34,10 +34,10 @@ int test_raw_request(modbus_t *, int); |
| 34 | 34 | |
| 35 | 35 | int main(int argc, char *argv[]) |
| 36 | 36 | { |
| 37 | - uint8_t *tab_rp_bits; | |
| 38 | - uint16_t *tab_rp_registers; | |
| 39 | - uint16_t *tab_rp_registers_bad; | |
| 40 | - modbus_t *ctx; | |
| 37 | + uint8_t *tab_rp_bits = NULL; | |
| 38 | + uint16_t *tab_rp_registers = NULL; | |
| 39 | + uint16_t *tab_rp_registers_bad = NULL; | |
| 40 | + modbus_t *ctx = NULL; | |
| 41 | 41 | int i; |
| 42 | 42 | uint8_t value; |
| 43 | 43 | int nb_points; |
| ... | ... | @@ -46,6 +46,8 @@ int main(int argc, char *argv[]) |
| 46 | 46 | uint32_t ireal; |
| 47 | 47 | uint32_t old_response_to_sec; |
| 48 | 48 | uint32_t old_response_to_usec; |
| 49 | + uint32_t new_response_to_sec; | |
| 50 | + uint32_t new_response_to_usec; | |
| 49 | 51 | uint32_t old_byte_to_sec; |
| 50 | 52 | uint32_t old_byte_to_usec; |
| 51 | 53 | int use_backend; |
| ... | ... | @@ -86,11 +88,24 @@ int main(int argc, char *argv[]) |
| 86 | 88 | modbus_set_slave(ctx, SERVER_ID); |
| 87 | 89 | } |
| 88 | 90 | |
| 91 | + modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); | |
| 89 | 92 | if (modbus_connect(ctx) == -1) { |
| 90 | 93 | fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); |
| 91 | 94 | modbus_free(ctx); |
| 92 | 95 | return -1; |
| 93 | 96 | } |
| 97 | + modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec); | |
| 98 | + | |
| 99 | + printf("** UNIT TESTING **\n"); | |
| 100 | + | |
| 101 | + printf("1/1 No response timeout modification on connect: "); | |
| 102 | + if (old_response_to_sec == new_response_to_sec && | |
| 103 | + old_response_to_usec == new_response_to_usec) { | |
| 104 | + printf("OK\n"); | |
| 105 | + } else { | |
| 106 | + printf("FAILED\n"); | |
| 107 | + goto close; | |
| 108 | + } | |
| 94 | 109 | |
| 95 | 110 | /* Allocate and initialize the memory to store the bits */ |
| 96 | 111 | nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB; |
| ... | ... | @@ -103,8 +118,6 @@ int main(int argc, char *argv[]) |
| 103 | 118 | tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); |
| 104 | 119 | memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); |
| 105 | 120 | |
| 106 | - printf("** UNIT TESTING **\n"); | |
| 107 | - | |
| 108 | 121 | printf("\nTEST WRITE/READ:\n"); |
| 109 | 122 | |
| 110 | 123 | /** COIL BITS **/ | ... | ... |