/*
 *  Copyright (c) 1996-2007 Digi International Inc., All Rights Reserved
 *
 *  This software contains proprietary and confidential information of Digi
 *  International Inc.  By accepting transfer of this copy, Recipient agrees
 *  to retain this software in confidence, to prevent disclosure to others,
 *  and to make no use of this software other than that for which it was
 *  delivered.  This is an unpublished copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *
 *  Restricted Rights Legend
 *
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 * Description.
 * =======================================================================
 *  This is the RAM based FTP Server
 *
 *
 *
 *  Edit Date/Ver   Edit Description
 *  =============   =======================================================
 *  04/16/02  DXL   Inform user to check flash if NAFlashSectorSizes returns error
 *  05/01/02  JW    Removed argument from NAFlashSectors()
 *  05/02/02  JW    Changed sectorSizes to (unsigned long*) from (unsigned int*)
 *  05/22/02  JW    Increased buffer size to 128K for optimal 16 or 32 bit mode
 *                  performance
 *  06/17/02  JW    Added error handling for images larger than flash memory
 *  06/18/02  JW    Use #define constant for buffer size
 *  07/01/02  JW    Added more error handling for images larger than flash memory
 *  07/03/02  JW    Modified algorithm in ftp_flash_stor() to support any positive
 *                  non-zero FTP_BFRSIZE value.
 *  07/08/02  JW    Added error handling to allow re-downloading images should
 *                  NAFlashWrite() fail.
 *                  
 */

#include <stdio.h>
#include <stdlib.h>
#include "sockapi.h"
#include "fservapi.h"
#include "sysAccess.h"
#include "bsp_api.h"
#include "reg_def.h"
#include "narmled.h"
#include "cs.h"
//#include "gpiomux_def.h"

/* 
 * ftp_flash_retr
 *
 * This routine is for client to get the ID of the ftp server.
 *
 * INPUTS
 *
 *    argp       Points to string containing file to be created at client.
 *    handle     Used to access FTP server client connection object. 
 *
 */
static int ftp_flash_retr (char *argp, unsigned long handle)
{
    int szData, total, result;
    static char buffer[128]= "NETARM-FTPDL-V400";
    unsigned int romsize   = 0;

#ifdef BSP_ARM9
    romsize = customizeGetCSSize ((WORD32 *)NA_SCM_CS1_BASE_ADDR, (WORD32 *)NA_SCM_CS1_BASE_ADDR);
#else
    romsize = customizeGetCSSize ((WORD32 *)NARM_CSAR0_ADDR, (WORD32 *)NARM_CSOR0_ADDR);
#endif

    if (strcmp (argp, "id") == 0)
    {
        if (romsize == 0x100000)
        {
            sprintf(buffer, "NETARM-FTPDL-V100");
        }
        else if (romsize == 0x200000)
        {
            sprintf(buffer, "NETARM-FTPDL-V200");
        }
        else
        {
            sprintf(buffer, "NETARM-FTPDL-V400");
        }

        szData = strlen(buffer);
    }
    else if (strcmp (argp, "errmsg") == 0)
    {
        buffer[0]='\0';
        szData = strlen (buffer);
    }
    else
    {
        szData = 1000000;
    }

    total = 0;
    do
    {
        result = send (FSHandleToSocket(handle), buffer + total, szData - total, 0);
        if (result > 0)
        {
            total += result;
        }
    }
    while (total < szData);

    return NAFTPS_SUCCESS;
}

static int app_syst_value (char * fsParameters, char * fsUserData, unsigned long handle)
{
    NA_UNUSED_PARAM (handle);
    NA_UNUSED_PARAM (fsParameters);

    sprintf (fsUserData, "NET+OS FTP for the %s", NABspPlatform);
    return NAFTPS_SUCCESS;
}


/*
 *
 *  Function: void ftp_init_flash_download (void)
 *
 *  Description:
 *
 *      This routine setups up the RAM based FTP Server.
 *
 *  Parameters:
 *
 *      none
 *
 *  Return Values:
 *
 *      none
 *
 */

void ftp_init_flash_download (void)

{
    int result;

    /* initialize ftp server with number of users */
    result = FSInitialize(5);
    if (result != NAFTPS_SUCCESS)
    {
        printf("NAFTPAPP: FSInitialize failed\n");
        return;
    }

    result = FSRegisterSTOR (FSStoreImageToFlash);
    if (result != NAFTPS_SUCCESS)
    {
        printf ("FSRegisterSTOR failed to register an override to default.\n");
        return;
    }

    result = FSRegisterRETR (ftp_flash_retr);
    if (result != NAFTPS_SUCCESS)
    {
        printf ("FSRegisterRETR failed to register an override to default.\n");
        return;
    }

    result = FSRegisterControlClose (FSCloseWithFlashCleanUp);
    if (result != NAFTPS_SUCCESS)
    {
        printf ("FSRegisterControlClose failed to register an override to default.\n");
        return;
    }

    result = FSRegisterSYST (app_syst_value);
    if (result != NAFTPS_SUCCESS)
    {
        printf ("FSRegisterSYST failed to register an override to default.\n");
        return;
    }

    /*  Add Username (none) with no password to the System Access Database   */
    NAsetSysAccess (NASYSACC_ADD, "(none)", "", NASYSACC_LEVEL_RW, NULL);

    /*  Actually start the FTP server.  */ 
    result = FSStartServer ();
    if (result != NAFTPS_SUCCESS)
    {
        printf ("FSStartServer failed to register an override to default.\n");
        return;
    }

    printf ("RAM based FTP Server ready.\n");

    NALedBlinkGreen (2, 1);
}
