Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Oct 05 04:45:22 2013 +0000
Revision:
40:1324da35afd4
Parent:
38:9070c17536cd
Child:
47:fbe956b10a91
first commit of major overhaul to 2013-2014 mbed code. NOT YET FULLY TESTED. too many changes to list (fix local file receive, fix rates, external comm packes, big SD cards, get to comm win w/o SD, v8 config frame, v4 files, SBD buffering changes...)

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