Instructions to build libmodbus in a VS project
[libmodbus.git] / src / modbus-private.h
blobb0bb1ab048708d32401ef88e1b936817e4f4ea5f
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
7 #ifndef MODBUS_PRIVATE_H
8 #define MODBUS_PRIVATE_H
10 // clang-format off
11 #ifndef _MSC_VER
12 # include <stdint.h>
13 # include <sys/time.h>
14 #else
15 # include "stdint.h"
16 # include <time.h>
17 typedef int ssize_t;
18 #endif
19 // clang-format on
20 #include <config.h>
21 #include <sys/types.h>
23 #include "modbus.h"
25 MODBUS_BEGIN_DECLS
27 /* It's not really the minimal length (the real one is report slave ID
28 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
29 * communications to read many values or write a single one.
30 * Maximum between :
31 * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
32 * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
34 #define _MIN_REQ_LENGTH 12
36 #define _REPORT_SLAVE_ID 180
38 #define _MODBUS_EXCEPTION_RSP_LENGTH 5
40 /* Timeouts in microsecond (0.5 s) */
41 #define _RESPONSE_TIMEOUT 500000
42 #define _BYTE_TIMEOUT 500000
44 typedef enum {
45 _MODBUS_BACKEND_TYPE_RTU = 0,
46 _MODBUS_BACKEND_TYPE_TCP
47 } modbus_backend_type_t;
50 * ---------- Request Indication ----------
51 * | Client | ---------------------->| Server |
52 * ---------- Confirmation Response ----------
54 typedef enum {
55 /* Request message on the server side */
56 MSG_INDICATION,
57 /* Request message on the client side */
58 MSG_CONFIRMATION
59 } msg_type_t;
61 /* This structure reduces the number of params in functions and so
62 * optimizes the speed of execution (~ 37%). */
63 typedef struct _sft {
64 int slave;
65 int function;
66 int t_id;
67 } sft_t;
69 typedef struct _modbus_backend {
70 unsigned int backend_type;
71 unsigned int header_length;
72 unsigned int checksum_length;
73 unsigned int max_adu_length;
74 int (*set_slave)(modbus_t *ctx, int slave);
75 int (*build_request_basis)(
76 modbus_t *ctx, int function, int addr, int nb, uint8_t *req);
77 int (*build_response_basis)(sft_t *sft, uint8_t *rsp);
78 int (*prepare_response_tid)(const uint8_t *req, int *req_length);
79 int (*send_msg_pre)(uint8_t *req, int req_length);
80 ssize_t (*send)(modbus_t *ctx, const uint8_t *req, int req_length);
81 int (*receive)(modbus_t *ctx, uint8_t *req);
82 ssize_t (*recv)(modbus_t *ctx, uint8_t *rsp, int rsp_length);
83 int (*check_integrity)(modbus_t *ctx, uint8_t *msg, const int msg_length);
84 int (*pre_check_confirmation)(modbus_t *ctx,
85 const uint8_t *req,
86 const uint8_t *rsp,
87 int rsp_length);
88 int (*connect)(modbus_t *ctx);
89 void (*close)(modbus_t *ctx);
90 int (*flush)(modbus_t *ctx);
91 int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
92 void (*free)(modbus_t *ctx);
93 } modbus_backend_t;
95 struct _modbus {
96 /* Slave address */
97 int slave;
98 /* Socket or file descriptor */
99 int s;
100 int debug;
101 int error_recovery;
102 int quirks;
103 struct timeval response_timeout;
104 struct timeval byte_timeout;
105 struct timeval indication_timeout;
106 const modbus_backend_t *backend;
107 void *backend_data;
110 void _modbus_init_common(modbus_t *ctx);
111 void _error_print(modbus_t *ctx, const char *context);
112 int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type);
114 #ifndef HAVE_STRLCPY
115 size_t strlcpy(char *dest, const char *src, size_t dest_size);
116 #endif
118 MODBUS_END_DECLS
120 #endif /* MODBUS_PRIVATE_H */