This is a sample program using the FTP library.
Sample: main.c
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include "FTP.h"
#include "logapi.h"

// For local log
#define PRINTF(...)            printf(__VA_ARGS__)
//For LogReceiver
//#define PRINTF(...)            Logmem_printf_App(1, __VA_ARGS__)

static const char ftpHost[] = "192.168.11.3";
static const char ftpUsername[] = "ftpuser";
static const char ftpPassword[] = "ftpftp";
static const char ftpPath[] = "OPH5000";

int ConnectAp(void)
{
    int wlanPower;
    unsigned long tickcount;
    int status;
    if (SysGetWLANPower(&wlanPower) == TRUE){
        if (wlanPower != SYS_WLAN_POWER_AUTO){
            if (SysSetWLANPower(SYS_WLAN_POWER_AUTO) != TRUE){
                return FALSE;
            }
        }
        PRINTF("AutoPowerDown(APD_DISABLE, 0)\n");
        AutoPowerDown(APD_DISABLE, 0);
        SysWLANInit(NULL);
        tickcount = GetTickCount();
        while (1){
            SysGetWLANConnectStatus(&status);
            if (GetTickCount() - tickcount > 30 * TIME_1SEC){
                PRINTF("ConnectAP() : timeout.\n");
                return FALSE;
            }
            if (status == SYS_WLAN_STATUS_CONNECTED){
                PRINTF("ConnectAP() : return SYS_WLAN_STATUS_CONNECTED\n");
                return TRUE;
            }
            if (kbhit() && (getchar() == CLR_KEY)){
                PRINTF("ConnectAP() : CLR_KEY pressed.\n");
                return FALSE;
            }
            if (status == SYS_WLAN_STATUS_UNAVAILABLE){
                PRINTF("ConnectAP() : return SYS_WLAN_STATUS_UNAVAILABLE\n");
                return FALSE;
            }
            Idle();
        }
    }
    return FALSE;
}
int IsApConnected(void)
{
    int wlanPower, status;
    if (SysGetWLANPower(&wlanPower) == TRUE){
        if (wlanPower == SYS_WLAN_POWER_AUTO){
            SysGetWLANConnectStatus(&status);
            if (status==SYS_WLAN_STATUS_CONNECTED){
                return TRUE;
            }
        }
    }
    return FALSE;
}
void ResponseCb(unsigned char command, char *msg)
{
    PRINTF("[c%d]%s", command, msg);
}
void main(void)
{
    int i, ch;
    int wlanPower;
    char ret;
    FILE *fp = NULL;
    char remoteName[64];
    char tempName[64];
    const char uploadFile[] = "TEST_UP.TXT";
    const char downloadFile[] = "TEST_DL.TXT";

    Cursor (AUTOWRAP);

    if (fsize((char *)uploadFile) <= 0){
        PRINTF("Creating upload data...");
        fp = fopen(uploadFile, "w");
        if (fp != NULL){
            for( i=0; i<10000; i++)
                fwrite("THIS IS TEST ", 1, 13, fp);
            fclose(fp);
            PRINTF("done.\n");
        }
    }
    printf("SCAN to FTP test\n");

    for ( ;; ){
        if (kbhit()){
            ch = getchar();
            if (ch == SCAN_KEY){
                SysGetWLANPower(&wlanPower);
                if (wlanPower != SYS_WLAN_POWER_AUTO){
                    SysSetWLANPower(SYS_WLAN_POWER_AUTO);
                }
                if (IsApConnected() == FALSE){
                    PRINTF("AP connect start.\n");
                    if (ConnectAp() == FALSE){
                        PRINTF("WLAN connect fail\n");
                        SysSetWLANPower(SYS_WLAN_POWER_OFF);
                        continue;
                    }else{
                        PRINTF("AP connected.\n");
                    }
                }
                PRINTF("Start.\n");
                /* Connect to FTP server */
                PRINTF("Con:%s\n", ftpHost);
                ret = FTP_Connect((char *)ftpHost, 21, FTP_PASV, ResponseCb);
                if (ret != M2M_SUCCESS){
                    PRINTF(" ERROR:%d\n", ret);
                }else{
                    PRINTF(" OK\n");
                    /* USERNAME command*/
                    PRINTF("NAME:%s\n", ftpUsername);
                    ret = FTP_Command(FTP_CMD_USERNAME, (char *)ftpUsername, NULL);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                if (ret == M2M_SUCCESS){
                    /* PASSWORD command*/
                    PRINTF("PASS:%s\n", ftpPassword);
                    ret = FTP_Command(FTP_CMD_PASSWORD, (char *)ftpPassword, NULL);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                if (ret == M2M_SUCCESS){
                    if (strlen(ftpPath) > 0){
                        /* CHPATH command*/
                        PRINTF("Chdir:%s\n", ftpPath);
                        ret = FTP_Command(FTP_CMD_CHPATH, (char *)ftpPath, NULL);
                        if (ret != M2M_SUCCESS){
                            PRINTF(" ERROR:%d\n", ret);
                        }else{
                            PRINTF(" OK\n");
                        }
                    }
                }
                if (ret == M2M_SUCCESS){
                    /* TYPESET command */
                    PRINTF("Type:A\n");
                    ret = FTP_Command(FTP_CMD_TYPESET, "A", NULL);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                if (ret == M2M_SUCCESS){
                    /* Create file names */
                    memset(remoteName, 0, sizeof(remoteName));    // remoteName: Target file name
                    memset(tempName, 0, sizeof(tempName));        // tempName:   Temporary file name
                    ret = FTP_CreateRemoteFilename(FTP_NAME_TIME_NOID, uploadFile, remoteName, tempName);
                }
                if (ret == M2M_SUCCESS){
                    /* Upload a file with the temporary file name */
                    PRINTF("UP:%s\n", tempName);
                    PRINTF("Local:%s\n", uploadFile);
                    ret = FTP_Command(FTP_CMD_PUT, (char *)uploadFile, tempName);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                if (ret == M2M_SUCCESS){
                    /* Rename the file to the target file name */
                    PRINTF("RENAME:%s\n->%s\n", tempName, remoteName);
                    ret = FTP_Command(FTP_CMD_RENAME, tempName, remoteName);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                if (ret == M2M_SUCCESS){
                    /* Download the target file */
                    PRINTF("DL:%s\n", remoteName);
                    PRINTF("Local:%s\n", downloadFile);
                    ret = FTP_Command(FTP_CMD_GET, (char *)downloadFile, remoteName);
                    if (ret != M2M_SUCCESS){
                        PRINTF(" ERROR:%d\n", ret);
                    }else{
                        PRINTF(" OK\n");
                    }
                }
                /* QUIT command */
                PRINTF("Quit\n");
                FTP_Command(FTP_CMD_QUIT, NULL, NULL);
                FTP_Finish();
                SysSetWLANPower(SYS_WLAN_POWER_OFF);
            }
            ResetKey();
            printf("SCAN to FTP test\n");
        }
        Idle();
    }
}


Last updated: 2021/06/03