RT_Vterm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "RT_Vterm.h"
  2. #include <finsh.h>
  3. #include <rtthread.h>
  4. #include <string.h>
  5. #include "RT_tunnel.h"
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. /**< Virtual Terminal device name */
  10. #define VTERM_DEVICE_NAME "VTerm"
  11. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  12. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  13. /**< Tunnel instance for upstream data transmission */
  14. RT_tunnel_t up_tunnel = RT_NULL;
  15. /**< Tunnel instance for downstream data transmission */
  16. RT_tunnel_t down_tunnel = RT_NULL;
  17. /*****************************************************************************
  18. * @brief Initialize the Virtual Terminal module
  19. *
  20. * @details This function acquires free tunnel instances for upstream and
  21. * downstream data transmission, and configures their operation modes.
  22. * Upstream tunnel is set to write mode, downstream to read mode.
  23. *
  24. * @retval int RT_EOK on success (0), error code otherwise
  25. *
  26. * @note Initialized after tunnel system but before device drivers
  27. *****************************************************************************/
  28. int RT_Vterm_Init(void)
  29. {
  30. up_tunnel = Get_Free_Tunnel();
  31. down_tunnel = Get_Free_Tunnel();
  32. if(!up_tunnel||!down_tunnel)
  33. {
  34. LOG_ERROR("no enough tunnel");
  35. return -RT_ENOMEM;
  36. }
  37. Set_Tunnel_Operation(up_tunnel, tunnel_write);
  38. Set_Tunnel_Operation(down_tunnel, tunnel_read);
  39. up_tunnel->ID = 0x56545455; // VTTU
  40. down_tunnel->ID = 0x56545444; // VTTD
  41. return RT_EOK;
  42. }
  43. // Ensure initialization after tunnel system and before device drivers
  44. // INIT_PREV_EXPORT(RT_Vterm_Init);
  45. MSH_CMD_EXPORT(RT_Vterm_Init, RT_Vterm_Init)
  46. /*****************************************************************************
  47. * @brief Write data to upstream tunnel buffer
  48. *
  49. * @param[in] pBuffer Pointer to data buffer to be written
  50. * @param[in] NumBytes Number of bytes to write
  51. *
  52. * @retval uint32_t Actual number of bytes written, 0 if tunnel is invalid
  53. *
  54. * @note Thread-safe operation with internal locking
  55. *****************************************************************************/
  56. uint32_t RT_Vterm_Write(const void *pBuffer, uint32_t NumBytes)
  57. {
  58. if (up_tunnel)
  59. return up_tunnel->write(up_tunnel, (void *)pBuffer, NumBytes);
  60. return 0;
  61. }
  62. /*****************************************************************************
  63. * @brief Write data to downstream tunnel buffer
  64. *
  65. * @param[in] pBuffer Pointer to data buffer to be written
  66. * @param[in] NumBytes Number of bytes to write
  67. *
  68. * @retval uint32_t Actual number of bytes written, 0 if tunnel is invalid
  69. *
  70. * @note Thread-safe operation with internal locking
  71. *****************************************************************************/
  72. uint32_t RT_Vterm_WriteDownBuffer(const void *pBuffer, uint32_t NumBytes)
  73. {
  74. if (down_tunnel)
  75. return down_tunnel->write(down_tunnel, (void *)pBuffer, NumBytes);
  76. return 0;
  77. }
  78. /*****************************************************************************
  79. * @brief Read data from upstream tunnel buffer
  80. *
  81. * @param[out] pData Pointer to buffer to store read data
  82. * @param[in] BufferSize Maximum number of bytes to read
  83. *
  84. * @retval uint32_t Actual number of bytes read, 0 if tunnel is invalid
  85. *
  86. * @note Thread-safe operation with internal locking
  87. *****************************************************************************/
  88. uint32_t RT_Vterm_ReadUpBuffer(void *pData, uint32_t BufferSize)
  89. {
  90. if (up_tunnel)
  91. return up_tunnel->read(up_tunnel, pData, BufferSize);
  92. return 0;
  93. }
  94. /*****************************************************************************
  95. * @brief Read data from downstream tunnel buffer
  96. *
  97. * @param[out] pData Pointer to buffer to store read data
  98. * @param[in] BufferSize Maximum number of bytes to read
  99. *
  100. * @retval uint32_t Actual number of bytes read, 0 if tunnel is invalid
  101. *
  102. * @note Thread-safe operation with internal locking
  103. *****************************************************************************/
  104. uint32_t RT_Vterm_Read(void *pData, uint32_t BufferSize)
  105. {
  106. if (down_tunnel)
  107. return down_tunnel->read(down_tunnel, pData, BufferSize);
  108. return 0;
  109. }
  110. /**
  111. * @brief Check used bytes in downstream tunnel buffer
  112. * @retval uint32_t Number of available bytes in downstream buffer, 0 if tunnel invalid
  113. */
  114. uint32_t RT_Vterm_HasData(void)
  115. {
  116. if (down_tunnel)
  117. return Get_Tunnel_Buffer_Used(down_tunnel);
  118. return 0;
  119. }
  120. /**
  121. * @brief Check used bytes in upstream tunnel buffer
  122. * @retval uint32_t Number of available bytes in upstream buffer, 0 if tunnel invalid
  123. */
  124. uint32_t RT_Vterm_HasDataUp(void)
  125. {
  126. if (up_tunnel)
  127. return Get_Tunnel_Buffer_Used(up_tunnel);
  128. return 0;
  129. }
  130. /**
  131. * @brief Check if there's available data in downstream buffer
  132. * @retval int 1 if data exists, 0 otherwise
  133. */
  134. int RT_Vterm_HasKey(void)
  135. {
  136. return RT_Vterm_HasData() > 0 ? 1 : 0;
  137. }
  138. /*****************************************************************************
  139. * @brief Block until data is available in downstream buffer, then read 1 byte
  140. *
  141. * @retval int The read byte (as unsigned char) when available
  142. *
  143. * @note This function blocks with 1ms delay between checks
  144. * Never returns -1 (infinite loop until data arrives)
  145. *****************************************************************************/
  146. int RT_Vterm_WaitKey(void)
  147. {
  148. int r;
  149. char c;
  150. do
  151. {
  152. r = RT_Vterm_Read(&c, 1);
  153. if (r == 1)
  154. return (unsigned char)c;
  155. rt_thread_mdelay(1);
  156. } while (1);
  157. }
  158. /**
  159. * @brief Read one byte from downstream buffer (non-blocking)
  160. * @retval int The read byte (as unsigned char) if available, -1 otherwise
  161. */
  162. int RT_Vterm_GetKey(void)
  163. {
  164. char c;
  165. int r = RT_Vterm_Read(&c, 1);
  166. if (r == 1)
  167. return (unsigned char)c;
  168. return -1;
  169. }
  170. /**
  171. * @brief Write a single character to upstream buffer
  172. * @param[in] c Character to write
  173. * @retval uint32_t 1 if successful, 0 otherwise
  174. */
  175. uint32_t RT_Vterm_PutChar(char c)
  176. {
  177. return RT_Vterm_Write(&c, 1);
  178. }
  179. /**
  180. * @brief Write a single character to upstream buffer (direct call)
  181. * @param[in] c Character to write
  182. * @retval uint32_t 1 if successful, 0 otherwise
  183. */
  184. uint32_t RT_Vterm_PutCharSkip(char c)
  185. {
  186. if (up_tunnel)
  187. return up_tunnel->write(up_tunnel, &c, 1);
  188. return 0;
  189. }
  190. /**
  191. * @brief Get available write space in upstream buffer
  192. * @retval uint32_t Number of free bytes in upstream buffer, 0 if tunnel invalid
  193. */
  194. uint32_t RT_Vterm_GetAvailWriteSpace(void)
  195. {
  196. if (up_tunnel)
  197. return Get_Tunnel_Buffer_Free(up_tunnel);
  198. return 0;
  199. }