Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
40:1324da35afd4
Parent:
39:2f17131d22a5
Child:
56:0bba0ef15697
--- a/SnConfigFrame.h	Tue Jul 30 02:03:21 2013 +0000
+++ b/SnConfigFrame.h	Sat Oct 05 04:45:22 2013 +0000
@@ -23,17 +23,19 @@
 
     static const uint32_t   kMaxSizeOfV1 = 
             + (9u*sizeof(uint32_t)) + (6u*sizeof(uint16_t))
-            + (10u*sizeof(uint8_t)) + (3u*kNplas*sizeof(uint16_t))
+            + (10u*sizeof(uint8_t)) + (3u*kNplasV1*sizeof(uint16_t))
             + (kTotDacs*sizeof(uint16_t))
             + (kConfLblLen*sizeof(uint8_t));
     static const uint32_t   kMaxSizeOfV2 =
             kMaxSizeOfV1 + sizeof(uint32_t) + sizeof(uint8_t);
     static const uint32_t   kMaxSizeOfV3 =
             kMaxSizeOfV2 + (2u*sizeof(uint16_t)) + (4u*kIPLen*sizeof(char));
-    static const uint32_t   kMaxSizeOfV4 = kMaxSizeOfV3 + (sizeof(float)-sizeof(uint16_t));
+    static const uint32_t   kMaxSizeOfV4 = kMaxSizeOfV3 + (sizeof(float)-sizeof(uint16_t)); // forced trig per to float
     static const uint32_t   kMaxSizeOfV5 = kMaxSizeOfV4;
-    static const uint32_t   kMaxSizeOfV6 = kMaxSizeOfV5 + sizeof(int16_t);
-    static const uint32_t   kMaxSizeOf = kMaxSizeOfV6;
+    static const uint32_t   kMaxSizeOfV6 = kMaxSizeOfV5 + sizeof(uint16_t); // To/From Low Power
+    static const uint32_t   kMaxSizeOfV7 = kMaxSizeOfV6 + (2u*sizeof(uint8_t)); // add ConnTOmins, ListenTOmins
+    static const uint32_t   kMaxSizeOfV8 = kMaxSizeOfV7 - sizeof(uint32_t) + sizeof(uint16_t); // FirstEvt -> FirstSeq
+    static const uint32_t   kMaxSizeOf = kMaxSizeOfV8;
     
     enum EDatPackBit {
         kSDcard = BIT(0),
@@ -48,6 +50,7 @@
         kTimeout        =  BIT(2), // if bit=0 => ignore timeout
         kDelete         =  BIT(3), // if bit=0 => do not delete sent files
         kForceSBDdata   =  BIT(4), // if bit=0 => do not send data over SBD
+        kHnShBefSendDat =  BIT(5), // if bit=0 => do not wait for a handshake after GetConfig before calling SendData
         kUseBits        = static_cast<int16_t>(-BIT(14)) // useful to initialize fCommSendData as a bit word
     };
     
@@ -81,7 +84,7 @@
     char        fLabel[kConfLblLen];       // configuration label
     uint32_t    fConfTime;                 // cpu config time
     uint32_t    fRun;                      // run number
-    uint32_t    fFirstEvt;                 // starting event number
+    uint16_t    fFirstSeq;                 // starting sequence number
     uint32_t    fEvtsPerSeq;               // number of events per file
     uint8_t     fRunMode;                  // mode of running (see ERunMode)
     uint8_t     fStreamHiLoPlas;           // (1byte bool) if true, add the separated hi/lo thresh PLA patterns to the i/o
@@ -102,9 +105,8 @@
     uint16_t    fEvtThrtlPeriodMs;         // throttle period to write events (ms)
     // power
     uint8_t     fPowerMode;                // power mode bit word: see EPowerModeBit
