RT_tunnel.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef __RT_TUNNEL_H__
  2. #define __RT_TUNNEL_H__
  3. #include "RT_LOG_Print.h"
  4. #include "chry_ringbuffer.h"
  5. #include "rtthread.h"
  6. /* ---------------- Tunnel Configuration ---------------- */
  7. // #define TUNNEL_NUM (3UL) /**< Number of tunnels */
  8. // #define TUNNEL_BUFFER_SIZE (8 * 1024UL) /**< Buffer size per tunnel */
  9. #define TUNNEL_RESET_ID (0xBAADF00DUL) /**< Default reset ID */
  10. /* ---------------- Tunnel Status Flags ---------------- */
  11. #define STATUS_ACTIVE_Pos (0UL)
  12. #define STATUS_ACTIVE_Msk (1UL << STATUS_ACTIVE_Pos)
  13. #define STATUS_ACTIVE (1UL << STATUS_ACTIVE_Pos)
  14. #define STATUS_UNACTIVE (0UL << STATUS_ACTIVE_Pos)
  15. #define STATUS_OPERATION_Pos (1UL)
  16. #define STATUS_OPERATION_Msk (1UL << STATUS_OPERATION_Pos)
  17. #define STATUS_OPERATION_READ (1UL << STATUS_OPERATION_Pos)
  18. #define STATUS_OPERATION_WRITE (0UL << STATUS_OPERATION_Pos)
  19. #define STATUS_USED_Pos (2UL)
  20. #define STATUS_USED_Msk (1UL << STATUS_USED_Pos)
  21. #define STATUS_USED_BUSY (1UL << STATUS_USED_Pos)
  22. #define STATUS_USED_FREE (0UL << STATUS_USED_Pos)
  23. #define STATUS_BUFFER_Pos (3UL)
  24. #define STATUS_BUFFER_Msk (1UL << STATUS_BUFFER_Pos)
  25. #define STATUS_BUFFER_FULL (1UL << STATUS_BUFFER_Pos)
  26. #define STATUS_BUFFER_AVAILABLE (0UL << STATUS_BUFFER_Pos)
  27. /* ---------------- Tunnel Return Codes ---------------- */
  28. #define PTR_ERROR_CODE (-1) /**< Error code indicating that a pointer is NULL or invalid */
  29. #define TUNNEL_BUSY_CODE (-2) /**< Error code indicating that the tunnel is currently busy */
  30. #define OPERATION_ERROR_CODE (-3) /**< Error code indicating an invalid or failed operation */
  31. #define TUNNEL_FULL_CODE (-4) /**< Error code indicating that the tunnel buffer is full */
  32. /* ---------------- Tunnel Operation Enum ---------------- */
  33. /**
  34. * @enum tunnel_operation
  35. * @brief Tunnel operation type.
  36. */
  37. typedef enum _tunnel_operation
  38. {
  39. tunnel_write = 0, /**< Write operation */
  40. tunnel_read = 1 /**< Read operation */
  41. } tunnel_operation;
  42. /* ---------------- Tunnel Structure ---------------- */
  43. /**
  44. * @struct RT_tunnel
  45. * @brief Tunnel structure containing buffer, lock, and operations.
  46. */
  47. typedef struct _RT_tunnel
  48. {
  49. uint32_t ID; /**< Tunnel ID */
  50. uint32_t status; /**< Tunnel status flags */
  51. chry_ringbuffer_t RB; /**< Ring buffer for data storage */
  52. int (*read)(struct _RT_tunnel *tunnel, void *buffer, uint32_t bytes); /**< Read callback */
  53. int (*write)(struct _RT_tunnel *tunnel, void *buffer, uint32_t bytes); /**< Write callback */
  54. } RT_tunnel;
  55. typedef RT_tunnel *RT_tunnel_t;
  56. /* ---------------- Tunnel Control Block ---------------- */
  57. /**
  58. * @struct RT_tunnel_CB
  59. * @brief Global control block for tunnel management.
  60. */
  61. typedef struct _RT_tunnel_CB
  62. {
  63. char ID[8]; /**< Control block identifier */
  64. uint32_t tunnel_num; /**< Number of tunnels */
  65. RT_tunnel *tunnel_ptr[TUNNEL_NUM]; /**< Array of tunnel pointers */
  66. } RT_tunnel_CB;
  67. typedef RT_tunnel_CB *RT_tunnel_CB_t;
  68. /* ---------------- API Functions ---------------- */
  69. /**
  70. * @brief Get free byte count of tunnel buffer.
  71. * @param[in] tunnel Tunnel instance
  72. * @retval int Number of free bytes in the buffer
  73. */
  74. int Get_Tunnel_Buffer_Free(RT_tunnel_t tunnel);
  75. /**
  76. * @brief Get used byte count of tunnel buffer.
  77. * @param[in] tunnel Tunnel instance
  78. * @retval int Number of used bytes in the buffer
  79. */
  80. int Get_Tunnel_Buffer_Used(RT_tunnel_t tunnel);
  81. /**
  82. * @brief Get a free tunnel instance.
  83. * @retval RT_tunnel_t A free tunnel instance, or NULL if none available
  84. */
  85. RT_tunnel_t Get_Free_Tunnel(void);
  86. /**
  87. * @brief Set operation type for a tunnel.
  88. * @param[in] tunnel Tunnel instance
  89. * @param[in] operation Operation type (read/write)
  90. * @retval int 0 on success, negative value on error
  91. */
  92. int Set_Tunnel_Operation(RT_tunnel_t tunnel, tunnel_operation operation);
  93. /*****************************************************************************
  94. * @brief Set ID for a tunnel
  95. * @param[in] tunnel Pointer to tunnel instance
  96. * @param[in] ID ID to assign to the tunnel
  97. * @retval int 0 if success, negative value on error
  98. *****************************************************************************/
  99. int Set_Tunnel_ID(RT_tunnel_t tunnel, uint32_t ID);
  100. /**
  101. * @brief Initialize tunnel system.
  102. * @note Should be called before using any tunnel functions.
  103. */
  104. int RT_Tunnel_Init(void);
  105. #endif /* __RT_TUNNEL_H__ */