Commit 7fe4a91787cb4889a41b4b40eef847fba755ee5e
1 parent
eeb6cd81
API cleanup with modbus_receive and modbus_receive_from
Split the original modbus_receive function in two functions to avoid the strange -1 value to ignore the sockfd argument.
Showing
10 changed files
with
108 additions
and
16 deletions
NEWS
| ... | ... | @@ -10,7 +10,9 @@ libmodbus 2.9.4 (2011-05-XX) |
| 10 | 10 | Raimbault |
| 11 | 11 | - New functions to send and receive raw requests |
| 12 | 12 | - Fix flush function of TCP backend on Windows |
| 13 | - | |
| 13 | +- API changes for server/slave: | |
| 14 | + * modbus_receive has been renamed modbus_receive_from | |
| 15 | + * modbus_receive has been added to use the socket of the context. | |
| 14 | 16 | |
| 15 | 17 | libmodbus 2.9.3 (2011-01-14) |
| 16 | 18 | ============================ | ... | ... |
doc/Makefile.am
doc/modbus_receive.txt
0 → 100644
| 1 | +modbus_receive(3) | |
| 2 | +================= | |
| 3 | + | |
| 4 | + | |
| 5 | +NAME | |
| 6 | +---- | |
| 7 | +modbus_receive - receive a indication request | |
| 8 | + | |
| 9 | + | |
| 10 | +SYNOPSIS | |
| 11 | +-------- | |
| 12 | +*int modbus_receive(*modbus_t 'ctx', uint8_t *'req');* | |
| 13 | + | |
| 14 | + | |
| 15 | +DESCRIPTION | |
| 16 | +----------- | |
| 17 | +The _modbus_receive()_ function shall receive an indication request from the | |
| 18 | +socket of the context 'ctx'. This function is used by Modbus slave/server to | |
| 19 | +receive and analyze indication request sent by the masters/clients. | |
| 20 | + | |
| 21 | +If you need to use another socket than the one defined in the context 'ctx', see | |
| 22 | +the function linkmb:modbus_receive_from[3]. | |
| 23 | + | |
| 24 | + | |
| 25 | +RETURN VALUE | |
| 26 | +------------ | |
| 27 | +The _modbus_receive()_ function shall store the indication request in 'req' and | |
| 28 | +return the request length if sucessful. Otherwise it shall return -1 and set | |
| 29 | +errno. | |
| 30 | + | |
| 31 | + | |
| 32 | +SEE ALSO | |
| 33 | +-------- | |
| 34 | +linkmb:modbus_receive_from[3] | |
| 35 | + | |
| 36 | + | |
| 37 | +AUTHORS | |
| 38 | +------- | |
| 39 | +The libmodbus documentation was written by Stéphane Raimbault | |
| 40 | +<stephane.raimbault@gmail.com> | ... | ... |
doc/modbus_receive_from.txt
0 → 100644
| 1 | +modbus_receive_from(3) | |
| 2 | +====================== | |
| 3 | + | |
| 4 | + | |
| 5 | +NAME | |
| 6 | +---- | |
| 7 | +modbus_receive_from - receive a indication request from a socket | |
| 8 | + | |
| 9 | + | |
| 10 | +SYNOPSIS | |
| 11 | +-------- | |
| 12 | +*int modbus_receive_from(*modbus_t 'ctx', int sockfd, uint8_t *'req');* | |
| 13 | + | |
| 14 | + | |
| 15 | +DESCRIPTION | |
| 16 | +----------- | |
| 17 | +The _modbus_receive_from()_ function shall receive an indication request from | |
| 18 | +the socket/file descriptor given in argument 'sockfd. This function is used by | |
| 19 | +Modbus slave/server to receive and analyze indication request sent by the | |
| 20 | +masters/clients. | |
| 21 | + | |
| 22 | + | |
| 23 | +RETURN VALUE | |
| 24 | +------------ | |
| 25 | +The _modbus_receive_from()_ function shall store the indication request in 'req' | |
| 26 | +and return the request length if sucessful. Otherwise it shall return -1 and set | |
| 27 | +errno. | |
| 28 | + | |
| 29 | + | |
| 30 | +SEE ALSO | |
| 31 | +-------- | |
| 32 | +linkmb:modbus_receive[3] | |
| 33 | + | |
| 34 | + | |
| 35 | +AUTHORS | |
| 36 | +------- | |
| 37 | +The libmodbus documentation was written by Stéphane Raimbault | |
| 38 | +<stephane.raimbault@gmail.com> | ... | ... |
src/modbus.c
| ... | ... | @@ -413,19 +413,27 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) |
| 413 | 413 | return ctx->backend->check_integrity(ctx, msg, msg_length); |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | -/* Receive the request from a modbus master, requires the socket file descriptor | |
| 417 | - etablished with the master device in argument or -1 to use the internal one | |
| 418 | - of modbus_t. | |
| 416 | +/* Receive the request from a modbus master */ | |
| 417 | +int modbus_receive(modbus_t *ctx, uint8_t *req) | |
| 418 | +{ | |
| 419 | + return receive_msg(ctx, req, MSG_INDICATION); | |
| 420 | +} | |
| 419 | 421 | |
| 420 | - The function shall return the request received and its byte length if | |
| 421 | - successul. Otherwise, it shall return -1 and errno is set. */ | |
| 422 | -int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req) | |
| 422 | +/* Requires a socket file descriptor with a connection etablished in | |
| 423 | + argument */ | |
| 424 | +int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req) | |
| 423 | 425 | { |
| 424 | - if (sockfd != -1) { | |
| 425 | - ctx->s = sockfd; | |
| 426 | - } | |
| 426 | + int rc; | |
| 427 | + const int s_old = ctx->s; | |
| 427 | 428 | |
| 428 | - return receive_msg(ctx, req, MSG_INDICATION); | |
| 429 | + ctx->s = sockfd; | |
| 430 | + | |
| 431 | + rc = receive_msg(ctx, req, MSG_INDICATION); | |
| 432 | + | |
| 433 | + /* Restore orignal socket */ | |
| 434 | + ctx->s = s_old; | |
| 435 | + | |
| 436 | + return rc; | |
| 429 | 437 | } |
| 430 | 438 | |
| 431 | 439 | /* Receives the confirmation. | ... | ... |
src/modbus.h
| ... | ... | @@ -175,7 +175,9 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); |
| 175 | 175 | |
| 176 | 176 | int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length); |
| 177 | 177 | |
| 178 | -int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req); | |
| 178 | +int modbus_receive(modbus_t *ctx, uint8_t *req); | |
| 179 | +int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req); | |
| 180 | + | |
| 179 | 181 | int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp); |
| 180 | 182 | |
| 181 | 183 | int modbus_reply(modbus_t *ctx, const uint8_t *req, | ... | ... |
tests/bandwidth-server-many-up.c
| ... | ... | @@ -118,7 +118,7 @@ int main(void) |
| 118 | 118 | /* An already connected master has sent a new query */ |
| 119 | 119 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 120 | 120 | |
| 121 | - rc = modbus_receive(ctx, master_socket, query); | |
| 121 | + rc = modbus_receive_from(ctx, master_socket, query); | |
| 122 | 122 | if (rc != -1) { |
| 123 | 123 | modbus_reply(ctx, query, rc, mb_mapping); |
| 124 | 124 | } else { | ... | ... |
tests/bandwidth-server-one.c
| ... | ... | @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) |
| 74 | 74 | for(;;) { |
| 75 | 75 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 76 | 76 | |
| 77 | - rc = modbus_receive(ctx, -1, query); | |
| 77 | + rc = modbus_receive(ctx, query); | |
| 78 | 78 | if (rc >= 0) { |
| 79 | 79 | modbus_reply(ctx, query, rc, mb_mapping); |
| 80 | 80 | } else { | ... | ... |
tests/random-test-server.c
tests/unit-test-server.c