Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
40:1324da35afd4
Parent:
39:2f17131d22a5
Child:
47:fbe956b10a91
--- a/SnSDUtils.cpp	Tue Jul 30 02:03:21 2013 +0000
+++ b/SnSDUtils.cpp	Sat Oct 05 04:45:22 2013 +0000
@@ -9,6 +9,7 @@
 #include "SnConfigFrame.h"
 #include "SnEventFrame.h"
 #include "SnHeartbeatFrame.h"
+#include "SnClockSetFrame.h"
 
 #include "Watchdog.h"
 
@@ -21,21 +22,35 @@
 char              SnSDUtils::fgCurFileName[kFNBufSize]={0};
 FILE*             SnSDUtils::fgCurFile  = 0;
 uint16_t          SnSDUtils::fgCurSeq   = 0;
-const uint8_t     SnSDUtils::kIOvers    = 3;
+bool              SnSDUtils::fgNeedToInit = true;
+const uint8_t     SnSDUtils::kIOvers    = 4;
 const uint32_t    SnSDUtils::kMaxSizeOfFileHdr = 
     sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t)
     +(sizeof(uint8_t)+(2u*sizeof(uint16_t))); // power frame v1
 
+SnSDUtils::InitSDFcn SnSDUtils::fgDoInit = 0;
+
 static const uint16_t __kMaxUShort = ~0;
 
+void SnSDUtils::InitSDCard(const bool force) {
+    if ((fgNeedToInit || force) && (fgDoInit!=0)) {
+        (*fgDoInit)();
+        fgNeedToInit = false;
+    }
+}
+
 const char* SnSDUtils::GetSubDirFor(const uint32_t run, const uint16_t seq,
                                     uint32_t& slen, const bool useSeq) {
-    // returns a STATIC string!
+    // returns a STATIC string! (so make a copy of it before use)
     // sets slen to the length of this string (same as strlen)
-    static char* tmpsd = new char[strlen(kSDsubDir)+25];
-    slen = sprintf(tmpsd, "%s/r%05ld", kSDsubDir, run);
+    static const uint16_t tmplen = strlen(kSDsubDir)+50; 
+    static char* tmpsd = new char[tmplen];
+    slen = snprintf(tmpsd, tmplen, "%s/r%05ld", kSDsubDir, run);
     if (useSeq) {
-        slen += sprintf(tmpsd+slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ);
+        slen += snprintf(tmpsd+slen, tmplen-slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ);
+    }
+    if (slen > tmplen) {
+        slen = tmplen;
     }
     return tmpsd;
 }
@@ -67,11 +82,6 @@
         return NULL;
     }
 }
