modbus-private.h 3.2 KB

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