Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Diff: SnSDUtils.cpp
- Revision:
- 12:d472f9811262
- Parent:
- 10:3c93db1cfb12
- Child:
- 15:f2569d8e4176
--- a/SnSDUtils.cpp Mon Aug 13 20:44:06 2012 +0000 +++ b/SnSDUtils.cpp Sat Aug 18 05:00:32 2012 +0000 @@ -3,6 +3,7 @@ #include "mbed.h" #include "SDFileSystem.h" #include <string.h> +#include <string> #include "SnConfigFrame.h" #include "SnEventFrame.h" @@ -11,6 +12,7 @@ const char* const SnSDUtils::kSDsubDir = "/sd"; char SnSDUtils::fgCurFileName[kFNBufSize]={0}; FILE* SnSDUtils::fgCurFile = 0; +uint16_t SnSDUtils::fgCurSeq = 0; const uint8_t SnSDUtils::kIOvers = 3; const uint32_t SnSDUtils::kMaxSizeOfFileHdr = sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t) @@ -43,11 +45,11 @@ return NULL; } } - +/* uint16_t SnSDUtils::GetCurSeqNum() { return GetSeqNumFromFileName(fgCurFileName); } - +*/ uint16_t SnSDUtils::GetSeqNumFromFileName(const char* fn) { uint16_t seq=0; const uint32_t ncomp = strrchr(fn, 's') - fn; @@ -76,10 +78,12 @@ // allow for deleted files to make gaps. // search for highest seq number and increase that sscanf((dent->d_name)+ncomp,"s%hu.dat",&seq); +#ifdef DEBUG /* printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", dent->d_name, seq, __kMaxUShort); */ +#endif if (seq==__kMaxUShort) { newseq = seq; break; @@ -102,15 +106,30 @@ FILE* f = 0; if (name!=NULL) { f = OpenSDFile(name, "rb"); + /* if (setcurrent) { fgCurFile = f; + strncpy(fgCurFileName, name, kFNBufSize-1); + fgCurSeq = GetSeqNumFromFileName(fgCurFileName); } + */ } return f; } FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode) { - FILE* f = fopen(name, mode); + // TODO: check if we have memory? + std::string fn(name); + if (strncmp(name, kSDsubDir, strlen(kSDsubDir))!=0) { + // filename lacks directory + fn = kSDsubDir; + fn += "/"; + fn += name; + } +#ifdef DEBUG + printf("OpenSDFile: %s, mode %s\r\n",fn.c_str(),mode); +#endif + FILE* f = fopen(fn.c_str(), mode); //setvbuf(f, 0, _IONBF, 0); // no buffering return f; } @@ -120,15 +139,15 @@ // opens a new file in the specified directory and writes this // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes) // - const uint16_t seq = GetSeqNum(macadr, run); + fgCurSeq = GetSeqNum(macadr, run); memset(fgCurFileName, 0, sizeof(char)*kFNBufSize); - strncpy(fgCurFileName,GetOutFileName(macadr, run, seq),kFNBufSize-1); - //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,seq); + strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1); + //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); fgCurFile = 0; if (fgCurFileName!=NULL) { fgCurFile = OpenSDFile(fgCurFileName, "wb"); if (fgCurFile!=NULL && ferror(fgCurFile)==0) { - WriteFileHeader(fgCurFile, macadr, run, seq); + WriteFileHeader(fgCurFile, macadr, run, fgCurSeq); } } return fgCurFile; @@ -164,19 +183,21 @@ } SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, - const bool doDelete, const uint32_t timeout, char* const buf, const uint32_t bsize, const SnConfigFrame& curConf, SnEventFrame& evt, - SnPowerFrame& pow) { + SnPowerFrame& pow, + const uint32_t handshakeTimeout) { DIR* d; struct dirent* dent; SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail; + const bool doDelete = curConf.IsDeletingFiles(); + if ( (d = opendir( kSDsubDir ))!=NULL ) { FILE* f; while ( (dent = readdir(d))!=NULL ) { @@ -189,9 +210,15 @@ } else { f = OpenExistingFile(dent->d_name, false); } +#ifdef DEBUG + printf("calling senddata: f=%p (cur %p), fn=%s\r\n", + f, GetCurFile(), dent->d_name); +#endif rs = comm->SendData(f, dent->d_name, curConf, evt, pow, buf, bsize, - 0, timeout, 0); + 0, timeout, handshakeTimeout); + // send data should close or delete the file (if appropriate) + // unless it's the current file if (isCurFile) { // move (back) to the end of the file // altho hopefully no writing will happen after this