zcore.c 21 KB

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