Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnCommWin.cpp
- Revision:
- 64:6c7a316eafad
- Parent:
- 63:4820a4460f00
- Child:
- 65:2cb3e99ce466
--- a/SnCommWin.cpp Fri Nov 28 05:41:42 2014 +0000 +++ b/SnCommWin.cpp Fri Nov 28 07:42:57 2014 +0000 @@ -40,14 +40,17 @@ } bool SnCommWin::Connect(const uint32_t timeout) { + Watchdog::kick(); // don't reset return (fComm!=0) ? (fComm->Connect(timeout)) : false; } bool SnCommWin::CloseConn(const uint32_t timeout) { + Watchdog::kick(); // don't reset return (fComm!=0) ? (fComm->CloseConn(timeout)) : false; } bool SnCommWin::PowerDown(const uint32_t timeout) { + Watchdog::kick(); // don't reset return (fComm!=0) ? (fComm->PowerDown(timeout)) : false; } @@ -60,6 +63,8 @@ #ifdef DEBUG printf("SnCommWin::SendData\r\n"); #endif + Watchdog::kick(); // don't reset + ECommWinResult res = kUndefFail; if ( (GetCommType() != SnConfigFrame::kIrid) || (conf.IsForcingSBDdata()) ) { @@ -131,6 +136,8 @@ #ifdef DEBUG printf("SnCommWin::SendDataFromFile file (%p), fn=%s\r\n",inf,infn); #endif + Watchdog::kick(); // don't reset + ECommWinResult res = kUndefFail; bool didDel = false; @@ -168,6 +175,8 @@ #ifdef DEBUG printf("calling wait handshake\r\n"); #endif + Watchdog::kick(); // don't reset + // wait for handshake uint8_t hndshk(0); uint32_t hndshkLen(0); res = WaitHandshake(curConf, timeout_clock, genBuf, bsize, hndshk, @@ -223,6 +232,8 @@ SnCommWin::ECommWinResult res = SnCommWin::kUnexpectedRec; + Watchdog::kick(); // don't reset + if ( (hndshk==SnHeaderFrame::kHnShNoReplyCode) || (hndshk==SnHeaderFrame::kHnShOkPartlCode) ) { // nothing to do @@ -341,6 +352,8 @@ #ifdef DEBUG printf("GetDeleteAllConfirmCode, timeout=%u\r\n",timeout); #endif + Watchdog::kick(); // don't reset + // better pull all the bytes no matter the buffer size const uint32_t ll = (bsize<length) ? bsize : length; int mlen = 0; @@ -350,6 +363,9 @@ break; } } + + Watchdog::kick(); // don't reset + if (mlen==length) { const char* b = buf; const char* c = kDelAllConfCodeStr; @@ -372,6 +388,8 @@ printf("WaitHandshake, timeout=%u\r\n",timeout); #endif + Watchdog::kick(); // don't reset + const int mlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeout); if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) { uint32_t msgLen=0; @@ -401,6 +419,9 @@ #ifdef DEBUG printf("GetMbedFile, to=%u\r\n",timeout); #endif + + Watchdog::kick(); // don't reset + const int mlen = fComm->ReceiveAll(buf, namelen, timeout); if (mlen>0 && static_cast<uint32_t>(mlen) == namelen) { return SnCommWin::kOkWithMsg; @@ -414,6 +435,8 @@ const uint32_t timeout) { // get a file and save it locally with the specified name + Watchdog::kick(); // don't reset + // get the header for the file uint8_t mcode=0; uint32_t mlen=0; SnCommWin::ECommWinResult res = @@ -435,6 +458,9 @@ const uint32_t fbs = ldlen+1+12+1; char fnb[fbs]; memset(fnb, 0, sizeof(char)*fbs); + + Watchdog::kick(); // don't reset + // find the extension const size_t xdot = fname.rfind('.'); if (xdot!=string::npos && (fname.size()==(4+xdot))) { @@ -445,6 +471,9 @@ strncpy(fb, fname.c_str(), fnpl); fb+=fnpl; // FILENAME *fb = '.'; ++fb; // . strncpy(fb, fname.c_str()+xdot+1, 3); // EXT + + Watchdog::kick(); // don't reset + // all that just for the file name! FILE* lf = fopen(fnb,"wb"); #ifdef DEBUG @@ -469,10 +498,15 @@ #ifdef DEBUG printf("writing %d bytes to file\r\n", got); #endif + Watchdog::kick(); // don't reset + SnBitUtils::WriteTo(lf, buf, got); } b += got; } // file data receive loop + + Watchdog::kick(); // don't reset + uint32_t crc=0; if (lf!=NULL) { // calculate the crc from what's actually in the file @@ -486,6 +520,9 @@ fseek(lf, 0, SEEK_SET); char c; for (int32_t i=0; i<fend; ++i) { + + Watchdog::kick(); // don't reset + SnBitUtils::ReadFrom(lf, c); if (feof(lf)==0 && ferror(lf)==0) { crc = SnCRCUtils::update_crc32_xfer(crc, c); @@ -507,6 +544,9 @@ res = kUnexpectedRec; } // otherwise it's already worse } else { + + Watchdog::kick(); // don't reset + // check that the file is ok // get the checksum res = GetHeader(timeout, buf, bsize, mcode, mlen); @@ -576,11 +616,17 @@ // if doRemove==true, will actually delete non-matching files // // return number of non-matching files + + Watchdog::kick(); // don't reset + uint16_t nSavedBins = 0; DIR* d( opendir(kLocalDir) ); struct dirent* dent; if ( d!=NULL ) { while ( (dent = readdir(d))!=NULL ) { + + Watchdog::kick(); // don't reset + const size_t flen = strlen(dent->d_name); const char* dext = dent->d_name + flen - 4; if ( strncmp(dext,".BIN",4)==0 || strncmp(dext,".bin",4)==0 ) { @@ -609,6 +655,9 @@ uint8_t& mcode, uint32_t& mlen) { SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; + + Watchdog::kick(); // don't reset + if (bsize>=SnHeaderFrame::kMaxSizeOf) { // get header const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut); @@ -636,6 +685,8 @@ printf("GetConfig, to=%u\r\n",timeOut); #endif + Watchdog::kick(); // don't reset + SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; if (bsize>=SnConfigFrame::kMaxSizeOf) { // get header @@ -690,6 +741,9 @@ // send a request demanding a handshake // as we will be waiting for a response after this, // FinishSending is also called + + Watchdog::kick(); // don't reset + char* b = genBuf; SnHeaderFrame::WriteTo(b, SnHeaderFrame::kHnShDemandCode, 0); const uint32_t bytesToBeSent = b-genBuf; @@ -706,6 +760,9 @@ SnSignalStrengthFrame& sigstr, const uint32_t timeout_clock) { // try to get the signal strength and send it if possible + + Watchdog::kick(); // don't reset + float ss; if ( fComm->CheckSignalStrength(timeout_clock, ss) ) { char* b = genBuf; @@ -739,6 +796,9 @@ #ifdef DEBUG printf("########### Send Status\r\n"); #endif + + Watchdog::kick(); // don't reset + uint8_t loseLSB=0, loseMSB=0; uint16_t wvBase=0; conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase); @@ -772,6 +832,9 @@ #ifdef DEBUG printf("status+power sent\r\n"); #endif + + Watchdog::kick(); // don't reset + // event compression parameters must be the same // as those sent in the status update b = genBuf; @@ -801,6 +864,8 @@ #ifdef DEBUG printf("SnCommWin::SendString %s\r\n",str); #endif + Watchdog::kick(); // don't reset + const bool ok = fComm->SendString(str, timeout); return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; } @@ -812,7 +877,9 @@ #ifdef DEBUG printf("SnCommWin::SendFilename %s\r\n",inf); #endif - + + Watchdog::kick(); // don't reset + // remove the directory const char* fn = strrchr(inf, '/'); if (fn==0) { @@ -848,6 +915,9 @@ // // bytesToSend will be set to the total bytes that should be delivered eventually // the number of bytes actually shipped out by SendAll is returned + + Watchdog::kick(); // don't reset + char* b = genBuf; SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize); SnBitUtils::ReadFrom(inf, b, blockSize); @@ -876,6 +946,8 @@ printf("SendFileContents (byte streaming)\r\n"); #endif + Watchdog::kick(); // don't reset + // store position in file so we can go back to it afterwards // this is probably not necessary, because if this file is open // in write-only mode, we're screwed.. @@ -898,6 +970,8 @@ // count number of events / power readings sent uint32_t evtsSent=0, powsSent=0, crc=0; + Watchdog::kick(); // don't reset + // first is the file header, which has no SnHeaderFrame msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers); bool ok = (ftell(inf)+msiz)<=fend; @@ -910,6 +984,9 @@ #ifdef DEBUG printf("sent file header. ok=%d\r\n",(int)ok); #endif + + Watchdog::kick(); // don't reset + // calc crc w/o the header block of the file header, as // this does not get stored in the file crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf+SnHeaderFrame::SizeOf(), @@ -925,6 +1002,9 @@ && (feof(inf)==0) && (ferror(inf)==0) && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) { + + Watchdog::kick(); // don't reset + // read the header for the block SnSDUtils::ReadBlockHeader(inf, hcode, hlen); fok &= (ftell(inf)+hlen)<=fend; @@ -968,6 +1048,8 @@ printf("loop done. ok=%d\r\n",(int)ok); #endif + Watchdog::kick(); // don't reset + // send the crc #ifdef DEBUG printf("sending crc (%u)\r\n",crc); @@ -984,6 +1066,7 @@ printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); #endif + Watchdog::kick(); // don't reset // send number of events sent #ifdef DEBUG @@ -1001,6 +1084,8 @@ printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); #endif + Watchdog::kick(); // don't reset + // send number of power readings sent #ifdef DEBUG printf("sending powsSent (%u)\r\n",powsSent);