Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed May 29 18:54:19 2013 +0000
Revision:
38:9070c17536cd
Parent:
27:efc4d654b139
Child:
40:1324da35afd4
Update SnCRCUtils to use templated function. Add MODSERIAL include to SnCommWinUsb when enabled. Move iridium constants to SnCommConstants.

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