protocol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: protocol.c
  4. 文件版本: 01.01
  5. 创建作者: xxxxxx
  6. 创建日期: 2015-5-5
  7. 功能说明: 规约层实现文件
  8. -------------------
  9. - APP层 -
  10. -------------------
  11. - 规约层 -
  12. -------------------
  13. - 通道层 -
  14. -------------------
  15. - 驱动层 -
  16. -------------------
  17. 其它说明:
  18. 修改记录:
  19. */
  20. #include "head.h"
  21. #include "myconf.h"
  22. #include "channel.h"
  23. #include "protocol.h"
  24. // 一个帧的缓冲区大小
  25. #define FRM_BUF_SIZE 256
  26. // 帧缓冲区个数
  27. #define MAX_BUF_SIZE 8
  28. // 1秒计数值
  29. #define PROTOCOL_TIMEOUT 1 * USTIMER_SEC
  30. // 缓冲区数据结构
  31. struct protocol_buf
  32. {
  33. unsigned char cTypeCounter;
  34. unsigned char cLenth; // 长度
  35. unsigned char cCnt;
  36. unsigned char src_addr;
  37. unsigned char dst_addr;
  38. unsigned char sum;
  39. unsigned char arrBuf[FRM_BUF_SIZE];
  40. };
  41. // 全局信息
  42. struct protocol_info
  43. {
  44. struct protocol_buf txbuf[MAX_BUF_SIZE];
  45. struct protocol_buf rxbuf[MAX_BUF_SIZE];
  46. unsigned char tx_head;
  47. unsigned char tx_tail;
  48. unsigned char rx_head;
  49. unsigned char rx_tail;
  50. uint32_t tm; // 接收超时参照时间点, 如果超过1秒没有数据,则认为帧结束
  51. T_channel *p_chan;
  52. int recvOK; // 在半双工的情况下,recvOK=1表示接受一个完整的数据帧,recvOK=0表示没有收到完整的帧或数据
  53. };
  54. struct protocol_info g_info[PROTOCOL_CHNS];
  55. static int g_chan_cnt = 0; // 已经使用的通道数
  56. /******************************************************************************
  57. 函数名称: protocol_reset
  58. 函数版本: 01.01
  59. 创建作者: xxxxxx
  60. 创建日期: 2015-05-05
  61. 函数说明: 解包复位处理
  62. 参数说明:
  63. p_info:全局信息指针
  64. 返回值: 无
  65. 修改记录:
  66. */
  67. static void protocol_reset(struct protocol_info *p_info)
  68. {
  69. p_info->rxbuf[p_info->rx_head].cTypeCounter = 0;
  70. p_info->rxbuf[p_info->rx_head].cCnt = 0;
  71. p_info->rxbuf[p_info->rx_head].sum = 0;
  72. p_info->recvOK = 1; // 接收完成数据
  73. }
  74. /******************************************************************************
  75. 函数名称: protocol_unpacket
  76. 函数版本: 01.01
  77. 创建作者: xxxxxx
  78. 创建日期: 2015-05-05
  79. 函数说明: 解包处理
  80. 参数说明:
  81. p_info:全局信息指针
  82. byRevData: 接收的数据
  83. 返回值: 无
  84. 修改记录:
  85. */
  86. static void protocol_unpacket(struct protocol_info *p_info, unsigned char byRevData)
  87. {
  88. // 将数据放到缓冲区
  89. p_info->rxbuf[p_info->rx_head].arrBuf[p_info->rxbuf[p_info->rx_head].cCnt++] = byRevData;
  90. // 逐步检查帧的合法性: 启动字符(0x68)+长度+长度+启动字符(0x68)+数据+校验和+结束字符
  91. switch (p_info->rxbuf[p_info->rx_head].cTypeCounter)
  92. {
  93. case 0: // 检查帧头.第一个启动字符
  94. if (byRevData == 0x68)
  95. {
  96. p_info->rxbuf[p_info->rx_head].cTypeCounter = 1;
  97. }
  98. else
  99. {
  100. // 解包复位
  101. protocol_reset(p_info);
  102. }
  103. break;
  104. case 1: // 第一个长度
  105. p_info->rxbuf[p_info->rx_head].cTypeCounter = 2;
  106. p_info->rxbuf[p_info->rx_head].cLenth = byRevData;
  107. break;
  108. case 2: // 第二个长度
  109. if (p_info->rxbuf[p_info->rx_head].cLenth == byRevData)
  110. {
  111. p_info->rxbuf[p_info->rx_head].cTypeCounter = 3;
  112. p_info->rxbuf[p_info->rx_head].cLenth = byRevData + 4;
  113. }
  114. else
  115. {
  116. // 解包复位
  117. protocol_reset(p_info);
  118. }
  119. break;
  120. case 3: // 第二个启动字符
  121. if (byRevData == 0x68)
  122. {
  123. p_info->rxbuf[p_info->rx_head].cTypeCounter = 4;
  124. p_info->rxbuf[p_info->rx_head].sum = 0;
  125. }
  126. else
  127. {
  128. // 解包复位
  129. protocol_reset(p_info);
  130. }
  131. break;
  132. case 4: // 计算校验和,如果数据读取完毕,下一步进行校验和比较
  133. p_info->rxbuf[p_info->rx_head].sum += byRevData;
  134. if (p_info->rxbuf[p_info->rx_head].cCnt >= p_info->rxbuf[p_info->rx_head].cLenth)
  135. {
  136. p_info->rxbuf[p_info->rx_head].cTypeCounter = 5;
  137. }
  138. break;
  139. case 5: // 比较校验和
  140. if (p_info->rxbuf[p_info->rx_head].sum == byRevData)
  141. {
  142. p_info->rxbuf[p_info->rx_head].cTypeCounter = 6;
  143. }
  144. else
  145. {
  146. // 解包复位
  147. protocol_reset(p_info);
  148. }
  149. break;
  150. case 6:
  151. // 缓冲区未满
  152. if (((p_info->rx_head + 1) & (MAX_BUF_SIZE - 1)) != p_info->rx_tail)
  153. {
  154. // 调整缓冲区下标
  155. p_info->rx_head = (p_info->rx_head + 1) & (MAX_BUF_SIZE - 1);
  156. }
  157. // else
  158. {
  159. // 缓冲区满,丢弃该帧数据
  160. // 解包复位
  161. protocol_reset(p_info);
  162. }
  163. // 唤醒主循环
  164. mainloop_wakeup();
  165. break;
  166. default:
  167. // 解包复位
  168. protocol_reset(p_info);
  169. break;
  170. }
  171. }
  172. /******************************************************************************
  173. 函数名称: protocol_init
  174. 函数版本: 01.01
  175. 创建作者: xxxxxx
  176. 创建日期: 2015-05-05
  177. 函数说明: 初始化协议
  178. 参数说明:
  179. 无
  180. 返回值: 无
  181. 修改记录:
  182. */
  183. void protocol_init(void)
  184. {
  185. int i = 0;
  186. g_chan_cnt = 0;
  187. memset(g_info, 0, sizeof(g_info));
  188. // 初始化假设已经接收到数据
  189. for (i = 0; i < PROTOCOL_CHNS; i++)
  190. {
  191. g_info[i].recvOK = 1;
  192. }
  193. }
  194. /******************************************************************************
  195. 函数名称: protocol_packet
  196. 函数版本: 01.01
  197. 创建作者: xxxxxx
  198. 创建日期: 2015-05-05
  199. 函数说明: 协议组包函数
  200. 参数说明:
  201. src: 源数据
  202. count: 源数据长度
  203. srcaddr: 源地址
  204. dstaddr: 目标地址
  205. dst: 组包后的数据
  206. 返回值: 返回组包后的长度
  207. 修改记录:
  208. */
  209. int protocol_packet(unsigned char *src, int count, unsigned char srcaddr, unsigned char dstaddr, unsigned char *dst)
  210. {
  211. unsigned char i = 0;
  212. unsigned char checksum = 0;
  213. unsigned char len = (unsigned char)count;
  214. unsigned char *p = dst;
  215. *p++ = 0x68;
  216. *p++ = len;
  217. *p++ = len;
  218. *p++ = 0x68;
  219. memcpy(p, src, count);
  220. p = &dst[4];
  221. for (i = 0; i < len; i++)
  222. {
  223. checksum += *p++;
  224. }
  225. *p++ = checksum;
  226. *p = 0x80 | (srcaddr << 3) | dstaddr;
  227. return (int)(len + 6);
  228. }
  229. /******************************************************************************
  230. 函数名称: protocol_add_chan
  231. 函数版本: 01.01
  232. 创建作者: xxxxxx
  233. 创建日期: 2015-05-05
  234. 函数说明: 绑定一个通道.每个通道都可以使用该规约
  235. 参数说明:
  236. h_obj: 通道指针
  237. 返回值: 成功返回0, 失败返回-1.
  238. 修改记录:
  239. */
  240. int protocol_add_chan(T_channel *h_obj)
  241. {
  242. // 检查通道是否已经用完
  243. if (g_chan_cnt >= PROTOCOL_CHNS)
  244. {
  245. return -1;
  246. }
  247. // 通道指针
  248. g_info[g_chan_cnt].p_chan = h_obj;
  249. // 通道号
  250. g_info[g_chan_cnt].p_chan->no = g_chan_cnt;
  251. // 通道计数
  252. g_chan_cnt++;
  253. return 0;
  254. }
  255. /******************************************************************************
  256. 函数名称: protocol_get_oneframe
  257. 函数版本: 01.01
  258. 创建作者: xxxxxx
  259. 创建日期: 2015-05-05
  260. 函数说明: 从通道中获取一帧数据
  261. 参数说明:
  262. h_obj: 通道指针
  263. frm_data: 帧数据
  264. 返回值: 成功返回0, 失败返回负数.
  265. 修改记录:
  266. */
  267. int protocol_get_oneframe(T_channel *h_obj, unsigned char *frm_data)
  268. {
  269. struct protocol_info *p_info = &g_info[h_obj->no];
  270. // 检查参数的合法性
  271. if ((!h_obj) || (!frm_data))
  272. {
  273. return -1;
  274. }
  275. // 接收缓冲区没有数据
  276. if (p_info->rx_head == p_info->rx_tail)
  277. {
  278. return -2;
  279. }
  280. // 拷贝数据
  281. memcpy(frm_data, p_info->rxbuf[p_info->rx_tail].arrBuf, p_info->rxbuf[p_info->rx_tail].arrBuf[1] + 6);
  282. p_info->rx_tail = (p_info->rx_tail + 1) & (MAX_BUF_SIZE - 1);
  283. return 0;
  284. }
  285. /******************************************************************************
  286. 函数名称: protocol_put_oneframe
  287. 函数版本: 01.01
  288. 创建作者: xxxxxx
  289. 创建日期: 2015-05-05
  290. 函数说明: 写一帧数据到通道中
  291. 参数说明:
  292. h_obj: 通道指针
  293. frm_data: 帧数据
  294. count: 帧数据长度
  295. 返回值: 成功返回0, 失败返回负数.
  296. 修改记录:
  297. */
  298. int protocol_put_oneframe(T_channel *h_obj, unsigned char *frm_data, int count)
  299. {
  300. struct protocol_info *p_info = &g_info[h_obj->no];
  301. // 检查参数的合法性
  302. if ((!h_obj) || (!frm_data) || (count == 0))
  303. {
  304. return -1;
  305. }
  306. // 缓冲去满
  307. if (((p_info->tx_head + 1) & (MAX_BUF_SIZE - 1)) == p_info->tx_tail)
  308. {
  309. return -2;
  310. }
  311. // 拷贝数据到发送缓冲区
  312. memcpy(p_info->txbuf[p_info->tx_head].arrBuf, frm_data, count);
  313. // 要发送的字节总数
  314. p_info->txbuf[p_info->tx_head].cLenth = count;
  315. // 已经发送的字节数
  316. p_info->txbuf[p_info->tx_head].cCnt = 0;
  317. // 调整发送缓冲区的下标
  318. p_info->tx_head = (p_info->tx_head + 1) & (MAX_BUF_SIZE - 1);
  319. return 0;
  320. }
  321. /******************************************************************************
  322. 函数名称: protocol_unpacket_task
  323. 函数版本: 01.01
  324. 创建作者: xxxxxx
  325. 创建日期: 2015-05-05
  326. 函数说明: 规约处理任务
  327. 参数说明:
  328. 无
  329. 返回值: 无
  330. 修改记录:
  331. */
  332. void protocol_proc_task(s8 chnl)
  333. {
  334. int i = 0;
  335. int ret = 0;
  336. char ch = 0;
  337. unsigned char cLenth = 0; // 长度
  338. unsigned char cCnt = 0;
  339. unsigned char *p = NULL;
  340. for (i = 0; i < g_chan_cnt; i++)
  341. {
  342. // 从通道读取数据
  343. ret = channel_read(g_info[i].p_chan, &ch, 1);
  344. // 有数据
  345. if (ret > 0)
  346. {
  347. // 解包
  348. protocol_unpacket(&g_info[i], (unsigned char)ch);
  349. // 接收超时参考点
  350. g_info[i].tm = ustimer_get_origin();
  351. }
  352. else
  353. {
  354. // 超时无数据
  355. if (ustimer_get_duration(g_info[i].tm) >= PROTOCOL_TIMEOUT)
  356. {
  357. // 解包复位
  358. protocol_reset(&g_info[i]);
  359. // 接收超时参考点
  360. g_info[i].tm = ustimer_get_origin();
  361. }
  362. }
  363. // 发送缓冲区有帧数据
  364. if (g_info[i].tx_head != g_info[i].tx_tail)
  365. {
  366. if (g_info[i].p_chan->halfduplex)
  367. {
  368. if (!g_info[i].recvOK)
  369. {
  370. continue;
  371. }
  372. }
  373. // 数据长度
  374. cLenth = g_info[i].txbuf[g_info[i].tx_tail].cLenth;
  375. // 已经发送的字节数
  376. cCnt = g_info[i].txbuf[g_info[i].tx_tail].cCnt;
  377. // 要发送的数据首地址
  378. p = &g_info[i].txbuf[g_info[i].tx_tail].arrBuf[cCnt];
  379. // 本次需要发送的字节数
  380. cCnt = cLenth - cCnt;
  381. // 还有数据要发送
  382. if (cCnt > 0)
  383. {
  384. // 在发送第一个字节前,将485发送允许
  385. if (g_info[i].txbuf[g_info[i].tx_tail].cCnt == 0)
  386. {
  387. RS_Send_Enable(chnl);
  388. }
  389. // 发送数据
  390. ret = channel_write(g_info[i].p_chan, (const char *)p, cCnt);
  391. // 发送出去了
  392. if (ret > 0)
  393. {
  394. // 计算发送了多少字节
  395. g_info[i].txbuf[g_info[i].tx_tail].cCnt += (unsigned char)ret;
  396. #if 0
  397. //如果数据发送完毕,则调整发送缓冲区的下标
  398. if(g_info[i].txbuf[g_info[i].tx_tail].cCnt == g_info[i].txbuf[g_info[i].tx_tail].cLenth)
  399. {
  400. //调整发送缓冲区的下标
  401. g_info[i].tx_tail = (g_info[i].tx_tail+1) & (MAX_BUF_SIZE - 1);
  402. g_info[i].recvOK=0; //置没有接收数据
  403. }
  404. #endif
  405. }
  406. }
  407. // 如果数据发送完毕,则调整发送缓冲区的下标
  408. else
  409. {
  410. RS_Recv_Enable(chnl);
  411. // 调整发送缓冲区的下标
  412. g_info[i].tx_tail = (g_info[i].tx_tail + 1) & (MAX_BUF_SIZE - 1);
  413. g_info[i].recvOK = 0; // 置没有接收数据
  414. }
  415. }
  416. }
  417. }