Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnCommWin.cpp
- Revision:
- 37:ff95e7070f26
- Parent:
- 36:87865913ae6f
- Child:
- 38:9070c17536cd
--- a/SnCommWin.cpp Thu May 23 22:26:39 2013 +0000 +++ b/SnCommWin.cpp Wed May 29 00:20:31 2013 +0000 @@ -8,36 +8,42 @@ #include "SnSDUtils.h" #include "SnConstants.h" #include "SnCRCUtils.h" +#include "SnCommPeripheral.h" #include <algorithm> #include <ctype.h> extern "C" void mbed_reset(); -#define DEBUG +//#define DEBUG const char* SnCommWin::kLocalDir = "/local"; const char* SnCommWin::kDelAllConfCodeStr = "fj2io32FIJ(#jd;;.O@++/]ewavk2[49ojv"; const uint8_t SnCommWin::kDelAllConfCodeLen = 35; // length of the above string +SnCommWin::SnCommWin(SnCommPeripheral* p) : + fComm(p) { + if (p==0) { + error("SnCommWin - must have peripheral! Received 0.\r\n"); + } +} + +SnCommWin::~SnCommWin() { + delete fComm; +} + uint32_t SnCommWin::GetConnectTimeout() const { return kConnectTimeout; } uint32_t SnCommWin::GetListenTimeout() const { return kListenTimeout; } -bool SnCommWin::IsTimedOut(const uint32_t timeout_clock) const { - if (timeout_clock==0) { - // for not obeying timeout option - return false; - } else { - const uint32_t ct = time(0); - if ( (ct==0) || - (fabs(static_cast<double>(timeout_clock-ct))>kSecsPerDay) ) { - // clock problems! - // timeout now. hope the clock problems - // get fixed in the next comm window - return true; - } else { - return (ct>timeout_clock); - } - } +bool SnCommWin::TrySetSysTimeUnix(const uint32_t timeout) { + return (fComm!=0) ? (fComm->TrySetSysTimeUnix(timeout)) : false; +} + +bool SnCommWin::Connect(const uint32_t timeout) { + return (fComm!=0) ? (fComm->Connect(timeout)) : false; +} + +bool SnCommWin::CloseConn(const uint32_t timeout) { + return (fComm!=0) ? (fComm->CloseConn(timeout)) : false; } SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf, @@ -180,7 +186,7 @@ if ( (inf!=SnSDUtils::GetCurFile()) && (didDel==false) ) { SnSDUtils::CloseOutputFile(inf); } - if ( IsTimedOut(timeout_clock) ) { + if ( fComm->IsTimedOut(timeout_clock) ) { if ( kFailTimeout < res ) { res = kFailTimeout; } @@ -205,8 +211,8 @@ const uint32_t ll = (bsize<length) ? bsize : length; int mlen = 0; while ( mlen<length ) { - mlen += ReceiveAll(buf, ll, to); - if (IsTimedOut(to)) { + mlen += fComm->ReceiveAll(buf, ll, to); + if (fComm->IsTimedOut(to)) { break; } } @@ -236,7 +242,7 @@ printf("WaitHandshake, to=%u\r\n",to); #endif - const int mlen = ReceiveAll(buf, SnHeaderFrame::SizeOf(), to); + const int mlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), to); if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) { uint32_t msgLen=0; const char* b = buf; @@ -265,23 +271,13 @@ #ifdef DEBUG printf("GetMbedFile, to=%u\r\n",timeout); #endif - const int mlen = ReceiveAll(buf, namelen, timeout); + const int mlen = fComm->ReceiveAll(buf, namelen, timeout); if (mlen>0 && static_cast<uint32_t>(mlen) == namelen) { return SnCommWin::kOkWithMsg; } return SnCommWin::kUnexpectedRec; } -void SnCommWin::CapitalizeInPlace(std::string::iterator s, - const std::string::const_iterator send) { - static const char upd = 'a' - 'A'; // a>A - for (; s!=send; ++s) { - if ( ((*s)>='a') && ((*s)<='z') ) { - (*s) -= upd; - } - } -} - SnCommWin::ECommWinResult SnCommWin::GetLocalFile(std::string fname, char* const buf, const uint32_t bsize, @@ -298,7 +294,7 @@ #endif // got the header.. make the file.. // make the file name ALLCAPS, since LocalFileSystem will do it anyway - CapitalizeInPlace(fname.begin(), fname.end()); + SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end()); // now ensure the file name is 8.3 only -- for LocalFileSystem const size_t ldlen = strlen(kLocalDir); // 12 = 8.3 filename format, 1 for / and 1 for \0 @@ -319,7 +315,7 @@ // get all the data and dump it into the file int b = 0, toget = 0; while ( mlen>b ) { - if (IsTimedOut(timeout)) { + if (fComm->IsTimedOut(timeout)) { #ifdef DEBUG printf("timeout while getting file\r\n"); #endif @@ -330,7 +326,7 @@ if (toget>bsize) { toget = bsize; } - const int got = ReceiveAll(buf, toget, timeout); + const int got = fComm->ReceiveAll(buf, toget, timeout); if (lf!=NULL) { #ifdef DEBUG printf("writing %d bytes to file\r\n", got); @@ -477,7 +473,7 @@ SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; if (bsize>=SnHeaderFrame::kMaxSizeOf) { // get header - const int hlen = ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut); + const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut); if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) { mcode=0; mlen=0; @@ -524,7 +520,7 @@ res = SnCommWin::kUnexpectedRec; } else { // get config - const int clen = ReceiveAll(confBuf, mlen, timeOut); + const int clen = fComm->ReceiveAll(confBuf, mlen, timeOut); if (clen>0 && static_cast<uint32_t>(clen)==mlen) { const char* b = confBuf; conf.ReadFrom(b); @@ -568,7 +564,7 @@ #ifdef DEBUG printf("calling SendAll (status)\r\n"); #endif - int mlen = SendAll(genBuf, msiz, timeout_clock); + int mlen = fComm->SendAll(genBuf, msiz, timeout_clock); #ifdef DEBUG printf("status frame:\r\n"); for (uint32_t i=0; i<msiz; i++) { @@ -594,7 +590,7 @@ } printf("\r\n"); #endif - mlen = SendAll(genBuf, msiz, timeout_clock); + mlen = fComm->SendAll(genBuf, msiz, timeout_clock); if (mlen==msiz) { return SnCommWin::kOkMsgSent; } @@ -608,16 +604,8 @@ #ifdef DEBUG printf("SnCommWin::SendString %s\r\n",str); #endif - const size_t rlen = strlen(str); - const size_t slen = rlen > kMaxStrLen ? kMaxStrLen : rlen; - const int msiz = slen+SnHeaderFrame::SizeOf(); - char* const ts = new char[msiz]; - char* t = ts; - SnHeaderFrame::WriteTo(t, SnHeaderFrame::kStringCode, slen); - strncpy(t, str, slen); - const int mlen = SendAll(ts, msiz, timeout); - delete[] ts; - return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; + const bool ok = fComm->SendString(str, timeout); + return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; } SnCommWin::ECommWinResult SnCommWin::SendFilename(const char* inf, @@ -643,7 +631,7 @@ SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen); b = SnBitUtils::WriteTo(b, fn, flen); const int msiz = b-genBuf; - const int mlen = SendAll(genBuf, msiz, timeout_clock); + const int mlen = fComm->SendAll(genBuf, msiz, timeout_clock); #ifdef DEBUG printf("time = %u, timeout = %u\r\n",time(0), timeout_clock); #endif @@ -664,7 +652,7 @@ #ifdef DEBUG printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf()); #endif - return SendAll(genBuf, blockSize+SnHeaderFrame::SizeOf(), timeout); + return fComm->SendAll(genBuf, blockSize+SnHeaderFrame::SizeOf(), timeout); } SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf, @@ -779,7 +767,7 @@ SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); b = SnBitUtils::WriteTo(b, evtsSent); msiz = b - genBuf; - mlen = SendAll(genBuf, msiz, timeout_clock); + mlen = fComm->SendAll(genBuf, msiz, timeout_clock); ok &= msiz==mlen; #ifdef DEBUG @@ -794,7 +782,7 @@ SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); b = SnBitUtils::WriteTo(b, powsSent); msiz = b - genBuf; - mlen = SendAll(genBuf, msiz, timeout_clock); + mlen = fComm->SendAll(genBuf, msiz, timeout_clock); ok &= msiz==mlen; #ifdef DEBUG