tr_queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * queue.c
  3. *
  4. * Created on: 2025年8月20日
  5. * Author: 龙三郎
  6. */
  7. #include <tr_queue.h>
  8. #include <stdio.h>
  9. #include "tr_protocol.h"
  10. // 初始化队列
  11. void initializeQueue(Queue *q)
  12. {
  13. q->front = 0;
  14. q->rear = 0;
  15. }
  16. // 判断队列是否已满
  17. bool isFull(Queue *q)
  18. {
  19. return (q->rear + 1) % QUEUE_MAX_SIZE == q->front;
  20. }
  21. // 判断队列是否为空
  22. bool isEmpty(Queue *q)
  23. {
  24. return q->front == q->rear;
  25. }
  26. // 队列现有元素个数
  27. int currentItems(Queue *q)
  28. {
  29. if(q->rear >= q->front) {
  30. return q->rear - q->front;
  31. } else {
  32. return q->rear + QUEUE_MAX_SIZE - q->front + 1;
  33. }
  34. }
  35. // 队列剩余空间
  36. int remainSpace(Queue *q)
  37. {
  38. return QUEUE_MAX_SIZE - currentItems(q);
  39. }
  40. // 入队列,单个元素
  41. bool enqueue(Queue *q, unsigned char element)
  42. {
  43. if (isFull(q)) {
  44. return false; // 队列已满,无法添加新元素
  45. }
  46. q->items[q->rear] = element; // 将元素添加到队尾
  47. q->rear = (q->rear + 1) % QUEUE_MAX_SIZE; // 更新队尾指针,注意循环队列的处理方式
  48. return true; // 成功入队返回true
  49. }
  50. // 批量入队列
  51. bool enqueueBatch(Queue *q, unsigned char *elements, unsigned int elSize)
  52. {
  53. if(remainSpace(q) < elSize)
  54. {
  55. return false; // 队列空间不够,无法批量添加。
  56. }
  57. int i;
  58. for(i = 0; i < elSize; i++)
  59. {
  60. enqueue(q, *(elements + i));
  61. }
  62. return true;
  63. }
  64. // 出队列,单个元素
  65. bool dequeue(Queue *q, unsigned char *element)
  66. {
  67. if (isEmpty(q)) {
  68. return false; // 队列为空,无法移除元素
  69. }
  70. *element = q->items[q->front]; // 获取队首元素的值并传出参数(可选)
  71. q->front = (q->front + 1) % QUEUE_MAX_SIZE; // 更新队首指针,注意循环队列的处理方式
  72. return true; // 成功出队返回true
  73. }
  74. // 批量出队列
  75. bool dequeueBatch(Queue *q, unsigned short len, unsigned char *elements)
  76. {
  77. if (currentItems(q) < len) {
  78. return false; // 长度不够
  79. }
  80. int i;
  81. for(i = 0; i < len; i++)
  82. {
  83. dequeue(q, elements + i);
  84. }
  85. return true; // 成功出队返回true
  86. }
  87. // 查看队列中的元素,不出队列
  88. static bool queryQueue(Queue *q, unsigned short start, unsigned short len, unsigned char *element)
  89. {
  90. if (currentItems(q) < (start + len)) {
  91. return false; // 长度不够
  92. }
  93. int i;
  94. for(i = 0; i < len; i++)
  95. {
  96. int index = (q->front + start + i) % QUEUE_MAX_SIZE;
  97. *(element + i) = q->items[index]; // 获取队首元素的值并传出参数(可选)
  98. }
  99. return true; // 成功出队返回true
  100. }
  101. // 从队列中取出一个数据包。
  102. bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl)
  103. {
  104. while(true)
  105. {
  106. unsigned char pheader[3] = {0};
  107. if(!queryQueue(q, 0, 3, pheader))
  108. {
  109. return false;
  110. }
  111. if(pheader[0] == 'T' && pheader[1] == 'R' && pheader[2] == 0x01)
  112. {
  113. break;
  114. }
  115. else
  116. {
  117. dequeue(q, pheader);
  118. }
  119. }
  120. unsigned char plength[2] = {0};
  121. if(!queryQueue(q, 6, 2, plength))
  122. {
  123. return false;
  124. }
  125. unsigned short length = 0;
  126. length |= (plength[0] << 8) & 0xff00;
  127. length |= plength[1] & 0x00ff;
  128. // printf("length=%d\r\n", length);
  129. *dl = length;
  130. return dequeueBatch(q, length, d);
  131. }
  132. // 协议解析相关
  133. bool getVerFromProtocol(unsigned char *p, unsigned short pl, unsigned char *ver)
  134. {
  135. uint8_t version = ProtocolGetVersion(p, pl);
  136. if(!version)
  137. {
  138. return false;
  139. }
  140. *ver = version;
  141. return true;
  142. }
  143. // 获取目的地址
  144. bool getDaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *da)
  145. {
  146. uint8_t destAddress = ProtocolGetDestAddress(p, pl);
  147. if(!destAddress)
  148. {
  149. return false;
  150. }
  151. *da = destAddress;
  152. return true;
  153. }
  154. // 获取数据类型
  155. bool getTypeFromProtocol(unsigned char *p, unsigned short pl, unsigned char *type)
  156. {
  157. uint8_t _type = ProtocolGetType(p, pl);
  158. if(!_type)
  159. {
  160. return false;
  161. }
  162. *type = _type;
  163. return true;
  164. }
  165. // 获取来源地址
  166. bool getSaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *sa)
  167. {
  168. uint8_t sourceAddress = ProtocolGetSourceAddress(p, pl);
  169. if(!sourceAddress)
  170. {
  171. return false;
  172. }
  173. *sa = sourceAddress;
  174. return true;
  175. }
  176. // 获取源数据
  177. bool getPayloadFromProtocol(unsigned char *p, unsigned short pl, unsigned char *s, unsigned short *sl)
  178. {
  179. uint16_t payloadLength = ProtocolGetPayload(p, pl, s);
  180. *sl = payloadLength;
  181. if(*sl == 0)
  182. {
  183. return false;
  184. }
  185. return true;
  186. }
  187. // 转发业务相关
  188. // 初始化
  189. Queue queueList[QUEUE_NUM];
  190. void dataforwardInit(void)
  191. {
  192. int i;
  193. for(i = 0; i < QUEUE_NUM; i++)
  194. {
  195. initializeQueue(&queueList[i]);
  196. }
  197. }
  198. // 主流程
  199. static unsigned char dfmBuffer[BUFFER_SIZE_2K];
  200. static unsigned short dfmBufferLength = 0;
  201. void dataforwardMain(void)
  202. {
  203. int i;
  204. for(i = 0; i < QUEUE_NUM; i++)
  205. {
  206. while(dequeueBatchWithProtocol(&queueList[i], dfmBuffer, &dfmBufferLength))
  207. {
  208. unsigned char da, sa, type;
  209. if(!getDaFromProtocol(dfmBuffer, dfmBufferLength, &da)
  210. || !getSaFromProtocol(dfmBuffer, dfmBufferLength, &sa)
  211. || !getTypeFromProtocol(dfmBuffer, dfmBufferLength, &type))
  212. {
  213. continue;
  214. }
  215. if(da == THIS_ADDRESS)
  216. {
  217. dataNotForwardedHandle(dfmBuffer, dfmBufferLength, sa, type);
  218. }
  219. else
  220. {
  221. dataForwardedHandle(dfmBuffer, dfmBufferLength, da);
  222. }
  223. }
  224. }
  225. }
  226. // 将从usb发来的协议数据入队列
  227. bool enqueueFromUsb(unsigned char *s, unsigned short sl)
  228. {
  229. return enqueueBatch(&queueList[QUEUE_INDEX_USB], s, sl);
  230. }
  231. // 将从以太网发来的协议数据入队列
  232. bool enqueueFromEthernet(unsigned char *s, unsigned short sl)
  233. {
  234. return enqueueBatch(&queueList[QUEUE_INDEX_ETHERNET], s, sl);
  235. }
  236. // 将从uart发来的协议数据入队列
  237. bool enqueueFromUart(unsigned char c)
  238. {
  239. return enqueue(&queueList[QUEUE_INDEX_UART], c);
  240. }
  241. // 将从uart发来的协议数据入队列
  242. bool enqueueFromUartBatch(unsigned char *s, unsigned short sl)
  243. {
  244. return enqueueBatch(&queueList[QUEUE_INDEX_UART], s, sl);
  245. }
  246. // 从本模块发出的数据入队列,源数据。带协议格式
  247. bool enqueueFromHere(unsigned char *s, unsigned short sl, unsigned char da, unsigned char type)
  248. {
  249. return enqueueBatch(&queueList[QUEUE_INDEX_HEAR], s, sl);
  250. }