| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef MODBUS_PRIVATE_H |
| 8 | #define MODBUS_PRIVATE_H |
| 9 | |
| 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> |
| 22 | |
| 23 | #include "modbus.h" |
| 24 | |
| 25 | MODBUS_BEGIN_DECLS |
| 26 | |
| 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) |
| 33 | */ |
| 34 | #define _MIN_REQ_LENGTH 12 |
| 35 | |
| 36 | #define _REPORT_SLAVE_ID 180 |
| 37 | |
| 38 | #define _MODBUS_EXCEPTION_RSP_LENGTH 5 |
| 39 | |
| 40 | /* Timeouts in microsecond (0.5 s) */ |
| 41 | #define _RESPONSE_TIMEOUT 500000 |
| 42 | #define _BYTE_TIMEOUT 500000 |
| 43 | |
| 44 | typedef enum { |
| 45 | _MODBUS_BACKEND_TYPE_RTU = 0, |
| 46 | _MODBUS_BACKEND_TYPE_TCP |
| 47 | } modbus_backend_type_t; |
| 48 | |
| 49 | /* |
| 50 | * ---------- Request Indication ---------- |
| 51 | * | Client | ---------------------->| Server | |
| 52 | * ---------- Confirmation Response ---------- |
| 53 | */ |
| 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; |
| 60 | |
| 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; |
| 68 | |
| 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 (*get_response_tid)(const uint8_t *req); |
| 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 | unsigned int (*is_connected)(modbus_t *ctx); |
| 90 | void (*close)(modbus_t *ctx); |
| 91 | int (*flush)(modbus_t *ctx); |
| 92 | int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length); |
| 93 | void (*free)(modbus_t *ctx); |
| 94 | } modbus_backend_t; |
| 95 | |
| 96 | struct _modbus { |
| 97 | /* Slave address */ |
| 98 | int slave; |
| 99 | /* Socket or file descriptor */ |
| 100 | int s; |
| 101 | int debug; |
| 102 | int error_recovery; |
| 103 | int quirks; |
| 104 | struct timeval response_timeout; |
| 105 | struct timeval byte_timeout; |
| 106 | struct timeval indication_timeout; |
| 107 | const modbus_backend_t *backend; |
| 108 | void *backend_data; |
| 109 | }; |
| 110 | |
| 111 | void _modbus_init_common(modbus_t *ctx); |
| 112 | void _error_print(modbus_t *ctx, const char *context); |
| 113 | int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type); |
| 114 | |
| 115 | #ifndef HAVE_STRLCPY |
| 116 | size_t strlcpy(char *dest, const char *src, size_t dest_size); |
| 117 | #endif |
| 118 | |
| 119 | MODBUS_END_DECLS |
| 120 | |
| 121 | #endif /* MODBUS_PRIVATE_H */ |