Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.h@47:fbe956b10a91, 2013-11-06 (annotated)
- Committer:
- uci1
- Date:
- Wed Nov 06 00:04:30 2013 +0000
- Revision:
- 47:fbe956b10a91
- Parent:
- 40:1324da35afd4
- Child:
- 56:0bba0ef15697
afar and sbd on. debug ON. recursively make directories on the SD card so blank cards can be used. move debug block in procForce to before pin high so forced trigs work in debug mode
Who changed what in which revision?
User | Revision | Line number | New 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 | 47:fbe956b10a91 | 106 | DIR* OpenOrMakeAllDirs(const char* dirname); |
uci1 | 47:fbe956b10a91 | 107 | static |
uci1 | 25:57b2627fe756 | 108 | DIR* OpenOrMakeDir(const char* dirname); |
uci1 | 0:664899e0b988 | 109 | |
uci1 | 0:664899e0b988 | 110 | static |
uci1 | 0:664899e0b988 | 111 | const char* GetCurFileName() { return fgCurFileName; } |
uci1 | 0:664899e0b988 | 112 | |
uci1 | 0:664899e0b988 | 113 | static |
uci1 | 1:e392595b4b76 | 114 | FILE* GetCurFile() { return fgCurFile; } |
uci1 | 1:e392595b4b76 | 115 | |
uci1 | 1:e392595b4b76 | 116 | static |
uci1 | 12:d472f9811262 | 117 | uint16_t GetCurSeqNum() { return fgCurSeq; } |
uci1 | 40:1324da35afd4 | 118 | |
uci1 | 25:57b2627fe756 | 119 | static |
uci1 | 25:57b2627fe756 | 120 | bool GetRunSeqFromFilename(const char* fn, |
uci1 | 25:57b2627fe756 | 121 | uint32_t& run, |
uci1 | 25:57b2627fe756 | 122 | uint16_t& seq); |
uci1 | 10:3c93db1cfb12 | 123 | |
uci1 | 10:3c93db1cfb12 | 124 | static |
uci1 | 25:57b2627fe756 | 125 | const char* GetSubDirFor(const uint32_t run, const uint16_t seq, |
uci1 | 25:57b2627fe756 | 126 | uint32_t& slen, const bool useSeq); |
uci1 | 25:57b2627fe756 | 127 | |
uci1 | 25:57b2627fe756 | 128 | static |
uci1 | 25:57b2627fe756 | 129 | bool GetFullFilename(const char* name, std::string& ffn); |
uci1 | 25:57b2627fe756 | 130 | |
uci1 | 25:57b2627fe756 | 131 | |
uci1 | 25:57b2627fe756 | 132 | static |
uci1 | 25:57b2627fe756 | 133 | void PrintFilesInDirs(const char* dirname); |
uci1 | 21:ce51bb0ba4a5 | 134 | |
uci1 | 21:ce51bb0ba4a5 | 135 | static |
uci1 | 21:ce51bb0ba4a5 | 136 | void GetDirProps(const char* dirname, |
uci1 | 21:ce51bb0ba4a5 | 137 | uint32_t& nfiles, |
uci1 | 21:ce51bb0ba4a5 | 138 | float& totbytes); |
uci1 | 10:3c93db1cfb12 | 139 | |
uci1 | 10:3c93db1cfb12 | 140 | static |
uci1 | 40:1324da35afd4 | 141 | float GetFreeBytes(); |
uci1 | 40:1324da35afd4 | 142 | |
uci1 | 40:1324da35afd4 | 143 | static |
uci1 | 22:f957c4f840ad | 144 | bool WriteHeartbeatTo(FILE* file, |
uci1 | 22:f957c4f840ad | 145 | const uint32_t time, |
uci1 | 22:f957c4f840ad | 146 | const uint32_t num); |
uci1 | 22:f957c4f840ad | 147 | |
uci1 | 22:f957c4f840ad | 148 | static |
uci1 | 40:1324da35afd4 | 149 | bool WriteTrigWaitWinTime(FILE* file, |
uci1 | 40:1324da35afd4 | 150 | SnClockSetFrame& clkset, |
uci1 | 40:1324da35afd4 | 151 | const bool isStart); |
uci1 | 40:1324da35afd4 | 152 | |
uci1 | 40:1324da35afd4 | 153 | static |
uci1 | 0:664899e0b988 | 154 | bool WriteEventTo(FILE* efile, char* const evtBuf, |
uci1 | 0:664899e0b988 | 155 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 156 | const SnConfigFrame& conf); |
uci1 | 0:664899e0b988 | 157 | |
uci1 | 0:664899e0b988 | 158 | static |
uci1 | 0:664899e0b988 | 159 | bool WriteConfig(FILE* efile, |
uci1 | 0:664899e0b988 | 160 | const SnConfigFrame& conf); |
uci1 | 2:e67f7c158087 | 161 | |
uci1 | 8:95a325df1f6b | 162 | template<class T> |
uci1 | 2:e67f7c158087 | 163 | static |
uci1 | 8:95a325df1f6b | 164 | SnCommWin::ECommWinResult WritePowerTo(T& f, |
uci1 | 8:95a325df1f6b | 165 | const SnPowerFrame& pow, |
uci1 | 15:f2569d8e4176 | 166 | uint32_t& pnum) { |
uci1 | 38:9070c17536cd | 167 | const bool rs = |
uci1 | 8:95a325df1f6b | 168 | SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode, |
uci1 | 8:95a325df1f6b | 169 | SnPowerFrame::SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 8:95a325df1f6b | 170 | const SnCommWin::ECommWinResult re = pow.WriteTo(f); |
uci1 | 8:95a325df1f6b | 171 | ++pnum; |
uci1 | 38:9070c17536cd | 172 | return (rs) ? re : SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 173 | } |
uci1 | 8:95a325df1f6b | 174 | |
uci1 | 8:95a325df1f6b | 175 | template<class T> |
uci1 | 8:95a325df1f6b | 176 | static |
uci1 | 8:95a325df1f6b | 177 | SnCommWin::ECommWinResult ReadBlockHeader(T& f, |
uci1 | 8:95a325df1f6b | 178 | uint8_t& mcode, |
uci1 | 8:95a325df1f6b | 179 | uint32_t& mlen) { |
uci1 | 38:9070c17536cd | 180 | return SnHeaderFrame::ReadFrom(f, mcode, mlen) ? |
uci1 | 38:9070c17536cd | 181 | SnCommWin::kOkWithMsg : SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 182 | } |
uci1 | 8:95a325df1f6b | 183 | |
uci1 | 5:9cea89700c66 | 184 | template<class T> |
uci1 | 5:9cea89700c66 | 185 | static |
uci1 | 5:9cea89700c66 | 186 | SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr, |
uci1 | 8:95a325df1f6b | 187 | const uint32_t run, const uint16_t seq) { |
uci1 | 5:9cea89700c66 | 188 | // MUST INCREMENT kIOvers if these writes are altered |
uci1 | 5:9cea89700c66 | 189 | f = SnBitUtils::WriteTo(f, kIOvers); |
uci1 | 5:9cea89700c66 | 190 | f = SnBitUtils::WriteTo(f, macadr); |
uci1 | 5:9cea89700c66 | 191 | f = SnBitUtils::WriteTo(f, run); |
uci1 | 5:9cea89700c66 | 192 | f = SnBitUtils::WriteTo(f, seq); |
uci1 | 5:9cea89700c66 | 193 | return SnCommWin::kOkMsgSent; |
uci1 | 5:9cea89700c66 | 194 | } |
uci1 | 5:9cea89700c66 | 195 | |
uci1 | 5:9cea89700c66 | 196 | template<class T> |
uci1 | 5:9cea89700c66 | 197 | static |
uci1 | 5:9cea89700c66 | 198 | SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr, |
uci1 | 5:9cea89700c66 | 199 | uint32_t& run, uint16_t& seq, |
uci1 | 8:95a325df1f6b | 200 | SnPowerFrame* pow=0) { |
uci1 | 8:95a325df1f6b | 201 | SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg; |
uci1 | 5:9cea89700c66 | 202 | uint8_t Rv=0; |
uci1 | 5:9cea89700c66 | 203 | f = SnBitUtils::ReadFrom(f, Rv); |
uci1 | 5:9cea89700c66 | 204 | f = SnBitUtils::ReadFrom(f, macadr); |
uci1 | 5:9cea89700c66 | 205 | f = SnBitUtils::ReadFrom(f, run); |
uci1 | 5:9cea89700c66 | 206 | f = SnBitUtils::ReadFrom(f, seq); |
uci1 | 8:95a325df1f6b | 207 | if (Rv==2) { |
uci1 | 8:95a325df1f6b | 208 | uint16_t v1, v2; |
uci1 | 8:95a325df1f6b | 209 | f = SnBitUtils::ReadFrom(f, v1); |
uci1 | 8:95a325df1f6b | 210 | f = SnBitUtils::ReadFrom(f, v2); |
uci1 | 8:95a325df1f6b | 211 | if (pow!=0) { |
uci1 | 8:95a325df1f6b | 212 | pow->Set(v1, v2, 0, 0, 0); |
uci1 | 8:95a325df1f6b | 213 | } |
uci1 | 5:9cea89700c66 | 214 | } |
uci1 | 8:95a325df1f6b | 215 | return res; |
uci1 | 5:9cea89700c66 | 216 | } |
uci1 | 5:9cea89700c66 | 217 | |
uci1 | 5:9cea89700c66 | 218 | static |
uci1 | 8:95a325df1f6b | 219 | uint32_t SizeOfFileHeader(const uint8_t rv) { |
uci1 | 8:95a325df1f6b | 220 | if (rv==2) { |
uci1 | 8:95a325df1f6b | 221 | return kMaxSizeOfFileHdr; |
uci1 | 8:95a325df1f6b | 222 | } else { |
uci1 | 8:95a325df1f6b | 223 | return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t); |
uci1 | 8:95a325df1f6b | 224 | } |
uci1 | 8:95a325df1f6b | 225 | } |
uci1 | 0:664899e0b988 | 226 | |
uci1 | 0:664899e0b988 | 227 | friend class SnSDUtilsWhisperer; // to restrict access to specific functions |
uci1 | 0:664899e0b988 | 228 | }; |
uci1 | 0:664899e0b988 | 229 | |
uci1 | 0:664899e0b988 | 230 | class SnSDUtilsWhisperer { |
uci1 | 0:664899e0b988 | 231 | static |
uci1 | 0:664899e0b988 | 232 | void DeleteFile(FILE*& f, const char* fname) { |
uci1 | 27:efc4d654b139 | 233 | SnSDUtils::DeleteFile(f, fname); |
uci1 | 27:efc4d654b139 | 234 | } |
uci1 | 27:efc4d654b139 | 235 | |
uci1 | 27:efc4d654b139 | 236 | static |
uci1 | 27:efc4d654b139 | 237 | void DeleteAllFiles(const char* dirname=SnSDUtils::kSDsubDir) { |
uci1 | 27:efc4d654b139 | 238 | SnSDUtils::DeleteAllFiles(dirname); |
uci1 | 27:efc4d654b139 | 239 | } |
uci1 | 27:efc4d654b139 | 240 | |
uci1 | 27:efc4d654b139 | 241 | static |
uci1 | 27:efc4d654b139 | 242 | void DeleteFilesOfRun(const uint32_t run) { |
uci1 | 27:efc4d654b139 | 243 | SnSDUtils::DeleteFilesOfRun(run); |
uci1 | 0:664899e0b988 | 244 | } |
uci1 | 25:57b2627fe756 | 245 | |
uci1 | 0:664899e0b988 | 246 | friend class SnCommWin; // the one who's allowed to use me |
uci1 | 0:664899e0b988 | 247 | }; |
uci1 | 0:664899e0b988 | 248 | |
uci1 | 0:664899e0b988 | 249 | #endif // SN_SnSDUtils |