Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Nov 24 21:52:27 2015 +0000
Revision:
110:d1da040a0cf2
Parent:
103:0ea896a0953a
Child:
122:c1b5023eac69
Stn32 (slow) with conf name. SD stall fix. No interface chip. Safety nets.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #ifndef SN_SnSDUtils
uci1 0:664899e0b988 2 #define SN_SnSDUtils
uci1 0:664899e0b988 3
uci1 0:664899e0b988 4 #include <stdio.h>
uci1 0:664899e0b988 5 #include <stdint.h>
uci1 0:664899e0b988 6
uci1 0:664899e0b988 7 #include "SnCommWin.h"
uci1 5:9cea89700c66 8 #include "SnBitUtils.h"
uci1 5:9cea89700c66 9 #include "SnPowerFrame.h"
uci1 56:0bba0ef15697 10 #include "SnTempFrame.h"
uci1 8:95a325df1f6b 11 #include "SnHeaderFrame.h"
uci1 0:664899e0b988 12
uci1 0:664899e0b988 13 class SnEventFrame;
uci1 0:664899e0b988 14 class SnConfigFrame;
uci1 40:1324da35afd4 15 class SnClockSetFrame;
uci1 84:80b15993944e 16 class SnHeartbeatFrame;
uci1 0:664899e0b988 17
uci1 0:664899e0b988 18 // a namespace-like class to handle the i/o with the SD card
uci1 0:664899e0b988 19
uci1 0:664899e0b988 20 class SnSDUtils {
uci1 0:664899e0b988 21 public:
uci1 19:74155d652c37 22 static const char* const kSDdir;
uci1 1:e392595b4b76 23 static const char* const kSDsubDir;
uci1 56:0bba0ef15697 24 static const char* const kRunSeqListFilenm;
uci1 56:0bba0ef15697 25 static const uint16_t kMaxSeqNum;
uci1 56:0bba0ef15697 26 static const uint16_t kBadSeqNum;
uci1 1:e392595b4b76 27 static const uint8_t kFNBufSize=128;
uci1 1:e392595b4b76 28 static const uint8_t kIOvers; // file I/O version
uci1 5:9cea89700c66 29 static const uint32_t kMaxSizeOfFileHdr;
uci1 0:664899e0b988 30
uci1 56:0bba0ef15697 31 typedef int (*InitSDFcn)(void);
uci1 40:1324da35afd4 32 static InitSDFcn fgDoInit;
uci1 56:0bba0ef15697 33 static bool fgInitOk;
uci1 40:1324da35afd4 34
uci1 0:664899e0b988 35 private:
uci1 40:1324da35afd4 36
uci1 40:1324da35afd4 37 static
uci1 56:0bba0ef15697 38 bool InitSDCard(const bool force=false);
uci1 0:664899e0b988 39
uci1 0:664899e0b988 40 static
uci1 103:0ea896a0953a 41 void SetSDCardInitTo(const bool ok) { fgInitOk = ok; }
uci1 103:0ea896a0953a 42
uci1 103:0ea896a0953a 43 static
uci1 25:57b2627fe756 44 FILE* OpenSDFile(const char* name, const char* mode, const bool redoDir);
uci1 1:e392595b4b76 45
uci1 1:e392595b4b76 46 static
uci1 0:664899e0b988 47 uint16_t GetSeqNum(const uint64_t macadr,
uci1 0:664899e0b988 48 const uint32_t run);
uci1 0:664899e0b988 49
uci1 0:664899e0b988 50 static
uci1 0:664899e0b988 51 const char* GetOutFileName(const uint64_t macadr,
uci1 0:664899e0b988 52 const uint32_t run,
uci1 0:664899e0b988 53 const uint16_t seq);
uci1 27:efc4d654b139 54
uci1 27:efc4d654b139 55 static
uci1 27:efc4d654b139 56 void DeleteAllFiles(const char* dirname=kSDsubDir);
uci1 27:efc4d654b139 57
uci1 27:efc4d654b139 58 static
uci1 27:efc4d654b139 59 void DeleteFilesOfRun(const uint32_t run);
uci1 27:efc4d654b139 60
uci1 0:664899e0b988 61 static
uci1 0:664899e0b988 62 void DeleteFile(FILE*& f, const char* fname);
uci1 27:efc4d654b139 63
uci1 27:efc4d654b139 64 static
uci1 56:0bba0ef15697 65 bool DeleteDirIfEmpty(const char* dirname);
uci1 27:efc4d654b139 66
uci1 25:57b2627fe756 67 private:
uci1 12:d472f9811262 68 static char fgCurFileName[kFNBufSize];
uci1 12:d472f9811262 69 static FILE* fgCurFile;
uci1 12:d472f9811262 70 static uint16_t fgCurSeq;
uci1 40:1324da35afd4 71 static bool fgNeedToInit;
uci1 40:1324da35afd4 72
uci1 0:664899e0b988 73
uci1 0:664899e0b988 74 public:
uci1 0:664899e0b988 75 SnSDUtils() {}
uci1 0:664899e0b988 76 virtual ~SnSDUtils() {}
uci1 56:0bba0ef15697 77
uci1 56:0bba0ef15697 78 static
uci1 56:0bba0ef15697 79 bool IsInitOk() {
uci1 56:0bba0ef15697 80 return fgInitOk;
uci1 56:0bba0ef15697 81 }
uci1 56:0bba0ef15697 82
uci1 0:664899e0b988 83 static
uci1 76:f8383f0292c2 84 void SetDoNeedToInit() { fgNeedToInit = true; }
uci1 76:f8383f0292c2 85
uci1 76:f8383f0292c2 86 static
uci1 10:3c93db1cfb12 87 int CloseOutputFile(FILE* f) {
uci1 12:d472f9811262 88 // TODO: set current file pointer to 0, IF f is current file
uci1 10:3c93db1cfb12 89 const int rt = (f!=0) ? fclose(f) : 0;
uci1 10:3c93db1cfb12 90 return rt;
uci1 10:3c93db1cfb12 91 }
uci1 0:664899e0b988 92
uci1 0:664899e0b988 93 static
uci1 0:664899e0b988 94 FILE* OpenNewOutputFile(const uint64_t macadr,
uci1 40:1324da35afd4 95 const uint32_t run,
uci1 76:f8383f0292c2 96 const uint16_t minseq,
uci1 76:f8383f0292c2 97 const bool useRSlist);
uci1 0:664899e0b988 98
uci1 0:664899e0b988 99 static
uci1 25:57b2627fe756 100 FILE* OpenExistingFile(const char* name, const bool setcurrent,
uci1 25:57b2627fe756 101 const bool redoDir);
uci1 0:664899e0b988 102
uci1 0:664899e0b988 103 static
uci1 76:f8383f0292c2 104 bool ClearRunSeqList(const bool useRSlist);
uci1 56:0bba0ef15697 105
uci1 56:0bba0ef15697 106 static
uci1 56:0bba0ef15697 107 bool AddToRunSeqList(const uint32_t run,
uci1 76:f8383f0292c2 108 const uint16_t seq,
uci1 76:f8383f0292c2 109 const bool useRSlist);
uci1 56:0bba0ef15697 110
uci1 56:0bba0ef15697 111 static
uci1 56:0bba0ef15697 112 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 113 SendFilesInRunSeqList(SnCommWin* comm,
uci1 56:0bba0ef15697 114 const uint32_t timeout,
uci1 56:0bba0ef15697 115 char* const buf,
uci1 56:0bba0ef15697 116 const uint32_t bsize,
uci1 56:0bba0ef15697 117 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 118 SnEventFrame& evt,
uci1 56:0bba0ef15697 119 SnPowerFrame& pow);
uci1 56:0bba0ef15697 120
uci1 56:0bba0ef15697 121 static
uci1 56:0bba0ef15697 122 SnCommWin::ECommWinResult
uci1 56:0bba0ef15697 123 SendFileWithRunSeq(SnCommWin* comm,
uci1 56:0bba0ef15697 124 const uint32_t timeout,
uci1 56:0bba0ef15697 125 char* const buf,
uci1 56:0bba0ef15697 126 const uint32_t bsize,
uci1 56:0bba0ef15697 127 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 128 SnEventFrame& evt,
uci1 56:0bba0ef15697 129 SnPowerFrame& pow,
uci1 56:0bba0ef15697 130 const uint32_t run,
uci1 56:0bba0ef15697 131 const uint16_t seq);
uci1 56:0bba0ef15697 132
uci1 56:0bba0ef15697 133
uci1 56:0bba0ef15697 134 static
uci1 56:0bba0ef15697 135 SnCommWin::ECommWinResult SendOneFile(const char* dfn,
uci1 66:685f9d0a48ae 136 const bool determineDir,
uci1 56:0bba0ef15697 137 SnCommWin* comm,
uci1 56:0bba0ef15697 138 const uint32_t timeout,
uci1 56:0bba0ef15697 139 char* const buf,
uci1 56:0bba0ef15697 140 const uint32_t bsize,
uci1 56:0bba0ef15697 141 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 142 SnEventFrame& evt,
uci1 56:0bba0ef15697 143 SnPowerFrame& pow);
uci1 56:0bba0ef15697 144
uci1 56:0bba0ef15697 145 static
uci1 56:0bba0ef15697 146 SnCommWin::ECommWinResult SendPartOfRun(SnCommWin* comm,
uci1 56:0bba0ef15697 147 const uint32_t timeout,
uci1 56:0bba0ef15697 148 char* const buf,
uci1 56:0bba0ef15697 149 const uint32_t bsize,
uci1 56:0bba0ef15697 150 const SnConfigFrame& curConf,
uci1 56:0bba0ef15697 151 SnEventFrame& evt,
uci1 56:0bba0ef15697 152 SnPowerFrame& pow,
uci1 56:0bba0ef15697 153 const uint32_t run,
uci1 56:0bba0ef15697 154 const uint16_t minseq,
uci1 56:0bba0ef15697 155 const uint16_t maxseq);
uci1 56:0bba0ef15697 156
uci1 56:0bba0ef15697 157 static
uci1 40:1324da35afd4 158 SnCommWin::ECommWinResult SendAllOfRun(SnCommWin* comm,
uci1 40:1324da35afd4 159 const uint32_t timeout,
uci1 40:1324da35afd4 160 char* const buf,
uci1 40:1324da35afd4 161 const uint32_t bsize,
uci1 40:1324da35afd4 162 const SnConfigFrame& curConf,
uci1 40:1324da35afd4 163 SnEventFrame& evt,
uci1 40:1324da35afd4 164 SnPowerFrame& pow,
uci1 40:1324da35afd4 165 const uint32_t runnum);
uci1 40:1324da35afd4 166
uci1 40:1324da35afd4 167 static
uci1 0:664899e0b988 168 SnCommWin::ECommWinResult SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 169 const uint32_t timeout,
uci1 3:24c5f0f50bf1 170 char* const buf,
uci1 6:6f002d202f59 171 const uint32_t bsize,
uci1 6:6f002d202f59 172 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 173 SnEventFrame& evt,
uci1 12:d472f9811262 174 SnPowerFrame& pow,
uci1 25:57b2627fe756 175 const char* dirname=kSDsubDir);
uci1 25:57b2627fe756 176
uci1 25:57b2627fe756 177 static
uci1 47:fbe956b10a91 178 DIR* OpenOrMakeAllDirs(const char* dirname);
uci1 47:fbe956b10a91 179 static
uci1 25:57b2627fe756 180 DIR* OpenOrMakeDir(const char* dirname);
uci1 0:664899e0b988 181
uci1 0:664899e0b988 182 static
uci1 0:664899e0b988 183 const char* GetCurFileName() { return fgCurFileName; }
uci1 0:664899e0b988 184
uci1 0:664899e0b988 185 static
uci1 1:e392595b4b76 186 FILE* GetCurFile() { return fgCurFile; }
uci1 1:e392595b4b76 187
uci1 1:e392595b4b76 188 static
uci1 12:d472f9811262 189 uint16_t GetCurSeqNum() { return fgCurSeq; }
uci1 40:1324da35afd4 190
uci1 25:57b2627fe756 191 static
uci1 25:57b2627fe756 192 bool GetRunSeqFromFilename(const char* fn,
uci1 25:57b2627fe756 193 uint32_t& run,
uci1 25:57b2627fe756 194 uint16_t& seq);
uci1 10:3c93db1cfb12 195
uci1 10:3c93db1cfb12 196 static
uci1 25:57b2627fe756 197 const char* GetSubDirFor(const uint32_t run, const uint16_t seq,
uci1 25:57b2627fe756 198 uint32_t& slen, const bool useSeq);
uci1 25:57b2627fe756 199
uci1 25:57b2627fe756 200 static
uci1 25:57b2627fe756 201 bool GetFullFilename(const char* name, std::string& ffn);
uci1 25:57b2627fe756 202
uci1 25:57b2627fe756 203
uci1 25:57b2627fe756 204 static
uci1 25:57b2627fe756 205 void PrintFilesInDirs(const char* dirname);
uci1 21:ce51bb0ba4a5 206
uci1 21:ce51bb0ba4a5 207 static
uci1 21:ce51bb0ba4a5 208 void GetDirProps(const char* dirname,
uci1 21:ce51bb0ba4a5 209 uint32_t& nfiles,
uci1 21:ce51bb0ba4a5 210 float& totbytes);
uci1 10:3c93db1cfb12 211
uci1 10:3c93db1cfb12 212 static
uci1 40:1324da35afd4 213 float GetFreeBytes();
uci1 40:1324da35afd4 214
uci1 40:1324da35afd4 215 static
uci1 84:80b15993944e 216 bool WriteHeartbeatTo(FILE* file, const SnHeartbeatFrame& htbt);
uci1 22:f957c4f840ad 217
uci1 22:f957c4f840ad 218 static
uci1 40:1324da35afd4 219 bool WriteTrigWaitWinTime(FILE* file,
uci1 40:1324da35afd4 220 SnClockSetFrame& clkset,
uci1 40:1324da35afd4 221 const bool isStart);
uci1 40:1324da35afd4 222
uci1 40:1324da35afd4 223 static
uci1 0:664899e0b988 224 bool WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 225 const SnEventFrame& evt,
uci1 0:664899e0b988 226 const SnConfigFrame& conf);
uci1 0:664899e0b988 227
uci1 0:664899e0b988 228 static
uci1 0:664899e0b988 229 bool WriteConfig(FILE* efile,
uci1 0:664899e0b988 230 const SnConfigFrame& conf);
uci1 2:e67f7c158087 231
uci1 8:95a325df1f6b 232 template<class T>
uci1 2:e67f7c158087 233 static
uci1 8:95a325df1f6b 234 SnCommWin::ECommWinResult WritePowerTo(T& f,
uci1 8:95a325df1f6b 235 const SnPowerFrame& pow,
uci1 15:f2569d8e4176 236 uint32_t& pnum) {
uci1 38:9070c17536cd 237 const bool rs =
uci1 8:95a325df1f6b 238 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 239 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 240 const SnCommWin::ECommWinResult re = pow.WriteTo(f);
uci1 8:95a325df1f6b 241 ++pnum;
uci1 38:9070c17536cd 242 return (rs) ? re : SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 243 }
uci1 8:95a325df1f6b 244
uci1 8:95a325df1f6b 245 template<class T>
uci1 8:95a325df1f6b 246 static
uci1 56:0bba0ef15697 247 SnCommWin::ECommWinResult WriteTempTo(T& f, const SnTempFrame& tmp) {
uci1 56:0bba0ef15697 248 const bool rs =
uci1 56:0bba0ef15697 249 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kTemperatureCode,
uci1 56:0bba0ef15697 250 SnTempFrame::SizeOf() );
uci1 56:0bba0ef15697 251 const SnCommWin::ECommWinResult re = tmp.WriteTo(f);
uci1 56:0bba0ef15697 252 return (rs) ? re : SnCommWin::kUndefFail;
uci1 56:0bba0ef15697 253 }
uci1 56:0bba0ef15697 254
uci1 56:0bba0ef15697 255 template<class T>
uci1 56:0bba0ef15697 256 static
uci1 8:95a325df1f6b 257 SnCommWin::ECommWinResult ReadBlockHeader(T& f,
uci1 8:95a325df1f6b 258 uint8_t& mcode,
uci1 8:95a325df1f6b 259 uint32_t& mlen) {
uci1 38:9070c17536cd 260 return SnHeaderFrame::ReadFrom(f, mcode, mlen) ?
uci1 38:9070c17536cd 261 SnCommWin::kOkWithMsg : SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 262 }
uci1 8:95a325df1f6b 263
uci1 5:9cea89700c66 264 template<class T>
uci1 5:9cea89700c66 265 static
uci1 5:9cea89700c66 266 SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr,
uci1 8:95a325df1f6b 267 const uint32_t run, const uint16_t seq) {
uci1 5:9cea89700c66 268 // MUST INCREMENT kIOvers if these writes are altered
uci1 5:9cea89700c66 269 f = SnBitUtils::WriteTo(f, kIOvers);
uci1 5:9cea89700c66 270 f = SnBitUtils::WriteTo(f, macadr);
uci1 5:9cea89700c66 271 f = SnBitUtils::WriteTo(f, run);
uci1 5:9cea89700c66 272 f = SnBitUtils::WriteTo(f, seq);
uci1 5:9cea89700c66 273 return SnCommWin::kOkMsgSent;
uci1 5:9cea89700c66 274 }
uci1 5:9cea89700c66 275
uci1 5:9cea89700c66 276 template<class T>
uci1 5:9cea89700c66 277 static
uci1 5:9cea89700c66 278 SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr,
uci1 5:9cea89700c66 279 uint32_t& run, uint16_t& seq,
uci1 8:95a325df1f6b 280 SnPowerFrame* pow=0) {
uci1 8:95a325df1f6b 281 SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg;
uci1 5:9cea89700c66 282 uint8_t Rv=0;
uci1 5:9cea89700c66 283 f = SnBitUtils::ReadFrom(f, Rv);
uci1 5:9cea89700c66 284 f = SnBitUtils::ReadFrom(f, macadr);
uci1 5:9cea89700c66 285 f = SnBitUtils::ReadFrom(f, run);
uci1 5:9cea89700c66 286 f = SnBitUtils::ReadFrom(f, seq);
uci1 8:95a325df1f6b 287 if (Rv==2) {
uci1 8:95a325df1f6b 288 uint16_t v1, v2;
uci1 8:95a325df1f6b 289 f = SnBitUtils::ReadFrom(f, v1);
uci1 8:95a325df1f6b 290 f = SnBitUtils::ReadFrom(f, v2);
uci1 8:95a325df1f6b 291 if (pow!=0) {
uci1 8:95a325df1f6b 292 pow->Set(v1, v2, 0, 0, 0);
uci1 8:95a325df1f6b 293 }
uci1 5:9cea89700c66 294 }
uci1 8:95a325df1f6b 295 return res;
uci1 5:9cea89700c66 296 }
uci1 5:9cea89700c66 297
uci1 5:9cea89700c66 298 static
uci1 8:95a325df1f6b 299 uint32_t SizeOfFileHeader(const uint8_t rv) {
uci1 8:95a325df1f6b 300 if (rv==2) {
uci1 8:95a325df1f6b 301 return kMaxSizeOfFileHdr;
uci1 8:95a325df1f6b 302 } else {
uci1 8:95a325df1f6b 303 return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t);
uci1 8:95a325df1f6b 304 }
uci1 8:95a325df1f6b 305 }
uci1 0:664899e0b988 306
uci1 0:664899e0b988 307 friend class SnSDUtilsWhisperer; // to restrict access to specific functions
uci1 0:664899e0b988 308 };
uci1 0:664899e0b988 309
uci1 0:664899e0b988 310 class SnSDUtilsWhisperer {
uci1 0:664899e0b988 311 static
uci1 0:664899e0b988 312 void DeleteFile(FILE*& f, const char* fname) {
uci1 27:efc4d654b139 313 SnSDUtils::DeleteFile(f, fname);
uci1 27:efc4d654b139 314 }
uci1 27:efc4d654b139 315
uci1 27:efc4d654b139 316 static
uci1 27:efc4d654b139 317 void DeleteAllFiles(const char* dirname=SnSDUtils::kSDsubDir) {
uci1 27:efc4d654b139 318 SnSDUtils::DeleteAllFiles(dirname);
uci1 27:efc4d654b139 319 }
uci1 27:efc4d654b139 320
uci1 27:efc4d654b139 321 static
uci1 27:efc4d654b139 322 void DeleteFilesOfRun(const uint32_t run) {
uci1 27:efc4d654b139 323 SnSDUtils::DeleteFilesOfRun(run);
uci1 0:664899e0b988 324 }
uci1 25:57b2627fe756 325
uci1 0:664899e0b988 326 friend class SnCommWin; // the one who's allowed to use me
uci1 0:664899e0b988 327 };
uci1 0:664899e0b988 328
uci1 0:664899e0b988 329 #endif // SN_SnSDUtils