Port of TI's CC3100 Websock camera demo. Using FreeRTOS, mbedTLS, also parts of Arducam for cams ov5642 and 0v2640. Can also use MT9D111. Work in progress. Be warned some parts maybe a bit flacky. This is for Seeed Arch max only, for an M3, see the demo for CM3 using the 0v5642 aducam mini.

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 /*****************************************************************************/
00038 /* Include files                                                             */
00039 /*****************************************************************************/
00040 
00041 #include "cc3100_simplelink.h"
00042 
00043 #ifndef FS_H_
00044 #define FS_H_
00045 
00046 /*!
00047 
00048     \addtogroup FileSystem
00049     @{
00050 
00051 */
00052 
00053 /*****************************************************************************/
00054 /* Macro declarations                                                        */
00055 /*****************************************************************************/
00056 
00057 /* FS error codes */
00058 #define SL_FS_OK                                              (0)
00059 #define SL_FS_ERR_EMPTY_SFLASH                                (-67)
00060 #define SL_FS_ERR_FILE_IS_NOT_SECURE_AND_SIGN                 (-66)
00061 #define SL_FS_ERASING_FLASH                                   (-65)
00062 #define SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY               (-64)
00063 #define SL_FS_WRONG_SIGNATURE                                 (-63)
00064 #define SL_FS_WRONG_SIGNATURE_OR_CERTIFIC_NAME_LENGTH         (-62)
00065 #define SL_FS_NOT_16_ALIGNED                                  (-61)
00066 #define SL_FS_CERT_CHAIN_ERROR                                (-60)
00067 #define SL_FS_FILE_NAME_EXIST                                 (-59)
00068 #define SL_FS_SECURITY_BUF_ALREADY_ALLOC                      (-58)
00069 #define SL_FS_SECURE_FILE_MUST_BE_COMMIT                      (-57)
00070 #define SL_FS_ERR_INCORRECT_OFFSET_ALIGNMENT                  (-56)
00071 #define SL_FS_ERR_FAILED_READ_NVMEM_HEADER                    (-55)
00072 #define SL_FS_WRONG_FILE_NAME                                 (-54)
00073 #define SL_FS_FILE_SYSTEM_IS_LOCKED                           (-53)
00074 #define SL_FS_SECURITY_ALLERT                                 (-52)
00075 #define SL_FS_FILE_UNVALID_FILE_SIZE                          (-51)
00076 #define SL_FS_ERR_TOKEN_IS_NOT_VALID                          (-50)
00077 #define SL_FS_NO_DEVICE_IS_LOADED                             (-49)
00078 #define SL_FS_DATA_ADDRESS_SHOUD_BE_IN_DATA_RAM               (-48)
00079 #define SL_FS_DATA_IS_NOT_ALIGNED                             (-47)
00080 #define SL_FS_ERR_OVERLAP_DETECTION_THRESHHOLD                (-46)
00081 #define SL_FS_FILE_HAS_RESERVED_NV_INDEX                      (-45)
00082 #define SL_FS_ERR_MAX_FS_FILES_IS_LARGER                      (-44)
00083 #define SL_FS_ERR_MAX_FS_FILES_IS_SMALLER                     (-43)
00084 #define SL_FS_FILE_MAX_SIZE_EXCEEDED                          (-42)
00085 #define SL_FS_INVALID_BUFFER_FOR_READ                         (-41)
00086 #define SL_FS_INVALID_BUFFER_FOR_WRITE                        (-40)
00087 #define SL_FS_ERR_FILE_IMAGE_IS_CORRUPTED                     (-39)
00088 #define SL_FS_ERR_SIZE_OF_FILE_EXT_EXCEEDED                   (-38)
00089 #define SL_FS_WARNING_FILE_NAME_NOT_KEPT                      (-37)
00090 #define SL_FS_ERR_DEVICE_IS_NOT_FORMATTED                     (-36)
00091 #define SL_FS_ERR_FAILED_WRITE_NVMEM_HEADER                   (-35)
00092 #define SL_FS_ERR_NO_AVAILABLE_NV_INDEX                       (-34)
00093 #define SL_FS_ERR_FAILED_TO_ALLOCATE_MEM                      (-33)
00094 #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_2           (-32)
00095 #define SL_FS_ERR_FAILED_TO_READ_INTEGRITY_HEADER_1           (-31)
00096 #define SL_FS_ERR_NO_AVAILABLE_BLOCKS                         (-30)
00097 #define SL_FS_ERR_FILE_MAX_SIZE_BIGGER_THAN_EXISTING_FILE     (-29)
00098 #define SL_FS_ERR_FILE_EXISTS_ON_DIFFERENT_DEVICE_ID          (-28)
00099 #define SL_FS_ERR_INVALID_ACCESS_TYPE                         (-27)
00100 #define SL_FS_ERR_FILE_ALREADY_EXISTS                         (-26)
00101 #define SL_FS_ERR_PROGRAM                                     (-25)
00102 #define SL_FS_ERR_NO_ENTRIES_AVAILABLE                        (-24)
00103 #define SL_FS_ERR_FILE_ACCESS_IS_DIFFERENT                    (-23)
00104 #define SL_FS_ERR_BAD_FILE_MODE                               (-22)
00105 #define SL_FS_ERR_FAILED_READ_NVFILE                          (-21)
00106 #define SL_FS_ERR_FAILED_INIT_STORAGE                         (-20)
00107 #define SL_FS_ERR_CONTINUE_WRITE_MUST_BE_MOD_4                (-19)
00108 #define SL_FS_ERR_FAILED_LOAD_FILE                            (-18)
00109 #define SL_FS_ERR_INVALID_HANDLE                              (-17)
00110 #define SL_FS_ERR_FAILED_TO_WRITE                             (-16)
00111 #define SL_FS_ERR_OFFSET_OUT_OF_RANGE                         (-15)
00112 #define SL_FS_ERR_ALLOC                                       (-14)
00113 #define SL_FS_ERR_READ_DATA_LENGTH                            (-13)
00114 #define SL_FS_ERR_INVALID_FILE_ID                             (-12)
00115 #define SL_FS_ERR_FILE_NOT_EXISTS                             (-11)
00116 #define SL_FS_ERR_EMPTY_ERROR                                 (-10)
00117 #define SL_FS_ERR_INVALID_ARGS                                (-9)
00118 #define SL_FS_ERR_FAILED_TO_CREATE_FILE                       (-8)
00119 #define SL_FS_ERR_FS_ALREADY_LOADED                           (-7)
00120 #define SL_FS_ERR_UNKNOWN                                     (-6)
00121 #define SL_FS_ERR_FAILED_TO_CREATE_LOCK_OBJ                   (-5)
00122 #define SL_FS_ERR_DEVICE_NOT_LOADED                           (-4)
00123 #define SL_FS_ERR_INVALID_MAGIC_NUM                           (-3)
00124 #define SL_FS_ERR_FAILED_TO_READ                              (-2)
00125 #define SL_FS_ERR_NOT_SUPPORTED                               (-1)
00126 /* end of error codes */
00127 
00128 #define _FS_MODE_ACCESS_RESERVED_OFFSET                       (24)
00129 #define _FS_MODE_ACCESS_RESERVED_MASK                         (0xFF)
00130 #define _FS_MODE_ACCESS_FLAGS_OFFSET                          (16)
00131 #define _FS_MODE_ACCESS_FLAGS_MASK                            (0xFF)
00132 #define _FS_MODE_ACCESS_OFFSET                                (12)
00133 #define _FS_MODE_ACCESS_MASK                                  (0xF)
00134 #define _FS_MODE_OPEN_SIZE_GRAN_OFFSET                        (8)
00135 #define _FS_MODE_OPEN_SIZE_GRAN_MASK                          (0xF)
00136 #define _FS_MODE_OPEN_SIZE_OFFSET                             (0)
00137 #define _FS_MODE_OPEN_SIZE_MASK                               (0xFF)
00138 #define MAX_MODE_SIZE                                         (0xFF)
00139 #define _FS_MODE(Access, SizeGran, Size,Flags)        (uint32_t)(((uint32_t)((Access) & _FS_MODE_ACCESS_MASK)<<_FS_MODE_ACCESS_OFFSET) |  \
00140                                                             ((uint32_t)((SizeGran) & _FS_MODE_OPEN_SIZE_GRAN_MASK)<<_FS_MODE_OPEN_SIZE_GRAN_OFFSET) | \
00141                                                             ((uint32_t)((Size) & _FS_MODE_OPEN_SIZE_MASK)<<_FS_MODE_OPEN_SIZE_OFFSET) | \
00142                                                             ((uint32_t)((Flags) & _FS_MODE_ACCESS_FLAGS_MASK)<<_FS_MODE_ACCESS_FLAGS_OFFSET))
00143 
00144 
00145 /*  sl_FsOpen options */
00146 /*  Open for Read */
00147 #define FS_MODE_OPEN_READ                                     _FS_MODE(_FS_MODE_OPEN_READ,0,0,0)
00148 /*  Open for Write (in case file exist) */
00149 #define FS_MODE_OPEN_WRITE                                    _FS_MODE(_FS_MODE_OPEN_WRITE,0,0,0)
00150 /* Open for Creating a new file */
00151 #define FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags)   _sl_GetCreateFsMode(maxSizeInBytes,accessModeFlags)
00152 
00153 namespace mbed_cc3100 {
00154 
00155 /*****************************************************************************/
00156 /* Structure/Enum declarations                                               */
00157 /*****************************************************************************/
00158 typedef struct {
00159     uint16_t flags;
00160     uint32_t  FileLen;
00161     uint32_t  AllocatedLen;
00162     uint32_t  Token[4];
00163 } SlFsFileInfo_t;
00164 
00165 typedef enum {
00166     _FS_MODE_OPEN_READ            = 0,
00167     _FS_MODE_OPEN_WRITE,
00168     _FS_MODE_OPEN_CREATE,
00169     _FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST
00170 } SlFsFileOpenAccessType_e;
00171 
00172 typedef enum {
00173     _FS_FILE_OPEN_FLAG_COMMIT  =  0x1,           /* MIRROR - for fail safe */
00174     _FS_FILE_OPEN_FLAG_SECURE  =  0x2,           /* SECURE */
00175     _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST = 0x4,  /* Relevant to secure file only  */
00176     _FS_FILE_OPEN_FLAG_STATIC  =           0x8,  /*  Relevant to secure file only */
00177     _FS_FILE_OPEN_FLAG_VENDOR  =           0x10, /*  Relevant to secure file only */
00178     _FS_FILE_PUBLIC_WRITE=                 0x20, /* Relevant to secure file only, the file can be opened for write without Token */
00179     _FS_FILE_PUBLIC_READ =                 0x40  /* Relevant to secure file only, the file can be opened for read without Token  */
00180 } SlFileOpenFlags_e;
00181 
00182 typedef enum {
00183     _FS_MODE_SIZE_GRAN_256B    = 0,   /*  MAX_SIZE = 64K  */
00184     _FS_MODE_SIZE_GRAN_1KB,           /*  MAX_SIZE = 256K */
00185     _FS_MODE_SIZE_GRAN_4KB,           /*  MAX_SZIE = 1M   */
00186     _FS_MODE_SIZE_GRAN_16KB,          /*  MAX_SIZE = 4M   */
00187     _FS_MODE_SIZE_GRAN_64KB,          /*  MAX_SIZE = 16M  */
00188     _FS_MAX_MODE_SIZE_GRAN
00189 } _SlFsFileOpenMaxSizeGran_e;
00190 
00191 class cc3100_driver;
00192 
00193 class cc3100_fs
00194 {
00195 
00196 public:
00197 
00198     cc3100_fs(cc3100_driver &driver);
00199 
00200     ~cc3100_fs();
00201 
00202     /*****************************************************************************/
00203     /* Internal Function prototypes                                              */
00204     /*****************************************************************************/
00205     uint32_t _sl_GetCreateFsMode(uint32_t maxSizeInBytes,uint32_t accessFlags);
00206     uint16_t _sl_Strlen(const uint8_t *buffer);
00207 
00208     /*****************************************************************************/
00209     /* Function prototypes                                                       */
00210     /*****************************************************************************/
00211 
00212     /*!
00213         \brief open file for read or write from/to storage device
00214 
00215         \param[in]      pFileName                  File Name buffer pointer
00216         \param[in]      AccessModeAndMaxSize       Options: As described below
00217         \param[in]      pToken                     Reserved for future use. Use NULL for this field
00218         \param[out]     pFileHandle      Pointing on the file and used for read and write commands to the file
00219 
00220          AccessModeAndMaxSize possible input                                                                        \n
00221          FS_MODE_OPEN_READ                                        - Read a file                                                                  \n
00222          FS_MODE_OPEN_WRITE                                       - Open for write for an existing file                                          \n
00223          FS_MODE_OPEN_CREATE(maxSizeInBytes,accessModeFlags)      - Open for creating a new file. Max file size is defined in bytes.             \n
00224                                                                     For optimal FS size, use max size in 4K-512 bytes steps (e.g. 3584,7680,117760)  \n
00225                                                                     Several access modes bits can be combined together from SlFileOpenFlags_e enum
00226 
00227         \return         On success, zero is returned. On error, an error code is returned
00228 
00229         \sa             sl_FsRead sl_FsWrite sl_FsClose
00230         \note           belongs to \ref basic_api
00231         \warning
00232         \par            Example:
00233         \code
00234            char*           DeviceFileName = "MyFile.txt";
00235            unsigned long   MaxSize = 63 * 1024; //62.5K is max file size
00236            long            DeviceFileHandle = -1;
00237            long            RetVal;        //negative retval is an error
00238            unsigned long   Offset = 0;
00239            uint8_t   InputBuffer[100];
00240 
00241            // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit
00242            RetVal = sl_FsOpen((uint8_t *)DeviceFileName,
00243                                             FS_MODE_OPEN_CREATE(MaxSize , _FS_FILE_OPEN_FLAG_NO_SIGNATURE_TEST | _FS_FILE_OPEN_FLAG_COMMIT ),
00244                                             NULL, &DeviceFileHandle);
00245 
00246            Offset = 0;
00247            //Preferred in secure file that the Offset and the length will be aligned to 16 bytes.
00248            RetVal = sl_FsWrite( DeviceFileHandle, Offset, (uint8_t *)"HelloWorld", strlen("HelloWorld"));
00249 
00250            RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
00251 
00252            // open the same file for read, using the Token we got from the creation procedure above
00253            RetVal = sl_FsOpen((uint8_t *)DeviceFileName,
00254                                             FS_MODE_OPEN_READ,
00255                                             NULL, &DeviceFileHandle);
00256 
00257            Offset = 0;
00258            RetVal = sl_FsRead( DeviceFileHandle, Offset, (uint8_t *)InputBuffer, strlen("HelloWorld"));
00259 
00260            RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
00261 
00262          \endcode
00263     */
00264 #if _SL_INCLUDE_FUNC(sl_FsOpen)
00265     int32_t sl_FsOpen(const uint8_t *pFileName, const uint32_t AccessModeAndMaxSize,uint32_t *pToken,int32_t *pFileHandle);
00266 #endif
00267 
00268     /*!
00269         \brief close file in storage device
00270 
00271         \param[in]      FileHdl                 Pointer to the file (assigned from sl_FsOpen)
00272         \param[in]      pCeritificateFileName   Reserved for future use. Use NULL.
00273         \param[in]      pSignature              Reserved for future use. Use NULL.
00274         \param[in]      SignatureLen            Reserved for future use. Use 0.
00275 
00276 
00277         \return         On success, zero is returned.  On error, an error code is returned
00278 
00279         \sa             sl_FsRead sl_FsWrite sl_FsOpen
00280         \note           Call the fs_Close  with signature = 'A' signature len = 1 for activating an abort action
00281         \warning
00282         \par            Example:
00283         \code
00284         sl_FsClose(FileHandle,0,0,0);
00285         \endcode
00286     */
00287 #if _SL_INCLUDE_FUNC(sl_FsClose)
00288     int16_t sl_FsClose(const int32_t FileHdl, const uint8_t* pCeritificateFileName, const uint8_t* pSignature, const uint32_t SignatureLen);
00289 #endif
00290 
00291     /*!
00292         \brief Read block of data from a file in storage device
00293 
00294         \param[in]      FileHdl Pointer to the file (assigned from sl_FsOpen)
00295         \param[in]      Offset  Offset to specific read block
00296         \param[out]     pData   Pointer for the received data
00297         \param[in]      Len     Length of the received data
00298 
00299         \return         On success, returns the number of read bytes. On error, negative number is returned
00300 
00301         \sa             sl_FsClose sl_FsWrite sl_FsOpen
00302         \note           belongs to \ref basic_api
00303         \warning
00304         \par            Example:
00305         \code
00306         Status = sl_FsRead(FileHandle, 0, &readBuff[0], readSize);
00307         \endcode
00308     */
00309 #if _SL_INCLUDE_FUNC(sl_FsRead)
00310     int32_t sl_FsRead(const int32_t FileHdl,uint32_t Offset ,uint8_t*  pData,uint32_t Len);
00311 #endif
00312 
00313     /*!
00314         \brief write block of data to a file in storage device
00315 
00316         \param[in]      FileHdl  Pointer to the file (assigned from sl_FsOpen)
00317         \param[in]      Offset   Offset to specific block to be written
00318         \param[in]      pData    Pointer the transmitted data to the storage device
00319         \param[in]      Len      Length of the transmitted data
00320 
00321         \return         On success, returns the number of written bytes.  On error, an error code is returned
00322 
00323         \sa
00324         \note           belongs to \ref basic_api
00325         \warning
00326         \par            Example:
00327         \code
00328         Status = sl_FsWrite(FileHandle, 0, &buff[0], readSize);
00329         \endcode
00330     */
00331 #if _SL_INCLUDE_FUNC(sl_FsWrite)
00332     int32_t sl_FsWrite(const int32_t FileHdl,uint32_t Offset,uint8_t*  pData,uint32_t Len);
00333 #endif
00334 
00335     /*!
00336         \brief get info on a file
00337 
00338         \param[in]      pFileName    File name
00339         \param[in]      Token        Reserved for future use. Use 0
00340         \param[out]     pFsFileInfo Returns the File's Information: flags,file size, allocated size and Tokens
00341 
00342         \return         On success, zero is returned.   On error, an error code is returned
00343 
00344         \sa             sl_FsOpen
00345         \note           belongs to \ref basic_api
00346         \warning
00347         \par            Example:
00348         \code
00349         Status = sl_FsGetInfo("FileName.html",0,&FsFileInfo);
00350         \endcode
00351     */
00352 #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
00353     int16_t sl_FsGetInfo(const uint8_t *pFileName, const uint32_t Token,SlFsFileInfo_t* pFsFileInfo);
00354 #endif
00355 
00356     /*!
00357         \brief Delete specific file from a storage or all files from a storage (format)
00358 
00359         \param[in]      pFileName    File Name
00360         \param[in]      Token        Reserved for future use. Use 0
00361         \return         On success, zero is returned.  On error, an error code is returned
00362 
00363         \sa
00364         \note           belongs to \ref basic_api
00365         \warning
00366         \par            Example:
00367         \code
00368         Status = sl_FsDel("FileName.html",0);
00369         \endcode
00370     */
00371 #if _SL_INCLUDE_FUNC(sl_FsDel)
00372     int16_t sl_FsDel(const uint8_t *pFileName, const uint32_t Token);
00373 #endif
00374 
00375 private:
00376 
00377     cc3100_driver &_driver;
00378 
00379 };//class
00380 
00381 }//namespace mbed_cc3100
00382 
00383 /*!
00384 
00385  Close the Doxygen group.
00386  @}
00387 
00388  */
00389 
00390 #endif /*  __FS_H__ */
00391 
00392