Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnCommWin.cpp
- Revision:
- 27:efc4d654b139
- Parent:
- 26:b3180073c8ec
- Child:
- 36:87865913ae6f
--- a/SnCommWin.cpp Tue Oct 30 06:02:32 2012 +0000 +++ b/SnCommWin.cpp Thu Nov 01 07:00:17 2012 +0000 @@ -15,7 +15,9 @@ //#define DEBUG -const char* SnCommWin::kLocalDir = "/local"; +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 uint32_t SnCommWin::GetConnectTimeout() const { return kConnectTimeout; } uint32_t SnCommWin::GetListenTimeout() const { return kListenTimeout; } @@ -128,8 +130,9 @@ printf("calling wait handshake\r\n"); #endif // wait for handshake - uint8_t hndshk=0; - res = WaitHandshake(curConf, handshakeTimeout, genBuf, bsize, hndshk); + uint8_t hndshk(0); uint32_t hndshkLen(0); + res = WaitHandshake(curConf, handshakeTimeout, genBuf, bsize, hndshk, + &hndshkLen); if (hndres!=0) { *hndres = hndshk; } @@ -138,20 +141,34 @@ res, nevts, curConf.IsDeletingFiles(), inf, SnSDUtils::GetCurFile(), hndshk); #endif -/* if ( (res==SnCommWin::kOkWithMsg) // got handshake && (nevts==0) // sent whole file - && curConf.IsDeletingFiles() // want to delete - && (inf!=SnSDUtils::GetCurFile()) // not current file - && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received - // delete it + && (inf!=SnSDUtils::GetCurFile()) ) { // not current file + if ( curConf.IsDeletingFiles() // want to delete + && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received + + // delete it #ifdef DEBUG - printf("deleting file %p, %s\r\n",inf,infn); + printf("deleting file %p, %s\r\n",inf,infn); #endif - SnSDUtilsWhisperer::DeleteFile(inf, infn); - didDel=true; + SnSDUtilsWhisperer::DeleteFile(inf, infn); + didDel=true; + } else if ( hndshk==SnHeaderFrame::kHnShOkStopCode ) { + res = SnCommWin::kOkStopComm; + } else if ( (hndshk==SnHeaderFrame::kHnShOkDelAlCode) || + (hndshk==SnHeaderFrame::kHnShOkDelRnCode) ) { + if ( GetDeleteAllConfirmCode(curConf, + kDelAllConfCodeLen, handshakeTimeout, + genBuf, bsize) ) { + if (hndshk==SnHeaderFrame::kHnShOkDelAlCode) { + SnSDUtilsWhisperer::DeleteAllFiles(); + } else if (hndshk==SnHeaderFrame::kHnShOkDelRnCode) { + SnSDUtilsWhisperer::DeleteFilesOfRun(hndshkLen); + } + res = SnCommWin::kOkStopComm; + } + } } -*/ } } } @@ -171,11 +188,46 @@ return res; } +bool SnCommWin::GetDeleteAllConfirmCode(const SnConfigFrame& conf, + const uint32_t length, + const uint32_t timeout, + char* const buf, + const uint32_t bsize) { + bool ret = false; + // ensure we don't wait forever + const uint32_t to = ((timeout>0) && (timeout<kAbsMaxTimer)) + ? conf.GetTimeoutTime(time(0), timeout) + : conf.GetTimeoutTime(time(0), kListenTimeout); +#ifdef DEBUG + printf("GetDeleteAllConfirmCode, to=%u\r\n",to); +#endif + // better pull all the bytes no matter the buffer size + const uint32_t ll = (bsize<length) ? bsize : length; + int mlen = 0; + while ( mlen<length ) { + mlen += ReceiveAll(buf, ll, to); + if (IsTimedOut(to)) { + break; + } + } + if (mlen==length) { + const char* b = buf; + const char* c = kDelAllConfCodeStr; + bool match = true; + for (uint8_t i=0; (i<kDelAllConfCodeLen) && match; ++i, ++b, ++c) { + match = ((*b)==(*c)); + } + ret = match; + } + return ret; +} + SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const SnConfigFrame& conf, const uint32_t timeout, char* const buf, const uint32_t bsize, - uint8_t& hndShkCode) { + uint8_t& hndShkCode, + uint32_t* hndShkLen) { // ensure we don't wait forever const uint32_t to = ((timeout>0) && (timeout<kAbsMaxTimer)) ? conf.GetTimeoutTime(time(0), timeout) @@ -193,6 +245,9 @@ printf("got handshake code=%02x, len=%u\r\n",hndShkCode,msgLen); #endif if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) { + if (hndShkLen!=0) { + *hndShkLen = msgLen; + } return SnCommWin::kOkWithMsg; } else { // TODO: somehow handle unexpected message? @@ -517,6 +572,7 @@ return SnCommWin::kFailPartSent; } + SnCommWin::ECommWinResult SnCommWin::SendString(const char* str, const uint32_t timeout) { #ifdef DEBUG