pubdir.c

来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 2,219 行 · 第 1/5 页

C
2,219
字号
    }
    /* iRealSize should equal iHostFileSize */
    if (iRealSize != iHostFileSize)
    {
        printf ("debug error\n");
        my_free (pcBuf);
        return;
    }
    /*ifreset();  reset network to avoid interrupts */
    /*(void)muxDevStopAll();*/
#undef VER_COMPRESSED 
#ifdef VER_COMPRESSED
        /* inflate (source, dest, size); */
        if (ERROR == inflate(pcBuf, (void*)VERSION_DEST_ADDR, iHostFileSize))
        {
            printf ("inflate fail.\n");
            my_free(pcBuf);
            return;
        }
#else
    /* memmove(dest, source, size) */
    printf ("memmove(%d, %d, %d)\n", VERSION_DEST_ADDR, (int)(pcBuf), iHostFileSize);
    memmove((void*)VERSION_DEST_ADDR, pcBuf, iHostFileSize);
#endif

    my_free(pcBuf);
    
#ifdef VER_COMPRESSED
    printf("inflate success.\n");
#endif
    printf("run the version!\n");
    taskDelay(60); /* let the previous messages shown */
    intLock();
    pfunc = (FUNCPTR)VERSION_DEST_ADDR;
    (*pfunc)();
	  return; /* for lint only */
}
void getFtpUser(char *pcUserName)
{
    if( strlen(s_structCfgPara.FtpUserName) > 15 )
        s_structCfgPara.FtpUserName[15] = '\0';
    strcpy (pcUserName, s_structCfgPara.FtpUserName);
}
void getFtpPasswd(char *pcPasswd)
{
    if( strlen(s_structCfgPara.FtpPasswd) > 15 )
        s_structCfgPara.FtpPasswd[15] = '\0';
    strcpy (pcPasswd, s_structCfgPara.FtpPasswd);
}
ULONG getFtpHostIp()
{
    return s_structCfgPara.FtpHostIp;
}

int getHostFileSize (
                char *v_pHostFile,
                ULONG v_ulHostIp,
                UCHAR *v_pUserName,
                UCHAR *v_pPassWord
                )
{
        return _getHostFileSize_pc ("", v_pHostFile, v_ulHostIp, v_pUserName, v_pPassWord);
}
/*
 * 函数描述:
 *   获取FTP服务器上一个文件的长度
 *   这个函数中不提供prompt向用户问信息,这个函数的过程对于用户来讲应该是不可见的
 * 返回值:
 *   成功则为文件长度,否则为ERROR
 *   错误值,细分出是因为连接失败(-2),还是该文件不存在(-1)
 *
 * getHostFileSize_pc与getHostFileSize_vx的区别在于是否FTP SERVER是否支持LIST FILENAME的命令
 */
int _getHostFileSize_pc (
                char *v_pHostDir,
                char *v_pHostFile,
                ULONG v_ulHostIp,
                UCHAR *v_pUserName,
                UCHAR *v_pPassWord
                )
{
#ifdef FILE_INFO_BUF_SIZE
#undef FILE_INFO_BUF_SIZE
#endif
#define FILE_INFO_BUF_SIZE  200

    char        acHostIp[16];
    int         dataSock;
    int         cntrlSock;
    int         iChars;
    int         fail_no;    /* 失败原因 */
	char        *p, *pBlockEnd;
	int         iFileSize;

    /* convert hostip to string */
    _ulIp2strIp((char *)acHostIp, v_ulHostIp);

    if (s_debug)
    printf ("FTPXFER(%s, %s, %s, %s, ...)\n", v_pHostDir, acHostIp, v_pUserName, v_pPassWord);

    if (ftpXfer (acHostIp, v_pUserName, v_pPassWord, "", "LIST %s", v_pHostDir, v_pHostFile,
                 &cntrlSock, &dataSock) != OK)
    {
        printf ("FTPXFER() failed\n");
#ifndef __VER
        printf ("can not connect to ftp server!\n");
#endif
        fail_no = -2;
        return fail_no;
    }

    p = (char *)acBlock;
    pBlockEnd = p + sizeof(acBlock);
    while ((iChars = select_recv (dataSock, p, (int)pBlockEnd - (int)p, 0)) > 0)
        p += iChars;
    *p = '\0';
    /* acBlock中存在的内容为:文件信息,最后有一个换行符0x0d, 0xoa, 0x00 */

    /* Close the sockets opened by FTPXFER */
    close (cntrlSock);
    close (dataSock);

    if (s_debug)
    printf ("LIST = %s\n", acBlock);

    iFileSize = parseFileSize(acBlock, strlen(acBlock));

    if (s_debug)
    printf ("host file size = %d\n", iFileSize);

    if (ERROR == iFileSize)
    {
        fail_no = -1;
        return fail_no;
    }
    else
    return iFileSize;
}
/*
 * 对内存申请函数进行了封装,为了统计申请和释放的匹配信息,用于调试
 * 注意,只有申请成功,才将统计量加1
 */
