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

📄 zmodem.c

📁 vxworks不支持ZMODEM协议
💻 C
📖 第 1 页 / 共 4 页
字号:

/*****************************************************************
Filename: zmodem.c
Description: 
****************************************************************/

#include <vxWorks.h>
#include <ioLib.h>
#include <tyLib.h>
#include <stdio.h>
#include <tickLib.h>
#include "zmodem.h"


/*--------------------------------------------------------------------------*/
/* Contro Text define                                                       */
/*--------------------------------------------------------------------------*/
static const int ZPAD       =     '*';                /* 052 Pad character begins frames   */
static const int ZDLE       =     030;                /* ^X Zmodem escape- `ala BISYNC DLE */
static const int ZDLEE      =     ZDLE^0100  ;        /* Escaped ZDLE as transmitted       */
static const int ZBIN       =     'A';                /* Binary frame indicator            */
static const int ZHEX       =     'B';                /* HEX frame indicator               */
static const int ZBIN32     =     'C';                /* Binary frame with 32 bit FCS      */

/*--------------------------------------------------------------------------*/
/* Frame types (see array "frametypes" in zmodem.c)                         */
/*--------------------------------------------------------------------------*/
static const int ZRQINIT    =   0;            /* Request receive init              */
static const int ZRINIT     =   1;            /* Receive init                      */
static const int ZSINIT     =   2;            /* Send init sequence (optional)     */
static const int ZACK       =   3;            /* ACK to above                      */
static const int ZFILE      =   4;            /* File name from sender             */
static const int ZSKIP      =   5;            /* To sender: skip this file         */
static const int ZNAK       =   6;            /* Last packet was garbled           */
static const int ZABORT     =   7;            /* Abort batch transfers             */
static const int ZFIN       =   8;            /* Finish session                    */
static const int ZRPOS      =   9;            /* Resume transmit at this position  */
static const int ZDATA      =   10;           /* Data packet(s) follow             */
static const int ZEOF       =   11;           /* End of file                       */
static const int ZFERR      =   12;           /* Fatal Read/Write error Detected   *//*not use*/
static const int ZCRC       =   13;           /* Request for file CRC and response */
static const int ZCHALLENGE =   14;           /* Receiver's Challenge              */
static const int ZCOMPL     =   15;           /* Request is complete               */
static const int ZCAN       =   16;           /* Other end canned with CAN*5       */
static const int ZFREECNT   =   17;           /* Request for free bytes on disk    */
static const int ZCOMMAND   =   18;           /* Command from sending program      */
static const int ZSTDERR    =   19;           /* Send following to stderr          */

/*--------------------------------------------------------------------------*/
/* ZDLE sequences    frameend type in subpacket                             */
/*--------------------------------------------------------------------------*/
static const int ZCRCE      =     'h';            /* CRC next/frame ends/hdr follows   */
static const int ZCRCG      =     'i';            /* CRC next/frame continues nonstop  */
static const int ZCRCQ      =     'j';            /* CRC next/frame continues/want ZACK*/
static const int ZCRCW      =     'k';            /* CRC next/ZACK expected/end of frame*/
static const int ZRUB0      =     'l';            /* Translate to rubout 0177          */
static const int ZRUB1      =     'm';            /* Translate to rubout 0377          */

/*--------------------------------------------------------------------------*/
/* zGetZDL return values (internal)                                        */
/* -1 is general error, -2 is timeout                                       */
/*--------------------------------------------------------------------------*/
static const int GOTOR      =     0400;            /* Octal alert! Octal alert!         */
static const int GOTCRCE    =     (ZCRCE|GOTOR);   /* ZDLE-ZCRCE received               */
static const int GOTCRCG    =     (ZCRCG|GOTOR);   /* ZDLE-ZCRCG received               */
static const int GOTCRCQ    =     (ZCRCQ|GOTOR);   /* ZDLE-ZCRCQ received               */
static const int GOTCRCW    =     (ZCRCW|GOTOR);   /* ZDLE-ZCRCW received               */
static const int GOTCAN     =     (GOTOR|030) ;    /* CAN*5 seen                        */

/*--------------------------------------------------------------------------*/
/* Byte positions within header array                                       */
/*--------------------------------------------------------------------------*/
static const int ZF0        =     3;         /* First flags byte                  */
static const int ZF1        =     2; 
static const int ZF2        =     1; 
static const int ZF3        =     0; 
static const int ZP0        =     0;         /* Low order 8 bits of position      */
static const int ZP1        =     1; 
static const int ZP2        =     2; 
static const int ZP3        =     3;         /* High order 8 bits of file pos     */

