Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.cpp@64:6c7a316eafad, 2014-11-28 (annotated)
- Committer:
- uci1
- Date:
- Fri Nov 28 07:42:57 2014 +0000
- Revision:
- 64:6c7a316eafad
- Parent:
- 63:4820a4460f00
- Child:
- 65:2cb3e99ce466
add watchdog kicks to SnCommWin and SnSDUtils
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 | 63:4820a4460f00 | 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 | 56:0bba0ef15697 | 317 | /* |
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 | 56:0bba0ef15697 | 745 | SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 746 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 747 | char* const buf, |
uci1 | 56:0bba0ef15697 | 748 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 749 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 750 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 751 | SnPowerFrame& pow) { |
uci1 | 56:0bba0ef15697 | 752 | |
uci1 | 56:0bba0ef15697 | 753 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 754 | printf("SendOneFile (%s)\r\n",dfn); |
uci1 | 56:0bba0ef15697 | 755 | #endif |
uci1 | 64:6c7a316eafad | 756 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 757 | SnCommWin::ECommWinResult res = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 758 | |
uci1 | 56:0bba0ef15697 | 759 | // open the file |
uci1 | 56:0bba0ef15697 | 760 | const bool isCurFile = (strcmp(dfn, GetCurFileName())==0); |
uci1 | 56:0bba0ef15697 | 761 | FILE* f(0); |
uci1 | 56:0bba0ef15697 | 762 | if (isCurFile) { |
uci1 | 56:0bba0ef15697 | 763 | // file must already be written out! |
uci1 | 56:0bba0ef15697 | 764 | f = GetCurFile(); |
uci1 | 56:0bba0ef15697 | 765 | } else { |
uci1 | 56:0bba0ef15697 | 766 | f = OpenExistingFile(dfn, false, false); |
uci1 | 56:0bba0ef15697 | 767 | } |
uci1 | 56:0bba0ef15697 | 768 | |
uci1 | 56:0bba0ef15697 | 769 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 770 | printf("SendOneFile: isCurFile=%d, f=%p, fn=%s\r\n", |
uci1 | 56:0bba0ef15697 | 771 | (int)isCurFile, (void*)f, dfn); |
uci1 | 56:0bba0ef15697 | 772 | #endif |
uci1 | 56:0bba0ef15697 | 773 | |
uci1 | 56:0bba0ef15697 | 774 | uint8_t hndres = SnHeaderFrame::kHnShFailNonCode; |
uci1 | 56:0bba0ef15697 | 775 | if (f!=0) { |
uci1 | 56:0bba0ef15697 | 776 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 777 | printf("SendOneFile: calling SendDataFromFile\r\n"); |
uci1 | 56:0bba0ef15697 | 778 | #endif |
uci1 | 56:0bba0ef15697 | 779 | res = comm->SendDataFromFile(f, dfn, |
uci1 | 56:0bba0ef15697 | 780 | curConf, evt, pow, buf, bsize, |
uci1 | 56:0bba0ef15697 | 781 | 0, timeout, |
uci1 | 56:0bba0ef15697 | 782 | &hndres); |
uci1 | 56:0bba0ef15697 | 783 | |
uci1 | 56:0bba0ef15697 | 784 | if (isCurFile) { |
uci1 | 56:0bba0ef15697 | 785 | // move (back) to the end of the file |
uci1 | 56:0bba0ef15697 | 786 | // altho hopefully no writing will happen after this |
uci1 | 56:0bba0ef15697 | 787 | fseek(fgCurFile, 0, SEEK_END); |
uci1 | 56:0bba0ef15697 | 788 | } |
uci1 | 56:0bba0ef15697 | 789 | } |
uci1 | 56:0bba0ef15697 | 790 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 791 | printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n", |
uci1 | 56:0bba0ef15697 | 792 | (int)isCurFile, (int)res, (int)(curConf.IsDeletingFiles()), |
uci1 | 56:0bba0ef15697 | 793 | hndres); |
uci1 | 56:0bba0ef15697 | 794 | #endif |
uci1 | 56:0bba0ef15697 | 795 | |
uci1 | 56:0bba0ef15697 | 796 | return res; |
uci1 | 56:0bba0ef15697 | 797 | } |
uci1 | 56:0bba0ef15697 | 798 | |
uci1 | 56:0bba0ef15697 | 799 | bool SnSDUtils::ClearRunSeqList() { |
uci1 | 64:6c7a316eafad | 800 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 801 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 802 | FILE* rslistf = fopen(kRunSeqListFilenm,"w"); |
uci1 | 56:0bba0ef15697 | 803 | const bool ok = rslistf!=0; |
uci1 | 56:0bba0ef15697 | 804 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 805 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 806 | printf("ClearRunSeqList. ok=%s\r\n",(ok?"true":"false")); |
uci1 | 56:0bba0ef15697 | 807 | #endif |
uci1 | 56:0bba0ef15697 | 808 | return ok; |
uci1 | 56:0bba0ef15697 | 809 | } |
uci1 | 56:0bba0ef15697 | 810 | return false; |
uci1 | 56:0bba0ef15697 | 811 | } |
uci1 | 56:0bba0ef15697 | 812 | |
uci1 | 56:0bba0ef15697 | 813 | bool SnSDUtils::AddToRunSeqList(const uint32_t run, |
uci1 | 56:0bba0ef15697 | 814 | const uint16_t seq) { |
uci1 | 64:6c7a316eafad | 815 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 816 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 817 | FILE* rslistf = fopen(kRunSeqListFilenm,"a"); |
uci1 | 56:0bba0ef15697 | 818 | bool ok = false; |
uci1 | 56:0bba0ef15697 | 819 | if (rslistf!=0) { |
uci1 | 56:0bba0ef15697 | 820 | ok = fprintf(rslistf, "%u %hu\n", run, seq) > 0; |
uci1 | 56:0bba0ef15697 | 821 | ok &= 0==ferror(rslistf); |
uci1 | 56:0bba0ef15697 | 822 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 823 | printf("AddToRunSeqList: run=%u, seq=%hu, ok=%s\r\n", |
uci1 | 56:0bba0ef15697 | 824 | run, seq, (ok?"true":"false")); |
uci1 | 56:0bba0ef15697 | 825 | #endif |
uci1 | 56:0bba0ef15697 | 826 | } |
uci1 | 56:0bba0ef15697 | 827 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 828 | return ok; |
uci1 | 56:0bba0ef15697 | 829 | } |
uci1 | 56:0bba0ef15697 | 830 | return false; |
uci1 | 56:0bba0ef15697 | 831 | } |
uci1 | 56:0bba0ef15697 | 832 | |
uci1 | 56:0bba0ef15697 | 833 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 834 | SnSDUtils::SendFileWithRunSeq(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 835 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 836 | char* const buf, |
uci1 | 56:0bba0ef15697 | 837 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 838 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 839 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 840 | SnPowerFrame& pow, |
uci1 | 56:0bba0ef15697 | 841 | const uint32_t run, |
uci1 | 56:0bba0ef15697 | 842 | const uint16_t seq) { |
uci1 | 56:0bba0ef15697 | 843 | |
uci1 | 56:0bba0ef15697 | 844 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 845 | printf("SendFileWithRunSeq\r\n"); |
uci1 | 56:0bba0ef15697 | 846 | #endif |
uci1 | 64:6c7a316eafad | 847 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 848 | |
uci1 | 56:0bba0ef15697 | 849 | // get the file name |
uci1 | 56:0bba0ef15697 | 850 | std::string dfn = |
uci1 | 56:0bba0ef15697 | 851 | GetOutFileName(SnConfigFrame::GetMacAddress(), run, seq); |
uci1 | 56:0bba0ef15697 | 852 | |
uci1 | 56:0bba0ef15697 | 853 | // send it |
uci1 | 56:0bba0ef15697 | 854 | const SnCommWin::ECommWinResult res = |
uci1 | 56:0bba0ef15697 | 855 | SendOneFile(dfn.c_str(), comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 856 | curConf, evt, pow); |
uci1 | 56:0bba0ef15697 | 857 | |
uci1 | 56:0bba0ef15697 | 858 | // see if we need to remove this directory now |
uci1 | 56:0bba0ef15697 | 859 | if (curConf.IsDeletingFiles()) { |
uci1 | 56:0bba0ef15697 | 860 | // get run/seq directory name |
uci1 | 56:0bba0ef15697 | 861 | uint32_t rdlen(0); |
uci1 | 56:0bba0ef15697 | 862 | std::string rdnm( GetSubDirFor(run, seq, rdlen, true) ); |
uci1 | 56:0bba0ef15697 | 863 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 864 | printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 865 | #endif |
uci1 | 56:0bba0ef15697 | 866 | if ( DeleteDirIfEmpty(rdnm.c_str()) ) { |
uci1 | 56:0bba0ef15697 | 867 | // removed the seq dir. do we need to remove |
uci1 | 56:0bba0ef15697 | 868 | // the run dir too? |
uci1 | 56:0bba0ef15697 | 869 | rdnm = GetSubDirFor(run, seq, rdlen, false); |
uci1 | 56:0bba0ef15697 | 870 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 871 | printf("Checking removal of dir [%s]\r\n",rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 872 | #endif |
uci1 | 56:0bba0ef15697 | 873 | DeleteDirIfEmpty(rdnm.c_str()); |
uci1 | 56:0bba0ef15697 | 874 | } |
uci1 | 56:0bba0ef15697 | 875 | } |
uci1 | 56:0bba0ef15697 | 876 | |
uci1 | 56:0bba0ef15697 | 877 | return res; |
uci1 | 56:0bba0ef15697 | 878 | } |
uci1 | 56:0bba0ef15697 | 879 | |
uci1 | 56:0bba0ef15697 | 880 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 881 | SnSDUtils::SendFilesInRunSeqList(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 882 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 883 | char* const buf, |
uci1 | 56:0bba0ef15697 | 884 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 885 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 886 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 887 | SnPowerFrame& pow) { |
uci1 | 56:0bba0ef15697 | 888 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 889 | printf("SendFilesInRunSeqList\r\n"); |
uci1 | 56:0bba0ef15697 | 890 | #endif |
uci1 | 56:0bba0ef15697 | 891 | |
uci1 | 56:0bba0ef15697 | 892 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 893 | |
uci1 | 64:6c7a316eafad | 894 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 895 | |
uci1 | 56:0bba0ef15697 | 896 | if (InitSDCard()) { |
uci1 | 56:0bba0ef15697 | 897 | |
uci1 | 56:0bba0ef15697 | 898 | // open up the run/seq list file and send each corresponding file |
uci1 | 56:0bba0ef15697 | 899 | FILE* rslistf = fopen(kRunSeqListFilenm,"r"); |
uci1 | 56:0bba0ef15697 | 900 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 901 | printf("fslistf=%p\r\n",(void*)rslistf); |
uci1 | 56:0bba0ef15697 | 902 | #endif |
uci1 | 56:0bba0ef15697 | 903 | if (rslistf!=0) { |
uci1 | 56:0bba0ef15697 | 904 | uint32_t run(0); |
uci1 | 56:0bba0ef15697 | 905 | uint16_t seq(0); |
uci1 | 56:0bba0ef15697 | 906 | while ( (feof(rslistf)==0) |
uci1 | 56:0bba0ef15697 | 907 | && (ferror(rslistf)==0) ) { |
uci1 | 56:0bba0ef15697 | 908 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 909 | printf("feof=%d, ferror=%d\r\n", |
uci1 | 56:0bba0ef15697 | 910 | (int)(feof(rslistf)), (int)(ferror(rslistf))); |
uci1 | 56:0bba0ef15697 | 911 | #endif |
uci1 | 64:6c7a316eafad | 912 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 913 | |
uci1 | 56:0bba0ef15697 | 914 | const int nfilled = fscanf(rslistf, "%u %hu\n", &run ,&seq); |
uci1 | 56:0bba0ef15697 | 915 | |
uci1 | 56:0bba0ef15697 | 916 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 917 | printf("nfilled=%d\r\n", nfilled); |
uci1 | 56:0bba0ef15697 | 918 | #endif |
uci1 | 56:0bba0ef15697 | 919 | |
uci1 | 56:0bba0ef15697 | 920 | if ( 2==nfilled ) { |
uci1 | 56:0bba0ef15697 | 921 | |
uci1 | 56:0bba0ef15697 | 922 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 923 | printf("run=%u, seq=%hu\r\n",run,seq); |
uci1 | 56:0bba0ef15697 | 924 | #endif |
uci1 | 56:0bba0ef15697 | 925 | |
uci1 | 56:0bba0ef15697 | 926 | SnCommWin::ECommWinResult res = |
uci1 | 56:0bba0ef15697 | 927 | SendFileWithRunSeq(comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 928 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 929 | run, seq); |
uci1 | 56:0bba0ef15697 | 930 | |
uci1 | 56:0bba0ef15697 | 931 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 932 | rs = res; |
uci1 | 56:0bba0ef15697 | 933 | } |
uci1 | 56:0bba0ef15697 | 934 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 935 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 936 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 937 | break; |
uci1 | 56:0bba0ef15697 | 938 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 939 | break; |
uci1 | 56:0bba0ef15697 | 940 | } |
uci1 | 56:0bba0ef15697 | 941 | |
uci1 | 56:0bba0ef15697 | 942 | } |
uci1 | 56:0bba0ef15697 | 943 | |
uci1 | 56:0bba0ef15697 | 944 | } |
uci1 | 56:0bba0ef15697 | 945 | |
uci1 | 56:0bba0ef15697 | 946 | // do we need to clear the list? |
uci1 | 56:0bba0ef15697 | 947 | if (curConf.IsRunSeqListOneCommWinOnly()==false) { |
uci1 | 56:0bba0ef15697 | 948 | if (rs >= SnCommWin::kOkMsgSent) { |
uci1 | 56:0bba0ef15697 | 949 | // all sent ok |
uci1 | 56:0bba0ef15697 | 950 | ClearRunSeqList(); |
uci1 | 56:0bba0ef15697 | 951 | } |
uci1 | 56:0bba0ef15697 | 952 | } |
uci1 | 56:0bba0ef15697 | 953 | |
uci1 | 56:0bba0ef15697 | 954 | } |
uci1 | 56:0bba0ef15697 | 955 | fclose(rslistf); |
uci1 | 56:0bba0ef15697 | 956 | } |
uci1 | 56:0bba0ef15697 | 957 | return rs; |
uci1 | 56:0bba0ef15697 | 958 | } |
uci1 | 56:0bba0ef15697 | 959 | |
uci1 | 56:0bba0ef15697 | 960 | SnCommWin::ECommWinResult |
uci1 | 56:0bba0ef15697 | 961 | SnSDUtils::SendPartOfRun(SnCommWin* comm, |
uci1 | 56:0bba0ef15697 | 962 | const uint32_t timeout, |
uci1 | 56:0bba0ef15697 | 963 | char* const buf, |
uci1 | 56:0bba0ef15697 | 964 | const uint32_t bsize, |
uci1 | 56:0bba0ef15697 | 965 | const SnConfigFrame& curConf, |
uci1 | 56:0bba0ef15697 | 966 | SnEventFrame& evt, |
uci1 | 56:0bba0ef15697 | 967 | SnPowerFrame& pow, |
uci1 | 56:0bba0ef15697 | 968 | const uint32_t run, |
uci1 | 56:0bba0ef15697 | 969 | const uint16_t minseq, |
uci1 | 56:0bba0ef15697 | 970 | const uint16_t maxseq) { |
uci1 | 56:0bba0ef15697 | 971 | // send files with run number 'run' |
uci1 | 56:0bba0ef15697 | 972 | // and seq number in [minseq,maxseq] (min/max inclusive) |
uci1 | 56:0bba0ef15697 | 973 | |
uci1 | 56:0bba0ef15697 | 974 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 975 | printf("SendPartOfRun\r\n"); |
uci1 | 56:0bba0ef15697 | 976 | #endif |
uci1 | 56:0bba0ef15697 | 977 | |
uci1 | 64:6c7a316eafad | 978 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 979 | |
uci1 | 56:0bba0ef15697 | 980 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 981 | |
uci1 | 56:0bba0ef15697 | 982 | for (uint16_t seq=minseq; seq<=maxseq; ++seq) { |
uci1 | 64:6c7a316eafad | 983 | |
uci1 | 64:6c7a316eafad | 984 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 985 | |
uci1 | 56:0bba0ef15697 | 986 | SnCommWin::ECommWinResult res = |
uci1 | 56:0bba0ef15697 | 987 | SendFileWithRunSeq(comm, |
uci1 | 56:0bba0ef15697 | 988 | timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 989 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 990 | run, seq); |
uci1 | 56:0bba0ef15697 | 991 | |
uci1 | 56:0bba0ef15697 | 992 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 993 | rs = res; |
uci1 | 56:0bba0ef15697 | 994 | } |
uci1 | 56:0bba0ef15697 | 995 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 996 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 997 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 998 | break; |
uci1 | 56:0bba0ef15697 | 999 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 1000 | break; |
uci1 | 56:0bba0ef15697 | 1001 | } |
uci1 | 56:0bba0ef15697 | 1002 | |
uci1 | 56:0bba0ef15697 | 1003 | } |
uci1 | 56:0bba0ef15697 | 1004 | |
uci1 | 56:0bba0ef15697 | 1005 | return rs; |
uci1 | 56:0bba0ef15697 | 1006 | } |
uci1 | 56:0bba0ef15697 | 1007 | |
uci1 | 56:0bba0ef15697 | 1008 | |
uci1 | 40:1324da35afd4 | 1009 | SnCommWin::ECommWinResult SnSDUtils::SendAllOfRun(SnCommWin* comm, |
uci1 | 40:1324da35afd4 | 1010 | const uint32_t timeout, |
uci1 | 40:1324da35afd4 | 1011 | char* const buf, |
uci1 | 40:1324da35afd4 | 1012 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 1013 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 1014 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 1015 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 1016 | const uint32_t runnum) { |
uci1 | 40:1324da35afd4 | 1017 | // send all files in a run |
uci1 | 64:6c7a316eafad | 1018 | |
uci1 | 64:6c7a316eafad | 1019 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1020 | |
uci1 | 40:1324da35afd4 | 1021 | // get the run dir |
uci1 | 40:1324da35afd4 | 1022 | uint32_t rdlen(0); |
uci1 | 40:1324da35afd4 | 1023 | std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) ); |
uci1 | 40:1324da35afd4 | 1024 | return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow, |
uci1 | 40:1324da35afd4 | 1025 | rdnms.c_str()); |
uci1 | 40:1324da35afd4 | 1026 | } |
uci1 | 40:1324da35afd4 | 1027 | |
uci1 | 0:664899e0b988 | 1028 | SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, |
uci1 | 3:24c5f0f50bf1 | 1029 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 1030 | char* const buf, |
uci1 | 6:6f002d202f59 | 1031 | const uint32_t bsize, |
uci1 | 6:6f002d202f59 | 1032 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 1033 | SnEventFrame& evt, |
uci1 | 12:d472f9811262 | 1034 | SnPowerFrame& pow, |
uci1 | 25:57b2627fe756 | 1035 | const char* dirname) { |
uci1 | 40:1324da35afd4 | 1036 | // send all files in the specified directory |
uci1 | 64:6c7a316eafad | 1037 | |
uci1 | 64:6c7a316eafad | 1038 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1039 | |
uci1 | 56:0bba0ef15697 | 1040 | SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail; |
uci1 | 40:1324da35afd4 | 1041 | |
uci1 | 56:0bba0ef15697 | 1042 | if (InitSDCard()) { |
uci1 | 16:744ce85aede2 | 1043 | |
uci1 | 56:0bba0ef15697 | 1044 | rs = SnCommWin::kOkMsgSent; |
uci1 | 56:0bba0ef15697 | 1045 | |
uci1 | 56:0bba0ef15697 | 1046 | DIR* d; |
uci1 | 56:0bba0ef15697 | 1047 | struct dirent* dent; |
uci1 | 56:0bba0ef15697 | 1048 | if ( (d = opendir( dirname ))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 1049 | FATDirHandle* dir = static_cast<FATDirHandle*>(d); |
uci1 | 56:0bba0ef15697 | 1050 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 56:0bba0ef15697 | 1051 | Watchdog::kick(); // don't reset |
uci1 | 56:0bba0ef15697 | 1052 | SnCommWin::ECommWinResult res = rs; |
uci1 | 56:0bba0ef15697 | 1053 | if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) { |
uci1 | 56:0bba0ef15697 | 1054 | // a subdirectory |
uci1 | 56:0bba0ef15697 | 1055 | std::string dnm(dirname); |
uci1 | 56:0bba0ef15697 | 1056 | dnm += "/"; |
uci1 | 56:0bba0ef15697 | 1057 | dnm += dent->d_name; |
uci1 | 56:0bba0ef15697 | 1058 | // send all the files in this new subdir |
uci1 | 56:0bba0ef15697 | 1059 | res = SendAllFiles(comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 1060 | curConf, evt, pow, |
uci1 | 56:0bba0ef15697 | 1061 | dnm.c_str()); |
uci1 | 56:0bba0ef15697 | 1062 | } else if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 56:0bba0ef15697 | 1063 | // a data file (send it) |
uci1 | 56:0bba0ef15697 | 1064 | res = SendOneFile(dent->d_name, comm, timeout, buf, bsize, |
uci1 | 56:0bba0ef15697 | 1065 | curConf, evt, pow); |
uci1 | 1:e392595b4b76 | 1066 | } |
uci1 | 56:0bba0ef15697 | 1067 | if ((res<rs) || (res==SnCommWin::kOkStopComm)) { |
uci1 | 56:0bba0ef15697 | 1068 | rs = res; |
uci1 | 56:0bba0ef15697 | 1069 | } |
uci1 | 56:0bba0ef15697 | 1070 | // don't necessarily stop if rs is bad. don't want one bad file to |
uci1 | 56:0bba0ef15697 | 1071 | // prevent sending the others |
uci1 | 56:0bba0ef15697 | 1072 | if (rs<=SnCommWin::kFailTimeout) { |
uci1 | 56:0bba0ef15697 | 1073 | break; |
uci1 | 56:0bba0ef15697 | 1074 | } else if (rs==SnCommWin::kOkStopComm) { |
uci1 | 56:0bba0ef15697 | 1075 | break; |
uci1 | 56:0bba0ef15697 | 1076 | } |
uci1 | 56:0bba0ef15697 | 1077 | } // loop over stuff in this directory |
uci1 | 56:0bba0ef15697 | 1078 | closedir(d); |
uci1 | 56:0bba0ef15697 | 1079 | // see if we need to remove this directory now |
uci1 | 56:0bba0ef15697 | 1080 | if (curConf.IsDeletingFiles()) { |
uci1 | 56:0bba0ef15697 | 1081 | DeleteDirIfEmpty(dirname); |
uci1 | 25:57b2627fe756 | 1082 | } |
uci1 | 0:664899e0b988 | 1083 | } |
uci1 | 0:664899e0b988 | 1084 | } |
uci1 | 0:664899e0b988 | 1085 | return rs; |
uci1 | 0:664899e0b988 | 1086 | } |
uci1 | 0:664899e0b988 | 1087 |