-    //int16_t     fBatVoltLowPwr;            // battery level at which to switch to low power (not used?) deprecated for hysteresis
-    int16_t     fBatVoltToLowPwr;          // battery level at which to switch to low power (not used?)
-    int16_t     fBatVoltFromLowPwr;        // battery level at which to switch back from low power (not used?)
+    uint16_t    fBatVoltToLowPwr;          // battery level at which to switch to low power (not used?)
+    uint16_t    fBatVoltFromLowPwr;        // battery level at which to switch back from low power (not used?)
     uint16_t    fVoltCheckPeriod;          // how often to check the voltages (s)
     // communication
     uint32_t    fCommWinPeriod;            // seconds between communication window startup (0=always on)
@@ -112,6 +114,8 @@
     int16_t     fCommSendData;             // data to send during comm win (=0: none, >0=send up to x events from last file until comm win closes, <0=see ESendDataBit)
     uint32_t    fCommWinPrdLowPwr;         // low power communication window period (seconds) (range enforced)
     uint32_t    fCommWinDurLowPwr;         // low power communication window duration (seconds) (range enforced)
+    uint8_t     fCommWinConnectTOMin;      // comm win connection timeout (minutes) (range enforced)
+    uint8_t     fCommWinListenTOMin;       // comm win listening timeout (minutes) (range enforced)
     char        fRemoteServer[kIPLen];     // IP address of remote server (for afar)
     uint16_t    fRemotePort;               // port number of remote server (for afar)
     char        fMbedIP[kIPLen];           // IP address of this mbed
@@ -150,7 +154,11 @@
         } else if (rv==5) {
             maxsize = kMaxSizeOfV5;   
         } else if (rv==6) {
-            maxsize = kMaxSizeOfV6;   
+            maxsize = kMaxSizeOfV6;
+        } else if (rv==7) {
+            maxsize = kMaxSizeOfV7;
+        } else if (rv==8) {
+            maxsize = kMaxSizeOfV8;
         }
         const int32_t lbldiff = kConfLblLen - lblLen;
         uint32_t sz = maxsize - lbldiff;
@@ -181,6 +189,8 @@
         Reset();
     }
     virtual ~SnConfigFrame() {}
+
+    void ApplyConnectListenSafetyNets();
     
     bool     IsCountingPowerReadings() const { return ((fRunMode & kCountPowerBit)!=0); }
     bool     IsSingleSeqRunMode() const { return ((fRunMode & kSingleSeqBit)!=0); }
@@ -188,7 +198,7 @@
     const char* GetLabel() const { return fLabel; }
     uint32_t GetLabelStrLen() const { return strlen(fLabel); }
     uint32_t GetRun() const { return fRun; }
-    uint32_t GetFirstEvt() const { return fFirstEvt; }
+    uint16_t GetFirstSeq() const { return fFirstSeq; }
     uint32_t GetEvtsPerFile() const { return fEvtsPerSeq; }
     uint16_t GetEvtThrtlPeriodMs() const { return fEvtThrtlPeriodMs; }
     float    GetForceTrigPeriod() const { return fForceTrigPeriod; }
@@ -218,6 +228,8 @@
     const char* GetMbedGate() const { return fMbedGate; }
     uint32_t GetCommWinPeriod() const { return fIsLowPower ? fCommWinPrdLowPwr : fCommWinPeriod; }
     uint32_t GetCommWinDuration() const { return fIsLowPower ? fCommWinDurLowPwr : fCommWinDuration; }
+    uint32_t GetCommWinConnectTO() const { return (static_cast<uint32_t>(fCommWinConnectTOMin) * 60u); }
+    uint32_t GetCommWinListenTO() const { return (static_cast<uint32_t>(fCommWinListenTOMin) * 60u); }
     int16_t  GetCommSendData() const { return fCommSendData; }
     
     bool     IsSendingAllFiles() const
@@ -228,6 +240,8 @@
         { return (fCommSendData<0) && ((fCommSendData & kDelete)!=0); }
     bool     IsForcingSBDdata() const
         { return (fCommSendData<0) && ((fCommSendData & kForceSBDdata)!=0); }