-/*
-uint16_t SnSDUtils::GetCurSeqNum() {
-    return GetSeqNumFromFileName(fgCurFileName);
-}
-*/
 
 bool SnSDUtils::GetRunSeqFromFilename(const char* fn,
                                       uint32_t& run,
@@ -105,6 +115,8 @@
 #ifdef DEBUG
     printf("open dir %s\r\n",dirname);
 #endif
+    InitSDCard();
+
     DIR* rd( opendir(dirname) );
     if (rd==NULL) {
         // try making the directory
@@ -126,6 +138,7 @@
 uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr,
                               const uint32_t run) {
     // count the files having expected filename format
+    InitSDCard();
     
     // get the run dir
     uint32_t rdlen(0);
@@ -165,7 +178,7 @@
 #endif    
     closedir(rd);
 #ifdef DEBUG
-    printf("Found max seq dir num %hu for run %u\r\n",run,maxs);
+    printf("Found max seq dir num %hu for run %u\r\n",maxs,run);
 #endif
     // open up the seq dir
     rdnms = GetSubDirFor(run, maxs, rdlen, true);
@@ -217,6 +230,7 @@
 
 FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent,
                                   const bool redoDir) {
+    InitSDCard();
     FILE* f = 0;
     //if ((name!=NULL) && ((*name)!=0) ) { // simple check if filename not set
     if (name!=NULL) { // simple check if filename not set
@@ -279,6 +293,8 @@
 #ifdef DEBUG
     printf("OpenSDFile: Trying to open %s.\r\n",name);
 #endif
+    InitSDCard();
+
     std::string ffn;
     FILE* f = 0;
     bool ok = true;
@@ -332,15 +348,23 @@
 }
 
 FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr,
-                                   const uint32_t run) {
+                                   const uint32_t run,
+                                   const uint16_t minseq) {
     // opens a new file in the specified directory and writes this
     // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes)
     //
 #ifdef DEBUG
-    printf("getting seq num\r\n");
+    printf("getting seq num for run %u, minseq %hu\r\n",
+        run, minseq);
 #endif
+    InitSDCard();
+
     fgCurSeq = GetSeqNum(macadr, run);
+    if (fgCurSeq<minseq) {
+        fgCurSeq=minseq;
+    }
 #ifdef DEBUG
+    printf("fgCurSeq=%hu\r\n",fgCurSeq);
     printf("getting output file name\r\n");
 #endif
     memset(fgCurFileName, 0, sizeof(char)*kFNBufSize);
@@ -369,6 +393,8 @@
 }
 
 void SnSDUtils::PrintFilesInDirs(const char* dirname) {
+    InitSDCard();
+
     DIR* d;
     struct dirent* dent;
     Watchdog::kick(); // don't reset
@@ -390,9 +416,31 @@
 
 }
 
+float SnSDUtils::GetFreeBytes() {
+    InitSDCard();
+
+    FATFS* fs;
+    DWORD fre_clust;
+    f_getfree("0:",&fre_clust,&fs);
+    const float frs = static_cast<float>(fs->csize)
+        *static_cast<float>(fs->free_clust)
+#if _MAX_SS != 512
+        *(fs->ssize);
+#else
+        *512;
+#endif
+#ifdef DEBUG
+    printf("free space = %g b (%g GB, %g MB, %g KB)\r\n",
+        frs, frs/1073741824.0, frs/1048576.0, frs/1024.0);
+#endif
+    return frs;
+}
+
 void SnSDUtils::GetDirProps(const char* dirname,
                             uint32_t& nfiles,
                             float& totbytes) {
+    InitSDCard();
+
     nfiles = 0;
     totbytes = 0;
     struct dirent* dent;
@@ -427,12 +475,35 @@
 bool SnSDUtils::WriteHeartbeatTo(FILE* file,
                                  const uint32_t time,
                                  const uint32_t num) {
-    const bool r1 =
-        SnHeaderFrame::WriteTo(file, SnHeaderFrame::kHeartbeatCode,
-                     SnHeartbeatFrame::SizeOf(SnHeartbeatFrame::kIOVers));
-    const bool r2 = 
-        SnHeartbeatFrame::WriteTo(file, time, num);
-    return (r1 && r2);
+    if (file!=0) {
+        InitSDCard();
+    
+        const bool r1 =
+            SnHeaderFrame::WriteTo(file, SnHeaderFrame::kHeartbeatCode,
+                         SnHeartbeatFrame::SizeOf(SnHeartbeatFrame::kIOVers));
+        const bool r2 = 
+            SnHeartbeatFrame::WriteTo(file, time, num);
+        return (r1 && r2);
+    } else {
+        return false;
+    }
+}
+
+bool SnSDUtils::WriteTrigWaitWinTime(FILE* file,
+                                     SnClockSetFrame& clkset,
+                                     const bool isStart) {
+    if (file!=0) {
+        InitSDCard();
+    
+        bool ok = SnHeaderFrame::WriteTo(file,
+                (isStart) ? (SnHeaderFrame::kFileTrgStrtCode)
+                          : (SnHeaderFrame::kFileTrgStopCode),
+                clkset.SizeOf());
+        ok &= (SnCommWin::kOkMsgSent == clkset.WriteTo(file));
+        return ok;
+    } else {
+        return false;
+    }
 }
 
 bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf,
@@ -440,28 +511,42 @@
                              const SnConfigFrame& conf) {
     // write event to SD card
         
-    uint8_t sLoseLSB=0, sLoseMSB=0;
-    uint16_t sWvBase=0;
-    conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
-    SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode, 
-                           evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB));
-    const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
-    fflush(efile);
-    return ret;
+    if (efile!=0) {
+        InitSDCard();
+        
+        uint8_t sLoseLSB=0, sLoseMSB=0;
+        uint16_t sWvBase=0;
+        conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
+        SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode, 
+                               evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB));
+        const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
+        fflush(efile);
+        return ret;
+    } else {
+        return false;
+    }
 }
 
 bool SnSDUtils::WriteConfig(FILE* efile,
                             const SnConfigFrame& conf) {
-    SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode, 
+    if (efile!=0) {
+        InitSDCard();
+
+        SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode, 
                            conf.SizeOf(SnConfigFrame::kIOVers));
-    conf.WriteTo(efile);
-    return true;
+        conf.WriteTo(efile);
+        return true;
+    } else {
+        return false;
+    }
 }
 
 void SnSDUtils::DeleteFile(FILE*& f, const char* fname) {
 #ifdef DEBUG
     printf("try to delete %s at %p\r\n",fname,f);
 #endif
+    InitSDCard();
+
     if (f!=0) {
         fclose(f);
         f=0;
@@ -488,6 +573,8 @@
 #ifdef DEBUG
     printf("deleting ALL files in %s\r\n",dirname);
 #endif
+    InitSDCard();
+
     DIR* d;
     struct dirent* dent;
     if ( (d = opendir( dirname ))!=NULL ) {
@@ -523,6 +610,8 @@
 #ifdef DEBUG
     printf("DeleteDirIfEmpty(%s)\r\n",dirname);
 #endif
+    InitSDCard();
+
     DIR* d;
     struct dirent* dent;
     if ( (d = opendir(dirname))!=NULL ) {
@@ -547,6 +636,23 @@
     }
 }
 
+SnCommWin::ECommWinResult SnSDUtils::SendAllOfRun(SnCommWin* comm,
+                                                  const uint32_t timeout,
+                                                  char* const buf,
+                                                  const uint32_t bsize,
+                                                  const SnConfigFrame& curConf,
+                                                  SnEventFrame& evt,
+                                                  SnPowerFrame& pow,
+                                                  const uint32_t runnum) {
+    // send all files in a run
+
+    // get the run dir
+    uint32_t rdlen(0);
+    std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) );
+    return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow,
+                        rdnms.c_str());
+}
+
 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
                                                   const uint32_t timeout,
                                                   char* const buf,
