Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
84:80b15993944e
Parent:
76:f8383f0292c2
Child:
98:ce72ef143b9b
--- a/SnCommWin.cpp	Mon Feb 23 03:04:39 2015 +0000
+++ b/SnCommWin.cpp	Fri Oct 30 04:49:40 2015 +0000
@@ -10,6 +10,8 @@
 #include "SnConstants.h"
 #include "SnCRCUtils.h"
 #include "SnCommPeripheral.h"
+#include "SnClockSetFrame.h"
+#include "SnHeartbeatFrame.h"
 
 #include <algorithm>
 #include <ctype.h>
@@ -784,14 +786,39 @@
     return SnCommWin::kFailNoneSent;
 }
 
+bool SnCommWin::WriteStatusDataPackHeaderTo(char*& b,
+                                            const uint32_t payloadLen) {
+    return CallHeaderWriteTo(b, 
+                             SnHeaderFrame::kStatusDataPack,
+                             SnHeaderFrame::SizeOf() // this is the header of the packed data, not the kStatusDataPack hdr itself
+                                + payloadLen);
+}
+
+bool SnCommWin::CallHeaderWriteTo(char*& b,
+                                  const uint8_t msgCode,
+                                  const uint32_t msgLen) {
+    // to call fro SnCommWin.h where we don't include SnHeaderFrame
+    return SnHeaderFrame::WriteTo(b, msgCode, msgLen);
+}
+
+int32_t SnCommWin::CallSendAll(const char* const data,
+                               const uint32_t length,
+                               const uint32_t timeout_clock) {
+    // to call fro SnCommWin.h where we don't include SnCommPeripheral
+    if (fComm!=0) {
+        return fComm->SendAll(data, length, timeout_clock);
+    }
+    return 0;
+}
+
 SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf,
-                                                const SnEventFrame& evt,
-                                                const SnPowerFrame& pow,
+                                                const SnPowerFrame& pow, // com win power
+                                                const SnEventFrame& stEvent,
                                                 const uint16_t seq,
                                                 const float thmrate,
                                                 const float evtrate,
                                                 const uint32_t powerOnTime,
-                                                const SnTempFrame& temper,
+                                                const SnTempFrame& temper, // com win temp
                                                 char* const genBuf,
                                                 const uint32_t timeout_clock) {
     // TODO: check if connected?
@@ -800,63 +827,265 @@
 #endif
     
     Watchdog::kick(); // don't reset
-    
+
+    int32_t toBeSent(0), byteSent(0), sendNow(0);
     uint8_t loseLSB=0, loseMSB=0;
     uint16_t wvBase=0;
     conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase);
     const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf);
     char* b = genBuf;
     SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize);
-    SnStatusFrame::WriteTo(b, conf, evt, genBuf, seq, thmrate, evtrate, 
+    SnStatusFrame::WriteTo(b, conf, stEvent, genBuf, seq, thmrate, evtrate, 
                            powerOnTime, temper,
                            loseLSB, loseMSB, wvBase);
     SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, 
         pow.SizeOf(SnPowerFrame::kIOvers));
     pow.WriteTo(b);
-    int32_t msiz = b-genBuf;
+    sendNow  = b-genBuf;
+    toBeSent = sendNow;
 #ifdef DEBUG
     printf("calling SendAll (status)\r\n");
 #endif
-    int32_t mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
+    byteSent = fComm->SendAll(genBuf, sendNow, timeout_clock);
+#ifdef DEBUG
+    printf("SendAll returned %d\r\n",byteSent);
+#endif
+    
+    //byteSent += fComm->FinishSending(timeout_clock);
 #ifdef DEBUG
-    printf("SendAll returned %d\r\n",mlen);
+    printf("byteSent=%d  toBeSent=%d\r\n",byteSent,toBeSent);
+    printf("status+power frame:\r\n");
+    for (uint32_t i=0; i<sendNow; i++) {
+        printf("%02X ",genBuf[i]);
+    }
+    printf("\r\n");
 #endif
-    mlen += fComm->FinishSending(timeout_clock);
+        
+    Watchdog::kick(); // don't reset
+
+    // finally, mark the end of the status update
+    b = genBuf;
+    SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusUpdDone, 0);
+    sendNow   = b-genBuf;
+    toBeSent += sendNow;
 #ifdef DEBUG
-    printf("mlen=%d after FinishSending. msiz=%d\r\n",mlen,msiz);
-    printf("status frame:\r\n");
+    printf("calling SendAll (status update done)\r\n");
+#endif
+    byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock);
+
+
+    /*
+    // event compression parameters must be the same
+    // as those sent in the status update
+    b = genBuf;
+    SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode,
+        evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB));
+    b = evt.WriteTo(b, loseLSB, loseMSB, wvBase);
+    msiz = b-genBuf;
+#ifdef DEBUG
+    printf("calling SendAll (event) %d:\r\n",msiz);
     for (uint32_t i=0; i<msiz; i++) {
         printf("%02X ",genBuf[i]);
     }
     printf("\r\n");
 #endif
-    if (mlen==msiz) {
+    mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
+    */
+    
+    // finish sending whatever didn't make it out
+    byteSent += fComm->FinishSending(timeout_clock);
+
+#ifdef DEBUG
+    printf("after FinishSending\r\n");
+    printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent);
+#endif
+
+    Watchdog::kick(); // don't reset
+
+    if (byteSent==toBeSent) {
+#ifdef DEBUG
+        printf("Sent all status bytes OK\r\n");
+#endif
+        return SnCommWin::kOkMsgSent;
+    }
+    
 #ifdef DEBUG
