rtlink.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-02 xiangxistu the first version
  9. * 2021-03-19 Sherman Streamline the struct rt_link_session
  10. */
  11. #ifndef __RT_LINK_H__
  12. #define __RT_LINK_H__
  13. #include <rtdef.h>
  14. #define RT_LINK_VER "0.2.0"
  15. #define RT_LINK_AUTO_INIT
  16. #define RT_LINK_FLAG_ACK 0x01U
  17. #define RT_LINK_FLAG_CRC 0x02U
  18. #define RT_LINK_FRAME_HEAD 0x15U
  19. #define RT_LINK_FRAME_HEAD_MASK 0x1FU
  20. /* The maximum number of split frames for a long package */
  21. #define RT_LINK_FRAMES_MAX 0x03U
  22. /* The length in the rt_link_frame_head structure occupies 11 bits,
  23. so the value range after 4-byte alignment is 0-2044.*/
  24. #define RT_LINK_MAX_FRAME_LENGTH 1024U
  25. #define RT_LINK_ACK_MAX 0x07U
  26. #define RT_LINK_CRC_LENGTH 4U
  27. #define RT_LINK_HEAD_LENGTH 4U
  28. #define RT_LINK_EXTEND_LENGTH 4U
  29. #define RT_LINK_MAX_DATA_LENGTH (RT_LINK_MAX_FRAME_LENGTH - \
  30. RT_LINK_HEAD_LENGTH - \
  31. RT_LINK_EXTEND_LENGTH - \
  32. RT_LINK_CRC_LENGTH)
  33. #define RT_LINK_RECEIVE_BUFFER_LENGTH (RT_LINK_MAX_FRAME_LENGTH * \
  34. RT_LINK_FRAMES_MAX + \
  35. RT_LINK_HEAD_LENGTH + \
  36. RT_LINK_EXTEND_LENGTH)
  37. typedef enum
  38. {
  39. RT_LINK_SERVICE_RTLINK = 0,
  40. RT_LINK_SERVICE_SOCKET = 1,
  41. RT_LINK_SERVICE_WIFI = 2,
  42. RT_LINK_SERVICE_MNGT = 3,
  43. RT_LINK_SERVICE_MSHTOOLS = 4,
  44. /* Expandable to a maximum of 31 */
  45. RT_LINK_SERVICE_MAX
  46. } rt_link_service_e;
  47. typedef enum
  48. {
  49. RT_LINK_RESEND_FRAME = 0,
  50. RT_LINK_CONFIRM_FRAME = 1,
  51. RT_LINK_HANDSHAKE_FRAME = 2,
  52. RT_LINK_DETACH_FRAME = 3, /* service is not online */
  53. RT_LINK_SESSION_END = 4, /* The retring failed to end the session */
  54. RT_LINK_LONG_DATA_FRAME = 5,
  55. RT_LINK_SHORT_DATA_FRAME = 6,
  56. RT_LINK_RESERVE_FRAME = 7
  57. } rt_link_frame_attr_e;
  58. typedef enum
  59. {
  60. /* receive event */
  61. RT_LINK_READ_CHECK_EVENT = 1 << 0,
  62. RT_LINK_RECV_TIMEOUT_FRAME_EVENT = 1 << 1,
  63. RT_LINK_RECV_TIMEOUT_LONG_EVENT = 1 << 2,
  64. /* send event */
  65. RT_LINK_SEND_READY_EVENT = 1 << 4,
  66. RT_LINK_SEND_OK_EVENT = 1 << 5,
  67. RT_LINK_SEND_FAILED_EVENT = 1 << 6,
  68. RT_LINK_SEND_TIMEOUT_EVENT = 1 << 7
  69. } rt_link_notice_e;
  70. typedef enum
  71. {
  72. RT_LINK_INIT = 0,
  73. RT_LINK_DISCONN = 1,
  74. RT_LINK_CONNECT = 2,
  75. } rt_link_linkstate_e;
  76. typedef enum
  77. {
  78. RT_LINK_EOK = 0,
  79. RT_LINK_ERR = 1,
  80. RT_LINK_ETIMEOUT = 2,
  81. RT_LINK_EFULL = 3,
  82. RT_LINK_EEMPTY = 4,
  83. RT_LINK_ENOMEM = 5,
  84. RT_LINK_EIO = 6,
  85. RT_LINK_ESESSION = 7,
  86. RT_LINK_ESERVICE = 8,
  87. RT_LINK_EMAX
  88. } rt_link_err_e;
  89. struct rt_link_receive_buffer
  90. {
  91. /* rt-link receive data buffer */
  92. rt_uint8_t data[RT_LINK_RECEIVE_BUFFER_LENGTH];
  93. rt_uint8_t *read_point;
  94. rt_uint8_t *write_point;
  95. rt_uint8_t *end_point;
  96. };
  97. struct rt_link_frame_head
  98. {
  99. rt_uint8_t magicid : 5;
  100. rt_uint8_t extend : 1;
  101. rt_uint8_t crc : 1;
  102. rt_uint8_t ack : 1;
  103. rt_uint8_t sequence;
  104. rt_uint16_t service: 5;
  105. rt_uint16_t length : 11; /* range 0~2047 */
  106. };
  107. /* record frame information that opposite */
  108. struct rt_link_record
  109. {
  110. rt_uint8_t rx_seq; /* record the opposite sequence */
  111. rt_uint8_t total; /* the number of long frame number */
  112. rt_uint8_t long_count; /* long packet recv counter */
  113. rt_uint8_t *dataspace; /* the space of long frame */
  114. };
  115. struct rt_link_extend
  116. {
  117. rt_uint16_t attribute; /* rt_link_frame_attr_e */
  118. rt_uint16_t parameter;
  119. };
  120. struct rt_link_frame
  121. {
  122. struct rt_link_frame_head head; /* frame head */
  123. struct rt_link_extend extend; /* frame extend data */
  124. rt_uint8_t *real_data; /* the origin data */
  125. rt_uint32_t crc; /* CRC result */
  126. rt_uint16_t data_len; /* the length of frame length */
  127. rt_uint16_t attribute; /* rt_link_frame_attr_e */
  128. rt_uint8_t issent;
  129. rt_uint8_t index; /* the index frame for long frame */
  130. rt_uint8_t total; /* the total frame for long frame */
  131. rt_slist_t slist; /* the frame will hang on the send list on session */
  132. };
  133. struct rt_link_service
  134. {
  135. rt_int32_t timeout_tx;
  136. void (*send_cb)(struct rt_link_service *service, void *buffer);
  137. void (*recv_cb)(struct rt_link_service *service, void *data, rt_size_t size);
  138. void *user_data;
  139. rt_uint8_t flag; /* Whether to use the CRC and ACK */
  140. rt_link_service_e service;
  141. rt_link_linkstate_e state; /* channel link state */
  142. rt_link_err_e err;
  143. };
  144. struct rt_link_session
  145. {
  146. struct rt_event event;
  147. struct rt_link_service *service[RT_LINK_SERVICE_MAX];
  148. rt_uint8_t tx_seq; /* Sequence number of the send data frame */
  149. rt_slist_t tx_data_slist;
  150. rt_uint8_t sendbuffer[RT_LINK_MAX_FRAME_LENGTH];
  151. struct rt_event sendevent;
  152. struct rt_timer sendtimer;
  153. struct rt_link_record rx_record; /* the memory of receive status */
  154. struct rt_timer recvtimer; /* receive a frame timer for rt link */
  155. struct rt_timer longframetimer; /* receive long frame timer for rt link */
  156. struct rt_link_receive_buffer *rx_buffer;
  157. rt_uint32_t (*calculate_crc)(rt_uint8_t using_buffer_ring, rt_uint8_t *data, rt_size_t size);
  158. rt_link_linkstate_e state; /* Link status */
  159. };
  160. #define SERV_ERR_GET(service) (service->err)
  161. /* rtlink init and deinit, default is automatic initialization*/
  162. int rt_link_init(void);
  163. rt_err_t rt_link_deinit(void);
  164. rt_size_t rt_link_send(struct rt_link_service *service, const void *data, rt_size_t size);
  165. /* rtlink service attach and detach */
  166. rt_err_t rt_link_service_attach(struct rt_link_service *service);
  167. rt_err_t rt_link_service_detach(struct rt_link_service *service);
  168. /* Private operator function */
  169. struct rt_link_session *rt_link_get_scb(void);
  170. #endif /* __RT_LINK_H__ */