/*--------------------------------------------------------------------------*/
/* Bit Masks for ZRINIT flags byte ZF0                                      */
/*--------------------------------------------------------------------------*/
static const int CANFDX     =     01;        /* Can send and receive true FDX     */
static const int CANOVIO    =     02;        /* Can receive data during disk I/O  */
static const int CANBRK     =     04;        /* Can send a break signal           *//*  not use  */
static const int CANCRY     =     010;       /* Can decrypt                       *//*  not use  */
static const int CANLZW     =     020;       /* Can uncompress                    *//*  not use  */
static const int CANFC32    =     040;       /* Can use 32 bit Frame Check        *//*  not use  */


/*--------------------------------------------------------------------------*/
/* PARAMETERS FOR ZFILE FRAME...                                            */
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*/
/* Conversion options one of these in ZF0                                   */
/*--------------------------------------------------------------------------*/
static const int ZCBIN      =     1;         /* Binary transfer - no conversion   */
static const int ZCNL       =     2;         /* Convert NL to local EOLN          *//*  not use  */
static const int ZCRESUM    =     3;         /* Resume interrupted file transfer  *//*  not use  */

/*--------------------------------------------------------------------------*/
/* Management options, one of these in ZF1                                  */
/*--------------------------------------------------------------------------*/
static const int ZMNEW      =     1;         /* Transfer iff source newer/longer  *//*  not use  */
static const int ZMCRC      =     2;         /* Transfer if different CRC/length  *//*  not use  */
static const int ZMAPND     =     3;         /* Append contents to existing file  *//*  not use  */
static const int ZMCLOB     =     4;         /* Replace existing file             *//*  not use  */
static const int ZMSPARS    =     5;         /* Encoding for sparse file          *//*  not use  */
static const int ZMDIFF     =     6;         /* Transfer if dates/lengths differ  *//*  not use  */
static const int ZMPROT     =     7;         /* Protect destination file          *//*  not use  */
                                          
/*--------------------------------------------------------------------------*/
/* Transport options, one of these in ZF2                                   */
/*--------------------------------------------------------------------------*/
static const int ZTLZW      =     1;         /* Lempel-Ziv compression            *//*  not use  */
static const int ZTCRYPT    =     2;         /* Encryption                        *//*  not use  */
static const int ZTRLE      =     3;         /* Run Length encoding               *//*  not use  */

/*--------------------------------------------------------------------------*/
/* Parameters for ZCOMMAND frame ZF0 (otherwise 0)                          */
/*--------------------------------------------------------------------------*/
static const int ZCACK1     =     1 ;             /* Acknowledge, then do command      *//*  not use  */

/*--------------------------------------------------------------------------*/
/* Miscellaneous definitions                                                */
/*--------------------------------------------------------------------------*/
static const int ZZOK       =     0;              
static const int ZZERROR    =     -1;           
static const int ZZTIMEOUT  =     -2;           
static const int ZZRCDO     =     -3;           
static const int ZZFUBAR    =     -4;           
                                                  
static const int XON        =     'Q'&037;      
static const int XOFF       =     'S'&037;      
static const int CPMEOF     =     'Z'&037;          /* not used */                                     /*  not use  */
                                                  
static const int RXBINARY   =     false;            /* Force binary mode uploads?        *//*  not use  */
static const int RXASCII    =     false;            /* Force ASCII mode uploads?         *//*  not use  */
static const int LZCONV     =     0;                /* Default ZMODEM conversion mode    */
static const int LZMANAG    =     0;                /* Default ZMODEM file mode          */
static const int LZTRANS    =     0;                /* Default ZMODEM transport mode     */
static const int PATHLEN    =     128;              /* Max legal MS-DOS path size?       *//*  not use  */
static const int KSIZE      =     1024;             /* Max packet size (non-WaZOO)       *//*  not use  */
static const int WAZOOMAX   =     8192;             /* Max packet bit size (WaZOO) 128bytes */
static const int SECSPERDAY =     (24L*60L*60L);    /* Number of seconds in one day      *//*  not use  */




