Update revision to use TI's mqtt and Freertos.

Dependencies:   mbed client server

Fork of cc3100_Test_mqtt_CM3 by David Fletcher

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