xrocusOS_ADXL355 version

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDDataStore.cpp Source File

SDDataStore.cpp

00001 #include "SDDataStore.h"
00002 #include "global.h"
00003 
00004 /* constructor, pSD, TimeManager */
00005 SDDataStore::SDDataStore(TimeManager *pSetTM)
00006 {
00007     //pS = new SDFileSystem(PC_12, PC_11, PC_10, D4, "sd"); // MOSI, MISO, SCK, CS;
00008     pS = new SDFileSystem(PC_12, PC_11, PC_10, PD_2, "sd"); // MOSI, MISO, SCK, CS;
00009     pT = pSetTM;
00010     fpCurrent = NULL;
00011     fnameCurrent[0] = '\0';
00012 }
00013 
00014 /* destructor */
00015 SDDataStore::~SDDataStore()
00016 {
00017     closeFile();
00018 }
00019     
00020 /* for Sensing and Logging generate (prefix)TimeStampFile.(ext)*/
00021 bool SDDataStore::startFileWithTimeStamp(char *prefix, char *ext)
00022 {
00023     char timeStamp[TimeManager::TimeStampLength + 1] = {0};
00024     char fname[MaxPathLength] = {0};
00025     pT->getTimeStamp(timeStamp);
00026     sprintf(fname, "%s%s.%s", prefix, timeStamp, ext);
00027     if (this->checkFileExist(fname) == true) {
00028         this->removeFile(fname);
00029     }
00030     if (this->openFile(fname) != true) {
00031         return false;
00032     }
00033     return true;
00034 }
00035     
00036 /* checkFileExist, false=NOT-EXIST, true=EXIST */
00037 bool SDDataStore::checkFileExist(char *fname)
00038 {
00039     FILE *fp;
00040     char filePath[MaxPathLength];
00041     sprintf(filePath, "/sd/%s", fname);
00042     pathTermination(filePath);        
00043     fp = fopen(filePath, "r");
00044     if (fp == NULL) {
00045         return false;
00046     }
00047     fclose(fp);
00048     return true;
00049 }
00050     
00051 /* removeFile, false=NOT-EXIST, true=EXIST */
00052 void SDDataStore::removeFile(char *fname)
00053 {
00054     char filePath[MaxPathLength];
00055     sprintf(filePath, "/sd/%s", fname);
00056     pathTermination(filePath);
00057     remove(filePath);
00058 }
00059     
00060 /* open file at WriteMode FALSE=ERR, TRUE=OK */
00061 bool SDDataStore::openFile(char *fname)
00062 {
00063     FILE *fp;
00064     char filePath[MaxPathLength];
00065     sprintf(filePath, "/sd/%s", fname);
00066     pathTermination(filePath);
00067     
00068     fp = fopen(filePath, "a+");
00069     if (fp == NULL) {
00070         return false;
00071     }
00072     fpCurrent = fp;
00073     dbgprintf("SDDataStore::openFile(%p) as %s\n", fpCurrent, filePath);
00074     sprintf(fnameCurrent, "%s", fname);
00075     return true;
00076 }
00077 
00078 /* get FileName of open file */
00079 char *SDDataStore::getFileName(void)
00080 {
00081     return fnameCurrent;
00082 }
00083 
00084 /* get current file pointer  */
00085 FILE *SDDataStore::getFilePointer(void)
00086 {
00087     return fpCurrent;
00088 }
00089     
00090 /* close file with this Class  */
00091 void SDDataStore::syncFile(void)
00092 {
00093     FILE *fp;
00094     char filePath[MaxPathLength];
00095     sprintf(filePath, "/sd/%s", getFileName());
00096     
00097     dbgprintf("SDDataStore::syncFile(%p) as %s\n", fpCurrent, filePath);
00098     if (fpCurrent != NULL) {
00099         /* fflush is not working on SDFileSystem :p */
00100         fclose(fpCurrent);
00101         fp = fopen(filePath, "a+");
00102         if (fp == NULL) {
00103             dbgprintf("SDDataStore::syncFile(CRITICAL) in fopen\n");
00104             return;
00105         }
00106         fpCurrent = fp;
00107     }
00108 }
00109 
00110 /* close file with this Class  */
00111 void SDDataStore::closeFile(void)
00112 {
00113     dbgprintf("SDDataStore::closeFile(%p)\n", fpCurrent);
00114     if (fpCurrent != NULL) {
00115         fclose(fpCurrent);
00116         fpCurrent = NULL;
00117         fnameCurrent[0] = '\0';
00118     }
00119 }
00120     
00121 void SDDataStore::pathTermination(char *pathArray)
00122 {
00123     pathArray[MaxPathLength-1] = '\0';
00124 }