Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Oct 30 04:49:40 2015 +0000
Revision:
84:80b15993944e
Parent:
76:f8383f0292c2
Child:
103:0ea896a0953a
conf v11,12. ip to ints. write 64chr lbl. send status data packs. comm pwr as needed. comm each evt. conn/list TO separate afar, sbd. calc FFT, L1 trigger, L1 scaledown. pre-compl opts in own file. max comm fail 75. htbt and clock frames store vars.

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