Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.h@8:95a325df1f6b, 2012-08-08 (annotated)
- Committer:
- uci1
- Date:
- Wed Aug 08 23:27:37 2012 +0000
- Revision:
- 8:95a325df1f6b
- Parent:
- 6:6f002d202f59
- Child:
- 10:3c93db1cfb12
Check power for low pow running. Average power over 500 readings. Use RTOS timers instead of tickers. Allow data taking of events, power readings or both, even if cards are off. Bug in EthernetInterface prevents IPs being reset. Lots of debug output.
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 | 0:664899e0b988 | 14 | |
uci1 | 0:664899e0b988 | 15 | // a namespace-like class to handle the i/o with the SD card |
uci1 | 0:664899e0b988 | 16 | |
uci1 | 0:664899e0b988 | 17 | class SnSDUtils { |
uci1 | 0:664899e0b988 | 18 | public: |
uci1 | 1:e392595b4b76 | 19 | static const char* const kSDsubDir; |
uci1 | 1:e392595b4b76 | 20 | static const uint8_t kFNBufSize=128; |
uci1 | 1:e392595b4b76 | 21 | static const uint8_t kIOvers; // file I/O version |
uci1 | 5:9cea89700c66 | 22 | static const uint32_t kMaxSizeOfFileHdr; |
uci1 | 0:664899e0b988 | 23 | |
uci1 | 0:664899e0b988 | 24 | private: |
uci1 | 0:664899e0b988 | 25 | |
uci1 | 0:664899e0b988 | 26 | static |
uci1 | 3:24c5f0f50bf1 | 27 | FILE* OpenSDFile(const char* name, const char* mode); |
uci1 | 1:e392595b4b76 | 28 | |
uci1 | 1:e392595b4b76 | 29 | static |
uci1 | 0:664899e0b988 | 30 | uint16_t GetSeqNum(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 31 | const uint32_t run); |
uci1 | 0:664899e0b988 | 32 | |
uci1 | 0:664899e0b988 | 33 | static |
uci1 | 0:664899e0b988 | 34 | const char* GetOutFileName(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 35 | const uint32_t run, |
uci1 | 0:664899e0b988 | 36 | const uint16_t seq); |
uci1 | 0:664899e0b988 | 37 | |
uci1 | 0:664899e0b988 | 38 | static |
uci1 | 0:664899e0b988 | 39 | void DeleteFile(FILE*& f, const char* fname); |
uci1 | 0:664899e0b988 | 40 | |
uci1 | 0:664899e0b988 | 41 | static char fgCurFileName[kFNBufSize]; |
uci1 | 1:e392595b4b76 | 42 | static FILE* fgCurFile; |
uci1 | 0:664899e0b988 | 43 | |
uci1 | 0:664899e0b988 | 44 | public: |
uci1 | 0:664899e0b988 | 45 | SnSDUtils() {} |
uci1 | 0:664899e0b988 | 46 | virtual ~SnSDUtils() {} |
uci1 | 0:664899e0b988 | 47 | |
uci1 | 0:664899e0b988 | 48 | static |
uci1 | 0:664899e0b988 | 49 | int CloseOutputFile(FILE* f) |
uci1 | 0:664899e0b988 | 50 | { return (f!=0) ? fclose(f) : 0; } |
uci1 | 0:664899e0b988 | 51 | |
uci1 | 0:664899e0b988 | 52 | static |
uci1 | 0:664899e0b988 | 53 | FILE* OpenNewOutputFile(const uint64_t macadr, |
uci1 | 8:95a325df1f6b | 54 | const uint32_t run); |
uci1 | 0:664899e0b988 | 55 | |
uci1 | 0:664899e0b988 | 56 | static |
uci1 | 3:24c5f0f50bf1 | 57 | FILE* OpenExistingFile(const char* name, const bool setcurrent); |
uci1 | 0:664899e0b988 | 58 | |
uci1 | 0:664899e0b988 | 59 | static |
uci1 | 0:664899e0b988 | 60 | SnCommWin::ECommWinResult SendAllFiles(SnCommWin* comm, |
uci1 | 3:24c5f0f50bf1 | 61 | const bool doDelete, |
uci1 | 3:24c5f0f50bf1 | 62 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 63 | char* const buf, |
uci1 | 6:6f002d202f59 | 64 | const uint32_t bsize, |
uci1 | 6:6f002d202f59 | 65 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 66 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 67 | SnPowerFrame& pow); |
uci1 | 0:664899e0b988 | 68 | |
uci1 | 0:664899e0b988 | 69 | static |
uci1 | 0:664899e0b988 | 70 | const char* GetCurFileName() { return fgCurFileName; } |
uci1 | 0:664899e0b988 | 71 | |
uci1 | 0:664899e0b988 | 72 | static |
uci1 | 1:e392595b4b76 | 73 | FILE* GetCurFile() { return fgCurFile; } |
uci1 | 1:e392595b4b76 | 74 | |
uci1 | 1:e392595b4b76 | 75 | static |
uci1 | 0:664899e0b988 | 76 | bool WriteEventTo(FILE* efile, char* const evtBuf, |
uci1 | 0:664899e0b988 | 77 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 78 | const SnConfigFrame& conf); |
uci1 | 0:664899e0b988 | 79 | |
uci1 | 0:664899e0b988 | 80 | static |
uci1 | 0:664899e0b988 | 81 | bool WriteConfig(FILE* efile, |
uci1 | 0:664899e0b988 | 82 | const SnConfigFrame& conf); |
uci1 | 2:e67f7c158087 | 83 | |
uci1 | 8:95a325df1f6b | 84 | template<class T> |
uci1 | 2:e67f7c158087 | 85 | static |
uci1 | 8:95a325df1f6b | 86 | SnCommWin::ECommWinResult WritePowerTo(T& f, |
uci1 | 8:95a325df1f6b | 87 | const SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 88 | uint32_t& pnum) { |
uci1 | 8:95a325df1f6b | 89 | const SnCommWin::ECommWinResult rs = |
uci1 | 8:95a325df1f6b | 90 | SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode, |
uci1 | 8:95a325df1f6b | 91 | SnPowerFrame::SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 8:95a325df1f6b | 92 | const SnCommWin::ECommWinResult re = pow.WriteTo(f); |
uci1 | 8:95a325df1f6b | 93 | ++pnum; |
uci1 | 8:95a325df1f6b | 94 | return (rs<re) ? rs : re; |
uci1 | 8:95a325df1f6b | 95 | } |
uci1 | 8:95a325df1f6b | 96 | |
uci1 | 8:95a325df1f6b | 97 | template<class T> |
uci1 | 8:95a325df1f6b | 98 | static |
uci1 | 8:95a325df1f6b | 99 | SnCommWin::ECommWinResult ReadBlockHeader(T& f, |
uci1 | 8:95a325df1f6b | 100 | uint8_t& mcode, |
uci1 | 8:95a325df1f6b | 101 | uint32_t& mlen) { |
uci1 | 8:95a325df1f6b | 102 | return SnHeaderFrame::ReadFrom(f, mcode, mlen); |
uci1 | 8:95a325df1f6b | 103 | } |
uci1 | 8:95a325df1f6b | 104 | |
uci1 | 5:9cea89700c66 | 105 | template<class T> |
uci1 | 5:9cea89700c66 | 106 | static |
uci1 | 5:9cea89700c66 | 107 | SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr, |
uci1 | 8:95a325df1f6b | 108 | const uint32_t run, const uint16_t seq) { |
uci1 | 5:9cea89700c66 | 109 | // MUST INCREMENT kIOvers if these writes are altered |
uci1 | 5:9cea89700c66 | 110 | f = SnBitUtils::WriteTo(f, kIOvers); |
uci1 | 5:9cea89700c66 | 111 | f = SnBitUtils::WriteTo(f, macadr); |
uci1 | 5:9cea89700c66 | 112 | f = SnBitUtils::WriteTo(f, run); |
uci1 | 5:9cea89700c66 | 113 | f = SnBitUtils::WriteTo(f, seq); |
uci1 | 5:9cea89700c66 | 114 | return SnCommWin::kOkMsgSent; |
uci1 | 5:9cea89700c66 | 115 | } |
uci1 | 5:9cea89700c66 | 116 | |
uci1 | 5:9cea89700c66 | 117 | template<class T> |
uci1 | 5:9cea89700c66 | 118 | static |
uci1 | 5:9cea89700c66 | 119 | SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr, |
uci1 | 5:9cea89700c66 | 120 | uint32_t& run, uint16_t& seq, |
uci1 | 8:95a325df1f6b | 121 | SnPowerFrame* pow=0) { |
uci1 | 8:95a325df1f6b | 122 | SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg; |
uci1 | 5:9cea89700c66 | 123 | uint8_t Rv=0; |
uci1 | 5:9cea89700c66 | 124 | f = SnBitUtils::ReadFrom(f, Rv); |
uci1 | 5:9cea89700c66 | 125 | f = SnBitUtils::ReadFrom(f, macadr); |
uci1 | 5:9cea89700c66 | 126 | f = SnBitUtils::ReadFrom(f, run); |
uci1 | 5:9cea89700c66 | 127 | f = SnBitUtils::ReadFrom(f, seq); |
uci1 | 8:95a325df1f6b | 128 | if (Rv==2) { |
uci1 | 8:95a325df1f6b | 129 | uint16_t v1, v2; |
uci1 | 8:95a325df1f6b | 130 | f = SnBitUtils::ReadFrom(f, v1); |
uci1 | 8:95a325df1f6b | 131 | f = SnBitUtils::ReadFrom(f, v2); |
uci1 | 8:95a325df1f6b | 132 | if (pow!=0) { |
uci1 | 8:95a325df1f6b | 133 | pow->Set(v1, v2, 0, 0, 0); |
uci1 | 8:95a325df1f6b | 134 | } |
uci1 | 5:9cea89700c66 | 135 | } |
uci1 | 8:95a325df1f6b | 136 | return res; |
uci1 | 5:9cea89700c66 | 137 | } |
uci1 | 5:9cea89700c66 | 138 | |
uci1 | 5:9cea89700c66 | 139 | static |
uci1 | 8:95a325df1f6b | 140 | uint32_t SizeOfFileHeader(const uint8_t rv) { |
uci1 | 8:95a325df1f6b | 141 | if (rv==2) { |
uci1 | 8:95a325df1f6b | 142 | return kMaxSizeOfFileHdr; |
uci1 | 8:95a325df1f6b | 143 | } else { |
uci1 | 8:95a325df1f6b | 144 | return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t); |
uci1 | 8:95a325df1f6b | 145 | } |
uci1 | 8:95a325df1f6b | 146 | } |
uci1 | 0:664899e0b988 | 147 | |
uci1 | 0:664899e0b988 | 148 | friend class SnSDUtilsWhisperer; // to restrict access to specific functions |
uci1 | 0:664899e0b988 | 149 | }; |
uci1 | 0:664899e0b988 | 150 | |
uci1 | 0:664899e0b988 | 151 | class SnSDUtilsWhisperer { |
uci1 | 0:664899e0b988 | 152 | static |
uci1 | 0:664899e0b988 | 153 | void DeleteFile(FILE*& f, const char* fname) { |
uci1 | 0:664899e0b988 | 154 | return SnSDUtils::DeleteFile(f, fname); |
uci1 | 0:664899e0b988 | 155 | } |
uci1 | 0:664899e0b988 | 156 | |
uci1 | 0:664899e0b988 | 157 | friend class SnCommWin; // the one who's allowed to use me |
uci1 | 0:664899e0b988 | 158 | }; |
uci1 | 0:664899e0b988 | 159 | |
uci1 | 0:664899e0b988 | 160 | #endif // SN_SnSDUtils |