/*--------------------------------------------------------------------------*/
/* ASCII MNEMONICS                                                          */
/*--------------------------------------------------------------------------*/
static const int NUL = 0x00;
static const int SOH = 0x01;
static const int STX = 0x02;
static const int ETX = 0x03;
static const int EOT = 0x04;
static const int ENQ = 0x05;
static const int ACK = 0x06;
static const int BEL = 0x07;
static const int BS  = 0x08;
static const int HT  = 0x09;
static const int LF  = 0x0a;
static const int VT  = 0x0b;
static const int FF  = 0x0c;
static const int CR  = 0x0d;
static const int SO  = 0x0e;
static const int SI  = 0x0f;
static const int DLE = 0x10;
static const int DC1 = 0x11;
static const int DC2 = 0x12;
static const int DC3 = 0x13;
static const int DC4 = 0x14;
static const int NAK = 0x15;
static const int SYN = 0x16;
static const int ETB = 0x17;
static const int CAN = 0x18;
static const int EM  = 0x19;
static const int SUB = 0x1a;
static const int ESC = 0x1b;
static const int FS  = 0x1c;
static const int GS  = 0x1d;
static const int RS  = 0x1e;
static const int US  = 0x1f;

static const int ZATTNLEN   =  32 ;        /* Max length of attention string  */
static const int WAZOOMAX   =  8192  ;     /* Max packet size (WaZOO)         */


static char  gpcAttn[ZATTNLEN+1];           /* String rx sends to tx on err            */
static char  gcZconv;                       /* ZMODEM file conversion request          */
static char  gpcRecBuf[64];                 /* Pointer to receive buffer               */
static int   giRxType;                      /* Type of header received                 */
static char  gcRxframeType;                 /* ZBIN ZBIN32,ZHEX type of frame received */
static char  gcTryZhdrType;                 /* Hdr type to send for Last rx close      */
static int   giRxCount;                     /* Count of data bytes received            */
static char  gpcRxhdr[4];                   /* Received header                         */
static char  gpcTxhdr[4];                   /* Transmitted header                      */
static int   giRxpos;                       /* Received file position                  */
static int   giRecBytes;                    /* bytes of data received                  */
static int   giFilesize;
static char  gpcFilename[ 32 ];
static bool  gbTimeOut         = false;
static int   giTaskIdZmodemRev = ERROR;
static int   giComFd           = ERROR;
staitc int   giFdRevFile       = ERROR; 
int zmodemRcvInit( void )
{
    if( giComFd == ERROR )
    {
        if( ( giComFd = open( "/tyCo/4", O_RDWR , 0 ) ) == ERROR )
        {
            printf("[ comRcvFileInit ] : Creat Com failure.\n" );
            zmodemRcvDInit( );
            return( 0 );
        }

        if( ioctl( giComFd, FIOBAUDRATE, 115200 ) == ERROR )
        {
            printf(" [ comRcvFileInit ]: set the BAUD ERROR.\n");
            zmodemRcvDInit( );
            return( 0 );
        }
    }

    if( giTaskIdZmodemRev == ERROR )
    {
        giTaskIdZmodemRev = taskSpawn( "zmodemRcv", 120, 0x100, 0x20000,( FUNCPTR )zGetZmodem,
                                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
        if( giTaskIdZmodemRev == ERROR )
        {
            printf(" [ comRcvFileInit ]: Create task ERROR.\n");
            zmodemRcvDInit( );
            return( 0 );
        }
    }
    return( 1 );
}


int zmodemRcvDInit( void )
{
    if( giTaskIdZmodemRev != ERROR )
    {
        if( taskDelete( giTaskIdZmodemRev ) == ERROR )
        {
            printf("[ zmodemRcvDInit ]: Delete taskRev Error.\n");
        }
        giTaskIdZmodemRev = ERROR;
    }

    if( giComFd != ERROR )
    {
        if( close( giComFd ) == ERROR )
        {
            printf("[ zmodemRcvDInit ]: close Com Error.\n");
        }
        giComFd = ERROR;
    }

    if( fdList != ERROR )
    {
        close( fdList );
    }

    return( 1 );
}


static int zSendChar( unsigned char cData )
{
	if( giComFd != ERROR )
	{
	    if( write( giComFd, &cData, 1 ) == 1 )
	    {
	        return( 1 );
	    }
	}
    else
    {
    	return ( 0 );
    }
}


static int zTestOneByte( void )
{
	return( ioctl( giComFd, FIONREAD, 0 ) );
}


static void zClearByte( void )
{
	ioctl( giComFd, FIOFLUSH, 0 );
}


static int zGetOneByte( void )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -