add DFS_USING_WORKDIR pre-define

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1443 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
itspy.wei 2011-06-02 03:55:00 +00:00
parent b41b576be9
commit 459e51fb46
4 changed files with 186 additions and 161 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Crc calculation stuff * crc calculation stuff
*/ */
/* crctab calculated by Mark G. Mendel, Network Systems Corporation */ /* crctab calculated by Mark G. Mendel, Network Systems Corporation */
@ -126,4 +126,4 @@ static unsigned long cr3tab[] = { /* CRC polynomial 0xedb88320 */
#define updcrc32(b, c) (cr3tab[((int)c ^ b) & 0xff] ^ ((c >> 8) & 0x00FFFFFF)) #define updcrc32(b, c) (cr3tab[((int)c ^ b) & 0xff] ^ ((c >> 8) & 0x00FFFFFF))
/* End of crctab.c */ /* End of crc.c */

View File

@ -34,8 +34,9 @@ void zr_start(char *path)
{ {
struct zfile *zf; struct zfile *zf;
rt_uint8_t n; rt_uint8_t n;
char ch; char ch,*p,*q;
rt_err_t res = -RT_ERROR; rt_err_t res = -RT_ERROR;
zf = rt_malloc(sizeof(struct zfile)); zf = rt_malloc(sizeof(struct zfile));
if (zf == RT_NULL) if (zf == RT_NULL)
{ {
@ -45,30 +46,35 @@ void zr_start(char *path)
memset(zf, 0, sizeof(struct zfile)); memset(zf, 0, sizeof(struct zfile));
zf->fname = path; zf->fname = path;
zf->fd = -1; zf->fd = -1;
rt_kprintf("\r\n");
res = zrec_files(zf); res = zrec_files(zf);
p = zf->fname;
for (;;)
{
q = strstr(p,"/");
if (q == RT_NULL) break;
p = q+1;
}
if (res == RT_EOK) if (res == RT_EOK)
{ {
rt_kprintf("\b\b\bfile: %s \r\n",zf->fname+1); rt_kprintf("\b\b\bfile: %s \r\n",p);
rt_kprintf("size: %ld bytes\r\n",zf->bytes_received); rt_kprintf("size: %ld bytes\r\n",zf->bytes_received);
rt_kprintf("receive completed.\r\n"); rt_kprintf("receive completed.\r\n");
close(zf->fd); close(zf->fd);
rt_free(zf->fname); rt_free(zf->fname);
rt_free(zf);
} }
else else
{ {
rt_kprintf("\b\b\bfile: %s \r\n",zf->fname+1); rt_kprintf("\b\b\bfile: %s \r\n",p);
rt_kprintf("size: 0 bytes\r\n"); rt_kprintf("size: 0 bytes\r\n");
rt_kprintf("receive failed.\r\n"); rt_kprintf("receive failed.\r\n");
if (zf->fd >= 0) if (zf->fd >= 0)
{ {
close(zf->fd); close(zf->fd);
unlink(zf->fname); /* remove this file */ unlink(zf->fname); /* remove this file */
}
rt_free(zf->fname); rt_free(zf->fname);
rt_free(zf);
} }
}
rt_free(zf);
/* waiting,clear console buffer */ /* waiting,clear console buffer */
rt_thread_delay(RT_TICK_PER_SECOND/2); rt_thread_delay(RT_TICK_PER_SECOND/2);
while(1) while(1)
@ -76,17 +82,16 @@ void zr_start(char *path)
n=rt_device_read(shell->device, 0, &ch, 1); n=rt_device_read(shell->device, 0, &ch, 1);
if (n == 0) break; if (n == 0) break;
} }
return ; return ;
} }
/* receiver init, wait for ack */ /* receiver init, wait for ack */
static rt_err_t zrec_init(rt_uint8_t *rxbuf,struct zfile *zf) static rt_err_t zrec_init(rt_uint8_t *rxbuf,struct zfile *zf)
{ {
rt_uint8_t err_cnt = 0; rt_uint8_t err_cnt = 0;
rt_err_t res = -RT_ERROR; rt_err_t res = -RT_ERROR;
for (;;) for (;;)
{ {
zput_pos(0L); zput_pos(0L);
@ -144,14 +149,15 @@ static rt_err_t zrec_files(struct zfile *zf)
{ {
rt_uint8_t *rxbuf; rt_uint8_t *rxbuf;
rt_err_t res = -RT_ERROR; rt_err_t res = -RT_ERROR;
zinit_parameter(); zinit_parameter();
rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t)); rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t));
if (rxbuf == RT_NULL) if (rxbuf == RT_NULL)
{ {
rt_kprintf("not enough memory\r\n"); rt_kprintf("rxbuf: out of memory\r\n");
return -RT_ERROR; return -RT_ERROR;
} }
rt_kprintf("rz: ready...\r\n"); /* here ready to receive things */ rt_kprintf("\r\nrz: ready...\r\n"); /* here ready to receive things */
if ((res = zrec_init(rxbuf,zf))!= RT_EOK) if ((res = zrec_init(rxbuf,zf))!= RT_EOK)
{ {
rt_kprintf("\b\b\breceive init failed\r\n"); rt_kprintf("\b\b\breceive init failed\r\n");
@ -181,6 +187,7 @@ static rt_err_t zrec_file(rt_uint8_t *rxbuf,struct zfile *zf)
{ {
rt_err_t res = - RT_ERROR; rt_err_t res = - RT_ERROR;
rt_uint16_t err_cnt = 0; rt_uint16_t err_cnt = 0;
do do
{ {
zput_pos(zf->bytes_received); zput_pos(zf->bytes_received);
@ -223,7 +230,6 @@ again:
#ifdef ZDEBUG #ifdef ZDEBUG
rt_kprintf("error code: sender cancelled \r\n"); rt_kprintf("error code: sender cancelled \r\n");
#endif #endif
unlink(zf->fname);
zf->bytes_received = 0L; /* throw the received data */ zf->bytes_received = 0L; /* throw the received data */
return res; return res;
case ZSKIP: case ZSKIP:
@ -237,6 +243,7 @@ again:
continue; continue;
} }
} while(++err_cnt < 100); } while(++err_cnt < 100);
return res; return res;
} }
@ -244,7 +251,7 @@ again:
static rt_err_t zget_file_info(char *name,struct zfile *zf) static rt_err_t zget_file_info(char *name,struct zfile *zf)
{ {
char *p; char *p;
char *ftemp,*ptr; char *full_path,*ptr;
rt_uint16_t i,len; rt_uint16_t i,len;
rt_err_t res = -RT_ERROR; rt_err_t res = -RT_ERROR;
struct statfs buf; struct statfs buf;
@ -256,25 +263,27 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
} }
else else
len = strlen(zf->fname)+strlen(name)+2; len = strlen(zf->fname)+strlen(name)+2;
ftemp = rt_malloc(len); full_path = rt_malloc(len);
if (ftemp == RT_NULL) if (full_path == RT_NULL)
{ {
zsend_can(); zsend_can();
rt_kprintf("\b\b\bftemp: out of memory\n"); rt_kprintf("\b\b\bfull_path: out of memory\n");
rt_free(full_path);
return -RT_ERROR; return -RT_ERROR;
} }
memset(ftemp,0,len); memset(full_path,0,len);
for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++) for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++)
ftemp[i] = *ptr++; full_path[i] = *ptr++;
ftemp[len-strlen(name)-2] = '/'; full_path[len-strlen(name)-2] = '/';
/* check if is a directory */ /* check if is a directory */
if ((zf->fd=open(ftemp, DFS_O_DIRECTORY,0)) < 0) if ((zf->fd=open(full_path, DFS_O_DIRECTORY,0)) < 0)
{ {
zsend_can(); zsend_can();
rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1); rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1);
close(zf->fd); close(zf->fd);
zf->fd = -1; zf->fd = -1;
rt_free(full_path);
return res; return res;
} }
fstat(zf->fd, &finfo); fstat(zf->fd, &finfo);
@ -286,18 +295,23 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
} }
close(zf->fd); close(zf->fd);
/* get fullpath && file attributes */ /* get fullpath && file attributes */
strcat(ftemp,name); strcat(full_path,name);
zf->fname = ftemp; zf->fname = full_path;
p = strlen(name)+name+1; p = strlen(name)+name+1;
sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode); sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode);
#ifdef DFS_USING_WORKDIR
dfs_statfs(working_directory,&buf); dfs_statfs(working_directory,&buf);
if (zf->bytes_total > (buf.f_blocks * buf.f_bfree)) if (zf->bytes_total > (buf.f_blocks * buf.f_bfree))
{ {
zsend_can(); zsend_can();
rt_kprintf("\b\b\bnot enough disk space\r\n"); rt_kprintf("\b\b\bnot enough disk space\r\n");
zf->fd = -1; zf->fd = -1;
rt_free(full_path);
return -RT_ERROR; return -RT_ERROR;
} }
#else
buf = buf;
#endif
zf->bytes_received = 0L; zf->bytes_received = 0L;
if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */ if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */
{ {
@ -305,6 +319,7 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname); rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname);
return -RT_ERROR; return -RT_ERROR;
} }
return RT_EOK; return RT_EOK;
} }
@ -312,6 +327,7 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
static rt_err_t zrec_file_data(rt_uint8_t *buf,struct zfile *zf) static rt_err_t zrec_file_data(rt_uint8_t *buf,struct zfile *zf)
{ {
rt_err_t res = -RT_ERROR; rt_err_t res = -RT_ERROR;
more_data: more_data:
res = zget_data(buf,RX_BUFFER_SIZE); res = zget_data(buf,RX_BUFFER_SIZE);
switch(res) switch(res)
@ -355,7 +371,6 @@ more_data:
/* write file */ /* write file */
static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf) static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf)
{ {
// return 0;
return (write(zf->fd,buf,size)); return (write(zf->fd,buf,size));
} }
@ -363,6 +378,7 @@ static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf)
static void zrec_ack_bibi(void) static void zrec_ack_bibi(void)
{ {
rt_uint8_t i; rt_uint8_t i;
zput_pos(0L); zput_pos(0L);
for (i=0;i<3;i++) for (i=0;i<3;i++)
{ {

View File

@ -39,30 +39,39 @@ void zs_start(char *path)
{ {
struct zfile *zf; struct zfile *zf;
rt_err_t res = RT_ERROR; rt_err_t res = RT_ERROR;
char *p,*q;
zf = rt_malloc(sizeof(struct zfile)); zf = rt_malloc(sizeof(struct zfile));
if (zf == RT_NULL) if (zf == RT_NULL)
{ {
rt_kprintf("zf: out of memory\r\n"); rt_kprintf("zf: out of memory\r\n");
return; return;
} }
rt_kprintf("\r\nsz: ready...\r\n"); /* here ready to send things */
memset(zf, 0, sizeof(struct zfile)); memset(zf, 0, sizeof(struct zfile));
zf->fname = path; zf->fname = path;
zf->fd = -1; zf->fd = -1;
res = zsend_files(zf); res = zsend_files(zf);
if (!res) p = zf->fname;
for (;;)
{
q = strstr(p,"/");
if (q == RT_NULL) break;
p = q+1;
}
if (res == RT_EOK)
{ {
rt_kprintf("\r\nfile: %s \r\nsize: %ld bytes\r\nsend completed.\r\n", rt_kprintf("\r\nfile: %s \r\nsize: %ld bytes\r\nsend completed.\r\n",
zf->fname+1,zf->bytes_received); p,zf->bytes_received);
} }
else else
{ {
rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",zf->fname+1); rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",p);
} }
rt_free(zf); rt_free(zf);
return; return;
} }
/* init the parameters */ /* init the parameters */
static void zsend_init(void) static void zsend_init(void)
{ {
@ -94,6 +103,7 @@ static void zsend_init(void)
/* send SINIT cmd */ /* send SINIT cmd */
return; return;
} }
/* send files */ /* send files */
static rt_err_t zsend_files(struct zfile *zf) static rt_err_t zsend_files(struct zfile *zf)
{ {
@ -147,7 +157,7 @@ static rt_err_t zsend_files(struct zfile *zf)
res = zsend_file(zf,TX_BUFFER, (p-(char*)TX_BUFFER)+strlen(p)+1); res = zsend_file(zf,TX_BUFFER, (p-(char*)TX_BUFFER)+strlen(p)+1);
zsay_bibi(); zsay_bibi();
close(zf->fd); close(zf->fd);
/* unlink(path); */
return res; return res;
} }

View File

@ -63,7 +63,6 @@ rt_int16_t zread_line(rt_uint16_t timeout)
timeout/=5; timeout/=5;
while (1) 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); Line_left = rt_device_read(shell->device, 0, buf, 1);
if (Line_left) if (Line_left)
{ {