?? zmodem.c
字號:
/* ZFILE .... Zmodem filename received */
/* ZCOMPL ... transaction finished */
/* ERROR .... any other condition */
/*--------------------------------------------------------------------------*/
static int zInitReceiver( void ) /* 初始化接收器 ,收ZFILE之前 */
{
int iHdrBits,iErrors = 0;
for ( iHdrBits = 3; --iHdrBits >= 0; )
{
zPutLongIntoHeader( 0L );
gpcTxhdr[ ZF0 ] = CANFDX; /* | CANOVIO; */
zSendHexHeader( gcTryZhdrType, gpcTxhdr );
AGAIN:
switch ( zGetHeader( gpcRxhdr ) )
{
case ZRQINIT: /*USED*/
zPutLongIntoHeader( 0L );
gpcTxhdr[ ZF0 ] = CANFDX; /* | CANOVIO; */
zSendHexHeader( ZRINIT, gpcTxhdr );
goto AGAIN;
case ZFILE: /*USED*/
gcZconv = gpcRxhdr[ ZF0 ];
gcTryZhdrType = ZRINIT;
if( zReceiveData( gpcRecBuf,WAZOOMAX ) == GOTCRCW )
{
return ( ZFILE );
}
zSendHexHeader( ZNAK, gpcTxhdr );
goto AGAIN;
case ZSINIT: /*not used*/
if( zReceiveData( gpcAttn, ZATTNLEN ) == GOTCRCW )
{
zSendHexHeader( ZACK,gpcTxhdr );
}
else
{
zSendHexHeader( ZNAK,gpcTxhdr );
}
goto AGAIN;
case ZFREECNT: /*not used*/
zPutLongIntoHeader( WAZOOMAX );
zSendHexHeader( ZACK, gpcTxhdr );
goto AGAIN;
case ZCOMMAND: /*not used*/
/*-----------------------------------------*/
/* Paranoia is good for you... */
/* Ignore command from remote, but lie and */
/* say we did the command ok. */
/*-----------------------------------------*/
if ( zReceiveData( gpcRecBuf,WAZOOMAX ) == GOTCRCW )
{
zPutLongIntoHeader( 0L );
do
{
zSendHexHeader( ZCOMPL, gpcTxhdr );
} while ( ++iErrors < 10 && zGetHeader( gpcRxhdr ) != ZFIN );
zAckBibi( );
return ( ZCOMPL );
}
else
{
zSendHexHeader( ZNAK, gpcTxhdr );
}
goto AGAIN;
case ZCOMPL: /*not used*/
goto AGAIN;
case ZFIN: /*USED*/
zAckBibi( );
return ( ZCOMPL );
case ZCAN: /*USED*/
case ZZRCDO:
return ( ZZERROR );
case ZZTIMEOUT: /*USED*/
return ( ZZERROR );
}
}
return ( ZZERROR );
}
/*--------------------------------------------------------------------------*/
/* RZ GET HEADER */
/* Process incoming file information header */
/*--------------------------------------------------------------------------*/
static int zGetZFILEHeader( void ) /* 收ZFILE frame的header */
{
char *pcTempBuf;
char i = 0;
giFilesize = 0L;
pcTempBuf = gpcRecBuf;
while ( *pcTempBuf )
{
gpcFilename[ i++ ] = *p++;
}
gpcFilename[ i ] = '\0';
if( ( giFdRevFile = open ( "gpcFilename", O_RDWR | O_CREAT , 0644 ) ) == ERROR )
{
printf(" [zGetZFILEHeader]:Creat file failed ! " );
return( 0 );
}
pcTempBuf++;
while ( *pcTempBuf )
{
giFilesize = giFilesize * 10 + ( *pcTempBuf - '0' );
pcTempBuf++;
}
return ( ZZOK );
}
/*--------------------------------------------------------------------------*/
/* RZ SAVE TO DISK */
/* Writes the received file data to the output file. */
/* */
/*--------------------------------------------------------------------------*/
static int zSaveToDisk( int *prxbytes )
{
int iWriteBytes;
if( ( iWriteBytes = write( giFdRevFile, pcFileBuff, giRxCount ) ) == ERROR )
{
printf( "[zSaveToDisk]:Write File error!\n" );
return( ZZERROR );
}
*prxbytes += giRxCount;
return ( ZZOK );
}
/*--------------------------------------------------------------------------*/
/* RZ RECEIVE FILE */
/* Receive one file; assumes file name frame is preloaded in gpcRecBuf */
/*--------------------------------------------------------------------------*/
static int zReceiveFile( char *pcFileBuf ) /*先發(fā)ZRPOS接著開始收ZDATA、data subpacket*/
{
int iRecChar, iRetryTimes;
iRetryTimes = 10; /* retry times */
giRecBytes = 0L;
while( 1 )
{
zPutLongIntoHeader( giRecBytes );
zSendHexHeader( ZRPOS, gpcTxhdr );
NxtHdr:
switch ( iRecChar = zGetHeader( gpcRxhdr ) )
{
case ZDATA: /* Data Packet */
if ( giRxpos != giRecBytes )
{
if ( --iRetryTimes < 0 )
{
return ( ZZERROR );
}
continue;
}
MoreData:
switch ( iRecChar = zReceiveData( gpcRecBuf + giRecBytes, WAZOOMAX ) )
{
case ZCAN:
case ZZRCDO:
return ( ZZERROR ); /*CAN or CARRIER*/
case ( ZZERROR ): /* CRC error*/
if ( --iRetryTimes<0 )
{
return ( ZZERROR );
}
continue;
case ZZTIMEOUT: /*Timeout*/
if ( --iRetryTimes < 0 )
{
return ( ZZERROR );
}
continue;
case GOTCRCW: /* End of frame*/
iRetryTimes = 10;
if ( zSaveToDisk( &giRecBytes ) == ZZERROR )
{
return ( ZZERROR );
}
zPutLongIntoHeader( giRecBytes );
zSendHexHeader( ZACK, gpcTxhdr );
goto NxtHdr;
case GOTCRCQ: /* Zack expected*/
iRetryTimes = 10;
if ( zSaveToDisk( &giRecBytes ) == ZZERROR )
{
return ( ZZERROR );
}
zPutLongIntoHeader( giRecBytes );
zSendHexHeader( ZACK, gpcTxhdr );
goto MoreData;
case GOTCRCG: /* Non-stop*/
iRetryTimes = 10;
if ( zSaveToDisk( &giRecBytes ) == ZZERROR )
{
return ( ZZERROR );
}
goto MoreData;
case GOTCRCE: /* Header to follow*/
iRetryTimes = 10;
if ( zSaveToDisk( &giRecBytes ) == ZZERROR )
{
return ( ZZERROR );
}
goto NxtHdr;
} /* end of switch */
case ZNAK:
case ZZTIMEOUT: /* Packed was probably garbled */
if ( --iRetryTimes < 0)
{
return ( ZZERROR );
}
continue;
case ZFILE: /* Sender didn't see our ZRPOS yet */
zReceiveData( gpcRecBuf, WAZOOMAX );
continue;
case ZEOF: /* End of the file*/
if ( giRxpos != giRecBytes )
{
continue;
}
return iRecChar;
case ZZERROR: /* Too much garbage in header search error */
if ( --iRetryTimes < 0 )
{
return ( ZZERROR );
}
continue;
default:
return ( ZZERROR );
} /* end of switch */
} /* end of while */
}
/*--------------------------------------------------------------------------*/
/* RZFILES */
/* Receive a batch of files using ZMODEM protocol */
/*--------------------------------------------------------------------------*/
static int zReceiveBatch( char *pcFileBuf ) /*接收多個文件*/
{
int iGetType;
while( 1 )
{
switch ( iGetType = zReceiveFile( pcFileBuf ) )
{
case ZEOF:
case ZSKIP:
switch ( zInitReceiver() )
{
case ZCOMPL:
return ZZOK;
case ZFILE:
break;
default:
return ZZERROR;
}
break;
default:
return ( iGetType );
}
}
}
/*--------------------------------------------------------------------------*/
/* GET ZMODEM */
/* Receive a batch of files. */
/* returns TRUE (1) for good xfer, FALSE (0) for bad */
/* can be called from f_upload or to get mail from a WaZOO Opus */
/*--------------------------------------------------------------------------*/
static int zGetZmodem( void )
{
int iRecType;
gcTryZhdrType = ZRINIT;
zClearByte( );
iRecType = zInitReceiver( );
if ( ( iRecType == ZCOMPL ) || ( ( iRecType == ZFILE ) &&
( ( zReceiveBatch( gpcRecBuf ) ) == ZZOK ) ) )
{
SendHEX( 1, giRecBytes>>24 );
SendHEX( 1, giRecBytes>>16 );
SendHEX( 1, giRecBytes>>8 );
SendHEX( 1, giRecBytes );
/* SendBuffer( 1," bytes received!!!\x0d\x0a" ); */
zClearByte( );
return ( giRecBytes );
}
else
{
/* SendBuffer( 1,"Download error!!!\x0d\x0a" ); */
zClearByte( );
return ( 0L );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -