From 1faf5c3aad99c7f7e88deeb26b16d8f46261cfda Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Thu, 5 May 2011 00:03:52 +0200 Subject: [PATCH] New setter/getter for context socket (fixes bandwidth-server-many-up) --- doc/Makefile.am | 3 ++- doc/libmodbus.txt | 3 ++- doc/modbus_get_socket.txt | 34 ++++++++++++++++++++++++++++++++++ doc/modbus_receive.txt | 7 ++++--- doc/modbus_set_socket.txt | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ doc/modbus_write_registers.txt | 4 ++-- src/modbus.c | 27 ++++++++++----------------- src/modbus.h | 3 ++- tests/bandwidth-client.c | 2 +- tests/bandwidth-server-many-up.c | 3 ++- 10 files changed, 115 insertions(+), 27 deletions(-) create mode 100644 doc/modbus_get_socket.txt create mode 100644 doc/modbus_set_socket.txt diff --git a/doc/Makefile.am b/doc/Makefile.am index 29f3ecd..b1ba218 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -4,6 +4,7 @@ MAN3 = \ modbus_flush.3 \ modbus_free.3 \ modbus_get_header_length.3 \ + modbus_get_socket.3 \ modbus_get_timeout_begin.3 \ modbus_get_timeout_end.3 \ modbus_new_rtu.3 \ @@ -14,12 +15,12 @@ MAN3 = \ modbus_read_input_registers.3 \ modbus_read_registers.3 \ modbus_receive.3 \ - modbus_receive_from.3 \ modbus_receive_confirmation.3 \ modbus_send_raw_request.3 \ modbus_set_debug.3 \ modbus_set_error_recovery.3 \ modbus_set_slave.3 \ + modbus_set_socket.3 \ modbus_set_timeout_begin.3 \ modbus_set_timeout_end.3 \ modbus_strerror.3 \ diff --git a/doc/libmodbus.txt b/doc/libmodbus.txt index f06a693..1375f38 100644 --- a/doc/libmodbus.txt +++ b/doc/libmodbus.txt @@ -102,6 +102,8 @@ Context setters and getters:: linkmb:modbus_set_debug[3] linkmb:modbus_set_error_recovery[3] linkmb:modbus_set_slave[3] + linkmb:modbus_set_socket[3] + linkmb:modbus_get_socket[3] linkmb:modbus_get_timeout_begin[3] linkmb:modbus_set_timeout_begin[3] linkmb:modbus_get_timeout_end[3] @@ -159,7 +161,6 @@ receive and reply: Receive:: linkmb:modbus_receive[3] - linkmb:modbus_receive_from[3] Reply:: linkmb:modbus_reply[3] diff --git a/doc/modbus_get_socket.txt b/doc/modbus_get_socket.txt new file mode 100644 index 0000000..b367bb5 --- /dev/null +++ b/doc/modbus_get_socket.txt @@ -0,0 +1,34 @@ +modbus_get_socket(3) +==================== + + +NAME +---- +modbus_get_socket - get the current socket of the context + + +SYNOPSIS +-------- +*int modbus_get_socket(modbus_t *'ctx')* + + +DESCRIPTION +----------- +The _modbus_get_socket()_ function shall return the current socket or file +descriptor of the libmodbus context. + + +RETURN VALUE +------------ +The current socket or file descriptor of the context. + + +SEE ALSO +-------- +linkmb:modbus_set_socket[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/doc/modbus_receive.txt b/doc/modbus_receive.txt index 882eaf9..8ee6591 100644 --- a/doc/modbus_receive.txt +++ b/doc/modbus_receive.txt @@ -18,8 +18,8 @@ The _modbus_receive()_ function shall receive an indication request from the socket of the context 'ctx'. This function is used by Modbus slave/server to receive and analyze indication request sent by the masters/clients. -If you need to use another socket than the one defined in the context 'ctx', see -the function linkmb:modbus_receive_from[3]. +If you need to use another socket or file descriptor than the one defined in the +context 'ctx', see the function linkmb:modbus_set_socket[3]. RETURN VALUE @@ -31,7 +31,8 @@ errno. SEE ALSO -------- -linkmb:modbus_receive_from[3] +linkmb:modbus_set_socket[3] +linkmb:modbus_reply[3] AUTHORS diff --git a/doc/modbus_set_socket.txt b/doc/modbus_set_socket.txt new file mode 100644 index 0000000..d68bcb3 --- /dev/null +++ b/doc/modbus_set_socket.txt @@ -0,0 +1,56 @@ +modbus_set_socket(3) +==================== + + +NAME +---- +modbus_set_socket - set socket of the context + + +SYNOPSIS +-------- +*void modbus_set_socket(modbus_t *'ctx', int 'socket')* + + +DESCRIPTION +----------- +The _modbus_set_socket()_ function shall set the socket or file descriptor in +the libmodbus context. This function is useful for managing multiple client +connections to the same server. + + +RETURN VALUE +------------ +There is no return values. + + +EXAMPLE +------- +[source,c] +------------------- +ctx = modbus_new_tcp("127.0.0.1", 1502); +server_socket = modbus_tcp_listen(ctx, NB_CONNECTION); + +FD_ZERO(&rdset); +FD_SET(server_socket, &rdset); + +/* .... */ + +if (FD_ISSET(master_socket, &rdset)) { + modbus_set_socket(ctx, master_socket); + rc = modbus_receive(ctx, query); + if (rc != -1) { + modbus_reply(ctx, query, rc, mb_mapping); + } +} +------------------- + +SEE ALSO +-------- +linkmb:modbus_get_socket[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/doc/modbus_write_registers.txt b/doc/modbus_write_registers.txt index 1116256..b292db1 100644 --- a/doc/modbus_write_registers.txt +++ b/doc/modbus_write_registers.txt @@ -23,8 +23,8 @@ The function uses the Modbus function code 0x10 (preset multiple registers). RETURN VALUE ------------ -The _modbus_write_registers()_ function shall return the number of written registers -if successful. Otherwise it shall return -1 and set errno. +The _modbus_write_registers()_ function shall return the number of written +registers if successful. Otherwise it shall return -1 and set errno. SEE ALSO diff --git a/src/modbus.c b/src/modbus.c index a24e0de..0e496e2 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -417,23 +417,6 @@ int modbus_receive(modbus_t *ctx, uint8_t *req) return receive_msg(ctx, req, MSG_INDICATION); } -/* Requires a socket file descriptor with a connection etablished in - argument */ -int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req) -{ - int rc; - const int s_old = ctx->s; - - ctx->s = sockfd; - - rc = receive_msg(ctx, req, MSG_INDICATION); - - /* Restore orignal socket */ - ctx->s = s_old; - - return rc; -} - /* Receives the confirmation. The function shall store the read response in rsp and return the number of @@ -1353,6 +1336,16 @@ int modbus_set_error_recovery(modbus_t *ctx, int enabled) return 0; } +void modbus_set_socket(modbus_t *ctx, int socket) +{ + ctx->s = socket; +} + +int modbus_get_socket(modbus_t *ctx) +{ + return ctx->s; +} + /* Get the timeout of begin of message */ void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout) { diff --git a/src/modbus.h b/src/modbus.h index f4cc579..5691b9a 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -135,8 +135,9 @@ typedef struct { } modbus_mapping_t; int modbus_set_slave(modbus_t* ctx, int slave); - int modbus_set_error_recovery(modbus_t *ctx, int enabled); +void modbus_set_socket(modbus_t *ctx, int socket); +int modbus_get_socket(modbus_t *ctx); void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout); void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout); diff --git a/tests/bandwidth-client.c b/tests/bandwidth-client.c index 266f424..ef478aa 100644 --- a/tests/bandwidth-client.c +++ b/tests/bandwidth-client.c @@ -167,7 +167,7 @@ int main(int argc, char *argv[]) rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); printf("* %.3f ms for %d bytes\n", elapsed, bytes); printf("* %d KiB/s\n", rate); - printf("\n"); + printf("\n\n"); printf("READ AND WRITE REGISTERS\n\n"); diff --git a/tests/bandwidth-server-many-up.c b/tests/bandwidth-server-many-up.c index b279d0a..02968c1 100644 --- a/tests/bandwidth-server-many-up.c +++ b/tests/bandwidth-server-many-up.c @@ -118,7 +118,8 @@ int main(void) /* An already connected master has sent a new query */ uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; - rc = modbus_receive_from(ctx, master_socket, query); + modbus_set_socket(ctx, master_socket); + rc = modbus_receive(ctx, query); if (rc != -1) { modbus_reply(ctx, query, rc, mb_mapping); } else { -- libgit2 0.21.4