Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Revision:
12:d472f9811262
Parent:
10:3c93db1cfb12
Child:
13:7a1fb885a8e4
--- a/SnCommAfarTCP.cpp	Mon Aug 13 20:44:06 2012 +0000
+++ b/SnCommAfarTCP.cpp	Sat Aug 18 05:00:32 2012 +0000
@@ -3,13 +3,9 @@
 
 #include "EthernetInterface.h"
 #include "TCPSocketConnection.h"
-/*
-#include "SnSDUtils.h"
-#include "SnHeaderFrame.h"
-#include "SnConfigFrame.h"
-#include "SnEventFrame.h"
-#include "SnStatusFrame.h"
-*/
+
+#include "Watchdog.h"
+
 #define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))
 
 SnCommAfarTCP::SnCommAfarTCP(const SnConfigFrame& conf) :
@@ -36,17 +32,26 @@
     fMyIp   = myip;
     fMyMask = mask;
     fMyGate = gate;
+#ifdef DEBUG
     printf("closing socket\r\n");
+#endif
     fSock->close();
     //delete fSock;
     //fSock = new TCPSocketConnection;
+#ifdef DEBUG
     printf("disconnect eth\r\n");
+#endif
     fEth->disconnect();
     //delete fEth;
     //fEth = new EthernetInterface;
+#ifdef DEBUG
     printf("init %s, %s, %s\r\n",fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
+#endif
     fEth->init(fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
+
+#ifdef DEBUG
     printf("Set done\r\n");
+#endif
 }
 
 int SnCommAfarTCP::DoIO(char* const data,
@@ -56,7 +61,10 @@
     // TODO: if B64, must return number of bytes of raw (non encoded) message
     int res=0;
     uint32_t b=0;
-    while ( (length>b) && (time(0)<timeout_clock) ) {
+    while ( (length>b) ) {
+        if (IsTimedOut(timeout_clock)) {
+            break;
+        }
         res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b);
         switch (res) {
             case -1:
@@ -67,6 +75,7 @@
             default:
                 b += res;
         };
+        Watchdog::kick(); // don't reset; wait until timeout
     }
     return res; // timeout
 }
@@ -74,32 +83,42 @@
 int32_t 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(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all);
+    //return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all);
+    // use regular receive; DoIO will do a receive_all but use our timeout
+    return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive);
 }
 
 int32_t 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);
+    // use regular sell; DoIO will do a send_all but use our timeout
+    return DoIO(data, length, timeout_clock, &TCPSocketConnection::send);
 }
 
 
 bool SnCommAfarTCP::Connect(const uint32_t timeout) {
+#ifdef DEBUG
     printf("SnCOmmAfarTCP::Connect\r\n");
+#endif
     bool isConn = false;
 
     while ( (isConn==false) && ( time(0) < timeout) ) {
         wait_ms(250);
         isConn = (fEth->connect()==0);
     }
+#ifdef DEBUG
     printf("eth isConn=%d\r\n",(int)isConn);
+#endif
     
     isConn=false;
     while ( (isConn==false) && ( time(0) < timeout) ) {
         wait_ms(250);
         isConn = (fSock->connect(fRserv.c_str(), fRport)==0);
     }
+#ifdef DEBUG
     printf("sock isConn=%d\r\n",(int)isConn);
+#endif
     return isConn;
 }
 
@@ -112,7 +131,9 @@
                                                     const float thmrate,
                                                     const float evtrate,
                                                     char* const genBuf) {
+#ifdef DEBUG
     printf("SnCommAfarTCP::OpenWindow\r\n");
+#endif
     const bool canCon = Connect(timeout);
     
     SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
@@ -125,3 +146,6 @@
     return ret;
 }
 
+bool SnCommAfarTCP::CloseConn() {
+    return (fSock->close())==0;
+}