![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.cpp@67:ec999336fcd1, 2015-02-03 (annotated)
- Committer:
- uci1
- Date:
- Tue Feb 03 00:04:30 2015 +0000
- Revision:
- 67:ec999336fcd1
- Parent:
- 66:685f9d0a48ae
- Child:
- 76:f8383f0292c2
STN20 Hardcoded (mac adr and config label). Power off periphs before powering anything on. do NOT use interface chip for mac adr or files (except reprogramming)
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 | 65:2cb3e99ce466 | 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 | 56:0bba0ef15697 | 22 | const char* const SnSDUtils::kRunSeqListFilenm = "/sd/RunSeqLC.txt"; |
uci1 | 56:0bba0ef15697 | 23 | const uint16_t SnSDUtils::kMaxSeqNum = 64000; // max "normal" seq |
uci1 | 56:0bba0ef15697 | 24 | const uint16_t SnSDUtils::kBadSeqNum = 65000; // should be larger than kMaxSeqNum to separate "normal" and "bad" seqs |
uci1 | 2:e67f7c158087 | 25 | char SnSDUtils::fgCurFileName[kFNBufSize]={0}; |
uci1 | 1:e392595b4b76 | 26 | FILE* SnSDUtils::fgCurFile = 0; |
uci1 | 12:d472f9811262 | 27 | uint16_t SnSDUtils::fgCurSeq = 0; |
uci1 | 40:1324da35afd4 | 28 | bool SnSDUtils::fgNeedToInit = true; |
uci1 | 40:1324da35afd4 | 29 | const uint8_t SnSDUtils::kIOvers = 4; |
uci1 | 5:9cea89700c66 | 30 | const uint32_t SnSDUtils::kMaxSizeOfFileHdr = |
uci1 | 5:9cea89700c66 | 31 | sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t) |
uci1 | 8:95a325df1f6b | 32 | +(sizeof(uint8_t)+(2u*sizeof(uint16_t))); // power frame v1 |
uci1 | 4:a91682e19d6b | 33 | |
uci1 | 40:1324da35afd4 | 34 | SnSDUtils::InitSDFcn SnSDUtils::fgDoInit = 0; |
uci1 | 56:0bba0ef15697 | 35 | bool SnSDUtils::fgInitOk = false; |
uci1 | 40:1324da35afd4 | 36 | |
uci1 | 4:a91682e19d6b | 37 | static const uint16_t __kMaxUShort = ~0; |
uci1 | 0:664899e0b988 | 38 | |
uci1 | 56:0bba0ef15697 | 39 | bool SnSDUtils::InitSDCard(const bool force) { |
uci1 | 64:6c7a316eafad | 40 | Watchdog::kick(); // don't reset |
uci1 | 40:1324da35afd4 | 41 | if ((fgNeedToInit || force) && (fgDoInit!=0)) { |
uci1 | 56:0bba0ef15697 | 42 | fgInitOk = (*fgDoInit)() == 0; |
uci1 | 56:0bba0ef15697 | 43 | if (IsInitOk()) { |
uci1 | 56:0bba0ef15697 | 44 | fgNeedToInit = false; |
uci1 | 56:0bba0ef15697 | 45 | } |
uci1 | 40:1324da35afd4 | 46 | } |
uci1 | 56:0bba0ef15697 | 47 | return fgInitOk; |
uci1 | 40:1324da35afd4 | 48 | } |
uci1 | 40:1324da35afd4 | 49 | |
uci1 | 25:57b2627fe756 | 50 | const char* SnSDUtils::GetSubDirFor(const uint32_t run, const uint16_t seq, |
uci1 | 25:57b2627fe756 | 51 | uint32_t& slen, const bool useSeq) { |
uci1 | 40:1324da35afd4 | 52 | // returns a STATIC string! (so make a copy of it before use) |
uci1 | 25:57b2627fe756 | 53 | // sets slen to the length of this string (same as strlen) |
uci1 | 64:6c7a316eafad | 54 | Watchdog::kick(); // don't reset |
uci1 | 40:1324da35afd4 | 55 | static const uint16_t tmplen = strlen(kSDsubDir)+50; |
uci1 | 40:1324da35afd4 | 56 | static char* tmpsd = new char[tmplen]; |
uci1 | 40:1324da35afd4 | 57 | slen = snprintf(tmpsd, tmplen, "%s/r%05ld", kSDsubDir, run); |
uci1 | 25:57b2627fe756 | 58 | if (useSeq) { |
uci1 | 40:1324da35afd4 | 59 | slen += snprintf(tmpsd+slen, tmplen-slen, "/s%05d", (seq/SUBDIRSEQ)*SUBDIRSEQ); |
uci1 | 40:1324da35afd4 | 60 | } |
uci1 | 40:1324da35afd4 | 61 | if (slen > tmplen) { |
uci1 | 40:1324da35afd4 | 62 | slen = tmplen; |
uci1 | 25:57b2627fe756 | 63 | } |
uci1 | 25:57b2627fe756 | 64 | return tmpsd; |
uci1 | 25:57b2627fe756 | 65 | } |
uci1 | 25:57b2627fe756 | 66 | |
uci1 | 0:664899e0b988 | 67 | const char* SnSDUtils::GetOutFileName(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 68 | const uint32_t run, |
uci1 | 0:664899e0b988 | 69 | const uint16_t seq) { |
uci1 | 0:664899e0b988 | 70 | // returns the formatted file name, or NULL if the directory is too long |
uci1 | 0:664899e0b988 | 71 | // and the full name cannot fit in the buffer |
uci1 | 0:664899e0b988 | 72 | // NOTE: this fcn uses a static buffer, and so should not be called |
uci1 | 0:664899e0b988 | 73 | // multiple times in the same line (that includes the calling of functions |
uci1 | 0:664899e0b988 | 74 | // that call this function!) |
uci1 | 0:664899e0b988 | 75 | // |
uci1 | 0:664899e0b988 | 76 | // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat |
uci1 | 0:664899e0b988 | 77 | // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4 |
uci1 | 64:6c7a316eafad | 78 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 79 | uint32_t sdlen(0); |
uci1 | 27:efc4d654b139 | 80 | std::string subdirs( GetSubDirFor(run, seq, sdlen, true) ); |
uci1 | 27:efc4d654b139 | 81 | const char* subdir = subdirs.c_str(); |
uci1 | 25:57b2627fe756 | 82 | if (sdlen<(kFNBufSize-37)) { |
uci1 | 0:664899e0b988 | 83 | static char tbuf[kFNBufSize]; |
uci1 | 0:664899e0b988 | 84 | memset(tbuf, 0, sizeof(char)*kFNBufSize); |
uci1 | 0:664899e0b988 | 85 | // if file name format changes, GetSeqNum must be changed too |
uci1 | 25:57b2627fe756 | 86 | sprintf(tbuf, "%s/SnEvtsM%012llXr%05lds%05d.dat", |
uci1 | 25:57b2627fe756 | 87 | subdir, |
uci1 | 0:664899e0b988 | 88 | macadr>>16, // 64 -> 48 bits |
uci1 | 0:664899e0b988 | 89 | run, seq); |
uci1 | 0:664899e0b988 | 90 | return tbuf; |
uci1 | 0:664899e0b988 | 91 | } else { |
uci1 | 0:664899e0b988 | 92 | return NULL; |
uci1 | 0:664899e0b988 | 93 | } |
uci1 | 0:664899e0b988 | 94 | } |
uci1 | 25:57b2627fe756 | 95 | |
uci1 | 25:57b2627fe756 | 96 | bool SnSDUtils::GetRunSeqFromFilename(const char* fn, |
uci1 | 25:57b2627fe756 | 97 | uint32_t& run, |
uci1 | 25:57b2627fe756 | 98 | uint16_t& seq) { |
uci1 | 64:6c7a316eafad | 99 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 100 | bool ret = false; |
uci1 | 25:57b2627fe756 | 101 | const int32_t ncomp = strrchr(fn, 'r') - fn; |
uci1 | 25:57b2627fe756 | 102 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 103 | printf("fn=%s, ncomp=%d\r\n",fn,ncomp); |
uci1 | 25:57b2627fe756 | 104 | #endif |
uci1 | 25:57b2627fe756 | 105 | if ((ncomp<strlen(fn)) && (ncomp>0)) { |
uci1 | 25:57b2627fe756 | 106 | if (sscanf(fn+ncomp,"r%lus%hu.dat",&run,&seq)==2) { |
uci1 | 25:57b2627fe756 | 107 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 108 | printf("run=%u, seq=%hu\r\n",run,seq); |
uci1 | 25:57b2627fe756 | 109 | #endif |
uci1 | 25:57b2627fe756 | 110 | ret = true; |
uci1 | 25:57b2627fe756 | 111 | } |
uci1 | 25:57b2627fe756 | 112 | } |
uci1 | 25:57b2627fe756 | 113 | return ret; |
uci1 | 25:57b2627fe756 | 114 | } |
uci1 | 25:57b2627fe756 | 115 | /* |
uci1 | 10:3c93db1cfb12 | 116 | uint16_t SnSDUtils::GetSeqNumFromFileName(const char* fn) { |
uci1 | 10:3c93db1cfb12 | 117 | uint16_t seq=0; |
uci1 | 10:3c93db1cfb12 | 118 | const uint32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 10:3c93db1cfb12 | 119 | if (ncomp<strlen(fn)) { |
uci1 | 10:3c93db1cfb12 | 120 | sscanf(fn+ncomp,"s%hu.dat",&seq); |
uci1 | 10:3c93db1cfb12 | 121 | } |
uci1 | 10:3c93db1cfb12 | 122 | return seq; |
uci1 | 10:3c93db1cfb12 | 123 | } |
uci1 | 25:57b2627fe756 | 124 | */ |
uci1 | 47:fbe956b10a91 | 125 | |
uci1 | 47:fbe956b10a91 | 126 | DIR* SnSDUtils::OpenOrMakeAllDirs(const char* dirname) { |
uci1 | 47:fbe956b10a91 | 127 | #ifdef DEBUG |
uci1 | 47:fbe956b10a91 | 128 | printf("OpenOrMakeAllDirs: [%s]\r\n",dirname); |
uci1 | 47:fbe956b10a91 | 129 | #endif |
uci1 | 64:6c7a316eafad | 130 | Watchdog::kick(); // don't reset |
uci1 | 47:fbe956b10a91 | 131 | // try making the subdir |
uci1 | 47:fbe956b10a91 | 132 | DIR* sd(0); |
uci1 | 47:fbe956b10a91 | 133 | std::string dn(dirname); |
uci1 | 47:fbe956b10a91 | 134 | std::size_t slash(0); |
uci1 | 47:fbe956b10a91 | 135 | std::string sdr; |
uci1 | 47:fbe956b10a91 | 136 | bool ok=true; |
uci1 | 47:fbe956b10a91 | 137 | while (ok) { |
uci1 | 47:fbe956b10a91 | 138 | slash=dn.find_first_of('/',slash+1); |
uci1 | 47:fbe956b10a91 | 139 | if (slash==std::string::npos) { |
uci1 | 47:fbe956b10a91 | 140 | sdr = dn; // no more slashes |
uci1 | 47:fbe956b10a91 | 141 | ok=false; |
uci1 | 47:fbe956b10a91 | 142 | } else { |
uci1 | 47:fbe956b10a91 | 143 | sdr = dn.substr(0, slash); |
uci1 | 47:fbe956b10a91 | 144 | } |
uci1 | 47:fbe956b10a91 | 145 | #ifdef DEBUG |
uci1 | 47:fbe956b10a91 | 146 | printf("slash=%d, sdr=[%s] (%d), ok=%s\r\n", |
uci1 | 47:fbe956b10a91 | 147 | (int)slash, sdr.c_str(), (int)sdr.size(), |
uci1 | 47:fbe956b10a91 | 148 | ok ? "true" : "false"); |
uci1 | 47:fbe956b10a91 | 149 | #endif |
uci1 | 47:fbe956b10a91 | 150 | if (sd!=0) { |
uci1 | 47:fbe956b10a91 | 151 | // close the one from last time |
uci1 | 47:fbe956b10a91 | 152 | closedir(sd); |
uci1 | 47:fbe956b10a91 | 153 | } |
uci1 | 47:fbe956b10a91 | 154 | // skip the /sd, as it's not a real directory |
uci1 | 47:fbe956b10a91 | 155 | // should be skipped anyway, but.. |
uci1 | 47:fbe956b10a91 | 156 | if (sdr.compare(kSDdir)!=0) { |
uci1 | 47:fbe956b10a91 | 157 | #ifdef DEBUG |
uci1 | 47:fbe956b10a91 | 158 | printf("calling OpenOrMakeDir [%s]\r\n",sdr.c_str()); |
uci1 | 47:fbe956b10a91 | 159 | #endif |
uci1 | 47:fbe956b10a91 | 160 | sd = OpenOrMakeDir(sdr.c_str()); |
uci1 | 47:fbe956b10a91 | 161 | } |
uci1 | 47:fbe956b10a91 | 162 | } |
uci1 | 47:fbe956b10a91 | 163 | return sd; |
uci1 | 47:fbe956b10a91 | 164 | } |
uci1 | 47:fbe956b10a91 | 165 | |
uci1 | 25:57b2627fe756 | 166 | DIR* SnSDUtils::OpenOrMakeDir(const char* dirname) { |
uci1 | 25:57b2627fe756 | 167 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 168 | printf("open dir %s\r\n",dirname); |
uci1 | 25:57b2627fe756 | 169 | #endif |
uci1 | 64:6c7a316eafad | 170 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 171 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 172 | |
uci1 | 56:0bba0ef15697 | 173 | DIR* rd( opendir(dirname) ); |
uci1 | 56:0bba0ef15697 | 174 | if (rd==NULL) { |
uci1 | 56:0bba0ef15697 | 175 | // try making the directory |
uci1 | 25:57b2627fe756 | 176 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 177 | printf("making dir %s\r\n",dirname); |
uci1 | 25:57b2627fe756 | 178 | #endif |
uci1 | 56:0bba0ef15697 | 179 | mkdir(dirname, 0777); |
uci1 | 36:87865913ae6f | 180 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 181 | printf("opening dir %s\r\n",dirname); |
uci1 | 36:87865913ae6f | 182 | #endif |
uci1 | 56:0bba0ef15697 | 183 | rd = opendir(dirname); |
uci1 | 56:0bba0ef15697 | 184 | } |
uci1 | 36:87865913ae6f | 185 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 186 | printf("returning rd=%p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 187 | #endif |
uci1 | 56:0bba0ef15697 | 188 | return rd; |
uci1 | 56:0bba0ef15697 | 189 | } else { |
uci1 | 56:0bba0ef15697 | 190 | return 0; |
uci1 | 56:0bba0ef15697 | 191 | } |
uci1 | 25:57b2627fe756 | 192 | } |
uci1 | 10:3c93db1cfb12 | 193 | |
uci1 | 0:664899e0b988 | 194 | uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 195 | const uint32_t run) { |
uci1 | 0:664899e0b988 | 196 | // count the files having expected filename format |
uci1 | 56:0bba0ef15697 | 197 | |
uci1 | 64:6c7a316eafad | 198 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 199 | |
uci1 | 56:0bba0ef15697 | 200 | uint16_t maxs(kBadSeqNum); |
uci1 | 0:664899e0b988 | 201 | |
uci1 | 56:0bba0ef15697 | 202 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 203 | |
uci1 | 56:0bba0ef15697 | 204 | maxs = 0; // change back from kBadSeqNum! |
uci1 | 56:0bba0ef15697 | 205 | |
uci1 | 56:0bba0ef15697 | 206 | // get the run dir |
uci1 | 56:0bba0ef15697 | 207 | uint32_t rdlen(0); |
uci1 | 56:0bba0ef15697 | 208 | std::string rdnms ( GetSubDirFor(run, 0, rdlen, false) ); |
uci1 | 56:0bba0ef15697 | 209 | const char* rdnm = rdnms.c_str(); |
uci1 | 56:0bba0ef15697 | 210 | // open/make the run directory |
uci1 | 56:0bba0ef15697 | 211 | FATDirHandle* rd(static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) )); |
uci1 | 56:0bba0ef15697 | 212 | struct dirent* rdent; |
uci1 | 56:0bba0ef15697 | 213 | uint16_t dseq(0); |
uci1 | 36:87865913ae6f | 214 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 215 | printf("starting readdir loop over %p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 216 | #endif |
uci1 | 56:0bba0ef15697 | 217 | while ( (rdent = readdir(rd))!=NULL ) { |
uci1 | 36:87865913ae6f | 218 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 219 | printf("rdent = %p\r\n",(void*)rdent); |
uci1 | 36:87865913ae6f | 220 | #endif |
uci1 | 56:0bba0ef15697 | 221 | if ((rd->filinfo()->fattrib & AM_DIR)!=0) { |
uci1 | 36:87865913ae6f | 222 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 223 | printf("is a dir\r\n"); |
uci1 | 36:87865913ae6f | 224 | #endif |
uci1 | 56:0bba0ef15697 | 225 | // is a directory |
uci1 | 56:0bba0ef15697 | 226 | const int ncm = sscanf(rdent->d_name, "s%hu", &dseq); |
uci1 | 56:0bba0ef15697 | 227 | if (ncm==1) { |
uci1 | 56:0bba0ef15697 | 228 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 229 | printf("dseq=%hu, maxs=%hu\r\n",dseq,maxs); |
uci1 | 56:0bba0ef15697 | 230 | #endif |
uci1 | 56:0bba0ef15697 | 231 | if ( (dseq>maxs) && (dseq<kMaxSeqNum) ) { |
uci1 | 56:0bba0ef15697 | 232 | maxs = dseq; |
uci1 | 56:0bba0ef15697 | 233 | } |
uci1 | 4:a91682e19d6b | 234 | } |
uci1 | 0:664899e0b988 | 235 | } |
uci1 | 56:0bba0ef15697 | 236 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 237 | else { |
uci1 | 56:0bba0ef15697 | 238 | printf("not a dir\r\n"); |
uci1 | 56:0bba0ef15697 | 239 | } |
uci1 | 56:0bba0ef15697 | 240 | #endif |
uci1 | 0:664899e0b988 | 241 | } |
uci1 | 36:87865913ae6f | 242 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 243 | printf("closing directory %p\r\n",(void*)rd); |
uci1 | 36:87865913ae6f | 244 | #endif |
uci1 | 56:0bba0ef15697 | 245 | closedir(rd); |
uci1 | 36:87865913ae6f | 246 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 247 | printf("Found max seq dir num %hu for run %u\r\n",maxs,run); |
uci1 | 25:57b2627fe756 | 248 | #endif |
uci1 | 64:6c7a316eafad | 249 | |
uci1 | 64:6c7a316eafad | 250 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 251 | |
uci1 | 56:0bba0ef15697 | 252 | // open up the seq dir |
uci1 | 56:0bba0ef15697 | 253 | rdnms = GetSubDirFor(run, maxs, rdlen, true); |
uci1 | 56:0bba0ef15697 | 254 | rdnm = rdnms.c_str(); |
uci1 | 56:0bba0ef15697 | 255 | // runXseq0 filename (fn points to a static buffer) |
uci1 | 56:0bba0ef15697 | 256 | const char* fn = GetOutFileName(macadr, run, maxs) |
uci1 | 56:0bba0ef15697 | 257 | + rdlen + 1; // take out dir and '/'s |
uci1 | 56:0bba0ef15697 | 258 | // don't compare seq#. don't use num of chars in case seq is >999 |
uci1 | 56:0bba0ef15697 | 259 | const int32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 56:0bba0ef15697 | 260 | // open (or make) the run/seq dir |
uci1 | 56:0bba0ef15697 | 261 | rd = static_cast<FATDirHandle*>( OpenOrMakeAllDirs(rdnm) ); |
uci1 | 56:0bba0ef15697 | 262 | // get the new sequence number (ok if it overflows this seq "bin") |
uci1 | 56:0bba0ef15697 | 263 | maxs=0; |
uci1 | 56:0bba0ef15697 | 264 | while ( (rdent = readdir(rd))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 265 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 266 | if ((rd->filinfo()->fattrib & AM_DIR)==0) { |
uci1 | 56:0bba0ef15697 | 267 | // is a file. |
uci1 | 56:0bba0ef15697 | 268 | // don't just count files, in case one seq was |
uci1 | 56:0bba0ef15697 | 269 | // transferred and erased in the middle of a run |
uci1 | 56:0bba0ef15697 | 270 | if (strncmp(rdent->d_name, fn, ncomp)==0) { |
uci1 | 56:0bba0ef15697 | 271 | // allow for deleted files to make gaps. |
uci1 | 56:0bba0ef15697 | 272 | // search for highest seq number and increase that |
uci1 | 56:0bba0ef15697 | 273 | if (sscanf((rdent->d_name)+ncomp,"s%hu.dat",&dseq)==1) { |
uci1 | 25:57b2627fe756 | 274 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 275 | printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", |
uci1 | 56:0bba0ef15697 | 276 | rdent->d_name, dseq, __kMaxUShort); |
uci1 | 25:57b2627fe756 | 277 | #endif |
uci1 | 56:0bba0ef15697 | 278 | if (dseq==__kMaxUShort) { |
uci1 | 56:0bba0ef15697 | 279 | maxs = dseq; |
uci1 | 56:0bba0ef15697 | 280 | break; |
uci1 | 56:0bba0ef15697 | 281 | } |
uci1 | 56:0bba0ef15697 | 282 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 283 | printf("dseq=%hu, maxs=%hu\r\n",dseq,maxs); |
uci1 | 56:0bba0ef15697 | 284 | #endif |
uci1 | 56:0bba0ef15697 | 285 | if ( (dseq>=maxs) && (dseq<kMaxSeqNum)) { |
uci1 | 56:0bba0ef15697 | 286 | maxs=dseq+1; |
uci1 | 56:0bba0ef15697 | 287 | } |
uci1 | 56:0bba0ef15697 | 288 | if (maxs==__kMaxUShort) { |
uci1 | 56:0bba0ef15697 | 289 | break; |
uci1 | 56:0bba0ef15697 | 290 | } |
uci1 | 25:57b2627fe756 | 291 | } |
uci1 | 25:57b2627fe756 | 292 | } |
uci1 | 25:57b2627fe756 | 293 | } |
uci1 | 25:57b2627fe756 | 294 | } |
uci1 | 56:0bba0ef15697 | 295 | closedir(rd); |
uci1 | 56:0bba0ef15697 | 296 | } else { |
uci1 | 56:0bba0ef15697 | 297 | // no SD card |
uci1 | 56:0bba0ef15697 | 298 | |
uci1 | 25:57b2627fe756 | 299 | } |
uci1 | 21:ce51bb0ba4a5 | 300 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 301 | printf("return maxs=%hu\r\n",maxs); |
uci1 | 21:ce51bb0ba4a5 | 302 | #endif |
uci1 | 25:57b2627fe756 | 303 | return maxs; |
uci1 | 0:664899e0b988 | 304 | } |
uci1 | 0:664899e0b988 | 305 | |
uci1 | 25:57b2627fe756 | 306 | FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent, |
uci1 | 25:57b2627fe756 | 307 | const bool redoDir) { |
uci1 | 64:6c7a316eafad | 308 | Watchdog::kick(); // don't reset |
uci1 | 0:664899e0b988 | 309 | FILE* f = 0; |
uci1 | 56:0bba0ef15697 | 310 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 311 | //if ((name!=NULL) && ((*name)!=0) ) { // simple check if filename not set |
uci1 | 56:0bba0ef15697 | 312 | if (name!=NULL) { // simple check if filename not set |
uci1 | 25:57b2627fe756 | 313 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 314 | printf("opening SD file. name=[%s]\r\n",name); |
uci1 | 25:57b2627fe756 | 315 | #endif |
uci1 | 56:0bba0ef15697 | 316 | f = OpenSDFile(name, "rb", redoDir); |
uci1 | 67:ec999336fcd1 | 317 | /* CJR: Jan 2015 -- why was this commented out? |
uci1 | 56:0bba0ef15697 | 318 | if (setcurrent) { |
uci1 | 56:0bba0ef15697 | 319 | fgCurFile = f; |
uci1 | 56:0bba0ef15697 | 320 | strncpy(fgCurFileName, name, kFNBufSize-1); |
uci1 | 56:0bba0ef15697 | 321 | fgCurSeq = GetSeqNumFromFileName(fgCurFileName); |
uci1 | 56:0bba0ef15697 | 322 | } |
uci1 | 56:0bba0ef15697 | 323 | */ |
uci1 | 3:24c5f0f50bf1 | 324 | } |
uci1 | 0:664899e0b988 | 325 | } |
uci1 | 0:664899e0b988 | 326 | return f; |
uci1 | 0:664899e0b988 | 327 | } |
uci1 | 0:664899e0b988 | 328 | |
uci1 | 25:57b2627fe756 | 329 | bool SnSDUtils::GetFullFilename(const char* name, std::string& ffn) { |
uci1 | 25:57b2627fe756 | 330 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 331 | printf("GetFullFilename (%s)\r\n",name); |
uci1 | 25:57b2627fe756 | 332 | #endif |
uci1 | 64:6c7a316eafad | 333 | Watchdog::kick(); // don't reset |
uci1 | 25:57b2627fe756 | 334 | bool ret = false; |
uci1 | 25:57b2627fe756 | 335 | uint32_t run(0); |
uci1 | 25:57b2627fe756 | 336 | uint16_t seq(0); |
uci1 | 25:57b2627fe756 | 337 | const char* fn = strrchr(name, '/'); |
uci1 | 25:57b2627fe756 | 338 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 339 | printf("w/o / : %s\r\n",fn); |
uci1 | 25:57b2627fe756 | 340 | #endif |
uci1 | 25:57b2627fe756 | 341 | if (fn!=NULL) { |
uci1 | 25:57b2627fe756 | 342 | ++fn; // remove the / |
uci1 | 25:57b2627fe756 | 343 | } else { |
uci1 | 25:57b2627fe756 | 344 | fn = name; |
uci1 | 25:57b2627fe756 | 345 | } |
uci1 | 25:57b2627fe756 | 346 | ffn = ""; |
uci1 | 25:57b2627fe756 | 347 | if (GetRunSeqFromFilename(fn, run, seq)) { |
uci1 | 25:57b2627fe756 | 348 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 349 | printf("got run=%d, seq=%hu\r\n",run,seq); |
uci1 | 25:57b2627fe756 | 350 | #endif |
uci1 | 25:57b2627fe756 | 351 | uint32_t sdlen(0); |
uci1 | 27:efc4d654b139 | 352 | std::string subds( GetSubDirFor(run,seq,sdlen, true) ); |
uci1 | 27:efc4d654b139 | 353 | const char* subd = subds.c_str(); |
uci1 | 25:57b2627fe756 | 354 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 355 | printf("subd=%s\r\n",subd); |
uci1 | 25:57b2627fe756 | 356 | #endif |
uci1 | 25:57b2627fe756 | 357 | ffn = subd; |
uci1 | 25:57b2627fe756 | 358 | ffn += "/"; |
uci1 | 25:57b2627fe756 | 359 | ret = true; |
uci1 | 25:57b2627fe756 | 360 | } |
uci1 | 25:57b2627fe756 | 361 | ffn += fn; |
uci1 | 25:57b2627fe756 | 362 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 363 | printf("ffn=%s, ret=%d\r\n",ffn.c_str(),(int)ret); |
uci1 | 25:57b2627fe756 | 364 | #endif |
uci1 | 25:57b2627fe756 | 365 | return ret; |
uci1 | 25:57b2627fe756 | 366 | } |
uci1 | 25:57b2627fe756 | 367 | |
uci1 | 25:57b2627fe756 | 368 | FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode, |
uci1 | 25:57b2627fe756 | 369 | const bool redoDir) { |
uci1 | 12:d472f9811262 | 370 | // TODO: check if we have memory? |
uci1 | 25:57b2627fe756 | 371 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 372 | printf("OpenSDFile: Trying to open %s.\r\n",name); |
uci1 | 25:57b2627fe756 | 373 | #endif |
uci1 | 64:6c7a316eafad | 374 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 375 | FILE* f = 0; |
uci1 | 56:0bba0ef15697 | 376 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 377 | |
uci1 | 56:0bba0ef15697 | 378 | std::string ffn; |
uci1 | 56:0bba0ef15697 | 379 | bool ok = true; |
uci1 | 56:0bba0ef15697 | 380 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 381 | printf("redoDir=%d\r\n",(int)redoDir); |
uci1 | 56:0bba0ef15697 | 382 | #endif |
uci1 | 56:0bba0ef15697 | 383 | if (redoDir) { |
uci1 | 56:0bba0ef15697 | 384 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 385 | printf("calling GetFullFilename\r\n"); |
uci1 | 56:0bba0ef15697 | 386 | #endif |
uci1 | 56:0bba0ef15697 | 387 | ok = GetFullFilename(name, ffn); |
uci1 | 56:0bba0ef15697 | 388 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 389 | printf("ffn=%s\r\n",ffn.c_str()); |
uci1 | 56:0bba0ef15697 | 390 | #endif |
uci1 | 56:0bba0ef15697 | 391 | } else { |
uci1 | 56:0bba0ef15697 | 392 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 393 | printf("looking for /\r\n"); |
uci1 | 56:0bba0ef15697 | 394 | #endif |
uci1 | 56:0bba0ef15697 | 395 | // make sure the directory exists |
uci1 | 56:0bba0ef15697 | 396 | const char* ld = strrchr(name, '/'); |
uci1 | 25:57b2627fe756 | 397 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 398 | printf("ld=%p, ld-name = %d\r\n",ld,(int)(ld-name)); |
uci1 | 25:57b2627fe756 | 399 | #endif |
uci1 | 56:0bba0ef15697 | 400 | if ((ld!=0) && (ld>name)) { |
uci1 | 56:0bba0ef15697 | 401 | std::string dn(name, ld-name); |
uci1 | 56:0bba0ef15697 | 402 | DIR* d = OpenOrMakeAllDirs(dn.c_str()); |
uci1 | 25:57b2627fe756 | 403 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 404 | printf("d=%p\r\n",d); |
uci1 | 25:57b2627fe756 | 405 | #endif |
uci1 | 56:0bba0ef15697 | 406 | if (d!=NULL) { |
uci1 | 56:0bba0ef15697 | 407 | closedir(d); |
uci1 | 56:0bba0ef15697 | 408 | } |
uci1 | 56:0bba0ef15697 | 409 | } |
uci1 | 56:0bba0ef15697 | 410 | // now just copy the (already-) full name |
uci1 | 56:0bba0ef15697 | 411 | ffn = name; |
uci1 | 56:0bba0ef15697 | 412 | } |
uci1 | 56:0bba0ef15697 | 413 | if ( ok && ffn.size()>0 ) { |
uci1 | 56:0bba0ef15697 | 414 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 415 | printf("OpenSDFile: %s, mode %s\r\n",ffn.c_str(),mode); |
uci1 | 56:0bba0ef15697 | 416 | #endif |
uci1 | 56:0bba0ef15697 | 417 | f = fopen(ffn.c_str(), mode); |
uci1 | 56:0bba0ef15697 | 418 | //setvbuf(f, 0, _IONBF, 0); // no buffering |
uci1 | 56:0bba0ef15697 | 419 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 420 | printf("OpenSDFile: f=%p\r\n",(void*)f); |
uci1 | 56:0bba0ef15697 | 421 | #endif |
uci1 | 56:0bba0ef15697 | 422 | } |
uci1 | 25:57b2627fe756 | 423 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 424 | printf("ffn=%s\r\n",ffn.c_str()); |
uci1 | 25:57b2627fe756 | 425 | #endif |
uci1 | 25:57b2627fe756 | 426 | } |
uci1 | 1:e392595b4b76 | 427 | return f; |
uci1 | 1:e392595b4b76 | 428 | } |
uci1 | 1:e392595b4b76 | 429 | |
uci1 | 0:664899e0b988 | 430 | FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr, |
uci1 | 40:1324da35afd4 | 431 | const uint32_t run, |
uci1 | 40:1324da35afd4 | 432 | const uint16_t minseq) { |
uci1 | 0:664899e0b988 | 433 | // opens a new file in the specified directory and writes this |
uci1 | 0:664899e0b988 | 434 | // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes) |
uci1 | 0:664899e0b988 | 435 | // |
uci1 | 22:f957c4f840ad | 436 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 437 | printf("getting seq num for run %u, minseq %hu\r\n", |
uci1 | 40:1324da35afd4 | 438 | run, minseq); |
uci1 | 22:f957c4f840ad | 439 | #endif |
uci1 | 64:6c7a316eafad | 440 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 441 | fgCurFile = 0; |
uci1 | 12:d472f9811262 | 442 | fgCurSeq = GetSeqNum(macadr, run); |
uci1 | 22:f957c4f840ad | 443 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 444 | printf("fgCurSeq=%hu\r\n",fgCurSeq); |
uci1 | 56:0bba0ef15697 | 445 | #endif |
uci1 | 56:0bba0ef15697 | 446 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 447 | if (fgCurSeq<minseq) { |
uci1 | 56:0bba0ef15697 | 448 | fgCurSeq=minseq; |
uci1 | 56:0bba0ef15697 | 449 | } |
uci1 | 56:0bba0ef15697 | 450 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 451 | printf("getting output file name\r\n"); |
uci1 | 22:f957c4f840ad | 452 | #endif |
uci1 | 56:0bba0ef15697 | 453 | memset(fgCurFileName, 0, sizeof(char)*kFNBufSize); |
uci1 | 56:0bba0ef15697 | 454 | strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1); |
uci1 | 56:0bba0ef15697 | 455 | //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); |
uci1 | 25:57b2627fe756 | 456 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 457 | printf("fgCurFileName=%s\r\n",fgCurFileName); |
uci1 | 25:57b2627fe756 | 458 | #endif |
uci1 | 56:0bba0ef15697 | 459 | fgCurFile = 0; |
uci1 | 56:0bba0ef15697 | 460 | if (fgCurFileName!=NULL) { |
uci1 | 22:f957c4f840ad | 461 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 462 | printf("opening SD file\r\n"); |
uci1 | 22:f957c4f840ad | 463 | #endif |
uci1 | 56:0bba0ef15697 | 464 | fgCurFile = OpenSDFile(fgCurFileName, "wb", false); |
uci1 | 56:0bba0ef15697 | 465 | if (fgCurFile!=NULL && ferror(fgCurFile)==0) { |
uci1 | 19:74155d652c37 | 466 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 467 | printf("Writing file header\r\n"); |
uci1 | 19:74155d652c37 | 468 | #endif |
uci1 | 56:0bba0ef15697 | 469 | WriteFileHeader(fgCurFile, macadr, run, fgCurSeq); |
uci1 | 56:0bba0ef15697 | 470 | |
uci1 | 56:0bba0ef15697 | 471 | AddToRunSeqList(run, fgCurSeq); |
uci1 | 56:0bba0ef15697 | 472 | } |
uci1 | 2:e67f7c158087 | 473 | } |
uci1 | 56:0bba0ef15697 | 474 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 475 | printf("fgCurFile=%p\r\n",(void*)fgCurFile); |
uci1 | 56:0bba0ef15697 | 476 | #endif |
uci1 | 0:664899e0b988 | 477 | } |
uci1 | 1:e392595b4b76 | 478 | return fgCurFile; |
uci1 | 0:664899e0b988 | 479 | } |
uci1 | 0:664899e0b988 | 480 | |
uci1 | 25:57b2627fe756 | 481 | void SnSDUtils::PrintFilesInDirs(const char* dirname) { |
uci1 | 64:6c7a316eafad | 482 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 483 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 484 | |
uci1 | 56:0bba0ef15697 | 485 | DIR* d; |
uci1 | 56:0bba0ef15697 | 486 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 487 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 488 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 489 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 56:0bba0ef15697 | 490 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 491 | printf("dn=%s. datr=%02x. dir=%d\r\n", |
uci1 | 56:0bba0ef15697 | 492 | dent->d_name, |
uci1 | 56:0bba0ef15697 | 493 | dir->filinfo()->fattrib, |
uci1 | 56:0bba0ef15697 | 494 | dir->filinfo()->fattrib & AM_DIR); |
uci1 | 56:0bba0ef15697 | 495 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 56:0bba0ef15697 | 496 | std::string dnm(dirname); |
uci1 | 56:0bba0ef15697 | 497 | dnm += "/"; |
uci1 | 56:0bba0ef15697 | 498 | dnm += dent->d_name; |
uci1 | 56:0bba0ef15697 | 499 | PrintFilesInDirs(dnm.c_str()); |
uci1 | 56:0bba0ef15697 | 500 | } |
uci1 | 25:57b2627fe756 | 501 | } |
uci1 | 56:0bba0ef15697 | 502 | closedir(d); |
uci1 | 25:57b2627fe756 | 503 | } |
uci1 | 25:57b2627fe756 | 504 | } |
uci1 | 25:57b2627fe756 | 505 | } |
uci1 | 25:57b2627fe756 | 506 | |
uci1 | 40:1324da35afd4 | 507 | float SnSDUtils::GetFreeBytes() { |
uci1 | 64:6c7a316eafad | 508 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 509 | float frs(0); |
uci1 | 56:0bba0ef15697 | 510 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 511 | |
uci1 | 56:0bba0ef15697 | 512 | FATFS* fs; |
uci1 | 56:0bba0ef15697 | 513 | DWORD fre_clust; |
uci1 | 56:0bba0ef15697 | 514 | f_getfree("0:",&fre_clust,&fs); |
uci1 | 56:0bba0ef15697 | 515 | frs = static_cast<float>(fs->csize) |
uci1 | 56:0bba0ef15697 | 516 | *static_cast<float>(fs->free_clust) |
uci1 | 40:1324da35afd4 | 517 | #if _MAX_SS != 512 |
uci1 | 56:0bba0ef15697 | 518 | *(fs->ssize); |
uci1 | 40:1324da35afd4 | 519 | #else |
uci1 | 56:0bba0ef15697 | 520 | *512; |
uci1 | 40:1324da35afd4 | 521 | #endif |
uci1 | 40:1324da35afd4 | 522 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 523 | printf("free space = %g b (%g GB, %g MB, %g KB)\r\n", |
uci1 | 56:0bba0ef15697 | 524 | frs, frs/1073741824.0, frs/1048576.0, frs/1024.0); |
uci1 | 40:1324da35afd4 | 525 | #endif |
uci1 | 56:0bba0ef15697 | 526 | } |
uci1 | 40:1324da35afd4 | 527 | return frs; |
uci1 | 40:1324da35afd4 | 528 | } |
uci1 | 40:1324da35afd4 | 529 | |
uci1 | 21:ce51bb0ba4a5 | 530 | void SnSDUtils::GetDirProps(const char* dirname, |
uci1 | 21:ce51bb0ba4a5 | 531 | uint32_t& nfiles, |
uci1 | 21:ce51bb0ba4a5 | 532 | float& totbytes) { |
uci1 | 64:6c7a316eafad | 533 | Watchdog::kick(); // don't reset |
uci1 | 21:ce51bb0ba4a5 | 534 | nfiles = 0; |
uci1 | 21:ce51bb0ba4a5 | 535 | totbytes = 0; |
uci1 | 56:0bba0ef15697 | 536 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 537 | |
uci1 | 56:0bba0ef15697 | 538 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 539 | FATDirHandle* d = static_cast<FATDirHandle*>( opendir(dirname) ); |
uci1 | 56:0bba0ef15697 | 540 | if (d!=0) { |
uci1 | 56:0bba0ef15697 | 541 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 542 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 543 | if ( (d->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 56:0bba0ef15697 | 544 | // a subdirectory |
uci1 | 56:0bba0ef15697 | 545 | std::string dnm(dirname); |
uci1 | 56:0bba0ef15697 | 546 | dnm += "/"; |
uci1 | 56:0bba0ef15697 | 547 | dnm += dent->d_name; |
uci1 | 56:0bba0ef15697 | 548 | uint32_t sdnf; |
uci1 | 56:0bba0ef15697 | 549 | float sdtb; |
uci1 | 56:0bba0ef15697 | 550 | GetDirProps(dnm.c_str(), sdnf, sdtb); |
uci1 | 56:0bba0ef15697 | 551 | nfiles += sdnf; |
uci1 | 56:0bba0ef15697 | 552 | totbytes += sdtb; |
uci1 | 56:0bba0ef15697 | 553 | } else { |
uci1 | 56:0bba0ef15697 | 554 | // a file |
uci1 | 56:0bba0ef15697 | 555 | ++nfiles; |
uci1 | 56:0bba0ef15697 | 556 | totbytes += d->filinfo()->fsize; |
uci1 | 56:0bba0ef15697 | 557 | } |
uci1 | 56:0bba0ef15697 | 558 | } |
uci1 | 56:0bba0ef15697 | 559 | closedir(d); |
uci1 | 21:ce51bb0ba4a5 | 560 | } |
uci1 | 21:ce51bb0ba4a5 | 561 | } |
uci1 | 21:ce51bb0ba4a5 | 562 | #ifdef DEBUG |
uci1 | 22:f957c4f840ad | 563 | printf("GetDirProps: %s :: nf=%u, tb=%g\r\n", |
uci1 | 21:ce51bb0ba4a5 | 564 | dirname, nfiles, totbytes); |
uci1 | 21:ce51bb0ba4a5 | 565 | #endif |
uci1 | 21:ce51bb0ba4a5 | 566 | } |
uci1 | 21:ce51bb0ba4a5 | 567 | |
uci1 | 22:f957c4f840ad | 568 | bool SnSDUtils::WriteHeartbeatTo(FILE* file, |
uci1 | 22:f957c4f840ad | 569 | const uint32_t time, |
uci1 | 22:f957c4f840ad | 570 | const uint32_t num) { |
uci1 | 64:6c7a316eafad | 571 | Watchdog::kick(); // don't reset |
uci1 | 40:1324da35afd4 | 572 | if (file!=0) { |
uci1 | 56:0bba0ef15697 | 573 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 574 | |
uci1 | 56:0bba0ef15697 | 575 | const bool r1 = |
uci1 | 56:0bba0ef15697 | 576 | SnHeaderFrame::WriteTo(file, SnHeaderFrame::kHeartbeatCode, |
uci1 | 56:0bba0ef15697 | 577 | SnHeartbeatFrame::SizeOf(SnHeartbeatFrame::kIOVers)); |
uci1 | 56:0bba0ef15697 | 578 | const bool r2 = |
uci1 | 56:0bba0ef15697 | 579 | SnHeartbeatFrame::WriteTo(file, time, num); |
uci1 | 56:0bba0ef15697 | 580 | return (r1 && r2); |
uci1 | 56:0bba0ef15697 | 581 | } |
uci1 | 40:1324da35afd4 | 582 | } |
uci1 | 56:0bba0ef15697 | 583 | return false; |
uci1 | 40:1324da35afd4 | 584 | } |
uci1 | 40:1324da35afd4 | 585 | |
uci1 | 40:1324da35afd4 | 586 | bool SnSDUtils::WriteTrigWaitWinTime(FILE* file, |
uci1 | 40:1324da35afd4 | 587 | SnClockSetFrame& clkset, |
uci1 | 40:1324da35afd4 | 588 | const bool isStart) { |
uci1 | 64:6c7a316eafad | 589 | Watchdog::kick(); // don't reset |
uci1 | 40:1324da35afd4 | 590 | if (file!=0) { |
uci1 | 56:0bba0ef15697 | 591 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 592 | |
uci1 | 56:0bba0ef15697 | 593 | bool ok = SnHeaderFrame::WriteTo(file, |
uci1 | 56:0bba0ef15697 | 594 | (isStart) ? (SnHeaderFrame::kFileTrgStrtCode) |
uci1 | 56:0bba0ef15697 | 595 | : (SnHeaderFrame::kFileTrgStopCode), |
uci1 | 56:0bba0ef15697 | 596 | clkset.SizeOf()); |
uci1 | 56:0bba0ef15697 | 597 | ok &= (SnCommWin::kOkMsgSent == clkset.WriteTo(file)); |
uci1 | 56:0bba0ef15697 | 598 | return ok; |
uci1 | 56:0bba0ef15697 | 599 | } |
uci1 | 40:1324da35afd4 | 600 | } |
uci1 | 56:0bba0ef15697 | 601 | return false; |
uci1 | 22:f957c4f840ad | 602 | } |
uci1 | 22:f957c4f840ad | 603 | |
uci1 | 0:664899e0b988 | 604 | bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf, |
uci1 | 0:664899e0b988 | 605 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 606 | const SnConfigFrame& conf) { |
uci1 | 64:6c7a316eafad | 607 | Watchdog::kick(); // don't reset |
uci1 | 0:664899e0b988 | 608 | // write event to SD card |
uci1 | 56:0bba0ef15697 | 609 | bool ret = false; |
uci1 | 40:1324da35afd4 | 610 | if (efile!=0) { |
uci1 | 56:0bba0ef15697 | 611 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 612 | |
uci1 | 56:0bba0ef15697 | 613 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 56:0bba0ef15697 | 614 | uint16_t sWvBase=0; |
uci1 | 56:0bba0ef15697 | 615 | conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 56:0bba0ef15697 | 616 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode, |
uci1 | 56:0bba0ef15697 | 617 | evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB)); |
uci1 | 56:0bba0ef15697 | 618 | ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 56:0bba0ef15697 | 619 | fflush(efile); |
uci1 | 56:0bba0ef15697 | 620 | } |
uci1 | 40:1324da35afd4 | 621 | } |
uci1 | 56:0bba0ef15697 | 622 | return ret; |
uci1 | 0:664899e0b988 | 623 | } |
uci1 | 0:664899e0b988 | 624 | |
uci1 | 0:664899e0b988 | 625 | bool SnSDUtils::WriteConfig(FILE* efile, |
uci1 | 0:664899e0b988 | 626 | const SnConfigFrame& conf) { |
uci1 | 64:6c7a316eafad | 627 | Watchdog::kick(); // don't reset |
uci1 | 40:1324da35afd4 | 628 | if (efile!=0) { |
uci1 | 56:0bba0ef15697 | 629 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 630 | |
uci1 | 56:0bba0ef15697 | 631 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode, |
uci1 | 56:0bba0ef15697 | 632 | conf.SizeOf(SnConfigFrame::kIOVers)); |
uci1 | 56:0bba0ef15697 | 633 | conf.WriteTo(efile); |
uci1 | 56:0bba0ef15697 | 634 | return true; |
uci1 | 56:0bba0ef15697 | 635 | } |
uci1 | 40:1324da35afd4 | 636 | } |
uci1 | 56:0bba0ef15697 | 637 | return false; |
uci1 | 0:664899e0b988 | 638 | } |
uci1 | 0:664899e0b988 | 639 | |
uci1 | 0:664899e0b988 | 640 | void SnSDUtils::DeleteFile(FILE*& f, const char* fname) { |
uci1 | 25:57b2627fe756 | 641 | #ifdef DEBUG |
uci1 | 25:57b2627fe756 | 642 | printf("try to delete %s at %p\r\n",fname,f); |
uci1 | 25:57b2627fe756 | 643 | #endif |
uci1 | 64:6c7a316eafad | 644 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 645 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 646 | |
uci1 | 56:0bba0ef15697 | 647 | if (f!=0) { |
uci1 | 56:0bba0ef15697 | 648 | fclose(f); |
uci1 | 56:0bba0ef15697 | 649 | f=0; |
uci1 | 56:0bba0ef15697 | 650 | } |
uci1 | 56:0bba0ef15697 | 651 | std::string fn(""); |
uci1 | 56:0bba0ef15697 | 652 | if (GetFullFilename(fname, fn)) { |
uci1 | 25:57b2627fe756 | 653 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 654 | printf("calling remove [%s]\r\n",fn.c_str()); |
uci1 | 25:57b2627fe756 | 655 | #endif |
uci1 | 56:0bba0ef15697 | 656 | remove(fn.c_str()); |
uci1 | 56:0bba0ef15697 | 657 | } |
uci1 | 21:ce51bb0ba4a5 | 658 | } |
uci1 | 0:664899e0b988 | 659 | } |
uci1 | 0:664899e0b988 | 660 | |
uci1 | 27:efc4d654b139 | 661 | void SnSDUtils::DeleteFilesOfRun(const uint32_t run) { |
uci1 | 27:efc4d654b139 | 662 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 663 | printf("deleteing files of run %lu\r\n",run); |
uci1 | 27:efc4d654b139 | 664 | #endif |
uci1 | 64:6c7a316eafad | 665 | Watchdog::kick(); // don't reset |
uci1 | 27:efc4d654b139 | 666 | uint32_t rdlen(0); |
uci1 | 27:efc4d654b139 | 667 | std::string rdnm( GetSubDirFor(run, 0, rdlen, false) ); |
uci1 | 27:efc4d654b139 | 668 | DeleteAllFiles(rdnm.c_str()); |
uci1 | 27:efc4d654b139 | 669 | } |
uci1 | 27:efc4d654b139 | 670 | |
uci1 | 27:efc4d654b139 | 671 | void SnSDUtils::DeleteAllFiles(const char* dirname) { |
uci1 | 27:efc4d654b139 | 672 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 673 | printf("deleting ALL files in %s\r\n",dirname); |
uci1 | 27:efc4d654b139 | 674 | #endif |
uci1 | 64:6c7a316eafad | 675 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 676 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 677 | |
uci1 | 56:0bba0ef15697 | 678 | DIR* d; |
uci1 | 56:0bba0ef15697 | 679 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 680 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 681 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 56:0bba0ef15697 | 682 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 683 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 684 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 56:0bba0ef15697 | 685 | // a subdirectory |
uci1 | 56:0bba0ef15697 | 686 | std::string dnm(dirname); |
uci1 | 56:0bba0ef15697 | 687 | dnm += "/"; |
uci1 | 56:0bba0ef15697 | 688 | dnm += dent->d_name; |
uci1 | 56:0bba0ef15697 | 689 | // delete all the files in this new subdir |
uci1 | 27:efc4d654b139 | 690 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 691 | printf("call DeleteAllFiles(%s)\r\n",dnm.c_str()); |
uci1 | 27:efc4d654b139 | 692 | #endif |
uci1 | 56:0bba0ef15697 | 693 | DeleteAllFiles(dnm.c_str()); |
uci1 | 56:0bba0ef15697 | 694 | } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 56:0bba0ef15697 | 695 | // a data file (delete it) |
uci1 | 56:0bba0ef15697 | 696 | const bool isCurFile = |
uci1 | 56:0bba0ef15697 | 697 | (strcmp(dent->d_name, GetCurFileName())==0); |
uci1 | 56:0bba0ef15697 | 698 | if (isCurFile==false) { // don't delete the current file |
uci1 | 56:0bba0ef15697 | 699 | FILE* f(0); // dummy |
uci1 | 56:0bba0ef15697 | 700 | DeleteFile(f, dent->d_name); |
uci1 | 56:0bba0ef15697 | 701 | } |
uci1 | 27:efc4d654b139 | 702 | } |
uci1 | 56:0bba0ef15697 | 703 | } // loop over stuff in this dir |
uci1 | 56:0bba0ef15697 | 704 | closedir(d); |
uci1 | 56:0bba0ef15697 | 705 | DeleteDirIfEmpty(dirname); |
uci1 | 56:0bba0ef15697 | 706 | } |
uci1 | 27:efc4d654b139 | 707 | } |
uci1 | 27:efc4d654b139 | 708 | } |
uci1 | 27:efc4d654b139 | 709 | |
uci1 | 56:0bba0ef15697 | 710 | bool SnSDUtils::DeleteDirIfEmpty(const char* dirname) { |
uci1 | 27:efc4d654b139 | 711 | #ifdef DEBUG |
uci1 | 27:efc4d654b139 | 712 | printf("DeleteDirIfEmpty(%s)\r\n",dirname); |
uci1 | 27:efc4d654b139 | 713 | #endif |
uci1 | 64:6c7a316eafad | 714 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 715 | bool doDel = false; |
uci1 | 56:0bba0ef15697 | 716 | if (InitSDCard()) { |
uci1 | 40:1324da35afd4 | 717 | |
uci1 | 56:0bba0ef15697 | 718 | DIR* d; |
uci1 | 56:0bba0ef15697 | 719 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 720 | if ( (d = opendir(dirname))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 721 | dent = readdir(d); |
uci1 | 56:0bba0ef15697 | 722 | if ( dent==NULL ) { |
uci1 | 56:0bba0ef15697 | 723 | // then this directory is empty |
uci1 | 56:0bba0ef15697 | 724 | static const size_t subdsz = strlen(kSDsubDir); |
uci1 | 56:0bba0ef15697 | 725 | // just double check that this directory is |
uci1 | 56:0bba0ef15697 | 726 | // a subdirectory of the data dir |
uci1 | 56:0bba0ef15697 | 727 | if (strlen(dirname)>subdsz) { |
uci1 | 56:0bba0ef15697 | 728 | doDel = true; |
uci1 | 56:0bba0ef15697 | 729 | } |
uci1 | 56:0bba0ef15697 | 730 | } // else this dir isn't empty |
uci1 | 56:0bba0ef15697 | 731 | closedir(d); |
uci1 | 56:0bba0ef15697 | 732 | if (doDel) { |
uci1 | 56:0bba0ef15697 | 733 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 734 | printf("removing directory [%s]\r\n",dirname); |
uci1 | 56:0bba0ef15697 | 735 | #endif |
uci1 | 56:0bba0ef15697 | 736 | remove(dirname); |
uci1 | 27:efc4d654b139 | 737 | } |
uci1 | 27:efc4d654b139 | 738 | } |
uci1 | 27:efc4d654b139 | 739 | } |
uci1 | 56:0bba0ef15697 | 740 | return doDel; |
uci1 | 27:efc4d654b139 | 741 | } |
uci1 | 27:efc4d654b139 | 742 | |
uci1 | 56:0bba0ef15697 | 743 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 744 | SnSDUtils::SendOneFile(const char* dfn, |
uci1 | 66:685f9d0a48ae | 745 | const bool determineDir, |
uci1 | 56:0bba0ef15697 | 746 | SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 747 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 748 | char* const buf, |
uci1 | 56:0bba0ef15697 | 749 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 750 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 751 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 752 | SnPowerFrame& pow) { |
uci1 | 56:0bba0ef15697 | 753 | |
uci1 | 56:0bba0ef15697 | 754 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 755 | printf("SendOneFile (%s)\r\n",dfn); |
uci1 | 56:0bba0ef15697 | 756 | #endif |
uci1 | 64:6c7a316eafad | 757 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 758 | SnCommWin::ECommWinResult res = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 759 | |
uci1 | 56:0bba0ef15697 | 760 | // open the file |
uci1 | 56:0bba0ef15697 | 761 | const bool isCurFile = (strcmp(dfn, GetCurFileName())==0); |
uci1 | 56:0bba0ef15697 | 762 | FILE* f(0); |
uci1 | 56:0bba0ef15697 | 763 | if (isCurFile) { |
uci1 | 56:0bba0ef15697 | 764 | // file must already be written out! |
uci1 | 56:0bba0ef15697 | 765 | f = GetCurFile(); |
uci1 | 56:0bba0ef15697 | 766 | } else { |
uci1 | 66:685f9d0a48ae | 767 | f = OpenExistingFile(dfn, false, determineDir); |
uci1 | 56:0bba0ef15697 | 768 | } |
uci1 | 56:0bba0ef15697 | 769 | |
uci1 | 56:0bba0ef15697 | 770 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 771 | printf("SendOneFile: isCurFile=%d, f=%p, fn=%s\r\n", |
uci1 | 56:0bba0ef15697 | 772 | (int)isCurFile, (void*)f, dfn); |
uci1 | 56:0bba0ef15697 | 773 | #endif |
uci1 | 56:0bba0ef15697 | 774 | |
uci1 | 56:0bba0ef15697 | 775 | uint8_t hndres = SnHeaderFrame::kHnShFailNonCode; |
uci1 | 56:0bba0ef15697 | 776 | if (f!=0) { |
uci1 | 56:0bba0ef15697 | 777 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 778 | printf("SendOneFile: calling SendDataFromFile\r\n"); |
uci1 | 56:0bba0ef15697 | 779 | #endif |
uci1 | 56:0bba0ef15697 | 780 | res = comm->SendDataFromFile(f, dfn, |
uci1 | 56:0bba0ef15697 | 781 | curConf, evt, pow, buf, bsize, |
uci1 | 56:0bba0ef15697 | 782 | 0, timeout, |
uci1 | 56:0bba0ef15697 | 783 | &hndres); |
uci1 | 56:0bba0ef15697 | 784 | |
uci1 | 56:0bba0ef15697 | 785 | if (isCurFile) { |
uci1 | 56:0bba0ef15697 | 786 | // move (back) to the end of the file |
uci1 | 56:0bba0ef15697 | 787 | // altho hopefully no writing will happen after this |
uci1 | 56:0bba0ef15697 | 788 | fseek(fgCurFile, 0, SEEK_END); |
uci1 | 56:0bba0ef15697 | 789 | } |
uci1 | 56:0bba0ef15697 | 790 | } |
uci1 | 56:0bba0ef15697 | 791 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 792 | printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n", |
uci1 | 56:0bba0ef15697 | 793 | (int)isCurFile, (int)res, (int)(curConf.IsDeletingFiles()), |
uci1 | 56:0bba0ef15697 | 794 | hndres); |
uci1 | 56:0bba0ef15697 | 795 | #endif |
uci1 | 56:0bba0ef15697 | 796 | |
uci1 | 56:0bba0ef15697 | 797 | return res; |
uci1 | 56:0bba0ef15697 | 798 | } |
uci1 | 56:0bba0ef15697 | 799 | |
uci1 | 56:0bba0ef15697 | 800 | bool SnSDUtils::ClearRunSeqList() { |
uci1 | 64:6c7a316eafad | 801 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 802 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 803 | FILE* rslistf = fopen(kRunSeqListFilenm,"w"); |
uci1 | 56:0bba0ef15697 | 804 | const bool ok = rslistf!=0; |
uci1 | 56:0bba0ef15697 | 805 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 806 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 807 | printf("ClearRunSeqList. ok=%s\r\n",(ok?"true":"false")); |
uci1 | 56:0bba0ef15697 | 808 | #endif |
uci1 | 56:0bba0ef15697 | 809 | return ok; |
uci1 | 56:0bba0ef15697 | 810 | } |
uci1 | 56:0bba0ef15697 | 811 | return false; |
uci1 | 56:0bba0ef15697 | 812 | } |
uci1 | 56:0bba0ef15697 | 813 | |
uci1 | 56:0bba0ef15697 | 814 | bool SnSDUtils::AddToRunSeqList(const uint32_t run, |
uci1 | 56:0bba0ef15697 | 815 | const uint16_t seq) { |
uci1 | 64:6c7a316eafad | 816 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 817 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 818 | FILE* rslistf = fopen(kRunSeqListFilenm,"a"); |
uci1 | 56:0bba0ef15697 | 819 | bool ok = false; |
uci1 | 56:0bba0ef15697 | 820 | if (rslistf!=0) { |
uci1 | 56:0bba0ef15697 | 821 | ok = fprintf(rslistf, "%u %hu\n", run, seq) > 0; |
uci1 | 56:0bba0ef15697 | 822 | ok &= 0==ferror(rslistf); |
uci1 | 56:0bba0ef15697 | 823 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 824 | printf("AddToRunSeqList: run=%u, seq=%hu, ok=%s\r\n", |
uci1 | 56:0bba0ef15697 | 825 | run, seq, (ok?"true":"false")); |
uci1 | 56:0bba0ef15697 | 826 | #endif |
uci1 | 56:0bba0ef15697 | 827 | } |
uci1 | 56:0bba0ef15697 | 828 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 829 | return ok; |
uci1 | 56:0bba0ef15697 | 830 | } |
uci1 | 56:0bba0ef15697 | 831 | return false; |
uci1 | 56:0bba0ef15697 | 832 | } |
uci1 | 56:0bba0ef15697 | 833 | |
uci1 | 56:0bba0ef15697 | 834 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 835 | SnSDUtils::SendFileWithRunSeq(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 836 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 837 | char* const buf, |
uci1 | 56:0bba0ef15697 | 838 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 839 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 840 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 841 | SnPowerFrame& pow, |
uci1 | 56:0bba0ef15697 | 842 | const uint32_t run, |
uci1 | 56:0bba0ef15697 | 843 | const uint16_t seq) { |
uci1 | 56:0bba0ef15697 | 844 | |
uci1 | 56:0bba0ef15697 | 845 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 846 | printf("SendFileWithRunSeq\r\n"); |
uci1 | 56:0bba0ef15697 | 847 | #endif |
uci1 | 64:6c7a316eafad | 848 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 849 | |
uci1 | 56:0bba0ef15697 | 850 | // get the file name |
uci1 | 56:0bba0ef15697 | 851 | std::string dfn = |
uci1 | 56:0bba0ef15697 | 852 | GetOutFileName(SnConfigFrame::GetMacAddress(), run, seq); |
uci1 | 56:0bba0ef15697 | 853 | |
uci1 | 56:0bba0ef15697 | 854 | // send it |
uci1 | 56:0bba0ef15697 | 855 | const SnCommWin::ECommWinResult res = |
uci1 | 66:685f9d0a48ae | 856 | SendOneFile(dfn.c_str(), false, comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 857 | curConf, evt, pow); |
uci1 | 56:0bba0ef15697 | 858 | |
uci1 | 56:0bba0ef15697 | 859 | // see if we need to remove this directory now |
uci1 | 56:0bba0ef15697 | 860 | if (curConf.IsDeletingFiles()) { |
uci1 | 56:0bba0ef15697 | 861 | // get run/seq directory name |
uci1 | 56:0bba0ef15697 | 862 | uint32_t rdlen(0); |
uci1 | 56:0bba0ef15697 | 863 | std::string rdnm( GetSubDirFor(run, seq, rdlen, true) ); |
uci1 | 56:0bba0ef15697 | 864 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 865 | printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 866 | #endif |
uci1 | 56:0bba0ef15697 | 867 | if ( DeleteDirIfEmpty(rdnm.c_str()) ) { |
uci1 | 56:0bba0ef15697 | 868 | // removed the seq dir. do we need to remove |
uci1 | 56:0bba0ef15697 | 869 | // the run dir too? |
uci1 | 56:0bba0ef15697 | 870 | rdnm = GetSubDirFor(run, seq, rdlen, false); |
uci1 | 56:0bba0ef15697 | 871 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 872 | printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 873 | #endif |
uci1 | 56:0bba0ef15697 | 874 | DeleteDirIfEmpty(rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 875 | } |
uci1 | 56:0bba0ef15697 | 876 | } |
uci1 | 56:0bba0ef15697 | 877 | |
uci1 | 56:0bba0ef15697 | 878 | return res; |
uci1 | 56:0bba0ef15697 | 879 | } |
uci1 | 56:0bba0ef15697 | 880 | |
uci1 | 56:0bba0ef15697 | 881 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 882 | SnSDUtils::SendFilesInRunSeqList(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 883 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 884 | char* const buf, |
uci1 | 56:0bba0ef15697 | 885 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 886 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 887 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 888 | SnPowerFrame& pow) { |
uci1 | 56:0bba0ef15697 | 889 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 890 | printf("SendFilesInRunSeqList\r\n"); |
uci1 | 56:0bba0ef15697 | 891 | #endif |
uci1 | 56:0bba0ef15697 | 892 | |
uci1 | 56:0bba0ef15697 | 893 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 894 | |
uci1 | 64:6c7a316eafad | 895 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 896 | |
uci1 | 56:0bba0ef15697 | 897 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 898 | |
uci1 | 56:0bba0ef15697 | 899 | // open up the run/seq list file and send each corresponding file |
uci1 | 56:0bba0ef15697 | 900 | FILE* rslistf = fopen(kRunSeqListFilenm,"r"); |
uci1 | 56:0bba0ef15697 | 901 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 902 | printf("fslistf=%p\r\n",(void*)rslistf); |
uci1 | 56:0bba0ef15697 | 903 | #endif |
uci1 | 56:0bba0ef15697 | 904 | if (rslistf!=0) { |
uci1 | 56:0bba0ef15697 | 905 | uint32_t run(0); |
uci1 | 56:0bba0ef15697 | 906 | uint16_t seq(0); |
uci1 | 56:0bba0ef15697 | 907 | while ( (feof(rslistf)==0) |
uci1 | 56:0bba0ef15697 | 908 | && (ferror(rslistf)==0) ) { |
uci1 | 56:0bba0ef15697 | 909 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 910 | printf("feof=%d, ferror=%d\r\n", |
uci1 | 56:0bba0ef15697 | 911 | (int)(feof(rslistf)), (int)(ferror(rslistf))); |
uci1 | 56:0bba0ef15697 | 912 | #endif |
uci1 | 64:6c7a316eafad | 913 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 914 | |
uci1 | 56:0bba0ef15697 | 915 | const int nfilled = fscanf(rslistf, "%u %hu\n", &run ,&seq); |
uci1 | 56:0bba0ef15697 | 916 | |
uci1 | 56:0bba0ef15697 | 917 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 918 | printf("nfilled=%d\r\n", nfilled); |
uci1 | 56:0bba0ef15697 | 919 | #endif |
uci1 | 56:0bba0ef15697 | 920 | |
uci1 | 56:0bba0ef15697 | 921 | if ( 2==nfilled ) { |
uci1 | 56:0bba0ef15697 | 922 | |
uci1 | 56:0bba0ef15697 | 923 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 924 | printf("run=%u, seq=%hu\r\n",run,seq); |
uci1 | 56:0bba0ef15697 | 925 | #endif |
uci1 | 56:0bba0ef15697 | 926 | |
uci1 | 56:0bba0ef15697 | 927 | SnCommWin::ECommWinResult res = |
uci1 | 56:0bba0ef15697 | 928 | SendFileWithRunSeq(comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 929 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 930 | run, seq); |
uci1 | 56:0bba0ef15697 | 931 | |
uci1 | 56:0bba0ef15697 | 932 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 933 | rs = res; |
uci1 | 56:0bba0ef15697 | 934 | } |
uci1 | 56:0bba0ef15697 | 935 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 936 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 937 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 938 | break; |
uci1 | 56:0bba0ef15697 | 939 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 940 | break; |
uci1 | 56:0bba0ef15697 | 941 | } |
uci1 | 56:0bba0ef15697 | 942 | |
uci1 | 56:0bba0ef15697 | 943 | } |
uci1 | 56:0bba0ef15697 | 944 | |
uci1 | 56:0bba0ef15697 | 945 | } |
uci1 | 56:0bba0ef15697 | 946 | |
uci1 | 56:0bba0ef15697 | 947 | // do we need to clear the list? |
uci1 | 56:0bba0ef15697 | 948 | if (curConf.IsRunSeqListOneCommWinOnly()==false) { |
uci1 | 56:0bba0ef15697 | 949 | if (rs >= SnCommWin::kOkMsgSent) { |
uci1 | 56:0bba0ef15697 | 950 | // all sent ok |
uci1 | 56:0bba0ef15697 | 951 | ClearRunSeqList(); |
uci1 | 56:0bba0ef15697 | 952 | } |
uci1 | 56:0bba0ef15697 | 953 | } |
uci1 | 56:0bba0ef15697 | 954 | |
uci1 | 56:0bba0ef15697 | 955 | } |
uci1 | 56:0bba0ef15697 | 956 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 957 | } |
uci1 | 56:0bba0ef15697 | 958 | return rs; |
uci1 | 56:0bba0ef15697 | 959 | } |
uci1 | 56:0bba0ef15697 | 960 | |
uci1 | 56:0bba0ef15697 | 961 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 962 | SnSDUtils::SendPartOfRun(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 963 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 964 | char* const buf, |
uci1 | 56:0bba0ef15697 | 965 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 966 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 967 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 968 | SnPowerFrame& pow, |
uci1 | 56:0bba0ef15697 | 969 | const uint32_t run, |
uci1 | 56:0bba0ef15697 | 970 | const uint16_t minseq, |
uci1 | 56:0bba0ef15697 | 971 | const uint16_t maxseq) { |
uci1 | 56:0bba0ef15697 | 972 | // send files with run number 'run' |
uci1 | 56:0bba0ef15697 | 973 | // and seq number in [minseq,maxseq] (min/max inclusive) |
uci1 | 56:0bba0ef15697 | 974 | |
uci1 | 56:0bba0ef15697 | 975 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 976 | printf("SendPartOfRun\r\n"); |
uci1 | 56:0bba0ef15697 | 977 | #endif |
uci1 | 56:0bba0ef15697 | 978 | |
uci1 | 64:6c7a316eafad | 979 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 980 | |
uci1 | 56:0bba0ef15697 | 981 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 982 | |
uci1 | 56:0bba0ef15697 | 983 | for (uint16_t seq=minseq; seq<=maxseq; ++seq) { |
uci1 | 64:6c7a316eafad | 984 | |
uci1 | 64:6c7a316eafad | 985 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 986 | |
uci1 | 56:0bba0ef15697 | 987 | SnCommWin::ECommWinResult res = |
uci1 | 56:0bba0ef15697 | 988 | SendFileWithRunSeq(comm, |
uci1 | 56:0bba0ef15697 | 989 | timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 990 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 991 | run, seq); |
uci1 | 56:0bba0ef15697 | 992 | |
uci1 | 56:0bba0ef15697 | 993 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 994 | rs = res; |
uci1 | 56:0bba0ef15697 | 995 | } |
uci1 | 56:0bba0ef15697 | 996 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 997 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 998 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 999 | break; |
uci1 | 56:0bba0ef15697 | 1000 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 1001 | break; |
uci1 | 56:0bba0ef15697 | 1002 | } |
uci1 | 56:0bba0ef15697 | 1003 | |
uci1 | 56:0bba0ef15697 | 1004 | } |
uci1 | 56:0bba0ef15697 | 1005 | |
uci1 | 56:0bba0ef15697 | 1006 | return rs; |
uci1 | 56:0bba0ef15697 | 1007 | } |
uci1 | 56:0bba0ef15697 | 1008 | |
uci1 | 56:0bba0ef15697 | 1009 | |
uci1 | 40:1324da35afd4 | 1010 | SnCommWin::ECommWinResult SnSDUtils::SendAllOfRun(SnCommWin* comm, |
uci1 | 40:1324da35afd4 | 1011 | const uint32_t timeout, |
uci1 | 40:1324da35afd4 | 1012 | char* const buf, |
uci1 | 40:1324da35afd4 | 1013 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 1014 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 1015 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 1016 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 1017 | const uint32_t runnum) { |
uci1 | 40:1324da35afd4 | 1018 | // send all files in a run |
uci1 | 64:6c7a316eafad | 1019 | |
uci1 | 64:6c7a316eafad | 1020 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1021 | |
uci1 | 40:1324da35afd4 | 1022 | // get the run dir |
uci1 | 40:1324da35afd4 | 1023 | uint32_t rdlen(0); |
uci1 | 40:1324da35afd4 | 1024 | std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) ); |
uci1 | 40:1324da35afd4 | 1025 | return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, |
uci1 | 40:1324da35afd4 | 1026 | rdnms.c_str()); |
uci1 | 40:1324da35afd4 | 1027 | } |
uci1 | 40:1324da35afd4 | 1028 | |
uci1 | 0:664899e0b988 | 1029 | SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, |
uci1 | 3:24c5f0f50bf1 | 1030 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 1031 | char* const buf, |
uci1 | 6:6f002d202f59 | 1032 | const uint32_t bsize, |
uci1 | 6:6f002d202f59 | 1033 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 1034 | SnEventFrame& evt, |
uci1 | 12:d472f9811262 | 1035 | SnPowerFrame& pow, |
uci1 | 25:57b2627fe756 | 1036 | const char* dirname) { |
uci1 | 40:1324da35afd4 | 1037 | // send all files in the specified directory |
uci1 | 64:6c7a316eafad | 1038 | |
uci1 | 64:6c7a316eafad | 1039 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1040 | |
uci1 | 56:0bba0ef15697 | 1041 | SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail; |
uci1 | 40:1324da35afd4 | 1042 | |
uci1 | 56:0bba0ef15697 | 1043 | if (InitSDCard()) { |
uci1 | 16:744ce85aede2 | 1044 | |
uci1 | 56:0bba0ef15697 | 1045 | rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 1046 | |
uci1 | 56:0bba0ef15697 | 1047 | DIR* d; |
uci1 | 56:0bba0ef15697 | 1048 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 1049 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 1050 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 56:0bba0ef15697 | 1051 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 1052 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 1053 | SnCommWin::ECommWinResult res = rs; |
uci1 | 56:0bba0ef15697 | 1054 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 56:0bba0ef15697 | 1055 | // a subdirectory |
uci1 | 56:0bba0ef15697 | 1056 | std::string dnm(dirname); |
uci1 | 56:0bba0ef15697 | 1057 | dnm += "/"; |
uci1 | 56:0bba0ef15697 | 1058 | dnm += dent->d_name; |
uci1 | 56:0bba0ef15697 | 1059 | // send all the files in this new subdir |
uci1 | 56:0bba0ef15697 | 1060 | res = SendAllFiles(comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 1061 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 1062 | dnm.c_str()); |
uci1 | 56:0bba0ef15697 | 1063 | } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 56:0bba0ef15697 | 1064 | // a data file (send it) |
uci1 | 66:685f9d0a48ae | 1065 | res = SendOneFile(dent->d_name, true, comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 1066 | curConf, evt, pow); |
uci1 | 1:e392595b4b76 | 1067 | } |
uci1 | 56:0bba0ef15697 | 1068 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 1069 | rs = res; |
uci1 | 56:0bba0ef15697 | 1070 | } |
uci1 | 56:0bba0ef15697 | 1071 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 1072 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 1073 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 1074 | break; |
uci1 | 56:0bba0ef15697 | 1075 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 1076 | break; |
uci1 | 56:0bba0ef15697 | 1077 | } |
uci1 | 56:0bba0ef15697 | 1078 | } // loop over stuff in this directory |
uci1 | 56:0bba0ef15697 | 1079 | closedir(d); |
uci1 | 56:0bba0ef15697 | 1080 | // see if we need to remove this directory now |
uci1 | 56:0bba0ef15697 | 1081 | if (curConf.IsDeletingFiles()) { |
uci1 | 56:0bba0ef15697 | 1082 | DeleteDirIfEmpty(dirname); |
uci1 | 25:57b2627fe756 | 1083 | } |
uci1 | 0:664899e0b988 | 1084 | } |
uci1 | 0:664899e0b988 | 1085 | } |
uci1 | 0:664899e0b988 | 1086 | return rs; |
uci1 | 0:664899e0b988 | 1087 | } |
uci1 | 0:664899e0b988 | 1088 |