9. M601 FTP的使用

1 FTP API介绍

具体请参考 ftp_client.c 文件,里面是函数实现,可供使用的函数如下:
struct ftp_client {
char ipaddr[FTP_CLIENT_IPADDR_LEN];
uint32_t port;
uint8_t contextid;
uint8_t transmode;
uint16_t rsptimeout;
uint8_t ssltype;
uint8_t sslctxid;
ftp_client_cb upload;
ftp_client_cb download;
ftp_network_t ctrlconn;
ftp_network_t dataconn;
};
extern int ftp_client_init(ftp_client_t *client);
extern int ftp_client_reset(ftp_client_t *client);
extern int ftp_client_ctrl_connect(ftp_client_t *client, char* addr, uint32_t port);
extern int ftp_client_login(ftp_client_t *client, char *username, char *password);
extern int ftp_client_size(ftp_client_t *client, char *filename, uint32_t *size);
extern int ftp_client_pasv(ftp_client_t *client);
extern int ftp_client_list(ftp_client_t *client, char *filepath, char *list);
extern int ftp_client_rest(ftp_client_t *client, uint16_t offset);
extern int ftp_client_type(ftp_client_t *client, uint8_t type);
extern int ftp_client_get(ftp_client_t *client, char *filepath, uint32_t size, uint8_t *buffer);

 

2 FTP例程介绍

本章节主要介绍如何在 SDK 中使用 example_ftpclient.c。
编译方法:.\examples\build\对应的.bat 文件双击执行或打开就可以编译。
生成文件:.\out\对应目录\hex\M601_example_**.pac

例程中,在 ZYF_FtpClientTest()中通过 ftp_file_init()初始化需要下载文件的 IP、端口、文件名、用户名和密码等信息。再通过 ftp_client_init()初始化客户端连接需要用到的参数如contextid、transmode 等。最后通过 ZYF_FtpClientThread()等待 FTP 连接,再开始下载文件的流程。

#include 
#include 
#include "ftp_client.h"

#include "zyf_trace.h"
#include "zyf_thread.h"
#include "zyf_fota.h"

#include "zyf_app.h"
#include "example_ftpclient.h"
#include "app_network.h"

static ZYF_FtpCtx_t s_tFtpCtx;

static Uart_Param_t g_uart1param;


void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    /*
    UART_PORT1 = 0,
    UART_PORT2 = 1,
    UART_PORT3 = 2,
    */
    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void AppUartInit(void)
{
    int32_t ret;
    g_uart1param.port = DEBUG_PORT;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("AppUartInit");
    return;
}


void ZYF_FtpPdpActCallback(uint8_t status)
{
    ZYF_FtpCtx_t *ptCtx = &s_tFtpCtx;
    ZYF_AppMsg_t tMsg;

    if (ptCtx->ptMsg != NULL) {
        tMsg.wMsgId = status;
        ZYF_MsgQPut(ptCtx->ptMsg, (void *)&tMsg);
    }
}

void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode)
{
    ZYF_LOG("PDP: Active");

    ZYF_FtpPdpActCallback(APP_MSG_PDP_ACTIVE);
}

void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode)
{
    ZYF_LOG("PDP: Deactive");
}


#if defined(FTP_CLIENT_APP_DOWNLOAD) || defined(FTP_CLIENT_LIB_DOWNLOAD)
static void ZYF_ImageUpgradeCb(void *param)
{
    int errcode = *(int *)param;
    ZYF_LOG("%d", errcode);
}
#endif

static void ZYF_FtpClientThread(void *pvParam)
{
    ZYF_FtpCtx_t *ptCtx = (ZYF_FtpCtx_t *)pvParam;
	ZYF_AppMsg_t tMsg;
    int iRet = -1;

    ZYF_LOG("thread enter!");

    while (1) {
        iRet = ZYF_MsgQGet(ptCtx->ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(10);
        }

        switch (tMsg.wMsgId) {
            case APP_MSG_PDP_ACTIVE:
                ZYF_LOG("PDP active!");
                #if defined(FTP_CLIENT_APP_DOWNLOAD)
                ZYF_AppDownloadStart(ZYF_ImageUpgradeCb);
                if (!ftp_file_download(&ptCtx->tFileXfr, "/cgw/app_file.img", 0, 1024 * 70)) {
                    ZYF_AppDownloadFinish();
                }
                #elif defined(FTP_CLIENT_LIB_DOWNLOAD)
                ZYF_LibDownloadStart(ZYF_ImageUpgradeCb);
                if (!ftp_file_download(&ptCtx->tFileXfr, "/cgw/fota.pack", 0, 1024 * 70)) {
                    ZYF_LibDownloadFinish();
                }
                #else
                ftp_client_ctrl_connect(&ptCtx->tClient, ptCtx->cIpAddr, ptCtx->wCtrlPort);
                ftp_client_login(&ptCtx->tClient, ptCtx->cUserName, ptCtx->cPassWord);

                ftp_client_pasv(&ptCtx->tClient);
                
                char buf[512] = {0};
                ftp_client_list(&ptCtx->tClient, "/cgw", buf);
                
                uint32_t size;
                ftp_client_size(&ptCtx->tClient, "/cgw/test.txt", &size);
                #endif
                break;
                
            case APP_MSG_PDP_DEACTIVE:
                ZYF_LOG("PDP Deactive!");
                break;
        }
    }
}

void ZYF_FtpClientTest(void)
{
    //a test for ftp file download
    #if defined(FTP_CLIENT_APP_DOWNLOAD)
    ftp_xfile_t *ptFileXfr = &s_tFtpCtx.tFileXfr;
    ftp_file_init(ptFileXfr, 
                  NULL, 
                  ZYF_AppDownloadImage, 
                  "IOT_TEST", "123456", 
                  "data.bsjiot.com", 21);
    #elif defined(FTP_CLIENT_LIB_DOWNLOAD) 
    ftp_xfile_t *ptFileXfr = &s_tFtpCtx.tFileXfr;
    ftp_file_init(ptFileXfr, 
                  NULL, 
                  ZYF_LibDownloadImage, 
                  "IOT_TEST", "123456", 
                  "data.bsjiot.com", 21);

    //a test for ftp client api
    #else  
    ftp_client_t *ptClient = &s_tFtpCtx.tClient;
    
    ptClient->contextid = 0;  //fixme
    ptClient->transmode = FTP_CLIENT_TRANSMODE_PASSIVE;
    ptClient->ssltype = 0;
    ptClient->sslctxid = 0;
    ptClient->rsptimeout = 90;

     (ptClient); 
    
    
    strcpy(s_tFtpCtx.cUserName, "IOT_TEST");
    strcpy(s_tFtpCtx.cPassWord, "123456");

    strcpy(s_tFtpCtx.cIpAddr, "data.bsjiot.com");
    s_tFtpCtx.wCtrlPort = 21;
    #endif
    s_tFtpCtx.ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
	ZYF_ThreadCreate("ZYF_FtpClientThread", ZYF_FtpClientThread, (void *)&s_tFtpCtx, ZYF_PRIORITY_NORMAL, 1024 * 8);
}


void FtpClient_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

    ZYF_NetworkInit();
    ZYF_FtpClientTest();
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}

static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("FtpClient_Example", FtpClient_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}


 

你可能感兴趣的:(M601)