Эх сурвалжийг харах

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>
Tobias Doerffel 14 жил өмнө
parent
commit
057be70d68

+ 2 - 2
src/modbus-private.h

@@ -76,8 +76,8 @@ typedef struct _modbus_backend {
     int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
     int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
     int (*prepare_response_tid) (const uint8_t *req, int *req_length);
     int (*prepare_response_tid) (const uint8_t *req, int *req_length);
     int (*send_msg_pre) (uint8_t *req, int req_length);
     int (*send_msg_pre) (uint8_t *req, int req_length);
-    ssize_t (*send) (int s, const uint8_t *req, int req_length);
-    ssize_t (*recv) (int s, uint8_t *rsp, int rsp_length);
+    ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);
+    ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);
     int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
     int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
                             const int msg_length);
                             const int msg_length);
     int (*connect) (modbus_t *ctx);
     int (*connect) (modbus_t *ctx);

+ 4 - 4
src/modbus-rtu.c

@@ -156,14 +156,14 @@ int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
     return req_length;
     return req_length;
 }
 }
 
 
-ssize_t _modbus_rtu_send(int s, const uint8_t *req, int req_length)
+ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
 {
 {
-    return write(s, req, req_length);
+    return write(ctx->s, req, req_length);
 }
 }
 
 
-ssize_t _modbus_rtu_recv(int s, uint8_t *rsp, int rsp_length)
+ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
 {
 {
-    return read(s, rsp, rsp_length);
+    return read(ctx->s, rsp, rsp_length);
 }
 }
 
 
 /* The check_crc16 function shall return the message length if the CRC is
 /* The check_crc16 function shall return the message length if the CRC is

+ 4 - 4
src/modbus-tcp.c

@@ -130,17 +130,17 @@ int _modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
     return req_length;
     return req_length;
 }
 }
 
 
-ssize_t _modbus_tcp_send(int s, const uint8_t *req, int req_length)
+ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_length)
 {
 {
     /* MSG_NOSIGNAL
     /* MSG_NOSIGNAL
        Requests not to send SIGPIPE on errors on stream oriented
        Requests not to send SIGPIPE on errors on stream oriented
        sockets when the other end breaks the connection.  The EPIPE
        sockets when the other end breaks the connection.  The EPIPE
        error is still returned. */
        error is still returned. */
-    return send(s, req, req_length, MSG_NOSIGNAL);
+    return send(ctx->s, req, req_length, MSG_NOSIGNAL);
 }
 }
 
 
-ssize_t _modbus_tcp_recv(int s, uint8_t *rsp, int rsp_length) {
-    return recv(s, rsp, rsp_length, 0);
+ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {
+    return recv(ctx->s, rsp, rsp_length, 0);
 }
 }
 
 
 int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
 int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)

+ 2 - 2
src/modbus.c

@@ -156,7 +156,7 @@ static int send_msg(modbus_t *ctx, uint8_t *req, int req_length)
     /* In recovery mode, the write command will be issued until to be
     /* In recovery mode, the write command will be issued until to be
        successful! Disabled by default. */
        successful! Disabled by default. */
     do {
     do {
-        rc = ctx->backend->send(ctx->s, req, req_length);
+        rc = ctx->backend->send(ctx, req, req_length);
         if (rc == -1) {
         if (rc == -1) {
             _error_print(ctx, NULL);
             _error_print(ctx, NULL);
             if (ctx->error_recovery &&
             if (ctx->error_recovery &&
@@ -346,7 +346,7 @@ static int receive_msg(modbus_t *ctx, int msg_length_computed,
 
 
     p_msg = msg;
     p_msg = msg;
     while (s_rc) {
     while (s_rc) {
-        read_rc = ctx->backend->recv(ctx->s, p_msg, length_to_read);
+        read_rc = ctx->backend->recv(ctx, p_msg, length_to_read);
         if (read_rc == 0) {
         if (read_rc == 0) {
             errno = ECONNRESET;
             errno = ECONNRESET;
             read_rc = -1;
             read_rc = -1;