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,7 +10,9 @@ libmodbus 2.9.4 (2011-05-XX) | ||
| 10 | Raimbault | 10 | Raimbault |
| 11 | - New functions to send and receive raw requests | 11 | - New functions to send and receive raw requests |
| 12 | - Fix flush function of TCP backend on Windows | 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 | libmodbus 2.9.3 (2011-01-14) | 17 | libmodbus 2.9.3 (2011-01-14) |
| 16 | ============================ | 18 | ============================ |
doc/Makefile.am
| @@ -13,6 +13,8 @@ MAN3 = \ | @@ -13,6 +13,8 @@ MAN3 = \ | ||
| 13 | modbus_read_input_bits.3 \ | 13 | modbus_read_input_bits.3 \ |
| 14 | modbus_read_input_registers.3 \ | 14 | modbus_read_input_registers.3 \ |
| 15 | modbus_read_registers.3 \ | 15 | modbus_read_registers.3 \ |
| 16 | + modbus_receive.3 \ | ||
| 17 | + modbus_receive_from.3 \ | ||
| 16 | modbus_receive_confirmation.3 \ | 18 | modbus_receive_confirmation.3 \ |
| 17 | modbus_send_raw_request.3 \ | 19 | modbus_send_raw_request.3 \ |
| 18 | modbus_set_debug.3 \ | 20 | modbus_set_debug.3 \ |
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,19 +413,27 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) | ||
| 413 | return ctx->backend->check_integrity(ctx, msg, msg_length); | 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 | /* Receives the confirmation. | 439 | /* Receives the confirmation. |
src/modbus.h
| @@ -175,7 +175,9 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); | @@ -175,7 +175,9 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping); | ||
| 175 | 175 | ||
| 176 | int modbus_send_raw_request(modbus_t *ctx, uint8_t *raw_req, int raw_req_length); | 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 | int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp); | 181 | int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp); |
| 180 | 182 | ||
| 181 | int modbus_reply(modbus_t *ctx, const uint8_t *req, | 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,7 +118,7 @@ int main(void) | ||
| 118 | /* An already connected master has sent a new query */ | 118 | /* An already connected master has sent a new query */ |
| 119 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 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 | if (rc != -1) { | 122 | if (rc != -1) { |
| 123 | modbus_reply(ctx, query, rc, mb_mapping); | 123 | modbus_reply(ctx, query, rc, mb_mapping); |
| 124 | } else { | 124 | } else { |
tests/bandwidth-server-one.c
| @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) | @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) | ||
| 74 | for(;;) { | 74 | for(;;) { |
| 75 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 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 | if (rc >= 0) { | 78 | if (rc >= 0) { |
| 79 | modbus_reply(ctx, query, rc, mb_mapping); | 79 | modbus_reply(ctx, query, rc, mb_mapping); |
| 80 | } else { | 80 | } else { |
tests/random-test-server.c
| @@ -46,7 +46,7 @@ int main(void) | @@ -46,7 +46,7 @@ int main(void) | ||
| 46 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 46 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 47 | int rc; | 47 | int rc; |
| 48 | 48 | ||
| 49 | - rc = modbus_receive(ctx, -1, query); | 49 | + rc = modbus_receive(ctx, query); |
| 50 | if (rc != -1) { | 50 | if (rc != -1) { |
| 51 | /* rc is the query size */ | 51 | /* rc is the query size */ |
| 52 | modbus_reply(ctx, query, rc, mb_mapping); | 52 | modbus_reply(ctx, query, rc, mb_mapping); |
tests/unit-test-server.c
| @@ -115,7 +115,7 @@ int main(int argc, char*argv[]) | @@ -115,7 +115,7 @@ int main(int argc, char*argv[]) | ||
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | for (;;) { | 117 | for (;;) { |
| 118 | - rc = modbus_receive(ctx, -1, query); | 118 | + rc = modbus_receive(ctx, query); |
| 119 | if (rc == -1) { | 119 | if (rc == -1) { |
| 120 | /* Connection closed by the client or error */ | 120 | /* Connection closed by the client or error */ |
| 121 | break; | 121 | break; |