⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zmodem.c

📁 vxworks不支持ZMODEM协议
💻 C
📖 第 1 页 / 共 4 页
字号:
/*    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 )   /*先发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 + -