Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
1:e392595b4b76
Parent:
0:664899e0b988
Child:
2:e67f7c158087
--- a/SnCommAfar.cpp	Sat Jun 30 02:03:51 2012 +0000
+++ b/SnCommAfar.cpp	Fri Jul 20 19:04:02 2012 +0000
@@ -1,142 +1,162 @@
-#include "SnCommAfar.h"
-
-#include "Websocket.h"
-#include "SnConfigFrame.h"
-#include "SnEventFrame.h"
-#include "SnStatusFrame.h"
-
-Websocket& SnCommAfar::GetWS() {
-    // only one make one socket
-    static Websocket* ws = new Websocket("ws://snowflake.ps.uci.edu:6767/ws");
-    return *ws;
-}
-
-bool SnCommAfar::Connect(Timer& timer, const uint32_t timeout) {
-    bool isConn = GetWS().connected();
-
-    while ( (isConn==false) && (timer.read() < timeout) ) {
-        wait_ms(250);
-        
-        isConn = GetWS().connect(&timer, timeout);
-    }
-    
-    return isConn;
-}
-
-SnCommWin::ECommWinResult SnCommAfar::OpenWindow(Timer& timer,
-                                                 const uint32_t timeout,
-                                                 const bool sendStatus,
-                                                 const SnConfigFrame& conf,
-                                                 const SnEventFrame& evt,
-                                                 char* const evtBuf,
-                                                 char* const statBuf) {
-    // timer = timer for full window
-    // timeout = number of seconds to try to connect (not full window)
-    
-    const bool canCon = Connect(timer, timeout);
-    
-    SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
-                                           : SnCommWin::kCanNotConnect;
-    
-    if (canCon && sendStatus) {
-        ret = SendStatus(conf, evt, evtBuf, statBuf);
-    }
-    
-    return ret;
-}
-
-
-SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf,
-                                                Timer& timer,
-                                                const uint32_t timeOut,
-                                                char* const confBuf) {
-    // confBuf assumed to alread be of allocated size
-    
-    SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
-    
-    const uint32_t rto = (timeOut < 3u) ? timeOut : 3u;
-    bool readConf = false;
-    while ( (readConf==false) && (timer.read() < timeOut) ) {
-        // TODO: check if read works (for binary)
-        readConf = GetWS().read(confBuf, &timer, rto);
-        if (readConf) {
-            const char* b = confBuf;
-            conf.ReadFrom(b);
-            res = SnCommWin::kOkWithMsg;
-        }
-    }
-    if (readConf==false) {
-        res = SnCommWin::kOkNoMsg;
-    }
-
-    return res;
-}
-
-SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf,
-                                                 const SnEventFrame& evt,
-                                                 char* const evtBuf,
-                                                 char* const statBuf) {
-    // TODO: check if connected?
-    SnStatusFrame::WriteTo(statBuf, SnConfigFrame::kAfar, conf, evt, evtBuf);
-    GetWS().sendBinary(statBuf, SnStatusFrame::SizeOf(conf));
-    return SnCommWin::kOkMsgSent;
-}
-
-SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) {
-    fseek(inf, 0, SEEK_END);
-    const uint32_t fsize = ftell(inf);
-    fseek(inf, 0, SEEK_SET);
-    const bool ok = GetWS().sendBinary(inf, fsize);
-    return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
-}
-
-SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf,
-                                                    const SnConfigFrame& curConf,
-                                                    SnEventFrame& evt,
-                                                    char* const evtBuf,
-                                                    char* const confBuf,
-                                                    const uint32_t nevts,
-                                                    const uint32_t firstEvt) {
-    // firstEvt==0 ==> start at beginning
-    // nevts==0 ==> NO events! (see SnCommWin::SendData
-    //    for the fcn to send the full file)
-    
-    // TODO: check memory for temporary config/event frames?
-    SnConfigFrame conf;
-    conf.ReadFrom(inf);
-    char* b = confBuf;
-    conf.WriteTo(b);
-    bool ok = GetWS().sendBinary(confBuf, conf.SizeOf());
-
-    if (ok) {
-        uint8_t sLoseLSB=0, sLoseMSB=0;
-        uint16_t sWvBase=0;
-        conf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase);
-        // do we have to unpack & repack events?
-        const bool repack =    (sLoseLSB != conf.GetWvLoseLSB())
-                            && (sLoseMSB != conf.GetWvLoseMSB())
-                            && (sWvBase  != conf.GetWvBaseline());
-        
-        // size of event in file
-        const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
-                                                    conf.GetWvLoseMSB());
-        // move up to first event
-        fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
-        if (repack) {
-            // repack ==> send event by event
-            // size of event sent over afar
-            const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
-            for (uint32_t i=0; (i<nevts) && ok; i++) {
-                evt.ReadFrom(inf, evtBuf,
-                    conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
-                    conf.GetWvBaseline());
-                evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
-                ok = GetWS().sendBinary(evtBuf, ssize);
-            }
-        } else {
-            // no repacking ==> just send the bytes from the file
-            ok = GetWS().sendBinary(inf, nevts*esize);
-        }
-    }
-    return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
-}
+#include "SnCommAfar.h"
+
+#include "Websocket.h"
+#include "SnConfigFrame.h"
+#include "SnEventFrame.h"
+#include "SnStatusFrame.h"
+
+Websocket& SnCommAfar::GetWS() {
+    // only one make one socket
+    //static Websocket* ws = new Websocket("ws://snowflake.ps.uci.edu:6767/ws");
+    // TODO: fix DNS
+    // TODO: remove hardcoded IP addresses from Websocket.cpp
+    static Websocket* ws = new Websocket("ws://128.195.204.151:6767/ws");
+    return *ws;
+}
+
+bool SnCommAfar::Connect(const uint32_t timeout) {
+    bool isConn = GetWS().connected();
+    printf("connect: ct=%d, timeout=%u\r\n",time(0),timeout);
+    while ( (isConn==false) && ( time(0) < timeout) ) {
+        wait_ms(250);
+#ifdef DEBUG
+        printf("connecting..\r\n");
+        DigitalOut led3(LED3);
+        led3=1; wait(0.2);
+#endif
+        isConn = GetWS().connect(timeout);
+#ifdef DEBUG
+        printf("isConn=%d\r\n",(int)isConn);
+        led3=0; wait(0.2);
+#endif
+    }
+    return isConn;
+}
+
+SnCommWin::ECommWinResult SnCommAfar::OpenWindow(const uint32_t timeout,
+                                                 const bool sendStatus,
+                                                 const SnConfigFrame& conf,
+                                                 const SnEventFrame& evt,
+                                                 char* const evtBuf,
+                                                 char* const statBuf) {
+    // timer = timer for full window
+    // timeout = number of seconds to try to connect (not full window)
+    
+    const bool canCon = Connect(timeout);
+    
+    SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
+                                           : SnCommWin::kCanNotConnect;
+    
+    if (canCon && sendStatus) {
+        ret = SendStatus(conf, evt, evtBuf, statBuf);
+    }
+    
+    return ret;
+}
+
+
+SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf,
+                                                const uint32_t timeOut,
+                                                char* const confBuf) {
+    // confBuf assumed to alread be of allocated size
+    
+    printf("GetConfig, to=%u\r\n",timeOut);
+    
+    SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
+    
+    bool readConf = false;
+    while ( (readConf==false) && (time(0) < timeOut) ) {
+        //printf("trying to read config\r\n");
+        // TODO: check if read works (for binary)
+        readConf = GetWS().read(confBuf, timeOut);
+        if (readConf) {
+            const char* b = confBuf;
+            conf.ReadFrom(b);
+            res = SnCommWin::kOkWithMsg;
+        }
+    }
+    if (readConf==false) {
+        res = SnCommWin::kOkNoMsg;
+    }
+
+    return res;
+}
+
+SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf,
+                                                 const SnEventFrame& evt,
+                                                 char* const evtBuf,
+                                                 char* const statBuf) {
+    // TODO: check if connected?
+    SnStatusFrame::WriteTo(statBuf, SnConfigFrame::kAfar, conf, evt, evtBuf);
+#ifdef DEBUG
+    printf("status frame:\r\n");
+    for (uint32_t i=0; i<SnStatusFrame::SizeOf(conf); i++) {
+        printf("%02X ",statBuf[i]);
+    }
+    printf("\r\n");
+#endif
+    GetWS().sendBinary(statBuf, SnStatusFrame::SizeOf(conf));
+    //base64enc(statBuf, SnStatusFrame::SizeOf(conf), gB64Buf);
+    //GetWS().send(gB64Buf);
+    printf("status sent\r\n");
+    return SnCommWin::kOkMsgSent;
+}
+
+SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) {
+    fseek(inf, 0, SEEK_END);
+    const uint32_t fsize = ftell(inf);
+    fseek(inf, 0, SEEK_SET);
+    const bool ok = GetWS().sendBinary(inf, fsize);
+    return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
+}
+
+SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf,
+                                                    const SnConfigFrame& curConf,
+                                                    SnEventFrame& evt,
+                                                    char* const evtBuf,
+                                                    char* const confBuf,
+                                                    const uint32_t nevts,
+                                                    const uint32_t firstEvt) {
+    // firstEvt==0 ==> start at beginning
+    // nevts==0 ==> NO events! (see SnCommWin::SendData
+    //    for the fcn to send the full file)
+    
+    // TODO: check memory for temporary config/event frames?
+    SnConfigFrame conf;
+    conf.ReadFrom(inf);
+    char* b = confBuf;
+    conf.WriteTo(b);
+    bool ok = GetWS().sendBinary(confBuf, conf.SizeOf());
+
+    if (ok) {
+        uint8_t sLoseLSB=0, sLoseMSB=0;
+        uint16_t sWvBase=0;
+        conf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase);
+        // do we have to unpack & repack events?
+        const bool repack =    (sLoseLSB != conf.GetWvLoseLSB())
+                            && (sLoseMSB != conf.GetWvLoseMSB())
+                            && (sWvBase  != conf.GetWvBaseline());
+        
+        // size of event in file
+        const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
+                                                    conf.GetWvLoseMSB());
+        // move up to first event
+        fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
+        if (repack) {
+            // repack ==> send event by event
+            // size of event sent over afar
+            const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
+            for (uint32_t i=0; (i<nevts) && ok; i++) {
+                evt.ReadFrom(inf, evtBuf,
+                    conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
+                    conf.GetWvBaseline());
+                evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
+                ok = GetWS().sendBinary(evtBuf, ssize);
+            }
+        } else {
+            // no repacking ==> just send the bytes from the file
+            ok = GetWS().sendBinary(inf, nevts*esize);
+        }
+    }
+    return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
+}