+    bool     IsWaitingHndShkBeforeSendData() const
+        { return (fCommSendData<0) && ((fCommSendData & kHnShBefSendDat)!=0); }
     
     uint8_t GetPowerMode() const { return fPowerMode; }
     int     GetPowPinSetting(const EPowerModeBit p, const bool isOn) const {
@@ -268,6 +282,20 @@
                             uint16_t& wvBase) const;
     
     // i/o
+    static
+    bool    IsIOversionOk(const uint8_t rv) {
+        return (kIOVers == rv);
+    }
+    
+    template<class T>
+    static
+    uint8_t PeekIOversion(T& b) {
+        // the buffer/file/whatever 'b' must be at the start of the config frame
+        uint8_t Rv=0;
+        SnBitUtils::ReadFrom(b, Rv);
+        return Rv;
+    }
+    
     template<class T>
     void ReadFrom(T& b) {
         // no check on the length of buf is done here
@@ -298,20 +326,27 @@
 #ifdef DEBUG
             printf("run=%u\r\n",fRun);
 #endif
-            b           = SnBitUtils::ReadFrom(b, fFirstEvt);
+            if (Rv>7) {
+                b           = SnBitUtils::ReadFrom(b, fFirstSeq);
+            } else {
+                uint32_t fe(0);
+                fFirstSeq = 0;
+                b           = SnBitUtils::ReadFrom(b, fe);
+            }
 #ifdef DEBUG
-            printf("fe=%u\r\n",fFirstEvt);
+            printf("firstseq=%hu\r\n",fFirstSeq);
 #endif
             if (Rv>1) {
                 b       = SnBitUtils::ReadFrom(b, fEvtsPerSeq);
+                b       = SnBitUtils::ReadFrom(b, fRunMode);
+            } else {
+                fEvtsPerSeq = 1000;
+                fRunMode    = 0;
+            }
 #ifdef DEBUG
                 printf("eps=%u\r\n",fEvtsPerSeq);
-#endif
-                b       = SnBitUtils::ReadFrom(b, fRunMode);
-#ifdef DEBUG
                 printf("rm=%hhu\r\n",fRunMode);
 #endif
-            }
             b           = SnBitUtils::ReadFrom(b, fStreamHiLoPlas);
 #ifdef DEBUG
             printf("shilo=%d\r\n",(int)fStreamHiLoPlas);
@@ -386,22 +421,22 @@
 #endif
             if (Rv<6) {
                 b       = SnBitUtils::ReadFrom(b, fBatVoltToLowPwr);
-#ifdef DEBUG
-                printf("batlow=%hd\r\n",fBatVoltToLowPwr);
-#endif
+                fBatVoltFromLowPwr = 1.1*fBatVoltToLowPwr;
             } else {
                 b       = SnBitUtils::ReadFrom(b, fBatVoltToLowPwr);
                 b       = SnBitUtils::ReadFrom(b, fBatVoltFromLowPwr);
             }
 #ifdef DEBUG
-                printf("batlow(to,from)=(%hd,%hd)\r\n",fBatVoltToLowPwr,fBatVoltFromLowPwr);
+                printf("batlow(to,from)=(%hu,%hu)\r\n",fBatVoltToLowPwr,fBatVoltFromLowPwr);
 #endif
             if (Rv>2) {
                 b       = SnBitUtils::ReadFrom(b, fVoltCheckPeriod);
+            } else {
+                fVoltCheckPeriod = 600u;
+            }
 #ifdef DEBUG
                 printf("vltchk=%hu\r\n",fVoltCheckPeriod);
 #endif
-            }
             b           = SnBitUtils::ReadFrom(b, fCommWinPeriod);
 #ifdef DEBUG
             printf("cmper=%u\r\n",fCommWinPeriod);
@@ -422,6 +457,16 @@
 #ifdef DEBUG
             printf("cmdurlp=%u\r\n",fCommWinDurLowPwr);
 #endif
