/*
 *  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
 *
 * Edit History
 * 
 * Date        Initials        Change description
 */
 
#ifndef FS_INTF_LOCAL_H
#define FS_INTF_LOCAL_H

#include <fs.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define _DEBUG_PRINTF           0

#define _READ_PERMISSION_BIT    2
#define _WRITE_PERMISSION_BIT   16      /* bit number for write permisstion */

extern int getErrno (void);

extern int yaffs_FindNetOSPartition(unsigned int *fsStart, unsigned int *fsSize);
extern int yaffs_FindUBootPartition(unsigned int *fsStart, unsigned int *fsSize);

/*ad*
 * When defined to 1, it allows users to list all files and directories which
 * the user has access permission to. It's defined to 0 by default which will
 * displays all files and directories. 
 * 
 * @external
 * @category Networking:FTP_Server:File_System_Interface
 * @since 6.2
 *
*ad*/
#define _RETRICTED_USER_LISTING     0

/*ad*
 * When defined, _FTPS_RESTRICTED_FILE_CREATION allows users to create a file
 * only if it does not already exist. If this definition is 0, data will overwrite
 * an existing file.
 *
 * @external
 * @category Networking:FTP_Server:File_System_Interface
 * @since 6.0
 *
*ad*/
#define _FTPS_RESTRICTED_FILE_CREATION   0

/*ad*
 * Parsing directory or file return status
 *
 * @external
 * @category Networking:FTP_Server:File_System_Interface
 * @since 6.0
 *
 * @name fs_parsing_status   "Parsing File Return Status"
 *
 * @param FILE_OK                 Directory or file was successfully parsed
 * @param INVALID_FILE_LEN        Unable to parse the file 
 * @param INVALID_DIRECTORY_LEN   Unable to parse the directory
*ad*/
#define FILE_OK                  0
#define INVALID_FILE_LEN        -1
#define INVALID_DIRECTORY_LEN   -2

#define LIST_FILE_ONLY          0
#define LIST_FILE_INFO          1

/*
 * This is used for listing files. If the date of a file exceeds this max
 * time (in seconds),  year will be returned. 
 * 
 */
#define FS_MAX_SECONDS      (180 * 24 * 60 * 60)  /* ~6 months */

/*
 * FTP Server Response Buffer Size
 *
 * category Networking:FTP_Server:File_System_Interface
 * since 6.0
 *
 */
#define FS_BUFFER_SIZE          2048  

#define FS_IO_REQ_EVENT_BIT     0x01

/*
 * FTP server to file system interface control block.
 *
 * category Networking:FTP_Server:File_System_Interface
 * since 6.0
 *
 * param fs_handle             The FTP server login user's handle.
 * param directory_path        The array to store the directory path.
 * param buffer                A general purpose buffer used as needed.
 * param io_request            The I/O request control block.
 * param io_event              Threadx event flag, used to wakeup the sys call
 * param dir_info_list         The pointer to a directory information array.
 * param dir_info_list_items   The number of entries in the directory information array.
 * param file_handler          The handle to a file.
 * param file_offset           The offset in bytes from the start of a file.
 * param file_size             The size of a file in bytes.
 * param permissions           The users read and write permissions mask.
 * param error_status          The error status after a file I/O request is completed or terminated.
 * 
 */
typedef struct {
    unsigned long fs_handle;
    char          directory_path[NAFS_DIRECTORY_PATH_SIZE*2+1];
    char          buffer[FS_BUFFER_SIZE];

    NAFS_IO_REQUEST_CB  io_request;
    TX_EVENT_FLAGS_GROUP io_event;
    NAFS_DIR_ENTRY_INFO *dir_info_list;
    unsigned int        dir_info_list_items;
    unsigned int        file_handler;
    unsigned int        file_offset;
    unsigned int        file_size;
    unsigned int        permissions;
    int                 error_status;    
} FS_FILESYSTEM_T;    


/******************************************************************************
 *
 *      Definitions and Declarations for Flash Image Downloads. 
 *
 *      BUFFERSIZE of the block of memory malloced to store FTP fragments 
 *
 *      DELAY_BEFORE_RESET is the number of seconds to wait prior to
 *      wait prior to reseting after an image was downloaded  
 */
#define BUFFERSIZE              (128 * 1024)
#define DELAY_BEFORE_RESET      (2 * NABspTicksPerSecond)
#define NOT_USED                0
#define FS_BLOCK_SIZE           (32 * 1024)

#define FWDL_STACK_SIZE         4096
#define FWDL_THREAD_PRIORITY    16

typedef struct NaFWDlNode_t
{
    unsigned long len;
    char *data;
    struct NaFWDlNode_t *next;
}NaFWDlNode_t;

/*
 * Link list to hold data packets in smaller chunk of data and later used to
 * do CRC validation of entire image before downloading to flash. 
 */
typedef struct NaFWDl_t
{
    int handle;     /* handle to avoid data corruption  */
    unsigned long total_len;  /* total data length received so far */
    unsigned long exp_len;    /* expected data length of the entire image */
    unsigned long allocated_size;  /* allocated size for the image (excluding rom.bin from this) */
    unsigned long romsize;
    unsigned long accumulator;
    NaFWDlNode_t *node;      /* link list used to store the data */
    NaFWDlNode_t *last_node; /* for faster access */
    unsigned char uboot;
}NaFWDl_t; 

#define  NA_FWDL_INIT               0x0000
#define  NA_FWDL_DIRECT             0x0001
#define  NA_FWDL_CRC_CHECK          0x0002
#define  NA_FWDL_UPDATE_FLASH       0x0004
#define  NA_FWDL_HEAP_FULL          0x0008
#define  NA_FWDL_DATA_CLOSED        0x0010    
#define  NA_FWDL_COMPLETED          0x0020
#define  NA_FWDL_INVALID_FILENAME   0x0040
#define  NA_FWDL_FAILED             0x1000
 
extern int FSGetNumberOfUser(void);
extern unsigned int FSGetAccessCode(unsigned long handle);

#ifdef __cplusplus
}
#endif
   
#endif /*  FTP_INTF_LOCAL_H */


