Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
6:6f002d202f59
Parent:
5:9cea89700c66
Child:
7:079617408fec
--- a/SnCommAfarTCP.cpp	Fri Aug 03 00:04:34 2012 +0000
+++ b/SnCommAfarTCP.cpp	Fri Aug 03 23:50:57 2012 +0000
@@ -1,21 +1,27 @@
 /*
 #include "SnCommAfarTCP.h"
 
+#include "EthernetInterface.h"
+#include "TCPSocketConnection.h"
+
+#include "SnSDUtils.h"
+#include "SnHeaderFrame.h"
+#include "SnConfigFrame.h"
+#include "SnEventFrame.h"
+#include "SnStatusFrame.h"
+
 #define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))
 
-const int SnCommAfarTCP::kSockTimeoutMs = 10;
-
-SnCommAfarTCP::SnCommAfarTCP(const bool useb64,
-                             char* const b64buf, const uint32_t bblen) :
-    fUseB64(useb64), fB64buf(b64buf), fbblen(bblen), fRmtServ(remote),
+SnCommAfarTCP::SnCommAfarTCP(const bool useb64, const char* remote, const int32_t rport) :
+    fUseB64(useb64), fRserv(remote), fRport(rport),
     fEth(new EthernetInterface), fSock(new TCPSocketConnection) {
     
     fEth->init("128.195.204.148",  // my IP
                "255.255.255.0",    // mask
                "128.195.204.1");   // gateway
     
-    fRserv = "128.195.204.151";
-    fRport = 6655;
+    //fRserv = "128.195.204.151";
+    //fRport = 6655;
     
 }
 
@@ -24,7 +30,7 @@
     delete fSock;
 }
 
-int SnCommAfarTCP::DoIO(const char* const data,
+int SnCommAfarTCP::DoIO(char* const data,
                         const uint32_t length,
                         const uint32_t timeout_clock,
                         TCPSendRecv fcn) {
@@ -32,7 +38,7 @@
     int res=0;
     uint32_t b=0;
     while ( (length>b) && (time(0)<timeout_clock) ) {
-        res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b, kSockTimeoutMs);
+        res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b);
         switch (res) {
             case -1:
                 // TODO: how to check the error?
@@ -46,16 +52,16 @@
     return res; // timeout
 }
 
-int SnCommAfarTCP::ReceiveAll(char* const buf, const uin32_t mlen,
+int SnCommAfarTCP::ReceiveAll(char* const buf, const uint32_t mlen,
                               const uint32_t timeout_clock) {
     // TODO: if B64, must return number of bytes of raw (non encoded) message
-    return DoIO(data, length, timeout_clock, &(TCPSocketConnection::receive_all));
+    return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all);
 }
 
-int SnCommAfarTCP::SendAll(const char* const data, const uint32_t length,
+int SnCommAfarTCP::SendAll(char* const data, const uint32_t length,
                            const uint32_t timeout_clock) {
     // TODO: if B64, must return number of bytes of raw (non encoded) message
-    return DoIO(data, length, timeout_clock, &(TCPSocketConnection::send_all));
+    return DoIO(data, length, timeout_clock, &TCPSocketConnection::send_all);
 }
 
 
@@ -69,7 +75,7 @@
     
     while ( (isConn==false) && ( time(0) < timeout) ) {
         wait_ms(250);
-        isConn = (fSock->connect(fRserv.c_str(), fRport)==0);s
+        isConn = (fSock->connect(fRserv.c_str(), fRport)==0);
     }
     
     return isConn;
@@ -86,7 +92,7 @@
                                            : SnCommWin::kCanNotConnect;
     
     if (canCon && sendStatus) {
-        ret = SendStatus(conf, evt, genBuf);
+        ret = SendStatus(conf, evt, genBuf, timeout);
     }
     
     return ret;
@@ -129,7 +135,7 @@
         const int hlen = ReceiveAll(confBuf, SnHeaderFrame::SizeOf(), timeOut);
         if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) {
             uint8_t mcode=0; uint32_t mlen=0;
-            SnHeaderFrame::ReadFrom(b, mcode, mlen);
+            SnHeaderFrame::ReadFrom(confBuf, mcode, mlen);
             printf("mcode=%02x, mlen=%u\r\n", mcode, mlen);
             if (mcode!=SnHeaderFrame::kConfigCode) {
                 res = SnCommWin::kUnexpectedRec;
@@ -184,7 +190,7 @@
     return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
 }
 
-SnCommWin::ECommWinResult SnCommAfar::SendFileContents(FILE* inf,
+SnCommWin::ECommWinResult SnCommAfarTCP::SendFileContents(FILE* inf,
                                                     const SnConfigFrame& curConf,
                                                     SnEventFrame& evt,
                                                     char* const genBuf,
@@ -217,15 +223,11 @@
     uint8_t sLoseLSB=0, sLoseMSB=0;
     uint16_t sWvBase=0;
     curConf.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());
     
-    printf("repack=%d, esize=%u\r\n",(int)repack,esize);
+    printf("esize=%u\r\n",esize);
     
     // how many events?
     // move up to first event
@@ -245,19 +247,20 @@
     
     // send number of events to expect
     bool ok = false;
-        char* b = genBuf;
+    char* b = genBuf;
     SnHeaderFrame::WriteTo(genBuf, SnHeaderFrame::kFileNevtsCode, nevts);
+    int msiz;
     int mlen = SendAll(genBuf, SnHeaderFrame::SizeOf(), timeout_clock);
     if (mlen==SnHeaderFrame::SizeOf()) {
         // send the file header
         SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileHeadrCode, SnSDUtils::SizeOfFileHeader());
         SnSDUtils::WriteFileHeader(b, macadr, run, seq, v1, v2);
-        int msiz = SnHeaderFrame::SizeOf()+SnSDUtils::SizeOfFileHeader();
+        msiz = SnHeaderFrame::SizeOf()+SnSDUtils::SizeOfFileHeader();
         mlen = SendAll(genBuf, msiz, timeout_clock);
         if (mlen==msiz) {
             // send the config
             b = genBuf;
-            SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfAndEvtsCode, conf.SizeOf());
+            SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfigCode, conf.SizeOf());
             conf.WriteTo(b);
             msiz = SnHeaderFrame::SizeOf()+conf.SizeOf();
             mlen = SendAll(genBuf, msiz, timeout_clock);
@@ -268,36 +271,24 @@
     }
         
     printf("ok=%d, nevts=%u\r\n",(int)ok,nevts);
-//TODO HERE
     
     if (ok) {
         // size of event sent over afar
-        const uint32_t ssize = (repack) ?
-            SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) :
-            esize;
+        const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
         
         for (uint32_t i=0; (i<nevts) && ok; i++) {
             if (feof(inf)==0 && ferror(inf)==0 && ((ftell(inf)+ssize)<=fend)) {
                 printf("sending evt %u\r\n",i);
-                if (repack) {
-                    evt.ReadFrom(inf, genBuf,
-                        conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
-                        conf.GetWvBaseline());
-                    // must be after evt.Read, since that uses the buffer
-                    b = genBuf;
-                    SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize);
-                    evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase);
-                    ok &= GetWS().sendBinary(
-                        genBuf, ssize+SnHeaderFrame::SizeOf(),
-                        fB64buf);
-                } else {
-                    ok &= GetWS().sendBinary(
-                        SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
-                                                ssize),
-                        SnHeaderFrame::SizeOf(),
-                        inf, ssize,
-                        fB64buf);
-                }
+                evt.ReadFrom(inf, genBuf,
+                    conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
+                    conf.GetWvBaseline());
+                // must be after evt.Read, since that uses the buffer
+                b = genBuf;
+                SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize);
+                evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase); // will repack if necessary
+                msiz = SnHeaderFrame::SizeOf()+ssize;
+                mlen = SendAll(genBuf, msiz, timeout_clock);
+                ok &= mlen==msiz;
             } else {
                 printf("cannot send requested event\r\n");
                 ok=false;