Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Oct 03 00:22:27 2012 +0000
Revision:
20:e5857b287b3b
Parent:
19:74155d652c37
Child:
21:ce51bb0ba4a5
Bug fix in AfarNetIf - set fEthConnected to false upon CloseConn. In SnSDUtils::GetSeqNum, try to make the data directory on the sd card if it doesn't exist already

Who changed what in which revision?

UserRevisionLine numberNew 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 20:e5857b287b3b 71 DIR* d( opendir(kSDsubDir) );
uci1 20:e5857b287b3b 72 if (d==NULL) {
uci1 20:e5857b287b3b 73 // try making the directory
uci1 20:e5857b287b3b 74 mkdir(kSDsubDir, 0777);
uci1 20:e5857b287b3b 75 d = opendir( kSDsubDir );
uci1 20:e5857b287b3b 76 }
uci1 0:664899e0b988 77 struct dirent* dent;
uci1 0:664899e0b988 78
uci1 6:6f002d202f59 79 uint16_t seq=0, newseq=0;
uci1 20:e5857b287b3b 80 if ( d!=NULL ) {
uci1 0:664899e0b988 81 // don't compare seq#. don't use num of chars in case seq is >999
uci1 0:664899e0b988 82 const uint32_t ncomp = strrchr(fn, 's') - fn;
uci1 0:664899e0b988 83 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 84 if (strncmp(dent->d_name, fn, ncomp)==0) {
uci1 6:6f002d202f59 85 // allow for deleted files to make gaps.
uci1 6:6f002d202f59 86 // search for highest seq number and increase that
uci1 7:079617408fec 87 sscanf((dent->d_name)+ncomp,"s%hu.dat",&seq);
uci1 12:d472f9811262 88 #ifdef DEBUG
uci1 7:079617408fec 89 /*
uci1 7:079617408fec 90 printf("dn=%s, seq=%hu, __kMaxUShort=%hu\r\n",
uci1 7:079617408fec 91 dent->d_name, seq, __kMaxUShort);
uci1 7:079617408fec 92 */
uci1 12:d472f9811262 93 #endif
uci1 4:a91682e19d6b 94 if (seq==__kMaxUShort) {
uci1 6:6f002d202f59 95 newseq = seq;
uci1 6:6f002d202f59 96 break;
uci1 6:6f002d202f59 97 }
uci1 6:6f002d202f59 98 if (seq>=newseq) {
uci1 6:6f002d202f59 99 newseq=seq+1;
uci1 6:6f002d202f59 100 }
uci1 6:6f002d202f59 101 if (newseq==__kMaxUShort) {
uci1 4:a91682e19d6b 102 break;
uci1 4:a91682e19d6b 103 }
uci1 0:664899e0b988 104 }
uci1 0:664899e0b988 105 }
uci1 0:664899e0b988 106 closedir(d);
uci1 0:664899e0b988 107 }
uci1 7:079617408fec 108
uci1 6:6f002d202f59 109 return newseq;
uci1 0:664899e0b988 110 }
uci1 0:664899e0b988 111
uci1 3:24c5f0f50bf1 112 FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent) {
uci1 0:664899e0b988 113 FILE* f = 0;
uci1 0:664899e0b988 114 if (name!=NULL) {
uci1 3:24c5f0f50bf1 115 f = OpenSDFile(name, "rb");
uci1 12:d472f9811262 116 /*
uci1 3:24c5f0f50bf1 117 if (setcurrent) {
uci1 3:24c5f0f50bf1 118 fgCurFile = f;
uci1 12:d472f9811262 119 strncpy(fgCurFileName, name, kFNBufSize-1);
uci1 12:d472f9811262 120 fgCurSeq = GetSeqNumFromFileName(fgCurFileName);
uci1 3:24c5f0f50bf1 121 }
uci1 12:d472f9811262 122 */
uci1 0:664899e0b988 123 }
uci1 0:664899e0b988 124 return f;
uci1 0:664899e0b988 125 }
uci1 0:664899e0b988 126
uci1 3:24c5f0f50bf1 127 FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode) {
uci1 12:d472f9811262 128 // TODO: check if we have memory?
uci1 12:d472f9811262 129 std::string fn(name);
uci1 12:d472f9811262 130 if (strncmp(name, kSDsubDir, strlen(kSDsubDir))!=0) {
uci1 12:d472f9811262 131 // filename lacks directory
uci1 12:d472f9811262 132 fn = kSDsubDir;
uci1 12:d472f9811262 133 fn += "/";
uci1 12:d472f9811262 134 fn += name;
uci1 12:d472f9811262 135 }
uci1 12:d472f9811262 136 #ifdef DEBUG
uci1 12:d472f9811262 137 printf("OpenSDFile: %s, mode %s\r\n",fn.c_str(),mode);
uci1 12:d472f9811262 138 #endif
uci1 12:d472f9811262 139 FILE* f = fopen(fn.c_str(), mode);
uci1 1:e392595b4b76 140 //setvbuf(f, 0, _IONBF, 0); // no buffering
uci1 19:74155d652c37 141 #ifdef DEBUG
uci1 19:74155d652c37 142 printf("OpenSDFile: f=%p\r\n",(void*)f);
uci1 19:74155d652c37 143 #endif
uci1 1:e392595b4b76 144 return f;
uci1 1:e392595b4b76 145 }
uci1 1:e392595b4b76 146
uci1 0:664899e0b988 147 FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr,
uci1 8:95a325df1f6b 148 const uint32_t run) {
uci1 0:664899e0b988 149 // opens a new file in the specified directory and writes this
uci1 0:664899e0b988 150 // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes)
uci1 0:664899e0b988 151 //
uci1 12:d472f9811262 152 fgCurSeq = GetSeqNum(macadr, run);
uci1 0:664899e0b988 153 memset(fgCurFileName, 0, sizeof(char)*kFNBufSize);
uci1 12:d472f9811262 154 strncpy(fgCurFileName,GetOutFileName(macadr, run, fgCurSeq),kFNBufSize-1);
uci1 12:d472f9811262 155 //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,fgCurSeq);
uci1 1:e392595b4b76 156 fgCurFile = 0;
uci1 0:664899e0b988 157 if (fgCurFileName!=NULL) {
uci1 3:24c5f0f50bf1 158 fgCurFile = OpenSDFile(fgCurFileName, "wb");
uci1 2:e67f7c158087 159 if (fgCurFile!=NULL && ferror(fgCurFile)==0) {
uci1 19:74155d652c37 160 #ifdef DEBUG
uci1 19:74155d652c37 161 printf("Writing file header\r\n");
uci1 19:74155d652c37 162 #endif
uci1 12:d472f9811262 163 WriteFileHeader(fgCurFile, macadr, run, fgCurSeq);
uci1 2:e67f7c158087 164 }
uci1 0:664899e0b988 165 }
uci1 19:74155d652c37 166 #ifdef DEBUG
uci1 19:74155d652c37 167 printf("fgCurFile=%p\r\n",(void*)fgCurFile);
uci1 19:74155d652c37 168 #endif
uci1 1:e392595b4b76 169 return fgCurFile;
uci1 0:664899e0b988 170 }
uci1 0:664899e0b988 171
uci1 0:664899e0b988 172 bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 173 const SnEventFrame& evt,
uci1 0:664899e0b988 174 const SnConfigFrame& conf) {
uci1 0:664899e0b988 175 // write event to SD card
uci1 0:664899e0b988 176
uci1 0:664899e0b988 177 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 0:664899e0b988 178 uint16_t sWvBase=0;
uci1 0:664899e0b988 179 conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
uci1 8:95a325df1f6b 180 SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kEventCode,
uci1 8:95a325df1f6b 181 evt.SizeOf(SnEventFrame::kIOVers, sLoseLSB, sLoseMSB));
uci1 0:664899e0b988 182 const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 183 fflush(efile);
uci1 0:664899e0b988 184 return ret;
uci1 0:664899e0b988 185 }
uci1 0:664899e0b988 186
uci1 0:664899e0b988 187 bool SnSDUtils::WriteConfig(FILE* efile,
uci1 0:664899e0b988 188 const SnConfigFrame& conf) {
uci1 8:95a325df1f6b 189 SnHeaderFrame::WriteTo(efile, SnHeaderFrame::kConfigCode,
uci1 8:95a325df1f6b 190 conf.SizeOf(SnConfigFrame::kIOVers));
uci1 0:664899e0b988 191 conf.WriteTo(efile);
uci1 0:664899e0b988 192 return true;
uci1 0:664899e0b988 193 }
uci1 0:664899e0b988 194
uci1 0:664899e0b988 195 void SnSDUtils::DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 196 fclose(f);
uci1 0:664899e0b988 197 f=0;
uci1 0:664899e0b988 198 remove(fname);
uci1 0:664899e0b988 199 }
uci1 0:664899e0b988 200
uci1 0:664899e0b988 201 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 202 const uint32_t timeout,
uci1 3:24c5f0f50bf1 203 char* const buf,
uci1 6:6f002d202f59 204 const uint32_t bsize,
uci1 6:6f002d202f59 205 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 206 SnEventFrame& evt,
uci1 12:d472f9811262 207 SnPowerFrame& pow,
uci1 12:d472f9811262 208 const uint32_t handshakeTimeout) {
uci1 16:744ce85aede2 209
uci1 0:664899e0b988 210 DIR* d;
uci1 0:664899e0b988 211 struct dirent* dent;
uci1 0:664899e0b988 212
uci1 16:744ce85aede2 213 SnCommWin::ECommWinResult rs = SnCommWin::kOkMsgSent;
uci1 0:664899e0b988 214
uci1 12:d472f9811262 215 const bool doDelete = curConf.IsDeletingFiles();
uci1 12:d472f9811262 216
uci1 0:664899e0b988 217 if ( (d = opendir( kSDsubDir ))!=NULL ) {
uci1 0:664899e0b988 218 FILE* f;
uci1 0:664899e0b988 219 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 220 if (strncmp(dent->d_name, "SnEvts", 6)==0) {
uci1 1:e392595b4b76 221 const bool isCurFile =
uci1 1:e392595b4b76 222 (strcmp(dent->d_name, GetCurFileName())==0);
uci1 1:e392595b4b76 223 if (isCurFile) {
uci1 3:24c5f0f50bf1 224 // file must already be written out!
uci1 1:e392595b4b76 225 f = GetCurFile();
uci1 1:e392595b4b76 226 } else {
uci1 3:24c5f0f50bf1 227 f = OpenExistingFile(dent->d_name, false);
uci1 1:e392595b4b76 228 }
uci1 12:d472f9811262 229 #ifdef DEBUG
uci1 12:d472f9811262 230 printf("calling senddata: f=%p (cur %p), fn=%s\r\n",
uci1 12:d472f9811262 231 f, GetCurFile(), dent->d_name);
uci1 12:d472f9811262 232 #endif
uci1 16:744ce85aede2 233 const SnCommWin::ECommWinResult res =
uci1 16:744ce85aede2 234 comm->SendData(f, dent->d_name,
uci1 16:744ce85aede2 235 curConf, evt, pow, buf, bsize,
uci1 16:744ce85aede2 236 0, timeout, handshakeTimeout);
uci1 16:744ce85aede2 237 if (res<rs) {
uci1 16:744ce85aede2 238 rs = res;
uci1 16:744ce85aede2 239 }
uci1 16:744ce85aede2 240 // don't stop if res is bad. don't want one bad file to
uci1 16:744ce85aede2 241 // prevent sending the others
uci1 16:744ce85aede2 242
uci1 12:d472f9811262 243 // send data should close or delete the file (if appropriate)
uci1 12:d472f9811262 244 // unless it's the current file
uci1 3:24c5f0f50bf1 245 if (isCurFile) {
uci1 3:24c5f0f50bf1 246 // move (back) to the end of the file
uci1 3:24c5f0f50bf1 247 // altho hopefully no writing will happen after this
uci1 3:24c5f0f50bf1 248 fseek(fgCurFile, 0, SEEK_END);
uci1 0:664899e0b988 249 }
uci1 0:664899e0b988 250 }
uci1 0:664899e0b988 251 }
uci1 0:664899e0b988 252 closedir(d);
uci1 0:664899e0b988 253 }
uci1 0:664899e0b988 254
uci1 0:664899e0b988 255 return rs;
uci1 0:664899e0b988 256 }
uci1 0:664899e0b988 257