Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Aug 03 00:04:34 2012 +0000
Revision:
5:9cea89700c66
Parent:
4:a91682e19d6b
Child:
6:6f002d202f59
Bug fix to power: set 0 for cards and amps if on. Still working on communications. Many debug printouts.

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 0:664899e0b988 6
uci1 0:664899e0b988 7 #include "SnConfigFrame.h"
uci1 0:664899e0b988 8 #include "SnEventFrame.h"
uci1 0:664899e0b988 9
uci1 0:664899e0b988 10
uci1 1:e392595b4b76 11 const char* const SnSDUtils::kSDsubDir = "/sd";
uci1 2:e67f7c158087 12 char SnSDUtils::fgCurFileName[kFNBufSize]={0};
uci1 1:e392595b4b76 13 FILE* SnSDUtils::fgCurFile = 0;
uci1 4:a91682e19d6b 14 const uint8_t SnSDUtils::kIOvers = 2;
uci1 5:9cea89700c66 15 const uint32_t SnSDUtils::kMaxSizeOfFileHdr =
uci1 5:9cea89700c66 16 sizeof(uint8_t)+sizeof(uint64_t)+sizeof(uint32_t)+sizeof(uint16_t)
uci1 5:9cea89700c66 17 +SnPowerFrame::kMaxSizeOf;
uci1 4:a91682e19d6b 18
uci1 4:a91682e19d6b 19 static const uint16_t __kMaxUShort = ~0;
uci1 0:664899e0b988 20
uci1 0:664899e0b988 21 const char* SnSDUtils::GetOutFileName(const uint64_t macadr,
uci1 0:664899e0b988 22 const uint32_t run,
uci1 0:664899e0b988 23 const uint16_t seq) {
uci1 0:664899e0b988 24 // returns the formatted file name, or NULL if the directory is too long
uci1 0:664899e0b988 25 // and the full name cannot fit in the buffer
uci1 0:664899e0b988 26 // NOTE: this fcn uses a static buffer, and so should not be called
uci1 0:664899e0b988 27 // multiple times in the same line (that includes the calling of functions
uci1 0:664899e0b988 28 // that call this function!)
uci1 0:664899e0b988 29 //
uci1 0:664899e0b988 30 // filename = SnEvtsM[6-byte hex mac adr]r[6-digit run num]s[5-digit seq num].dat
uci1 0:664899e0b988 31 // 35 chars 7 + 12 +1+ 5 +1+ 5 + 4
uci1 0:664899e0b988 32
uci1 0:664899e0b988 33 if (strlen(kSDsubDir)<(kFNBufSize-37)) {
uci1 0:664899e0b988 34 static char tbuf[kFNBufSize];
uci1 0:664899e0b988 35 memset(tbuf, 0, sizeof(char)*kFNBufSize);
uci1 0:664899e0b988 36 // if file name format changes, GetSeqNum must be changed too
uci1 0:664899e0b988 37 sprintf(tbuf, "%s/SnEvtsM%012llXr%05ds%05d.dat",
uci1 0:664899e0b988 38 kSDsubDir,
uci1 0:664899e0b988 39 macadr>>16, // 64 -> 48 bits
uci1 0:664899e0b988 40 run, seq);
uci1 0:664899e0b988 41 return tbuf;
uci1 0:664899e0b988 42 } else {
uci1 0:664899e0b988 43 return NULL;
uci1 0:664899e0b988 44 }
uci1 0:664899e0b988 45 }
uci1 0:664899e0b988 46
uci1 0:664899e0b988 47 uint16_t SnSDUtils::GetSeqNum(const uint64_t macadr,
uci1 0:664899e0b988 48 const uint32_t run) {
uci1 0:664899e0b988 49 // count the files having expected filename format
uci1 0:664899e0b988 50
uci1 0:664899e0b988 51 const char* fn = GetOutFileName(macadr, run, 0)
uci1 0:664899e0b988 52 + strlen(kSDsubDir) + 1; // take out dir and '/'s
uci1 0:664899e0b988 53
uci1 0:664899e0b988 54 DIR* d;
uci1 0:664899e0b988 55 struct dirent* dent;
uci1 0:664899e0b988 56
uci1 0:664899e0b988 57 uint16_t seq=0;
uci1 0:664899e0b988 58 if ( (d = opendir( kSDsubDir ))!=NULL ) {
uci1 0:664899e0b988 59 // don't compare seq#. don't use num of chars in case seq is >999
uci1 0:664899e0b988 60 const uint32_t ncomp = strrchr(fn, 's') - fn;
uci1 0:664899e0b988 61 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 62 if (strncmp(dent->d_name, fn, ncomp)==0) {
uci1 0:664899e0b988 63 seq++;
uci1 4:a91682e19d6b 64 if (seq==__kMaxUShort) {
uci1 4:a91682e19d6b 65 break;
uci1 4:a91682e19d6b 66 }
uci1 0:664899e0b988 67 }
uci1 0:664899e0b988 68 }
uci1 0:664899e0b988 69 closedir(d);
uci1 0:664899e0b988 70 }
uci1 0:664899e0b988 71
uci1 0:664899e0b988 72 return seq;
uci1 0:664899e0b988 73 }
uci1 0:664899e0b988 74
uci1 3:24c5f0f50bf1 75 FILE* SnSDUtils::OpenExistingFile(const char* name, const bool setcurrent) {
uci1 0:664899e0b988 76 FILE* f = 0;
uci1 0:664899e0b988 77 if (name!=NULL) {
uci1 3:24c5f0f50bf1 78 f = OpenSDFile(name, "rb");
uci1 3:24c5f0f50bf1 79 if (setcurrent) {
uci1 3:24c5f0f50bf1 80 fgCurFile = f;
uci1 3:24c5f0f50bf1 81 }
uci1 0:664899e0b988 82 }
uci1 0:664899e0b988 83 return f;
uci1 0:664899e0b988 84 }
uci1 0:664899e0b988 85
uci1 3:24c5f0f50bf1 86 FILE* SnSDUtils::OpenSDFile(const char* name, const char* mode) {
uci1 3:24c5f0f50bf1 87 FILE* f = fopen(name, mode);
uci1 1:e392595b4b76 88 //setvbuf(f, 0, _IONBF, 0); // no buffering
uci1 1:e392595b4b76 89 return f;
uci1 1:e392595b4b76 90 }
uci1 1:e392595b4b76 91
uci1 0:664899e0b988 92 FILE* SnSDUtils::OpenNewOutputFile(const uint64_t macadr,
uci1 4:a91682e19d6b 93 const uint32_t run,
uci1 4:a91682e19d6b 94 const uint16_t v1, const uint16_t v2) {
uci1 0:664899e0b988 95 // opens a new file in the specified directory and writes this
uci1 0:664899e0b988 96 // this mbed's mac address as the first sizeof(uint64_t) bytes (i.e. 4 bytes)
uci1 0:664899e0b988 97 //
uci1 0:664899e0b988 98 const uint16_t seq = GetSeqNum(macadr, run);
uci1 0:664899e0b988 99 memset(fgCurFileName, 0, sizeof(char)*kFNBufSize);
uci1 2:e67f7c158087 100 strncpy(fgCurFileName,GetOutFileName(macadr, run, seq),kFNBufSize-1);
uci1 0:664899e0b988 101 //fprintf(stderr,"cur file = %s (%hu)\n\r",fgCurFileName,seq);
uci1 1:e392595b4b76 102 fgCurFile = 0;
uci1 0:664899e0b988 103 if (fgCurFileName!=NULL) {
uci1 3:24c5f0f50bf1 104 fgCurFile = OpenSDFile(fgCurFileName, "wb");
uci1 2:e67f7c158087 105 if (fgCurFile!=NULL && ferror(fgCurFile)==0) {
uci1 4:a91682e19d6b 106 WriteFileHeader(fgCurFile, macadr, run, seq, v1, v2);
uci1 2:e67f7c158087 107 }
uci1 0:664899e0b988 108 }
uci1 1:e392595b4b76 109 return fgCurFile;
uci1 0:664899e0b988 110 }
uci1 0:664899e0b988 111
uci1 0:664899e0b988 112 bool SnSDUtils::WriteEventTo(FILE* efile, char* const evtBuf,
uci1 0:664899e0b988 113 const SnEventFrame& evt,
uci1 0:664899e0b988 114 const SnConfigFrame& conf) {
uci1 0:664899e0b988 115 // write event to SD card
uci1 0:664899e0b988 116
uci1 0:664899e0b988 117 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 0:664899e0b988 118 uint16_t sWvBase=0;
uci1 0:664899e0b988 119 conf.GetPackParsFor(SnConfigFrame::kSDcard, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 120 const bool ret = evt.WriteTo(efile, evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 0:664899e0b988 121 fflush(efile);
uci1 0:664899e0b988 122 return ret;
uci1 0:664899e0b988 123 }
uci1 0:664899e0b988 124
uci1 0:664899e0b988 125 bool SnSDUtils::WriteConfig(FILE* efile,
uci1 0:664899e0b988 126 const SnConfigFrame& conf) {
uci1 0:664899e0b988 127 conf.WriteTo(efile);
uci1 0:664899e0b988 128 return true;
uci1 0:664899e0b988 129 }
uci1 0:664899e0b988 130
uci1 0:664899e0b988 131 void SnSDUtils::DeleteFile(FILE*& f, const char* fname) {
uci1 0:664899e0b988 132 fclose(f);
uci1 0:664899e0b988 133 f=0;
uci1 0:664899e0b988 134 remove(fname);
uci1 0:664899e0b988 135 }
uci1 0:664899e0b988 136
uci1 0:664899e0b988 137 SnCommWin::ECommWinResult SnSDUtils::SendAllFiles(SnCommWin* comm,
uci1 3:24c5f0f50bf1 138 const bool doDelete,
uci1 3:24c5f0f50bf1 139 const uint32_t timeout,
uci1 3:24c5f0f50bf1 140 char* const buf,
uci1 3:24c5f0f50bf1 141 const uint32_t bsize) {
uci1 0:664899e0b988 142
uci1 0:664899e0b988 143 DIR* d;
uci1 0:664899e0b988 144 struct dirent* dent;
uci1 0:664899e0b988 145
uci1 0:664899e0b988 146 SnCommWin::ECommWinResult rs = SnCommWin::kUndefFail;
uci1 0:664899e0b988 147
uci1 0:664899e0b988 148 if ( (d = opendir( kSDsubDir ))!=NULL ) {
uci1 0:664899e0b988 149 FILE* f;
uci1 0:664899e0b988 150 while ( (dent = readdir(d))!=NULL ) {
uci1 0:664899e0b988 151 if (strncmp(dent->d_name, "SnEvts", 6)==0) {
uci1 1:e392595b4b76 152 const bool isCurFile =
uci1 1:e392595b4b76 153 (strcmp(dent->d_name, GetCurFileName())==0);
uci1 1:e392595b4b76 154 if (isCurFile) {
uci1 3:24c5f0f50bf1 155 // file must already be written out!
uci1 1:e392595b4b76 156 f = GetCurFile();
uci1 1:e392595b4b76 157 } else {
uci1 3:24c5f0f50bf1 158 f = OpenExistingFile(dent->d_name, false);
uci1 1:e392595b4b76 159 }
uci1 3:24c5f0f50bf1 160 // send the filename
uci1 3:24c5f0f50bf1 161 rs = comm->SendFilename(dent->d_name, buf);
uci1 0:664899e0b988 162 if (rs<SnCommWin::kAllFails) {
uci1 3:24c5f0f50bf1 163 // send the data from the file
uci1 3:24c5f0f50bf1 164 rs = comm->SendData(f);
uci1 3:24c5f0f50bf1 165 if (rs<SnCommWin::kAllFails) {
uci1 3:24c5f0f50bf1 166 break;
uci1 3:24c5f0f50bf1 167 } else {
uci1 3:24c5f0f50bf1 168 // wait for handshake before next file
uci1 3:24c5f0f50bf1 169 rs = comm->WaitHandshake(timeout, buf, bsize);
uci1 3:24c5f0f50bf1 170 if ((rs==SnCommWin::kOkWithMsg)
uci1 3:24c5f0f50bf1 171 && doDelete && (isCurFile==false)) {
uci1 3:24c5f0f50bf1 172 DeleteFile(f, dent->d_name);
uci1 3:24c5f0f50bf1 173 }
uci1 3:24c5f0f50bf1 174 }
uci1 3:24c5f0f50bf1 175 }
uci1 3:24c5f0f50bf1 176 if (isCurFile) {
uci1 3:24c5f0f50bf1 177 // move (back) to the end of the file
uci1 3:24c5f0f50bf1 178 // altho hopefully no writing will happen after this
uci1 3:24c5f0f50bf1 179 fseek(fgCurFile, 0, SEEK_END);
uci1 0:664899e0b988 180 }
uci1 0:664899e0b988 181 }
uci1 0:664899e0b988 182 }
uci1 0:664899e0b988 183 closedir(d);
uci1 0:664899e0b988 184 }
uci1 0:664899e0b988 185
uci1 0:664899e0b988 186 return rs;
uci1 0:664899e0b988 187 }
uci1 0:664899e0b988 188