+            if (Rv>6) {
+                b       = SnBitUtils::ReadFrom(b, fCommWinConnectTOMin);
+                b       = SnBitUtils::ReadFrom(b, fCommWinListenTOMin);
+            } else {
+                fCommWinConnectTOMin = fCommWinListenTOMin = 3u;
+            }
+#ifdef DEBUG
+                printf("connectTO=%hhu, listenTO=%hhu\r\n",
+                    fCommWinConnectTOMin, fCommWinListenTOMin);
+#endif
             if (Rv>2) {
                 b       = SnBitUtils::ReadFrom(b, fRemoteServer, kIPLen);
                 if (strncmp(fRemoteServer, kDefIPflag,kIPLen)==0) {
@@ -458,6 +503,8 @@
 #ifdef DEBUG
                 printf("mbedgate=%s\r\n",fMbedGate);
 #endif
+            } else {
+                SetDefaultIPs();
             }
             b           = SnBitUtils::ReadFrom(b, fWatchDogPeriod);
 #ifdef DEBUG
@@ -497,14 +544,19 @@
         //
         // intentionally not writing mac address here, so we don't have to read it in
         
-        const uint32_t llen = strlen(fLabel);
         b           = SnBitUtils::WriteTo(b, kIOVers); // i/o version
-        b           = SnBitUtils::WriteTo(b, llen);
+        // account for the ending \0
+        uint32_t llen = strlen(fLabel);
+        static const uint32_t maxllen = kConfLblLen-1;
+        if (llen > maxllen) {
+            llen = maxllen;
+        }        
+        b           = SnBitUtils::WriteTo(b, llen+1); // strlen + \0
         b           = SnBitUtils::WriteTo(b, fLabel, llen);
         b           = SnBitUtils::WriteTo(b, char('\0'));
         b           = SnBitUtils::WriteTo(b, fConfTime);
         b           = SnBitUtils::WriteTo(b, fRun);
-        b           = SnBitUtils::WriteTo(b, fFirstEvt);
+        b           = SnBitUtils::WriteTo(b, fFirstSeq);
         b           = SnBitUtils::WriteTo(b, fEvtsPerSeq);
         b           = SnBitUtils::WriteTo(b, fRunMode);
         b           = SnBitUtils::WriteTo(b, fStreamHiLoPlas);
@@ -536,6 +588,8 @@
         b           = SnBitUtils::WriteTo(b, fCommSendData);
         b           = SnBitUtils::WriteTo(b, fCommWinPrdLowPwr);
         b           = SnBitUtils::WriteTo(b, fCommWinDurLowPwr);
+        b           = SnBitUtils::WriteTo(b, fCommWinConnectTOMin);
+        b           = SnBitUtils::WriteTo(b, fCommWinListenTOMin);
         b           = SnBitUtils::WriteTo(b, fRemoteServer, kIPLen);
         b           = SnBitUtils::WriteTo(b, fRemotePort);
         b           = SnBitUtils::WriteTo(b, fMbedIP, kIPLen);
@@ -558,6 +612,7 @@
     
     void Reset() {
         memset(fLabel, 0, sizeof(char)*kConfLblLen);
+        SetHardDefaults();
         if (ReadFromFile(kDefConfFile)==false) {
             // couldn't get default. use hardcoded version.
             SetHardDefaults();
@@ -566,14 +621,14 @@
         printf("config reset to %s\r\n",fLabel);
 #endif
     }
-        
+    
     uint32_t    SizeOf(const uint8_t rv) const {
         // returns the num of bytes needed to stream this object
         // = size of member vars + 1 for i/o version + extra PLA strings (maybe)
         //   + length of label string
         return SizeOf(rv, fStreamHiLoPlas!=0, fNumPlas, strlen(fLabel));
     }
-
+    
     static void     SetMacAddress();
     static uint64_t GetMacAddress() {
         if (fgMacAdr==0) {