Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnSDUtils.cpp
- Revision:
- 27:efc4d654b139
- Parent:
- 25:57b2627fe756
- Child:
- 30:f869ed4bcc08
--- a/SnSDUtils.cpp Tue Oct 30 06:02:32 2012 +0000 +++ b/SnSDUtils.cpp Thu Nov 01 07:00:17 2012 +0000 @@ -52,7 +52,8 @@ // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4 uint32_t sdlen(0); - const char* subdir = GetSubDirFor(run, seq, sdlen, true); + std::string subdirs( GetSubDirFor(run, seq, sdlen, true) ); + const char* subdir = subdirs.c_str(); if (sdlen<(kFNBufSize-37)) { static char tbuf[kFNBufSize]; memset(tbuf, 0, sizeof(char)*kFNBufSize); @@ -122,7 +123,8 @@ // get the run dir uint32_t rdlen(0); - const char* rdnm = GetSubDirFor(run, 0, rdlen, false); + std::string rdnms ( GetSubDirFor(run, 0, rdlen, false) ); + const char* rdnm = rdnms.c_str(); // open/make the run directory FATDirHandle* rd(dynamic_cast<FATDirHandle*>( OpenOrMakeDir(rdnm) )); struct dirent* rdent; @@ -143,7 +145,8 @@ printf("Found max seq dir num %hu for run %u\r\n",run,maxs); #endif // open up the seq dir - rdnm = GetSubDirFor(run, maxs, rdlen, true); + rdnms = GetSubDirFor(run, maxs, rdlen, true); + rdnm = rdnms.c_str(); // runXseq0 filename (fn points to a static buffer) const char* fn = GetOutFileName(macadr, run, maxs) + rdlen + 1; // take out dir and '/'s @@ -231,7 +234,8 @@ printf("got run=%d, seq=%hu\r\n",run,seq); #endif uint32_t sdlen(0); - const char* subd = GetSubDirFor(run,seq,sdlen, true); + std::string subds( GetSubDirFor(run,seq,sdlen, true) ); + const char* subd = subds.c_str(); #ifdef DEBUG printf("subd=%s\r\n",subd); #endif @@ -436,8 +440,10 @@ #ifdef DEBUG printf("try to delete %s at %p\r\n",fname,f); #endif - fclose(f); - f=0; + if (f!=0) { + fclose(f); + f=0; + } std::string fn(""); if (GetFullFilename(fname, fn)) { #ifdef DEBUG @@ -447,6 +453,78 @@ } } +void SnSDUtils::DeleteFilesOfRun(const uint32_t run) { +#ifdef DEBUG + printf("deleteing files of run %lu\r\n",run); +#endif + uint32_t rdlen(0); + std::string rdnm( GetSubDirFor(run, 0, rdlen, false) ); + DeleteAllFiles(rdnm.c_str()); +} + +void SnSDUtils::DeleteAllFiles(const char* dirname) { +#ifdef DEBUG + printf("deleting ALL files in %s\r\n",dirname); +#endif + DIR* d; + struct dirent* dent; + if ( (d = opendir( dirname ))!=NULL ) { + FATDirHandle* dir = dynamic_cast<FATDirHandle*>(d); + while ( (dent = readdir(d))!=NULL ) { + Watchdog::kick(); // don't reset + if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { + // a subdirectory + std::string dnm(dirname); + dnm += "/"; + dnm += dent->d_name; + // delete all the files in this new subdir +#ifdef DEBUG + printf("call DeleteAllFiles(%s)\r\n",dnm.c_str()); +#endif + DeleteAllFiles(dnm.c_str()); + } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { + // a data file (delete it) + const bool isCurFile = + (strcmp(dent->d_name, GetCurFileName())==0); + if (isCurFile==false) { // don't delete the current file + FILE* f(0); // dummy + DeleteFile(f, dent->d_name); + } + } + } // loop over stuff in this dir + closedir(d); + DeleteDirIfEmpty(dirname); + } +} + +void SnSDUtils::DeleteDirIfEmpty(const char* dirname) { +#ifdef DEBUG + printf("DeleteDirIfEmpty(%s)\r\n",dirname); +#endif + DIR* d; + struct dirent* dent; + if ( (d = opendir(dirname))!=NULL ) { + dent = readdir(d); + bool doDel = false; + if ( dent==NULL ) { + // then this directory is empty + static const size_t subdsz = strlen(kSDsubDir); + // just double check that this directory is + // a subdirectory of the data dir + if (strlen(dirname)>subdsz) { + doDel = true; + } + } // else this dir isn't empty + closedir(d); + if (doDel) { +#ifdef DEBUG + printf("removing directory [%s]\r\n",dirname); +#endif + remove(dirname); + } + } +} + SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, const uint32_t timeout, char* const buf, @@ -461,18 +539,19 @@ DIR* d; struct dirent* dent; - std::string delfile(""); if ( (d = opendir( dirname ))!=NULL ) { FATDirHandle* dir = dynamic_cast<FATDirHandle*>(d); while ( (dent = readdir(d))!=NULL ) { Watchdog::kick(); // don't reset + SnCommWin::ECommWinResult res = rs; if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { // a subdirectory std::string dnm(dirname); dnm += "/"; dnm += dent->d_name; // send all the files in this new subdir - SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, + res = SendAllFiles(comm, timeout, buf, bsize, + curConf, evt, pow, handshakeTimeout, dnm.c_str()); } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { // a data file (send it) @@ -493,16 +572,10 @@ f, GetCurFile(), dent->d_name); #endif uint8_t hndres = SnHeaderFrame::kHnShFailNonCode; - const SnCommWin::ECommWinResult res = - comm->SendData(f, dent->d_name, - curConf, evt, pow, buf, bsize, - 0, timeout, handshakeTimeout, - &hndres); - if (res<rs) { - rs = res; - } - // don't stop if res is bad. don't want one bad file to - // prevent sending the others + res = comm->SendData(f, dent->d_name, + curConf, evt, pow, buf, bsize, + 0, timeout, handshakeTimeout, + &hndres); #ifdef DEBUG printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n", @@ -513,40 +586,24 @@ // move (back) to the end of the file // altho hopefully no writing will happen after this fseek(fgCurFile, 0, SEEK_END); - } else if ( (res==SnCommWin::kOkWithMsg) // got handshake - && curConf.IsDeletingFiles() // want to delete - && (hndres==SnHeaderFrame::kHnShOkComplCode) ) { // whole file received - // delete it - DeleteFile(f, dent->d_name); } + } + if ((res<rs) || (res==SnCommWin::kOkStopComm)) { + rs = res; + } + // don't necessarily stop if rs is bad. don't want one bad file to + // prevent sending the others if (rs<=SnCommWin::kFailTimeout) { break; + } else if (rs==SnCommWin::kOkStopComm) { + break; } - } + } // loop over stuff in this directory closedir(d); // see if we need to remove this directory now if (curConf.IsDeletingFiles()) { - if ( (d = opendir(dirname))!=NULL ) { - dent = readdir(d); - bool doDel = false; - if ( dent==NULL ) { - // then this directory is empty - static const size_t subdsz = strlen(kSDsubDir); - // just double check that this directory is - // a subdirectory of the data dir - if (strlen(dirname)>subdsz) { - doDel = true; - } - } // else this dir isn't empty - closedir(d); - if (doDel) { -#ifdef DEBUG - printf("removing directory [%s]\r\n",dirname); -#endif - remove(dirname); - } - } + DeleteDirIfEmpty(dirname); } }