Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Oct 02 01:58:50 2012 +0000
Revision:
19:74155d652c37
Parent:
15:f2569d8e4176
Child:
21:ce51bb0ba4a5
Moved output directory of event files to /sd/data in order to avoid the SD file system's maximum number of files in its root directory. Fix bug in SnConfigFrame::SizeOf to account for new i/o version 4 (number of bytes should be equal, tho). This versi...

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 10:3c93db1cfb12 85
uci1 10:3c93db1cfb12 86 static
uci1 0:664899e0b988 87 bool WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 88 const SnEventFrame& evt,
uci1 0:664899e0b988 89 const SnConfigFrame& conf);
uci1 0:664899e0b988 90
uci1 0:664899e0b988 91 static
uci1 0:664899e0b988 92 bool WriteConfig(FILE* efile,
uci1 0:664899e0b988 93 const SnConfigFrame& conf);
uci1 2:e67f7c158087 94
uci1 8:95a325df1f6b 95 template<class T>
uci1 2:e67f7c158087 96 static
uci1 8:95a325df1f6b 97 SnCommWin::ECommWinResult WritePowerTo(T& f,
uci1 8:95a325df1f6b 98 const SnPowerFrame& pow,
uci1 15:f2569d8e4176 99 uint32_t& pnum) {
uci1 8:95a325df1f6b 100 const SnCommWin::ECommWinResult rs =
uci1 8:95a325df1f6b 101 SnHeaderFrame::WriteTo(f, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 102 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 103 const SnCommWin::ECommWinResult re = pow.WriteTo(f);
uci1 8:95a325df1f6b 104 ++pnum;
uci1 8:95a325df1f6b 105 return (rs<re) ? rs : re;
uci1 8:95a325df1f6b 106 }
uci1 8:95a325df1f6b 107
uci1 8:95a325df1f6b 108 template<class T>
uci1 8:95a325df1f6b 109 static
uci1 8:95a325df1f6b 110 SnCommWin::ECommWinResult ReadBlockHeader(T& f,
uci1 8:95a325df1f6b 111 uint8_t& mcode,
uci1 8:95a325df1f6b 112 uint32_t& mlen) {
uci1 8:95a325df1f6b 113 return SnHeaderFrame::ReadFrom(f, mcode, mlen);
uci1 8:95a325df1f6b 114 }
uci1 8:95a325df1f6b 115
uci1 5:9cea89700c66 116 template<class T>
uci1 5:9cea89700c66 117 static
uci1 5:9cea89700c66 118 SnCommWin::ECommWinResult WriteFileHeader(T& f, const uint64_t macadr,
uci1 8:95a325df1f6b 119 const uint32_t run, const uint16_t seq) {
uci1 5:9cea89700c66 120 // MUST INCREMENT kIOvers if these writes are altered
uci1 5:9cea89700c66 121 f = SnBitUtils::WriteTo(f, kIOvers);
uci1 5:9cea89700c66 122 f = SnBitUtils::WriteTo(f, macadr);
uci1 5:9cea89700c66 123 f = SnBitUtils::WriteTo(f, run);
uci1 5:9cea89700c66 124 f = SnBitUtils::WriteTo(f, seq);
uci1 5:9cea89700c66 125 return SnCommWin::kOkMsgSent;
uci1 5:9cea89700c66 126 }
uci1 5:9cea89700c66 127
uci1 5:9cea89700c66 128 template<class T>
uci1 5:9cea89700c66 129 static
uci1 5:9cea89700c66 130 SnCommWin::ECommWinResult ReadFileHeader(T& f, uint64_t& macadr,
uci1 5:9cea89700c66 131 uint32_t& run, uint16_t& seq,
uci1 8:95a325df1f6b 132 SnPowerFrame* pow=0) {
uci1 8:95a325df1f6b 133 SnCommWin::ECommWinResult res = SnCommWin::kOkWithMsg;
uci1 5:9cea89700c66 134 uint8_t Rv=0;
uci1 5:9cea89700c66 135 f = SnBitUtils::ReadFrom(f, Rv);
uci1 5:9cea89700c66 136 f = SnBitUtils::ReadFrom(f, macadr);
uci1 5:9cea89700c66 137 f = SnBitUtils::ReadFrom(f, run);
uci1 5:9cea89700c66 138 f = SnBitUtils::ReadFrom(f, seq);
uci1 8:95a325df1f6b 139 if (Rv==2) {
uci1 8:95a325df1f6b 140 uint16_t v1, v2;
uci1 8:95a325df1f6b 141 f = SnBitUtils::ReadFrom(f, v1);
uci1 8:95a325df1f6b 142 f = SnBitUtils::ReadFrom(f, v2);
uci1 8:95a325df1f6b 143 if (pow!=0) {
uci1 8:95a325df1f6b 144 pow->Set(v1, v2, 0, 0, 0);
uci1 8:95a325df1f6b 145 }
uci1 5:9cea89700c66 146 }
uci1 8:95a325df1f6b 147 return res;
uci1 5:9cea89700c66 148 }
uci1 5:9cea89700c66 149
uci1 5:9cea89700c66 150 static
uci1 8:95a325df1f6b 151 uint32_t SizeOfFileHeader(const uint8_t rv) {
uci1 8:95a325df1f6b 152 if (rv==2) {
uci1 8:95a325df1f6b 153 return kMaxSizeOfFileHdr;
uci1 8:95a325df1f6b 154 } else {
uci1 8:95a325df1f6b 155 return sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t);
uci1 8:95a325df1f6b 156 }
uci1 8:95a325df1f6b 157 }
uci1 0:664899e0b988 158
uci1 0:664899e0b988 159 friend class SnSDUtilsWhisperer; // to restrict access to specific functions
uci1 0:664899e0b988 160 };
uci1 0:664899e0b988 161
uci1 0:664899e0b988 162 class SnSDUtilsWhisperer {
uci1 0:664899e0b988 163 static
uci1 0:664899e0b988 164 void DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 165 return SnSDUtils::DeleteFile(f, fname);
uci1 0:664899e0b988 166 }
uci1 0:664899e0b988 167
uci1 0:664899e0b988 168 friend class SnCommWin; // the one who's allowed to use me
uci1 0:664899e0b988 169 };
uci1 0:664899e0b988 170
uci1 0:664899e0b988 171 #endif // SN_SnSDUtils