Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
122:c1b5023eac69
Parent:
116:8099b754fbb4
--- a/SnCommWin.cpp	Thu Nov 30 06:26:18 2017 +0000
+++ b/SnCommWin.cpp	Fri Dec 01 16:29:10 2017 +0000
@@ -447,10 +447,46 @@
     return SnCommWin::kUnexpectedRec;
 }
 
+bool SnCommWin::BuildLocalFileName(std::string fname,
+                                   const char* const dir,
+                                   std::string& lfname) {
+    // make the file name ALLCAPS, since LocalFileSystem will do it anyway
+    SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end());
+    // now ensure the file name is 8.3 only -- for LocalFileSystem
+    const size_t ldlen = strlen(dir);
+    // 12 = 8.3 filename format, 1 for / and 1 for \0
+    const uint32_t fbs = ldlen+1+12+1;
+    char fnb[fbs];
+    memset(fnb, 0, sizeof(char)*fbs);
+    
+    Watchdog::kick(); // don't reset
+    
+    // find the extension
+    const size_t xdot = fname.rfind('.');
+    if (xdot!=string::npos && (fname.size()==(4+xdot))) {
+        const size_t fnpl = (xdot>8) ? 8 : xdot;
+        char* fb = fnb;
+        memcpy(fb, dir, ldlen); fb+=ldlen;        // /local
+        *fb = '/'; ++fb;                                //       /
+        strncpy(fb, fname.c_str(), fnpl); fb+=fnpl;     //        FILENAME
+        *fb = '.'; ++fb;                                //                .
+        strncpy(fb, fname.c_str()+xdot+1, 3);           //                 EXT
+        // success
+        lfname = fnb;
+        return true;
+    }
+ #ifdef DEBUG
+    printf("filname mangled. (%s) size=%u, xdot=%u",
+        fname.c_str(), fname.size(), xdot);
+#endif            
+   return false;  // fail
+}
+
 SnCommWin::ECommWinResult SnCommWin::GetLocalFile(std::string fname,
                                                   char* const buf,
                                                   const uint32_t bsize,
-                                                  const uint32_t timeout) {
+                                                  const uint32_t timeout,
+                                                  std::string& lfname) {
     // get a file and save it locally with the specified name
     
     Watchdog::kick(); // don't reset
@@ -468,34 +504,13 @@
         printf("Got mbed file header. File length = %u\r\n",mlen);
 #endif
         // got the header.. make the file..
-        // make the file name ALLCAPS, since LocalFileSystem will do it anyway
-        SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end());
-        // now ensure the file name is 8.3 only -- for LocalFileSystem
-        const size_t ldlen = strlen(kLocalDir);
-        // 12 = 8.3 filename format, 1 for / and 1 for \0
-        const uint32_t fbs = ldlen+1+12+1;
-        char fnb[fbs];
-        memset(fnb, 0, sizeof(char)*fbs);
-        
-        Watchdog::kick(); // don't reset
-        
-        // find the extension
-        const size_t xdot = fname.rfind('.');
-        if (xdot!=string::npos && (fname.size()==(4+xdot))) {
-            const size_t fnpl = (xdot>8) ? 8 : xdot;
-            char* fb = fnb;
-            memcpy(fb, kLocalDir, ldlen); fb+=ldlen;        // /local
-            *fb = '/'; ++fb;                                //       /
-            strncpy(fb, fname.c_str(), fnpl); fb+=fnpl;     //        FILENAME
-            *fb = '.'; ++fb;                                //                .
-            strncpy(fb, fname.c_str()+xdot+1, 3);           //                 EXT
-            
+        if ( BuildLocalFileName(fname, kLocalDir, lfname) ) {
             Watchdog::kick(); // don't reset
             
             // all that just for the file name!
-            FILE* lf  = fopen(fnb,"wb");
+            FILE* lf  = fopen(lfname.c_str(),"wb");
 #ifdef DEBUG
-            printf("tried to open file [%s]. lf=%p\r\n",fnb,(void*)lf);
+            printf("tried to open file [%s]. lf=%p\r\n",lfname.c_str(),(void*)lf);
 #endif
             // get all the data and dump it into the file
             int b = 0, toget = 0;
@@ -530,9 +545,9 @@
                 // calculate the crc from what's actually in the file
                 fclose(lf); // to flush it
 #ifdef DEBUG
-                printf("fopen: %s\r\n",fnb);
+                printf("fopen: %s\r\n",lfname.c_str());
 #endif
-                lf  = fopen(fnb,"rb");
+                lf  = fopen(lfname.c_str(),"rb");
                 if (lf!=0) {
                     fseek(lf, 0, SEEK_END);
                     int32_t fend = ftell(lf);
@@ -588,9 +603,9 @@
                 if ( res<kOkWithMsg ) {
                     // timeout, bad checksum, something else bad?
 #ifdef DEBUG
-                   printf("removing %s\r\n",fnb);
+                    printf("removing %s\r\n",lfname.c_str());
 #endif
-                    remove(fnb); // delete the file
+                    remove(lfname.c_str()); // delete the file
                 } else {
                     // if we got a new program, remove the old ones.
                     // (schedule myself for immediate de-resolution)
@@ -616,10 +631,6 @@
         } else {
             // filename mangled. either doesn't contain a '.'
             // or the extension is longer than 3 letters
-#ifdef DEBUG
-            printf("filname mangled. (%s) size=%u, xdot=%u",
-                fname.c_str(), fname.size(), xdot);
-#endif            
             res = SnCommWin::kUnexpectedRec;
         }
     } // if header ok
@@ -719,12 +730,27 @@
             } else if (mcode==SnHeaderFrame::kMbedFilenameCode) {
                 res = GetFilename(timeOut, confBuf, mlen);
                 if (res>kAllFails) {
-                    std::string fnameb(confBuf, mlen); // the filename
-                    std::string fname(fnameb.c_str()); // mlen may include the \0 which would be counted in size()
+                    std::string fname(confBuf, mlen); // the filename
+                    fname = fname.c_str(); // mlen may include the \0 which would be counted in size()
 #ifdef DEBUG
                     printf("got filename = [%s] (%u)\r\n", fname.c_str(), fname.size());
 #endif  
-                    res = GetLocalFile(fname, confBuf, bsize, timeOut);
+                    std::string lfname;
+                    res = GetLocalFile(fname, confBuf, bsize, timeOut, lfname);
+                    
+#ifdef LOAD_DEFAULT_CONFIG_FROM_SD
+                    const size_t flen = strlen(fname.c_str());
+                    const char*  dext = fname.c_str() + flen - 4;
+                    if ( strncmp(dext,".BIN",4)!=0 || strncmp(dext,".bin",4)!=0 ) {
+                        // does NOT end with .BIN
+                        // copy it to the SD card
+                        std::string sdfname;
+                        if ( BuildLocalFileName(fname, SnSDUtils::kSDdir, sdfname) ) {
+                            SnSDUtils::CopyFileToSD(lfname.c_str(), sdfname.c_str(),
+                                                    confBuf, bsize);
+                        }
+                    }
+#endif
                 }
             } else if (mcode!=SnHeaderFrame::kConfigCode) {
                 res = SnCommWin::kUnexpectedRec;