![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnSDUtils.cpp
- Revision:
- 103:0ea896a0953a
- Parent:
- 84:80b15993944e
- Child:
- 122:c1b5023eac69
--- a/SnSDUtils.cpp Tue Nov 17 23:41:07 2015 +0000 +++ b/SnSDUtils.cpp Sun Nov 22 04:28:56 2015 +0000 @@ -57,14 +57,25 @@ Watchdog::kick(); // don't reset static const uint16_t tmplen = strlen(kSDsubDir)+50; static char* tmpsd = new char[tmplen]; - slen = snprintf(tmpsd, tmplen, "%s/r%05ld", kSDsubDir, run); - if (useSeq) { - slen += snprintf(tmpsd+slen, tmplen-slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ); + if (tmpsd==NULL) { +#ifdef DEBUG + printf("Could not create temp subdir string " + "memory of len %hu\r\n", tmplen); +#endif + // bad things -- can't use SD card + SetSDCardInitTo(false); + slen = 0; + return NULL; + } else { + slen = snprintf(tmpsd, tmplen, "%s/r%05ld", kSDsubDir, run); + if (useSeq) { + slen += snprintf(tmpsd+slen, tmplen-slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ); + } + if (slen > tmplen) { + slen = tmplen; + } + return tmpsd; } - if (slen > tmplen) { - slen = tmplen; - } - return tmpsd; } const char* SnSDUtils::GetOutFileName(const uint64_t macadr, @@ -82,7 +93,7 @@ uint32_t sdlen(0); std::string subdirs( GetSubDirFor(run, seq, sdlen, true) ); const char* subdir = subdirs.c_str(); - if (sdlen<(kFNBufSize-37)) { + if ((sdlen>0) && (sdlen<(kFNBufSize-37))) { static char tbuf[kFNBufSize]; memset(tbuf, 0, sizeof(char)*kFNBufSize); // if file name format changes, GetSeqNum must be changed too @@ -186,8 +197,18 @@ rd = opendir(dirname); } #ifdef DEBUG - printf("returning rd=%p\r\n",(void*)rd); + printf("returning rd=%p\r\n",(void*)rd); #endif + + if (rd==0) { +#ifdef DEBUG + printf("unable to get or make dir." + "set SD init to false\r\n"); +#endif + // SD no good -- pretend init failed + SetSDCardInitTo(false); + } + return rd; } else { return 0; @@ -209,93 +230,107 @@ // get the run dir uint32_t rdlen(0); std::string rdnms ( GetSubDirFor(run, 0, rdlen, false) ); - const char* rdnm = rdnms.c_str(); - // open/make the run directory - FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) )); - struct dirent* rdent; - uint16_t dseq(0); -#ifdef DEBUG - printf("starting readdir loop over %p\r\n",(void*)rd); -#endif - while ( (rdent = readdir(rd))!=NULL ) { -#ifdef DEBUG - printf("rdent = %p\r\n",(void*)rdent); -#endif - if ((rd->filinfo()->fattrib & AM_DIR)!=0) { + if (rdlen==0) { + // failed to get subdir + maxs = kBadSeqNum; + } else { + const char* rdnm = rdnms.c_str(); + // open/make the run directory + FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) )); + struct dirent* rdent; + uint16_t dseq(0); #ifdef DEBUG - printf("is a dir\r\n"); -#endif - // is a directory - const int ncm = sscanf(rdent->d_name, "s%hu", &dseq); - if (ncm==1) { -#ifdef DEBUG - printf("dseq=%hu, maxs=%hu\r\n",dseq,maxs); -#endif - if ( (dseq>maxs) && (dseq<kMaxSeqNum) ) { - maxs = dseq; - } - } - } -#ifdef DEBUG - else { - printf("not a dir\r\n"); - } -#endif - } -#ifdef DEBUG - printf("closing directory %p\r\n",(void*)rd); + printf("starting readdir loop over %p\r\n",(void*)rd); #endif - closedir(rd); + while ( (rd!=0) && ((rdent = readdir(rd))!=NULL) ) { +#ifdef DEBUG + printf("rdent = %p\r\n",(void*)rdent); +#endif + if ((rd->filinfo()->fattrib & AM_DIR)!=0) { #ifdef DEBUG - printf("Found max seq dir num %hu for run %u\r\n",maxs,run); -#endif - - Watchdog::kick(); // don't reset - - // open up the seq dir - 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 - // don't compare seq#. don't use num of chars in case seq is >999 - const int32_t ncomp = strrchr(fn, 's') - fn; - // open (or make) the run/seq dir - rd = static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) ); - // get the new sequence number (ok if it overflows this seq "bin") - maxs=0; - while ( (rdent = readdir(rd))!=NULL ) { - Watchdog::kick(); // don't reset - if ((rd->filinfo()->fattrib & AM_DIR)==0) { - // is a file. - // don't just count files, in case one seq was - // transferred and erased in the middle of a run - if (strncmp(rdent->d_name, fn, ncomp)==0) { - // allow for deleted files to make gaps. - // search for highest seq number and increase that - if (sscanf((rdent->d_name)+ncomp,"s%hu.dat",&dseq)==1) { -#ifdef DEBUG - printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", - rdent->d_name, dseq, __kMaxUShort); -#endif - if (dseq==__kMaxUShort) { - maxs = dseq; - break; - } + printf("is a dir\r\n"); +#endif + // is a directory + const int ncm = sscanf(rdent->d_name, "s%hu", &dseq); + if (ncm==1) { #ifdef DEBUG printf("dseq=%hu, maxs=%hu\r\n",dseq,maxs); #endif - if ( (dseq>=maxs) && (dseq<kMaxSeqNum)) { - maxs=dseq+1; - } - if (maxs==__kMaxUShort) { - break; + if ( (dseq>maxs) && (dseq<kMaxSeqNum) ) { + maxs = dseq; } } } +#ifdef DEBUG + else { + printf("not a dir\r\n"); + } +#endif + } + if (rd!=0) { +#ifdef DEBUG + printf("closing directory %p\r\n",(void*)rd); +#endif + closedir(rd); + } +#ifdef DEBUG + printf("Found max seq dir num %hu for run %u\r\n",maxs,run); +#endif + + Watchdog::kick(); // don't reset + + // open up the seq dir + rdnms = GetSubDirFor(run, maxs, rdlen, true); + rdnm = rdnms.c_str(); + // runXseq0 filename (fn points to a static buffer) + const char* outfilenm = GetOutFileName(macadr, run, maxs); + if (outfilenm!=NULL) { + const char* fn = outfilenm + rdlen + 1; // take out dir and '/'s + // don't compare seq#. don't use num of chars in case seq is >999 + const int32_t ncomp = strrchr(fn, 's') - fn; + // open (or make) the run/seq dir + rd = static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) ); + // get the new sequence number (ok if it overflows this seq "bin") + maxs=0; + while ( (rd!=0) && ((rdent = readdir(rd))!=NULL) ) { + Watchdog::kick(); // don't reset + if ((rd->filinfo()->fattrib & AM_DIR)==0) { + // is a file. + // don't just count files, in case one seq was + // transferred and erased in the middle of a run + if (strncmp(rdent->d_name, fn, ncomp)==0) { + // allow for deleted files to make gaps. + // search for highest seq number and increase that + if (sscanf((rdent->d_name)+ncomp,"s%hu.dat",&dseq)==1) { + #ifdef DEBUG + printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", + rdent->d_name, dseq, __kMaxUShort); + #endif + if (dseq==__kMaxUShort) { + maxs = dseq; + break; + } + #ifdef DEBUG + printf("dseq=%hu, maxs=%hu\r\n",dseq,maxs); + #endif + if ( (dseq>=maxs) && (dseq<kMaxSeqNum)) { + maxs=dseq+1; + } + if (maxs==__kMaxUShort) { + break; + } + } + } + } + } + if (rd!=0) { + closedir(rd); + } + } else { + // failed to get subdir + maxs = kBadSeqNum; } } - closedir(rd); } else { // no SD card @@ -353,13 +388,15 @@ #endif uint32_t sdlen(0); std::string subds( GetSubDirFor(run,seq,sdlen, true) ); - const char* subd = subds.c_str(); + if (sdlen!=0) { + const char* subd = subds.c_str(); #ifdef DEBUG - printf("subd=%s\r\n",subd); + printf("subd=%s\r\n",subd); #endif - ffn = subd; - ffn += "/"; - ret = true; + ffn = subd; + ffn += "/"; + ret = true; + } // else failed to make subdir } ffn += fn; #ifdef DEBUG @@ -404,7 +441,7 @@ std::string dn(name, ld-name); DIR* d = OpenOrMakeAllDirs(dn.c_str()); #ifdef DEBUG - printf("d=%p\r\n",d); + printf("d=%p\r\n",d); #endif if (d!=NULL) { closedir(d); @@ -455,8 +492,11 @@ printf("getting output file name\r\n"); #endif memset(fgCurFileName, 0, sizeof(char)*kFNBufSize); - strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1); - //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); + const char* outfilenm = GetOutFileName(macadr, run, fgCurSeq); + if (outfilenm!=NULL) { + strncpy(fgCurFileName,outfilenm,kFNBufSize-1); + //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); + } #ifdef DEBUG printf("fgCurFileName=%s\r\n",fgCurFileName); #endif @@ -503,7 +543,9 @@ PrintFilesInDirs(dnm.c_str()); } } - closedir(d); + if (d!=0) { + closedir(d); + } } } } @@ -516,17 +558,19 @@ FATFS* fs; DWORD fre_clust; f_getfree("0:",&fre_clust,&fs); - frs = static_cast<float>(fs->csize) - *static_cast<float>(fs->free_clust) + if (fs!=0) { + frs = static_cast<float>(fs->csize) + *static_cast<float>(fs->free_clust) #if _MAX_SS != 512 - *(fs->ssize); + *(fs->ssize); #else - *512; + *512; #endif #ifdef DEBUG - printf("free space = %g b (%g GB, %g MB, %g KB)\r\n", - frs, frs/1073741824.0, frs/1048576.0, frs/1024.0); + printf("free space = %g b (%g GB, %g MB, %g KB)\r\n", + frs, frs/1073741824.0, frs/1048576.0, frs/1024.0); #endif + } } return frs; } @@ -557,10 +601,14 @@ } else { // a file ++nfiles; - totbytes += d->filinfo()->fsize; + if (d->filinfo()!=0) { + totbytes += d->filinfo()->fsize; + } } } - closedir(d); + if (d!=0) { + closedir(d); + } } } #ifdef DEBUG @@ -667,7 +715,14 @@ Watchdog::kick(); // don't reset uint32_t rdlen(0); std::string rdnm( GetSubDirFor(run, 0, rdlen, false) ); - DeleteAllFiles(rdnm.c_str()); + if (rdlen!=0) { + DeleteAllFiles(rdnm.c_str()); + } +#ifdef DEBUG + else { + printf("could not get subdir -- not deleting files\r\n"); + } +#endif } void SnSDUtils::DeleteAllFiles(const char* dirname) { @@ -681,29 +736,33 @@ struct dirent* dent; if ( (d = opendir( dirname ))!=NULL ) { FATDirHandle* dir = static_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 + if (dir!=0) { + 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()); + 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); + 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); + } // loop over stuff in this dir + } + if (d!=0) { + closedir(d); + } DeleteDirIfEmpty(dirname); } } @@ -730,7 +789,9 @@ doDel = true; } } // else this dir isn't empty - closedir(d); + if (d!=0) { + closedir(d); + } if (doDel) { #ifdef DEBUG printf("removing directory [%s]\r\n",dirname); @@ -873,14 +934,16 @@ #ifdef DEBUG printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); #endif - if ( DeleteDirIfEmpty(rdnm.c_str()) ) { + if ( (rdlen!=0) && DeleteDirIfEmpty(rdnm.c_str()) ) { // removed the seq dir. do we need to remove // the run dir too? rdnm = GetSubDirFor(run, seq, rdlen, false); #ifdef DEBUG printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); #endif - DeleteDirIfEmpty(rdnm.c_str()); + if (rdlen!=0) { + DeleteDirIfEmpty(rdnm.c_str()); + } } } @@ -1031,8 +1094,15 @@ // get the run dir uint32_t rdlen(0); std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) ); - return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, - rdnms.c_str()); + if (rdlen!=0) { + return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, + rdnms.c_str()); + } else { +#ifdef DEBUG + printf("canont sent files - coul dot open subdir\r\n"); +#endif + return SnCommWin::kUndefFail; + } } SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, @@ -1057,35 +1127,39 @@ struct dirent* dent; if ( (d = opendir( dirname ))!=NULL ) { FATDirHandle* dir = static_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 - res = SendAllFiles(comm, timeout, buf, bsize, - curConf, evt, pow, - dnm.c_str()); - } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { - // a data file (send it) - res = SendOneFile(dent->d_name, true, comm, timeout, buf, bsize, - curConf, evt, pow); - } - 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); + if (dir!=0) { + 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 + res = SendAllFiles(comm, timeout, buf, bsize, + curConf, evt, pow, + dnm.c_str()); + } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { + // a data file (send it) + res = SendOneFile(dent->d_name, true, comm, timeout, buf, bsize, + curConf, evt, pow); + } + 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 + } + if (d!=0) { + closedir(d); + } // see if we need to remove this directory now if (curConf.IsDeletingFiles()) { DeleteDirIfEmpty(dirname);