Skip to content

Commit

Permalink
Fix response timeout modification on connect (closes stephane#80)
Browse files Browse the repository at this point in the history
Thanks to Perry Kundert for bug report and initial patches.
  • Loading branch information
stephane authored and mk8 committed Jan 29, 2014
1 parent e47e97d commit 50a7ccc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/modbus-tcp.c
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
25 changes: 19 additions & 6 deletions tests/unit-test-client.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 **/
Expand Down

0 comments on commit 50a7ccc

Please sign in to comment.