TI's CC3100 websocket camera demo with Arducam mini ov5642 and freertos. Should work with other M3's. Work in progress test demo.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3100_fs.cpp Source File

cc3100_fs.cpp

00001 /*
00002  * fs.c - 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 
00039 /*****************************************************************************/
00040 /* Include files                                                             */
00041 /*****************************************************************************/
00042 #include "cc3100_simplelink.h"
00043 #include "cc3100_protocol.h"
00044 #include "cc3100_driver.h"
00045 
00046 #include "cc3100_fs.h"
00047 
00048 
00049 
00050 /*****************************************************************************/
00051 /* Macro declarations                                                        */
00052 /*****************************************************************************/
00053 #define sl_min(a,b) (((a) < (b)) ? (a) : (b))
00054 #define MAX_NVMEM_CHUNK_SIZE  1460
00055 
00056 namespace mbed_cc3100 {
00057 
00058 cc3100_fs::cc3100_fs(cc3100_driver &driver)
00059     : _driver(driver)
00060 {
00061 
00062 }
00063 
00064 cc3100_fs::~cc3100_fs()
00065 {
00066 
00067 }
00068 
00069 #ifndef SL_TINY
00070 /*****************************************************************************/
00071 /* Internal functions                                                        */
00072 /*****************************************************************************/
00073 
00074 
00075 /*****************************************************************************/
00076 /* _sl_Strlen                                                                */
00077 /*****************************************************************************/
00078 uint16_t cc3100_fs::_sl_Strlen(const uint8_t *buffer)
00079 {
00080     uint16_t len = 0;
00081     if( buffer != NULL ) {
00082         while(*buffer++) len++;
00083     }
00084     return len;
00085 }
00086 
00087 /*****************************************************************************/
00088 /* _sl_GetCreateFsMode                                                       */
00089 /*****************************************************************************/
00090 uint32_t cc3100_fs::_sl_GetCreateFsMode(uint32_t maxSizeInBytes,uint32_t accessFlags)
00091 {
00092     uint32_t granIdx = 0;
00093     uint32_t granNum = 0;
00094     uint32_t granTable[_FS_MAX_MODE_SIZE_GRAN] = {256,1024,4096,16384,65536};
00095     for(granIdx= _FS_MODE_SIZE_GRAN_256B ; granIdx< _FS_MAX_MODE_SIZE_GRAN; granIdx++) {
00096         if( granTable[granIdx]*255 >= maxSizeInBytes )
00097             break;
00098     }
00099     granNum = maxSizeInBytes/granTable[granIdx];
00100     if( maxSizeInBytes % granTable[granIdx] != 0 )
00101         granNum++;
00102 
00103     return _FS_MODE(_FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST,  granIdx, granNum, accessFlags);
00104 }
00105 #endif
00106 
00107 /*****************************************************************************/
00108 /* API functions                                                        */
00109 /*****************************************************************************/
00110 
00111 /*****************************************************************************/
00112 /*  sl_FsOpen */
00113 /*****************************************************************************/
00114 typedef union {
00115     _FsOpenCommand_t        Cmd;
00116     _FsOpenResponse_t       Rsp;
00117 } _SlFsOpenMsg_u;
00118 
00119 #if _SL_INCLUDE_FUNC(sl_FsOpen)
00120 const _SlCmdCtrl_t _SlFsOpenCmdCtrl = {
00121     SL_OPCODE_NVMEM_FILEOPEN,
00122     sizeof(_FsOpenCommand_t),
00123     sizeof(_FsOpenResponse_t)
00124 };
00125 
00126 int32_t cc3100_fs::sl_FsOpen(const uint8_t *pFileName, const uint32_t AccessModeAndMaxSize, uint32_t *pToken,int32_t *pFileHandle)
00127 {
00128     _SlReturnVal_t        RetVal;
00129     _SlFsOpenMsg_u        Msg;
00130     _SlCmdExt_t           CmdExt;
00131 
00132     CmdExt.TxPayloadLen = (_sl_Strlen(pFileName)+4) & (~3); // add 4: 1 for NULL and the 3 for align
00133     CmdExt.RxPayloadLen = 0;
00134     CmdExt.pTxPayload = (uint8_t *)pFileName;
00135     CmdExt.pRxPayload = NULL;
00136 
00137     Msg.Cmd.Mode          =  AccessModeAndMaxSize;
00138 
00139     if(pToken != NULL) {
00140         Msg.Cmd.Token         = *pToken;
00141     } else {
00142         Msg.Cmd.Token         = 0;
00143     }
00144 
00145     RetVal = _driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsOpenCmdCtrl, &Msg, &CmdExt);
00146     *pFileHandle = Msg.Rsp.FileHandle;
00147     if (pToken != NULL) {
00148         *pToken =      Msg.Rsp.Token;
00149     }
00150 
00151     /* in case of an error, return the erros file handler as an error code */
00152     if( *pFileHandle < 0 ) {
00153         return *pFileHandle;
00154     }
00155     return (int32_t)RetVal;
00156 }
00157 #endif
00158 
00159 /*****************************************************************************/
00160 /* sl_FsClose */
00161 /*****************************************************************************/
00162 typedef union {
00163     _FsCloseCommand_t       Cmd;
00164     _BasicResponse_t        Rsp;
00165 } _SlFsCloseMsg_u;
00166 
00167 #if _SL_INCLUDE_FUNC(sl_FsClose)
00168 const _SlCmdCtrl_t _SlFsCloseCmdCtrl = {
00169     SL_OPCODE_NVMEM_FILECLOSE,
00170     sizeof(_FsCloseCommand_t),
00171     sizeof(_FsCloseResponse_t)
00172 };
00173 
00174 int16_t cc3100_fs::sl_FsClose(const int32_t FileHdl, const uint8_t*  pCeritificateFileName, const uint8_t*  pSignature ,const uint32_t SignatureLen)
00175 {
00176     _SlFsCloseMsg_u Msg = {0};
00177     _SlCmdExt_t         ExtCtrl;
00178 
00179     Msg.Cmd.FileHandle             = FileHdl;
00180     if( pCeritificateFileName != NULL ) {
00181         Msg.Cmd.CertificFileNameLength = (_sl_Strlen(pCeritificateFileName)+4) & (~3); /* add 4: 1 for NULL and the 3 for align */
00182     }
00183     Msg.Cmd.SignatureLen           = SignatureLen;
00184 
00185     ExtCtrl.TxPayloadLen = ((SignatureLen+3) & (~3)); /* align */
00186     ExtCtrl.pTxPayload   = (uint8_t*)pSignature;
00187     ExtCtrl.RxPayloadLen = (int16_t)Msg.Cmd.CertificFileNameLength;
00188     ExtCtrl.pRxPayload   = (uint8_t*)pCeritificateFileName; /* Add signature */
00189 
00190     if(ExtCtrl.pRxPayload != NULL &&  ExtCtrl.RxPayloadLen != 0) {
00191         ExtCtrl.RxPayloadLen = ExtCtrl.RxPayloadLen * (-1);
00192     }
00193 
00194     VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsCloseCmdCtrl, &Msg, &ExtCtrl));
00195 
00196     return (int16_t)((int16_t)Msg.Rsp.status);
00197 }
00198 #endif
00199 
00200 
00201 /*****************************************************************************/
00202 /* sl_FsRead */
00203 /*****************************************************************************/
00204 typedef union {
00205     _FsReadCommand_t        Cmd;
00206     _FsReadResponse_t       Rsp;
00207 } _SlFsReadMsg_u;
00208 
00209 #if _SL_INCLUDE_FUNC(sl_FsRead)
00210 const _SlCmdCtrl_t _SlFsReadCmdCtrl = {
00211     SL_OPCODE_NVMEM_FILEREADCOMMAND,
00212     sizeof(_FsReadCommand_t),
00213     sizeof(_FsReadResponse_t)
00214 };
00215 
00216 int32_t cc3100_fs::sl_FsRead(const int32_t FileHdl, uint32_t Offset, uint8_t*  pData, uint32_t Len)
00217 {
00218     _SlFsReadMsg_u      Msg;
00219     _SlCmdExt_t         ExtCtrl;
00220     uint16_t      ChunkLen;
00221     _SlReturnVal_t      RetVal =0;
00222     int32_t                RetCount = 0;
00223 
00224     ExtCtrl.TxPayloadLen = 0;
00225     ExtCtrl.pTxPayload   = NULL;
00226 
00227     ChunkLen = (uint16_t)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
00228     ExtCtrl.RxPayloadLen = ChunkLen;
00229     ExtCtrl.pRxPayload   = (uint8_t *)(pData);
00230     Msg.Cmd.Offset       = Offset;
00231     Msg.Cmd.Len          = ChunkLen;
00232     Msg.Cmd.FileHandle   = FileHdl;
00233     do {
00234         RetVal = _driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsReadCmdCtrl, &Msg, &ExtCtrl);
00235         if(SL_OS_RET_CODE_OK == RetVal) {
00236             if( Msg.Rsp.status < 0) {
00237                 if( RetCount > 0) {
00238                     return RetCount;
00239                 } else {
00240                     return Msg.Rsp.status;
00241                 }
00242             }
00243             RetCount += (int32_t)Msg.Rsp.status;
00244             Len -= ChunkLen;
00245             Offset += ChunkLen;
00246             Msg.Cmd.Offset      = Offset;
00247             ExtCtrl.pRxPayload   += ChunkLen;
00248             ChunkLen = (uint16_t)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
00249             ExtCtrl.RxPayloadLen  = ChunkLen;
00250             Msg.Cmd.Len           = ChunkLen;
00251             Msg.Cmd.FileHandle  = FileHdl;
00252         } else {
00253             return RetVal;
00254         }
00255     } while(ChunkLen > 0);
00256 
00257     return (int32_t)RetCount;
00258 }
00259 #endif
00260 
00261 /*****************************************************************************/
00262 /* sl_FsWrite */
00263 /*****************************************************************************/
00264 typedef union {
00265     _FsWriteCommand_t       Cmd;
00266     _FsWriteResponse_t      Rsp;
00267 } _SlFsWriteMsg_u;
00268 
00269 #if _SL_INCLUDE_FUNC(sl_FsWrite)
00270 const _SlCmdCtrl_t _SlFsWriteCmdCtrl = {
00271     SL_OPCODE_NVMEM_FILEWRITECOMMAND,
00272     sizeof(_FsWriteCommand_t),
00273     sizeof(_FsWriteResponse_t)
00274 };
00275 
00276 int32_t cc3100_fs::sl_FsWrite(const int32_t FileHdl, uint32_t Offset, uint8_t*  pData, uint32_t Len)
00277 {
00278     _SlFsWriteMsg_u     Msg;
00279     _SlCmdExt_t         ExtCtrl;
00280     uint16_t      ChunkLen;
00281     _SlReturnVal_t      RetVal;
00282     int32_t                RetCount = 0;
00283 
00284     ExtCtrl.RxPayloadLen = 0;
00285     ExtCtrl.pRxPayload   = NULL;
00286 
00287     ChunkLen = (uint16_t)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
00288     ExtCtrl.TxPayloadLen = ChunkLen;
00289     ExtCtrl.pTxPayload   = (uint8_t *)(pData);
00290     Msg.Cmd.Offset      = Offset;
00291     Msg.Cmd.Len          = ChunkLen;
00292     Msg.Cmd.FileHandle  = FileHdl;
00293 
00294     do {
00295 
00296         RetVal = _driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsWriteCmdCtrl, &Msg, &ExtCtrl);
00297         if(SL_OS_RET_CODE_OK == RetVal) {
00298             if( Msg.Rsp.status < 0) {
00299                 if( RetCount > 0) {
00300                     return RetCount;
00301                 } else {
00302                     return Msg.Rsp.status;
00303                 }
00304             }
00305 
00306             RetCount += (int32_t)Msg.Rsp.status;
00307             Len -= ChunkLen;
00308             Offset += ChunkLen;
00309             Msg.Cmd.Offset        = Offset;
00310             ExtCtrl.pTxPayload   += ChunkLen;
00311             ChunkLen = (uint16_t)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
00312             ExtCtrl.TxPayloadLen  = ChunkLen;
00313             Msg.Cmd.Len           = ChunkLen;
00314             Msg.Cmd.FileHandle  = FileHdl;
00315         } else {
00316             return RetVal;
00317         }
00318     } while(ChunkLen > 0);
00319 
00320     return (int32_t)RetCount;
00321 }
00322 #endif
00323 
00324 /*****************************************************************************/
00325 /* sl_FsGetInfo */
00326 /*****************************************************************************/
00327 typedef union {
00328     _FsGetInfoCommand_t     Cmd;
00329     _FsGetInfoResponse_t    Rsp;
00330 } _SlFsGetInfoMsg_u;
00331 
00332 #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
00333 const _SlCmdCtrl_t _SlFsGetInfoCmdCtrl = {
00334     SL_OPCODE_NVMEM_FILEGETINFOCOMMAND,
00335     sizeof(_FsGetInfoCommand_t),
00336     sizeof(_FsGetInfoResponse_t)
00337 };
00338 
00339 int16_t cc3100_fs::sl_FsGetInfo(const uint8_t *pFileName, const uint32_t Token,SlFsFileInfo_t* pFsFileInfo)
00340 {
00341     _SlFsGetInfoMsg_u    Msg;
00342     _SlCmdExt_t          CmdExt;
00343 
00344     CmdExt.TxPayloadLen = (_sl_Strlen(pFileName)+4) & (~3); /* add 4: 1 for NULL and the 3 for align  */
00345     CmdExt.RxPayloadLen = 0;
00346     CmdExt.pTxPayload   = (uint8_t *)pFileName;
00347     CmdExt.pRxPayload   = NULL;
00348     Msg.Cmd.Token       = Token;
00349 
00350     VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetInfoCmdCtrl, &Msg, &CmdExt));
00351 
00352     pFsFileInfo->flags        = Msg.Rsp.flags;
00353     pFsFileInfo->FileLen      = Msg.Rsp.FileLen;
00354     pFsFileInfo->AllocatedLen = Msg.Rsp.AllocatedLen;
00355     pFsFileInfo->Token[0]     = Msg.Rsp.Token[0];
00356     pFsFileInfo->Token[1]     = Msg.Rsp.Token[1];
00357     pFsFileInfo->Token[2]     = Msg.Rsp.Token[2];
00358     pFsFileInfo->Token[3]     = Msg.Rsp.Token[3];
00359     return  (int16_t)((int16_t)Msg.Rsp.Status);
00360 }
00361 #endif
00362 
00363 /*****************************************************************************/
00364 /* sl_FsDel */
00365 /*****************************************************************************/
00366 typedef union {
00367     _FsDeleteCommand_t          Cmd;
00368     _FsDeleteResponse_t         Rsp;
00369 } _SlFsDeleteMsg_u;
00370 
00371 #if _SL_INCLUDE_FUNC(sl_FsDel)
00372 const _SlCmdCtrl_t _SlFsDeleteCmdCtrl = {
00373     SL_OPCODE_NVMEM_FILEDELCOMMAND,
00374     sizeof(_FsDeleteCommand_t),
00375     sizeof(_FsDeleteResponse_t)
00376 };
00377 
00378 int16_t cc3100_fs::sl_FsDel(const uint8_t *pFileName, const uint32_t Token)
00379 {
00380     _SlFsDeleteMsg_u Msg;
00381     _SlCmdExt_t          CmdExt;
00382 
00383     CmdExt.TxPayloadLen = (_sl_Strlen(pFileName)+4) & (~3); /* add 4: 1 for NULL and the 3 for align */
00384     CmdExt.RxPayloadLen = 0;
00385     CmdExt.pTxPayload   = (uint8_t *)pFileName;
00386     CmdExt.pRxPayload   = NULL;
00387     Msg.Cmd.Token       = Token;
00388 
00389 
00390     VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsDeleteCmdCtrl, &Msg, &CmdExt));
00391 
00392     return  (int16_t)((int16_t)Msg.Rsp.status);
00393 }
00394 #endif
00395 
00396 }//namespace mbed_cc3100
00397 
00398