@@ -554,8 +660,10 @@
                                                   const SnConfigFrame& curConf,
                                                   SnEventFrame& evt,
                                                   SnPowerFrame& pow,
-                                                  const uint32_t handshakeTimeout,
                                                   const char* dirname) {
+    // send all files in the specified directory
+    
+    InitSDCard();
 
     SnCommWin::ECommWinResult rs  = SnCommWin::kOkMsgSent;
 
@@ -574,7 +682,7 @@
                 // send all the files in this new subdir
                 res = SendAllFiles(comm, timeout, buf, bsize, 
                              curConf, evt, pow,
-                             handshakeTimeout, dnm.c_str());
+                             dnm.c_str());
             } else if (strncmp(dent->d_name, "SnEvts", 6)==0) {
                 // a data file (send it)
                 const bool isCurFile = 
@@ -594,21 +702,23 @@
                     f, GetCurFile(), dent->d_name);
 #endif
                 uint8_t hndres = SnHeaderFrame::kHnShFailNonCode;
-                res = comm->SendData(f, dent->d_name,
-                                     curConf, evt, pow, buf, bsize,
-                                     0, timeout, handshakeTimeout,
-                                     &hndres);
-                
+                if (f!=0) {
+                    res = comm->SendDataFromFile(f, dent->d_name,
+                                         curConf, evt, pow, buf, bsize,
+                                         0, timeout,
+                                         &hndres);
+
+                    if (isCurFile) {
+                        // move (back) to the end of the file
+                        // altho hopefully no writing will happen after this
+                        fseek(fgCurFile, 0, SEEK_END);
+                    }
+                }                
 #ifdef DEBUG
                 printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n",
                     (int)isCurFile, (int)res, (int)(curConf.IsDeletingFiles()),
                     hndres);
 #endif
-                if (isCurFile) {
-                    // move (back) to the end of the file
-                    // altho hopefully no writing will happen after this
-                    fseek(fgCurFile, 0, SEEK_END);
-                }
                 
             }
             if ((res<rs) || (res==SnCommWin::kOkStopComm)) {