Blame view

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