zcore.c 18 KB

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