Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Dec 02 21:55:27 2014 +0000
Revision:
66:685f9d0a48ae
Parent:
56:0bba0ef15697
Child:
76:f8383f0292c2
Bug fix: add "determineDir" parameter to SendOneFile.  this allows it to be called when only the filename (not dir) is known, as is the case for multi-file loops

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