1414
1515#include <rtdef.h>
1616
17+ #define RT_LINK_VER "0.2.0"
18+
1719#define RT_LINK_AUTO_INIT
1820
19- #define RT_LINK_FRAME_HEAD 0x15
20- #define RT_LINK_FRAME_HEAD_MASK 0x1F
21- #define RT_LINK_MAX_DATA_LENGTH 2044 /*can exact divide by 4 bytes*/
22- #define RT_LINK_FRAMES_MAX 0x03 /* The maximum number of split frames for a long package*/
21+ #define RT_LINK_FLAG_ACK 0x01
22+ #define RT_LINK_FLAG_CRC 0x02
23+
24+ #define RT_LINK_FRAME_HEAD 0x15
25+ #define RT_LINK_FRAME_HEAD_MASK 0x1F
26+ /* The maximum number of split frames for a long package */
27+ #define RT_LINK_FRAMES_MAX 0x03
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 1024
2331
24- #define RT_LINK_ACK_MAX 0x07
32+ #define RT_LINK_ACK_MAX 0x07
2533#define RT_LINK_CRC_LENGTH 4
2634#define RT_LINK_HEAD_LENGTH 4
27- #define RT_LINK_MAX_EXTEND_LENGTH 4
28- #define RT_LINK_MAX_FRAME_LENGTH (RT_LINK_HEAD_LENGTH + RT_LINK_MAX_EXTEND_LENGTH + RT_LINK_MAX_DATA_LENGTH + RT_LINK_CRC_LENGTH)
29- #define RT_LINK_RECEIVE_BUFFER_LENGTH (RT_LINK_MAX_FRAME_LENGTH * RT_LINK_FRAMES_MAX + RT_LINK_HEAD_LENGTH + RT_LINK_MAX_EXTEND_LENGTH)
35+ #define RT_LINK_EXTEND_LENGTH 4
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)
3045
3146typedef enum
3247{
33- RT_LINK_SERVICE_RTLINK = 0 ,
34- RT_LINK_SERVICE_LINK_SOCKET = 1 ,
35- RT_LINK_SERVICE_LINK_WIFI = 2 ,
36- RT_LINK_SERVICE_LINK_MNGT = 3 ,
37- RT_LINK_SERVICE_LINK_MSHTOOLS = 4 ,
38- RT_LINK_SERVICE_MAX
39- } rt_link_service_t ;
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 ,
4053
41- enum
42- {
43- FRAME_EXTEND = 1 << 0 ,
44- FRAME_CRC = 1 << 1 ,
45- FRAME_ACK = 1 << 2
46- };
54+ /* Expandable to a maximum of 31 */
55+ RT_LINK_SERVICE_MAX
56+ } rt_link_service_e ;
4757
4858typedef enum
4959{
50- RT_LINK_RESERVE_FRAME = 0 ,
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 */
5166
52- RT_LINK_RESEND_FRAME ,
53- RT_LINK_CONFIRM_FRAME ,
54- RT_LINK_SHORT_DATA_FRAME ,
55- RT_LINK_LONG_DATA_FRAME ,
56- RT_LINK_SESSION_END , /* The retring failed to end the session */
67+ RT_LINK_LONG_DATA_FRAME = 5 ,
68+ RT_LINK_SHORT_DATA_FRAME = 6 ,
5769
58- RT_LINK_HANDSHAKE_FRAME
59- } rt_link_frame_attribute_t ;
70+ RT_LINK_RESERVE_FRAME = 7
71+ } rt_link_frame_attr_e ;
6072
6173typedef enum
6274{
6375 /* receive event */
64- RT_LINK_READ_CHECK_EVENT = 1 << 0 ,
65- RT_LINK_RECV_TIMEOUT_FRAME_EVENT = 1 << 1 ,
66- RT_LINK_RECV_TIMEOUT_LONG_EVENT = 1 << 2 ,
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 ,
6779
6880 /* send event */
6981 RT_LINK_SEND_READY_EVENT = 1 << 4 ,
7082 RT_LINK_SEND_OK_EVENT = 1 << 5 ,
7183 RT_LINK_SEND_FAILED_EVENT = 1 << 6 ,
7284 RT_LINK_SEND_TIMEOUT_EVENT = 1 << 7
73- } rt_link_notice_t ;
85+ } rt_link_notice_e ;
7486
7587typedef enum
7688{
77- RT_LINK_ESTABLISHING = 0 ,
78- RT_LINK_NO_RESPONSE ,
79- RT_LINK_CONNECT_DONE ,
80- } rt_link_linkstatus_t ;
89+ RT_LINK_INIT = 0 ,
90+ RT_LINK_DISCONN = 1 ,
91+ RT_LINK_CONNECT = 2 ,
92+ } rt_link_linkstate_e ;
8193
8294typedef enum
8395{
84- RECVTIMER_NONE = 0 ,
85- RECVTIMER_FRAME ,
86- RECVTIMER_LONGFRAME
87- } rt_link_recvtimer_status_t ;
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 ;
88108
89109struct rt_link_receive_buffer
90110{
91- rt_uint8_t data [RT_LINK_RECEIVE_BUFFER_LENGTH ]; /* rt-link receive data buffer */
111+ /* rt-link receive data buffer */
112+ rt_uint8_t data [RT_LINK_RECEIVE_BUFFER_LENGTH ];
92113 rt_uint8_t * read_point ;
93114 rt_uint8_t * write_point ;
94115 rt_uint8_t * end_point ;
@@ -100,74 +121,88 @@ struct rt_link_frame_head
100121 rt_uint8_t extend : 1 ;
101122 rt_uint8_t crc : 1 ;
102123 rt_uint8_t ack : 1 ;
124+
103125 rt_uint8_t sequence ;
104- rt_uint16_t channel : 5 ;
105- rt_uint16_t length : 11 ;
126+ rt_uint16_t service : 5 ;
127+ rt_uint16_t length : 11 ; /* range 0~2047 */
106128};
107129
108130/* record frame information that opposite */
109131struct rt_link_record
110132{
111- rt_uint8_t rx_seq ; /* record the opposite sequence */
133+ rt_uint8_t rx_seq ; /* record the opposite sequence */
112134 rt_uint8_t total ; /* the number of long frame number */
113135 rt_uint8_t long_count ; /* long packet recv counter */
114136 rt_uint8_t * dataspace ; /* the space of long frame */
115137};
116138
117139struct rt_link_extend
118140{
119- rt_uint16_t attribute ; /* rt_link_frame_attribute_t */
141+ rt_uint16_t attribute ; /* rt_link_frame_attr_e */
120142 rt_uint16_t parameter ;
121143};
122144
123145struct rt_link_frame
124146{
125- struct rt_link_frame_head head ; /* frame head */
126- struct rt_link_extend extend ; /* frame extend data */
127- rt_uint8_t * real_data ; /* the origin data */
128- rt_uint32_t crc ; /* CRC result */
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 */
129151
130- rt_uint16_t data_len ; /* the length of frame length */
131- rt_uint16_t attribute ; /* this will show frame attribute , rt_link_frame_attribute_t */
152+ rt_uint16_t data_len ; /* the length of frame length */
153+ rt_uint16_t attribute ; /* rt_link_frame_attr_e */
132154
133- rt_uint8_t index ; /* the index frame for long frame */
134- rt_uint8_t total ; /* the total frame for long frame */
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 */
135158
136- rt_slist_t slist ; /* the frame will hang on the send list on session */
159+ rt_slist_t slist ; /* the frame will hang on the send list on session */
137160};
138161
139162struct rt_link_service
140163{
141- rt_err_t (* upload_callback )(void * data , rt_size_t size );
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 ;
142173};
143174
144175struct rt_link_session
145176{
146- rt_link_linkstatus_t link_status ; /* Link connection status*/
147- struct rt_event event ; /* the event that core logic */
148- struct rt_link_service channel [RT_LINK_SERVICE_MAX ]; /* thansfer to app layer */
177+ struct rt_event event ;
178+ struct rt_link_service * service [RT_LINK_SERVICE_MAX ];
149179
180+ rt_uint8_t tx_seq ; /* Sequence number of the send data frame */
150181 rt_slist_t tx_data_slist ;
151- rt_uint8_t tx_seq ; /* sequence for frame */
152- struct rt_mutex tx_lock ; /* protect send data interface, only one thread can hold it */
153- struct rt_timer sendtimer ; /* send function timer for rt link */
182+ rt_uint8_t sendbuffer [ RT_LINK_MAX_FRAME_LENGTH ];
183+ struct rt_event sendevent ;
184+ struct rt_timer sendtimer ;
154185
155- struct rt_link_record rx_record ; /* the memory of receive status */
156- struct rt_timer recvtimer ; /* receive a frame timer for rt link */
157- struct rt_timer longframetimer ; /* receive long frame timer for rt link */
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 */
158189
159- struct rt_link_receive_buffer * rx_buffer ; /* the buffer will store data */
160- rt_uint32_t (* calculate_crc )(rt_uint8_t using_buffer_ring , rt_uint8_t * data , rt_size_t size ); /* this function will calculate crc */
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 */
161193};
162194
163- /* rtlink init and deinit */
195+ #define SERV_ERR_GET (service ) (service->err)
196+
197+ /* rtlink init and deinit, default is automatic initialization*/
164198int rt_link_init (void );
165199rt_err_t rt_link_deinit (void );
166- /* rtlink send data interface */
167- rt_size_t rt_link_send (rt_link_service_t service , void * data , rt_size_t size );
200+
201+ rt_size_t rt_link_send (struct rt_link_service * service , const void * data , rt_size_t size );
202+
168203/* rtlink service attach and detach */
169- rt_err_t rt_link_service_attach (rt_link_service_t service , rt_err_t ( * function )( void * data , rt_size_t size ) );
170- rt_err_t rt_link_service_detach (rt_link_service_t service );
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 );
171206
172207/* Private operator function */
173208struct rt_link_session * rt_link_get_scb (void );
0 commit comments