rt-thread/components/utilities/zmodem/rz.c

403 lines
10 KiB
C
Raw Normal View History

/*
2013-01-08 21:05:02 +08:00
* File : rz.c
2021-03-08 18:19:04 +08:00
* the implemention of receiving files from the remote computers
* through the zmodem protocol.
* Change Logs:
* Date Author Notes
2021-03-08 18:19:04 +08:00
* 2011-03-29 itspy
* 2011-12-12 aozima fixed syntax error.
2013-01-08 21:05:02 +08:00
*/
#include <rtthread.h>
#include <finsh.h>
#include <shell.h>
#include <rtdef.h>
#include <dfs.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.h>
2013-01-08 21:05:02 +08:00
#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;
2021-03-08 18:19:04 +08:00
char ch,*p,*q;
rt_err_t res = -RT_ERROR;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
zf = rt_malloc(sizeof(struct zfile));
if (zf == RT_NULL)
{
rt_kprintf("zf: out of memory\r\n");
return;
}
rt_memset(zf, 0, sizeof(struct zfile));
2013-01-08 21:05:02 +08:00
zf->fname = path;
2021-03-08 18:19:04 +08:00
zf->fd = -1;
res = zrec_files(zf);
p = zf->fname;
for (;;)
{
q = strstr(p,"/");
if (q == RT_NULL) break;
p = q+1;
}
2013-01-08 21:05:02 +08:00
if (res == RT_EOK)
2021-03-08 18:19:04 +08:00
{
2013-01-08 21:05:02 +08:00
rt_kprintf("\b\b\bfile: %s \r\n",p);
2021-03-08 18:19:04 +08:00
rt_kprintf("size: %ld bytes\r\n",zf->bytes_received);
rt_kprintf("receive completed.\r\n");
close(zf->fd);
rt_free(zf->fname);
2013-01-08 21:05:02 +08:00
}
else
{
rt_kprintf("\b\b\bfile: %s \r\n",p);
2021-03-08 18:19:04 +08:00
rt_kprintf("size: 0 bytes\r\n");
rt_kprintf("receive failed.\r\n");
if (zf->fd >= 0)
{
close(zf->fd);
unlink(zf->fname); /* remove this file */
rt_free(zf->fname);
}
}
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;
2013-01-08 21:05:02 +08:00
}
2021-03-08 18:19:04 +08:00
return ;
2013-01-08 21:05:02 +08:00
}
/* receiver init, wait for ack */
static rt_err_t zrec_init(rt_uint8_t *rxbuf, struct zfile *zf)
{
rt_uint8_t err_cnt = 0;
2021-03-08 18:19:04 +08:00
rt_err_t res = -RT_ERROR;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
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);
2013-01-08 21:05:02 +08:00
again:
res = zget_header(rx_header);
2021-03-08 18:19:04 +08:00
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 ((res =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;
}
}
2013-01-08 21:05:02 +08:00
}
/* receive files */
static rt_err_t zrec_files(struct zfile *zf)
{
2021-03-08 18:19:04 +08:00
rt_uint8_t *rxbuf;
rt_err_t res = -RT_ERROR;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
zinit_parameter();
rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t));
if (rxbuf == RT_NULL)
{
rt_kprintf("rxbuf: out of memory\r\n");
return -RT_ERROR;
}
rt_kprintf("\r\nrz: ready...\r\n"); /* here ready to receive things */
if ((res = zrec_init(rxbuf,zf))!= RT_EOK)
{
rt_kprintf("\b\b\breceive init failed\r\n");
rt_free(rxbuf);
return -RT_ERROR;
}
res = zrec_file(rxbuf,zf);
if (res == ZFIN)
{
2013-01-08 21:05:02 +08:00
rt_free(rxbuf);
2021-03-08 18:19:04 +08:00
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;
}
2013-01-08 21:05:02 +08:00
}
/* receive file */
static rt_err_t zrec_file(rt_uint8_t *rxbuf, struct zfile *zf)
{
2021-03-08 18:19:04 +08:00
rt_err_t res = - RT_ERROR;
rt_uint16_t err_cnt = 0;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
do
{
zput_pos(zf->bytes_received);
zsend_hex_header(ZRPOS, tx_header);
2013-01-08 21:05:02 +08:00
again:
res = zget_header(rx_header);
2021-03-08 18:19:04 +08:00
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 */
2013-01-08 21:05:02 +08:00
case ZFIN:
2021-03-08 18:19:04 +08:00
zrec_ack_bibi();
return ZCOMPL;
case ZCAN:
2013-01-08 21:05:02 +08:00
#ifdef ZDEBUG
rt_kprintf("error code: sender cancelled \r\n");
#endif
2021-03-08 18:19:04 +08:00
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);
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
return res;
2013-01-08 21:05:02 +08:00
}
/* proccess file infomation */
static rt_err_t zget_file_info(char *name, struct zfile *zf)
{
2021-03-08 18:19:04 +08:00
char *p;
char *full_path,*ptr;
rt_uint16_t i,len;
rt_err_t res = -RT_ERROR;
struct statfs buf;
struct stat finfo;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
if (zf->fname == RT_NULL) /* extract file path */
2013-01-08 21:05:02 +08:00
{
2021-03-08 18:19:04 +08:00
len = strlen(name)+2;
}
else
len = strlen(zf->fname)+strlen(name)+2;
2013-01-08 21:05:02 +08:00
full_path = rt_malloc(len);
2021-03-08 18:19:04 +08:00
if (full_path == RT_NULL)
{
zsend_can();
rt_kprintf("\b\b\bfull_path: out of memory\n");
rt_free(full_path);
return -RT_ERROR;
}
rt_memset(full_path,0,len);
2013-01-08 21:05:02 +08:00
for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++)
2021-03-08 18:19:04 +08:00
full_path[i] = *ptr++;
2013-01-08 21:05:02 +08:00
full_path[len-strlen(name)-2] = '/';
2021-03-08 18:19:04 +08:00
/* check if is a directory */
if ((zf->fd=open(full_path, DFS_O_DIRECTORY,0)) < 0)
{
zsend_can();
rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1);
close(zf->fd);
zf->fd = -1;
rt_free(full_path);
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 */
2013-01-08 21:05:02 +08:00
strcat(full_path,name);
zf->fname = full_path;
2021-03-08 18:19:04 +08:00
p = strlen(name)+name+1;
sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode);
2013-01-08 21:05:02 +08:00
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
2021-03-08 18:19:04 +08:00
dfs_statfs(working_directory,&buf);
if (zf->bytes_total > (buf.f_blocks * buf.f_bfree))
{
zsend_can();
rt_kprintf("\b\b\bnot enough disk space\r\n");
zf->fd = -1;
rt_free(full_path);
return -RT_ERROR;
}
2013-01-08 21:05:02 +08:00
#else
buf = buf;
#endif
2021-03-08 18:19:04 +08:00
zf->bytes_received = 0L;
if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */
{
zsend_can();
rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname);
return -RT_ERROR;
}
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
return RT_EOK;
2013-01-08 21:05:02 +08:00
}
/* 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:
2021-03-08 18:19:04 +08:00
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:
2013-01-08 21:05:02 +08:00
#ifdef ZDEBUG
2021-03-08 18:19:04 +08:00
rt_kprintf("error code : ZCAN \r\n");
2013-01-08 21:05:02 +08:00
#endif
2021-03-08 18:19:04 +08:00
return res;
case TIMEOUT:
return res;
2013-01-08 21:05:02 +08:00
case -RT_ERROR:
2021-03-08 18:19:04 +08:00
zsend_break(Attn);
return res;
default:
return res;
}
2013-01-08 21:05:02 +08:00
}
/* write file */
static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size, struct zfile *zf)
{
2021-03-08 18:19:04 +08:00
return (write(zf->fd,buf,size));
2013-01-08 21:05:02 +08:00
}
/* ack bibi */
static void zrec_ack_bibi(void)
{
2021-03-08 18:19:04 +08:00
rt_uint8_t i;
2013-01-08 21:05:02 +08:00
2021-03-08 18:19:04 +08:00
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;
}
}
2013-01-08 21:05:02 +08:00
}
/* end of rz.c */