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