From 7fe4a91787cb4889a41b4b40eef847fba755ee5e Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Wed, 4 May 2011 16:51:53 +0200 Subject: [PATCH] API cleanup with modbus_receive and modbus_receive_from --- NEWS | 4 +++- doc/Makefile.am | 2 ++ doc/modbus_receive.txt | 40 ++++++++++++++++++++++++++++++++++++++++ doc/modbus_receive_from.txt | 38 ++++++++++++++++++++++++++++++++++++++ src/modbus.c | 28 ++++++++++++++++++---------- src/modbus.h | 4 +++- tests/bandwidth-server-many-up.c | 2 +- tests/bandwidth-server-one.c | 2 +- tests/random-test-server.c | 2 +- tests/unit-test-server.c | 2 +- 10 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 doc/modbus_receive.txt create mode 100644 doc/modbus_receive_from.txt diff --git a/NEWS b/NEWS index 0a5792a..106dd9b 100644 --- a/NEWS +++ b/NEWS @@ -10,7 +10,9 @@ libmodbus 2.9.4 (2011-05-XX) Raimbault - New functions to send and receive raw requests - Fix flush function of TCP backend on Windows - +- API changes for server/slave: + * modbus_receive has been renamed modbus_receive_from + * modbus_receive has been added to use the socket of the context. libmodbus 2.9.3 (2011-01-14) ============================ diff --git a/doc/Makefile.am b/doc/Makefile.am index 4dd6033..29f3ecd 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -13,6 +13,8 @@ MAN3 = \ modbus_read_input_bits.3 \ 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 \ diff --git a/doc/modbus_receive.txt b/doc/modbus_receive.txt new file mode 100644 index 0000000..882eaf9 --- /dev/null +++ b/doc/modbus_receive.txt @@ -0,0 +1,40 @@ +modbus_receive(3) +================= + + +NAME +---- +modbus_receive - receive a indication request + + +SYNOPSIS +-------- +*int modbus_receive(*modbus_t 'ctx', uint8_t *'req');* + + +DESCRIPTION +----------- +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]. + + +RETURN VALUE +------------ +The _modbus_receive()_ function shall store the indication request in 'req' and +return the request length if sucessful. Otherwise it shall return -1 and set +errno. + + +SEE ALSO +-------- +linkmb:modbus_receive_from[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/doc/modbus_receive_from.txt b/doc/modbus_receive_from.txt new file mode 100644 index 0000000..8d4520a --- /dev/null +++ b/doc/modbus_receive_from.txt @@ -0,0 +1,38 @@ +modbus_receive_from(3) +====================== + + +NAME +---- +modbus_receive_from - receive a indication request from a socket + + +SYNOPSIS +-------- +*int modbus_receive_from(*modbus_t 'ctx', int sockfd, uint8_t *'req');* + + +DESCRIPTION +----------- +The _modbus_receive_from()_ function shall receive an indication request from +the socket/file descriptor given in argument 'sockfd. This function is used by +Modbus slave/server to receive and analyze indication request sent by the +masters/clients. + + +RETURN VALUE +------------ +The _modbus_receive_from()_ function shall store the indication request in 'req' +and return the request length if sucessful. Otherwise it shall return -1 and set +errno. + + +SEE ALSO +-------- +linkmb:modbus_receive[3] + + +AUTHORS +------- +The libmodbus documentation was written by Stéphane Raimbault + diff --git a/src/modbus.c b/src/modbus.c index 0fd7fd6..ab9264f 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -413,19 +413,27 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) return ctx->backend->check_integrity(ctx, msg, msg_length); } -/* Receive the request from a modbus master, requires the socket file descriptor - etablished with the master device in argument or -1 to use the internal one - of modbus_t. +/* Receive the request from a modbus master */ +int modbus_receive(modbus_t *ctx, uint8_t *req) +{ + return receive_msg(ctx, req, MSG_INDICATION); +} - The function shall return the request received and its byte length if - successul. Otherwise, it shall return -1 and errno is set. */ -int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req) +/* Requires a socket file descriptor with a connection etablished in + argument */ +int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req) { - if (sockfd != -1) { - ctx->s = sockfd; - } + int rc; + const int s_old = ctx->s; - return receive_msg(ctx, req, MSG_INDICATION); + ctx->s = sockfd; + + rc = receive_msg(ctx, req, MSG_INDICATION); + + /* Restore orignal socket */ + ctx->s = s_old; + + return rc; } /* Receives the confirmation. diff --git a/src/modbus.h b/src/modbus.h index ad40da0..f4cc579 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -175,7 +175,9 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length); -int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req); +int modbus_receive(modbus_t *ctx, uint8_t *req); +int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req); + int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp); int modbus_reply(modbus_t *ctx, const uint8_t *req, diff --git a/tests/bandwidth-server-many-up.c b/tests/bandwidth-server-many-up.c index d6f5804..b279d0a 100644 --- a/tests/bandwidth-server-many-up.c +++ b/tests/bandwidth-server-many-up.c @@ -118,7 +118,7 @@ int main(void) /* An already connected master has sent a new query */ uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; - rc = modbus_receive(ctx, master_socket, query); + rc = modbus_receive_from(ctx, master_socket, query); if (rc != -1) { modbus_reply(ctx, query, rc, mb_mapping); } else { diff --git a/tests/bandwidth-server-one.c b/tests/bandwidth-server-one.c index 4e37fa8..e15fb69 100644 --- a/tests/bandwidth-server-one.c +++ b/tests/bandwidth-server-one.c @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) for(;;) { uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; - rc = modbus_receive(ctx, -1, query); + rc = modbus_receive(ctx, query); if (rc >= 0) { modbus_reply(ctx, query, rc, mb_mapping); } else { diff --git a/tests/random-test-server.c b/tests/random-test-server.c index 91ad753..9569fe4 100644 --- a/tests/random-test-server.c +++ b/tests/random-test-server.c @@ -46,7 +46,7 @@ int main(void) uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; int rc; - rc = modbus_receive(ctx, -1, query); + rc = modbus_receive(ctx, query); if (rc != -1) { /* rc is the query size */ modbus_reply(ctx, query, rc, mb_mapping); diff --git a/tests/unit-test-server.c b/tests/unit-test-server.c index e25c8d2..5a2d5eb 100644 --- a/tests/unit-test-server.c +++ b/tests/unit-test-server.c @@ -115,7 +115,7 @@ int main(int argc, char*argv[]) } for (;;) { - rc = modbus_receive(ctx, -1, query); + rc = modbus_receive(ctx, query); if (rc == -1) { /* Connection closed by the client or error */ break; -- libgit2 0.21.4