Commit 5665306800bbd4b3fa5277b26a93b5409f67d3b8

Authored by Stéphane Raimbault
1 parent bf060ff2

Fix response timeout modification on connect (closes #80)

Thanks to Perry Kundert for bug report and initial patches.
src/modbus-tcp.c
@@ -264,7 +264,7 @@ static int _modbus_tcp_set_ipv4_options(int s) @@ -264,7 +264,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
264 } 264 }
265 265
266 static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, 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 int rc; 269 int rc;
270 270
@@ -278,11 +278,12 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, @@ -278,11 +278,12 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
278 fd_set wset; 278 fd_set wset;
279 int optval; 279 int optval;
280 socklen_t optlen = sizeof(optval); 280 socklen_t optlen = sizeof(optval);
  281 + struct timeval tv = *ro_tv;
281 282
282 /* Wait to be available in writing */ 283 /* Wait to be available in writing */
283 FD_ZERO(&wset); 284 FD_ZERO(&wset);
284 FD_SET(sockfd, &wset); 285 FD_SET(sockfd, &wset);
285 - rc = select(sockfd + 1, NULL, &wset, NULL, tv); 286 + rc = select(sockfd + 1, NULL, &wset, NULL, &tv);
286 if (rc <= 0) { 287 if (rc <= 0) {
287 /* Timeout or fail */ 288 /* Timeout or fail */
288 return -1; 289 return -1;
tests/unit-test-client.c
@@ -34,10 +34,10 @@ int test_raw_request(modbus_t *, int); @@ -34,10 +34,10 @@ int test_raw_request(modbus_t *, int);
34 34
35 int main(int argc, char *argv[]) 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 int i; 41 int i;
42 uint8_t value; 42 uint8_t value;
43 int nb_points; 43 int nb_points;
@@ -46,6 +46,8 @@ int main(int argc, char *argv[]) @@ -46,6 +46,8 @@ int main(int argc, char *argv[])
46 uint32_t ireal; 46 uint32_t ireal;
47 uint32_t old_response_to_sec; 47 uint32_t old_response_to_sec;
48 uint32_t old_response_to_usec; 48 uint32_t old_response_to_usec;
  49 + uint32_t new_response_to_sec;
  50 + uint32_t new_response_to_usec;
49 uint32_t old_byte_to_sec; 51 uint32_t old_byte_to_sec;
50 uint32_t old_byte_to_usec; 52 uint32_t old_byte_to_usec;
51 int use_backend; 53 int use_backend;
@@ -86,11 +88,24 @@ int main(int argc, char *argv[]) @@ -86,11 +88,24 @@ int main(int argc, char *argv[])
86 modbus_set_slave(ctx, SERVER_ID); 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 if (modbus_connect(ctx) == -1) { 92 if (modbus_connect(ctx) == -1) {
90 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); 93 fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
91 modbus_free(ctx); 94 modbus_free(ctx);
92 return -1; 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 /* Allocate and initialize the memory to store the bits */ 110 /* Allocate and initialize the memory to store the bits */
96 nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB; 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,8 +118,6 @@ int main(int argc, char *argv[])
103 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); 118 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
104 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); 119 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
105 120
106 - printf("** UNIT TESTING **\n");  
107 -  
108 printf("\nTEST WRITE/READ:\n"); 121 printf("\nTEST WRITE/READ:\n");
109 122
110 /** COIL BITS **/ 123 /** COIL BITS **/