void * my_malloc (unsigned int v_nBytes)
{
    void *p;

    p = malloc (v_nBytes);
    /* p = memalign (4, v_nBytes); */
    if (NULL != p) g_mallocTimes++;
    return p;
}
/*
 * 对内存释放函数进行了封装,为了统计申请和释放的匹配信息,用于调试
 */
void my_free (void *p)
{
    g_freeTimes++;
    free (p);
}
/*manual
 * 描述:
 *   从FTP服务器上下载一个指定的文件,存放到某个指定的内存位置(pblock)中。
 *   屏幕上输出显示下载过程的打点信息。
 * 参数说明:
 *   如果withparam = 1,意指v_pHostFile, v_ulHostIp, username, password参数的值已由调用者提供,
 *   否则(v_iWithParam = 0),意指v_pHostFile, v_ulHostIp, username, password会通过交互答问方式获取。
 *   v_piDataLen, 实际下载的数据长度
 *   viblocklen指明了vpblock内存块的大小,通常调用者事先会算好,使得viblocklen等于要下载的文件的长度
 * 返回值:
 *   ERROR or OK
 */
int downloadData(     int v_iWithParam,
                    UCHAR *v_pBlock,
                      int v_iBlockLen,
                      int *v_piDataLen,
                    UCHAR *v_pHostFile,
                    ULONG v_ulHostIp,
                    UCHAR *v_pUserName,
                    UCHAR *v_pPassWord)
{
    /* 连接FTP所需的 */
    char acHostFile[80]; /* 要下载的程序名 */
    ULONG ulHostIp = DEFAULT_FTP_HOST_IP;
    char acHostIp[80];
    char acUserName[80]; /* FTP用户名 */
    char acPassWord[80];

    int ctrlSock;
    int dataSock;

    int iTotalLength = 0;
    int length; /* 每次调用recv收到的字节数 */
    int iRemains = 0; /* 已经收到,但未打点表示的数据量 */

    char buffer[512]; /* 存放每次调用recv得到的数据 */
    char *pBuf; /* 用来遍历buffer */
		IMPORT int          netTaskPriority;
    int                 oldTaskPriority;
    int i;


    /* check params */
    if ((v_iBlockLen == 0) || (v_pBlock == NULL))
        return FTP_PARAMETER_ERR;

    if (v_iWithParam)
    {
        strcpy(acHostFile, v_pHostFile);
        ulHostIp = v_ulHostIp;
        strcpy(acUserName, v_pUserName);
        strcpy(acPassWord, v_pPassWord);
    }
    else
    {
    }
    Print("\n\rStart downloading program...\n");
    _ulIp2strIp (acHostIp, ulHostIp);

BEGIN_DBG (SWITCH_UP_DOWN)
    printf ("ulHostIp = %x\n", (int)ulHostIp);
    printf ("FTPXFER(%s, %s, %s, ...)\n", acHostIp, acUserName, acPassWord);
END_DBG
		taskPriorityGet (0, &oldTaskPriority);
    taskPrioritySet (0, netTaskPriority + 1);
    if (ftpXfer ( acHostIp, acUserName, acPassWord, "",
        "RETR %s", "", acHostFile, &ctrlSock, &dataSock) == ERROR)
    {
        printf ("FTPXFER() failed\n");
        Print("Make sure the host ip and account are correct\n");
        taskPrioritySet (0, oldTaskPriority);
        return FTP_CONNECT_ERR;
    }

    while ((length = select_recv (dataSock, buffer, 512, 0)) >0)
    {
        iTotalLength += length;
        iRemains += length;

        if (iTotalLength > v_iBlockLen)
            break;

        pBuf = (char*)buffer;
        for (i = 0; i < length; i++)
            *v_pBlock++ = *pBuf++;

        if (iRemains >= 0x4000) /* for every 16k bytes, print a dot */
        {
            Print(".");
            iRemains -= 0x4000;
        }
    }

    if (iTotalLength > v_iBlockLen){
        printf ("\nThe version is too long.\n");
    }else if (length == 0){
        Print ("Done!\n");
    }else if (length < 0){
        printf ("error, recv() return value < 0\n");
        close(dataSock);
        close (ctrlSock);
        taskPrioritySet (0, oldTaskPriority);
        return FTP_RECV_ERR;
    }
    
    /* close socket */

    close(dataSock);

    if (ftpReplyGet (ctrlSock, TRUE) != FTP_COMPLETE)
    {
        printf ("\nCtrlSock complete error\n");
        taskPrioritySet (0, oldTaskPriority);
        return FTP_COMPLETE_ERR;
    }

    if (ftpCommand (ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE)
    {
        printf ("\nFtp quit error\n");
        taskPrioritySet (0, oldTaskPriority);
        return FTP_QUIT_ERR;
    }
    close (ctrlSock);

    if (iTotalLength > v_iBlockLen || length < 0)
    {
    		taskPrioritySet (0, oldTaskPriority);
        return FTP_GENERAL_ERR;
    }

    *v_piDataLen = iTotalLength;
    taskPrioritySet (0, oldTaskPriority);
    return OK;
}
/*
 * in: v_ipaddr
 * out: v_ipstr
 * e.g. v_ipaddr = 0x0a3e19b0 -> v_ipstr = "10.62.25.176"
 *               10 62 25 176
 */
void _ulIp2strIp(char *v_ipstr, unsigned long v_ipaddr)
{
    char *pStr;

    pStr = (char*)&v_ipaddr;
    sprintf(v_ipstr, "%d.%d.%d.%d",
                    (int)(*pStr), (int)(*(pStr+1)), (int)(*(pStr+2)), (int)(*(pStr+3)));
    return;
}
int select_recv (int sock, char *pBuf, unsigned long BufSize, unsigned long options)
{
    options = options;
    return (ftpSocketRead (sock,pBuf,BufSize));
}
static int  ftpSocketRead(int sock, char *pBuf, unsigned long BufSize)
{
    int     BufLen;
    long    numReaders;
    fd_set  fdSocks;
    struct timeval  tTimeout;

    FD_ZERO(&fdSocks);
    FD_SET(sock, &fdSocks);

    tTimeout.tv_sec  = (1 == BufSize) ? 30: 60; 
    tTimeout.tv_usec = 0;
    numReaders = select(sock+1, &fdSocks, 0, 0, &tTimeout);
    /* if timeout, report it */
    if (0 == numReaders)
    {
        printf ("socket timeout!");
        return -1;
    }
    /* report any error */
    else if (0 > numReaders)
    {
        printf ("socket error!");
        return -1;
    }

    /* There's data in the socket, so nab all you can... */

    BufLen = recv(sock, pBuf, BufSize, 0);

    if ( BufLen < 0 )   
        return -1;

    return BufLen;
}
/*
 * 返回值:
 *   ERROR or 文件的长度
 */
int parseFileSize(char *v_ptr, int v_iLen)
{
    int numOfSSToCross = 4; /* number of space sequence to cross */
    char *pEnd = NULL;
    char *pLenHead = NULL; /* 长度信息的头位置 */
    int iFileLen = INT_INIT;

    pEnd = v_ptr + v_iLen;
    while (numOfSSToCross > 0)
    {
        while (v_ptr != pEnd && *v_ptr != ' ') v_ptr++;
        if (v_ptr == pEnd) return ERROR;
        while (v_ptr != pEnd && *v_ptr == ' ') v_ptr++;
        if (v_ptr == pEnd) return ERROR;
        numOfSSToCross--;
    }

    pLenHead = v_ptr;

    while (v_ptr != pEnd && *v_ptr != ' ') v_ptr++;
    if (v_ptr == pEnd) return ERROR;

    *v_ptr = '\0';

    if (0 == strncmp(pLenHead, "0", 1)) return 0; /* file size is 0, we think it valid */

    iFileLen = atoi(pLenHead);
    if (0 == iFileLen)
        return ERROR;
    else
        return iFileLen;
}
#ifdef _MKBOOT
static
void cmd_bootLoad()
{
    char acHostIp[16];
    char acHostFile[16]; /* ?? */
    char acUser[FTP_USER_LEN];
    char acPasswd[FTP_PASSWD_LEN];
    FUNCPTR entry;

    /* 从FLASH中取一些参数 */
    _ulIp2strIp (acHostIp, getFtpHostIp());

    strcpy (acHostFile, "vxworks");
    if (tokens[1])
    {
        strcpy (acHostFile, tokens[1]);
    }

    getFtpUser(acUser);
    getFtpPasswd(acPasswd);
BEGIN_DBG (SWITCH_CMD_DEBUG)
    Print ("netLoad(acHostIp = %s, acHostFile = %s, acUser = %s, acPasswd = %s, &entry)\n", acHostIp, acHostFile, acUser, acPasswd);
END_DBG
    if (OK != netLoad(acHostIp, acHostFile, acUser, acPasswd, &entry))
    {
        FAIL;
        return;
    }

    go(entry);
}
#endif
/* -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> */
/* cmd_* */

/*
 * run the version, if no version available download it first
 */
static
void cmd_run()
{
     run(); 
}
static
void cmd_config(void)
{
    char buf[100];
    UCHAR sum, tmp;
    int bFirstPart;
    char *p;
    int idx;
    unsigned long ulIp, ulOriginIp, ulSubnetMask, ulOriginSubnetMask;
    ULONG ulFtpHostIp, ulOriginFtpHostIp;
    ULONG ulUdpHostIp, ulOriginUdpHostIp;
    char acUserName[16], acOriginUserName[16];
    char acPasswd[16], acOriginPasswd[16];
    char acMac[6], acOriginMac[6];
    int i;
    int bChanged;

⌨️ 快捷键说明

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