rtlink.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_AUTO_INIT
  15. #define RT_LINK_FRAME_HEAD 0x15
  16. #define RT_LINK_FRAME_HEAD_MASK 0x1F
  17. #define RT_LINK_MAX_DATA_LENGTH 2044 /*can exact divide by 4 bytes*/
  18. #define RT_LINK_FRAMES_MAX 0x03 /* The maximum number of split frames for a long package*/
  19. #define RT_LINK_ACK_MAX 0x07
  20. #define RT_LINK_CRC_LENGTH 4
  21. #define RT_LINK_HEAD_LENGTH 4
  22. #define RT_LINK_MAX_EXTEND_LENGTH 4
  23. #define RT_LINK_MAX_FRAME_LENGTH (RT_LINK_HEAD_LENGTH + RT_LINK_MAX_EXTEND_LENGTH + RT_LINK_MAX_DATA_LENGTH + RT_LINK_CRC_LENGTH)
  24. #define RT_LINK_RECEIVE_BUFFER_LENGTH (RT_LINK_MAX_FRAME_LENGTH * RT_LINK_FRAMES_MAX + RT_LINK_HEAD_LENGTH + RT_LINK_MAX_EXTEND_LENGTH)
  25. typedef enum
  26. {
  27. RT_LINK_SERVICE_RTLINK = 0,
  28. RT_LINK_SERVICE_LINK_SOCKET = 1,
  29. RT_LINK_SERVICE_LINK_WIFI = 2,
  30. RT_LINK_SERVICE_LINK_MNGT = 3,
  31. RT_LINK_SERVICE_LINK_MSHTOOLS = 4,
  32. RT_LINK_SERVICE_MAX
  33. } rt_link_service_t;
  34. enum
  35. {
  36. FRAME_EXTEND = 1 << 0,
  37. FRAME_CRC = 1 << 1,
  38. FRAME_ACK = 1 << 2
  39. };
  40. typedef enum
  41. {
  42. RT_LINK_RESERVE_FRAME = 0,
  43. RT_LINK_RESEND_FRAME,
  44. RT_LINK_CONFIRM_FRAME,
  45. RT_LINK_SHORT_DATA_FRAME,
  46. RT_LINK_LONG_DATA_FRAME,
  47. RT_LINK_SESSION_END, /* The retring failed to end the session */
  48. RT_LINK_HANDSHAKE_FRAME
  49. } rt_link_frame_attribute_t;
  50. typedef enum
  51. {
  52. /* receive event */
  53. RT_LINK_READ_CHECK_EVENT = 1 << 0,
  54. RT_LINK_RECV_TIMEOUT_FRAME_EVENT = 1 << 1,
  55. RT_LINK_RECV_TIMEOUT_LONG_EVENT = 1 << 2,
  56. /* send event */
  57. RT_LINK_SEND_READY_EVENT = 1 << 4,
  58. RT_LINK_SEND_OK_EVENT = 1 << 5,
  59. RT_LINK_SEND_FAILED_EVENT = 1 << 6,
  60. RT_LINK_SEND_TIMEOUT_EVENT = 1 << 7
  61. } rt_link_notice_t;
  62. typedef enum
  63. {
  64. RT_LINK_ESTABLISHING = 0,
  65. RT_LINK_NO_RESPONSE,
  66. RT_LINK_CONNECT_DONE,
  67. } rt_link_linkstatus_t;
  68. typedef enum
  69. {
  70. RECVTIMER_NONE = 0,
  71. RECVTIMER_FRAME,
  72. RECVTIMER_LONGFRAME
  73. } rt_link_recvtimer_status_t;
  74. struct rt_link_receive_buffer
  75. {
  76. rt_uint8_t data[RT_LINK_RECEIVE_BUFFER_LENGTH]; /* rt-link receive data buffer */
  77. rt_uint8_t *read_point;
  78. rt_uint8_t *write_point;
  79. rt_uint8_t *end_point;
  80. };
  81. struct rt_link_frame_head
  82. {
  83. rt_uint8_t magicid : 5;
  84. rt_uint8_t extend : 1;
  85. rt_uint8_t crc : 1;
  86. rt_uint8_t ack : 1;
  87. rt_uint8_t sequence;
  88. rt_uint16_t channel: 5;
  89. rt_uint16_t length : 11;
  90. };
  91. /* record frame information that opposite */
  92. struct rt_link_record
  93. {
  94. rt_uint8_t rx_seq; /* record the opposite sequence */
  95. rt_uint8_t total; /* the number of long frame number */
  96. rt_uint8_t long_count; /* long packet recv counter */
  97. rt_uint8_t *dataspace; /* the space of long frame */
  98. };
  99. struct rt_link_extend
  100. {
  101. rt_uint16_t attribute; /* rt_link_frame_attribute_t */
  102. rt_uint16_t parameter;
  103. };
  104. struct rt_link_frame
  105. {
  106. struct rt_link_frame_head head; /* frame head */
  107. struct rt_link_extend extend; /* frame extend data */
  108. rt_uint8_t *real_data; /* the origin data */
  109. rt_uint32_t crc; /* CRC result */
  110. rt_uint16_t data_len; /* the length of frame length */
  111. rt_uint16_t attribute; /* this will show frame attribute , rt_link_frame_attribute_t */
  112. rt_uint8_t index; /* the index frame for long frame */
  113. rt_uint8_t total; /* the total frame for long frame */
  114. rt_slist_t slist; /* the frame will hang on the send list on session */
  115. };
  116. struct rt_link_service
  117. {
  118. rt_err_t (*upload_callback)(void *data, rt_size_t size);
  119. };
  120. struct rt_link_session
  121. {
  122. rt_link_linkstatus_t link_status; /* Link connection status*/
  123. struct rt_event event; /* the event that core logic */
  124. struct rt_link_service channel[RT_LINK_SERVICE_MAX]; /* thansfer to app layer */
  125. rt_slist_t tx_data_slist;
  126. rt_uint8_t tx_seq; /* sequence for frame */
  127. struct rt_mutex tx_lock; /* protect send data interface, only one thread can hold it */
  128. struct rt_timer sendtimer; /* send function timer for rt link */
  129. struct rt_link_record rx_record; /* the memory of receive status */
  130. struct rt_timer recvtimer; /* receive a frame timer for rt link */
  131. struct rt_timer longframetimer; /* receive long frame timer for rt link */
  132. struct rt_link_receive_buffer *rx_buffer; /* the buffer will store data */
  133. rt_uint32_t (*calculate_crc)(rt_uint8_t using_buffer_ring, rt_uint8_t *data, rt_size_t size); /* this function will calculate crc */
  134. };
  135. /* rtlink init and deinit */
  136. int rt_link_init(void);
  137. rt_err_t rt_link_deinit(void);
  138. /* rtlink send data interface */
  139. rt_size_t rt_link_send(rt_link_service_t service, void *data, rt_size_t size);
  140. /* rtlink service attach and detach */
  141. rt_err_t rt_link_service_attach(rt_link_service_t service, rt_err_t (*function)(void *data, rt_size_t size));
  142. rt_err_t rt_link_service_detach(rt_link_service_t service);
  143. /* Private operator function */
  144. struct rt_link_session *rt_link_get_scb(void);
  145. #endif /* __RT_LINK_H__ */