-        printf("status+power sent\r\n");
+    printf("Status failed with part-sent\r\n");
+#endif
+    return SnCommWin::kFailPartSent;
+}
+    
+
+SnCommWin::ECommWinResult SnCommWin::SendStatusData(const SnConfigFrame& conf,
+                                                    const SnClockSetFrame& stTrgStartClk,
+                                                    const SnClockSetFrame& stTrgStopClk,
+                                                    const SnPowerFrame& stPower,
+                                                    const SnEventFrame& stEvent,
+                                                    const SnTempFrame& stTemperature,
+                                                    const SnHeartbeatFrame& stHeartbeat,
+                                                    const bool stNewPower,
+                                                    const bool stNewEvent,
+                                                    const bool stNewHeartbeat,
+                                                    const bool stNewTemperature,
+                                                    char* const genBuf,
+                                                    const uint32_t timeout_clock) {
+    //
+    //
+    // check what data we are sending with the status frame.
+    //
+    // ordered like they appear in SnEvts files
+    //
+    //
+    
+#ifdef DEBUG
+    printf("########### Send Status Data\r\n");
 #endif
-        
-        Watchdog::kick(); // don't reset
-        
-        // event compression parameters must be the same
-        // as those sent in the status update
+    
+    Watchdog::kick(); // don't reset
+
+    int32_t toBeSent(0), byteSent(0), sendNow(0);
+    char* b = genBuf;
+
+    // power reading first
+    if (conf.IsStatusSendingPowerReading() && stNewPower) {
+        SendStatusDataPack(stPower,
+                           SnHeaderFrame::kPowerCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
+#ifdef DEBUG
+        printf("Send status power data. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+
+    // config next
+    if (conf.IsStatusSendingConfig()) {
+        SendStatusDataPack(conf,
+                           SnHeaderFrame::kConfigCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
+#ifdef DEBUG
+        printf("Send status config. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+    
+    // trigger start time next
+    if (conf.IsStatusSendingTrigTimes() ) {
+        SendStatusDataPack(stTrgStartClk,
+                           SnHeaderFrame::kFileTrgStrtCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
+#ifdef DEBUG
+        printf("Send status trigger start. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+    
+    // event next
+    if (conf.IsStatusSendingEvent() && stNewEvent) {
+        // event has a different WriteTo
         b = genBuf;
-        SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode,
-            evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB));
-        b = evt.WriteTo(b, loseLSB, loseMSB, wvBase);
-        msiz = b-genBuf;
+        const uint32_t evtLen = stEvent.SizeOf(SnEventFrame::kIOVers,
+                                               conf.GetWvLoseLSB(),
+                                               conf.GetWvLoseMSB());
+        WriteStatusDataPackHeaderTo(b, evtLen);
+        CallHeaderWriteTo(b, SnHeaderFrame::kEventCode, evtLen);
+        b = stEvent.WriteTo(b,
+                            conf.GetWvLoseLSB(),
+                            conf.GetWvLoseMSB(),
+                            conf.GetWvBaseline());
+        sendNow   = (b - genBuf);
+        toBeSent += sendNow;
+        byteSent += CallSendAll(genBuf, sendNow, timeout_clock);
+
+#ifdef DEBUG
+        printf("Send status event. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+    
+    // temperature next
+    if (conf.IsStatusSendingTemperature() && stNewTemperature) {
+        SendStatusDataPack(stTemperature,
+                           SnHeaderFrame::kTemperatureCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
+#ifdef DEBUG
+        printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+
+    // heartbeat next
+    if (conf.IsStatusSendingHeartbeat() && stNewHeartbeat) {
+        SendStatusDataPack(stHeartbeat,
+                           SnHeaderFrame::kHeartbeatCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
 #ifdef DEBUG
-        printf("calling SendAll (event) %d:\r\n",msiz);
-        for (uint32_t i=0; i<msiz; i++) {
-            printf("%02X ",genBuf[i]);
-        }
-        printf("\r\n");
+        printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
+#endif
+    }
+
+    // trigger stop time next
+    if (conf.IsStatusSendingTrigTimes() ) {
+        SendStatusDataPack(stTrgStopClk,
+                           SnHeaderFrame::kFileTrgStopCode,
+                           toBeSent,
+                           byteSent,
+                           genBuf,
+                           timeout_clock);
+#ifdef DEBUG
+        printf("Send status trigger stop. toBeSent=%u, byteSent=%u\r\n",
+            toBeSent, byteSent);
 #endif
-        mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
-        mlen += fComm->FinishSending(timeout_clock);
-        if (mlen==msiz) {
-            return SnCommWin::kOkMsgSent;
-        }
     }
+
+    // finally, mark the end of the status update
+    if (toBeSent>0) {
+        b = genBuf;
+        SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusDatPakDone, 0);
+        sendNow   = b-genBuf;
+        toBeSent += sendNow;
+#ifdef DEBUG
+        printf("calling SendAll (status update done)\r\n");
+#endif
+        byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock);
+        
+        // finish sending whatever didn't make it out
+        byteSent += fComm->FinishSending(timeout_clock);
+
+#ifdef DEBUG
+        printf("after FinishSending status data\r\n");
+#endif
+    } // if toBeSent>0
+
+#ifdef DEBUG
+    printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent);
+#endif
+
+    if (byteSent==toBeSent) {
+#ifdef DEBUG
+        printf("Sent all status data bytes OK\r\n");
+#endif
+        return SnCommWin::kOkMsgSent;
+    }
+    
+#ifdef DEBUG
+    printf("Status data failed with part-sent\r\n");
+#endif
     return SnCommWin::kFailPartSent;
 }