[components] remove zmodem (#7801)

This commit is contained in:
Man, Jianting (Meco) 2023-07-09 08:13:12 -04:00 committed by GitHub
parent 237bbfd968
commit 7cdf0ac5cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 0 additions and 2191 deletions

View File

@ -1,129 +0,0 @@
/*
* crc calculation stuff
*/
/* crctab calculated by Mark G. Mendel, Network Systems Corporation */
static unsigned short crctab[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
/*
* updcrc macro derived from article Copyright (C) 1986 Stephen Satchell.
* NOTE: First srgument must be in range 0 to 255.
* Second argument is referenced twice.
*
* Programmers may incorporate any or all code into their programs,
* giving proper credit within the source. Publication of the
* source routines is permitted so long as proper credit is given
* to Stephen Satchell, Satchell Evaluations and Chuck Forsberg,
* Omen Technology.
*/
#define updcrc16(cp, crc) ( crctab[((crc >> 8) & 255)] ^ (crc << 8) ^ cp)
/*
* Copyright (C) 1986 Gary S. Brown. You may use this program, or
* code or tables extracted from it, as desired without restriction.
*/
/* First, the polynomial itself and its table of feedback terms. The */
/* polynomial is */
/* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */
/* Note that we take it "backwards" and put the highest-order term in */
/* the lowest-order bit. The X^32 term is "implied"; the LSB is the */
/* X^31 term, etc. The X^0 term (usually shown as "+1") results in */
/* the MSB being 1. */
/* Note that the usual hardware shift register implementation, which */
/* is what we're using (we're merely optimizing it by doing eight-bit */
/* chunks at a time) shifts bits into the lowest-order term. In our */
/* implementation, that means shifting towards the right. Why do we */
/* do it this way? Because the calculated CRC must be transmitted in */
/* order from highest-order term to lowest-order term. UARTs transmit */
/* characters in order from LSB to MSB. By storing the CRC this way, */
/* we hand it to the UART in the order low-byte to high-byte; the UART */
/* sends each low-bit to hight-bit; and the result is transmission bit */
/* by bit from highest- to lowest-order term without requiring any bit */
/* shuffling on our part. Reception works similarly. */
/* The feedback terms table consists of 256, 32-bit entries. Notes: */
/* */
/* The table can be generated at runtime if desired; code to do so */
/* is shown later. It might not be obvious, but the feedback */
/* terms simply represent the results of eight shift/xor opera- */
/* tions for all combinations of data and CRC register values. */
/* */
/* The values must be right-shifted by eight bits by the "updcrc" */
/* logic; the shift must be unsigned (bring in zeroes). On some */
/* hardware you could probably optimize the shift in assembler by */
/* using byte-swap instructions. */
static unsigned long cr3tab[] = { /* CRC polynomial 0xedb88320 */
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
#define updcrc32(b, c) (cr3tab[((int)c ^ b) & 0xff] ^ ((c >> 8) & 0x00FFFFFF))
/* End of crc.c */

View File

@ -1,402 +0,0 @@
/*
* File : rz.c
* the implemention of receiving files from the remote computers
* through the zmodem protocol.
* Change Logs:
* Date Author Notes
* 2011-03-29 itspy
* 2011-12-12 aozima fixed syntax error.
*/
#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>
#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,*p,*q;
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;
}
rt_memset(zf, 0, sizeof(struct zfile));
zf->fname = path;
zf->fd = -1;
res = zrec_files(zf);
p = zf->fname;
for (;;)
{
q = strstr(p,"/");
if (q == RT_NULL) break;
p = q+1;
}
if (res == RT_EOK)
{
rt_kprintf("\b\b\bfile: %s \r\n",p);
rt_kprintf("size: %ld bytes\r\n",zf->bytes_received);
rt_kprintf("receive completed.\r\n");
close(zf->fd);
rt_free(zf->fname);
}
else
{
rt_kprintf("\b\b\bfile: %s \r\n",p);
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;
}
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 ((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;
}
}
}
/* 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("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)
{
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
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 *full_path,*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;
full_path = rt_malloc(len);
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);
for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++)
full_path[i] = *ptr++;
full_path[len-strlen(name)-2] = '/';
/* 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 */
strcat(full_path,name);
zf->fname = full_path;
p = strlen(name)+name+1;
sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode);
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
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;
}
#else
buf = buf;
#endif
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;
}
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 */

View File

@ -1,322 +0,0 @@
/*
* File : sz.c
* the implemention of sending files to the remote computers
* through the zmodem protocol.
* 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 <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.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;
char *p,*q;
zf = rt_malloc(sizeof(struct zfile));
if (zf == RT_NULL)
{
rt_kprintf("zf: out of memory\r\n");
return;
}
rt_kprintf("\r\nsz: ready...\r\n"); /* here ready to send things */
rt_memset(zf, 0, sizeof(struct zfile));
zf->fname = path;
zf->fd = -1;
res = zsend_files(zf);
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",
p,zf->bytes_received);
}
else
{
rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",p);
}
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);
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 */

View File

@ -1,886 +0,0 @@
/*
* File : rz.c
* the core functions of implementing zmodem protocol
* 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 <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.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_cnt = 0; crc = 0L;
Rxcount = 0;
while(buf <= p+len)
{
if ((res = zread_byte()) & ~0377)
{
if (res == GOTCRCE || res == GOTCRCG ||
res == GOTCRCQ || res == GOTCRCW)
{
c = res;
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;
rt_uint32_t crc;
rt_err_t res = -RT_ERROR;
rt_uint8_t *p,flag = 0;
p = buf;
crc_cnt = 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;
rt_uint32_t crc;
rt_err_t res = -RT_ERROR;
rt_uint8_t *p,flag = 0;
crc_cnt = 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_baud(); /* get console baud rate */
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;
}
}
}
/* 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);
return;
}
/* end of zcore.c */

View File

@ -1,217 +0,0 @@
#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 0x01 /* rx can send and receive true FDX */
#define CANOVIO 0x02 /* rx can receive data during disk I/O */
#define CANBRK 0x04 /* rx can send a break signal */
#define CANRLE 0x10 /* receiver can decode RLE */
#define CANLZW 0x20 /* receiver can uncompress */
#define CANFC32 0x28 /* receiver can use 32 bit Frame Check */
#define ESCCTL 0x64 /* receiver expects ctl chars to be escaped */
#define ESC8 0xc8 /* 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 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 0
/* 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_baud(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__ */

View File

@ -1,115 +0,0 @@
/*
* File : zdevice.c
* the implemention of zmodem protocol.
* 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 <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.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_baud(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;
}
rt_int16_t zread_line(rt_uint16_t timeout)
{
char *str;
static char buf[10];
if (Line_left > 0)
{
Line_left -= 1;
return (*str++ & 0377);
}
Line_left = 0;
timeout/=5;
while (1)
{
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 */
return;
}
/* end of zdevice.c */

View File

@ -1,120 +0,0 @@
/*
* File : zstart.c
* the implemention of zmodem protocol.
* 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, RT_NULL);
/* 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