Commit 057be70d689590ea89f4b21032a8233a7440452b
Committed by
Stéphane Raimbault
1 parent
01ba9771
Pass complete modbus_t structure to send()/recv() of the backends
The send() and recv() functions of the backends might require more information than just a file descriptor, therefore pass the complete modbus_t structure. Signed-off-by: Stéphane Raimbault <stephane.raimbault@gmail.com>
Showing
4 changed files
with
12 additions
and
12 deletions
src/modbus-private.h
| ... | ... | @@ -76,8 +76,8 @@ typedef struct _modbus_backend { |
| 76 | 76 | int (*build_response_basis) (sft_t *sft, uint8_t *rsp); |
| 77 | 77 | int (*prepare_response_tid) (const uint8_t *req, int *req_length); |
| 78 | 78 | int (*send_msg_pre) (uint8_t *req, int req_length); |
| 79 | - ssize_t (*send) (int s, const uint8_t *req, int req_length); | |
| 80 | - ssize_t (*recv) (int s, uint8_t *rsp, int rsp_length); | |
| 79 | + ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length); | |
| 80 | + ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length); | |
| 81 | 81 | int (*check_integrity) (modbus_t *ctx, uint8_t *msg, |
| 82 | 82 | const int msg_length); |
| 83 | 83 | int (*connect) (modbus_t *ctx); | ... | ... |
src/modbus-rtu.c
| ... | ... | @@ -156,14 +156,14 @@ int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length) |
| 156 | 156 | return req_length; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | -ssize_t _modbus_rtu_send(int s, const uint8_t *req, int req_length) | |
| 159 | +ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) | |
| 160 | 160 | { |
| 161 | - return write(s, req, req_length); | |
| 161 | + return write(ctx->s, req, req_length); | |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | -ssize_t _modbus_rtu_recv(int s, uint8_t *rsp, int rsp_length) | |
| 164 | +ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) | |
| 165 | 165 | { |
| 166 | - return read(s, rsp, rsp_length); | |
| 166 | + return read(ctx->s, rsp, rsp_length); | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /* The check_crc16 function shall return the message length if the CRC is | ... | ... |
src/modbus-tcp.c
| ... | ... | @@ -130,17 +130,17 @@ int _modbus_tcp_send_msg_pre(uint8_t *req, int req_length) |
| 130 | 130 | return req_length; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | -ssize_t _modbus_tcp_send(int s, const uint8_t *req, int req_length) | |
| 133 | +ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_length) | |
| 134 | 134 | { |
| 135 | 135 | /* MSG_NOSIGNAL |
| 136 | 136 | Requests not to send SIGPIPE on errors on stream oriented |
| 137 | 137 | sockets when the other end breaks the connection. The EPIPE |
| 138 | 138 | error is still returned. */ |
| 139 | - return send(s, req, req_length, MSG_NOSIGNAL); | |
| 139 | + return send(ctx->s, req, req_length, MSG_NOSIGNAL); | |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | -ssize_t _modbus_tcp_recv(int s, uint8_t *rsp, int rsp_length) { | |
| 143 | - return recv(s, rsp, rsp_length, 0); | |
| 142 | +ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) { | |
| 143 | + return recv(ctx->s, rsp, rsp_length, 0); | |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length) | ... | ... |
src/modbus.c
| ... | ... | @@ -156,7 +156,7 @@ static int send_msg(modbus_t *ctx, uint8_t *req, int req_length) |
| 156 | 156 | /* In recovery mode, the write command will be issued until to be |
| 157 | 157 | successful! Disabled by default. */ |
| 158 | 158 | do { |
| 159 | - rc = ctx->backend->send(ctx->s, req, req_length); | |
| 159 | + rc = ctx->backend->send(ctx, req, req_length); | |
| 160 | 160 | if (rc == -1) { |
| 161 | 161 | _error_print(ctx, NULL); |
| 162 | 162 | if (ctx->error_recovery && |
| ... | ... | @@ -346,7 +346,7 @@ static int receive_msg(modbus_t *ctx, int msg_length_computed, |
| 346 | 346 | |
| 347 | 347 | p_msg = msg; |
| 348 | 348 | while (s_rc) { |
| 349 | - read_rc = ctx->backend->recv(ctx->s, p_msg, length_to_read); | |
| 349 | + read_rc = ctx->backend->recv(ctx, p_msg, length_to_read); | |
| 350 | 350 | if (read_rc == 0) { |
| 351 | 351 | errno = ECONNRESET; |
| 352 | 352 | read_rc = -1; | ... | ... |