TI's CC3100 host driver and demo. Experimental and a work in progress.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3100_fs.h Source File

cc3100_fs.h

00001 /*
00002  * fs.h - CC31xx/CC32xx Host Driver Implementation
00003  *
00004  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ 
00005  * 
00006  * 
00007  *  Redistribution and use in source and binary forms, with or without 
00008  *  modification, are permitted provided that the following conditions 
00009  *  are met:
00010  *
00011  *    Redistributions of source code must retain the above copyright 
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  *    Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the 
00016  *    documentation and/or other materials provided with the   
00017  *    distribution.
00018  *
00019  *    Neither the name of Texas Instruments Incorporated nor the names of
00020  *    its contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00027  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00028  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00029  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00033  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035 */
00036     
00037 #ifndef __FS_H__
00038 #define __FS_H__
00039 
00040 /*****************************************************************************/
00041 /* Include files                                                             */
00042 /*****************************************************************************/
00043 
00044 #include "cc3100_simplelink.h"
00045 
00046 #ifdef __cplusplus
00047 extern "C" {
00048 #endif
00049 
00050 /*!
00051 
00052     \addtogroup FileSystem
00053     @{
00054 
00055 */
00056 
00057 /*****************************************************************************/
00058 /* Macro declarations                                                        */
00059 /*****************************************************************************/
00060 
00061 /* FS error codes */
00062 #define SL_FS_OK                                              (0)
00063 #define SL_FS_ERR_EMPTY_SFLASH                                (-67)
00064 #define SL_FS_ERR_FILE_IS_NOT_SECURE_AND_SIGN                 (-66)
00065 #define SL_FS_ERASING_FLASH                                   (-65)
00066 #define SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY               (-64)
00067 #define SL_FS_WRONG_SIGNATURE                                 (-63)
00068 #define SL_FS_WRONG_SIGNATURE_OR_CERTIFIC_NAME_LENGTH         (-62)
00069 #define SL_FS_NOT_16_ALIGNED                                  (-61)
00070 #define SL_FS_CERT_CHAIN_ERROR                                (-60)
00071 #define SL_FS_FILE_NAME_EXIST                                 (-59)
00072 #define SL_FS_SECURITY_BUF_ALREADY_ALLOC                      (-58)
00073 #define SL_FS_SECURE_FILE_MUST_BE_COMMIT                      (-57)
00074 #define SL_FS_ERR_INCORRECT_OFFSET_ALIGNMENT                  (-56)
00075 #define SL_FS_ERR_FAILED_READ_NVMEM_HEADER                    (-55)
00076 #define SL_FS_WRONG_FILE_NAME                                 (-54)
00077 #define SL_FS_FILE_SYSTEM_IS_LOCKED                           (-53)
00078 #define SL_FS_SECURITY_ALLERT                                 (-52)
00079 #define SL_FS_FILE_UNVALID_FILE_SIZE                          (-51)
00080 #define SL_FS_ERR_TOKEN_IS_NOT_VALID                          (-50)
00081 #define SL_FS_NO_DEVICE_IS_LOADED                             (-49)
00082 #define SL_FS_DATA_ADDRESS_SHOUD_BE_IN_DATA_RAM               (-48)
00083 #define SL_FS_DATA_IS_NOT_ALIGNED                             (-47)
00084 #define SL_FS_ERR_OVERLAP_DETECTION_THRESHHOLD                (-46)
00085 #define SL_FS_FILE_HAS_RESERVED_NV_INDEX                      (-45)
00086 #define SL_FS_ERR_MAX_FS_FILES_IS_LARGER                      (-44)
00087 #define SL_FS_ERR_MAX_FS_FILES_IS_SMALLER                     (-43)
00088 #define SL_FS_FILE_MAX_SIZE_EXCEEDED                          (-42)
00089 #define SL_FS_INVALID_BUFFER_FOR_READ                         (-41)
00090 #define SL_FS_INVALID_BUFFER_FOR_WRITE                        (-40)
00091 #define SL_FS_ERR_FILE_IMAGE_IS_CORRUPTED                     (-39)
00092 #define SL_FS_ERR_SIZE_OF_FILE_EXT_EXCEEDED                   (-38)
00093 #define SL_FS_WARNING_FILE_NAME_NOT_KEPT                      (-37)
00094 #define SL_FS_ERR_DEVICE_IS_NOT_FORMATTED                     (-36)
00095 #define SL_FS_ERR_FAILED_WRITE_NVMEM_HEADER                   (-35)
00096 #define SL_FS_ERR_NO_AVAILABLE_NV_INDEX                       (-34)
00097 #define SL_FS_ERR_FAILED_TO_ALLOCATE_MEM                      (-33)
00098 #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_2           (-32)
00099 #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_1           (-31)
00100 #define SL_FS_ERR_NO_AVAILABLE_BLOCKS                         (-30)
00101 #define SL_FS_ERR_FILE_MAX_SIZE_BIGGER_THAN_EXISTING_FILE     (-29)
00102 #define SL_FS_ERR_FILE_EXISTS_ON_DIFFERENT_DEVICE_ID          (-28)
00103 #define SL_FS_ERR_INVALID_ACCESS_TYPE                         (-27)
00104 #define SL_FS_ERR_FILE_ALREADY_EXISTS                         (-26)
00105 #define SL_FS_ERR_PROGRAM                                     (-25)
00106 #define SL_FS_ERR_NO_ENTRIES_AVAILABLE                        (-24)
00107 #define SL_FS_ERR_FILE_ACCESS_IS_DIFFERENT                    (-23)
00108 #define SL_FS_ERR_BAD_FILE_MODE                               (-22)
00109 #define SL_FS_ERR_FAILED_READ_NVFILE                          (-21)
00110 #define SL_FS_ERR_FAILED_INIT_STORAGE                         (-20)
00111 #define SL_FS_ERR_CONTINUE_WRITE_MUST_BE_MOD_4                (-19)
00112 #define SL_FS_ERR_FAILED_LOAD_FILE                            (-18)
00113 #define SL_FS_ERR_INVALID_HANDLE                              (-17)
00114 #define SL_FS_ERR_FAILED_TO_WRITE                             (-16)
00115 #define SL_FS_ERR_OFFSET_OUT_OF_RANGE                         (-15)
00116 #define SL_FS_ERR_ALLOC                                       (-14)
00117 #define SL_FS_ERR_READ_DATA_LENGTH                            (-13)
00118 #define SL_FS_ERR_INVALID_FILE_ID                             (-12)
00119 #define SL_FS_ERR_FILE_NOT_EXISTS                             (-11)
00120 #define SL_FS_ERR_EMPTY_ERROR                                 (-10)
00121 #define SL_FS_ERR_INVALID_ARGS                                (-9)
00122 #define SL_FS_ERR_FAILED_TO_CREATE_FILE                       (-8)
00123 #define SL_FS_ERR_FS_ALREADY_LOADED                           (-7)
00124 #define SL_FS_ERR_UNKNOWN                                     (-6)
00125 #define SL_FS_ERR_FAILED_TO_CREATE_LOCK_OBJ                   (-5)
00126 #define SL_FS_ERR_DEVICE_NOT_LOADED                           (-4)
00127 #define SL_FS_ERR_INVALID_MAGIC_NUM                           (-3)
00128 #define SL_FS_ERR_FAILED_TO_READ                              (-2)
00129 #define SL_FS_ERR_NOT_SUPPORTED                               (-1)
00130 /* end of error codes */
00131 
00132 #define _FS_MODE_ACCESS_RESERVED_OFFSET                       (24)
00133 #define _FS_MODE_ACCESS_RESERVED_MASK                         (0xFF)
00134 #define _FS_MODE_ACCESS_FLAGS_OFFSET                          (16)
00135 #define _FS_MODE_ACCESS_FLAGS_MASK                            (0xFF)
00136 #define _FS_MODE_ACCESS_OFFSET                                (12)
00137 #define _FS_MODE_ACCESS_MASK                                  (0xF)
00138 #define _FS_MODE_OPEN_SIZE_GRAN_OFFSET                        (8)
00139 #define _FS_MODE_OPEN_SIZE_GRAN_MASK                          (0xF)
00140 #define _FS_MODE_OPEN_SIZE_OFFSET                             (0)
00141 #define _FS_MODE_OPEN_SIZE_MASK                               (0xFF)
00142 #define MAX_MODE_SIZE                                         (0xFF)
00143 #define _FS_MODE(Access, SizeGran, Size,Flags)        (_u32)(((_u32)((Access) & _FS_MODE_ACCESS_MASK)<<_FS_MODE_ACCESS_OFFSET) |  \
00144                                                             ((_u32)((SizeGran) & _FS_MODE_OPEN_SIZE_GRAN_MASK)<<_FS_MODE_OPEN_SIZE_GRAN_OFFSET) | \
00145                                                             ((_u32)((Size) & _FS_MODE_OPEN_SIZE_MASK)<<_FS_MODE_OPEN_SIZE_OFFSET) | \
00146                                                             ((_u32)((Flags) & _FS_MODE_ACCESS_FLAGS_MASK)<<_FS_MODE_ACCESS_FLAGS_OFFSET))
00147 
00148 
00149 /*  sl_FsOpen options */
00150 /*  Open for Read */
00151 #define FS_MODE_OPEN_READ                                     _FS_MODE(_FS_MODE_OPEN_READ,0,0,0)
00152 /*  Open for Write (in case file exist) */
00153 #define FS_MODE_OPEN_WRITE                                    _FS_MODE(_FS_MODE_OPEN_WRITE,0,0,0)
00154 /* Open for Creating a new file */
00155 #define FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags)   _sl_GetCreateFsMode(maxSizeInBytes,accessModeFlags)        
00156 
00157 /*****************************************************************************/
00158 /* Structure/Enum declarations                                               */
00159 /*****************************************************************************/
00160 typedef struct
00161 {
00162     _u16 flags;
00163     _u32  FileLen;
00164     _u32  AllocatedLen;
00165     _u32  Token[4];
00166 }SlFsFileInfo_t;
00167 
00168 typedef enum
00169 {
00170        _FS_MODE_OPEN_READ            = 0,
00171        _FS_MODE_OPEN_WRITE,
00172        _FS_MODE_OPEN_CREATE,
00173        _FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST
00174 }SlFsFileOpenAccessType_e;
00175 
00176 typedef enum
00177 {
00178    _FS_FILE_OPEN_FLAG_COMMIT  =  0x1,           /* MIRROR - for fail safe */
00179    _FS_FILE_OPEN_FLAG_SECURE  =  0x2,           /* SECURE */
00180    _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST = 0x4,  /* Relevant to secure file only  */
00181    _FS_FILE_OPEN_FLAG_STATIC  =           0x8,  /*  Relevant to secure file only */
00182    _FS_FILE_OPEN_FLAG_VENDOR  =           0x10, /*  Relevant to secure file only */
00183    _FS_FILE_PUBLIC_WRITE=                 0x20, /* Relevant to secure file only, the file can be opened for write without Token */
00184    _FS_FILE_PUBLIC_READ =                 0x40  /* Relevant to secure file only, the file can be opened for read without Token  */
00185 }SlFileOpenFlags_e;
00186 
00187 typedef enum
00188 {
00189        _FS_MODE_SIZE_GRAN_256B    = 0,   /*  MAX_SIZE = 64K  */
00190        _FS_MODE_SIZE_GRAN_1KB,           /*  MAX_SIZE = 256K */
00191        _FS_MODE_SIZE_GRAN_4KB,           /*  MAX_SZIE = 1M   */
00192        _FS_MODE_SIZE_GRAN_16KB,          /*  MAX_SIZE = 4M   */
00193        _FS_MODE_SIZE_GRAN_64KB,          /*  MAX_SIZE = 16M  */
00194        _FS_MAX_MODE_SIZE_GRAN
00195 }_SlFsFileOpenMaxSizeGran_e;
00196 
00197 /*****************************************************************************/
00198 /* Internal Function prototypes                                              */
00199 /*****************************************************************************/
00200 _u32 _sl_GetCreateFsMode(_u32 maxSizeInBytes,_u32 accessFlags);
00201 
00202 /*****************************************************************************/
00203 /* Function prototypes                                                       */
00204 /*****************************************************************************/
00205 
00206 /*!
00207     \brief open file for read or write from/to storage device
00208     
00209     \param[in]      pFileName                  File Name buffer pointer  
00210     \param[in]      AccessModeAndMaxSize       Options: As described below
00211     \param[in]      pToken                     Reserved for future use. Use NULL for this field
00212     \param[out]     pFileHandle      Pointing on the file and used for read and write commands to the file     
00213      
00214      AccessModeAndMaxSize possible input                                                                        \n
00215      FS_MODE_OPEN_READ                                        - Read a file                                                                  \n
00216      FS_MODE_OPEN_WRITE                                       - Open for write for an existing file                                          \n
00217      FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags)      - Open for creating a new file. Max file size is defined in bytes.             \n
00218                                                                 For optimal FS size, use max size in 4K-512 bytes steps (e.g. 3584,7680,117760)  \n
00219                                                                 Several access modes bits can be combined together from SlFileOpenFlags_e enum
00220 
00221     \return         On success, zero is returned. On error, an error code is returned    
00222     
00223     \sa             sl_FsRead sl_FsWrite sl_FsClose       
00224     \note           belongs to \ref basic_api       
00225     \warning        
00226     \par            Example:
00227     \code
00228        char*           DeviceFileName = "MyFile.txt";
00229        unsigned long   MaxSize = 63 * 1024; //62.5K is max file size
00230        long            DeviceFileHandle = -1;
00231        long            RetVal;        //negative retval is an error
00232        unsigned long   Offset = 0;
00233        unsigned char   InputBuffer[100];
00234 
00235        // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit
00236        RetVal = sl_FsOpen((unsigned char *)DeviceFileName,
00237                                         FS_MODE_OPEN_CREATE(MaxSize , _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST | _FS_FILE_OPEN_FLAG_COMMIT ),
00238                                         NULL, &DeviceFileHandle);
00239 
00240        Offset = 0;
00241        //Preferred in secure file that the Offset and the length will be aligned to 16 bytes.
00242        RetVal = sl_FsWrite( DeviceFileHandle, Offset, (unsigned char *)"HelloWorld", strlen("HelloWorld"));
00243 
00244        RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
00245 
00246        // open the same file for read, using the Token we got from the creation procedure above
00247        RetVal = sl_FsOpen((unsigned char *)DeviceFileName,
00248                                         FS_MODE_OPEN_READ,
00249                                         NULL, &DeviceFileHandle);
00250 
00251        Offset = 0;
00252        RetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer, strlen("HelloWorld"));
00253 
00254        RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
00255 
00256      \endcode
00257 */
00258 #if _SL_INCLUDE_FUNC(sl_FsOpen)
00259 _i32 sl_FsOpen(_u8 *pFileName,_u32 AccessModeAndMaxSize,_u32 *pToken,_i32 *pFileHandle);
00260 #endif
00261 
00262 /*!
00263     \brief close file in storage device
00264     
00265     \param[in]      FileHdl                 Pointer to the file (assigned from sl_FsOpen) 
00266     \param[in]      pCeritificateFileName   Reserved for future use. Use NULL.
00267     \param[in]      pSignature              Reserved for future use. Use NULL.
00268     \param[in]      SignatureLen            Reserved for future use. Use 0.
00269                     
00270 
00271     \return         On success, zero is returned.  On error, an error code is returned   
00272     
00273     \sa             sl_FsRead sl_FsWrite sl_FsOpen        
00274     \note           Call the fs_Close  with signature = 'A' signature len = 1 for activating an abort action
00275     \warning
00276     \par            Example:
00277     \code            
00278     sl_FsClose(FileHandle,0,0,0);
00279     \endcode
00280 */
00281 #if _SL_INCLUDE_FUNC(sl_FsClose)
00282 _i16 sl_FsClose(_i32 FileHdl,_u8* pCeritificateFileName,_u8* pSignature,_u32 SignatureLen);
00283 #endif
00284 
00285 /*!
00286     \brief Read block of data from a file in storage device
00287     
00288     \param[in]      FileHdl Pointer to the file (assigned from sl_FsOpen)    
00289     \param[in]      Offset  Offset to specific read block
00290     \param[out]     pData   Pointer for the received data
00291     \param[in]      Len     Length of the received data
00292      
00293     \return         On success, returns the number of read bytes. On error, negative number is returned    
00294     
00295     \sa             sl_FsClose sl_FsWrite sl_FsOpen        
00296     \note           belongs to \ref basic_api       
00297     \warning     
00298     \par            Example:
00299     \code    
00300     Status = sl_FsRead(FileHandle, 0, &readBuff[0], readSize);
00301     \endcode
00302 */
00303 #if _SL_INCLUDE_FUNC(sl_FsRead)
00304 _i32 sl_FsRead(_i32 FileHdl,_u32 Offset ,_u8*  pData,_u32 Len);
00305 #endif
00306 
00307 /*!
00308     \brief write block of data to a file in storage device
00309     
00310     \param[in]      FileHdl  Pointer to the file (assigned from sl_FsOpen)  
00311     \param[in]      Offset   Offset to specific block to be written
00312     \param[in]      pData    Pointer the transmitted data to the storage device
00313     \param[in]      Len      Length of the transmitted data
00314      
00315     \return         On success, returns the number of written bytes.  On error, an error code is returned
00316     
00317     \sa                     
00318     \note           belongs to \ref basic_api       
00319     \warning     
00320     \par            Example:
00321     \code    
00322     Status = sl_FsWrite(FileHandle, 0, &buff[0], readSize);
00323     \endcode
00324 */
00325 #if _SL_INCLUDE_FUNC(sl_FsWrite)
00326 _i32 sl_FsWrite(_i32 FileHdl,_u32 Offset,_u8*  pData,_u32 Len);
00327 #endif
00328 
00329 /*!
00330     \brief get info on a file
00331     
00332     \param[in]      pFileName    File name
00333     \param[in]      Token        Reserved for future use. Use 0
00334     \param[out]     pFsFileInfo Returns the File's Information: flags,file size, allocated size and Tokens 
00335      
00336     \return         On success, zero is returned.   On error, an error code is returned    
00337     
00338     \sa             sl_FsOpen        
00339     \note           belongs to \ref basic_api       
00340     \warning        
00341     \par            Example:
00342     \code    
00343     Status = sl_FsGetInfo("FileName.html",0,&FsFileInfo);
00344     \endcode
00345 */
00346 #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
00347 _i16 sl_FsGetInfo(_u8 *pFileName,_u32 Token,SlFsFileInfo_t* pFsFileInfo);
00348 #endif
00349 
00350 /*!
00351     \brief Delete specific file from a storage or all files from a storage (format)
00352     
00353     \param[in]      pFileName    File Name 
00354     \param[in]      Token        Reserved for future use. Use 0
00355     \return         On success, zero is returned.  On error, an error code is returned    
00356     
00357     \sa                     
00358     \note           belongs to \ref basic_api       
00359     \warning     
00360     \par            Example:
00361     \code    
00362     Status = sl_FsDel("FileName.html",0);
00363     \endcode
00364 */
00365 #if _SL_INCLUDE_FUNC(sl_FsDel)
00366 _i16 sl_FsDel(_u8 *pFileName,_u32 Token);
00367 #endif
00368 /*!
00369 
00370  Close the Doxygen group.
00371  @}
00372 
00373  */
00374 
00375 #ifdef  __cplusplus
00376 }
00377 #endif /*  __cplusplus */
00378 
00379 #endif /*  __FS_H__ */
00380 
00381