Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Aug 08 23:27:37 2012 +0000
Revision:
8:95a325df1f6b
Parent:
6:6f002d202f59
Child:
10:3c93db1cfb12
Check power for low pow running. Average power over 500 readings. Use RTOS timers instead of tickers. Allow data taking of events, power readings or both, even if cards are off. Bug in EthernetInterface prevents IPs being reset. Lots of debug output.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 2:e67f7c158087 1 #include "SnCommWin.h"
uci1 2:e67f7c158087 2
uci1 6:6f002d202f59 3 #include "SnHeaderFrame.h"
uci1 2:e67f7c158087 4 #include "SnConfigFrame.h"
uci1 8:95a325df1f6b 5 #include "SnEventFrame.h"
uci1 8:95a325df1f6b 6 #include "SnPowerFrame.h"
uci1 8:95a325df1f6b 7 #include "SnStatusFrame.h"
uci1 2:e67f7c158087 8 #include "SnSDUtils.h"
uci1 2:e67f7c158087 9
uci1 2:e67f7c158087 10 SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf,
uci1 2:e67f7c158087 11 SnEventFrame& evt,
uci1 8:95a325df1f6b 12 SnPowerFrame& pow,
uci1 2:e67f7c158087 13 char* const genBuf,
uci1 3:24c5f0f50bf1 14 const uint32_t bsize,
uci1 2:e67f7c158087 15 const uint32_t timeout) {
uci1 3:24c5f0f50bf1 16 printf("SnCommWin::SendData\r\n");
uci1 2:e67f7c158087 17 ECommWinResult res = kUndefFail;
uci1 6:6f002d202f59 18 const uint32_t to = (conf.IsObeyingTimeout()) ? timeout : kSecsPerYear;
uci1 2:e67f7c158087 19 if (conf.IsSendingAllFiles()) {
uci1 3:24c5f0f50bf1 20 printf("sending all files\r\n");
uci1 6:6f002d202f59 21 res = SnSDUtils::SendAllFiles(this, conf.IsDeletingFiles(), to,
uci1 8:95a325df1f6b 22 genBuf, bsize, conf, evt, pow);
uci1 2:e67f7c158087 23 } else {
uci1 2:e67f7c158087 24 if (conf.GetCommSendData()==0) {
uci1 3:24c5f0f50bf1 25 printf("no data to send\r\n");
uci1 2:e67f7c158087 26 res = kOkNoMsg;
uci1 2:e67f7c158087 27 } else {
uci1 2:e67f7c158087 28 const uint32_t nev = (conf.GetCommSendData()>0) ?
uci1 3:24c5f0f50bf1 29 conf.GetCommSendData() // send N events
uci1 3:24c5f0f50bf1 30 : 0u; // send all events in last file
uci1 2:e67f7c158087 31 res = SendData(SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(),
uci1 8:95a325df1f6b 32 conf, evt, pow, genBuf, bsize, nev, to);
uci1 3:24c5f0f50bf1 33 printf("after send data cur file, res=%d\r\n",(int)res);
uci1 2:e67f7c158087 34 }
uci1 2:e67f7c158087 35 }
uci1 2:e67f7c158087 36 return res;
uci1 2:e67f7c158087 37 }
uci1 2:e67f7c158087 38
uci1 2:e67f7c158087 39 SnCommWin::ECommWinResult SnCommWin::SendData(FILE* inf, const char* infn,
uci1 2:e67f7c158087 40 const SnConfigFrame& curConf,
uci1 2:e67f7c158087 41 SnEventFrame& evt,
uci1 8:95a325df1f6b 42 SnPowerFrame& pow,
uci1 2:e67f7c158087 43 char* const genBuf,
uci1 6:6f002d202f59 44 const uint32_t bsize,
uci1 2:e67f7c158087 45 const uint32_t nevts,
uci1 6:6f002d202f59 46 const uint32_t timeout_clock,
uci1 2:e67f7c158087 47 const uint32_t firstEvt) {
uci1 2:e67f7c158087 48 // nevts==0 ==> send all events
uci1 2:e67f7c158087 49
uci1 3:24c5f0f50bf1 50 printf("SnCommWin::SendData cur file\r\n");
uci1 2:e67f7c158087 51 ECommWinResult res = kUndefFail;
uci1 2:e67f7c158087 52 if (inf!=0) {
uci1 6:6f002d202f59 53 // send the file name
uci1 6:6f002d202f59 54 res = SendFilename(infn, genBuf, timeout_clock);
uci1 3:24c5f0f50bf1 55 if (res>kAllFails) {
uci1 6:6f002d202f59 56 if (genBuf!=0) {
uci1 6:6f002d202f59 57 // send the contents
uci1 8:95a325df1f6b 58 res = SendFileContents(inf, curConf, evt, pow, genBuf,
uci1 6:6f002d202f59 59 nevts, timeout_clock, firstEvt);
uci1 6:6f002d202f59 60 if (res>SnCommWin::kAllFails) {
uci1 6:6f002d202f59 61 // wait for handshake
uci1 6:6f002d202f59 62 uint8_t hndshk=0;
uci1 6:6f002d202f59 63 res = WaitHandshake(timeout_clock, genBuf, bsize, hndshk);
uci1 6:6f002d202f59 64 if ( (res==SnCommWin::kOkWithMsg) // got handshake
uci1 6:6f002d202f59 65 && (nevts==0) // sent whole file
uci1 6:6f002d202f59 66 && curConf.IsDeletingFiles() // want to delete
uci1 6:6f002d202f59 67 //&& (strcmp(infn, SnSDUtils::GetCurFileName()!=0) // not current file (TODO?)
uci1 6:6f002d202f59 68 && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received
uci1 6:6f002d202f59 69 // delete it
uci1 6:6f002d202f59 70 SnSDUtilsWhisperer::DeleteFile(inf, infn);
uci1 6:6f002d202f59 71 }
uci1 3:24c5f0f50bf1 72 }
uci1 2:e67f7c158087 73 }
uci1 2:e67f7c158087 74 }
uci1 2:e67f7c158087 75 }
uci1 2:e67f7c158087 76 return res;
uci1 2:e67f7c158087 77 }
uci1 8:95a325df1f6b 78
uci1 8:95a325df1f6b 79 SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const uint32_t timeout,
uci1 8:95a325df1f6b 80 char* const buf,
uci1 8:95a325df1f6b 81 const uint32_t bsize,
uci1 8:95a325df1f6b 82 uint8_t& hndShkCode) {
uci1 8:95a325df1f6b 83 printf("WaitHandshake, to=%u\r\n",timeout);
uci1 8:95a325df1f6b 84
uci1 8:95a325df1f6b 85 const int mlen = ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeout);
uci1 8:95a325df1f6b 86 if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) {
uci1 8:95a325df1f6b 87 uint32_t msgLen=0;
uci1 8:95a325df1f6b 88 const char* b = buf;
uci1 8:95a325df1f6b 89 SnHeaderFrame::ReadFrom(b, hndShkCode, msgLen);
uci1 8:95a325df1f6b 90 if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) {
uci1 8:95a325df1f6b 91 return SnCommWin::kOkWithMsg;
uci1 8:95a325df1f6b 92 } else {
uci1 8:95a325df1f6b 93 // TODO: somehow handle unexpected message?
uci1 8:95a325df1f6b 94 return SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 95 }
uci1 8:95a325df1f6b 96 }
uci1 8:95a325df1f6b 97 return SnCommWin::kOkNoMsg;
uci1 8:95a325df1f6b 98 }
uci1 8:95a325df1f6b 99
uci1 8:95a325df1f6b 100 SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf,
uci1 8:95a325df1f6b 101 const uint32_t timeOut,
uci1 8:95a325df1f6b 102 char* const confBuf,
uci1 8:95a325df1f6b 103 const uint32_t bsize) {
uci1 8:95a325df1f6b 104 // confBuf assumed to alread be of allocated size
uci1 8:95a325df1f6b 105
uci1 8:95a325df1f6b 106 printf("GetConfig, to=%u\r\n",timeOut);
uci1 8:95a325df1f6b 107
uci1 8:95a325df1f6b 108 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 109 if (bsize<SnHeaderFrame::kMaxSizeOf || bsize<SnConfigFrame::kMaxSizeOf) {
uci1 8:95a325df1f6b 110 res = SnCommWin::kUndefFail;
uci1 8:95a325df1f6b 111 } else {
uci1 8:95a325df1f6b 112 // get header
uci1 8:95a325df1f6b 113 const int hlen = ReceiveAll(confBuf, SnHeaderFrame::SizeOf(), timeOut);
uci1 8:95a325df1f6b 114 if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) {
uci1 8:95a325df1f6b 115 uint8_t mcode=0; uint32_t mlen=0;
uci1 8:95a325df1f6b 116 const char* b = confBuf;
uci1 8:95a325df1f6b 117 SnHeaderFrame::ReadFrom(b, mcode, mlen);
uci1 8:95a325df1f6b 118 printf("mcode=%02x, mlen=%u\r\n", mcode, mlen);
uci1 8:95a325df1f6b 119 if (mcode!=SnHeaderFrame::kConfigCode) {
uci1 8:95a325df1f6b 120 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 121 } else {
uci1 8:95a325df1f6b 122 // get config
uci1 8:95a325df1f6b 123 const int clen = ReceiveAll(confBuf, mlen, timeOut);
uci1 8:95a325df1f6b 124 if (clen>0 && static_cast<uint32_t>(clen)==mlen) {
uci1 8:95a325df1f6b 125 b = confBuf;
uci1 8:95a325df1f6b 126 conf.ReadFrom(b);
uci1 8:95a325df1f6b 127 res = SnCommWin::kOkWithMsg;
uci1 8:95a325df1f6b 128 } else {
uci1 8:95a325df1f6b 129 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 130 }
uci1 8:95a325df1f6b 131 }
uci1 8:95a325df1f6b 132 } else {
uci1 8:95a325df1f6b 133 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 134 }
uci1 8:95a325df1f6b 135 }
uci1 8:95a325df1f6b 136 return res;
uci1 8:95a325df1f6b 137 }
uci1 8:95a325df1f6b 138
uci1 8:95a325df1f6b 139 SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf,
uci1 8:95a325df1f6b 140 const SnEventFrame& evt,
uci1 8:95a325df1f6b 141 const SnPowerFrame& pow,
uci1 8:95a325df1f6b 142 char* const genBuf,
uci1 8:95a325df1f6b 143 const uint32_t timeout_clock) {
uci1 8:95a325df1f6b 144 // TODO: check if connected?
uci1 8:95a325df1f6b 145 const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf);
uci1 8:95a325df1f6b 146 char* b = genBuf;
uci1 8:95a325df1f6b 147 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize);
uci1 8:95a325df1f6b 148 SnStatusFrame::WriteTo(b, GetCommType(), conf, evt, genBuf);
uci1 8:95a325df1f6b 149 #ifdef DEBUG
uci1 8:95a325df1f6b 150 printf("status frame:\r\n");
uci1 8:95a325df1f6b 151 for (uint32_t i=0; i<msize; i++) {
uci1 8:95a325df1f6b 152 printf("%02X ",genBuf[i]);
uci1 8:95a325df1f6b 153 }
uci1 8:95a325df1f6b 154 printf("\r\n");
uci1 8:95a325df1f6b 155 #endif
uci1 8:95a325df1f6b 156 int msiz = b-genBuf;
uci1 8:95a325df1f6b 157 int mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 158 if (mlen==msiz) {
uci1 8:95a325df1f6b 159 printf("status sent\r\n");
uci1 8:95a325df1f6b 160 b = genBuf;
uci1 8:95a325df1f6b 161 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 162 pow.SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 163 //SnPowerFrame::WriteTo(b, pow.GetAveV1(), pow.GetAveV2(), pow.GetTime());
uci1 8:95a325df1f6b 164 pow.WriteTo(b);
uci1 8:95a325df1f6b 165 msiz = b-genBuf;
uci1 8:95a325df1f6b 166 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 167 if (mlen==msiz) {
uci1 8:95a325df1f6b 168 return SnCommWin::kOkMsgSent;
uci1 8:95a325df1f6b 169 }
uci1 8:95a325df1f6b 170 }
uci1 8:95a325df1f6b 171 return SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 172 }
uci1 8:95a325df1f6b 173
uci1 8:95a325df1f6b 174 SnCommWin::ECommWinResult SnCommWin::SendFilename(const char* fn,
uci1 8:95a325df1f6b 175 char* const genBuf,
uci1 8:95a325df1f6b 176 const uint32_t timeout_clock) {
uci1 8:95a325df1f6b 177 printf("afar send filename %s\r\n",fn);
uci1 8:95a325df1f6b 178 const size_t flen = strlen(fn);
uci1 8:95a325df1f6b 179 char* b = genBuf;
uci1 8:95a325df1f6b 180 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen);
uci1 8:95a325df1f6b 181 sprintf(b, "%s", fn);
uci1 8:95a325df1f6b 182 const int msiz = SnHeaderFrame::SizeOf()+flen;
uci1 8:95a325df1f6b 183 const int mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 184 return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 185 }
uci1 8:95a325df1f6b 186
uci1 8:95a325df1f6b 187 SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf,
uci1 8:95a325df1f6b 188 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 189 SnEventFrame& evt,
uci1 8:95a325df1f6b 190 SnPowerFrame& pow,
uci1 8:95a325df1f6b 191 char* const genBuf,
uci1 8:95a325df1f6b 192 uint32_t nevts,
uci1 8:95a325df1f6b 193 const uint32_t timeout_clock,
uci1 8:95a325df1f6b 194 const uint32_t firstEvt) {
uci1 8:95a325df1f6b 195 printf("comm win send conf and events\r\n");
uci1 8:95a325df1f6b 196 // firstEvt==0 ==> start at beginning
uci1 8:95a325df1f6b 197 // nevts==0 ==> all events
uci1 8:95a325df1f6b 198
uci1 8:95a325df1f6b 199 const int fpos = ftell(inf);
uci1 8:95a325df1f6b 200 if (fpos>0) {
uci1 8:95a325df1f6b 201 fseek(inf, 0, SEEK_SET);
uci1 8:95a325df1f6b 202 }
uci1 8:95a325df1f6b 203
uci1 8:95a325df1f6b 204 printf("fpos=%d\r\n",fpos);
uci1 8:95a325df1f6b 205
uci1 8:95a325df1f6b 206 // get file header
uci1 8:95a325df1f6b 207 uint64_t macadr;
uci1 8:95a325df1f6b 208 uint32_t run;
uci1 8:95a325df1f6b 209 uint16_t seq;
uci1 8:95a325df1f6b 210 SnSDUtils::ReadFileHeader(inf, macadr, run, seq, &pow);
uci1 8:95a325df1f6b 211
uci1 8:95a325df1f6b 212 // check block
uci1 8:95a325df1f6b 213 uint8_t hcode;
uci1 8:95a325df1f6b 214 uint32_t hlen;
uci1 8:95a325df1f6b 215 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 8:95a325df1f6b 216 if (hcode==SnHeaderFrame::kPowerCode) {
uci1 8:95a325df1f6b 217 pow.ReadFrom(inf);
uci1 8:95a325df1f6b 218 }
uci1 8:95a325df1f6b 219
uci1 8:95a325df1f6b 220 // TODO: check memory for temporary config/event frames?
uci1 8:95a325df1f6b 221 // get config
uci1 8:95a325df1f6b 222 while ((hcode!=SnHeaderFrame::kConfigCode)
uci1 8:95a325df1f6b 223 && (feof(inf)==0)
uci1 8:95a325df1f6b 224 && (ferror(inf)==0) ) {
uci1 8:95a325df1f6b 225 fseek(inf, hlen, SEEK_CUR); // skip this block
uci1 8:95a325df1f6b 226 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 8:95a325df1f6b 227 }
uci1 8:95a325df1f6b 228 SnConfigFrame conf;
uci1 8:95a325df1f6b 229 conf.ReadFrom(inf);
uci1 8:95a325df1f6b 230
uci1 8:95a325df1f6b 231 // get event size
uci1 8:95a325df1f6b 232 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 8:95a325df1f6b 233 uint16_t sWvBase=0;
uci1 8:95a325df1f6b 234 curConf.GetPackParsFor(GetCommType(), sLoseLSB, sLoseMSB, sWvBase);
uci1 8:95a325df1f6b 235
uci1 8:95a325df1f6b 236 // size of event in file
uci1 8:95a325df1f6b 237 const uint32_t esize = SnEventFrame::SizeOf(SnEventFrame::kIOVers,
uci1 8:95a325df1f6b 238 conf.GetWvLoseLSB(),
uci1 8:95a325df1f6b 239 conf.GetWvLoseMSB());
uci1 8:95a325df1f6b 240
uci1 8:95a325df1f6b 241 printf("esize=%u\r\n",esize);
uci1 8:95a325df1f6b 242
uci1 8:95a325df1f6b 243 // how many more bytes?
uci1 8:95a325df1f6b 244 const int fcur = ftell(inf);
uci1 8:95a325df1f6b 245 fseek(inf, 0, SEEK_END); // go to end
uci1 8:95a325df1f6b 246 const int fend = ftell(inf);
uci1 8:95a325df1f6b 247 fseek(inf, fcur, SEEK_SET); // go back
uci1 8:95a325df1f6b 248
uci1 8:95a325df1f6b 249 bool ok = false;
uci1 8:95a325df1f6b 250 char* b = genBuf;
uci1 8:95a325df1f6b 251 // send the file header
uci1 8:95a325df1f6b 252 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileHeadrCode,
uci1 8:95a325df1f6b 253 SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers));
uci1 8:95a325df1f6b 254 SnSDUtils::WriteFileHeader(b, macadr, run, seq);
uci1 8:95a325df1f6b 255 int msiz = b-genBuf;
uci1 8:95a325df1f6b 256 int mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 257 if (mlen==msiz) {
uci1 8:95a325df1f6b 258 // send power header
uci1 8:95a325df1f6b 259 b = genBuf;
uci1 8:95a325df1f6b 260 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 261 pow.SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 262 pow.WriteTo(b);
uci1 8:95a325df1f6b 263 msiz = b-genBuf;
uci1 8:95a325df1f6b 264 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 265 if (mlen==msiz) {
uci1 8:95a325df1f6b 266 // send the config
uci1 8:95a325df1f6b 267 b = genBuf;
uci1 8:95a325df1f6b 268 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfigCode,
uci1 8:95a325df1f6b 269 conf.SizeOf(SnConfigFrame::kIOVers));
uci1 8:95a325df1f6b 270 conf.WriteTo(b);
uci1 8:95a325df1f6b 271 msiz = b-genBuf;
uci1 8:95a325df1f6b 272 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 273 if (mlen==msiz) {
uci1 8:95a325df1f6b 274 ok = true;
uci1 8:95a325df1f6b 275 }
uci1 8:95a325df1f6b 276 }
uci1 8:95a325df1f6b 277 }
uci1 8:95a325df1f6b 278
uci1 8:95a325df1f6b 279 printf("ok=%d, nevts=%u\r\n",(int)ok,nevts);
uci1 8:95a325df1f6b 280
uci1 8:95a325df1f6b 281 if (ok) {
uci1 8:95a325df1f6b 282 // size of event sent over afar
uci1 8:95a325df1f6b 283 const uint32_t ssize = SnEventFrame::SizeOf(SnEventFrame::kIOVers,
uci1 8:95a325df1f6b 284 sLoseLSB, sLoseMSB);
uci1 8:95a325df1f6b 285
uci1 8:95a325df1f6b 286 // loop over blocks. send event & power frames
uci1 8:95a325df1f6b 287 // until EOF or nevets have been sent
uci1 8:95a325df1f6b 288 uint32_t evtsSent=0, powsSent=0;
uci1 8:95a325df1f6b 289 uint8_t hc; uint32_t hl;
uci1 8:95a325df1f6b 290 while (ok && (feof(inf)==0)
uci1 8:95a325df1f6b 291 && (ferror(inf)==0) ) {
uci1 8:95a325df1f6b 292 SnSDUtils::ReadBlockHeader(inf, hc, hl);
uci1 8:95a325df1f6b 293 if (hc==SnHeaderFrame::kEventCode) {
uci1 8:95a325df1f6b 294 if ((ftell(inf)+ssize)<=fend) {
uci1 8:95a325df1f6b 295 evt.ReadFrom(inf, genBuf,
uci1 8:95a325df1f6b 296 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
uci1 8:95a325df1f6b 297 conf.GetWvBaseline());
uci1 8:95a325df1f6b 298 if (evt.GetEvtNum()>=firstEvt) {
uci1 8:95a325df1f6b 299 ++evtsSent;
uci1 8:95a325df1f6b 300 printf("sending evt %u\r\n",evtsSent);
uci1 8:95a325df1f6b 301 // must be after evt.Read, since that uses the buffer
uci1 8:95a325df1f6b 302 b = genBuf;
uci1 8:95a325df1f6b 303 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize);
uci1 8:95a325df1f6b 304 evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase); // will repack if necessary
uci1 8:95a325df1f6b 305 msiz = b-genBuf; //SnHeaderFrame::SizeOf()+ssize;
uci1 8:95a325df1f6b 306 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 307 ok &= mlen==msiz;
uci1 8:95a325df1f6b 308 }
uci1 8:95a325df1f6b 309 } else {
uci1 8:95a325df1f6b 310 printf("event header, but not enough bytes in file\r\n");
uci1 8:95a325df1f6b 311 ok = false;
uci1 8:95a325df1f6b 312 }
uci1 8:95a325df1f6b 313 } else if (hc==SnHeaderFrame::kPowerCode) {
uci1 8:95a325df1f6b 314 if ((ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend) {
uci1 8:95a325df1f6b 315 ++powsSent;
uci1 8:95a325df1f6b 316 pow.ReadFrom(inf);
uci1 8:95a325df1f6b 317 printf("sending power frame %u, v1=%g, v2=%g, t=%u\r\n",
uci1 8:95a325df1f6b 318 powsSent, pow.GetAveV1(), pow.GetAveV2(), pow.GetTime());
uci1 8:95a325df1f6b 319 b = genBuf;
uci1 8:95a325df1f6b 320 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 8:95a325df1f6b 321 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 8:95a325df1f6b 322 pow.WriteTo(b);
uci1 8:95a325df1f6b 323 msiz = b-genBuf;
uci1 8:95a325df1f6b 324 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 325 ok &= mlen==msiz;
uci1 8:95a325df1f6b 326 } else {
uci1 8:95a325df1f6b 327 printf("power header, but not enough bytes in file\r\n");
uci1 8:95a325df1f6b 328 ok = false;
uci1 8:95a325df1f6b 329 }
uci1 8:95a325df1f6b 330 } else {
uci1 8:95a325df1f6b 331 printf("unhandled block. hc=%hhu, hl=%u\r\n",
uci1 8:95a325df1f6b 332 hc, hl);
uci1 8:95a325df1f6b 333 if ((ftell(inf)+hl)<=fend) {
uci1 8:95a325df1f6b 334 fseek(inf, hl, SEEK_CUR); // skip this block
uci1 8:95a325df1f6b 335 } else {
uci1 8:95a325df1f6b 336 printf("not enough bytes in file to skip block\r\n");
uci1 8:95a325df1f6b 337 ok = false;
uci1 8:95a325df1f6b 338 }
uci1 8:95a325df1f6b 339 }
uci1 8:95a325df1f6b 340 }
uci1 8:95a325df1f6b 341 // send number of events sent
uci1 8:95a325df1f6b 342 b = genBuf;
uci1 8:95a325df1f6b 343 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t));
uci1 8:95a325df1f6b 344 b = SnBitUtils::WriteTo(b, evtsSent);
uci1 8:95a325df1f6b 345 msiz = b - genBuf;
uci1 8:95a325df1f6b 346 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 347 ok &= msiz==mlen;
uci1 8:95a325df1f6b 348 // send number of power readings sent
uci1 8:95a325df1f6b 349 b = genBuf;
uci1 8:95a325df1f6b 350 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t));
uci1 8:95a325df1f6b 351 b = SnBitUtils::WriteTo(b, powsSent);
uci1 8:95a325df1f6b 352 msiz = b -genBuf;
uci1 8:95a325df1f6b 353 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 354 ok &= msiz==mlen;
uci1 8:95a325df1f6b 355 }
uci1 8:95a325df1f6b 356
uci1 8:95a325df1f6b 357 // put file position back
uci1 8:95a325df1f6b 358 fseek(inf, fpos, SEEK_SET);
uci1 8:95a325df1f6b 359
uci1 8:95a325df1f6b 360 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 361 }