modbus-private.h 3.3 KB

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