diff options
| author | itspy.wei <[email protected]> | 2011-03-30 05:41:49 +0000 |
|---|---|---|
| committer | itspy.wei <[email protected]> | 2011-03-30 05:41:49 +0000 |
| commit | 300825d8cb5a1fc1b50d45b6f2f813c626706c5c (patch) | |
| tree | ebac35acbaacf67f2a123dff5927b7a6cf4c8bb0 /components | |
| parent | 578b245b0e4b39e2109d2f4baebdbee608f791ca (diff) | |
file transimission through zmodem protocol.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1354 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components')
| -rw-r--r-- | components/rzsz/rz.c | 374 | ||||
| -rw-r--r-- | components/rzsz/sz.c | 305 | ||||
| -rw-r--r-- | components/rzsz/zcore.c | 865 | ||||
| -rw-r--r-- | components/rzsz/zdef.h | 217 | ||||
| -rw-r--r-- | components/rzsz/zdevice.c | 109 | ||||
| -rw-r--r-- | components/rzsz/zstart.c | 120 |
6 files changed, 1990 insertions, 0 deletions
diff --git a/components/rzsz/rz.c b/components/rzsz/rz.c new file mode 100644 index 0000000000..c6963e934e --- /dev/null +++ b/components/rzsz/rz.c @@ -0,0 +1,374 @@ +/* + * File : rz.c
+ * the implemention of receiving files from the remote computers
+ * through the zmodem proctocol. + * Change Logs: + * Date Author Notes + * 2011-03-29 itspy + */
+
+#include <rtthread.h>
+#include <finsh.h>
+#include <shell.h>
+#include <rtdef.h>
+#include <dfs.h>
+#include <dfs_file.h>
+#include <dfs_posix.h>
+#include <stdio.h>
+#include "zdef.h"
+
+
+void zr_start(char *path);
+static rt_err_t zrec_init(rt_uint8_t *rxbuf,struct zfile *zf);
+static rt_err_t zrec_files(struct zfile *zf);
+static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf);
+static rt_err_t zrec_file_data(rt_uint8_t *buf,struct zfile *zf);;
+static rt_err_t zrec_file(rt_uint8_t *rxbuf,struct zfile *zf);
+static rt_err_t zget_file_info(char *name,struct zfile *zf);
+static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf);
+static void zrec_ack_bibi(void);
+
+
+/* start zmodem receive proccess */
+void zr_start(char *path)
+{
+ struct zfile *zf;
+ rt_uint8_t n;
+ char ch;
+ rt_err_t res = -RT_ERROR;
+ zf = rt_malloc(sizeof(struct zfile));
+ if (zf == RT_NULL)
+ {
+ rt_kprintf("zf: out of memory\r\n");
+ return;
+ }
+ memset(zf, 0, sizeof(struct zfile));
+ zf->fname = path;
+ zf->fd = -1;
+ rt_kprintf("\r\n");
+ res = zrec_files(zf);
+ if (res == RT_EOK)
+ {
+ rt_kprintf("\b\b\bfile: %s \r\n",zf->fname+1);
+ rt_kprintf("size: %ld bytes\r\n",zf->bytes_received);
+ rt_kprintf("receive completed.\r\n");
+ close(zf->fd);
+ rt_free(zf);
+ }
+ else
+ {
+ rt_kprintf("\b\b\bfile: %s \r\n",zf->fname+1);
+ rt_kprintf("size: 0 bytes\r\n");
+ rt_kprintf("receive failed.\r\n");
+ close(zf->fd);
+ unlink(zf->fname); /* remove this file */
+ rt_free(zf);
+ }
+ /* waiting,clear console buffer */
+ rt_thread_delay(RT_TICK_PER_SECOND/2);
+ while(1)
+ {
+ n=rt_device_read(shell->device, 0, &ch, 1);
+ if (n == 0) break;
+ }
+ return ;
+}
+
+
+
+
+/* receiver init, wait for ack */
+static rt_err_t zrec_init(rt_uint8_t *rxbuf,struct zfile *zf)
+{
+ rt_uint8_t err_cnt = 0;
+ rt_err_t res = -RT_ERROR;
+ for (;;)
+ {
+ zput_pos(0L);
+ tx_header[ZF0] = ZF0_CMD;
+ tx_header[ZF1] = ZF1_CMD;
+ tx_header[ZF2] = ZF2_CMD;
+ zsend_hex_header(ZRINIT, tx_header);
+again:
+ res = zget_header(rx_header);
+ switch(res)
+ {
+ case ZFILE:
+ ZF0_CMD = rx_header[ZF0];
+ ZF1_CMD = rx_header[ZF1];
+ ZF2_CMD = rx_header[ZF2];
+ ZF3_CMD = rx_header[ZF3];
+ res = zget_data(rxbuf, RX_BUFFER_SIZE);
+ if (res == GOTCRCW)
+ {
+ if (zget_file_info((char*)rxbuf,zf) != RT_EOK)
+ {
+ zsend_hex_header(ZSKIP, tx_header);
+ return (res);
+ }
+ return RT_EOK;;
+ }
+ zsend_hex_header(ZNAK, tx_header);
+ goto again;
+ case ZSINIT:
+ if (zget_data((rt_uint8_t*)Attn, ZATTNLEN) == GOTCRCW) /* send zack */
+ {
+ zsend_hex_header(ZACK, tx_header);
+ goto again;
+ }
+ zsend_hex_header(ZNAK, tx_header); /* send znak */
+ goto again;
+ case ZRQINIT:
+ continue;
+ case ZEOF:
+ continue;
+ case ZCOMPL:
+ goto again;
+ case ZFIN: /* end file session */
+ zrec_ack_bibi();
+ return res;
+ default:
+ if (++err_cnt >1000) return -RT_ERROR;
+ continue;
+ }
+ }
+}
+
+/* receive files */
+static rt_err_t zrec_files(struct zfile *zf)
+{
+ rt_uint8_t *rxbuf;
+ rt_err_t res = -RT_ERROR;
+ zinit_parameter();
+ rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t));
+ if (rxbuf == RT_NULL)
+ {
+ rt_kprintf("not enough memory\r\n");
+ return -RT_ERROR;
+ }
+ rt_kprintf("rz: ready...\r\n"); /* here ready to receive things */
+ if ((res = zrec_init(rxbuf,zf))!= RT_EOK)
+ {
+ rt_kprintf("receive init failed !\r\n");
+ rt_free(rxbuf);
+ return -RT_ERROR;
+ }
+ res = zrec_file(rxbuf,zf);
+ if (res == ZFIN)
+ {
+ rt_free(rxbuf);
+ return RT_EOK; /* if finish session */
+ }
+ else if (res == ZCAN)
+ {
+ rt_free(rxbuf);
+ return ZCAN; /* cancel by sender */
+ }
+ else
+ {
+ zsend_can();
+ rt_free(rxbuf);
+ return res;
+ }
+}
+/* receive file */
+static rt_err_t zrec_file(rt_uint8_t *rxbuf,struct zfile *zf)
+{
+ rt_err_t res = - RT_ERROR;
+ rt_uint16_t err_cnt = 0;
+ do
+ {
+ zput_pos(zf->bytes_received);
+ zsend_hex_header(ZRPOS, tx_header);
+again:
+ res = zget_header(rx_header);
+ switch (res)
+ {
+ case ZDATA:
+ zget_pos(Rxpos);
+ if (Rxpos != zf->bytes_received)
+ {
+ zsend_break(Attn);
+ continue;
+ }
+ err_cnt = 0;
+ res = zrec_file_data(rxbuf,zf);
+ if (res == -RT_ERROR)
+ {
+ zsend_break(Attn);
+ continue;
+ }
+ else if (res == GOTCAN) return res;
+ else goto again;
+ case ZRPOS:
+ zget_pos(Rxpos);
+ continue;
+ case ZEOF:
+ err_cnt = 0;
+ zget_pos(Rxpos);
+ if (Rxpos != zf->bytes_received || Rxpos != zf->bytes_total)
+ {
+ continue;
+ }
+ return (zrec_init(rxbuf,zf)); /* resend ZRINIT packet,ready to receive next file */
+ case ZFIN:
+ zrec_ack_bibi();
+ return ZCOMPL;
+ case ZCAN:
+#ifdef ZDEBUG
+ rt_kprintf("error code: sender cancelled \r\n");
+#endif
+ unlink(zf->fname);
+ zf->bytes_received = 0L; /* throw the received data */
+ return res;
+ case ZSKIP:
+ return res;
+ case -RT_ERROR:
+ zsend_break(Attn);
+ continue;
+ case ZNAK:
+ case TIMEOUT:
+ default:
+ continue;
+ }
+ } while(++err_cnt < 100);
+ return res;
+}
+
+/* proccess file infomation */
+static rt_err_t zget_file_info(char *name,struct zfile *zf)
+{
+ char *p;
+ char *ftemp,*ptr;
+ rt_uint16_t i,len;
+ rt_err_t res = -RT_ERROR;
+ struct statfs buf;
+ struct stat finfo;
+
+ if (zf->fname == RT_NULL) /* extract file path */
+ {
+ len = strlen(name)+2;
+ }
+ else
+ len = strlen(zf->fname)+strlen(name)+2;
+ ftemp = rt_malloc(len);
+ if (ftemp == RT_NULL)
+ {
+ rt_kprintf("ftemp: out of memory\n");
+ return -RT_ERROR;
+ }
+ memset(ftemp,0,len);
+
+ for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++)
+ ftemp[i] = *ptr++;
+ ftemp[len-strlen(name)-2] = '/';
+ /* check if is a directory */
+ if ((zf->fd=open(ftemp, DFS_O_DIRECTORY,0)) < 0)
+ {
+ rt_kprintf("can not open file:%s\r\n",zf->fname+1);
+ close(zf->fd);
+ zf->fd = -1;
+ return res;
+ }
+ fstat(zf->fd, &finfo);
+ if ((finfo.st_mode&S_IFDIR) != S_IFDIR)
+ {
+ close(zf->fd);
+ zf->fd = -1;
+ return res;
+ }
+ close(zf->fd);
+ /* get fullpath && file attributes */
+ strcat(ftemp,name);
+ zf->fname = ftemp;
+ p = strlen(name)+name+1;
+ sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode);
+ dfs_statfs(working_directory,&buf);
+ if (zf->bytes_total > (buf.f_blocks * buf.f_bfree))
+ {
+ rt_kprintf(" not enough disk space !\r\n");
+ return -RT_ERROR;
+ }
+ zf->bytes_received = 0L;
+ if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */
+ {
+ rt_kprintf("can not create file:%s \r\n",zf->fname);
+ rt_free(ftemp);
+ return -RT_ERROR;
+ }
+ return RT_EOK;
+}
+
+/* receive file data,continously, no ack */
+static rt_err_t zrec_file_data(rt_uint8_t *buf,struct zfile *zf)
+{
+ rt_err_t res = -RT_ERROR;
+more_data:
+ res = zget_data(buf,RX_BUFFER_SIZE);
+ switch(res)
+ {
+ case GOTCRCW: /* zack received */
+ zwrite_file(buf,Rxcount,zf);
+ zf->bytes_received += Rxcount;
+ zput_pos(zf->bytes_received);
+ zsend_line(XON);
+ zsend_hex_header(ZACK, tx_header);
+ return RT_EOK;
+ case GOTCRCQ:
+ zwrite_file(buf,Rxcount,zf);
+ zf->bytes_received += Rxcount;
+ zput_pos(zf->bytes_received);
+ zsend_hex_header(ZACK, tx_header);
+ goto more_data;
+ case GOTCRCG:
+ zwrite_file(buf,Rxcount,zf);
+ zf->bytes_received += Rxcount;
+ goto more_data;
+ case GOTCRCE:
+ zwrite_file(buf,Rxcount,zf);
+ zf->bytes_received += Rxcount;
+ return RT_EOK;
+ case GOTCAN:
+#ifdef ZDEBUG
+ rt_kprintf("error code : ZCAN \r\n");
+#endif
+ return res;
+ case TIMEOUT:
+ return res;
+ case -RT_ERROR:
+ zsend_break(Attn);
+ return res;
+ default:
+ return res;
+ }
+}
+
+/* write file */
+static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf)
+{
+ return (write(zf->fd,buf,size));
+}
+
+/* ack bibi */
+static void zrec_ack_bibi(void)
+{
+ rt_uint8_t i;
+ zput_pos(0L);
+ for (i=0;i<3;i++)
+ {
+ zsend_hex_header(ZFIN, tx_header);
+ switch (zread_line(100))
+ {
+ case 'O':
+ zread_line(1);
+ return;
+ case RCDO:
+ return;
+ case TIMEOUT:
+ default:
+ break;
+ }
+ }
+}
+
+/* end of rz.c */
diff --git a/components/rzsz/sz.c b/components/rzsz/sz.c new file mode 100644 index 0000000000..67cd236c5f --- /dev/null +++ b/components/rzsz/sz.c @@ -0,0 +1,305 @@ +/* + * File : sz.c
+ * the implemention of sending files to the remote computers
+ * through the zmodem proctocol. + * Change Logs: + * Date Author Notes + * 2011-03-29 itspy + */
+
+#include <rtthread.h>
+#include <finsh.h>
+#include <shell.h>
+#include <rtdef.h>
+#include <dfs.h>
+#include <dfs_file.h>
+#include <dfs_posix.h>
+#include "zdef.h"
+
+
+static rt_uint8_t TX_BUFFER[TX_BUFFER_SIZE]; /* sender buffer */
+static rt_uint8_t file_cnt = 0; /* count of number of files opened */
+static rt_uint8_t Rxflags = 0; /* rx parameter flags */
+static rt_uint8_t ZF2_OP; /* file transfer option */
+
+void zs_start(char *path);
+static void zsend_init(void);
+static rt_err_t zsend_files(struct zfile *zf);
+static rt_err_t zsend_file(struct zfile *zf,rt_uint8_t *buf, rt_uint16_t len);
+static rt_err_t zsend_file_data(struct zfile *zf);
+static rt_uint16_t zfill_buffer(struct zfile *zf,rt_uint8_t *buf,rt_uint16_t size);
+static rt_err_t zget_sync(void);
+static void zsay_bibi(void);
+
+
+
+
+/* start zmodem send process */
+void zs_start(char *path)
+{
+ struct zfile *zf;
+ rt_err_t res = RT_ERROR;
+ zf = rt_malloc(sizeof(struct zfile));
+ if (zf == RT_NULL)
+ {
+ rt_kprintf("zf: out of memory\r\n");
+ return;
+ }
+ memset(zf, 0, sizeof(struct zfile));
+ zf->fname = path;
+ zf->fd = -1;
+ res = zsend_files(zf);
+ if (!res)
+ {
+ rt_kprintf("\r\nfile: %s \r\nsize: %ld bytes\r\nsend completed.\r\n",
+ zf->fname+1,zf->bytes_received);
+ }
+ else
+ {
+ rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",zf->fname+1);
+ }
+ rt_free(zf);
+ return;
+}
+/* init the parameters */
+static void zsend_init(void)
+{
+ rt_err_t res = -RT_ERROR;
+ zinit_parameter();
+ for(;;) /* wait ZPAD */
+ {
+ res = zread_line(800);
+ if (res == ZPAD) break;
+ }
+ for (;;)
+ {
+ res = zget_header(rx_header);
+ if (res == ZRINIT) break;
+ }
+ if ((rx_header[ZF1] & ZRQNVH))
+ {
+ zput_pos(0x80L); /* Show we can var header */
+ zsend_hex_header(ZRQINIT, tx_header);
+ }
+ Rxflags = rx_header[ZF0] & 0377;
+ if (Rxflags & CANFC32) Txfcs32 = 1; /* used 32bits CRC check */
+
+ if (ZF2_OP == ZTRLE && (Rxflags & CANRLE)) /* for RLE packet */
+ Txfcs32 = 2;
+ else
+ ZF2_OP = 0;
+ /* send SINIT cmd */
+ return;
+}
+/* send files */
+static rt_err_t zsend_files(struct zfile *zf)
+{
+ char *p,*q;
+ char *str = "/";
+ struct stat finfo;
+ rt_err_t res = -RT_ERROR;
+
+ if (zf->fname == RT_NULL)
+ {
+ rt_kprintf("\r\nerror: no file to be send.\r\n");
+ return res;
+ }
+ if ((zf->fd=open(zf->fname, DFS_O_RDONLY,0)) <0)
+ {
+ rt_kprintf("\r\ncan not open file:%s\r\n",zf->fname+1);
+ return res;
+ }
+
+ zf->file_end = 0;
+ ++file_cnt;
+ /* extract file name */
+ p = zf->fname;
+ for (;;)
+ {
+ q = strstr(p,str);
+ if (q == RT_NULL) break;
+ p = q+1;
+ }
+ q = (char*)TX_BUFFER;
+ for (;;)
+ {
+ *q++ = *p++;
+ if (*p == 0) break;
+ }
+ *q++ = 0;
+ p=q;
+ while (q < (char*)(TX_BUFFER + 1024))
+ *q++ = 0;
+ /* get file attributes */
+ fstat(zf->fd,&finfo);
+ Left_sizes += finfo.st_size;
+ rt_sprintf(p, "%lu %lo %o 3 %d %ld", (long)finfo.st_size, finfo.st_mtime,
+ finfo.st_mode, file_cnt, Left_sizes);
+ Left_sizes -= finfo.st_size;
+ TX_BUFFER[127] = (finfo.st_size + 127) >>7;
+ TX_BUFFER[126] = (finfo.st_size + 127) >>15;
+
+ zsend_init();
+ /* start sending files */
+ res = zsend_file(zf,TX_BUFFER, (p-(char*)TX_BUFFER)+strlen(p)+1);
+ zsay_bibi();
+ close(zf->fd);
+/* unlink(path); */
+ return res;
+}
+
+/* send file name and related info */
+static rt_err_t zsend_file(struct zfile *zf,rt_uint8_t *buf, rt_uint16_t len)
+{
+ rt_uint8_t cnt;
+ rt_err_t res = -RT_ERROR;
+ for (cnt=0;cnt<5;cnt++)
+ {
+ tx_header[ZF0] = ZF0_CMD; /* file conversion option */
+ tx_header[ZF1] = ZF1_CMD; /* file management option */
+ tx_header[ZF2] = (ZF3_CMD|ZF2_OP); /* file transfer option */
+ tx_header[ZF3] = ZF3_CMD;
+ zsend_bin_header(ZFILE, tx_header);
+ zsend_bin_data(buf, len, ZCRCW);
+loop:
+ res = zget_header(rx_header);
+ switch (res)
+ {
+ case ZRINIT:
+ while ((res = zread_line(50)) > 0)
+ {
+ if (res == ZPAD)
+ {
+ goto loop;
+ }
+ }
+ break;
+ case ZCAN:
+ case TIMEOUT:
+ case ZABORT:
+ case ZFIN:
+ break;
+ case -RT_ERROR:
+ case ZNAK:
+ break;
+ case ZCRC: /* no CRC request */
+ goto loop;
+ case ZFERR:
+ case ZSKIP:
+ break;
+ case ZRPOS: /* here we want */
+ zget_pos(Rxpos);
+ Txpos = Rxpos;
+ return(zsend_file_data(zf));
+ default:
+ break;
+ }
+ }
+ return res;
+}
+
+/* send the file data */
+static rt_err_t zsend_file_data(struct zfile *zf)
+{
+ rt_int16_t cnt;
+ rt_uint8_t cmd;
+ rt_err_t res = -RT_ERROR;
+ /* send ZDATA packet, start to send data */
+start_send:
+ zput_pos(Txpos);
+ zsend_bin_header(ZDATA, tx_header);
+ do
+ {
+ cnt = zfill_buffer(zf,TX_BUFFER,RX_BUFFER_SIZE);
+ if (cnt < RX_BUFFER_SIZE )
+ cmd = ZCRCE;
+ else
+ cmd = ZCRCG;
+ zsend_bin_data(TX_BUFFER, cnt, cmd);
+ zf->bytes_received= Txpos += cnt;
+ if (cmd == ZCRCW)
+ goto get_syn1;
+ } while (cnt == RX_BUFFER_SIZE);
+ for (;;) /* get ack and check if send finish */
+ {
+ zput_pos(Txpos);
+ zsend_bin_header(ZEOF, tx_header);
+get_syn1:
+ res = zget_sync();
+ switch (res)
+ {
+ case ZACK:
+ goto get_syn1;
+ case ZNAK:
+ continue;
+ case ZRPOS: /* resend here */
+ lseek(zf->fd,Txpos,0);
+ goto start_send;
+ case ZRINIT: /* send finish,then begin to send next file */
+ return RT_EOK;
+ case ZSKIP:
+ case -RT_ERROR:
+ return res;
+ default:
+ return res;
+ }
+ }
+}
+
+/* fill file data to buffer*/
+static rt_uint16_t zfill_buffer(struct zfile *zf,rt_uint8_t *buf,rt_uint16_t size)
+{
+ return (read(zf->fd,buf,size));
+}
+
+/* wait sync(ack) from the receiver */
+static rt_err_t zget_sync(void)
+{
+ rt_err_t res = -RT_ERROR;
+ for (;;)
+ {
+ res = zget_header(rx_header);
+ switch (res)
+ {
+ case ZCAN:
+ case ZABORT:
+ case ZFIN:
+ case TIMEOUT:
+ return -RT_ERROR;
+ case ZRPOS: /* get pos, need to resend */
+ zget_pos(Rxpos);
+ Txpos = Rxpos;
+ return res;
+ case ZACK:
+ return res;
+ case ZRINIT: /* get ZRINIT indicate that the prev file send completed */
+ return res;
+ case ZSKIP:
+ return res;
+ case -RT_ERROR:
+ default:
+ zsend_bin_header(ZNAK, tx_header);
+ continue;
+ }
+ }
+}
+
+/* say "bibi" to the receiver */
+static void zsay_bibi(void)
+{
+ for (;;)
+ {
+ zput_pos(0L); /* reninit position of next file*/
+ zsend_hex_header(ZFIN, tx_header); /* send finished session cmd */
+ switch (zget_header(rx_header))
+ {
+ case ZFIN:
+ zsend_line('O');
+ zsend_line('O');
+ case ZCAN:
+ case TIMEOUT:
+ return;
+ }
+ }
+}
+/* end of sz.c */
diff --git a/components/rzsz/zcore.c b/components/rzsz/zcore.c new file mode 100644 index 0000000000..7c55b3671a --- /dev/null +++ b/components/rzsz/zcore.c @@ -0,0 +1,865 @@ +/* + * File : rz.c
+ * the core functions of implementing zmodem proctocol + * Change Logs: + * Date Author Notes + * 2011-03-29 itspy + */
+
+#include <rtthread.h>
+#include <finsh.h>
+#include <shell.h>
+#include <rtdef.h>
+#include <dfs.h>
+#include <dfs_file.h>
+#include <dfs_posix.h>
+#include <stdio.h>
+#include "zdef.h"
+ +char ZF0_CMD; /* file conversion request */ +char ZF1_CMD; /* file management request */ +char ZF2_CMD; /* file transport request */
+char ZF3_CMD; +rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */ +rt_uint16_t Rxcount; /* received count*/
+char header_type; /* header type */ +rt_uint8_t rx_header[4]; /* received header */ +rt_uint8_t tx_header[4]; /* transmitted header */ +rt_uint32_t Rxpos; /* received file position */ +rt_uint32_t Txpos; /* transmitted file position */ +rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */ +rt_uint8_t TxCRC; /* controls 32 bit CRC being sent */ +rt_uint8_t RxCRC; /* indicates/controls 32 bit CRC being received */ /* 0 == CRC16, 1 == CRC32, 2 == CRC32 + RLE */ +char Attn[ZATTNLEN+1]; /* attention string rx sends to tx on err */
+
+void zinit_parameter(void);
+void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr);
+void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr);
+void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend);
+static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len);
+static rt_int16_t zrec_data32(rt_uint8_t *buf,rt_int16_t len);
+static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len);
+rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len);
+rt_int16_t zget_header(rt_uint8_t *hdr);
+static rt_int16_t zget_bin_header(rt_uint8_t *hdr);
+static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr);
+rt_int16_t zget_hex_header(rt_uint8_t *hdr);
+static void zsend_ascii(rt_uint8_t c);
+void zsend_zdle_char(rt_uint16_t ch);
+static rt_int16_t zget_hex(void);
+rt_int16_t zread_byte(void);
+rt_int16_t zxor_read(void);
+void zput_pos(rt_uint32_t pos);
+void zget_pos(rt_uint32_t pos);
+
+ +
+
+void zinit_parameter(void)
+{
+ rt_uint8_t i;
+
+ ZF0_CMD = /*CANFC32|*/CANFDX|CANOVIO; /* not chose CANFC32,CANRLE,although it have been supported */
+ ZF1_CMD = 0; /* fix header length,not support CANVHDR */
+ ZF2_CMD = 0;
+ ZF3_CMD = 0;
+ Rxframeind =0;
+ header_type = 0;
+ Rxcount = 0;
+ for (i=0;i<4;i++) rx_header[i] = tx_header[i] = 0;
+ Rxpos = Txpos = 0;
+ RxCRC = 0;
+ Txfcs32 = 0;
+ return ;
+}
+
+/* send binary header */ +void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr) +{ + rt_uint8_t i; + rt_uint32_t crc;
+ + zsend_byte(ZPAD);
+ zsend_byte(ZDLE); + TxCRC = Txfcs32;
+ if (TxCRC == 0)
+ { + zsend_byte(ZBIN);
+ zsend_zdle_char(type);
+ /* add 16bits crc */
+ crc = 0L;
+ crc = updcrc16(type, 0);
+ for (i=0;i<4;i++)
+ {
+ zsend_zdle_char(*hdr);
+ crc = updcrc16((0377 & *hdr++),crc);
+ }
+ crc = updcrc16(0,updcrc16(0,crc)); + zsend_zdle_char(((int)(crc>>8))); + zsend_zdle_char(crc);
+ }
+ else if(TxCRC == 1)
+ {
+ zsend_byte(ZBIN32);
+ zsend_zdle_char(type);
+ /* add 32bits crc */
+ crc = 0xffffffffL;
+ crc = updcrc32(type, crc);
+ for (i=0;i<4;i++)
+ {
+ zsend_zdle_char(*hdr);
+ crc = updcrc32((0377 & *hdr++), crc);
+ }
+ crc = ~crc; + for (i=0; i<4;i++)
+ { + zsend_zdle_char(crc); + crc >>= 8; + }
+ }
+ else if (TxCRC == 2)
+ {
+ zsend_byte(ZBINR32);
+ zsend_zdle_char(type);
+ /* add 32bits crc */
+ crc = 0xffffffffL;
+ crc = updcrc32(type, crc);
+ for (i=0;i<4;i++)
+ {
+ zsend_zdle_char(*hdr);
+ crc = updcrc32((0377 & *hdr++), crc);
+ }
+ crc = ~crc; + for (i=0; i<4;i++)
+ { + zsend_zdle_char(crc); + crc >>= 8; + }
+ }
+ return; +} + +/* send hex header */ +void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr) +{ + rt_uint8_t i; + rt_uint16_t crc;
+ + zsend_line(ZPAD); zsend_line(ZPAD); zsend_line(ZDLE); + zsend_line(ZHEX); + zsend_ascii(type); + crc = updcrc16(type, 0); + for (i=0; i<4; i++)
+ { + zsend_ascii(*hdr);
+ crc = updcrc16((0377 & *hdr++), crc); + } + crc = updcrc16(0,updcrc16(0,crc)); + zsend_ascii(crc>>8);
+ zsend_ascii(crc); + /* send display control cmd */ + zsend_line(015); zsend_line(0212); + if (type != ZFIN && type != ZACK) + zsend_line(021);
+ TxCRC = 0; /* clear tx crc type */
+ return; +} + +/* send binary data,with frameend */ +void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend) +{
+ rt_int16_t i,c,tmp; + rt_uint32_t crc;
+ + if (TxCRC == 0) /* send binary data with 16bits crc check */
+ {
+ crc = 0x0L; + for (i=0;i<len;i++)
+ { + zsend_zdle_char(*buf);
+ crc = updcrc16((0377 & *buf++), crc); + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc16(frameend, crc); + crc = updcrc16(0,updcrc16(0,crc)); + zsend_zdle_char(crc>>8);
+ zsend_zdle_char(crc);
+ }
+ else if (TxCRC == 1) /* send binary data with 32 bits crc check */
+ {
+ crc = 0xffffffffL; + for (i=0;i<len;i++)
+ { + c = *buf++ & 0377; + zsend_zdle_char(c); + crc = updcrc32(c, crc); + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc32(frameend, crc); + crc = ~crc; + for (i=0;i<4;i++)
+ { + zsend_zdle_char((int)crc); crc >>= 8; + }
+ }
+ else if (TxCRC == 2) /* send binary data with 32bits crc check,RLE encode */
+ {
+ crc = 0xffffffffL;
+ tmp = *buf++ & 0377; + for (i = 0; --len >= 0; ++buf)
+ { + if ((c = *buf & 0377) == tmp && i < 126 && len>0)
+ { + ++i; continue; + } + if (i==0)
+ { + zsend_zdle_char(tmp); + crc = updcrc32(tmp, crc); + if (tmp == ZRESC)
+ { + zsend_zdle_char(0100); crc = updcrc32(0100, crc); + } + tmp = c;
+ }
+ else if (i == 1)
+ {
+ if (tmp != ZRESC)
+ { + zsend_zdle_char(tmp); zsend_zdle_char(tmp); + crc = updcrc32(tmp, crc); + crc = updcrc32(tmp, crc); + i = 0; tmp = c; + }
+
+ }
+ else
+ {
+ zsend_zdle_char(ZRESC); crc = updcrc32(ZRESC, crc); + if (tmp == 040 && i < 34)
+ { + i += 036; + zsend_zdle_char(i);
+ crc = updcrc32(i, crc); + } + else
+ { + i += 0101; + zsend_zdle_char(i); crc = updcrc32(i, crc); + zsend_zdle_char(tmp); crc = updcrc32(tmp, crc); + } + i = 0; tmp = c;
+ } + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc32(frameend, crc); + crc = ~crc; + for (i=0;i<4;i++)
+ { + zsend_zdle_char(crc);
+ crc >>= 8; + }
+ } + if (frameend == ZCRCW) + zsend_byte(XON);
+ return; +}
+
+/* receive data,with 16bits CRC check */
+static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len)
+{
+ rt_int16_t c,crc_cnt; + rt_uint16_t crc;
+ rt_err_t res = -RT_ERROR;
+ rt_uint8_t *p,flag = 0;
+ p = buf;
+ crc = 0L;
+ Rxcount = 0; + while(buf <= p+len)
+ {
+ if ((res = zread_byte()) & ~0377)
+ {
+ if (res == GOTCRCE || res == GOTCRCG ||
+ res == GOTCRCQ || res == GOTCRCW)
+ {
+ c = res; + crc = updcrc16(res&0377, crc);
+ flag = 1;
+ continue;
+ }
+ else if (res == GOTCAN) return ZCAN;
+ else if (res == TIMEOUT) return TIMEOUT;
+ else return res;
+
+ }
+ else
+ {
+ if (flag)
+ {
+ crc = updcrc16(res, crc);
+ crc_cnt++;
+ if (crc_cnt < 2) continue;
+ if ((crc & 0xffff))
+ {
+#ifdef ZDEBUG + rt_kprintf("error code: CRC16 error \r\n");
+#endif + return -RT_ERROR;
+ }
+ return c;
+ }
+ else
+ {
+ *buf++ = res;
+ Rxcount++;
+ crc = updcrc16(res, crc);
+ }
+ } + }
+ return -RT_ERROR;
+}
+ +/* receive data,with 32bits CRC check */ +static rt_int16_t zrec_data32(rt_uint8_t *buf,rt_int16_t len) +{ + rt_int16_t c,crc_cnt = 0; + rt_uint32_t crc; + rt_err_t res = -RT_ERROR;
+ rt_uint8_t *p,flag = 0;
+ + crc = 0xffffffffL;
+ Rxcount = 0; + while (buf <= p+len)
+ {
+ if ((res = zread_byte()) & ~0377)
+ {
+ if (res == GOTCRCE || res == GOTCRCG ||
+ res == GOTCRCQ || res == GOTCRCW)
+ {
+ c = res; + crc = updcrc32(res&0377, crc);
+ flag = 1;
+ continue;
+ }
+ else if (res == GOTCAN) return ZCAN;
+ else if (res == TIMEOUT) return TIMEOUT;
+ else return res;
+
+ }
+ else
+ {
+ if (flag)
+ {
+ crc = updcrc32(res, crc);
+ crc_cnt++;
+ if (crc_cnt < 4) continue;
+ if ((crc & 0xDEBB20E3))
+ {
+#ifdef ZDEBUG + rt_kprintf("error code: CRC32 error \r\n");
+#endif + return -RT_ERROR;
+ }
+ return c;
+ }
+ else
+ {
+ *buf++ = res;
+ Rxcount++;
+ crc = updcrc32(res, crc);
+ }
+ } + } + return -RT_ERROR; +}
+/* receive data,with RLE encoded,32bits CRC check */ +static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len) +{ + rt_int16_t c,crc_cnt = 0; + rt_uint32_t crc; + rt_err_t res = -RT_ERROR;
+ rt_uint8_t *p,flag = 0;
+ + crc = 0xffffffffL;
+ Rxcount = 0;
+ p = buf; + while (buf <= p+len)
+ { + if ((res = zread_byte()) & ~0377)
+ {
+ if (res == GOTCRCE || res == GOTCRCG ||
+ res == GOTCRCQ || res == GOTCRCW)
+ {
+ c = res; + crc = updcrc32(res&0377, crc);
+ flag = 1;
+ continue;
+ }
+ else if (res == GOTCAN) return ZCAN;
+ else if (res == TIMEOUT) return TIMEOUT;
+ else return res;
+
+ }
+ else
+ {
+ if (flag)
+ {
+ crc = updcrc32(res, crc);
+ crc_cnt++;
+ if (crc_cnt < 4) continue;
+ if ((crc & 0xDEBB20E3))
+ {
+#ifdef ZDEBUG + rt_kprintf("error code: CRC32 error \r\n");
+#endif + return -RT_ERROR;
+ }
+ return c;
+ }
+ else
+ {
+ crc = updcrc32(res, crc); + switch (c)
+ { + case 0: + if (res == ZRESC)
+ { + c = -1; continue; + } + *buf++ = res;
+ Rxcount++;
+ continue; + case -1: + if (res >= 040 && res < 0100)
+ { + c = res - 035; res = 040;
+ goto spaces; + } + if (res == 0100)
+ { + c = 0; + *buf++ = ZRESC;
+ Rxcount++;
+ continue; + } + c = res; continue; + default: + c -= 0100; + if (c < 1) + goto end; +spaces: + if ((buf + c) > p+len) + goto end; + while ( --res >= 0)
+ { + *buf++ = res;
+ Rxcount++;
+ } + c = 0; continue; + }
+ }
+ } // if -else + + } +end: + return -RT_ERROR; +} +rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len) +{ + rt_int16_t res = -RT_ERROR;
+
+ if (RxCRC == 0)
+ {
+ res = zrec_data16(buf,len);
+ }
+ else if (RxCRC == 1)
+ {
+ res = zrec_data32(buf, len);
+ }
+ else if (RxCRC == 2)
+ {
+ res = zrec_data32r(buf, len);
+ }
+ return res; +} +/* get type and cmd of header, fix lenght */ +rt_int16_t zget_header(rt_uint8_t *hdr) +{ + rt_int16_t c,prev_char;
+ rt_uint32_t bit; + rt_uint16_t get_can,step_out;
+ + bit = get_device_speed(); /* getbaudrate */ + Rxframeind = header_type = 0; + step_out = 0;
+ prev_char = 0xff;
+ for (;;)
+ {
+ c = zread_line(100);
+ switch(c)
+ {
+ case 021:
+ case 0221:
+ if (prev_char == CAN) break;
+ if (prev_char == ZCRCW) goto start_again;
+ break;
+ case RCDO:
+ goto end;
+ case TIMEOUT:
+ if (prev_char == CAN) break;
+ if (prev_char == ZCRCW)
+ {
+ c = -RT_ERROR; goto end;
+ }
+ goto end;
+ case ZCRCW:
+ if (prev_char == CAN) goto start_again;
+ break;
+ case CAN:
+get_can:
+ if (++get_can > 5)
+ {
+ c = ZCAN; goto end;
+ }
+ break;
+ case ZPAD:
+ if (prev_char == CAN) break;
+ if (prev_char == ZCRCW) goto start_again;
+ step_out = 1;
+ break;
+ default:
+ if (prev_char == CAN) break;
+ if (prev_char == ZCRCW) goto start_again;
+start_again:
+ if (--bit == 0)
+ {
+ c = GCOUNT; goto end;
+ }
+ get_can = 0;
+ break;
+ }
+ prev_char = c;
+ if (step_out) break; /* exit loop */
+ }
+ step_out = get_can = 0;
+ for (;;)
+ {
+ c = zxor_read();
+ switch(c)
+ {
+ case ZPAD:
+ break;
+ case RCDO:
+ case TIMEOUT:
+ goto end;
+ case ZDLE:
+ step_out = 1;
+ break;
+ default:
+ goto start_again;
+ }
+ if (step_out) break;
+ } + + Rxframeind = c = zxor_read(); + switch (c)
+ {
+ case ZBIN32: + RxCRC = 1; c = zget_bin_fcs(hdr); break; + case ZBINR32: + RxCRC = 2; c = zget_bin_fcs(hdr); break; + case ZBIN: + RxCRC = 0; c = zget_bin_header(hdr); break; + case ZHEX: + RxCRC = 0; c = zget_hex_header(hdr); break; + case CAN: + goto get_can;
+ case RCDO: + case TIMEOUT: + goto end; + default: + goto start_again; + } +end: + return c; +} + +/* receive a binary header */ +static rt_int16_t zget_bin_header(rt_uint8_t *hdr) +{ + rt_int16_t res, i; + rt_uint16_t crc; + + if ((res = zread_byte()) & ~0377) + return res; + header_type = res; + crc = updcrc16(res, 0); + + for (i=0;i<4;i++)
+ { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + *hdr++ = res; + } + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + if (crc & 0xFFFF)
+ { + rt_kprintf("CRC error\n"); + return -RT_ERROR; + } + return header_type; +} + +/* receive a binary header,with 32bits FCS */ +static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr) +{ + rt_int16_t res, i; + rt_uint32_t crc; + + if ((res = zread_byte()) & ~0377) + return res; + header_type = res; + crc = 0xFFFFFFFFL; crc = updcrc32(res, crc); + + for (i=0;i<4;i++) /* 4headers */
+ { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc32(res, crc); + *hdr++ = res; + + } + for (i=0;i<4;i++) /* 4bytes crc */
+ { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc32(res, crc); + + } + if (crc != 0xDEBB20E3)
+ {
+#ifdef ZDEBUG + rt_kprintf("CRC error\n");
+#endif + return -RT_ERROR; + } + return header_type; +} + + +/* receive a hex style header (type and position) */ +rt_int16_t zget_hex_header(rt_uint8_t *hdr) +{ + rt_int16_t res,i; + rt_uint16_t crc;
+ + if ((res = zget_hex()) < 0) + return res; + header_type = res; + crc = updcrc16(res, 0); + + for (i=0;i<4;i++)
+ { + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + *hdr++ = res; + } + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + if (crc & 0xFFFF)
+ {
+#ifdef ZDEBUG + rt_kprintf("error code : CRC error\r\n");
+#endif
+ return -RT_ERROR; + } + res = zread_line(100); + if (res < 0) + return res; + res = zread_line(100); + if (res < 0) + return res; + return header_type; +} + +/* convert to ascii */ +static void zsend_ascii(rt_uint8_t c) +{ + const char hex[] = "0123456789abcdef";
+ + zsend_line(hex[(c&0xF0)>>4]); + zsend_line(hex[(c)&0xF]);
+ return; +} + +/* + * aend character c with ZMODEM escape sequence encoding. + */ +void zsend_zdle_char(rt_uint16_t ch) +{
+ rt_uint16_t res;
+
+ res = ch & 0377; + switch (res)
+ { + case 0377: + zsend_byte(res); + break; + case ZDLE: + zsend_byte(ZDLE);
+ res ^= 0100;
+ zsend_byte(res); + break; + case 021:
+ case 023: + case 0221:
+ case 0223: + zsend_byte(ZDLE);
+ res ^= 0100;
+ zsend_byte(res); + break; + default: + zsend_byte(res); + } +} + +/* decode two lower case hex digits into an 8 bit byte value */ +static rt_int16_t zget_hex(void) +{ + rt_int16_t res,n;
+ + if ((res = zxor_read()) < 0) + return res; + n = res - '0'; + if (n > 9) + n -= ('a' - ':'); + if (n & ~0x0f) + return -RT_ERROR; + if ((res = zxor_read()) < 0) + return res; + res -= '0'; + if (res > 9) + res -= ('a' - ':'); + if (res & ~0x0f) + return -RT_ERROR; + res += (n<<4); + return res; +} + + +/* + * read a byte, checking for ZMODEM escape encoding + * including CAN*5 which represents a quick abort + */ +rt_int16_t zread_byte(void) +{ + register int res; + +again: + /* Quick check for non control characters */ + if ((res = zread_line(100)) & 0140) + return res; + switch (res)
+ { + case ZDLE: + break; + case 023: + case 0223: + case 021: + case 0221: + goto again; + default: + return res; + } +again2: + if ((res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + switch (res)
+ { + case CAN: + return GOTCAN; + case ZCRCE: + case ZCRCG: + case ZCRCQ: + case ZCRCW: + return (res | GOTOR); + case ZRUB0: + return 0177; + case ZRUB1: + return 0377; + case 023: + case 0223: + case 021: + case 0221: + goto again2; + default: + if ((res & 0140) == 0100) + return (res ^ 0100); + break; + } + return -RT_ERROR; +} + +/* + * @read a character from the modem line with timeout. + * @eat parity, XON and XOFF characters. + */ +rt_int16_t zxor_read(void) +{ + rt_int16_t res; + + for (;;)
+ { + if ((res = zread_line(100)) < 0) + return res; + switch (res &= 0177) { + case XON: + case XOFF: + continue; + case '\r': + case '\n': + case ZDLE:
+ default: + return res; + } + } + /* NOTREACHED */ +} + +/* put file posistion into the header*/ +void zput_pos(rt_uint32_t pos) +{ + tx_header[ZP0] = pos; + tx_header[ZP1] = pos>>8; + tx_header[ZP2] = pos>>16; + tx_header[ZP3] = pos>>24;
+ return; +} + +/* Recover a long integer from a header */ +void zget_pos(rt_uint32_t pos) +{ + Rxpos = (rx_header[ZP3] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377); +}
+ +/* End of zm.c */ diff --git a/components/rzsz/zdef.h b/components/rzsz/zdef.h new file mode 100644 index 0000000000..1a06e4bf23 --- /dev/null +++ b/components/rzsz/zdef.h @@ -0,0 +1,217 @@ +#ifndef __ZDEF_H__
+#define __ZDEF_H__
+
+#include <rtthread.h>
+#include "crc.h" +#define ZPAD '*' /* 052 padding character begins frames */ +#define ZDLE 030 /* ctrl-X ZMODEM escape - `ala BISYNC DLE */ +#define ZDLEE (ZDLE^0100) /* escaped ZDLE as transmitted */ +#define ZBIN 'A' /* binary frame indicator (CRC-16) */ +#define ZHEX 'B' /* hex frame indicator */ +#define ZBIN32 'C' /* binary frame with 32 bit FCS */ +#define ZBINR32 'D' /* RLE packed Binary frame with 32 bit FCS */ +#define ZVBIN 'a' /* binary frame indicator (CRC-16) */ +#define ZVHEX 'b' /* hex frame indicator */ +#define ZVBIN32 'c' /* binary frame with 32 bit FCS */ +#define ZVBINR32 'd' /* RLE packed Binary frame with 32 bit FCS */ +#define ZRESC 0176 /* RLE flag/escape character */ + + +/* Frame types */ +#define ZRQINIT 0 /* request receive init */ +#define ZRINIT 1 /* receive init */ +#define ZSINIT 2 /* send init sequence (optional) */ +#define ZACK 3 /* ACK to above */ +#define ZFILE 4 /* file name from sender */ +#define ZSKIP 5 /* ro sender: skip this file */ +#define ZNAK 6 /* last packet was garbled */ +#define ZABORT 7 /* abort batch transfers */ +#define ZFIN 8 /* finish session */ +#define ZRPOS 9 /* resume data trans at this position */ +#define ZDATA 10 /* data packet(s) follow */ +#define ZEOF 11 /* end of file */ +#define ZFERR 12 /* fatal Read or Write error Detected */ +#define ZCRC 13 /* request for file CRC and response */ +#define ZCHALLENGE 14 /* receiver's Challenge */ +#define ZCOMPL 15 /* request is complete */ +#define ZCAN 16 /* other end canned session with CAN*5 */ +#define ZFREECNT 17 /* request for free bytes on filesystem */ +#define ZCOMMAND 18 /* command from sending program */ + +/* ZDLE sequfences */ +#define ZCRCE 'h' /* CRC next, frame ends, header packet follows */ +#define ZCRCG 'i' /* CRC next, frame continues nonstop */ +#define ZCRCQ 'j' /* CRC next, frame continues, ZACK expected */ +#define ZCRCW 'k' /* CRC next, ZACK expected, end of frame */ +#define ZRUB0 'l' /* translate to rubout 0177 */ +#define ZRUB1 'm' /* translate to rubout 0377 */ + +/* zdlread return values (internal) */ +/* -1 is general error, -2 is timeout */ +#define GOTOR 0400 +#define GOTCRCE (ZCRCE|GOTOR) /* ZDLE-ZCRCE received */ +#define GOTCRCG (ZCRCG|GOTOR) /* ZDLE-ZCRCG received */ +#define GOTCRCQ (ZCRCQ|GOTOR) /* ZDLE-ZCRCQ received */ +#define GOTCRCW (ZCRCW|GOTOR) /* ZDLE-ZCRCW received */ +#define GOTCAN (GOTOR|030) /* CAN*5 seen */ + +/* Byte positions within header array */ +#define ZF0 3 /* first flags byte */ +#define ZF1 2 +#define ZF2 1 +#define ZF3 0 +#define ZP0 0 /* low order 8 bits of position */ +#define ZP1 1 +#define ZP2 2 +#define ZP3 3 /* high order 8 bits of file position */ + +/* Parameters for ZRINIT header */ +#define ZRPXWN 8 /* 9th byte in header contains window size/256 */ +#define ZRPXQQ 9 /* 10th to 14th bytes contain quote mask */ +/* Bit Masks for ZRINIT flags byte ZF0 */ +#define CANFDX 01 /* rx can send and receive true FDX */ +#define CANOVIO 02 /* rx can receive data during disk I/O */ +#define CANBRK 04 /* rx can send a break signal */ +#define CANRLE 010 /* receiver can decode RLE */ +#define CANLZW 020 /* receiver can uncompress */ +#define CANFC32 040 /* receiver can use 32 bit Frame Check */ +#define ESCCTL 0100 /* receiver expects ctl chars to be escaped */ +#define ESC8 0200 /* receiver expects 8th bit to be escaped */ + +/* Bit Masks for ZRINIT flags byte ZF1 */ +#define CANVHDR 01 /* variable headers OK */ +#define ZRRQWN 8 /* receiver specified window size in ZRPXWN */ +#define ZRRQQQ 16 /* additional control chars to quote in ZRPXQQ */ +#define ZRQNVH (ZRRQWN|ZRRQQQ) /* variable len hdr reqd to access info */ + +/* Parameters for ZSINIT frame */ +#define ZATTNLEN 32 /* max length of attention string */ +#define ALTCOFF ZF1 /* offset to alternate canit string, 0 if not used */ + +/* Parameters for ZFILE frame */ +/* Conversion options one of these in ZF0 */ +#define ZCBIN 1 /* binary transfer - inhibit conversion */ +#define ZCNL 2 /* convert NL to local end of line convention */ +#define ZCRESUM 3 /* resume interrupted file transfer */ +/* Management include options, one of these ored in ZF1 */ +#define ZMSKNOLOC 0200 /* skip file if not present at rx */ +/* Management options, one of these ored in ZF1 */ +#define ZMMASK 037 /* mask for the choices below */ +#define ZMNEWL 1 /* transfer if source newer or longer */ +#define ZMCRC 2 /* transfer if different file CRC or length */ +#define ZMAPND 3 /* append contents to existing file (if any) */ +#define ZMCLOB 4 /* replace existing file */ +#define ZMNEW 5 /* transfer if source newer */ + /* Number 5 is alive ... */ +#define ZMDIFF 6 /* transfer if dates or lengths different */ +#define ZMPROT 7 /* protect destination file */ +#define ZMCHNG 8 /* change filename if destination exists */ +/* Transport options, one of these in ZF2 */ +#define ZTLZW 1 /* lempel-Ziv compression */ +#define ZTRLE 3 /* run Length encoding */ +/* Extended options for ZF3, bit encoded */ +#define ZXSPARS 64 /* encoding for sparse file operations */ +#define ZCANVHDR 01 /* variable headers OK */ +/* Receiver window size override */ +#define ZRWOVR 4 /* byte position for receive window override/256 */ + +/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */ +#define ZCACK1 1 /* acknowledge, then do command */ +extern char Attn[ZATTNLEN+1]; /* Attention string rx sends to tx on err */ + +/* Globals used by ZMODEM functions */ +extern rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */ +extern char header_type; /* type of header received */
+extern rt_uint8_t rx_header[4]; /* received header */ +extern rt_uint8_t tx_header[4]; /* transmitted header */ +extern rt_uint8_t Txfcs32; /* TRUE means send binary frames with 32 bit FCS */ +extern rt_uint16_t Rxcount; /* count of data bytes received */ +extern rt_uint16_t Rxtimeout; /* tenths of seconds to wait for something */ +extern rt_uint32_t Rxpos; /* received file position */ +extern rt_uint32_t Txpos; /* transmitted file position */ +extern rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */
+
+/* ward Christensen / CP/M parameters - Don't change these! */ +#define ENQ 005 +#define CAN ('X'&037) +#define XOFF ('s'&037) +#define XON ('q'&037) +#define SOH 1 +#define STX 2
+#define ETX 3
+#define SYN 026
+#define ESC 033
+#define WANTG 0107 /* send G not NAK to get nonstop batch xmsn */ +#define EOT 4 +#define ACK 6 +#define NAK 025 +#define CPMEOF 032 +#define WANTCRC 0103 /* send C not NAK to get crc not checksum */ +#define TIMEOUT (-2) +#define RCDO (-3) +#define GCOUNT (-4) +#define ERRORMAX 5 +#define RETRYMAX 5 +#define WCEOT (-10) +#define PATHLEN 257 /* ready for 4.2 bsd ? */ +#define UNIXFILE 0xF000 /* the S_IFMT file mask bit for stat */
+
+
+
+#define BITRATE 115200
+#define TX_BUFFER_SIZE 1024
+#define RX_BUFFER_SIZE 1024 /*sender or receiver's max buffer size */
+extern char ZF0_CMD; /* local ZMODEM file conversion request */ +extern char ZF1_CMD; /* local ZMODEM file management request */
+extern char ZF2_CMD; /* local ZMODEM file management request */
+extern char ZF3_CMD; /* local ZMODEM file management request */
+extern rt_uint32_t Baudrate ;
+extern rt_uint32_t Left_bytes;
+extern rt_uint32_t Left_sizes;
+
+
+struct zmodemf
+{
+ struct rt_semaphore zsem;
+ rt_device_t device;
+};
+extern struct zmodemf zmodem;
+
+struct zfile
+{
+ char *fname;
+ rt_int32_t fd; + rt_uint32_t ctime;
+ rt_uint32_t mode; + rt_uint32_t bytes_total; + rt_uint32_t bytes_sent; + rt_uint32_t bytes_received; + rt_uint32_t file_end;
+
+};
+extern struct finsh_shell* shell;
+
+#define ZDEBUG 1
+/* sz.c */
+extern void zs_start(char *path);
+/* rz.c */
+extern void zr_start(char *path);
+
+/* zcore.c */
+extern void zinit_parameter(void);
+extern rt_int16_t zget_header(rt_uint8_t *hdr);
+extern void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr);
+extern void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr);
+extern rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len);
+extern void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend);
+extern void zput_pos(rt_uint32_t pos);
+extern void zget_pos(rt_uint32_t pos);
+/* zdevice.c */
+extern rt_uint32_t get_device_speed(void);
+extern void zsend_byte(rt_uint16_t c);
+extern void zsend_line(rt_uint16_t c);
+extern rt_int16_t zread_line(rt_uint16_t timeout);
+extern void zsend_break(char *cmd);
+extern void zsend_can(void); +
+#endif /* __ZDEF_H__ */ diff --git a/components/rzsz/zdevice.c b/components/rzsz/zdevice.c new file mode 100644 index 0000000000..b6db451709 --- /dev/null +++ b/components/rzsz/zdevice.c @@ -0,0 +1,109 @@ +/* + * File : zdevice.c
+ * the implemention of zmodem proctocol. + * Change Logs: + * Date Author Notes + * 2011-03-29 itspy + */
+
+#include <rtthread.h>
+#include <finsh.h>
+#include <shell.h>
+#include <rtdef.h>
+#include <dfs.h>
+#include <dfs_file.h>
+#include <dfs_posix.h>
+#include "zdef.h"
+ + +rt_uint32_t Line_left = 0; /* left number of data in the read line buffer*/
+rt_uint32_t Left_sizes = 0; /* left file sizes */
+rt_uint32_t Baudrate = BITRATE; /* console baudrate */ +
+
+
+rt_uint32_t get_device_speed(void)
+{
+ return(Baudrate);
+}
+
+rt_uint32_t get_sys_time(void)
+{
+ return(0L);
+}
+
+void zsend_byte(rt_uint16_t ch)
+{
+ rt_device_write(zmodem.device,0,&ch,1);
+ return;
+}
+
+void zsend_line(rt_uint16_t c)
+{
+ rt_uint16_t ch;
+ ch = (c & 0377);
+ rt_device_write(zmodem.device,0,&ch,1);
+ return;
+} + +static char buf[RX_BUFFER_SIZE + 1];
+ +rt_int16_t zread_line(rt_uint16_t timeout) +{ + char *str;
+
+ if (Line_left > 0)
+ {
+ Line_left -= 1;
+ return (*str++ & 0377);
+ }
+ Line_left = 0;
+ timeout/=5;
+ while (1)
+ {
+ if (rt_sem_take(&zmodem.zsem, RT_TICK_PER_SECOND*timeout) != RT_EOK) continue;
+ Line_left = rt_device_read(shell->device, 0, buf, 1);
+ if (Line_left)
+ {
+ Line_left = Line_left;
+ str = buf;
+ break;
+ }
+ }
+ if (Line_left < 1) return TIMEOUT;
+ Line_left -=1;
+ return (*str++ & 0377); +} + +/* + * send a string to the modem, processing for \336 (sleep 1 sec) + * and \335 (break signal) + */ +void zsend_break(char *cmd) +{
+ + while (*cmd++)
+ { + switch (*cmd)
+ { + case '\336':
+ continue; + case '\335':
+ rt_thread_delay(RT_TICK_PER_SECOND);
+ continue; + default: + zsend_line(*cmd);
+ break; + } + } +}
+/* send cancel string to get the other end to shut up */ +void zsend_can(void) +{ + static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0}; + zsend_break(cmd);
+ rt_kprintf("\x0d"); + Line_left=0; /* clear Line_left */ +}
+ +/* end of rbsb.c */ diff --git a/components/rzsz/zstart.c b/components/rzsz/zstart.c new file mode 100644 index 0000000000..4f805f5a64 --- /dev/null +++ b/components/rzsz/zstart.c @@ -0,0 +1,120 @@ +/* + * File : zstart.c
+ * the implemention of zmodem proctocol. + * Change Logs: + * Date Author Notes + * 2011-03-29 itspy + */
+
+#include <rtthread.h>
+#include <finsh.h>
+#include <shell.h>
+#include <dfs.h>
+#include <dfs_file.h>
+#include "zdef.h"
+
+
+
+struct zmodemf zmodem;
+
+rt_err_t zmodem_rx_ind(rt_device_t dev, rt_size_t size)
+{
+ /* release semaphore */
+ rt_sem_release(&zmodem.zsem);
+
+ return RT_EOK;
+}
+
+void finsh_rz(void *parameter)
+{
+ char *path;
+ rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
+ rt_uint8_t flag;
+
+ flag = RT_DEVICE_FLAG_STREAM;
+ zmodem.device->flag &=(~flag);
+ rt_sem_init(&(zmodem.zsem), "zsem", 0, 0);
+ path = rt_thread_self()->parameter;
+ /* save old rx_indicate */
+ rx_indicate = zmodem.device->rx_indicate;
+ /* set new rx_indicate */
+ rt_device_set_rx_indicate(zmodem.device, zmodem_rx_ind);
+ /* start receive remote files */
+ zr_start(path);
+ zmodem.device->flag |=flag;
+ /* recovery old rx_indicate */
+ rt_device_set_rx_indicate(zmodem.device, rx_indicate);
+ /* finsh>> */
+ rt_kprintf(FINSH_PROMPT);
+}
+void finsh_sz(void *parameter)
+{
+ char *path;
+ rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
+ rt_uint8_t flag;
+
+ flag = RT_DEVICE_FLAG_STREAM;
+ zmodem.device->flag &=(~flag);
+ rt_sem_init(&(zmodem.zsem), "zsem", 0, 0);
+ path = rt_thread_self()->parameter;
+ /* save old rx_indicate */
+ rx_indicate = zmodem.device->rx_indicate;
+ /* set new rx_indicate */
+ rt_device_set_rx_indicate(zmodem.device, zmodem_rx_ind);
+ zs_start(path);
+ zmodem.device->flag |=flag;
+ /* recovery old rx_indicate */
+ rt_device_set_rx_indicate(zmodem.device, rx_indicate);
+ /* finsh>> */
+ rt_kprintf(FINSH_PROMPT);
+}
+
+#ifdef RT_USING_FINSH
+#include <finsh.h>
+#include <shell.h>
+
+static void rz(char *para)
+{
+ rt_thread_t init_thread;
+ rt_device_t device;
+ const char* device_name = finsh_get_device();
+
+ device = rt_device_find(device_name);
+ if( device == RT_NULL )
+ {
+ rt_kprintf("%s not find\r\n",device_name);
+ }
+ zmodem.device = device;
+ init_thread = rt_thread_create("rz",
+ finsh_rz,
+ (void*)para,
+ 2048,
+ rt_thread_self()->current_priority+1,
+ 20);
+
+ if (init_thread != RT_NULL) rt_thread_startup(init_thread);
+}
+FINSH_FUNCTION_EXPORT(rz, receive files by zmodem protocol)
+static void sz(char *para)
+{
+ rt_thread_t init_thread;
+ rt_device_t device;
+ const char* device_name = finsh_get_device();
+
+ device = rt_device_find(device_name);
+ if( device == RT_NULL )
+ {
+ rt_kprintf("%s not find\r\n",device_name);
+ }
+ zmodem.device = device;
+ init_thread = rt_thread_create("sz",
+ finsh_sz,
+ (void*)para,
+ 2048,
+ rt_thread_self()->current_priority+1,
+ 20);
+
+ if (init_thread != RT_NULL) rt_thread_startup(init_thread);
+}
+FINSH_FUNCTION_EXPORT(sz, send files by zmodem protocol)
+#endif
|
