Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
36:87865913ae6f
Parent:
27:efc4d654b139
Child:
37:ff95e7070f26
--- a/SnCommWin.cpp	Thu Feb 07 23:06:36 2013 +0000
+++ b/SnCommWin.cpp	Thu May 23 22:26:39 2013 +0000
@@ -13,7 +13,7 @@
 #include <ctype.h>
 extern "C" void mbed_reset();
 
-//#define DEBUG
+#define DEBUG
 
 const char*   SnCommWin::kLocalDir = "/local";
 const char*   SnCommWin::kDelAllConfCodeStr = "fj2io32FIJ(#jd;;.O@++/]ewavk2[49ojv";
@@ -304,6 +304,7 @@
         // 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);
         char* fb = fnb;
         memcpy(fb, kLocalDir, ldlen); fb+=ldlen;        // /local
         memset(fb, '/', 1); fb+=1;                      //       /
@@ -331,6 +332,9 @@
             }
             const int got = ReceiveAll(buf, toget, timeout);
             if (lf!=NULL) {
+#ifdef DEBUG
+                printf("writing %d bytes to file\r\n", got);
+#endif
                 SnBitUtils::WriteTo(lf, buf, got);
             }
             b += got;
@@ -339,6 +343,9 @@
         if (lf!=NULL) {
             // calculate the crc from what's actually in the file
             fclose(lf); // to flush it
+#ifdef DEBUG
+            printf("fopen: %s\r\n",fnb);
+#endif
             lf  = fopen(fnb,"rb");
             fseek(lf, 0, SEEK_END);
             int32_t fend = ftell(lf);
@@ -353,7 +360,14 @@
                 }
             }
             fclose(lf);
+#ifdef DEBUG
+            printf("closed file. crc = %u\r\n", crc);
+#endif
         }
+#ifdef DEBUG
+        printf("mlen=%u, b=%d, eq=%s\r\n",mlen,b,
+            (mlen==b)?"true":"false");
+#endif
         if ( mlen!=b ) {
             if (res > kUnexpectedRec) {
                 res = kUnexpectedRec;
@@ -363,7 +377,14 @@
             // get the checksum
             res = GetHeader(timeout, buf, bsize, mcode, mlen);
             if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileChksCode) ) {
+#ifdef DEBUG
+                printf("received chksum=%u, crc=%u, eq=%s\r\n",
+                    mlen,crc,(crc==mlen)?"true":"false");
+#endif
                 if (crc != mlen) {
+#ifdef DEBUG
+                    printf("checksums differ! unexpected.\r\n");
+#endif
                     res = kUnexpectedRec; // important!
                 }
             }
@@ -381,6 +402,9 @@
         if (lf!=NULL) {
             if ( res<kOkWithMsg ) {
                 // timeout, bad checksum, something else bad?
+#ifdef DEBUG
+                printf("removing %s\r\n",fnb);
+#endif
                 remove(fnb); // delete the file
             } else {
                 // if we got a new program, remove the old ones.
@@ -388,11 +412,17 @@
                 //
                 // first just count how many we would save
                 const uint16_t nSavedBins = LoopLocalDirBinFiles(false, fname);
+#ifdef DEBUG
+                printf("nSavedBins=%hu",nSavedBins);
+#endif
                 if (nSavedBins==1) {
                     // ok. new program will be the only one. remove the others
                     // hope the new program works!
                     LoopLocalDirBinFiles(true, fname);
-                    
+
+#ifdef DEBUG
+                    printf("rebooting\r\n");
+#endif                    
                     // goodbye cruel world, it's over. walk on by...
                     mbed_reset();
                 }