9. Use of M601 FTP

1 Introduction to FTP API

For details, please refer to the ftp_client.c file, which is the function implementation. The available functions are as follows:
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 ssltype; uint8_t ssltype; uint8_t ssltype; uint8_t ssltype ; 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_tr_ftp_client_client in ftp_client_t, int_client, add *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 Introduction to FTP routines

This chapter mainly introduces how to use example_ftpclient.c in SDK.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac

In the routine, in ZYF_FtpClientTest(), initialize the IP, port, file name, user name and password of the file to be downloaded through ftp_file_init() in ZYF_FtpClientTest(). Then use ftp_client_init() to initialize the parameters needed for the client connection, such as contextid, transmode, etc. Finally, wait for the FTP connection through ZYF_FtpClientThread(), and then start the process of downloading files.

#include <stdint.h>
#include <string.h>
#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");
}


 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107226194