1
0

zcore.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * File : rz.c
  3. * the core functions of implementing zmodem protocol
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2011-03-29 itspy
  7. */
  8. #include <rtthread.h>
  9. #include <finsh.h>
  10. #include <shell.h>
  11. #include <rtdef.h>
  12. #include <dfs.h>
  13. #include <dfs_file.h>
  14. #include <dfs_posix.h>
  15. #include <stdio.h>
  16. #include "zdef.h"
  17. char ZF0_CMD; /* file conversion request */
  18. char ZF1_CMD; /* file management request */
  19. char ZF2_CMD; /* file transport request */
  20. char ZF3_CMD;
  21. rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */
  22. rt_uint16_t Rxcount; /* received count*/
  23. char header_type; /* header type */
  24. rt_uint8_t rx_header[4]; /* received header */
  25. rt_uint8_t tx_header[4]; /* transmitted header */
  26. rt_uint32_t Rxpos; /* received file position */
  27. rt_uint32_t Txpos; /* transmitted file position */
  28. rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */
  29. rt_uint8_t TxCRC; /* controls 32 bit CRC being sent */
  30. rt_uint8_t RxCRC; /* indicates/controls 32 bit CRC being received */
  31. /* 0 == CRC16, 1 == CRC32, 2 == CRC32 + RLE */
  32. char Attn[ZATTNLEN+1]; /* attention string rx sends to tx on err */
  33. void zinit_parameter(void);
  34. void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr);
  35. void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr);
  36. void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend);
  37. static rt_int16_t zrec_data16(rt_uint8_t *buf, rt_uint16_t len);
  38. static rt_int16_t zrec_data32(rt_uint8_t *buf, rt_int16_t len);
  39. static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len);
  40. rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len);
  41. rt_int16_t zget_header(rt_uint8_t *hdr);
  42. static rt_int16_t zget_bin_header(rt_uint8_t *hdr);
  43. static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr);
  44. rt_int16_t zget_hex_header(rt_uint8_t *hdr);
  45. static void zsend_ascii(rt_uint8_t c);
  46. void zsend_zdle_char(rt_uint16_t ch);
  47. static rt_int16_t zget_hex(void);
  48. rt_int16_t zread_byte(void);
  49. rt_int16_t zxor_read(void);
  50. void zput_pos(rt_uint32_t pos);
  51. void zget_pos(rt_uint32_t pos);
  52. void zinit_parameter(void)
  53. {
  54. rt_uint8_t i;
  55. ZF0_CMD = CANFC32|CANFDX|CANOVIO; /* not chose CANFC32,CANRLE,although it have been supported */
  56. ZF1_CMD = 0; /* fix header length,not support CANVHDR */
  57. ZF2_CMD = 0;
  58. ZF3_CMD = 0;
  59. Rxframeind =0;
  60. header_type = 0;
  61. Rxcount = 0;
  62. for (i=0;i<4;i++) rx_header[i] = tx_header[i] = 0;
  63. Rxpos = Txpos = 0;
  64. RxCRC = 0;
  65. Txfcs32 = 0;
  66. return ;
  67. }
  68. /* send binary header */
  69. void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr)
  70. {
  71. rt_uint8_t i;
  72. rt_uint32_t crc;
  73. zsend_byte(ZPAD);
  74. zsend_byte(ZDLE);
  75. TxCRC = Txfcs32;
  76. if (TxCRC == 0)
  77. {
  78. zsend_byte(ZBIN);
  79. zsend_zdle_char(type);
  80. /* add 16bits crc */
  81. crc = 0L;
  82. crc = updcrc16(type, 0);
  83. for (i=0;i<4;i++)
  84. {
  85. zsend_zdle_char(*hdr);
  86. crc = updcrc16((0377 & *hdr++),crc);
  87. }
  88. crc = updcrc16(0,updcrc16(0,crc));
  89. zsend_zdle_char(((int)(crc>>8)));
  90. zsend_zdle_char(crc);
  91. }
  92. else if(TxCRC == 1)
  93. {
  94. zsend_byte(ZBIN32);
  95. zsend_zdle_char(type);
  96. /* add 32bits crc */
  97. crc = 0xffffffffL;
  98. crc = updcrc32(type, crc);
  99. for (i=0;i<4;i++)
  100. {
  101. zsend_zdle_char(*hdr);
  102. crc = updcrc32((0377 & *hdr++), crc);
  103. }
  104. crc = ~crc;
  105. for (i=0; i<4;i++)
  106. {
  107. zsend_zdle_char(crc);
  108. crc >>= 8;
  109. }
  110. }
  111. else if (TxCRC == 2)
  112. {
  113. zsend_byte(ZBINR32);
  114. zsend_zdle_char(type);
  115. /* add 32bits crc */
  116. crc = 0xffffffffL;
  117. crc = updcrc32(type, crc);
  118. for (i=0;i<4;i++)
  119. {
  120. zsend_zdle_char(*hdr);
  121. crc = updcrc32((0377 & *hdr++), crc);
  122. }
  123. crc = ~crc;
  124. for (i=0; i<4;i++)
  125. {
  126. zsend_zdle_char(crc);
  127. crc >>= 8;
  128. }
  129. }
  130. return;
  131. }
  132. /* send hex header */
  133. void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr)
  134. {
  135. rt_uint8_t i;
  136. rt_uint16_t crc;
  137. zsend_line(ZPAD); zsend_line(ZPAD); zsend_line(ZDLE);
  138. zsend_line(ZHEX);
  139. zsend_ascii(type);
  140. crc = updcrc16(type, 0);
  141. for (i=0; i<4; i++)
  142. {
  143. zsend_ascii(*hdr);
  144. crc = updcrc16((0377 & *hdr++), crc);
  145. }
  146. crc = updcrc16(0,updcrc16(0,crc));
  147. zsend_ascii(crc>>8);
  148. zsend_ascii(crc);
  149. /* send display control cmd */
  150. zsend_line(015); zsend_line(0212);
  151. if (type != ZFIN && type != ZACK)
  152. zsend_line(021);
  153. TxCRC = 0; /* clear tx crc type */
  154. return;
  155. }
  156. /* send binary data,with frameend */
  157. void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend)
  158. {
  159. rt_int16_t i,c,tmp;
  160. rt_uint32_t crc;
  161. if (TxCRC == 0) /* send binary data with 16bits crc check */
  162. {
  163. crc = 0x0L;
  164. for (i=0;i<len;i++)
  165. {
  166. zsend_zdle_char(*buf);
  167. crc = updcrc16((0377 & *buf++), crc);
  168. }
  169. zsend_byte(ZDLE); zsend_byte(frameend);
  170. crc = updcrc16(frameend, crc);
  171. crc = updcrc16(0,updcrc16(0,crc));
  172. zsend_zdle_char(crc>>8);
  173. zsend_zdle_char(crc);
  174. }
  175. else if (TxCRC == 1) /* send binary data with 32 bits crc check */
  176. {
  177. crc = 0xffffffffL;
  178. for (i=0;i<len;i++)
  179. {
  180. c = *buf++ & 0377;
  181. zsend_zdle_char(c);
  182. crc = updcrc32(c, crc);
  183. }
  184. zsend_byte(ZDLE); zsend_byte(frameend);
  185. crc = updcrc32(frameend, crc);
  186. crc = ~crc;
  187. for (i=0;i<4;i++)
  188. {
  189. zsend_zdle_char((int)crc); crc >>= 8;
  190. }
  191. }
  192. else if (TxCRC == 2) /* send binary data with 32bits crc check,RLE encode */
  193. {
  194. crc = 0xffffffffL;
  195. tmp = *buf++ & 0377;
  196. for (i = 0; --len >= 0; ++buf)
  197. {
  198. if ((c = *buf & 0377) == tmp && i < 126 && len>0)
  199. {
  200. ++i; continue;
  201. }
  202. if (i==0)
  203. {
  204. zsend_zdle_char(tmp);
  205. crc = updcrc32(tmp, crc);
  206. if (tmp == ZRESC)
  207. {
  208. zsend_zdle_char(0100); crc = updcrc32(0100, crc);
  209. }
  210. tmp = c;
  211. }
  212. else if (i == 1)
  213. {
  214. if (tmp != ZRESC)
  215. {
  216. zsend_zdle_char(tmp); zsend_zdle_char(tmp);
  217. crc = updcrc32(tmp, crc);
  218. crc = updcrc32(tmp, crc);
  219. i = 0; tmp = c;
  220. }
  221. }
  222. else
  223. {
  224. zsend_zdle_char(ZRESC); crc = updcrc32(ZRESC, crc);
  225. if (tmp == 040 && i < 34)
  226. {
  227. i += 036;
  228. zsend_zdle_char(i);
  229. crc = updcrc32(i, crc);
  230. }
  231. else
  232. {
  233. i += 0101;
  234. zsend_zdle_char(i); crc = updcrc32(i, crc);
  235. zsend_zdle_char(tmp); crc = updcrc32(tmp, crc);
  236. }
  237. i = 0; tmp = c;
  238. }
  239. }
  240. zsend_byte(ZDLE); zsend_byte(frameend);
  241. crc = updcrc32(frameend, crc);
  242. crc = ~crc;
  243. for (i=0;i<4;i++)
  244. {
  245. zsend_zdle_char(crc);
  246. crc >>= 8;
  247. }
  248. }
  249. if (frameend == ZCRCW)
  250. zsend_byte(XON);
  251. return;
  252. }
  253. /* receive data,with 16bits CRC check */
  254. static rt_int16_t zrec_data16(rt_uint8_t *buf, rt_uint16_t len)
  255. {
  256. rt_int16_t c,crc_cnt;
  257. rt_uint16_t crc;
  258. rt_err_t res = -RT_ERROR;
  259. rt_uint8_t *p,flag = 0;
  260. p = buf;
  261. crc_cnt = 0; crc = 0L;
  262. Rxcount = 0;
  263. while(buf <= p+len)
  264. {
  265. if ((res = zread_byte()) & ~0377)
  266. {
  267. if (res == GOTCRCE || res == GOTCRCG ||
  268. res == GOTCRCQ || res == GOTCRCW)
  269. {
  270. c = res;
  271. c = res;
  272. crc = updcrc16(res&0377, crc);
  273. flag = 1;
  274. continue;
  275. }
  276. else if (res == GOTCAN) return ZCAN;
  277. else if (res == TIMEOUT) return TIMEOUT;
  278. else return res;
  279. }
  280. else
  281. {
  282. if (flag)
  283. {
  284. crc = updcrc16(res, crc);
  285. crc_cnt++;
  286. if (crc_cnt < 2) continue;
  287. if ((crc & 0xffff))
  288. {
  289. #ifdef ZDEBUG
  290. rt_kprintf("error code: CRC16 error \r\n");
  291. #endif
  292. return -RT_ERROR;
  293. }
  294. return c;
  295. }
  296. else
  297. {
  298. *buf++ = res;
  299. Rxcount++;
  300. crc = updcrc16(res, crc);
  301. }
  302. }
  303. }
  304. return -RT_ERROR;
  305. }
  306. /* receive data,with 32bits CRC check */
  307. static rt_int16_t zrec_data32(rt_uint8_t *buf, rt_int16_t len)
  308. {
  309. rt_int16_t c,crc_cnt;
  310. rt_uint32_t crc;
  311. rt_err_t res = -RT_ERROR;
  312. rt_uint8_t *p,flag = 0;
  313. crc_cnt = 0; crc = 0xffffffffL;
  314. Rxcount = 0;
  315. while (buf <= p+len)
  316. {
  317. if ((res = zread_byte()) & ~0377)
  318. {
  319. if (res == GOTCRCE || res == GOTCRCG ||
  320. res == GOTCRCQ || res == GOTCRCW)
  321. {
  322. c = res;
  323. crc = updcrc32(res&0377, crc);
  324. flag = 1;
  325. continue;
  326. }
  327. else if (res == GOTCAN) return ZCAN;
  328. else if (res == TIMEOUT) return TIMEOUT;
  329. else return res;
  330. }
  331. else
  332. {
  333. if (flag)
  334. {
  335. crc = updcrc32(res, crc);
  336. crc_cnt++;
  337. if (crc_cnt < 4) continue;
  338. if ((crc & 0xDEBB20E3))
  339. {
  340. #ifdef ZDEBUG
  341. rt_kprintf("error code: CRC32 error \r\n");
  342. #endif
  343. return -RT_ERROR;
  344. }
  345. return c;
  346. }
  347. else
  348. {
  349. *buf++ = res;
  350. Rxcount++;
  351. crc = updcrc32(res, crc);
  352. }
  353. }
  354. }
  355. return -RT_ERROR;
  356. }
  357. /* receive data,with RLE encoded,32bits CRC check */
  358. static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len)
  359. {
  360. rt_int16_t c,crc_cnt;
  361. rt_uint32_t crc;
  362. rt_err_t res = -RT_ERROR;
  363. rt_uint8_t *p,flag = 0;
  364. crc_cnt = 0; crc = 0xffffffffL;
  365. Rxcount = 0;
  366. p = buf;
  367. while (buf <= p+len)
  368. {
  369. if ((res = zread_byte()) & ~0377)
  370. {
  371. if (res == GOTCRCE || res == GOTCRCG ||
  372. res == GOTCRCQ || res == GOTCRCW)
  373. {
  374. c = res;
  375. crc = updcrc32(res&0377, crc);
  376. flag = 1;
  377. continue;
  378. }
  379. else if (res == GOTCAN) return ZCAN;
  380. else if (res == TIMEOUT) return TIMEOUT;
  381. else return res;
  382. }
  383. else
  384. {
  385. if (flag)
  386. {
  387. crc = updcrc32(res, crc);
  388. crc_cnt++;
  389. if (crc_cnt < 4) continue;
  390. if ((crc & 0xDEBB20E3))
  391. {
  392. #ifdef ZDEBUG
  393. rt_kprintf("error code: CRC32 error \r\n");
  394. #endif
  395. return -RT_ERROR;
  396. }
  397. return c;
  398. }
  399. else
  400. {
  401. crc = updcrc32(res, crc);
  402. switch (c)
  403. {
  404. case 0:
  405. if (res == ZRESC)
  406. {
  407. c = -1; continue;
  408. }
  409. *buf++ = res;
  410. Rxcount++;
  411. continue;
  412. case -1:
  413. if (res >= 040 && res < 0100)
  414. {
  415. c = res - 035; res = 040;
  416. goto spaces;
  417. }
  418. if (res == 0100)
  419. {
  420. c = 0;
  421. *buf++ = ZRESC;
  422. Rxcount++;
  423. continue;
  424. }
  425. c = res; continue;
  426. default:
  427. c -= 0100;
  428. if (c < 1)
  429. goto end;
  430. spaces:
  431. if ((buf + c) > p+len)
  432. goto end;
  433. while ( --res >= 0)
  434. {
  435. *buf++ = res;
  436. Rxcount++;
  437. }
  438. c = 0; continue;
  439. }
  440. }
  441. } // if -else
  442. }
  443. end:
  444. return -RT_ERROR;
  445. }
  446. rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len)
  447. {
  448. rt_int16_t res = -RT_ERROR;
  449. if (RxCRC == 0)
  450. {
  451. res = zrec_data16(buf,len);
  452. }
  453. else if (RxCRC == 1)
  454. {
  455. res = zrec_data32(buf, len);
  456. }
  457. else if (RxCRC == 2)
  458. {
  459. res = zrec_data32r(buf, len);
  460. }
  461. return res;
  462. }
  463. /* get type and cmd of header, fix lenght */
  464. rt_int16_t zget_header(rt_uint8_t *hdr)
  465. {
  466. rt_int16_t c,prev_char;
  467. rt_uint32_t bit;
  468. rt_uint16_t get_can,step_out;
  469. bit = get_device_baud(); /* get console baud rate */
  470. Rxframeind = header_type = 0;
  471. step_out = 0;
  472. prev_char = 0xff;
  473. for (;;)
  474. {
  475. c = zread_line(100);
  476. switch(c)
  477. {
  478. case 021:
  479. case 0221:
  480. if (prev_char == CAN) break;
  481. if (prev_char == ZCRCW) goto start_again;
  482. break;
  483. case RCDO:
  484. goto end;
  485. case TIMEOUT:
  486. if (prev_char == CAN) break;
  487. if (prev_char == ZCRCW)
  488. {
  489. c = -RT_ERROR; goto end;
  490. }
  491. goto end;
  492. case ZCRCW:
  493. if (prev_char == CAN) goto start_again;
  494. break;
  495. case CAN:
  496. get_can:
  497. if (++get_can > 5)
  498. {
  499. c = ZCAN; goto end;
  500. }
  501. break;
  502. case ZPAD:
  503. if (prev_char == CAN) break;
  504. if (prev_char == ZCRCW) goto start_again;
  505. step_out = 1;
  506. break;
  507. default:
  508. if (prev_char == CAN) break;
  509. if (prev_char == ZCRCW) goto start_again;
  510. start_again:
  511. if (--bit == 0)
  512. {
  513. c = GCOUNT; goto end;
  514. }
  515. get_can = 0;
  516. break;
  517. }
  518. prev_char = c;
  519. if (step_out) break; /* exit loop */
  520. }
  521. step_out = get_can = 0;
  522. for (;;)
  523. {
  524. c = zxor_read();
  525. switch(c)
  526. {
  527. case ZPAD:
  528. break;
  529. case RCDO:
  530. case TIMEOUT:
  531. goto end;
  532. case ZDLE:
  533. step_out = 1;
  534. break;
  535. default:
  536. goto start_again;
  537. }
  538. if (step_out) break;
  539. }
  540. Rxframeind = c = zxor_read();
  541. switch (c)
  542. {
  543. case ZBIN32:
  544. RxCRC = 1; c = zget_bin_fcs(hdr); break;
  545. case ZBINR32:
  546. RxCRC = 2; c = zget_bin_fcs(hdr); break;
  547. case ZBIN:
  548. RxCRC = 0; c = zget_bin_header(hdr); break;
  549. case ZHEX:
  550. RxCRC = 0; c = zget_hex_header(hdr); break;
  551. case CAN:
  552. goto get_can;
  553. case RCDO:
  554. case TIMEOUT:
  555. goto end;
  556. default:
  557. goto start_again;
  558. }
  559. end:
  560. return c;
  561. }
  562. /* receive a binary header */
  563. static rt_int16_t zget_bin_header(rt_uint8_t *hdr)
  564. {
  565. rt_int16_t res, i;
  566. rt_uint16_t crc;
  567. if ((res = zread_byte()) & ~0377)
  568. return res;
  569. header_type = res;
  570. crc = updcrc16(res, 0);
  571. for (i=0;i<4;i++)
  572. {
  573. if ((res = zread_byte()) & ~0377)
  574. return res;
  575. crc = updcrc16(res, crc);
  576. *hdr++ = res;
  577. }
  578. if ((res = zread_byte()) & ~0377)
  579. return res;
  580. crc = updcrc16(res, crc);
  581. if ((res = zread_byte()) & ~0377)
  582. return res;
  583. crc = updcrc16(res, crc);
  584. if (crc & 0xFFFF)
  585. {
  586. rt_kprintf("CRC error\n");
  587. return -RT_ERROR;
  588. }
  589. return header_type;
  590. }
  591. /* receive a binary header,with 32bits FCS */
  592. static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr)
  593. {
  594. rt_int16_t res, i;
  595. rt_uint32_t crc;
  596. if ((res = zread_byte()) & ~0377)
  597. return res;
  598. header_type = res;
  599. crc = 0xFFFFFFFFL;
  600. crc = updcrc32(res, crc);
  601. for (i=0;i<4;i++) /* 4headers */
  602. {
  603. if ((res = zread_byte()) & ~0377)
  604. return res;
  605. crc = updcrc32(res, crc);
  606. *hdr++ = res;
  607. }
  608. for (i=0;i<4;i++) /* 4bytes crc */
  609. {
  610. if ((res = zread_byte()) & ~0377)
  611. return res;
  612. crc = updcrc32(res, crc);
  613. }
  614. if (crc != 0xDEBB20E3)
  615. {
  616. #ifdef ZDEBUG
  617. rt_kprintf("CRC error\n");
  618. #endif
  619. return -RT_ERROR;
  620. }
  621. return header_type;
  622. }
  623. /* receive a hex style header (type and position) */
  624. rt_int16_t zget_hex_header(rt_uint8_t *hdr)
  625. {
  626. rt_int16_t res,i;
  627. rt_uint16_t crc;
  628. if ((res = zget_hex()) < 0)
  629. return res;
  630. header_type = res;
  631. crc = updcrc16(res, 0);
  632. for (i=0;i<4;i++)
  633. {
  634. if ((res = zget_hex()) < 0)
  635. return res;
  636. crc = updcrc16(res, crc);
  637. *hdr++ = res;
  638. }
  639. if ((res = zget_hex()) < 0)
  640. return res;
  641. crc = updcrc16(res, crc);
  642. if ((res = zget_hex()) < 0)
  643. return res;
  644. crc = updcrc16(res, crc);
  645. if (crc & 0xFFFF)
  646. {
  647. #ifdef ZDEBUG
  648. rt_kprintf("error code : CRC error\r\n");
  649. #endif
  650. return -RT_ERROR;
  651. }
  652. res = zread_line(100);
  653. if (res < 0)
  654. return res;
  655. res = zread_line(100);
  656. if (res < 0)
  657. return res;
  658. return header_type;
  659. }
  660. /* convert to ascii */
  661. static void zsend_ascii(rt_uint8_t c)
  662. {
  663. const char hex[] = "0123456789abcdef";
  664. zsend_line(hex[(c&0xF0)>>4]);
  665. zsend_line(hex[(c)&0xF]);
  666. return;
  667. }
  668. /*
  669. * aend character c with ZMODEM escape sequence encoding.
  670. */
  671. void zsend_zdle_char(rt_uint16_t ch)
  672. {
  673. rt_uint16_t res;
  674. res = ch & 0377;
  675. switch (res)
  676. {
  677. case 0377:
  678. zsend_byte(res);
  679. break;
  680. case ZDLE:
  681. zsend_byte(ZDLE);
  682. res ^= 0100;
  683. zsend_byte(res);
  684. break;
  685. case 021:
  686. case 023:
  687. case 0221:
  688. case 0223:
  689. zsend_byte(ZDLE);
  690. res ^= 0100;
  691. zsend_byte(res);
  692. break;
  693. default:
  694. zsend_byte(res);
  695. }
  696. }
  697. /* decode two lower case hex digits into an 8 bit byte value */
  698. static rt_int16_t zget_hex(void)
  699. {
  700. rt_int16_t res,n;
  701. if ((res = zxor_read()) < 0)
  702. return res;
  703. n = res - '0';
  704. if (n > 9)
  705. n -= ('a' - ':');
  706. if (n & ~0x0f)
  707. return -RT_ERROR;
  708. if ((res = zxor_read()) < 0)
  709. return res;
  710. res -= '0';
  711. if (res > 9)
  712. res -= ('a' - ':');
  713. if (res & ~0x0f)
  714. return -RT_ERROR;
  715. res += (n<<4);
  716. return res;
  717. }
  718. /*
  719. * read a byte, checking for ZMODEM escape encoding
  720. * including CAN*5 which represents a quick abort
  721. */
  722. rt_int16_t zread_byte(void)
  723. {
  724. register int res;
  725. again:
  726. /* Quick check for non control characters */
  727. if ((res = zread_line(100)) & 0140)
  728. return res;
  729. switch (res)
  730. {
  731. case ZDLE:
  732. break;
  733. case 023:
  734. case 0223:
  735. case 021:
  736. case 0221:
  737. goto again;
  738. default:
  739. return res;
  740. }
  741. again2:
  742. if ((res = zread_line(100)) < 0)
  743. return res;
  744. if (res == CAN && (res = zread_line(100)) < 0)
  745. return res;
  746. if (res == CAN && (res = zread_line(100)) < 0)
  747. return res;
  748. if (res == CAN && (res = zread_line(100)) < 0)
  749. return res;
  750. switch (res)
  751. {
  752. case CAN:
  753. return GOTCAN;
  754. case ZCRCE:
  755. case ZCRCG:
  756. case ZCRCQ:
  757. case ZCRCW:
  758. return (res | GOTOR);
  759. case ZRUB0:
  760. return 0177;
  761. case ZRUB1:
  762. return 0377;
  763. case 023:
  764. case 0223:
  765. case 021:
  766. case 0221:
  767. goto again2;
  768. default:
  769. if ((res & 0140) == 0100)
  770. return (res ^ 0100);
  771. break;
  772. }
  773. return -RT_ERROR;
  774. }
  775. /*
  776. * @read a character from the modem line with timeout.
  777. * @eat parity, XON and XOFF characters.
  778. */
  779. rt_int16_t zxor_read(void)
  780. {
  781. rt_int16_t res;
  782. for (;;)
  783. {
  784. if ((res = zread_line(100)) < 0)
  785. return res;
  786. switch (res &= 0177) {
  787. case XON:
  788. case XOFF:
  789. continue;
  790. case '\r':
  791. case '\n':
  792. case ZDLE:
  793. default:
  794. return res;
  795. }
  796. }
  797. }
  798. /* put file posistion into the header*/
  799. void zput_pos(rt_uint32_t pos)
  800. {
  801. tx_header[ZP0] = pos;
  802. tx_header[ZP1] = pos>>8;
  803. tx_header[ZP2] = pos>>16;
  804. tx_header[ZP3] = pos>>24;
  805. return;
  806. }
  807. /* Recover a long integer from a header */
  808. void zget_pos(rt_uint32_t pos)
  809. {
  810. Rxpos = (rx_header[ZP3] & 0377);
  811. Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377);
  812. Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377);
  813. Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377);
  814. return;
  815. }
  816. /* end of zcore.c */