Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sun Nov 22 04:28:56 2015 +0000
Revision:
103:0ea896a0953a
Parent:
84:80b15993944e
Child:
122:c1b5023eac69
Stn17 with conf name. Fix so SD card loss does not stall station. No interface chip. Safety nets.

Who changed what in which revision?

UserRevisionLine numberNew 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 25:57b2627fe756 525 void SnSDUtils::PrintFilesInDirs(const char* dirname) {
uci1 64:6c7a316eafad 526 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 527 if (InitSDCard()) {
uci1 40:1324da35afd4 528
uci1 56:0bba0ef15697 529 DIR* d;
uci1 56:0bba0ef15697 530 struct dirent* dent;
uci1 56:0bba0ef15697 531 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 532 if ( (d = opendir( dirname ))!=NULL ) {
uci1 56:0bba0ef15697 533 FATDirHandle* dir = static_cast<FATDirHandle*>(d);
uci1 56:0bba0ef15697 534 while ( (dent = readdir(d))!=NULL ) {
uci1 56:0bba0ef15697 535 printf("dn=%s. datr=%02x. dir=%d\r\n",
uci1 56:0bba0ef15697 536 dent->d_name,
uci1 56:0bba0ef15697 537 dir->filinfo()->fattrib,
uci1 56:0bba0ef15697 538 dir->filinfo()->fattrib & AM_DIR);
uci1 56:0bba0ef15697 539 if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) {
uci1 56:0bba0ef15697 540 std::string dnm(dirname);
uci1 56:0bba0ef15697 541 dnm += "/";
uci1 56:0bba0ef15697 542 dnm += dent->d_name;
uci1 56:0bba0ef15697 543 PrintFilesInDirs(dnm.c_str());
uci1 56:0bba0ef15697 544 }
uci1 25:57b2627fe756 545 }
uci1 103:0ea896a0953a 546 if (d!=0) {
uci1 103:0ea896a0953a 547 closedir(d);
uci1 103:0ea896a0953a 548 }
uci1 25:57b2627fe756 549 }
uci1 25:57b2627fe756 550 }
uci1 25:57b2627fe756 551 }
uci1 25:57b2627fe756 552
uci1 40:1324da35afd4 553 float SnSDUtils::GetFreeBytes() {
uci1 64:6c7a316eafad 554 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 555 float frs(0);
uci1 56:0bba0ef15697 556 if (InitSDCard()) {
uci1 40:1324da35afd4 557
uci1 56:0bba0ef15697 558 FATFS* fs;
uci1 56:0bba0ef15697 559 DWORD fre_clust;
uci1 56:0bba0ef15697 560 f_getfree("0:",&fre_clust,&fs);
uci1 103:0ea896a0953a 561 if (fs!=0) {
uci1 103:0ea896a0953a 562 frs = static_cast<float>(fs->csize)
uci1 103:0ea896a0953a 563 *static_cast<float>(fs->free_clust)
uci1 40:1324da35afd4 564 #if _MAX_SS != 512
uci1 103:0ea896a0953a 565 *(fs->ssize);
uci1 40:1324da35afd4 566 #else
uci1 103:0ea896a0953a 567 *512;
uci1 40:1324da35afd4 568 #endif
uci1 40:1324da35afd4 569 #ifdef DEBUG
uci1 103:0ea896a0953a 570 printf("free space = %g b (%g GB, %g MB, %g KB)\r\n",
uci1 103:0ea896a0953a 571 frs, frs/1073741824.0, frs/1048576.0, frs/1024.0);
uci1 40:1324da35afd4 572 #endif
uci1 103:0ea896a0953a 573 }
uci1 56:0bba0ef15697 574 }
uci1 40:1324da35afd4 575 return frs;
uci1 40:1324da35afd4 576 }
uci1 40:1324da35afd4 577
uci1 21:ce51bb0ba4a5 578 void SnSDUtils::GetDirProps(const char* dirname,
uci1 21:ce51bb0ba4a5 579 uint32_t& nfiles,
uci1 21:ce51bb0ba4a5 580 float& totbytes) {
uci1 64:6c7a316eafad 581 Watchdog::kick(); // don't reset
uci1 21:ce51bb0ba4a5 582 nfiles = 0;
uci1 21:ce51bb0ba4a5 583 totbytes = 0;
uci1 56:0bba0ef15697 584 if (InitSDCard()) {
uci1 56:0bba0ef15697 585
uci1 56:0bba0ef15697 586 struct dirent* dent;
uci1 56:0bba0ef15697 587 FATDirHandle* d = static_cast<FATDirHandle*>( opendir(dirname) );
uci1 56:0bba0ef15697 588 if (d!=0) {
uci1 56:0bba0ef15697 589 while ( (dent = readdir(d))!=NULL ) {
uci1 56:0bba0ef15697 590 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 591 if ( (d->filinfo()->fattrib & AM_DIR)!=0 ) {
uci1 56:0bba0ef15697 592 // a subdirectory
uci1 56:0bba0ef15697 593 std::string dnm(dirname);
uci1 56:0bba0ef15697 594 dnm += "/";
uci1 56:0bba0ef15697 595 dnm += dent->d_name;
uci1 56:0bba0ef15697 596 uint32_t sdnf;
uci1 56:0bba0ef15697 597 float sdtb;
uci1 56:0bba0ef15697 598 GetDirProps(dnm.c_str(), sdnf, sdtb);
uci1 56:0bba0ef15697 599 nfiles += sdnf;
uci1 56:0bba0ef15697 600 totbytes += sdtb;
uci1 56:0bba0ef15697 601 } else {
uci1 56:0bba0ef15697 602 // a file
uci1 56:0bba0ef15697 603 ++nfiles;
uci1 103:0ea896a0953a 604 if (d->filinfo()!=0) {
uci1 103:0ea896a0953a 605 totbytes += d->filinfo()->fsize;
uci1 103:0ea896a0953a 606 }
uci1 56:0bba0ef15697 607 }
uci1 56:0bba0ef15697 608 }
uci1 103:0ea896a0953a 609 if (d!=0) {
uci1 103:0ea896a0953a 610 closedir(d);
uci1 103:0ea896a0953a 611 }
uci1 21:ce51bb0ba4a5 612 }
uci1 21:ce51bb0ba4a5 613 }
uci1 21:ce51bb0ba4a5 614 #ifdef DEBUG
uci1 22:f957c4f840ad 615 printf("GetDirProps: %s :: nf=%u, tb=%g\r\n",
uci1 21:ce51bb0ba4a5 616 dirname, nfiles, totbytes);
uci1 21:ce51bb0ba4a5 617 #endif
uci1 21:ce51bb0ba4a5 618 }
uci1 21:ce51bb0ba4a5 619
uci1 84:80b15993944e 620 bool SnSDUtils::WriteHeartbeatTo(FILE* file, const SnHeartbeatFrame& htbt) {
uci1 64:6c7a316eafad 621 Watchdog::kick(); // don't reset
uci1 40:1324da35afd4 622 if (file!=0) {
uci1 56:0bba0ef15697 623 if (InitSDCard()) {
uci1 56:0bba0ef15697 624
uci1 56:0bba0ef15697 625 const bool r1 =
uci1 56:0bba0ef15697 626 SnHeaderFrame::WriteTo(file, SnHeaderFrame::kHeartbeatCode,
uci1 56:0bba0ef15697 627 SnHeartbeatFrame::SizeOf(SnHeartbeatFrame::kIOVers));
uci1 84:80b15993944e 628 const bool r2 = htbt.WriteTo(file);
uci1 84:80b15993944e 629 //SnHeartbeatFrame::WriteTo(file, time, num);
uci1 56:0bba0ef15697 630 return (r1 && r2);
uci1 56:0bba0ef15697 631 }
uci1 40:1324da35afd4 632 }
uci1 56:0bba0ef15697 633 return false;
uci1 40:1324da35afd4 634 }
uci1 40:1324da35afd4 635
uci1 40:1324da35afd4 636 bool SnSDUtils::WriteTrigWaitWinTime(FILE* file,
uci1 40:1324da35afd4 637 SnClockSetFrame& clkset,
uci1 40:1324da35afd4 638 const bool isStart) {
uci1 64:6c7a316eafad 639 Watchdog::kick(); // don't reset
uci1 40:1324da35afd4 640 if (file!=0) {
uci1 56:0bba0ef15697 641 if (InitSDCard()) {
uci1 56:0bba0ef15697 642
uci1 56:0bba0ef15697 643 bool ok = SnHeaderFrame::WriteTo(file,
uci1 56:0bba0ef15697 644 (isStart) ? (SnHeaderFrame::kFileTrgStrtCode)
uci1 56:0bba0ef15697 645 : (SnHeaderFrame::kFileTrgStopCode),
uci1 56:0bba0ef15697 646 clkset.SizeOf());
uci1 56:0bba0ef15697 647 ok &= (SnCommWin::kOkMsgSent == clkset.WriteTo(file));
uci1 56:0bba0ef15697 648 return ok;
uci1 56:0bba0ef15697 649 }
uci1 40:1324da35afd4 650 }
uci1 56:0bba0ef15697 651 return false;
uci1 22:f957c4f840ad 652 }
uci1 22:f957c4f840ad 653
uci1 0:664899e0b988 654 bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 655 const SnEventFrame& evt,
uci1 0:664899e0b988 656 const SnConfigFrame& conf) {
uci1 64:6c7a316eafad 657 Watchdog::kick(); // don't reset
uci1 0:664899e0b988 658 // write event to SD card
uci1 56:0bba0ef15697 659 bool ret = false;
uci1 40:1324da35afd4 660 if (efile!=0) {
uci1 56:0bba0ef15697 661 if (InitSDCard()) {
uci1 56:0bba0ef15697 662
uci1 56:0bba0ef15697 663 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 56:0bba0ef15697 664 uint16_t sWvBase=0;
uci1 56:0bba0ef15697 665 conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
uci1 56:0bba0ef15697 666 SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode,
uci1 56:0bba0ef15697 667 evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB));
uci1 56:0bba0ef15697 668 ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 56:0bba0ef15697 669 fflush(efile);
uci1 56:0bba0ef15697 670 }
uci1 40:1324da35afd4 671 }
uci1 56:0bba0ef15697 672 return ret;
uci1 0:664899e0b988 673 }
uci1 0:664899e0b988 674
uci1 0:664899e0b988 675 bool SnSDUtils::WriteConfig(FILE* efile,
uci1 0:664899e0b988 676 const SnConfigFrame& conf) {
uci1 64:6c7a316eafad 677 Watchdog::kick(); // don't reset
uci1 40:1324da35afd4 678 if (efile!=0) {
uci1 56:0bba0ef15697 679 if (InitSDCard()) {
uci1 40:1324da35afd4 680
uci1 56:0bba0ef15697 681 SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode,
uci1 56:0bba0ef15697 682 conf.SizeOf(SnConfigFrame::kIOVers));
uci1 56:0bba0ef15697 683 conf.WriteTo(efile);
uci1 56:0bba0ef15697 684 return true;
uci1 56:0bba0ef15697 685 }
uci1 40:1324da35afd4 686 }
uci1 56:0bba0ef15697 687 return false;
uci1 0:664899e0b988 688 }
uci1 0:664899e0b988 689
uci1 0:664899e0b988 690 void SnSDUtils::DeleteFile(FILE*& f, const char* fname) {
uci1 25:57b2627fe756 691 #ifdef DEBUG
uci1 25:57b2627fe756 692 printf("try to delete %s at %p\r\n",fname,f);
uci1 25:57b2627fe756 693 #endif
uci1 64:6c7a316eafad 694 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 695 if (InitSDCard()) {
uci1 40:1324da35afd4 696
uci1 56:0bba0ef15697 697 if (f!=0) {
uci1 56:0bba0ef15697 698 fclose(f);
uci1 56:0bba0ef15697 699 f=0;
uci1 56:0bba0ef15697 700 }
uci1 56:0bba0ef15697 701 std::string fn("");
uci1 56:0bba0ef15697 702 if (GetFullFilename(fname, fn)) {
uci1 25:57b2627fe756 703 #ifdef DEBUG
uci1 56:0bba0ef15697 704 printf("calling remove [%s]\r\n",fn.c_str());
uci1 25:57b2627fe756 705 #endif
uci1 56:0bba0ef15697 706 remove(fn.c_str());
uci1 56:0bba0ef15697 707 }
uci1 21:ce51bb0ba4a5 708 }
uci1 0:664899e0b988 709 }
uci1 0:664899e0b988 710
uci1 27:efc4d654b139 711 void SnSDUtils::DeleteFilesOfRun(const uint32_t run) {
uci1 27:efc4d654b139 712 #ifdef DEBUG
uci1 27:efc4d654b139 713 printf("deleteing files of run %lu\r\n",run);
uci1 27:efc4d654b139 714 #endif
uci1 64:6c7a316eafad 715 Watchdog::kick(); // don't reset
uci1 27:efc4d654b139 716 uint32_t rdlen(0);
uci1 27:efc4d654b139 717 std::string rdnm( GetSubDirFor(run, 0, rdlen, false) );
uci1 103:0ea896a0953a 718 if (rdlen!=0) {
uci1 103:0ea896a0953a 719 DeleteAllFiles(rdnm.c_str());
uci1 103:0ea896a0953a 720 }
uci1 103:0ea896a0953a 721 #ifdef DEBUG
uci1 103:0ea896a0953a 722 else {
uci1 103:0ea896a0953a 723 printf("could not get subdir -- not deleting files\r\n");
uci1 103:0ea896a0953a 724 }
uci1 103:0ea896a0953a 725 #endif
uci1 27:efc4d654b139 726 }
uci1 27:efc4d654b139 727
uci1 27:efc4d654b139 728 void SnSDUtils::DeleteAllFiles(const char* dirname) {
uci1 27:efc4d654b139 729 #ifdef DEBUG
uci1 27:efc4d654b139 730 printf("deleting ALL files in %s\r\n",dirname);
uci1 27:efc4d654b139 731 #endif
uci1 64:6c7a316eafad 732 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 733 if (InitSDCard()) {
uci1 40:1324da35afd4 734
uci1 56:0bba0ef15697 735 DIR* d;
uci1 56:0bba0ef15697 736 struct dirent* dent;
uci1 56:0bba0ef15697 737 if ( (d = opendir( dirname ))!=NULL ) {
uci1 56:0bba0ef15697 738 FATDirHandle* dir = static_cast<FATDirHandle*>(d);
uci1 103:0ea896a0953a 739 if (dir!=0) {
uci1 103:0ea896a0953a 740 while ( (dent = readdir(d))!=NULL ) {
uci1 103:0ea896a0953a 741 Watchdog::kick(); // don't reset
uci1 103:0ea896a0953a 742 if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) {
uci1 103:0ea896a0953a 743 // a subdirectory
uci1 103:0ea896a0953a 744 std::string dnm(dirname);
uci1 103:0ea896a0953a 745 dnm += "/";
uci1 103:0ea896a0953a 746 dnm += dent->d_name;
uci1 103:0ea896a0953a 747 // delete all the files in this new subdir
uci1 27:efc4d654b139 748 #ifdef DEBUG
uci1 103:0ea896a0953a 749 printf("call DeleteAllFiles(%s)\r\n",dnm.c_str());
uci1 27:efc4d654b139 750 #endif
uci1 103:0ea896a0953a 751 DeleteAllFiles(dnm.c_str());
uci1 103:0ea896a0953a 752 } else if (strncmp(dent->d_name, "SnEvts", 6)==0) {
uci1 103:0ea896a0953a 753 // a data file (delete it)
uci1 103:0ea896a0953a 754 const bool isCurFile =
uci1 103:0ea896a0953a 755 (strcmp(dent->d_name, GetCurFileName())==0);
uci1 103:0ea896a0953a 756 if (isCurFile==false) { // don't delete the current file
uci1 103:0ea896a0953a 757 FILE* f(0); // dummy
uci1 103:0ea896a0953a 758 DeleteFile(f, dent->d_name);
uci1 103:0ea896a0953a 759 }
uci1 56:0bba0ef15697 760 }
uci1 103:0ea896a0953a 761 } // loop over stuff in this dir
uci1 103:0ea896a0953a 762 }
uci1 103:0ea896a0953a 763 if (d!=0) {
uci1 103:0ea896a0953a 764 closedir(d);
uci1 103:0ea896a0953a 765 }
uci1 56:0bba0ef15697 766 DeleteDirIfEmpty(dirname);
uci1 56:0bba0ef15697 767 }
uci1 27:efc4d654b139 768 }
uci1 27:efc4d654b139 769 }
uci1 27:efc4d654b139 770
uci1 56:0bba0ef15697 771 bool SnSDUtils::DeleteDirIfEmpty(const char* dirname) {
uci1 27:efc4d654b139 772 #ifdef DEBUG
uci1 27:efc4d654b139 773 printf("DeleteDirIfEmpty(%s)\r\n",dirname);
uci1 27:efc4d654b139 774 #endif
uci1 64:6c7a316eafad 775 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 776 bool doDel = false;
uci1 56:0bba0ef15697 777 if (InitSDCard()) {
uci1 40:1324da35afd4 778
uci1 56:0bba0ef15697 779 DIR* d;
uci1 56:0bba0ef15697 780 struct dirent* dent;
uci1 56:0bba0ef15697 781 if ( (d = opendir(dirname))!=NULL ) {
uci1 56:0bba0ef15697 782 dent = readdir(d);
uci1 56:0bba0ef15697 783 if ( dent==NULL ) {
uci1 56:0bba0ef15697 784 // then this directory is empty
uci1 56:0bba0ef15697 785 static const size_t subdsz = strlen(kSDsubDir);
uci1 56:0bba0ef15697 786 // just double check that this directory is
uci1 56:0bba0ef15697 787 // a subdirectory of the data dir
uci1 56:0bba0ef15697 788 if (strlen(dirname)>subdsz) {
uci1 56:0bba0ef15697 789 doDel = true;
uci1 56:0bba0ef15697 790 }
uci1 56:0bba0ef15697 791 } // else this dir isn't empty
uci1 103:0ea896a0953a 792 if (d!=0) {
uci1 103:0ea896a0953a 793 closedir(d);
uci1 103:0ea896a0953a 794 }
uci1 56:0bba0ef15697 795 if (doDel) {
uci1 56:0bba0ef15697 796 #ifdef DEBUG
uci1 56:0bba0ef15697 797 printf("removing directory [%s]\r\n",dirname);
uci1 56:0bba0ef15697 798 #endif
uci1 56:0bba0ef15697 799 remove(dirname);
uci1 27:efc4d654b139 800 }
uci1 27:efc4d654b139 801 }
uci1 27:efc4d654b139 802 }
uci1 56:0bba0ef15697 803 return doDel;
uci1 27:efc4d654b139 804 }
uci1 27:efc4d654b139 805
uci1 56:0bba0ef15697 806 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 807 SnSDUtils::SendOneFile(const char* dfn,
uci1 66:685f9d0a48ae 808 const bool determineDir,
uci1 56:0bba0ef15697 809 SnCommWin* comm,
uci1 56:0bba0ef15697 810 const uint32_t timeout,
uci1 56:0bba0ef15697 811 char* const buf,
uci1 56:0bba0ef15697 812 const uint32_t bsize,
uci1 56:0bba0ef15697 813 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 814 SnEventFrame& evt,
uci1 56:0bba0ef15697 815 SnPowerFrame& pow) {
uci1 56:0bba0ef15697 816
uci1 56:0bba0ef15697 817 #ifdef DEBUG
uci1 56:0bba0ef15697 818 printf("SendOneFile (%s)\r\n",dfn);
uci1 56:0bba0ef15697 819 #endif
uci1 64:6c7a316eafad 820 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 821 SnCommWin::ECommWinResult res = SnCommWin::kOkMsgSent;
uci1 56:0bba0ef15697 822
uci1 56:0bba0ef15697 823 // open the file
uci1 56:0bba0ef15697 824 const bool isCurFile = (strcmp(dfn, GetCurFileName())==0);
uci1 56:0bba0ef15697 825 FILE* f(0);
uci1 56:0bba0ef15697 826 if (isCurFile) {
uci1 56:0bba0ef15697 827 // file must already be written out!
uci1 56:0bba0ef15697 828 f = GetCurFile();
uci1 56:0bba0ef15697 829 } else {
uci1 66:685f9d0a48ae 830 f = OpenExistingFile(dfn, false, determineDir);
uci1 56:0bba0ef15697 831 }
uci1 56:0bba0ef15697 832
uci1 56:0bba0ef15697 833 #ifdef DEBUG
uci1 56:0bba0ef15697 834 printf("SendOneFile: isCurFile=%d, f=%p, fn=%s\r\n",
uci1 56:0bba0ef15697 835 (int)isCurFile, (void*)f, dfn);
uci1 56:0bba0ef15697 836 #endif
uci1 56:0bba0ef15697 837
uci1 56:0bba0ef15697 838 uint8_t hndres = SnHeaderFrame::kHnShFailNonCode;
uci1 56:0bba0ef15697 839 if (f!=0) {
uci1 56:0bba0ef15697 840 #ifdef DEBUG
uci1 56:0bba0ef15697 841 printf("SendOneFile: calling SendDataFromFile\r\n");
uci1 56:0bba0ef15697 842 #endif
uci1 56:0bba0ef15697 843 res = comm->SendDataFromFile(f, dfn,
uci1 56:0bba0ef15697 844 curConf, evt, pow, buf, bsize,
uci1 56:0bba0ef15697 845 0, timeout,
uci1 56:0bba0ef15697 846 &hndres);
uci1 56:0bba0ef15697 847
uci1 56:0bba0ef15697 848 if (isCurFile) {
uci1 56:0bba0ef15697 849 // move (back) to the end of the file
uci1 56:0bba0ef15697 850 // altho hopefully no writing will happen after this
uci1 56:0bba0ef15697 851 fseek(fgCurFile, 0, SEEK_END);
uci1 56:0bba0ef15697 852 }
uci1 56:0bba0ef15697 853 }
uci1 56:0bba0ef15697 854 #ifdef DEBUG
uci1 56:0bba0ef15697 855 printf("isCurFile=%d, res=%d, deleting=%d, hndres=%02x\r\n",
uci1 56:0bba0ef15697 856 (int)isCurFile, (int)res, (int)(curConf.IsDeletingFiles()),
uci1 56:0bba0ef15697 857 hndres);
uci1 56:0bba0ef15697 858 #endif
uci1 56:0bba0ef15697 859
uci1 56:0bba0ef15697 860 return res;
uci1 56:0bba0ef15697 861 }
uci1 56:0bba0ef15697 862
uci1 76:f8383f0292c2 863 bool SnSDUtils::ClearRunSeqList(const bool useRSlist) {
uci1 64:6c7a316eafad 864 Watchdog::kick(); // don't reset
uci1 76:f8383f0292c2 865 if (useRSlist==false) {
uci1 76:f8383f0292c2 866 return true;
uci1 76:f8383f0292c2 867 } else if (InitSDCard()) {
uci1 56:0bba0ef15697 868 FILE* rslistf = fopen(kRunSeqListFilenm,"w");
uci1 76:f8383f0292c2 869 const bool ok = rslistf!=0 && (ferror(rslistf)==0);
uci1 76:f8383f0292c2 870 if (ok) {
uci1 76:f8383f0292c2 871 fclose(rslistf);
uci1 76:f8383f0292c2 872 }
uci1 56:0bba0ef15697 873 #ifdef DEBUG
uci1 56:0bba0ef15697 874 printf("ClearRunSeqList. ok=%s\r\n",(ok?"true":"false"));
uci1 56:0bba0ef15697 875 #endif
uci1 56:0bba0ef15697 876 return ok;
uci1 56:0bba0ef15697 877 }
uci1 56:0bba0ef15697 878 return false;
uci1 56:0bba0ef15697 879 }
uci1 56:0bba0ef15697 880
uci1 56:0bba0ef15697 881 bool SnSDUtils::AddToRunSeqList(const uint32_t run,
uci1 76:f8383f0292c2 882 const uint16_t seq,
uci1 76:f8383f0292c2 883 const bool useRSlist) {
uci1 64:6c7a316eafad 884 Watchdog::kick(); // don't reset
uci1 76:f8383f0292c2 885 if (useRSlist==false) {
uci1 76:f8383f0292c2 886 return true;
uci1 76:f8383f0292c2 887 } else if (InitSDCard()) {
uci1 56:0bba0ef15697 888 FILE* rslistf = fopen(kRunSeqListFilenm,"a");
uci1 76:f8383f0292c2 889 bool ok = (rslistf!=0) && (ferror(rslistf)==0);
uci1 76:f8383f0292c2 890 if (ok) {
uci1 56:0bba0ef15697 891 ok = fprintf(rslistf, "%u %hu\n", run, seq) > 0;
uci1 56:0bba0ef15697 892 ok &= 0==ferror(rslistf);
uci1 56:0bba0ef15697 893 #ifdef DEBUG
uci1 56:0bba0ef15697 894 printf("AddToRunSeqList: run=%u, seq=%hu, ok=%s\r\n",
uci1 56:0bba0ef15697 895 run, seq, (ok?"true":"false"));
uci1 56:0bba0ef15697 896 #endif
uci1 76:f8383f0292c2 897 fclose(rslistf);
uci1 56:0bba0ef15697 898 }
uci1 56:0bba0ef15697 899 return ok;
uci1 56:0bba0ef15697 900 }
uci1 56:0bba0ef15697 901 return false;
uci1 56:0bba0ef15697 902 }
uci1 56:0bba0ef15697 903
uci1 56:0bba0ef15697 904 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 905 SnSDUtils::SendFileWithRunSeq(SnCommWin* comm,
uci1 56:0bba0ef15697 906 const uint32_t timeout,
uci1 56:0bba0ef15697 907 char* const buf,
uci1 56:0bba0ef15697 908 const uint32_t bsize,
uci1 56:0bba0ef15697 909 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 910 SnEventFrame& evt,
uci1 56:0bba0ef15697 911 SnPowerFrame& pow,
uci1 56:0bba0ef15697 912 const uint32_t run,
uci1 56:0bba0ef15697 913 const uint16_t seq) {
uci1 56:0bba0ef15697 914
uci1 56:0bba0ef15697 915 #ifdef DEBUG
uci1 56:0bba0ef15697 916 printf("SendFileWithRunSeq\r\n");
uci1 56:0bba0ef15697 917 #endif
uci1 64:6c7a316eafad 918 Watchdog::kick(); // don't reset
uci1 56:0bba0ef15697 919
uci1 56:0bba0ef15697 920 // get the file name
uci1 56:0bba0ef15697 921 std::string dfn =
uci1 56:0bba0ef15697 922 GetOutFileName(SnConfigFrame::GetMacAddress(), run, seq);
uci1 56:0bba0ef15697 923
uci1 56:0bba0ef15697 924 // send it
uci1 56:0bba0ef15697 925 const SnCommWin::ECommWinResult res =
uci1 66:685f9d0a48ae 926 SendOneFile(dfn.c_str(), false, comm, timeout, buf, bsize,
uci1 56:0bba0ef15697 927 curConf, evt, pow);
uci1 56:0bba0ef15697 928
uci1 56:0bba0ef15697 929 // see if we need to remove this directory now
uci1 56:0bba0ef15697 930 if (curConf.IsDeletingFiles()) {
uci1 56:0bba0ef15697 931 // get run/seq directory name
uci1 56:0bba0ef15697 932 uint32_t rdlen(0);
uci1 56:0bba0ef15697 933 std::string rdnm( GetSubDirFor(run, seq, rdlen, true) );
uci1 56:0bba0ef15697 934 #ifdef DEBUG
uci1 56:0bba0ef15697 935 printf("Checking removal of dir [%s]\r\n",rdnm.c_str());
uci1 56:0bba0ef15697 936 #endif
uci1 103:0ea896a0953a 937 if ( (rdlen!=0) && DeleteDirIfEmpty(rdnm.c_str()) ) {
uci1 56:0bba0ef15697 938 // removed the seq dir. do we need to remove
uci1 56:0bba0ef15697 939 // the run dir too?
uci1 56:0bba0ef15697 940 rdnm = GetSubDirFor(run, seq, rdlen, false);
uci1 56:0bba0ef15697 941 #ifdef DEBUG
uci1 56:0bba0ef15697 942 printf("Checking removal of dir [%s]\r\n",rdnm.c_str());
uci1 56:0bba0ef15697 943 #endif
uci1 103:0ea896a0953a 944 if (rdlen!=0) {
uci1 103:0ea896a0953a 945 DeleteDirIfEmpty(rdnm.c_str());
uci1 103:0ea896a0953a 946 }
uci1 56:0bba0ef15697 947 }
uci1 56:0bba0ef15697 948 }
uci1 56:0bba0ef15697 949
uci1 56:0bba0ef15697 950 return res;
uci1 56:0bba0ef15697 951 }
uci1 56:0bba0ef15697 952
uci1 56:0bba0ef15697 953 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 954 SnSDUtils::SendFilesInRunSeqList(SnCommWin* comm,
uci1 56:0bba0ef15697 955 const uint32_t timeout,
uci1 56:0bba0ef15697 956 char* const buf,
uci1 56:0bba0ef15697 957 const uint32_t bsize,
uci1 56:0bba0ef15697 958 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 959 SnEventFrame& evt,
uci1 56:0bba0ef15697 960 SnPowerFrame& pow) {
uci1 56:0bba0ef15697 961 #ifdef DEBUG
uci1 56:0bba0ef15697 962 printf("SendFilesInRunSeqList\r\n");
uci1 56:0bba0ef15697 963 #endif
uci1 56:0bba0ef15697 964
uci1 56:0bba0ef15697 965 SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent;
uci1 56:0bba0ef15697 966
uci1 64:6c7a316eafad 967 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 968
uci1 76:f8383f0292c2 969 if (curConf.IsSendingFilesRunSeqList() && InitSDCard()) {
uci1 56:0bba0ef15697 970
uci1 56:0bba0ef15697 971 // open up the run/seq list file and send each corresponding file
uci1 56:0bba0ef15697 972 FILE* rslistf = fopen(kRunSeqListFilenm,"r");
uci1 56:0bba0ef15697 973 #ifdef DEBUG
uci1 56:0bba0ef15697 974 printf("fslistf=%p\r\n",(void*)rslistf);
uci1 56:0bba0ef15697 975 #endif
uci1 56:0bba0ef15697 976 if (rslistf!=0) {
uci1 56:0bba0ef15697 977 uint32_t run(0);
uci1 56:0bba0ef15697 978 uint16_t seq(0);
uci1 56:0bba0ef15697 979 while ( (feof(rslistf)==0)
uci1 56:0bba0ef15697 980 && (ferror(rslistf)==0) ) {
uci1 56:0bba0ef15697 981 #ifdef DEBUG
uci1 56:0bba0ef15697 982 printf("feof=%d, ferror=%d\r\n",
uci1 56:0bba0ef15697 983 (int)(feof(rslistf)), (int)(ferror(rslistf)));
uci1 56:0bba0ef15697 984 #endif
uci1 64:6c7a316eafad 985 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 986
uci1 56:0bba0ef15697 987 const int nfilled = fscanf(rslistf, "%u %hu\n", &run ,&seq);
uci1 56:0bba0ef15697 988
uci1 56:0bba0ef15697 989 #ifdef DEBUG
uci1 56:0bba0ef15697 990 printf("nfilled=%d\r\n", nfilled);
uci1 56:0bba0ef15697 991 #endif
uci1 56:0bba0ef15697 992
uci1 56:0bba0ef15697 993 if ( 2==nfilled ) {
uci1 56:0bba0ef15697 994
uci1 56:0bba0ef15697 995 #ifdef DEBUG
uci1 56:0bba0ef15697 996 printf("run=%u, seq=%hu\r\n",run,seq);
uci1 56:0bba0ef15697 997 #endif
uci1 56:0bba0ef15697 998
uci1 56:0bba0ef15697 999 SnCommWin::ECommWinResult res =
uci1 56:0bba0ef15697 1000 SendFileWithRunSeq(comm, timeout, buf, bsize,
uci1 56:0bba0ef15697 1001 curConf, evt, pow,
uci1 56:0bba0ef15697 1002 run, seq);
uci1 56:0bba0ef15697 1003
uci1 56:0bba0ef15697 1004 if ((res<rs) || (res==SnCommWin::kOkStopComm)) {
uci1 56:0bba0ef15697 1005 rs = res;
uci1 56:0bba0ef15697 1006 }
uci1 56:0bba0ef15697 1007 // don't necessarily stop if rs is bad. don't want one bad file to
uci1 56:0bba0ef15697 1008 // prevent sending the others
uci1 56:0bba0ef15697 1009 if (rs<=SnCommWin::kFailTimeout) {
uci1 56:0bba0ef15697 1010 break;
uci1 56:0bba0ef15697 1011 } else if (rs==SnCommWin::kOkStopComm) {
uci1 56:0bba0ef15697 1012 break;
uci1 56:0bba0ef15697 1013 }
uci1 56:0bba0ef15697 1014
uci1 56:0bba0ef15697 1015 }
uci1 56:0bba0ef15697 1016
uci1 56:0bba0ef15697 1017 }
uci1 76:f8383f0292c2 1018
uci1 76:f8383f0292c2 1019 fclose(rslistf);
uci1 56:0bba0ef15697 1020
uci1 56:0bba0ef15697 1021 // do we need to clear the list?
uci1 56:0bba0ef15697 1022 if (curConf.IsRunSeqListOneCommWinOnly()==false) {
uci1 56:0bba0ef15697 1023 if (rs >= SnCommWin::kOkMsgSent) {
uci1 56:0bba0ef15697 1024 // all sent ok
uci1 76:f8383f0292c2 1025 ClearRunSeqList(curConf.IsSendingFilesRunSeqList());
uci1 56:0bba0ef15697 1026 }
uci1 56:0bba0ef15697 1027 }
uci1 56:0bba0ef15697 1028 }
uci1 56:0bba0ef15697 1029 }
uci1 56:0bba0ef15697 1030 return rs;
uci1 56:0bba0ef15697 1031 }
uci1 56:0bba0ef15697 1032
uci1 56:0bba0ef15697 1033 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 1034 SnSDUtils::SendPartOfRun(SnCommWin* comm,
uci1 56:0bba0ef15697 1035 const uint32_t timeout,
uci1 56:0bba0ef15697 1036 char* const buf,
uci1 56:0bba0ef15697 1037 const uint32_t bsize,
uci1 56:0bba0ef15697 1038 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 1039 SnEventFrame& evt,
uci1 56:0bba0ef15697 1040 SnPowerFrame& pow,
uci1 56:0bba0ef15697 1041 const uint32_t run,
uci1 56:0bba0ef15697 1042 const uint16_t minseq,
uci1 56:0bba0ef15697 1043 const uint16_t maxseq) {
uci1 56:0bba0ef15697 1044 // send files with run number 'run'
uci1 56:0bba0ef15697 1045 // and seq number in [minseq,maxseq] (min/max inclusive)
uci1 56:0bba0ef15697 1046
uci1 56:0bba0ef15697 1047 #ifdef DEBUG
uci1 56:0bba0ef15697 1048 printf("SendPartOfRun\r\n");
uci1 56:0bba0ef15697 1049 #endif
uci1 56:0bba0ef15697 1050
uci1 64:6c7a316eafad 1051 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1052
uci1 56:0bba0ef15697 1053 SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent;
uci1 56:0bba0ef15697 1054
uci1 56:0bba0ef15697 1055 for (uint16_t seq=minseq; seq<=maxseq; ++seq) {
uci1 64:6c7a316eafad 1056
uci1 64:6c7a316eafad 1057 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1058
uci1 56:0bba0ef15697 1059 SnCommWin::ECommWinResult res =
uci1 56:0bba0ef15697 1060 SendFileWithRunSeq(comm,
uci1 56:0bba0ef15697 1061 timeout, buf, bsize,
uci1 56:0bba0ef15697 1062 curConf, evt, pow,
uci1 56:0bba0ef15697 1063 run, seq);
uci1 56:0bba0ef15697 1064
uci1 56:0bba0ef15697 1065 if ((res<rs) || (res==SnCommWin::kOkStopComm)) {
uci1 56:0bba0ef15697 1066 rs = res;
uci1 56:0bba0ef15697 1067 }
uci1 56:0bba0ef15697 1068 // don't necessarily stop if rs is bad. don't want one bad file to
uci1 56:0bba0ef15697 1069 // prevent sending the others
uci1 56:0bba0ef15697 1070 if (rs<=SnCommWin::kFailTimeout) {
uci1 56:0bba0ef15697 1071 break;
uci1 56:0bba0ef15697 1072 } else if (rs==SnCommWin::kOkStopComm) {
uci1 56:0bba0ef15697 1073 break;
uci1 56:0bba0ef15697 1074 }
uci1 56:0bba0ef15697 1075
uci1 56:0bba0ef15697 1076 }
uci1 56:0bba0ef15697 1077
uci1 56:0bba0ef15697 1078 return rs;
uci1 56:0bba0ef15697 1079 }
uci1 56:0bba0ef15697 1080
uci1 56:0bba0ef15697 1081
uci1 40:1324da35afd4 1082 SnCommWin::ECommWinResult SnSDUtils::SendAllOfRun(SnCommWin* comm,
uci1 40:1324da35afd4 1083 const uint32_t timeout,
uci1 40:1324da35afd4 1084 char* const buf,
uci1 40:1324da35afd4 1085 const uint32_t bsize,
uci1 40:1324da35afd4 1086 const SnConfigFrame& curConf,
uci1 40:1324da35afd4 1087 SnEventFrame& evt,
uci1 40:1324da35afd4 1088 SnPowerFrame& pow,
uci1 40:1324da35afd4 1089 const uint32_t runnum) {
uci1 40:1324da35afd4 1090 // send all files in a run
uci1 64:6c7a316eafad 1091
uci1 64:6c7a316eafad 1092 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1093
uci1 40:1324da35afd4 1094 // get the run dir
uci1 40:1324da35afd4 1095 uint32_t rdlen(0);
uci1 40:1324da35afd4 1096 std::string rdnms ( GetSubDirFor(runnum, 0, rdlen, false) );
uci1 103:0ea896a0953a 1097 if (rdlen!=0) {
uci1 103:0ea896a0953a 1098 return SendAllFiles(comm, timeout, buf, bsize, curConf, evt, pow,
uci1 103:0ea896a0953a 1099 rdnms.c_str());
uci1 103:0ea896a0953a 1100 } else {
uci1 103:0ea896a0953a 1101 #ifdef DEBUG
uci1 103:0ea896a0953a 1102 printf("canont sent files - coul dot open subdir\r\n");
uci1 103:0ea896a0953a 1103 #endif
uci1 103:0ea896a0953a 1104 return SnCommWin::kUndefFail;
uci1 103:0ea896a0953a 1105 }
uci1 40:1324da35afd4 1106 }
uci1 40:1324da35afd4 1107
uci1 0:664899e0b988 1108 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 1109 const uint32_t timeout,
uci1 3:24c5f0f50bf1 1110 char* const buf,
uci1 6:6f002d202f59 1111 const uint32_t bsize,
uci1 6:6f002d202f59 1112 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 1113 SnEventFrame& evt,
uci1 12:d472f9811262 1114 SnPowerFrame& pow,
uci1 25:57b2627fe756 1115 const char* dirname) {
uci1 40:1324da35afd4 1116 // send all files in the specified directory
uci1 64:6c7a316eafad 1117
uci1 64:6c7a316eafad 1118 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1119
uci1 56:0bba0ef15697 1120 SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail;
uci1 40:1324da35afd4 1121
uci1 56:0bba0ef15697 1122 if (InitSDCard()) {
uci1 16:744ce85aede2 1123
uci1 56:0bba0ef15697 1124 rs = SnCommWin::kOkMsgSent;
uci1 56:0bba0ef15697 1125
uci1 56:0bba0ef15697 1126 DIR* d;
uci1 56:0bba0ef15697 1127 struct dirent* dent;
uci1 56:0bba0ef15697 1128 if ( (d = opendir( dirname ))!=NULL ) {
uci1 56:0bba0ef15697 1129 FATDirHandle* dir = static_cast<FATDirHandle*>(d);
uci1 103:0ea896a0953a 1130 if (dir!=0) {
uci1 103:0ea896a0953a 1131 while ( (dent = readdir(d))!=NULL ) {
uci1 103:0ea896a0953a 1132 Watchdog::kick(); // don't reset
uci1 103:0ea896a0953a 1133 SnCommWin::ECommWinResult res = rs;
uci1 103:0ea896a0953a 1134 if ( (dir->filinfo()->fattrib & AM_DIR)!=0 ) {
uci1 103:0ea896a0953a 1135 // a subdirectory
uci1 103:0ea896a0953a 1136 std::string dnm(dirname);
uci1 103:0ea896a0953a 1137 dnm += "/";
uci1 103:0ea896a0953a 1138 dnm += dent->d_name;
uci1 103:0ea896a0953a 1139 // send all the files in this new subdir
uci1 103:0ea896a0953a 1140 res = SendAllFiles(comm, timeout, buf, bsize,
uci1 103:0ea896a0953a 1141 curConf, evt, pow,
uci1 103:0ea896a0953a 1142 dnm.c_str());
uci1 103:0ea896a0953a 1143 } else if (strncmp(dent->d_name, "SnEvts", 6)==0) {
uci1 103:0ea896a0953a 1144 // a data file (send it)
uci1 103:0ea896a0953a 1145 res = SendOneFile(dent->d_name, true, comm, timeout, buf, bsize,
uci1 103:0ea896a0953a 1146 curConf, evt, pow);
uci1 103:0ea896a0953a 1147 }
uci1 103:0ea896a0953a 1148 if ((res<rs) || (res==SnCommWin::kOkStopComm)) {
uci1 103:0ea896a0953a 1149 rs = res;
uci1 103:0ea896a0953a 1150 }
uci1 103:0ea896a0953a 1151 // don't necessarily stop if rs is bad. don't want one bad file to
uci1 103:0ea896a0953a 1152 // prevent sending the others
uci1 103:0ea896a0953a 1153 if (rs<=SnCommWin::kFailTimeout) {
uci1 103:0ea896a0953a 1154 break;
uci1 103:0ea896a0953a 1155 } else if (rs==SnCommWin::kOkStopComm) {
uci1 103:0ea896a0953a 1156 break;
uci1 103:0ea896a0953a 1157 }
uci1 103:0ea896a0953a 1158 } // loop over stuff in this directory
uci1 103:0ea896a0953a 1159 }
uci1 103:0ea896a0953a 1160 if (d!=0) {
uci1 103:0ea896a0953a 1161 closedir(d);
uci1 103:0ea896a0953a 1162 }
uci1 56:0bba0ef15697 1163 // see if we need to remove this directory now
uci1 56:0bba0ef15697 1164 if (curConf.IsDeletingFiles()) {
uci1 56:0bba0ef15697 1165 DeleteDirIfEmpty(dirname);
uci1 25:57b2627fe756 1166 }
uci1 0:664899e0b988 1167 }
uci1 0:664899e0b988 1168 }
uci1 0:664899e0b988 1169 return rs;
uci1 0:664899e0b988 1170 }
uci1 0:664899e0b988 1171