Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
1:e392595b4b76
Parent:
0:664899e0b988
Child:
2:e67f7c158087
--- a/SnSDUtils.cpp	Sat Jun 30 02:03:51 2012 +0000
+++ b/SnSDUtils.cpp	Fri Jul 20 19:04:02 2012 +0000
@@ -8,9 +8,10 @@
 #include "SnEventFrame.h"
 
 
-const char*   SnSDUtils::kSDsubDir  = "/sd";
-char          SnSDUtils::fgCurFileName[kFNBufSize];
-const uint8_t SnSDUtils::kIOvers    = 1;
+const char* const SnSDUtils::kSDsubDir  = "/sd";
+char              SnSDUtils::fgCurFileName[kFNBufSize];
+FILE*             SnSDUtils::fgCurFile  = 0;
+const uint8_t     SnSDUtils::kIOvers    = 1;
 
 const char* SnSDUtils::GetOutFileName(const uint64_t macadr,
                                       const uint32_t run,
@@ -66,11 +67,17 @@
 FILE* SnSDUtils::OpenExistingFile(const char* name) {
     FILE* f = 0;
     if (name!=NULL) {
-        f = fopen(name, "wb");
+        f = OpenSDFile(name);
     }
     return f;
 }
 
+FILE* SnSDUtils::OpenSDFile(const char* name) {
+    FILE* f = fopen(name, "wb");
+    //setvbuf(f, 0, _IONBF, 0); // no buffering
+    return f;
+}
+
 FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr,
                                    const uint32_t run) {
     // opens a new file in the specified directory and writes this
@@ -80,17 +87,18 @@
     memset(fgCurFileName, 0, sizeof(char)*kFNBufSize);
     strcpy(fgCurFileName,GetOutFileName(macadr, run, seq));
     //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,seq);
-    FILE* f = 0;
+    fgCurFile = 0;
     if (fgCurFileName!=NULL) {
-        f = fopen(fgCurFileName, "wb");
+        fgCurFile = OpenSDFile(fgCurFileName);
     }
-    return f;
+    return fgCurFile;
 }
 
 bool SnSDUtils::WriteFileHeader(FILE* f, const uint64_t macadr) {
     // MUST INCREMENT kIOvers if these writes are altered
     fwrite(&kIOvers, sizeof(uint8_t), 1, f);
     fwrite(&macadr, sizeof(uint64_t), 1, f);
+    fflush(f);
     return ferror(f);
 }
 
@@ -131,11 +139,17 @@
         FILE* f;
         while ( (dent = readdir(d))!=NULL ) {
             if (strncmp(dent->d_name, "SnEvts", 6)==0) {
-                f = SnSDUtils::OpenExistingFile(dent->d_name);
+                const bool isCurFile = 
+                    (strcmp(dent->d_name, GetCurFileName())==0);
+                if (isCurFile) {
+                    f = GetCurFile();
+                } else {
+                    f = OpenExistingFile(dent->d_name);
+                }
                 rs = comm->SendData(f);
                 if (rs<SnCommWin::kAllFails) {
                     break;
-                } else if (doDelete) {
+                } else if (doDelete && (isCurFile==false)) {
                     DeleteFile(f, dent->d_name);
                 }
             }