Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.cpp@40:1324da35afd4, 2013-10-05 (annotated)
- Committer:
- uci1
- Date:
- Sat Oct 05 04:45:22 2013 +0000
- Revision:
- 40:1324da35afd4
- Parent:
- 39:2f17131d22a5
- Child:
- 47:fbe956b10a91
first commit of major overhaul to 2013-2014 mbed code. NOT YET FULLY TESTED. too many changes to list (fix local file receive, fix rates, external comm packes, big SD cards, get to comm win w/o SD, v8 config frame, v4 files, SBD buffering changes...)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | #include "SnSDUtils.h" |
uci1 | 0:664899e0b988 | 2 | |
uci1 | 0:664899e0b988 | 3 | #include "mbed.h" |
uci1 | 0:664899e0b988 | 4 | #include "SDFileSystem.h" |
uci1 | 21:ce51bb0ba4a5 | 5 | #include "FATDirHandle.h" |
uci1 | 0:664899e0b988 | 6 | #include <string.h> |
uci1 | 12:d472f9811262 | 7 | #include <string> |
uci1 | 0:664899e0b988 | 8 | |
uci1 | 0:664899e0b988 | 9 | #include "SnConfigFrame.h" |
uci1 | 0:664899e0b988 | 10 | #include "SnEventFrame.h" |
uci1 | 22:f957c4f840ad | 11 | #include "SnHeartbeatFrame.h" |
uci1 | 40:1324da35afd4 | 12 | #include "SnClockSetFrame.h" |
uci1 | 0:664899e0b988 | 13 | |
uci1 | 25:57b2627fe756 | 14 | #include "Watchdog.h" |
uci1 | 25:57b2627fe756 | 15 | |
uci1 | 15:f2569d8e4176 | 16 | //#define DEBUG |
uci1 | 0:664899e0b988 | 17 | |
uci1 | 25:57b2627fe756 | 18 | #define SUBDIRSEQ 100 // make a new subdir every X sequences |
uci1 | 25:57b2627fe756 | 19 | |
uci1 | 19:74155d652c37 | 20 | const char* const SnSDUtils::kSDdir = "/sd"; |
uci1 | 19:74155d652c37 | 21 | const char* const SnSDUtils::kSDsubDir = "/sd/data"; |
uci1 | 2:e67f7c158087 | 22 | char SnSDUtils::fgCurFileName[kFNBufSize]={0}; |
uci1 | 1:e392595b4b76 | 23 | FILE* SnSDUtils::fgCurFile = 0; |
uci1 | 12:d472f9811262 | 24 | uint16_t SnSDUtils::fgCurSeq = 0; |
uci1 | 40:1324da35afd4 | 25 | bool SnSDUtils::fgNeedToInit = true; |
uci1 | 40:1324da35afd4 | 26 | const uint8_t SnSDUtils::kIOvers = 4; |
uci1 | 5:9cea89700c66 | 27 | const uint32_t SnSDUtils::kMaxSizeOfFileHdr = |
uci1 | 5:9cea89700c66 | 28 | sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t) |
uci1 | 8:95a325df1f6b | 29 | +(sizeof(uint8_t)+(2u*sizeof(uint16_t))); // power frame v1 |
uci1 | 4:a91682e19d6b | 30 | |
uci1 | 40:1324da35afd4 | 31 | SnSDUtils::InitSDFcn SnSDUtils::fgDoInit = 0; |
uci1 | 40:1324da35afd4 | 32 | |
uci1 | 4:a91682e19d6b | 33 | static const uint16_t __kMaxUShort = ~0; |
uci1 | 0:664899e0b988 | 34 | |
uci1 | 40:1324da35afd4 | 35 | void SnSDUtils::InitSDCard(const bool force) { |
uci1 | 40:1324da35afd4 | 36 | if ((fgNeedToInit || force) && (fgDoInit!=0)) { |
uci1 | 40:1324da35afd4 | 37 | (*fgDoInit)(); |
uci1 | 40:1324da35afd4 | 38 | fgNeedToInit = false; |
uci1 | 40:1324da35afd4 | 39 | } |
uci1 | 40:1324da35afd4 | 40 | } |
uci1 | 40:1324da35afd4 | 41 | |
uci1 | 25:57b2627fe756 | 42 | const char* SnSDUtils::GetSubDirFor(const uint32_t run, const uint16_t seq, |
uci1 | 25:57b2627fe756 | 43 | uint32_t& slen, const bool useSeq) { |
uci1 | 40:1324da35afd4 | 44 | // returns a STATIC string! (so make a copy of it before use) |
uci1 | 25:57b2627fe756 | 45 | // sets slen to the length of this string (same as strlen) |
uci1 | 40:1324da35afd4 | 46 | static const uint16_t tmplen = strlen(kSDsubDir)+50; |
uci1 | 40:1324da35afd4 | 47 | static char* tmpsd = new char[tmplen]; |
uci1 | 40:1324da35afd4 | 48 | slen = snprintf(tmpsd, tmplen, "%s/r%05ld", kSDsubDir, run); |
uci1 | 25:57b2627fe756 | 49 | if (useSeq) { |
uci1 | 40:1324da35afd4 | 50 | slen += snprintf(tmpsd+slen, tmplen-slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ); |
uci1 | 40:1324da35afd4 | 51 | } |
uci1 | 40:1324da35afd4 | 52 | if (slen > tmplen) { |
uci1 | 40:1324da35afd4 | 53 | slen = tmplen; |
uci1 | 25:57b2627fe756 | 54 | } |
uci1 | 25:57b2627fe756 | 55 | return tmpsd; |
uci1 | 25:57b2627fe756 | 56 | } |
uci1 | 25:57b2627fe756 | 57 | |
uci1 | 0:664899e0b988 | 58 | const char* SnSDUtils::GetOutFileName(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 59 | const uint32_t run, |
uci1 | 0:664899e0b988 | 60 | const uint16_t seq) { |
uci1 | 0:664899e0b988 | 61 | // returns the formatted file name, or NULL if the directory is too long |
uci1 | 0:664899e0b988 | 62 | // and the full name cannot fit in the buffer |
uci1 | 0:664899e0b988 | 63 | // NOTE: this fcn uses a static buffer, and so should not be called |
uci1 | 0:664899e0b988 | 64 | // multiple times in the same line (that includes the calling of functions |
uci1 | 0:664899e0b988 | 65 | // that call this function!) |
uci1 | 0:664899e0b988 | 66 | // |
uci1 | 0:664899e0b988 | 67 | // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat |
uci1 | 0:664899e0b988 | 68 | // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4 |
uci1 | 25:57b2627fe756 | 69 | uint32_t sdlen(0); |
uci1 | 27:efc4d654b139 | 70 | std::string subdirs( GetSubDirFor(run, seq, sdlen, true) ); |
uci1 | 27:efc4d654b139 | 71 | const char* subdir = subdirs.c_str(); |
uci1 | 25:57b2627fe756 | 72 | if (sdlen<(kFNBufSize-37)) { |
uci1 | 0:664899e0b988 | 73 | static char tbuf[kFNBufSize]; |
uci1 | 0:664899e0b988 | 74 | memset(tbuf, 0, sizeof(char)*kFNBufSize); |
uci1 | 0:664899e0b988 | 75 | // if file name format changes, GetSeqNum must be changed too |
uci1 | 25:57b2627fe756 | 76 | sprintf(tbuf, "%s/SnEvtsM%012llXr%05lds%05d.dat", |
uci1 | 25:57b2627fe756 | 77 | subdir, |
uci1 | 0:664899e0b988 | 78 | macadr>>16, // 64 -> 48 bits |
uci1 | 0:664899e0b988 | 79 | run, seq); |
uci1 | 0:664899e0b988 | 80 | return tbuf; |
uci1 | 0:664899e0b988 | 81 | } else { |
uci1 | 0:664899e0b988 | 82 | return NULL; |
uci1 | 0:664899e0b988 | 83 | } |
uci1 | 0:664899e0b988 | 84 | } |
uci1 | 25:57b2627fe756 | 85 | |
uci1 | 25:57b2627fe756 | 86 | bool SnSDUtils::GetRunSeqFromFilename(const char* fn, |
uci1 | 25:57b2627fe756 | 87 | uint32_t& run, |
uci1 | 25:57b2627fe756 | 88 | uint16_t& seq) { |
uci1 | 25:57b2627fe756 | 89 | bool ret = false; |
uci1 | 25:57b2627fe756 | 90 | const int32_t ncomp = strrchr(fn, 'r') - fn; |
uci1 | 25:57b2627fe756 | 91 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 92 | printf("fn=%s, ncomp=%d\r\n",fn,ncomp); |
uci1 | 25:57b2627fe756 | 93 | #endif |
uci1 | 25:57b2627fe756 | 94 | if ((ncomp<strlen(fn)) && (ncomp>0)) { |
uci1 | 25:57b2627fe756 | 95 | if (sscanf(fn+ncomp,"r%lus%hu.dat",&run,&seq)==2) { |
uci1 | 25:57b2627fe756 | 96 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 97 | printf("run=%u, seq=%hu\r\n",run,seq); |
uci1 | 25:57b2627fe756 | 98 | #endif |
uci1 | 25:57b2627fe756 | 99 | ret = true; |
uci1 | 25:57b2627fe756 | 100 | } |
uci1 | 25:57b2627fe756 | 101 | } |
uci1 | 25:57b2627fe756 | 102 | return ret; |
uci1 | 25:57b2627fe756 | 103 | } |
uci1 | 25:57b2627fe756 | 104 | /* |
uci1 | 10:3c93db1cfb12 | 105 | uint16_t SnSDUtils::GetSeqNumFromFileName(const char* fn) { |
uci1 | 10:3c93db1cfb12 | 106 | uint16_t seq=0; |
uci1 | 10:3c93db1cfb12 | 107 | const uint32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 10:3c93db1cfb12 | 108 | if (ncomp<strlen(fn)) { |
uci1 | 10:3c93db1cfb12 | 109 | sscanf(fn+ncomp,"s%hu.dat",&seq); |
uci1 | 10:3c93db1cfb12 | 110 | } |
uci1 | 10:3c93db1cfb12 | 111 | return seq; |
uci1 | 10:3c93db1cfb12 | 112 | } |
uci1 | 25:57b2627fe756 | 113 | */ |
uci1 | 25:57b2627fe756 | 114 | DIR* SnSDUtils::OpenOrMakeDir(const char* dirname) { |
uci1 | 25:57b2627fe756 | 115 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 116 | printf("open dir %s\r\n",dirname); |
uci1 | 25:57b2627fe756 | 117 | #endif |
uci1 | 40:1324da35afd4 | 118 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 119 | |
uci1 | 25:57b2627fe756 | 120 | DIR* rd( opendir(dirname) ); |
uci1 | 25:57b2627fe756 | 121 | if (rd==NULL) { |
uci1 | 25:57b2627fe756 | 122 | // try making the directory |
uci1 | 25:57b2627fe756 | 123 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 124 | printf("making dir %s\r\n",dirname); |
uci1 | 25:57b2627fe756 | 125 | #endif |
uci1 | 25:57b2627fe756 | 126 | mkdir(dirname, 0777); |
uci1 | 36:87865913ae6f | 127 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 128 | printf("opening dir %s\r\n",dirname); |
uci1 | 36:87865913ae6f | 129 | #endif |
uci1 | 25:57b2627fe756 | 130 | rd = opendir(dirname); |
uci1 | 25:57b2627fe756 | 131 | } |
uci1 | 36:87865913ae6f | 132 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 133 | printf("returning rd=%p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 134 | #endif |
uci1 | 25:57b2627fe756 | 135 | return rd; |
uci1 | 25:57b2627fe756 | 136 | } |
uci1 | 10:3c93db1cfb12 | 137 | |
uci1 | 0:664899e0b988 | 138 | uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 139 | const uint32_t run) { |
uci1 | 0:664899e0b988 | 140 | // count the files having expected filename format |
uci1 | 40:1324da35afd4 | 141 | InitSDCard(); |
uci1 | 0:664899e0b988 | 142 | |
uci1 | 25:57b2627fe756 | 143 | // get the run dir |
uci1 | 25:57b2627fe756 | 144 | uint32_t rdlen(0); |
uci1 | 27:efc4d654b139 | 145 | std::string rdnms ( GetSubDirFor(run, 0, rdlen, false) ); |
uci1 | 27:efc4d654b139 | 146 | const char* rdnm = rdnms.c_str(); |
uci1 | 25:57b2627fe756 | 147 | // open/make the run directory |
uci1 | 39:2f17131d22a5 | 148 | FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeDir(rdnm) )); |
uci1 | 25:57b2627fe756 | 149 | struct dirent* rdent; |
uci1 | 25:57b2627fe756 | 150 | uint16_t dseq(0), maxs(0); |
uci1 | 36:87865913ae6f | 151 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 152 | printf("starting readdir loop over %p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 153 | #endif |
uci1 | 25:57b2627fe756 | 154 | while ( (rdent = readdir(rd))!=NULL ) { |
uci1 | 36:87865913ae6f | 155 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 156 | printf("rdent = %p\r\n",(void*)rdent); |
uci1 | 36:87865913ae6f | 157 | #endif |
uci1 | 25:57b2627fe756 | 158 | if ((rd->filinfo()->fattrib & AM_DIR)!=0) { |
uci1 | 36:87865913ae6f | 159 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 160 | printf("is a dir\r\n"); |
uci1 | 36:87865913ae6f | 161 | #endif |
uci1 | 25:57b2627fe756 | 162 | // is a directory |
uci1 | 25:57b2627fe756 | 163 | const int ncm = sscanf(rdent->d_name, "s%hu", &dseq); |
uci1 | 25:57b2627fe756 | 164 | if (ncm==1) { |
uci1 | 25:57b2627fe756 | 165 | if (dseq>maxs) { |
uci1 | 25:57b2627fe756 | 166 | maxs = dseq; |
uci1 | 4:a91682e19d6b | 167 | } |
uci1 | 0:664899e0b988 | 168 | } |
uci1 | 0:664899e0b988 | 169 | } |
uci1 | 36:87865913ae6f | 170 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 171 | else { |
uci1 | 36:87865913ae6f | 172 | printf("not a dir\r\n"); |
uci1 | 36:87865913ae6f | 173 | } |
uci1 | 36:87865913ae6f | 174 | #endif |
uci1 | 0:664899e0b988 | 175 | } |
uci1 | 36:87865913ae6f | 176 | #ifdef DEBUG |
uci1 | 36:87865913ae6f | 177 | printf("closing directory %p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 178 | #endif |
uci1 | 25:57b2627fe756 | 179 | closedir(rd); |
uci1 | 25:57b2627fe756 | 180 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 181 | printf("Found max seq dir num %hu for run %u\r\n",maxs,run); |
uci1 | 25:57b2627fe756 | 182 | #endif |
uci1 | 25:57b2627fe756 | 183 | // open up the seq dir |
uci1 | 27:efc4d654b139 | 184 | rdnms = GetSubDirFor(run, maxs, rdlen, true); |
uci1 | 27:efc4d654b139 | 185 | rdnm = rdnms.c_str(); |
uci1 | 25:57b2627fe756 | 186 | // runXseq0 filename (fn points to a static buffer) |
uci1 | 25:57b2627fe756 | 187 | const char* fn = GetOutFileName(macadr, run, maxs) |
uci1 | 25:57b2627fe756 | 188 | + rdlen + 1; // take out dir and '/'s |
uci1 | 25:57b2627fe756 | 189 | // don't compare seq#. don't use num of chars in case seq is >999 |
uci1 | 25:57b2627fe756 | 190 | const int32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 25:57b2627fe756 | 191 | // open (or make) the run/seq dir |
uci1 | 39:2f17131d22a5 | 192 | rd = static_cast<FATDirHandle*>( OpenOrMakeDir(rdnm) ); |
uci1 | 25:57b2627fe756 | 193 | // get the new sequence number (ok if it overflows this seq "bin") |
uci1 | 25:57b2627fe756 | 194 | maxs=0; |
uci1 | 25:57b2627fe756 | 195 | while ( (rdent = readdir(rd))!=NULL ) { |
uci1 | 25:57b2627fe756 | 196 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 197 | if ((rd->filinfo()->fattrib & AM_DIR)==0) { |
uci1 | 25:57b2627fe756 | 198 | // is a file. |
uci1 | 25:57b2627fe756 | 199 | // don't just count files, in case one seq was |
uci1 | 25:57b2627fe756 | 200 | // transferred and erased in the middle of a run |
uci1 | 25:57b2627fe756 | 201 | if (strncmp(rdent->d_name, fn, ncomp)==0) { |
uci1 | 25:57b2627fe756 | 202 | // allow for deleted files to make gaps. |
uci1 | 25:57b2627fe756 | 203 | // search for highest seq number and increase that |
uci1 | 25:57b2627fe756 | 204 | if (sscanf((rdent->d_name)+ncomp,"s%hu.dat",&dseq)==1) { |
uci1 | 25:57b2627fe756 | 205 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 206 | printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", |
uci1 | 25:57b2627fe756 | 207 | rdent->d_name, dseq, __kMaxUShort); |
uci1 | 25:57b2627fe756 | 208 | #endif |
uci1 | 25:57b2627fe756 | 209 | if (dseq==__kMaxUShort) { |
uci1 | 25:57b2627fe756 | 210 | maxs = dseq; |
uci1 | 25:57b2627fe756 | 211 | break; |
uci1 | 25:57b2627fe756 | 212 | } |
uci1 | 25:57b2627fe756 | 213 | if (dseq>=maxs) { |
uci1 | 25:57b2627fe756 | 214 | maxs=dseq+1; |
uci1 | 25:57b2627fe756 | 215 | } |
uci1 | 25:57b2627fe756 | 216 | if (maxs==__kMaxUShort) { |
uci1 | 25:57b2627fe756 | 217 | break; |
uci1 | 25:57b2627fe756 | 218 | } |
uci1 | 25:57b2627fe756 | 219 | } |
uci1 | 25:57b2627fe756 | 220 | } |
uci1 | 25:57b2627fe756 | 221 | } |
uci1 | 25:57b2627fe756 | 222 | } |
uci1 | 25:57b2627fe756 | 223 | closedir(rd); |
uci1 | 7:079617408fec | 224 | |
uci1 | 21:ce51bb0ba4a5 | 225 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 226 | printf("return maxs=%hu\r\n",maxs); |
uci1 | 21:ce51bb0ba4a5 | 227 | #endif |
uci1 | 25:57b2627fe756 | 228 | return maxs; |
uci1 | 0:664899e0b988 | 229 | } |
uci1 | 0:664899e0b988 | 230 | |
uci1 | 25:57b2627fe756 | 231 | FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent, |
uci1 | 25:57b2627fe756 | 232 | const bool redoDir) { |
uci1 | 40:1324da35afd4 | 233 | InitSDCard(); |
uci1 | 0:664899e0b988 | 234 | FILE* f = 0; |
uci1 | 25:57b2627fe756 | 235 | //if ((name!=NULL) && ((*name)!=0) ) { // simple check if filename not set |
uci1 | 25:57b2627fe756 | 236 | if (name!=NULL) { // simple check if filename not set |
uci1 | 25:57b2627fe756 | 237 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 238 | printf("opening SD file. name=[%s]\r\n",name); |
uci1 | 25:57b2627fe756 | 239 | #endif |
uci1 | 25:57b2627fe756 | 240 | f = OpenSDFile(name, "rb", redoDir); |
uci1 | 12:d472f9811262 | 241 | /* |
uci1 | 3:24c5f0f50bf1 | 242 | if (setcurrent) { |
uci1 | 3:24c5f0f50bf1 | 243 | fgCurFile = f; |
uci1 | 12:d472f9811262 | 244 | strncpy(fgCurFileName, name, kFNBufSize-1); |
uci1 | 12:d472f9811262 | 245 | fgCurSeq = GetSeqNumFromFileName(fgCurFileName); |
uci1 | 3:24c5f0f50bf1 | 246 | } |
uci1 | 12:d472f9811262 | 247 | */ |
uci1 | 0:664899e0b988 | 248 | } |
uci1 | 0:664899e0b988 | 249 | return f; |
uci1 | 0:664899e0b988 | 250 | } |
uci1 | 0:664899e0b988 | 251 | |
uci1 | 25:57b2627fe756 | 252 | bool SnSDUtils::GetFullFilename(const char* name, std::string& ffn) { |
uci1 | 25:57b2627fe756 | 253 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 254 | printf("GetFullFilename (%s)\r\n",name); |
uci1 | 25:57b2627fe756 | 255 | #endif |
uci1 | 25:57b2627fe756 | 256 | bool ret = false; |
uci1 | 25:57b2627fe756 | 257 | uint32_t run(0); |
uci1 | 25:57b2627fe756 | 258 | uint16_t seq(0); |
uci1 | 25:57b2627fe756 | 259 | const char* fn = strrchr(name, '/'); |
uci1 | 25:57b2627fe756 | 260 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 261 | printf("w/o / : %s\r\n",fn); |
uci1 | 25:57b2627fe756 | 262 | #endif |
uci1 | 25:57b2627fe756 | 263 | if (fn!=NULL) { |
uci1 | 25:57b2627fe756 | 264 | ++fn; // remove the / |
uci1 | 25:57b2627fe756 | 265 | } else { |
uci1 | 25:57b2627fe756 | 266 | fn = name; |
uci1 | 25:57b2627fe756 | 267 | } |
uci1 | 25:57b2627fe756 | 268 | ffn = ""; |
uci1 | 25:57b2627fe756 | 269 | if (GetRunSeqFromFilename(fn, run, seq)) { |
uci1 | 25:57b2627fe756 | 270 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 271 | printf("got run=%d, seq=%hu\r\n",run,seq); |
uci1 | 25:57b2627fe756 | 272 | #endif |
uci1 | 25:57b2627fe756 | 273 | uint32_t sdlen(0); |
uci1 | 27:efc4d654b139 | 274 | std::string subds( GetSubDirFor(run,seq,sdlen, true) ); |
uci1 | 27:efc4d654b139 | 275 | const char* subd = subds.c_str(); |
uci1 | 25:57b2627fe756 | 276 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 277 | printf("subd=%s\r\n",subd); |
uci1 | 25:57b2627fe756 | 278 | #endif |
uci1 | 25:57b2627fe756 | 279 | ffn = subd; |
uci1 | 25:57b2627fe756 | 280 | ffn += "/"; |
uci1 | 25:57b2627fe756 | 281 | ret = true; |
uci1 | 25:57b2627fe756 | 282 | } |
uci1 | 25:57b2627fe756 | 283 | ffn += fn; |
uci1 | 25:57b2627fe756 | 284 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 285 | printf("ffn=%s, ret=%d\r\n",ffn.c_str(),(int)ret); |
uci1 | 25:57b2627fe756 | 286 | #endif |
uci1 | 25:57b2627fe756 | 287 | return ret; |
uci1 | 25:57b2627fe756 | 288 | } |
uci1 | 25:57b2627fe756 | 289 | |
uci1 | 25:57b2627fe756 | 290 | FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode, |
uci1 | 25:57b2627fe756 | 291 | const bool redoDir) { |
uci1 | 12:d472f9811262 | 292 | // TODO: check if we have memory? |
uci1 | 25:57b2627fe756 | 293 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 294 | printf("OpenSDFile: Trying to open %s.\r\n",name); |
uci1 | 25:57b2627fe756 | 295 | #endif |
uci1 | 40:1324da35afd4 | 296 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 297 | |
uci1 | 25:57b2627fe756 | 298 | std::string ffn; |
uci1 | 25:57b2627fe756 | 299 | FILE* f = 0; |
uci1 | 25:57b2627fe756 | 300 | bool ok = true; |
uci1 | 25:57b2627fe756 | 301 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 302 | printf("redoDir=%d\r\n",(int)redoDir); |
uci1 | 25:57b2627fe756 | 303 | #endif |
uci1 | 25:57b2627fe756 | 304 | if (redoDir) { |
uci1 | 25:57b2627fe756 | 305 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 306 | printf("calling GetFullFilename\r\n"); |
uci1 | 25:57b2627fe756 | 307 | #endif |
uci1 | 25:57b2627fe756 | 308 | ok = GetFullFilename(name, ffn); |
uci1 | 25:57b2627fe756 | 309 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 310 | printf("ffn=%s\r\n",ffn.c_str()); |
uci1 | 25:57b2627fe756 | 311 | #endif |
uci1 | 25:57b2627fe756 | 312 | } else { |
uci1 | 25:57b2627fe756 | 313 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 314 | printf("looking for /\r\n"); |
uci1 | 25:57b2627fe756 | 315 | #endif |
uci1 | 25:57b2627fe756 | 316 | // make sure the directory exists |
uci1 | 25:57b2627fe756 | 317 | const char* ld = strrchr(name, '/'); |
uci1 | 25:57b2627fe756 | 318 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 319 | printf("ld=%p, ld-name = %d\r\n",ld,(int)(ld-name)); |
uci1 | 25:57b2627fe756 | 320 | #endif |
uci1 | 25:57b2627fe756 | 321 | if ((ld!=0) && (ld>name)) { |
uci1 | 25:57b2627fe756 | 322 | std::string dn(name, ld-name); |
uci1 | 25:57b2627fe756 | 323 | DIR* d = OpenOrMakeDir(dn.c_str()); |
uci1 | 25:57b2627fe756 | 324 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 325 | printf("d=%p\r\n",d); |
uci1 | 25:57b2627fe756 | 326 | #endif |
uci1 | 25:57b2627fe756 | 327 | if (d!=NULL) { |
uci1 | 25:57b2627fe756 | 328 | closedir(d); |
uci1 | 25:57b2627fe756 | 329 | } |
uci1 | 25:57b2627fe756 | 330 | } |
uci1 | 25:57b2627fe756 | 331 | // now just copy the (already-) full name |
uci1 | 25:57b2627fe756 | 332 | ffn = name; |
uci1 | 25:57b2627fe756 | 333 | } |
uci1 | 30:f869ed4bcc08 | 334 | if ( ok && ffn.size()>0 ) { |
uci1 | 25:57b2627fe756 | 335 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 336 | printf("OpenSDFile: %s, mode %s\r\n",ffn.c_str(),mode); |
uci1 | 25:57b2627fe756 | 337 | #endif |
uci1 | 25:57b2627fe756 | 338 | f = fopen(ffn.c_str(), mode); |
uci1 | 25:57b2627fe756 | 339 | //setvbuf(f, 0, _IONBF, 0); // no buffering |
uci1 | 25:57b2627fe756 | 340 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 341 | printf("OpenSDFile: f=%p\r\n",(void*)f); |
uci1 | 25:57b2627fe756 | 342 | #endif |
uci1 | 12:d472f9811262 | 343 | } |
uci1 | 12:d472f9811262 | 344 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 345 | printf("ffn=%s\r\n",ffn.c_str()); |
uci1 | 19:74155d652c37 | 346 | #endif |
uci1 | 1:e392595b4b76 | 347 | return f; |
uci1 | 1:e392595b4b76 | 348 | } |
uci1 | 1:e392595b4b76 | 349 | |
uci1 | 0:664899e0b988 | 350 | FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr, |
uci1 | 40:1324da35afd4 | 351 | const uint32_t run, |
uci1 | 40:1324da35afd4 | 352 | const uint16_t minseq) { |
uci1 | 0:664899e0b988 | 353 | // opens a new file in the specified directory and writes this |
uci1 | 0:664899e0b988 | 354 | // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes) |
uci1 | 0:664899e0b988 | 355 | // |
uci1 | 22:f957c4f840ad | 356 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 357 | printf("getting seq num for run %u, minseq %hu\r\n", |
uci1 | 40:1324da35afd4 | 358 | run, minseq); |
uci1 | 22:f957c4f840ad | 359 | #endif |
uci1 | 40:1324da35afd4 | 360 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 361 | |
uci1 | 12:d472f9811262 | 362 | fgCurSeq = GetSeqNum(macadr, run); |
uci1 | 40:1324da35afd4 | 363 | if (fgCurSeq<minseq) { |
uci1 | 40:1324da35afd4 | 364 | fgCurSeq=minseq; |
uci1 | 40:1324da35afd4 | 365 | } |
uci1 | 22:f957c4f840ad | 366 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 367 | printf("fgCurSeq=%hu\r\n",fgCurSeq); |
uci1 | 22:f957c4f840ad | 368 | printf("getting output file name\r\n"); |
uci1 | 22:f957c4f840ad | 369 | #endif |
uci1 | 0:664899e0b988 | 370 | memset(fgCurFileName, 0, sizeof(char)*kFNBufSize); |
uci1 | 12:d472f9811262 | 371 | strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1); |
uci1 | 12:d472f9811262 | 372 | //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); |
uci1 | 25:57b2627fe756 | 373 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 374 | printf("fgCurFileName=%s\r\n",fgCurFileName); |
uci1 | 25:57b2627fe756 | 375 | #endif |
uci1 | 1:e392595b4b76 | 376 | fgCurFile = 0; |
uci1 | 0:664899e0b988 | 377 | if (fgCurFileName!=NULL) { |
uci1 | 22:f957c4f840ad | 378 | #ifdef DEBUG |
uci1 | 22:f957c4f840ad | 379 | printf("opening SD file\r\n"); |
uci1 | 22:f957c4f840ad | 380 | #endif |
uci1 | 25:57b2627fe756 | 381 | fgCurFile = OpenSDFile(fgCurFileName, "wb", false); |
uci1 | 2:e67f7c158087 | 382 | if (fgCurFile!=NULL && ferror(fgCurFile)==0) { |
uci1 | 19:74155d652c37 | 383 | #ifdef DEBUG |
uci1 | 19:74155d652c37 | 384 | printf("Writing file header\r\n"); |
uci1 | 19:74155d652c37 | 385 | #endif |
uci1 | 12:d472f9811262 | 386 | WriteFileHeader(fgCurFile, macadr, run, fgCurSeq); |
uci1 | 2:e67f7c158087 | 387 | } |
uci1 | 0:664899e0b988 | 388 | } |
uci1 | 19:74155d652c37 | 389 | #ifdef DEBUG |
uci1 | 19:74155d652c37 | 390 | printf("fgCurFile=%p\r\n",(void*)fgCurFile); |
uci1 | 19:74155d652c37 | 391 | #endif |
uci1 | 1:e392595b4b76 | 392 | return fgCurFile; |
uci1 | 0:664899e0b988 | 393 | } |
uci1 | 0:664899e0b988 | 394 | |
uci1 | 25:57b2627fe756 | 395 | void SnSDUtils::PrintFilesInDirs(const char* dirname) { |
uci1 | 40:1324da35afd4 | 396 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 397 | |
uci1 | 25:57b2627fe756 | 398 | DIR* d; |
uci1 | 25:57b2627fe756 | 399 | struct dirent* dent; |
uci1 | 25:57b2627fe756 | 400 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 401 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 39:2f17131d22a5 | 402 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 25:57b2627fe756 | 403 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 25:57b2627fe756 | 404 | printf("dn=%s. datr=%02x. dir=%d\r\n", |
uci1 | 25:57b2627fe756 | 405 | dent->d_name, |
uci1 | 25:57b2627fe756 | 406 | dir->filinfo()->fattrib, |
uci1 | 25:57b2627fe756 | 407 | dir->filinfo()->fattrib & AM_DIR); |
uci1 | 25:57b2627fe756 | 408 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 25:57b2627fe756 | 409 | std::string dnm(dirname); |
uci1 | 25:57b2627fe756 | 410 | dnm += "/"; |
uci1 | 25:57b2627fe756 | 411 | dnm += dent->d_name; |
uci1 | 25:57b2627fe756 | 412 | PrintFilesInDirs(dnm.c_str()); |
uci1 | 25:57b2627fe756 | 413 | } |
uci1 | 25:57b2627fe756 | 414 | } |
uci1 | 25:57b2627fe756 | 415 | } |
uci1 | 25:57b2627fe756 | 416 | |
uci1 | 25:57b2627fe756 | 417 | } |
uci1 | 25:57b2627fe756 | 418 | |
uci1 | 40:1324da35afd4 | 419 | float SnSDUtils::GetFreeBytes() { |
uci1 | 40:1324da35afd4 | 420 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 421 | |
uci1 | 40:1324da35afd4 | 422 | FATFS* fs; |
uci1 | 40:1324da35afd4 | 423 | DWORD fre_clust; |
uci1 | 40:1324da35afd4 | 424 | f_getfree("0:",&fre_clust,&fs); |
uci1 | 40:1324da35afd4 | 425 | const float frs = static_cast<float>(fs->csize) |
uci1 | 40:1324da35afd4 | 426 | *static_cast<float>(fs->free_clust) |
uci1 | 40:1324da35afd4 | 427 | #if _MAX_SS != 512 |
uci1 | 40:1324da35afd4 | 428 | *(fs->ssize); |
uci1 | 40:1324da35afd4 | 429 | #else |
uci1 | 40:1324da35afd4 | 430 | *512; |
uci1 | 40:1324da35afd4 | 431 | #endif |
uci1 | 40:1324da35afd4 | 432 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 433 | printf("free space = %g b (%g GB, %g MB, %g KB)\r\n", |
uci1 | 40:1324da35afd4 | 434 | frs, frs/1073741824.0, frs/1048576.0, frs/1024.0); |
uci1 | 40:1324da35afd4 | 435 | #endif |
uci1 | 40:1324da35afd4 | 436 | return frs; |
uci1 | 40:1324da35afd4 | 437 | } |
uci1 | 40:1324da35afd4 | 438 | |
uci1 | 21:ce51bb0ba4a5 | 439 | void SnSDUtils::GetDirProps(const char* dirname, |
uci1 | 21:ce51bb0ba4a5 | 440 | uint32_t& nfiles, |
uci1 | 21:ce51bb0ba4a5 | 441 | float& totbytes) { |
uci1 | 40:1324da35afd4 | 442 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 443 | |
uci1 | 21:ce51bb0ba4a5 | 444 | nfiles = 0; |
uci1 | 21:ce51bb0ba4a5 | 445 | totbytes = 0; |
uci1 | 25:57b2627fe756 | 446 | struct dirent* dent; |
uci1 | 39:2f17131d22a5 | 447 | FATDirHandle* d = static_cast<FATDirHandle*>( opendir(dirname) ); |
uci1 | 21:ce51bb0ba4a5 | 448 | if (d!=0) { |
uci1 | 25:57b2627fe756 | 449 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 25:57b2627fe756 | 450 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 451 | if ( (d->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 25:57b2627fe756 | 452 | // a subdirectory |
uci1 | 25:57b2627fe756 | 453 | std::string dnm(dirname); |
uci1 | 25:57b2627fe756 | 454 | dnm += "/"; |
uci1 | 25:57b2627fe756 | 455 | dnm += dent->d_name; |
uci1 | 25:57b2627fe756 | 456 | uint32_t sdnf; |
uci1 | 25:57b2627fe756 | 457 | float sdtb; |
uci1 | 25:57b2627fe756 | 458 | GetDirProps(dnm.c_str(), sdnf, sdtb); |
uci1 | 25:57b2627fe756 | 459 | nfiles += sdnf; |
uci1 | 25:57b2627fe756 | 460 | totbytes += sdtb; |
uci1 | 25:57b2627fe756 | 461 | } else { |
uci1 | 25:57b2627fe756 | 462 | // a file |
uci1 | 25:57b2627fe756 | 463 | ++nfiles; |
uci1 | 25:57b2627fe756 | 464 | totbytes += d->filinfo()->fsize; |
uci1 | 25:57b2627fe756 | 465 | } |
uci1 | 21:ce51bb0ba4a5 | 466 | } |
uci1 | 21:ce51bb0ba4a5 | 467 | closedir(d); |
uci1 | 21:ce51bb0ba4a5 | 468 | } |
uci1 | 21:ce51bb0ba4a5 | 469 | #ifdef DEBUG |
uci1 | 22:f957c4f840ad | 470 | printf("GetDirProps: %s :: nf=%u, tb=%g\r\n", |
uci1 | 21:ce51bb0ba4a5 | 471 | dirname, nfiles, totbytes); |
uci1 | 21:ce51bb0ba4a5 | 472 | #endif |
uci1 | 21:ce51bb0ba4a5 | 473 | } |
uci1 | 21:ce51bb0ba4a5 | 474 | |
uci1 | 22:f957c4f840ad | 475 | bool SnSDUtils::WriteHeartbeatTo(FILE* file, |
uci1 | 22:f957c4f840ad | 476 | const uint32_t time, |
uci1 | 22:f957c4f840ad | 477 | const uint32_t num) { |
uci1 | 40:1324da35afd4 | 478 | if (file!=0) { |
uci1 | 40:1324da35afd4 | 479 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 480 | |
uci1 | 40:1324da35afd4 | 481 | const bool r1 = |
uci1 | 40:1324da35afd4 | 482 | SnHeaderFrame::WriteTo(file, SnHeaderFrame::kHeartbeatCode, |
uci1 | 40:1324da35afd4 | 483 | SnHeartbeatFrame::SizeOf(SnHeartbeatFrame::kIOVers)); |
uci1 | 40:1324da35afd4 | 484 | const bool r2 = |
uci1 | 40:1324da35afd4 | 485 | SnHeartbeatFrame::WriteTo(file, time, num); |
uci1 | 40:1324da35afd4 | 486 | return (r1 && r2); |
uci1 | 40:1324da35afd4 | 487 | } else { |
uci1 | 40:1324da35afd4 | 488 | return false; |
uci1 | 40:1324da35afd4 | 489 | } |
uci1 | 40:1324da35afd4 | 490 | } |
uci1 | 40:1324da35afd4 | 491 | |
uci1 | 40:1324da35afd4 | 492 | bool SnSDUtils::WriteTrigWaitWinTime(FILE* file, |
uci1 | 40:1324da35afd4 | 493 | SnClockSetFrame& clkset, |
uci1 | 40:1324da35afd4 | 494 | const bool isStart) { |
uci1 | 40:1324da35afd4 | 495 | if (file!=0) { |
uci1 | 40:1324da35afd4 | 496 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 497 | |
uci1 | 40:1324da35afd4 | 498 | bool ok = SnHeaderFrame::WriteTo(file, |
uci1 | 40:1324da35afd4 | 499 | (isStart) ? (SnHeaderFrame::kFileTrgStrtCode) |
uci1 | 40:1324da35afd4 | 500 | : (SnHeaderFrame::kFileTrgStopCode), |
uci1 | 40:1324da35afd4 | 501 | clkset.SizeOf()); |
uci1 | 40:1324da35afd4 | 502 | ok &= (SnCommWin::kOkMsgSent == clkset.WriteTo(file)); |
uci1 | 40:1324da35afd4 | 503 | return ok; |
uci1 | 40:1324da35afd4 | 504 | } else { |
uci1 | 40:1324da35afd4 | 505 | return false; |
uci1 | 40:1324da35afd4 | 506 | } |
uci1 | 22:f957c4f840ad | 507 | } |
uci1 | 22:f957c4f840ad | 508 | |
uci1 | 0:664899e0b988 | 509 | bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf, |
uci1 | 0:664899e0b988 | 510 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 511 | const SnConfigFrame& conf) { |
uci1 | 0:664899e0b988 | 512 | // write event to SD card |
uci1 | 0:664899e0b988 | 513 | |
uci1 | 40:1324da35afd4 | 514 | if (efile!=0) { |
uci1 | 40:1324da35afd4 | 515 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 516 | |
uci1 | 40:1324da35afd4 | 517 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 40:1324da35afd4 | 518 | uint16_t sWvBase=0; |
uci1 | 40:1324da35afd4 | 519 | conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 40:1324da35afd4 | 520 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode, |
uci1 | 40:1324da35afd4 | 521 | evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB)); |
uci1 | 40:1324da35afd4 | 522 | const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 40:1324da35afd4 | 523 | fflush(efile); |
uci1 | 40:1324da35afd4 | 524 | return ret; |
uci1 | 40:1324da35afd4 | 525 | } else { |
uci1 | 40:1324da35afd4 | 526 | return false; |
uci1 | 40:1324da35afd4 | 527 | } |
uci1 | 0:664899e0b988 | 528 | } |
uci1 | 0:664899e0b988 | 529 | |
uci1 | 0:664899e0b988 | 530 | bool SnSDUtils::WriteConfig(FILE* efile, |
uci1 | 0:664899e0b988 | 531 | const SnConfigFrame& conf) { |
uci1 | 40:1324da35afd4 | 532 | if (efile!=0) { |
uci1 | 40:1324da35afd4 | 533 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 534 | |
uci1 | 40:1324da35afd4 | 535 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode, |
uci1 | 8:95a325df1f6b | 536 | conf.SizeOf(SnConfigFrame::kIOVers)); |
uci1 | 40:1324da35afd4 | 537 | conf.WriteTo(efile); |
uci1 | 40:1324da35afd4 | 538 | return true; |
uci1 | 40:1324da35afd4 | 539 | } else { |
uci1 | 40:1324da35afd4 | 540 | return false; |
uci1 | 40:1324da35afd4 | 541 | } |
uci1 | 0:664899e0b988 | 542 | } |
uci1 | 0:664899e0b988 | 543 | |
uci1 | 0:664899e0b988 | 544 | void SnSDUtils::DeleteFile(FILE*& f, const char* fname) { |
uci1 | 25:57b2627fe756 | 545 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 546 | printf("try to delete %s at %p\r\n",fname,f); |
uci1 | 25:57b2627fe756 | 547 | #endif |
uci1 | 40:1324da35afd4 | 548 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 549 | |
uci1 | 27:efc4d654b139 | 550 | if (f!=0) { |
uci1 | 27:efc4d654b139 | 551 | fclose(f); |
uci1 | 27:efc4d654b139 | 552 | f=0; |
uci1 | 27:efc4d654b139 | 553 | } |
uci1 | 25:57b2627fe756 | 554 | std::string fn(""); |
uci1 | 25:57b2627fe756 | 555 | if (GetFullFilename(fname, fn)) { |
uci1 | 25:57b2627fe756 | 556 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 557 | printf("calling remove [%s]\r\n",fn.c_str()); |
uci1 | 25:57b2627fe756 | 558 | #endif |
uci1 | 25:57b2627fe756 | 559 | remove(fn.c_str()); |
uci1 | 21:ce51bb0ba4a5 | 560 | } |
uci1 | 0:664899e0b988 | 561 | } |
uci1 | 0:664899e0b988 | 562 | |
uci1 | 27:efc4d654b139 | 563 | void SnSDUtils::DeleteFilesOfRun(const uint32_t run) { |
uci1 | 27:efc4d654b139 | 564 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 565 | printf("deleteing files of run %lu\r\n",run); |
uci1 | 27:efc4d654b139 | 566 | #endif |
uci1 | 27:efc4d654b139 | 567 | uint32_t rdlen(0); |
uci1 | 27:efc4d654b139 | 568 | std::string rdnm( GetSubDirFor(run, 0, rdlen, false) ); |
uci1 | 27:efc4d654b139 | 569 | DeleteAllFiles(rdnm.c_str()); |
uci1 | 27:efc4d654b139 | 570 | } |
uci1 | 27:efc4d654b139 | 571 | |
uci1 | 27:efc4d654b139 | 572 | void SnSDUtils::DeleteAllFiles(const char* dirname) { |
uci1 | 27:efc4d654b139 | 573 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 574 | printf("deleting ALL files in %s\r\n",dirname); |
uci1 | 27:efc4d654b139 | 575 | #endif |
uci1 | 40:1324da35afd4 | 576 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 577 | |
uci1 | 27:efc4d654b139 | 578 | DIR* d; |
uci1 | 27:efc4d654b139 | 579 | struct dirent* dent; |
uci1 | 27:efc4d654b139 | 580 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 39:2f17131d22a5 | 581 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 27:efc4d654b139 | 582 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 27:efc4d654b139 | 583 | Watchdog::kick(); // don't reset |
uci1 | 27:efc4d654b139 | 584 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 27:efc4d654b139 | 585 | // a subdirectory |
uci1 | 27:efc4d654b139 | 586 | std::string dnm(dirname); |
uci1 | 27:efc4d654b139 | 587 | dnm += "/"; |
uci1 | 27:efc4d654b139 | 588 | dnm += dent->d_name; |
uci1 | 27:efc4d654b139 | 589 | // delete all the files in this new subdir |
uci1 | 27:efc4d654b139 | 590 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 591 | printf("call DeleteAllFiles(%s)\r\n",dnm.c_str()); |
uci1 | 27:efc4d654b139 | 592 | #endif |
uci1 | 27:efc4d654b139 | 593 | DeleteAllFiles(dnm.c_str()); |
uci1 | 27:efc4d654b139 | 594 | } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 27:efc4d654b139 | 595 | // a data file (delete it) |
uci1 | 27:efc4d654b139 | 596 | const bool isCurFile = |
uci1 | 27:efc4d654b139 | 597 | (strcmp(dent->d_name, GetCurFileName())==0); |
uci1 | 27:efc4d654b139 | 598 | if (isCurFile==false) { // don't delete the current file |
uci1 | 27:efc4d654b139 | 599 | FILE* f(0); // dummy |
uci1 | 27:efc4d654b139 | 600 | DeleteFile(f, dent->d_name); |
uci1 | 27:efc4d654b139 | 601 | } |
uci1 | 27:efc4d654b139 | 602 | } |
uci1 | 27:efc4d654b139 | 603 | } // loop over stuff in this dir |
uci1 | 27:efc4d654b139 | 604 | closedir(d); |
uci1 | 27:efc4d654b139 | 605 | DeleteDirIfEmpty(dirname); |
uci1 | 27:efc4d654b139 | 606 | } |
uci1 | 27:efc4d654b139 | 607 | } |
uci1 | 27:efc4d654b139 | 608 | |
uci1 | 27:efc4d654b139 | 609 | void SnSDUtils::DeleteDirIfEmpty(const char* dirname) { |
uci1 | 27:efc4d654b139 | 610 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 611 | printf("DeleteDirIfEmpty(%s)\r\n",dirname); |
uci1 | 27:efc4d654b139 | 612 | #endif |
uci1 | 40:1324da35afd4 | 613 | InitSDCard(); |
uci1 | 40:1324da35afd4 | 614 | |
uci1 | 27:efc4d654b139 | 615 | DIR* d; |
uci1 | 27:efc4d654b139 | 616 | struct dirent* dent; |
uci1 | 27:efc4d654b139 | 617 | if ( (d = opendir(dirname))!=NULL ) { |
uci1 | 27:efc4d654b139 | 618 | dent = readdir(d); |
uci1 | 27:efc4d654b139 | 619 | bool doDel = false; |
uci1 | 27:efc4d654b139 | 620 | if ( dent==NULL ) { |
uci1 | 27:efc4d654b139 | 621 | // then this directory is empty |
uci1 | 27:efc4d654b139 | 622 | static const size_t subdsz = strlen(kSDsubDir); |
uci1 | 27:efc4d654b139 | 623 | // just double check that this directory is |
uci1 | 27:efc4d654b139 | 624 | // a subdirectory of the data dir |
uci1 | 27:efc4d654b139 | 625 | if (strlen(dirname)>subdsz) { |
uci1 | 27:efc4d654b139 | 626 | doDel = true; |
uci1 | 27:efc4d654b139 | 627 | } |
uci1 | 27:efc4d654b139 | 628 | } // else this dir isn't empty |
uci1 | 27:efc4d654b139 | 629 | closedir(d); |
uci1 | 27:efc4d654b139 | 630 | if (doDel) { |
uci1 | 27:efc4d654b139 | 631 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 632 | printf("removing directory [%s]\r\n",dirname); |
uci1 | 27:efc4d654b139 | 633 | #endif |
uci1 | 27:efc4d654b139 | 634 | remove(dirname); |
uci1 | 27:efc4d654b139 | 635 | } |
uci1 | 27:efc4d654b139 | 636 | } |
uci1 | 27:efc4d654b139 | 637 | } |
uci1 | 27:efc4d654b139 | 638 | |
uci1 | 40:1324da35afd4 | 639 | SnCommWin::ECommWinResult SnSDUtils::SendAllOfRun(SnCommWin* comm, |
uci1 | 40:1324da35afd4 | 640 | const uint32_t timeout, |
uci1 | 40:1324da35afd4 | 641 | char* const buf, |
uci1 | 40:1324da35afd4 | 642 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 643 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 644 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 645 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 646 | const uint32_t runnum) { |
uci1 | 40:1324da35afd4 | 647 | // send all files in a run |
uci1 | 40:1324da35afd4 | 648 | |
uci1 | 40:1324da35afd4 | 649 | // get the run dir |
uci1 | 40:1324da35afd4 | 650 | uint32_t rdlen(0); |
uci1 | 40:1324da35afd4 | 651 | std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) ); |
uci1 | 40:1324da35afd4 | 652 | return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, |
uci1 | 40:1324da35afd4 | 653 | rdnms.c_str()); |
uci1 | 40:1324da35afd4 | 654 | } |
uci1 | 40:1324da35afd4 | 655 | |
uci1 | 0:664899e0b988 | 656 | SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, |
uci1 | 3:24c5f0f50bf1 | 657 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 658 | char* const buf, |
uci1 | 6:6f002d202f59 | 659 | const uint32_t bsize, |
uci1 | 6:6f002d202f59 | 660 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 661 | SnEventFrame& evt, |
uci1 | 12:d472f9811262 | 662 | SnPowerFrame& pow, |
uci1 | 25:57b2627fe756 | 663 | const char* dirname) { |
uci1 | 40:1324da35afd4 | 664 | // send all files in the specified directory |
uci1 | 40:1324da35afd4 | 665 | |
uci1 | 40:1324da35afd4 | 666 | InitSDCard(); |
uci1 | 25:57b2627fe756 | 667 | |
uci1 | 25:57b2627fe756 | 668 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 16:744ce85aede2 | 669 | |
uci1 | 0:664899e0b988 | 670 | DIR* d; |
uci1 | 0:664899e0b988 | 671 | struct dirent* dent; |
uci1 | 25:57b2627fe756 | 672 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 39:2f17131d22a5 | 673 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 0:664899e0b988 | 674 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 25:57b2627fe756 | 675 | Watchdog::kick(); // don't reset |
uci1 | 27:efc4d654b139 | 676 | SnCommWin::ECommWinResult res = rs; |
uci1 | 25:57b2627fe756 | 677 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 25:57b2627fe756 | 678 | // a subdirectory |
uci1 | 25:57b2627fe756 | 679 | std::string dnm(dirname); |
uci1 | 25:57b2627fe756 | 680 | dnm += "/"; |
uci1 | 25:57b2627fe756 | 681 | dnm += dent->d_name; |
uci1 | 25:57b2627fe756 | 682 | // send all the files in this new subdir |
uci1 | 27:efc4d654b139 | 683 | res = SendAllFiles(comm, timeout, buf, bsize, |
uci1 | 27:efc4d654b139 | 684 | curConf, evt, pow, |
uci1 | 40:1324da35afd4 | 685 | dnm.c_str()); |
uci1 | 25:57b2627fe756 | 686 | } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 25:57b2627fe756 | 687 | // a data file (send it) |
uci1 | 1:e392595b4b76 | 688 | const bool isCurFile = |
uci1 | 1:e392595b4b76 | 689 | (strcmp(dent->d_name, GetCurFileName())==0); |
uci1 | 25:57b2627fe756 | 690 | FILE* f(0); |
uci1 | 1:e392595b4b76 | 691 | if (isCurFile) { |
uci1 | 3:24c5f0f50bf1 | 692 | // file must already be written out! |
uci1 | 1:e392595b4b76 | 693 | f = GetCurFile(); |
uci1 | 1:e392595b4b76 | 694 | } else { |
uci1 | 25:57b2627fe756 | 695 | std::string ffn(dirname); |
uci1 | 25:57b2627fe756 | 696 | ffn += "/"; |
uci1 | 25:57b2627fe756 | 697 | ffn += dent->d_name; |
uci1 | 25:57b2627fe756 | 698 | f = OpenExistingFile(ffn.c_str(), false, false); |
uci1 | 1:e392595b4b76 | 699 | } |
uci1 | 12:d472f9811262 | 700 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 701 | printf("calling senddata: f=%p (cur %p), fn=%s\r\n", |
uci1 | 12:d472f9811262 | 702 | f, GetCurFile(), dent->d_name); |
uci1 | 12:d472f9811262 | 703 | #endif |
uci1 | 25:57b2627fe756 | 704 | uint8_t hndres = SnHeaderFrame::kHnShFailNonCode; |
uci1 | 40:1324da35afd4 | 705 | if (f!=0) { |
uci1 | 40:1324da35afd4 | 706 | res = comm->SendDataFromFile(f, dent->d_name, |
uci1 | 40:1324da35afd4 | 707 | curConf, evt, pow, buf, bsize, |
uci1 | 40:1324da35afd4 | 708 | 0, timeout, |
uci1 | 40:1324da35afd4 | 709 | &hndres); |
uci1 | 40:1324da35afd4 | 710 | |
uci1 | 40:1324da35afd4 | 711 | if (isCurFile) { |
uci1 | 40:1324da35afd4 | 712 | // move (back) to the end of the file |
uci1 | 40:1324da35afd4 | 713 | // altho hopefully no writing will happen after this |
uci1 | 40:1324da35afd4 | 714 | fseek(fgCurFile, 0, SEEK_END); |
uci1 | 40:1324da35afd4 | 715 | } |
uci1 | 40:1324da35afd4 | 716 | } |
uci1 | 25:57b2627fe756 | 717 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 718 | printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n", |
uci1 | 25:57b2627fe756 | 719 | (int)isCurFile, (int)res, (int)(curConf.IsDeletingFiles()), |
uci1 | 25:57b2627fe756 | 720 | hndres); |
uci1 | 25:57b2627fe756 | 721 | #endif |
uci1 | 27:efc4d654b139 | 722 | |
uci1 | 25:57b2627fe756 | 723 | } |
uci1 | 27:efc4d654b139 | 724 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 27:efc4d654b139 | 725 | rs = res; |
uci1 | 27:efc4d654b139 | 726 | } |
uci1 | 27:efc4d654b139 | 727 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 27:efc4d654b139 | 728 | // prevent sending the others |
uci1 | 25:57b2627fe756 | 729 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 25:57b2627fe756 | 730 | break; |
uci1 | 27:efc4d654b139 | 731 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 27:efc4d654b139 | 732 | break; |
uci1 | 25:57b2627fe756 | 733 | } |
uci1 | 27:efc4d654b139 | 734 | } // loop over stuff in this directory |
uci1 | 25:57b2627fe756 | 735 | closedir(d); |
uci1 | 25:57b2627fe756 | 736 | // see if we need to remove this directory now |
uci1 | 25:57b2627fe756 | 737 | if (curConf.IsDeletingFiles()) { |
uci1 | 27:efc4d654b139 | 738 | DeleteDirIfEmpty(dirname); |
uci1 | 0:664899e0b988 | 739 | } |
uci1 | 0:664899e0b988 | 740 | } |
uci1 | 0:664899e0b988 | 741 | |
uci1 | 0:664899e0b988 | 742 | return rs; |
uci1 | 0:664899e0b988 | 743 | } |
uci1 | 0:664899e0b988 | 744 |