Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Jun 05 17:29:31 2019 +0000
Revision:
125:ce4045184366
Parent:
122:c1b5023eac69
Added SnRateListner proto-class, publishing this version of the code in order to enable exporting of most recent features.

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 122:c1b5023eac69 205 bool CopyFileToSD(const char* const infn,
uci1 122:c1b5023eac69 206 const char* const outfn,
uci1 122:c1b5023eac69 207 char* const buf,
uci1 122:c1b5023eac69 208 const uint32_t bsize);
uci1 122:c1b5023eac69 209
uci1 122:c1b5023eac69 210 static
uci1 25:57b2627fe756 211 void PrintFilesInDirs(const char* dirname);
uci1 21:ce51bb0ba4a5 212
uci1 21:ce51bb0ba4a5 213 static
uci1 21:ce51bb0ba4a5 214 void GetDirProps(const char* dirname,
uci1 21:ce51bb0ba4a5 215 uint32_t& nfiles,
uci1 21:ce51bb0ba4a5 216 float& totbytes);
uci1 10:3c93db1cfb12 217
uci1 10:3c93db1cfb12 218 static
uci1 40:1324da35afd4 219 float GetFreeBytes();
uci1 40:1324da35afd4 220
uci1 40:1324da35afd4 221 static
uci1 84:80b15993944e 222 bool WriteHeartbeatTo(FILE* file, const SnHeartbeatFrame& htbt);
uci1 22:f957c4f840ad 223
uci1 22:f957c4f840ad 224 static
uci1 40:1324da35afd4 225 bool WriteTrigWaitWinTime(FILE* file,
uci1 40:1324da35afd4 226 SnClockSetFrame& clkset,
uci1 40:1324da35afd4 227 const bool isStart);
uci1 40:1324da35afd4 228
uci1 40:1324da35afd4 229 static
uci1 0:664899e0b988 230 bool WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 231 const SnEventFrame& evt,
uci1 0:664899e0b988 232 const SnConfigFrame& conf);
uci1 0:664899e0b988 233
uci1 0:664899e0b988 234 static
uci1 0:664899e0b988 235 bool WriteConfig(FILE* efile,
uci1 0:664899e0b988 236 const SnConfigFrame& conf);
uci1 2:e67f7c158087 237
uci1 8:95a325df1f6b 238 template<class T>
uci1 2:e67f7c158087 239 static
uci1 8:95a325df1f6b 240 SnCommWin::ECommWinResult WritePowerTo(T& f,
uci1 8:95a325df1f6b 241 const SnPowerFrame& pow,
uci1 15:f2569d8e4176 242 uint32_t& pnum) {
uci1 38:9070c17536cd 243 const bool rs =
uci1 8:95a325df1f6b 244 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 245 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 246 const SnCommWin::ECommWinResult re = pow.WriteTo(f);
uci1 8:95a325df1f6b 247 ++pnum;
uci1 38:9070c17536cd 248 return (rs) ? re : SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 249 }
uci1 8:95a325df1f6b 250
uci1 8:95a325df1f6b 251 template<class T>
uci1 8:95a325df1f6b 252 static
uci1 56:0bba0ef15697 253 SnCommWin::ECommWinResult WriteTempTo(T& f, const SnTempFrame& tmp) {
uci1 56:0bba0ef15697 254 const bool rs =
uci1 56:0bba0ef15697 255 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kTemperatureCode,
uci1 56:0bba0ef15697 256 SnTempFrame::SizeOf() );
uci1 56:0bba0ef15697 257 const SnCommWin::ECommWinResult re = tmp.WriteTo(f);
uci1 56:0bba0ef15697 258 return (rs) ? re : SnCommWin::kUndefFail;
uci1 56:0bba0ef15697 259 }
uci1 56:0bba0ef15697 260
uci1 56:0bba0ef15697 261 template<class T>
uci1 56:0bba0ef15697 262 static
uci1 8:95a325df1f6b 263 SnCommWin::ECommWinResult ReadBlockHeader(T& f,
uci1 8:95a325df1f6b 264 uint8_t& mcode,
uci1 8:95a325df1f6b 265 uint32_t& mlen) {
uci1 38:9070c17536cd 266 return SnHeaderFrame::ReadFrom(f, mcode, mlen) ?
uci1 38:9070c17536cd 267 SnCommWin::kOkWithMsg : SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 268 }
uci1 8:95a325df1f6b 269
uci1 5:9cea89700c66 270 template<class T>
uci1 5:9cea89700c66 271 static
uci1 5:9cea89700c66 272 SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr,
uci1 8:95a325df1f6b 273 const uint32_t run, const uint16_t seq) {
uci1 5:9cea89700c66 274 // MUST INCREMENT kIOvers if these writes are altered
uci1 5:9cea89700c66 275 f = SnBitUtils::WriteTo(f, kIOvers);
uci1 5:9cea89700c66 276 f = SnBitUtils::WriteTo(f, macadr);
uci1 5:9cea89700c66 277 f = SnBitUtils::WriteTo(f, run);
uci1 5:9cea89700c66 278 f = SnBitUtils::WriteTo(f, seq);
uci1 5:9cea89700c66 279 return SnCommWin::kOkMsgSent;
uci1 5:9cea89700c66 280 }
uci1 5:9cea89700c66 281
uci1 5:9cea89700c66 282 template<class T>
uci1 5:9cea89700c66 283 static
uci1 5:9cea89700c66 284 SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr,
uci1 5:9cea89700c66 285 uint32_t& run, uint16_t& seq,
uci1 8:95a325df1f6b 286 SnPowerFrame* pow=0) {
uci1 8:95a325df1f6b 287 SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg;
uci1 5:9cea89700c66 288 uint8_t Rv=0;
uci1 5:9cea89700c66 289 f = SnBitUtils::ReadFrom(f, Rv);
uci1 5:9cea89700c66 290 f = SnBitUtils::ReadFrom(f, macadr);
uci1 5:9cea89700c66 291 f = SnBitUtils::ReadFrom(f, run);
uci1 5:9cea89700c66 292 f = SnBitUtils::ReadFrom(f, seq);
uci1 8:95a325df1f6b 293 if (Rv==2) {
uci1 8:95a325df1f6b 294 uint16_t v1, v2;
uci1 8:95a325df1f6b 295 f = SnBitUtils::ReadFrom(f, v1);
uci1 8:95a325df1f6b 296 f = SnBitUtils::ReadFrom(f, v2);
uci1 8:95a325df1f6b 297 if (pow!=0) {
uci1 8:95a325df1f6b 298 pow->Set(v1, v2, 0, 0, 0);
uci1 8:95a325df1f6b 299 }
uci1 5:9cea89700c66 300 }
uci1 8:95a325df1f6b 301 return res;
uci1 5:9cea89700c66 302 }
uci1 5:9cea89700c66 303
uci1 5:9cea89700c66 304 static
uci1 8:95a325df1f6b 305 uint32_t SizeOfFileHeader(const uint8_t rv) {
uci1 8:95a325df1f6b 306 if (rv==2) {
uci1 8:95a325df1f6b 307 return kMaxSizeOfFileHdr;
uci1 8:95a325df1f6b 308 } else {
uci1 8:95a325df1f6b 309 return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t);
uci1 8:95a325df1f6b 310 }
uci1 8:95a325df1f6b 311 }
uci1 0:664899e0b988 312
uci1 0:664899e0b988 313 friend class SnSDUtilsWhisperer; // to restrict access to specific functions
uci1 0:664899e0b988 314 };
uci1 0:664899e0b988 315
uci1 0:664899e0b988 316 class SnSDUtilsWhisperer {
uci1 0:664899e0b988 317 static
uci1 0:664899e0b988 318 void DeleteFile(FILE*& f, const char* fname) {
uci1 27:efc4d654b139 319 SnSDUtils::DeleteFile(f, fname);
uci1 27:efc4d654b139 320 }
uci1 27:efc4d654b139 321
uci1 27:efc4d654b139 322 static
uci1 27:efc4d654b139 323 void DeleteAllFiles(const char* dirname=SnSDUtils::kSDsubDir) {
uci1 27:efc4d654b139 324 SnSDUtils::DeleteAllFiles(dirname);
uci1 27:efc4d654b139 325 }
uci1 27:efc4d654b139 326
uci1 27:efc4d654b139 327 static
uci1 27:efc4d654b139 328 void DeleteFilesOfRun(const uint32_t run) {
uci1 27:efc4d654b139 329 SnSDUtils::DeleteFilesOfRun(run);
uci1 0:664899e0b988 330 }
uci1 25:57b2627fe756 331
uci1 0:664899e0b988 332 friend class SnCommWin; // the one who's allowed to use me
uci1 0:664899e0b988 333 };
uci1 0:664899e0b988 334
uci1 0:664899e0b988 335 #endif // SN_SnSDUtils