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. p = buf;
  315. crc_cnt = 0; crc = 0xffffffffL;
  316. Rxcount = 0;
  317. while (buf <= p+len)
  318. {
  319. if ((res = zread_byte()) & ~0377)
  320. {
  321. if (res == GOTCRCE || res == GOTCRCG ||
  322. res == GOTCRCQ || res == GOTCRCW)
  323. {
  324. c = res;
  325. crc = updcrc32(res&0377, crc);
  326. flag = 1;
  327. continue;
  328. }
  329. else if (res == GOTCAN) return ZCAN;
  330. else if (res == TIMEOUT) return TIMEOUT;
  331. else return res;
  332. }
  333. else
  334. {
  335. if (flag)
  336. {
  337. crc = updcrc32(res, crc);
  338. crc_cnt++;
  339. if (crc_cnt < 4) continue;
  340. if ((crc & 0xDEBB20E3))
  341. {
  342. #ifdef ZDEBUG
  343. rt_kprintf("error code: CRC32 error \r\n");
  344. #endif
  345. return -RT_ERROR;
  346. }
  347. return c;
  348. }
  349. else
  350. {
  351. *buf++ = res;
  352. Rxcount++;
  353. crc = updcrc32(res, crc);
  354. }
  355. }
  356. }
  357. return -RT_ERROR;
  358. }
  359. /* receive data,with RLE encoded,32bits CRC check */
  360. static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len)
  361. {
  362. rt_int16_t c,crc_cnt;
  363. rt_uint32_t crc;
  364. rt_err_t res = -RT_ERROR;
  365. rt_uint8_t *p,flag = 0;
  366. crc_cnt = 0; crc = 0xffffffffL;
  367. Rxcount = 0;
  368. p = buf;
  369. while (buf <= p+len)
  370. {
  371. if ((res = zread_byte()) & ~0377)
  372. {
  373. if (res == GOTCRCE || res == GOTCRCG ||
  374. res == GOTCRCQ || res == GOTCRCW)
  375. {
  376. c = res;
  377. crc = updcrc32(res&0377, crc);
  378. flag = 1;
  379. continue;
  380. }
  381. else if (res == GOTCAN) return ZCAN;
  382. else if (res == TIMEOUT) return TIMEOUT;
  383. else return res;
  384. }
  385. else
  386. {
  387. if (flag)
  388. {
  389. crc = updcrc32(res, crc);
  390. crc_cnt++;
  391. if (crc_cnt < 4) continue;
  392. if ((crc & 0xDEBB20E3))
  393. {
  394. #ifdef ZDEBUG
  395. rt_kprintf("error code: CRC32 error \r\n");
  396. #endif
  397. return -RT_ERROR;
  398. }
  399. return c;
  400. }
  401. else
  402. {
  403. crc = updcrc32(res, crc);
  404. switch (c)
  405. {
  406. case 0:
  407. if (res == ZRESC)
  408. {
  409. c = -1; continue;
  410. }
  411. *buf++ = res;
  412. Rxcount++;
  413. continue;
  414. case -1:
  415. if (res >= 040 && res < 0100)
  416. {
  417. c = res - 035; res = 040;
  418. goto spaces;
  419. }
  420. if (res == 0100)
  421. {
  422. c = 0;
  423. *buf++ = ZRESC;
  424. Rxcount++;
  425. continue;
  426. }
  427. c = res; continue;
  428. default:
  429. c -= 0100;
  430. if (c < 1)
  431. goto end;
  432. spaces:
  433. if ((buf + c) > p+len)
  434. goto end;
  435. while ( --res >= 0)
  436. {
  437. *buf++ = res;
  438. Rxcount++;
  439. }
  440. c = 0; continue;
  441. }
  442. }
  443. } // if -else
  444. }
  445. end:
  446. return -RT_ERROR;
  447. }
  448. rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len)
  449. {
  450. rt_int16_t res = -RT_ERROR;
  451. if (RxCRC == 0)
  452. {
  453. res = zrec_data16(buf,len);
  454. }
  455. else if (RxCRC == 1)
  456. {
  457. res = zrec_data32(buf, len);
  458. }
  459. else if (RxCRC == 2)
  460. {
  461. res = zrec_data32r(buf, len);
  462. }
  463. return res;
  464. }
  465. /* get type and cmd of header, fix lenght */
  466. rt_int16_t zget_header(rt_uint8_t *hdr)
  467. {
  468. rt_int16_t c,prev_char;
  469. rt_uint32_t bit;
  470. rt_uint16_t get_can,step_out;
  471. bit = get_device_baud(); /* get console baud rate */
  472. Rxframeind = header_type = 0;
  473. step_out = 0;
  474. prev_char = 0xff;
  475. for (;;)
  476. {
  477. c = zread_line(100);
  478. switch(c)
  479. {
  480. case 021:
  481. case 0221:
  482. if (prev_char == CAN) break;
  483. if (prev_char == ZCRCW) goto start_again;
  484. break;
  485. case RCDO:
  486. goto end;
  487. case TIMEOUT:
  488. if (prev_char == CAN) break;
  489. if (prev_char == ZCRCW)
  490. {
  491. c = -RT_ERROR; goto end;
  492. }
  493. goto end;
  494. case ZCRCW:
  495. if (prev_char == CAN) goto start_again;
  496. break;
  497. case CAN:
  498. get_can:
  499. if (++get_can > 5)
  500. {
  501. c = ZCAN; goto end;
  502. }
  503. break;
  504. case ZPAD:
  505. if (prev_char == CAN) break;
  506. if (prev_char == ZCRCW) goto start_again;
  507. step_out = 1;
  508. break;
  509. default:
  510. if (prev_char == CAN) break;
  511. if (prev_char == ZCRCW) goto start_again;
  512. start_again:
  513. if (--bit == 0)
  514. {
  515. c = GCOUNT; goto end;
  516. }
  517. get_can = 0;
  518. break;
  519. }
  520. prev_char = c;
  521. if (step_out) break; /* exit loop */
  522. }
  523. step_out = get_can = 0;
  524. for (;;)
  525. {
  526. c = zxor_read();
  527. switch(c)
  528. {
  529. case ZPAD:
  530. break;
  531. case RCDO:
  532. case TIMEOUT:
  533. goto end;
  534. case ZDLE:
  535. step_out = 1;
  536. break;
  537. default:
  538. goto start_again;
  539. }
  540. if (step_out) break;
  541. }
  542. Rxframeind = c = zxor_read();
  543. switch (c)
  544. {
  545. case ZBIN32:
  546. RxCRC = 1; c = zget_bin_fcs(hdr); break;
  547. case ZBINR32:
  548. RxCRC = 2; c = zget_bin_fcs(hdr); break;
  549. case ZBIN:
  550. RxCRC = 0; c = zget_bin_header(hdr); break;
  551. case ZHEX:
  552. RxCRC = 0; c = zget_hex_header(hdr); break;
  553. case CAN:
  554. goto get_can;
  555. case RCDO:
  556. case TIMEOUT:
  557. goto end;
  558. default:
  559. goto start_again;
  560. }
  561. end:
  562. return c;
  563. }
  564. /* receive a binary header */
  565. static rt_int16_t zget_bin_header(rt_uint8_t *hdr)
  566. {
  567. rt_int16_t res, i;
  568. rt_uint16_t crc;
  569. if ((res = zread_byte()) & ~0377)
  570. return res;
  571. header_type = res;
  572. crc = updcrc16(res, 0);
  573. for (i=0;i<4;i++)
  574. {
  575. if ((res = zread_byte()) & ~0377)
  576. return res;
  577. crc = updcrc16(res, crc);
  578. *hdr++ = res;
  579. }
  580. if ((res = zread_byte()) & ~0377)
  581. return res;
  582. crc = updcrc16(res, crc);
  583. if ((res = zread_byte()) & ~0377)
  584. return res;
  585. crc = updcrc16(res, crc);
  586. if (crc & 0xFFFF)
  587. {
  588. rt_kprintf("CRC error\n");
  589. return -RT_ERROR;
  590. }
  591. return header_type;
  592. }
  593. /* receive a binary header,with 32bits FCS */
  594. static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr)
  595. {
  596. rt_int16_t res, i;
  597. rt_uint32_t crc;
  598. if ((res = zread_byte()) & ~0377)
  599. return res;
  600. header_type = res;
  601. crc = 0xFFFFFFFFL;
  602. crc = updcrc32(res, crc);
  603. for (i=0;i<4;i++) /* 4headers */
  604. {
  605. if ((res = zread_byte()) & ~0377)
  606. return res;
  607. crc = updcrc32(res, crc);
  608. *hdr++ = res;
  609. }
  610. for (i=0;i<4;i++) /* 4bytes crc */
  611. {
  612. if ((res = zread_byte()) & ~0377)
  613. return res;
  614. crc = updcrc32(res, crc);
  615. }
  616. if (crc != 0xDEBB20E3)
  617. {
  618. #ifdef ZDEBUG
  619. rt_kprintf("CRC error\n");
  620. #endif
  621. return -RT_ERROR;
  622. }
  623. return header_type;
  624. }
  625. /* receive a hex style header (type and position) */
  626. rt_int16_t zget_hex_header(rt_uint8_t *hdr)
  627. {
  628. rt_int16_t res,i;
  629. rt_uint16_t crc;
  630. if ((res = zget_hex()) < 0)
  631. return res;
  632. header_type = res;
  633. crc = updcrc16(res, 0);
  634. for (i=0;i<4;i++)
  635. {
  636. if ((res = zget_hex()) < 0)
  637. return res;
  638. crc = updcrc16(res, crc);
  639. *hdr++ = res;
  640. }
  641. if ((res = zget_hex()) < 0)
  642. return res;
  643. crc = updcrc16(res, crc);
  644. if ((res = zget_hex()) < 0)
  645. return res;
  646. crc = updcrc16(res, crc);
  647. if (crc & 0xFFFF)
  648. {
  649. #ifdef ZDEBUG
  650. rt_kprintf("error code : CRC error\r\n");
  651. #endif
  652. return -RT_ERROR;
  653. }
  654. res = zread_line(100);
  655. if (res < 0)
  656. return res;
  657. res = zread_line(100);
  658. if (res < 0)
  659. return res;
  660. return header_type;
  661. }
  662. /* convert to ascii */
  663. static void zsend_ascii(rt_uint8_t c)
  664. {
  665. const char hex[] = "0123456789abcdef";
  666. zsend_line(hex[(c&0xF0)>>4]);
  667. zsend_line(hex[(c)&0xF]);
  668. return;
  669. }
  670. /*
  671. * aend character c with ZMODEM escape sequence encoding.
  672. */
  673. void zsend_zdle_char(rt_uint16_t ch)
  674. {
  675. rt_uint16_t res;
  676. res = ch & 0377;
  677. switch (res)
  678. {
  679. case 0377:
  680. zsend_byte(res);
  681. break;
  682. case ZDLE:
  683. zsend_byte(ZDLE);
  684. res ^= 0100;
  685. zsend_byte(res);
  686. break;
  687. case 021:
  688. case 023:
  689. case 0221:
  690. case 0223:
  691. zsend_byte(ZDLE);
  692. res ^= 0100;
  693. zsend_byte(res);
  694. break;
  695. default:
  696. zsend_byte(res);
  697. }
  698. }
  699. /* decode two lower case hex digits into an 8 bit byte value */
  700. static rt_int16_t zget_hex(void)
  701. {
  702. rt_int16_t res,n;
  703. if ((res = zxor_read()) < 0)
  704. return res;
  705. n = res - '0';
  706. if (n > 9)
  707. n -= ('a' - ':');
  708. if (n & ~0x0f)
  709. return -RT_ERROR;
  710. if ((res = zxor_read()) < 0)
  711. return res;
  712. res -= '0';
  713. if (res > 9)
  714. res -= ('a' - ':');
  715. if (res & ~0x0f)
  716. return -RT_ERROR;
  717. res += (n<<4);
  718. return res;
  719. }
  720. /*
  721. * read a byte, checking for ZMODEM escape encoding
  722. * including CAN*5 which represents a quick abort
  723. */
  724. rt_int16_t zread_byte(void)
  725. {
  726. register int res;
  727. again:
  728. /* Quick check for non control characters */
  729. if ((res = zread_line(100)) & 0140)
  730. return res;
  731. switch (res)
  732. {
  733. case ZDLE:
  734. break;
  735. case 023:
  736. case 0223:
  737. case 021:
  738. case 0221:
  739. goto again;
  740. default:
  741. return res;
  742. }
  743. again2:
  744. if ((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. if (res == CAN && (res = zread_line(100)) < 0)
  751. return res;
  752. switch (res)
  753. {
  754. case CAN:
  755. return GOTCAN;
  756. case ZCRCE:
  757. case ZCRCG:
  758. case ZCRCQ:
  759. case ZCRCW:
  760. return (res | GOTOR);
  761. case ZRUB0:
  762. return 0177;
  763. case ZRUB1:
  764. return 0377;
  765. case 023:
  766. case 0223:
  767. case 021:
  768. case 0221:
  769. goto again2;
  770. default:
  771. if ((res & 0140) == 0100)
  772. return (res ^ 0100);
  773. break;
  774. }
  775. return -RT_ERROR;
  776. }
  777. /*
  778. * @read a character from the modem line with timeout.
  779. * @eat parity, XON and XOFF characters.
  780. */
  781. rt_int16_t zxor_read(void)
  782. {
  783. rt_int16_t res;
  784. for (;;)
  785. {
  786. if ((res = zread_line(100)) < 0)
  787. return res;
  788. switch (res &= 0177) {
  789. case XON:
  790. case XOFF:
  791. continue;
  792. case '\r':
  793. case '\n':
  794. case ZDLE:
  795. default:
  796. return res;
  797. }
  798. }
  799. }
  800. /* put file posistion into the header*/
  801. void zput_pos(rt_uint32_t pos)
  802. {
  803. tx_header[ZP0] = pos;
  804. tx_header[ZP1] = pos>>8;
  805. tx_header[ZP2] = pos>>16;
  806. tx_header[ZP3] = pos>>24;
  807. return;
  808. }
  809. /* Recover a long integer from a header */
  810. void zget_pos(rt_uint32_t pos)
  811. {
  812. Rxpos = (rx_header[ZP3] & 0377);
  813. Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377);
  814. Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377);
  815. Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377);
  816. return;
  817. }
  818. /* end of zcore.c */