Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 16 04:47:44 2012 +0000
Revision:
22:f957c4f840ad
Parent:
21:ce51bb0ba4a5
Child:
25:57b2627fe756
USB comm only. Make firing of comm window independent of real time clock. Add heartbeat firing time to the data stream.

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 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 19:74155d652c37 19 static const char* const kSDdir;
uci1 1:e392595b4b76 20 static const char* const kSDsubDir;
uci1 1:e392595b4b76 21 static const uint8_t kFNBufSize=128;
uci1 1:e392595b4b76 22 static const uint8_t kIOvers; // file I/O version
uci1 5:9cea89700c66 23 static const uint32_t kMaxSizeOfFileHdr;
uci1 0:664899e0b988 24
uci1 0:664899e0b988 25 private:
uci1 0:664899e0b988 26
uci1 0:664899e0b988 27 static
uci1 3:24c5f0f50bf1 28 FILE* OpenSDFile(const char* name, const char* mode);
uci1 1:e392595b4b76 29
uci1 1:e392595b4b76 30 static
uci1 0:664899e0b988 31 uint16_t GetSeqNum(const uint64_t macadr,
uci1 0:664899e0b988 32 const uint32_t run);
uci1 0:664899e0b988 33
uci1 0:664899e0b988 34 static
uci1 0:664899e0b988 35 const char* GetOutFileName(const uint64_t macadr,
uci1 0:664899e0b988 36 const uint32_t run,
uci1 0:664899e0b988 37 const uint16_t seq);
uci1 0:664899e0b988 38
uci1 0:664899e0b988 39 static
uci1 0:664899e0b988 40 void DeleteFile(FILE*& f, const char* fname);
uci1 0:664899e0b988 41
uci1 12:d472f9811262 42 static char fgCurFileName[kFNBufSize];
uci1 12:d472f9811262 43 static FILE* fgCurFile;
uci1 12:d472f9811262 44 static uint16_t fgCurSeq;
uci1 0:664899e0b988 45
uci1 0:664899e0b988 46 public:
uci1 0:664899e0b988 47 SnSDUtils() {}
uci1 0:664899e0b988 48 virtual ~SnSDUtils() {}
uci1 0:664899e0b988 49
uci1 0:664899e0b988 50 static
uci1 10:3c93db1cfb12 51 int CloseOutputFile(FILE* f) {
uci1 12:d472f9811262 52 // TODO: set current file pointer to 0, IF f is current file
uci1 10:3c93db1cfb12 53 const int rt = (f!=0) ? fclose(f) : 0;
uci1 10:3c93db1cfb12 54 return rt;
uci1 10:3c93db1cfb12 55 }
uci1 0:664899e0b988 56
uci1 0:664899e0b988 57 static
uci1 0:664899e0b988 58 FILE* OpenNewOutputFile(const uint64_t macadr,
uci1 8:95a325df1f6b 59 const uint32_t run);
uci1 0:664899e0b988 60
uci1 0:664899e0b988 61 static
uci1 3:24c5f0f50bf1 62 FILE* OpenExistingFile(const char* name, const bool setcurrent);
uci1 0:664899e0b988 63
uci1 0:664899e0b988 64 static
uci1 0:664899e0b988 65 SnCommWin::ECommWinResult SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 66 const uint32_t timeout,
uci1 3:24c5f0f50bf1 67 char* const buf,
uci1 6:6f002d202f59 68 const uint32_t bsize,
uci1 6:6f002d202f59 69 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 70 SnEventFrame& evt,
uci1 12:d472f9811262 71 SnPowerFrame& pow,
uci1 12:d472f9811262 72 const uint32_t handshakeTimeout);
uci1 0:664899e0b988 73
uci1 0:664899e0b988 74 static
uci1 0:664899e0b988 75 const char* GetCurFileName() { return fgCurFileName; }
uci1 0:664899e0b988 76
uci1 0:664899e0b988 77 static
uci1 1:e392595b4b76 78 FILE* GetCurFile() { return fgCurFile; }
uci1 1:e392595b4b76 79
uci1 1:e392595b4b76 80 static
uci1 12:d472f9811262 81 uint16_t GetCurSeqNum() { return fgCurSeq; }
uci1 10:3c93db1cfb12 82
uci1 10:3c93db1cfb12 83 static
uci1 10:3c93db1cfb12 84 uint16_t GetSeqNumFromFileName(const char* fn);
uci1 21:ce51bb0ba4a5 85
uci1 21:ce51bb0ba4a5 86 static
uci1 21:ce51bb0ba4a5 87 void GetDirProps(const char* dirname,
uci1 21:ce51bb0ba4a5 88 uint32_t& nfiles,
uci1 21:ce51bb0ba4a5 89 float& totbytes);
uci1 10:3c93db1cfb12 90
uci1 10:3c93db1cfb12 91 static
uci1 22:f957c4f840ad 92 bool WriteHeartbeatTo(FILE* file,
uci1 22:f957c4f840ad 93 const uint32_t time,
uci1 22:f957c4f840ad 94 const uint32_t num);
uci1 22:f957c4f840ad 95
uci1 22:f957c4f840ad 96 static
uci1 0:664899e0b988 97 bool WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 98 const SnEventFrame& evt,
uci1 0:664899e0b988 99 const SnConfigFrame& conf);
uci1 0:664899e0b988 100
uci1 0:664899e0b988 101 static
uci1 0:664899e0b988 102 bool WriteConfig(FILE* efile,
uci1 0:664899e0b988 103 const SnConfigFrame& conf);
uci1 2:e67f7c158087 104
uci1 8:95a325df1f6b 105 template<class T>
uci1 2:e67f7c158087 106 static
uci1 8:95a325df1f6b 107 SnCommWin::ECommWinResult WritePowerTo(T& f,
uci1 8:95a325df1f6b 108 const SnPowerFrame& pow,
uci1 15:f2569d8e4176 109 uint32_t& pnum) {
uci1 8:95a325df1f6b 110 const SnCommWin::ECommWinResult rs =
uci1 8:95a325df1f6b 111 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 112 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 113 const SnCommWin::ECommWinResult re = pow.WriteTo(f);
uci1 8:95a325df1f6b 114 ++pnum;
uci1 8:95a325df1f6b 115 return (rs<re) ? rs : re;
uci1 8:95a325df1f6b 116 }
uci1 8:95a325df1f6b 117
uci1 8:95a325df1f6b 118 template<class T>
uci1 8:95a325df1f6b 119 static
uci1 8:95a325df1f6b 120 SnCommWin::ECommWinResult ReadBlockHeader(T& f,
uci1 8:95a325df1f6b 121 uint8_t& mcode,
uci1 8:95a325df1f6b 122 uint32_t& mlen) {
uci1 8:95a325df1f6b 123 return SnHeaderFrame::ReadFrom(f, mcode, mlen);
uci1 8:95a325df1f6b 124 }
uci1 8:95a325df1f6b 125
uci1 5:9cea89700c66 126 template<class T>
uci1 5:9cea89700c66 127 static
uci1 5:9cea89700c66 128 SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr,
uci1 8:95a325df1f6b 129 const uint32_t run, const uint16_t seq) {
uci1 5:9cea89700c66 130 // MUST INCREMENT kIOvers if these writes are altered
uci1 5:9cea89700c66 131 f = SnBitUtils::WriteTo(f, kIOvers);
uci1 5:9cea89700c66 132 f = SnBitUtils::WriteTo(f, macadr);
uci1 5:9cea89700c66 133 f = SnBitUtils::WriteTo(f, run);
uci1 5:9cea89700c66 134 f = SnBitUtils::WriteTo(f, seq);
uci1 5:9cea89700c66 135 return SnCommWin::kOkMsgSent;
uci1 5:9cea89700c66 136 }
uci1 5:9cea89700c66 137
uci1 5:9cea89700c66 138 template<class T>
uci1 5:9cea89700c66 139 static
uci1 5:9cea89700c66 140 SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr,
uci1 5:9cea89700c66 141 uint32_t& run, uint16_t& seq,
uci1 8:95a325df1f6b 142 SnPowerFrame* pow=0) {
uci1 8:95a325df1f6b 143 SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg;
uci1 5:9cea89700c66 144 uint8_t Rv=0;
uci1 5:9cea89700c66 145 f = SnBitUtils::ReadFrom(f, Rv);
uci1 5:9cea89700c66 146 f = SnBitUtils::ReadFrom(f, macadr);
uci1 5:9cea89700c66 147 f = SnBitUtils::ReadFrom(f, run);
uci1 5:9cea89700c66 148 f = SnBitUtils::ReadFrom(f, seq);
uci1 8:95a325df1f6b 149 if (Rv==2) {
uci1 8:95a325df1f6b 150 uint16_t v1, v2;
uci1 8:95a325df1f6b 151 f = SnBitUtils::ReadFrom(f, v1);
uci1 8:95a325df1f6b 152 f = SnBitUtils::ReadFrom(f, v2);
uci1 8:95a325df1f6b 153 if (pow!=0) {
uci1 8:95a325df1f6b 154 pow->Set(v1, v2, 0, 0, 0);
uci1 8:95a325df1f6b 155 }
uci1 5:9cea89700c66 156 }
uci1 8:95a325df1f6b 157 return res;
uci1 5:9cea89700c66 158 }
uci1 5:9cea89700c66 159
uci1 5:9cea89700c66 160 static
uci1 8:95a325df1f6b 161 uint32_t SizeOfFileHeader(const uint8_t rv) {
uci1 8:95a325df1f6b 162 if (rv==2) {
uci1 8:95a325df1f6b 163 return kMaxSizeOfFileHdr;
uci1 8:95a325df1f6b 164 } else {
uci1 8:95a325df1f6b 165 return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t);
uci1 8:95a325df1f6b 166 }
uci1 8:95a325df1f6b 167 }
uci1 0:664899e0b988 168
uci1 0:664899e0b988 169 friend class SnSDUtilsWhisperer; // to restrict access to specific functions
uci1 0:664899e0b988 170 };
uci1 0:664899e0b988 171
uci1 0:664899e0b988 172 class SnSDUtilsWhisperer {
uci1 0:664899e0b988 173 static
uci1 0:664899e0b988 174 void DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 175 return SnSDUtils::DeleteFile(f, fname);
uci1 0:664899e0b988 176 }
uci1 0:664899e0b988 177
uci1 0:664899e0b988 178 friend class SnCommWin; // the one who's allowed to use me
uci1 0:664899e0b988 179 };
uci1 0:664899e0b988 180
uci1 0:664899e0b988 181 #endif // SN_SnSDUtils