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