Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
3:24c5f0f50bf1
Parent:
2:e67f7c158087
Child:
4:a91682e19d6b
diff -r e67f7c158087 -r 24c5f0f50bf1 SnSDUtils.cpp
--- a/SnSDUtils.cpp	Tue Jul 24 02:07:23 2012 +0000
+++ b/SnSDUtils.cpp	Tue Jul 31 04:59:16 2012 +0000
@@ -64,16 +64,19 @@
     return seq;
 }
 
-FILE* SnSDUtils::OpenExistingFile(const char* name) {
+FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent) {
     FILE* f = 0;
     if (name!=NULL) {
-        f = OpenSDFile(name);
+        f = OpenSDFile(name, "rb");
+        if (setcurrent) {
+            fgCurFile = f;
+        }
     }
     return f;
 }
 
-FILE* SnSDUtils::OpenSDFile(const char* name) {
-    FILE* f = fopen(name, "wb");
+FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode) {
+    FILE* f = fopen(name, mode);
     //setvbuf(f, 0, _IONBF, 0); // no buffering
     return f;
 }
@@ -89,7 +92,7 @@
     //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,seq);
     fgCurFile = 0;
     if (fgCurFileName!=NULL) {
-        fgCurFile = OpenSDFile(fgCurFileName);
+        fgCurFile = OpenSDFile(fgCurFileName, "wb");
         if (fgCurFile!=NULL && ferror(fgCurFile)==0) {
             WriteFileHeader(fgCurFile, macadr, run, seq);
         }
@@ -144,7 +147,10 @@
 }
 
 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
-                                                  const bool doDelete) {
+                                                  const bool doDelete,
+                                                  const uint32_t timeout,
+                                                  char* const buf,
+                                                  const uint32_t bsize) {
     
     DIR* d;
     struct dirent* dent;
@@ -158,15 +164,31 @@
                 const bool isCurFile = 
                     (strcmp(dent->d_name, GetCurFileName())==0);
                 if (isCurFile) {
+                    // file must already be written out!
                     f = GetCurFile();
                 } else {
-                    f = OpenExistingFile(dent->d_name);
+                    f = OpenExistingFile(dent->d_name, false);
                 }
-                rs = comm->SendData(f);
+                // send the filename
+                rs = comm->SendFilename(dent->d_name, buf);
                 if (rs<SnCommWin::kAllFails) {
-                    break;
-                } else if (doDelete && (isCurFile==false)) {
-                    DeleteFile(f, dent->d_name);
+                    // send the data from the file
+                    rs = comm->SendData(f);
+                    if (rs<SnCommWin::kAllFails) {
+                        break;
+                    } else {
+                        // wait for handshake before next file
+                        rs = comm->WaitHandshake(timeout, buf, bsize);
+                        if ((rs==SnCommWin::kOkWithMsg) 
+                             && doDelete && (isCurFile==false)) {
+                            DeleteFile(f, dent->d_name);
+                        }
+                    }
+                }
+                if (isCurFile) {
+                    // move (back) to the end of the file
+                    // altho hopefully no writing will happen after this
+                    fseek(fgCurFile, 0, SEEK_END);
                 }
             }
         }