2 * Copyright © 2010-2012 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
7 #ifndef MODBUS_PRIVATE_H
8 #define MODBUS_PRIVATE_H
12 # include <sys/time.h>
18 #include <sys/types.h>
25 /* It's not really the minimal length (the real one is report slave ID
26 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
27 * communications to read many values or write a single one.
29 * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
30 * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
32 #define _MIN_REQ_LENGTH 12
34 #define _REPORT_SLAVE_ID 180
36 #define _MODBUS_EXCEPTION_RSP_LENGTH 5
38 /* Timeouts in microsecond (0.5 s) */
39 #define _RESPONSE_TIMEOUT 500000
40 #define _BYTE_TIMEOUT 500000
43 _MODBUS_BACKEND_TYPE_RTU
=0,
44 _MODBUS_BACKEND_TYPE_TCP
45 } modbus_backend_type_t
;
48 * ---------- Request Indication ----------
49 * | Client | ---------------------->| Server |
50 * ---------- Confirmation Response ----------
53 /* Request message on the server side */
55 /* Request message on the client side */
59 /* This structure reduces the number of params in functions and so
60 * optimizes the speed of execution (~ 37%). */
67 typedef struct _modbus_backend
{
68 unsigned int backend_type
;
69 unsigned int header_length
;
70 unsigned int checksum_length
;
71 unsigned int max_adu_length
;
72 int (*set_slave
) (modbus_t
*ctx
, int slave
);
73 int (*build_request_basis
) (modbus_t
*ctx
, int function
, int addr
,
74 int nb
, uint8_t *req
);
75 int (*build_response_basis
) (sft_t
*sft
, uint8_t *rsp
);
76 int (*prepare_response_tid
) (const uint8_t *req
, int *req_length
);
77 int (*send_msg_pre
) (uint8_t *req
, int req_length
);
78 ssize_t (*send
) (modbus_t
*ctx
, const uint8_t *req
, int req_length
);
79 int (*receive
) (modbus_t
*ctx
, uint8_t *req
);
80 ssize_t (*recv
) (modbus_t
*ctx
, uint8_t *rsp
, int rsp_length
);
81 int (*check_integrity
) (modbus_t
*ctx
, uint8_t *msg
,
82 const int msg_length
);
83 int (*pre_check_confirmation
) (modbus_t
*ctx
, const uint8_t *req
,
84 const uint8_t *rsp
, int rsp_length
);
85 int (*connect
) (modbus_t
*ctx
);
86 void (*close
) (modbus_t
*ctx
);
87 int (*flush
) (modbus_t
*ctx
);
88 int (*select
) (modbus_t
*ctx
, fd_set
*rset
, struct timeval
*tv
, int msg_length
);
89 void (*free
) (modbus_t
*ctx
);
95 /* Socket or file descriptor */
99 struct timeval response_timeout
;
100 struct timeval byte_timeout
;
101 struct timeval indication_timeout
;
102 const modbus_backend_t
*backend
;
106 void _modbus_init_common(modbus_t
*ctx
);
107 void _error_print(modbus_t
*ctx
, const char *context
);
108 int _modbus_receive_msg(modbus_t
*ctx
, uint8_t *msg
, msg_type_t msg_type
);
111 size_t strlcpy(char *dest
, const char *src
, size_t dest_size
);
116 #endif /* MODBUS_PRIVATE_H */