Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnSDUtils.cpp@19:74155d652c37, 2012-10-02 (annotated)
- Committer:
- uci1
- Date:
- Tue Oct 02 01:58:50 2012 +0000
- Revision:
- 19:74155d652c37
- Parent:
- 16:744ce85aede2
- Child:
- 20:e5857b287b3b
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?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | #include "SnSDUtils.h" |
uci1 | 0:664899e0b988 | 2 | |
uci1 | 0:664899e0b988 | 3 | #include "mbed.h" |
uci1 | 0:664899e0b988 | 4 | #include "SDFileSystem.h" |
uci1 | 0:664899e0b988 | 5 | #include <string.h> |
uci1 | 12:d472f9811262 | 6 | #include <string> |
uci1 | 0:664899e0b988 | 7 | |
uci1 | 0:664899e0b988 | 8 | #include "SnConfigFrame.h" |
uci1 | 0:664899e0b988 | 9 | #include "SnEventFrame.h" |
uci1 | 0:664899e0b988 | 10 | |
uci1 | 15:f2569d8e4176 | 11 | //#define DEBUG |
uci1 | 0:664899e0b988 | 12 | |
uci1 | 19:74155d652c37 | 13 | const char* const SnSDUtils::kSDdir = "/sd"; |
uci1 | 19:74155d652c37 | 14 | const char* const SnSDUtils::kSDsubDir = "/sd/data"; |
uci1 | 2:e67f7c158087 | 15 | char SnSDUtils::fgCurFileName[kFNBufSize]={0}; |
uci1 | 1:e392595b4b76 | 16 | FILE* SnSDUtils::fgCurFile = 0; |
uci1 | 12:d472f9811262 | 17 | uint16_t SnSDUtils::fgCurSeq = 0; |
uci1 | 8:95a325df1f6b | 18 | const uint8_t SnSDUtils::kIOvers = 3; |
uci1 | 5:9cea89700c66 | 19 | const uint32_t SnSDUtils::kMaxSizeOfFileHdr = |
uci1 | 5:9cea89700c66 | 20 | sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t) |
uci1 | 8:95a325df1f6b | 21 | +(sizeof(uint8_t)+(2u*sizeof(uint16_t))); // power frame v1 |
uci1 | 4:a91682e19d6b | 22 | |
uci1 | 4:a91682e19d6b | 23 | static const uint16_t __kMaxUShort = ~0; |
uci1 | 0:664899e0b988 | 24 | |
uci1 | 0:664899e0b988 | 25 | const char* SnSDUtils::GetOutFileName(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 26 | const uint32_t run, |
uci1 | 0:664899e0b988 | 27 | const uint16_t seq) { |
uci1 | 0:664899e0b988 | 28 | // returns the formatted file name, or NULL if the directory is too long |
uci1 | 0:664899e0b988 | 29 | // and the full name cannot fit in the buffer |
uci1 | 0:664899e0b988 | 30 | // NOTE: this fcn uses a static buffer, and so should not be called |
uci1 | 0:664899e0b988 | 31 | // multiple times in the same line (that includes the calling of functions |
uci1 | 0:664899e0b988 | 32 | // that call this function!) |
uci1 | 0:664899e0b988 | 33 | // |
uci1 | 0:664899e0b988 | 34 | // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat |
uci1 | 0:664899e0b988 | 35 | // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4 |
uci1 | 0:664899e0b988 | 36 | |
uci1 | 0:664899e0b988 | 37 | if (strlen(kSDsubDir)<(kFNBufSize-37)) { |
uci1 | 0:664899e0b988 | 38 | static char tbuf[kFNBufSize]; |
uci1 | 0:664899e0b988 | 39 | memset(tbuf, 0, sizeof(char)*kFNBufSize); |
uci1 | 0:664899e0b988 | 40 | // if file name format changes, GetSeqNum must be changed too |
uci1 | 0:664899e0b988 | 41 | sprintf(tbuf, "%s/SnEvtsM%012llXr%05ds%05d.dat", |
uci1 | 0:664899e0b988 | 42 | kSDsubDir, |
uci1 | 0:664899e0b988 | 43 | macadr>>16, // 64 -> 48 bits |
uci1 | 0:664899e0b988 | 44 | run, seq); |
uci1 | 0:664899e0b988 | 45 | return tbuf; |
uci1 | 0:664899e0b988 | 46 | } else { |
uci1 | 0:664899e0b988 | 47 | return NULL; |
uci1 | 0:664899e0b988 | 48 | } |
uci1 | 0:664899e0b988 | 49 | } |
uci1 | 12:d472f9811262 | 50 | /* |
uci1 | 10:3c93db1cfb12 | 51 | uint16_t SnSDUtils::GetCurSeqNum() { |
uci1 | 10:3c93db1cfb12 | 52 | return GetSeqNumFromFileName(fgCurFileName); |
uci1 | 10:3c93db1cfb12 | 53 | } |
uci1 | 12:d472f9811262 | 54 | */ |
uci1 | 10:3c93db1cfb12 | 55 | uint16_t SnSDUtils::GetSeqNumFromFileName(const char* fn) { |
uci1 | 10:3c93db1cfb12 | 56 | uint16_t seq=0; |
uci1 | 10:3c93db1cfb12 | 57 | const uint32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 10:3c93db1cfb12 | 58 | if (ncomp<strlen(fn)) { |
uci1 | 10:3c93db1cfb12 | 59 | sscanf(fn+ncomp,"s%hu.dat",&seq); |
uci1 | 10:3c93db1cfb12 | 60 | } |
uci1 | 10:3c93db1cfb12 | 61 | return seq; |
uci1 | 10:3c93db1cfb12 | 62 | } |
uci1 | 10:3c93db1cfb12 | 63 | |
uci1 | 0:664899e0b988 | 64 | uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr, |
uci1 | 0:664899e0b988 | 65 | const uint32_t run) { |
uci1 | 0:664899e0b988 | 66 | // count the files having expected filename format |
uci1 | 0:664899e0b988 | 67 | |
uci1 | 0:664899e0b988 | 68 | const char* fn = GetOutFileName(macadr, run, 0) |
uci1 | 0:664899e0b988 | 69 | + strlen(kSDsubDir) + 1; // take out dir and '/'s |
uci1 | 0:664899e0b988 | 70 | |
uci1 | 0:664899e0b988 | 71 | DIR* d; |
uci1 | 0:664899e0b988 | 72 | struct dirent* dent; |
uci1 | 0:664899e0b988 | 73 | |
uci1 | 6:6f002d202f59 | 74 | uint16_t seq=0, newseq=0; |
uci1 | 0:664899e0b988 | 75 | if ( (d = opendir( kSDsubDir ))!=NULL ) { |
uci1 | 0:664899e0b988 | 76 | // don't compare seq#. don't use num of chars in case seq is >999 |
uci1 | 0:664899e0b988 | 77 | const uint32_t ncomp = strrchr(fn, 's') - fn; |
uci1 | 0:664899e0b988 | 78 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 0:664899e0b988 | 79 | if (strncmp(dent->d_name, fn, ncomp)==0) { |
uci1 | 6:6f002d202f59 | 80 | // allow for deleted files to make gaps. |
uci1 | 6:6f002d202f59 | 81 | // search for highest seq number and increase that |
uci1 | 7:079617408fec | 82 | sscanf((dent->d_name)+ncomp,"s%hu.dat",&seq); |
uci1 | 12:d472f9811262 | 83 | #ifdef DEBUG |
uci1 | 7:079617408fec | 84 | /* |
uci1 | 7:079617408fec | 85 | printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n", |
uci1 | 7:079617408fec | 86 | dent->d_name, seq, __kMaxUShort); |
uci1 | 7:079617408fec | 87 | */ |
uci1 | 12:d472f9811262 | 88 | #endif |
uci1 | 4:a91682e19d6b | 89 | if (seq==__kMaxUShort) { |
uci1 | 6:6f002d202f59 | 90 | newseq = seq; |
uci1 | 6:6f002d202f59 | 91 | break; |
uci1 | 6:6f002d202f59 | 92 | } |
uci1 | 6:6f002d202f59 | 93 | if (seq>=newseq) { |
uci1 | 6:6f002d202f59 | 94 | newseq=seq+1; |
uci1 | 6:6f002d202f59 | 95 | } |
uci1 | 6:6f002d202f59 | 96 | if (newseq==__kMaxUShort) { |
uci1 | 4:a91682e19d6b | 97 | break; |
uci1 | 4:a91682e19d6b | 98 | } |
uci1 | 0:664899e0b988 | 99 | } |
uci1 | 0:664899e0b988 | 100 | } |
uci1 | 0:664899e0b988 | 101 | closedir(d); |
uci1 | 0:664899e0b988 | 102 | } |
uci1 | 7:079617408fec | 103 | |
uci1 | 6:6f002d202f59 | 104 | return newseq; |
uci1 | 0:664899e0b988 | 105 | } |
uci1 | 0:664899e0b988 | 106 | |
uci1 | 3:24c5f0f50bf1 | 107 | FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent) { |
uci1 | 0:664899e0b988 | 108 | FILE* f = 0; |
uci1 | 0:664899e0b988 | 109 | if (name!=NULL) { |
uci1 | 3:24c5f0f50bf1 | 110 | f = OpenSDFile(name, "rb"); |
uci1 | 12:d472f9811262 | 111 | /* |
uci1 | 3:24c5f0f50bf1 | 112 | if (setcurrent) { |
uci1 | 3:24c5f0f50bf1 | 113 | fgCurFile = f; |
uci1 | 12:d472f9811262 | 114 | strncpy(fgCurFileName, name, kFNBufSize-1); |
uci1 | 12:d472f9811262 | 115 | fgCurSeq = GetSeqNumFromFileName(fgCurFileName); |
uci1 | 3:24c5f0f50bf1 | 116 | } |
uci1 | 12:d472f9811262 | 117 | */ |
uci1 | 0:664899e0b988 | 118 | } |
uci1 | 0:664899e0b988 | 119 | return f; |
uci1 | 0:664899e0b988 | 120 | } |
uci1 | 0:664899e0b988 | 121 | |
uci1 | 3:24c5f0f50bf1 | 122 | FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode) { |
uci1 | 12:d472f9811262 | 123 | // TODO: check if we have memory? |
uci1 | 12:d472f9811262 | 124 | std::string fn(name); |
uci1 | 12:d472f9811262 | 125 | if (strncmp(name, kSDsubDir, strlen(kSDsubDir))!=0) { |
uci1 | 12:d472f9811262 | 126 | // filename lacks directory |
uci1 | 12:d472f9811262 | 127 | fn = kSDsubDir; |
uci1 | 12:d472f9811262 | 128 | fn += "/"; |
uci1 | 12:d472f9811262 | 129 | fn += name; |
uci1 | 12:d472f9811262 | 130 | } |
uci1 | 12:d472f9811262 | 131 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 132 | printf("OpenSDFile: %s, mode %s\r\n",fn.c_str(),mode); |
uci1 | 12:d472f9811262 | 133 | #endif |
uci1 | 12:d472f9811262 | 134 | FILE* f = fopen(fn.c_str(), mode); |
uci1 | 1:e392595b4b76 | 135 | //setvbuf(f, 0, _IONBF, 0); // no buffering |
uci1 | 19:74155d652c37 | 136 | #ifdef DEBUG |
uci1 | 19:74155d652c37 | 137 | printf("OpenSDFile: f=%p\r\n",(void*)f); |
uci1 | 19:74155d652c37 | 138 | #endif |
uci1 | 1:e392595b4b76 | 139 | return f; |
uci1 | 1:e392595b4b76 | 140 | } |
uci1 | 1:e392595b4b76 | 141 | |
uci1 | 0:664899e0b988 | 142 | FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr, |
uci1 | 8:95a325df1f6b | 143 | const uint32_t run) { |
uci1 | 0:664899e0b988 | 144 | // opens a new file in the specified directory and writes this |
uci1 | 0:664899e0b988 | 145 | // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes) |
uci1 | 0:664899e0b988 | 146 | // |
uci1 | 12:d472f9811262 | 147 | fgCurSeq = GetSeqNum(macadr, run); |
uci1 | 0:664899e0b988 | 148 | memset(fgCurFileName, 0, sizeof(char)*kFNBufSize); |
uci1 | 12:d472f9811262 | 149 | strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1); |
uci1 | 12:d472f9811262 | 150 | //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq); |
uci1 | 1:e392595b4b76 | 151 | fgCurFile = 0; |
uci1 | 0:664899e0b988 | 152 | if (fgCurFileName!=NULL) { |
uci1 | 3:24c5f0f50bf1 | 153 | fgCurFile = OpenSDFile(fgCurFileName, "wb"); |
uci1 | 2:e67f7c158087 | 154 | if (fgCurFile!=NULL && ferror(fgCurFile)==0) { |
uci1 | 19:74155d652c37 | 155 | #ifdef DEBUG |
uci1 | 19:74155d652c37 | 156 | printf("Writing file header\r\n"); |
uci1 | 19:74155d652c37 | 157 | #endif |
uci1 | 12:d472f9811262 | 158 | WriteFileHeader(fgCurFile, macadr, run, fgCurSeq); |
uci1 | 2:e67f7c158087 | 159 | } |
uci1 | 0:664899e0b988 | 160 | } |
uci1 | 19:74155d652c37 | 161 | #ifdef DEBUG |
uci1 | 19:74155d652c37 | 162 | printf("fgCurFile=%p\r\n",(void*)fgCurFile); |
uci1 | 19:74155d652c37 | 163 | #endif |
uci1 | 1:e392595b4b76 | 164 | return fgCurFile; |
uci1 | 0:664899e0b988 | 165 | } |
uci1 | 0:664899e0b988 | 166 | |
uci1 | 0:664899e0b988 | 167 | bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf, |
uci1 | 0:664899e0b988 | 168 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 169 | const SnConfigFrame& conf) { |
uci1 | 0:664899e0b988 | 170 | // write event to SD card |
uci1 | 0:664899e0b988 | 171 | |
uci1 | 0:664899e0b988 | 172 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 0:664899e0b988 | 173 | uint16_t sWvBase=0; |
uci1 | 0:664899e0b988 | 174 | conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 8:95a325df1f6b | 175 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode, |
uci1 | 8:95a325df1f6b | 176 | evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB)); |
uci1 | 0:664899e0b988 | 177 | const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 0:664899e0b988 | 178 | fflush(efile); |
uci1 | 0:664899e0b988 | 179 | return ret; |
uci1 | 0:664899e0b988 | 180 | } |
uci1 | 0:664899e0b988 | 181 | |
uci1 | 0:664899e0b988 | 182 | bool SnSDUtils::WriteConfig(FILE* efile, |
uci1 | 0:664899e0b988 | 183 | const SnConfigFrame& conf) { |
uci1 | 8:95a325df1f6b | 184 | SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode, |
uci1 | 8:95a325df1f6b | 185 | conf.SizeOf(SnConfigFrame::kIOVers)); |
uci1 | 0:664899e0b988 | 186 | conf.WriteTo(efile); |
uci1 | 0:664899e0b988 | 187 | return true; |
uci1 | 0:664899e0b988 | 188 | } |
uci1 | 0:664899e0b988 | 189 | |
uci1 | 0:664899e0b988 | 190 | void SnSDUtils::DeleteFile(FILE*& f, const char* fname) { |
uci1 | 0:664899e0b988 | 191 | fclose(f); |
uci1 | 0:664899e0b988 | 192 | f=0; |
uci1 | 0:664899e0b988 | 193 | remove(fname); |
uci1 | 0:664899e0b988 | 194 | } |
uci1 | 0:664899e0b988 | 195 | |
uci1 | 0:664899e0b988 | 196 | SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm, |
uci1 | 3:24c5f0f50bf1 | 197 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 198 | char* const buf, |
uci1 | 6:6f002d202f59 | 199 | const uint32_t bsize, |
uci1 | 6:6f002d202f59 | 200 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 201 | SnEventFrame& evt, |
uci1 | 12:d472f9811262 | 202 | SnPowerFrame& pow, |
uci1 | 12:d472f9811262 | 203 | const uint32_t handshakeTimeout) { |
uci1 | 16:744ce85aede2 | 204 | |
uci1 | 0:664899e0b988 | 205 | DIR* d; |
uci1 | 0:664899e0b988 | 206 | struct dirent* dent; |
uci1 | 0:664899e0b988 | 207 | |
uci1 | 16:744ce85aede2 | 208 | SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent; |
uci1 | 0:664899e0b988 | 209 | |
uci1 | 12:d472f9811262 | 210 | const bool doDelete = curConf.IsDeletingFiles(); |
uci1 | 12:d472f9811262 | 211 | |
uci1 | 0:664899e0b988 | 212 | if ( (d = opendir( kSDsubDir ))!=NULL ) { |
uci1 | 0:664899e0b988 | 213 | FILE* f; |
uci1 | 0:664899e0b988 | 214 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 0:664899e0b988 | 215 | if (strncmp(dent->d_name, "SnEvts", 6)==0) { |
uci1 | 1:e392595b4b76 | 216 | const bool isCurFile = |
uci1 | 1:e392595b4b76 | 217 | (strcmp(dent->d_name, GetCurFileName())==0); |
uci1 | 1:e392595b4b76 | 218 | if (isCurFile) { |
uci1 | 3:24c5f0f50bf1 | 219 | // file must already be written out! |
uci1 | 1:e392595b4b76 | 220 | f = GetCurFile(); |
uci1 | 1:e392595b4b76 | 221 | } else { |
uci1 | 3:24c5f0f50bf1 | 222 | f = OpenExistingFile(dent->d_name, false); |
uci1 | 1:e392595b4b76 | 223 | } |
uci1 | 12:d472f9811262 | 224 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 225 | printf("calling senddata: f=%p (cur %p), fn=%s\r\n", |
uci1 | 12:d472f9811262 | 226 | f, GetCurFile(), dent->d_name); |
uci1 | 12:d472f9811262 | 227 | #endif |
uci1 | 16:744ce85aede2 | 228 | const SnCommWin::ECommWinResult res = |
uci1 | 16:744ce85aede2 | 229 | comm->SendData(f, dent->d_name, |
uci1 | 16:744ce85aede2 | 230 | curConf, evt, pow, buf, bsize, |
uci1 | 16:744ce85aede2 | 231 | 0, timeout, handshakeTimeout); |
uci1 | 16:744ce85aede2 | 232 | if (res<rs) { |
uci1 | 16:744ce85aede2 | 233 | rs = res; |
uci1 | 16:744ce85aede2 | 234 | } |
uci1 | 16:744ce85aede2 | 235 | // don't stop if res is bad. don't want one bad file to |
uci1 | 16:744ce85aede2 | 236 | // prevent sending the others |
uci1 | 16:744ce85aede2 | 237 | |
uci1 | 12:d472f9811262 | 238 | // send data should close or delete the file (if appropriate) |
uci1 | 12:d472f9811262 | 239 | // unless it's the current file |
uci1 | 3:24c5f0f50bf1 | 240 | if (isCurFile) { |
uci1 | 3:24c5f0f50bf1 | 241 | // move (back) to the end of the file |
uci1 | 3:24c5f0f50bf1 | 242 | // altho hopefully no writing will happen after this |
uci1 | 3:24c5f0f50bf1 | 243 | fseek(fgCurFile, 0, SEEK_END); |
uci1 | 0:664899e0b988 | 244 | } |
uci1 | 0:664899e0b988 | 245 | } |
uci1 | 0:664899e0b988 | 246 | } |
uci1 | 0:664899e0b988 | 247 | closedir(d); |
uci1 | 0:664899e0b988 | 248 | } |
uci1 | 0:664899e0b988 | 249 | |
uci1 | 0:664899e0b988 | 250 | return rs; |
uci1 | 0:664899e0b988 | 251 | } |
uci1 | 0:664899e0b988 | 252 |