Эх сурвалжийг харах

Fix response timeout modification on connect (closes #80)

Thanks to Perry Kundert for bug report and initial patches.
Stéphane Raimbault 11 жил өмнө
parent
commit
5665306800

+ 3 - 2
src/modbus-tcp.c

@@ -264,7 +264,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
 }
 
 static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
-                    struct timeval *tv)
+                    const struct timeval *ro_tv)
 {
     int rc;
 
@@ -278,11 +278,12 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
         fd_set wset;
         int optval;
         socklen_t optlen = sizeof(optval);
+        struct timeval tv = *ro_tv;
 
         /* Wait to be available in writing */
         FD_ZERO(&wset);
         FD_SET(sockfd, &wset);
-        rc = select(sockfd + 1, NULL, &wset, NULL, tv);
+        rc = select(sockfd + 1, NULL, &wset, NULL, &tv);
         if (rc <= 0) {
             /* Timeout or fail */
             return -1;

+ 19 - 6
tests/unit-test-client.c

@@ -34,10 +34,10 @@ int test_raw_request(modbus_t *, int);
 
 int main(int argc, char *argv[])
 {
-    uint8_t *tab_rp_bits;
-    uint16_t *tab_rp_registers;
-    uint16_t *tab_rp_registers_bad;
-    modbus_t *ctx;
+    uint8_t *tab_rp_bits = NULL;
+    uint16_t *tab_rp_registers = NULL;
+    uint16_t *tab_rp_registers_bad = NULL;
+    modbus_t *ctx = NULL;
     int i;
     uint8_t value;
     int nb_points;
@@ -46,6 +46,8 @@ int main(int argc, char *argv[])
     uint32_t ireal;
     uint32_t old_response_to_sec;
     uint32_t old_response_to_usec;
+    uint32_t new_response_to_sec;
+    uint32_t new_response_to_usec;
     uint32_t old_byte_to_sec;
     uint32_t old_byte_to_usec;
     int use_backend;
@@ -86,11 +88,24 @@ int main(int argc, char *argv[])
           modbus_set_slave(ctx, SERVER_ID);
     }
 
+    modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
     if (modbus_connect(ctx) == -1) {
         fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
+    modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
+
+    printf("** UNIT TESTING **\n");
+
+    printf("1/1 No response timeout modification on connect: ");
+    if (old_response_to_sec == new_response_to_sec &&
+        old_response_to_usec == new_response_to_usec) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
 
     /* Allocate and initialize the memory to store the bits */
     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[])
     tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
     memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
 
-    printf("** UNIT TESTING **\n");
-
     printf("\nTEST WRITE/READ:\n");
 
     /** COIL BITS **/