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