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:
37:ff95e7070f26
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 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 16:744ce85aede2 9 #include "SnConstants.h"
uci1 21:ce51bb0ba4a5 10 #include "SnCRCUtils.h"
uci1 37:ff95e7070f26 11 #include "SnCommPeripheral.h"
uci1 21:ce51bb0ba4a5 12
uci1 21:ce51bb0ba4a5 13 #include <algorithm>
uci1 21:ce51bb0ba4a5 14 #include <ctype.h>
uci1 21:ce51bb0ba4a5 15 extern "C" void mbed_reset();
uci1 16:744ce85aede2 16
uci1 37:ff95e7070f26 17 //#define DEBUG
uci1 16:744ce85aede2 18
uci1 27:efc4d654b139 19 const char* SnCommWin::kLocalDir = "/local";
uci1 27:efc4d654b139 20 const char* SnCommWin::kDelAllConfCodeStr = "fj2io32FIJ(#jd;;.O@++/]ewavk2[49ojv";
uci1 27:efc4d654b139 21 const uint8_t SnCommWin::kDelAllConfCodeLen = 35; // length of the above string
uci1 25:57b2627fe756 22
uci1 37:ff95e7070f26 23 SnCommWin::SnCommWin(SnCommPeripheral* p) :
uci1 37:ff95e7070f26 24 fComm(p) {
uci1 37:ff95e7070f26 25 if (p==0) {
uci1 37:ff95e7070f26 26 error("SnCommWin - must have peripheral! Received 0.\r\n");
uci1 37:ff95e7070f26 27 }
uci1 37:ff95e7070f26 28 }
uci1 37:ff95e7070f26 29
uci1 37:ff95e7070f26 30 SnCommWin::~SnCommWin() {
uci1 37:ff95e7070f26 31 delete fComm;
uci1 37:ff95e7070f26 32 }
uci1 37:ff95e7070f26 33
uci1 16:744ce85aede2 34 uint32_t SnCommWin::GetConnectTimeout() const { return kConnectTimeout; }
uci1 16:744ce85aede2 35 uint32_t SnCommWin::GetListenTimeout() const { return kListenTimeout; }
uci1 2:e67f7c158087 36
uci1 37:ff95e7070f26 37 bool SnCommWin::TrySetSysTimeUnix(const uint32_t timeout) {
uci1 37:ff95e7070f26 38 return (fComm!=0) ? (fComm->TrySetSysTimeUnix(timeout)) : false;
uci1 37:ff95e7070f26 39 }
uci1 37:ff95e7070f26 40
uci1 37:ff95e7070f26 41 bool SnCommWin::Connect(const uint32_t timeout) {
uci1 37:ff95e7070f26 42 return (fComm!=0) ? (fComm->Connect(timeout)) : false;
uci1 37:ff95e7070f26 43 }
uci1 37:ff95e7070f26 44
uci1 37:ff95e7070f26 45 bool SnCommWin::CloseConn(const uint32_t timeout) {
uci1 37:ff95e7070f26 46 return (fComm!=0) ? (fComm->CloseConn(timeout)) : false;
uci1 12:d472f9811262 47 }
uci1 12:d472f9811262 48
uci1 2:e67f7c158087 49 SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf,
uci1 2:e67f7c158087 50 SnEventFrame& evt,
uci1 8:95a325df1f6b 51 SnPowerFrame& pow,
uci1 2:e67f7c158087 52 char* const genBuf,
uci1 3:24c5f0f50bf1 53 const uint32_t bsize,
uci1 12:d472f9811262 54 const uint32_t timeout,
uci1 12:d472f9811262 55 const uint32_t handshakeTimeout) {
uci1 12:d472f9811262 56 #ifdef DEBUG
uci1 3:24c5f0f50bf1 57 printf("SnCommWin::SendData\r\n");
uci1 12:d472f9811262 58 #endif
uci1 2:e67f7c158087 59 ECommWinResult res = kUndefFail;
uci1 15:f2569d8e4176 60 if ( (GetCommType() != SnConfigFrame::kIrid) ||
uci1 15:f2569d8e4176 61 (conf.IsForcingSBDdata()) ) {
uci1 15:f2569d8e4176 62 // only send large amounts if we're not communicating by Iridum,
uci1 15:f2569d8e4176 63 // or if we are being forced to send the data over SBD
uci1 15:f2569d8e4176 64 if (conf.IsSendingAllFiles()) {
uci1 12:d472f9811262 65 #ifdef DEBUG
uci1 15:f2569d8e4176 66 printf("sending all files\r\n");
uci1 12:d472f9811262 67 #endif
uci1 15:f2569d8e4176 68 res = SnSDUtils::SendAllFiles(this, timeout,
uci1 15:f2569d8e4176 69 genBuf, bsize, conf, evt, pow,
uci1 15:f2569d8e4176 70 handshakeTimeout);
uci1 15:f2569d8e4176 71 } else {
uci1 15:f2569d8e4176 72 if (conf.GetCommSendData()==0) {
uci1 12:d472f9811262 73 #ifdef DEBUG
uci1 15:f2569d8e4176 74 printf("no data to send\r\n");
uci1 12:d472f9811262 75 #endif
uci1 15:f2569d8e4176 76 res = kOkNoMsg;
uci1 15:f2569d8e4176 77 } else {
uci1 15:f2569d8e4176 78 const uint32_t nev = (conf.GetCommSendData()>0) ?
uci1 15:f2569d8e4176 79 conf.GetCommSendData() // send N events
uci1 15:f2569d8e4176 80 : 0u; // send all events in last file
uci1 12:d472f9811262 81 #ifdef DEBUG
uci1 16:744ce85aede2 82 printf("calling SendData. f=%s, nev=%u\r\n",SnSDUtils::GetCurFileName(), nev);
uci1 12:d472f9811262 83 #endif
uci1 15:f2569d8e4176 84 res = SendData(SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(),
uci1 15:f2569d8e4176 85 conf, evt, pow, genBuf, bsize, nev, timeout, handshakeTimeout);
uci1 12:d472f9811262 86 #ifdef DEBUG
uci1 15:f2569d8e4176 87 printf("after send data cur file, res=%d\r\n",(int)res);
uci1 12:d472f9811262 88 #endif
uci1 15:f2569d8e4176 89 }
uci1 2:e67f7c158087 90 }
uci1 15:f2569d8e4176 91 } else {
uci1 15:f2569d8e4176 92 return kOkNoMsg;
uci1 2:e67f7c158087 93 }
uci1 2:e67f7c158087 94 return res;
uci1 2:e67f7c158087 95 }
uci1 2:e67f7c158087 96
uci1 2:e67f7c158087 97 SnCommWin::ECommWinResult SnCommWin::SendData(FILE* inf, const char* infn,
uci1 2:e67f7c158087 98 const SnConfigFrame& curConf,
uci1 2:e67f7c158087 99 SnEventFrame& evt,
uci1 8:95a325df1f6b 100 SnPowerFrame& pow,
uci1 2:e67f7c158087 101 char* const genBuf,
uci1 6:6f002d202f59 102 const uint32_t bsize,
uci1 2:e67f7c158087 103 const uint32_t nevts,
uci1 6:6f002d202f59 104 const uint32_t timeout_clock,
uci1 25:57b2627fe756 105 const uint32_t handshakeTimeout,
uci1 25:57b2627fe756 106 uint8_t* hndres) {
uci1 2:e67f7c158087 107 // nevts==0 ==> send all events
uci1 21:ce51bb0ba4a5 108 // handshakeTimeout is never a clock time -- always a delta(t)
uci1 2:e67f7c158087 109
uci1 12:d472f9811262 110 #ifdef DEBUG
uci1 12:d472f9811262 111 printf("SnCommWin::SendData file (%p), fn=%s\r\n",inf,infn);
uci1 12:d472f9811262 112 #endif
uci1 2:e67f7c158087 113 ECommWinResult res = kUndefFail;
uci1 12:d472f9811262 114 bool didDel = false;
uci1 2:e67f7c158087 115 if (inf!=0) {
uci1 12:d472f9811262 116 #ifdef DEBUG
uci1 12:d472f9811262 117 printf("sending file name %s\r\n",infn);
uci1 12:d472f9811262 118 #endif
uci1 16:744ce85aede2 119
uci1 6:6f002d202f59 120 // send the file name
uci1 6:6f002d202f59 121 res = SendFilename(infn, genBuf, timeout_clock);
uci1 3:24c5f0f50bf1 122 if (res>kAllFails) {
uci1 6:6f002d202f59 123 if (genBuf!=0) {
uci1 12:d472f9811262 124 #ifdef DEBUG
uci1 12:d472f9811262 125 printf("calling send file contents\r\n");
uci1 12:d472f9811262 126 #endif
uci1 16:744ce85aede2 127
uci1 6:6f002d202f59 128 // send the contents
uci1 8:95a325df1f6b 129 res = SendFileContents(inf, curConf, evt, pow, genBuf,
uci1 12:d472f9811262 130 nevts, timeout_clock);
uci1 12:d472f9811262 131 #ifdef DEBUG
uci1 12:d472f9811262 132 printf("res=%d (fails=%d)\r\n",(int)res,(int)kAllFails);
uci1 12:d472f9811262 133 #endif
uci1 12:d472f9811262 134 if (res>kAllFails) {
uci1 12:d472f9811262 135 #ifdef DEBUG
uci1 12:d472f9811262 136 printf("calling wait handshake\r\n");
uci1 12:d472f9811262 137 #endif
uci1 6:6f002d202f59 138 // wait for handshake
uci1 27:efc4d654b139 139 uint8_t hndshk(0); uint32_t hndshkLen(0);
uci1 27:efc4d654b139 140 res = WaitHandshake(curConf, handshakeTimeout, genBuf, bsize, hndshk,
uci1 27:efc4d654b139 141 &hndshkLen);
uci1 25:57b2627fe756 142 if (hndres!=0) {
uci1 25:57b2627fe756 143 *hndres = hndshk;
uci1 25:57b2627fe756 144 }
uci1 26:b3180073c8ec 145 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 146 printf("res=%d, nevts=%u, isdel=%d, inf=%p, curfile=%p, hndshk=%02x\r\n",
uci1 21:ce51bb0ba4a5 147 res, nevts, curConf.IsDeletingFiles(), inf,
uci1 21:ce51bb0ba4a5 148 SnSDUtils::GetCurFile(), hndshk);
uci1 21:ce51bb0ba4a5 149 #endif
uci1 6:6f002d202f59 150 if ( (res==SnCommWin::kOkWithMsg) // got handshake
uci1 6:6f002d202f59 151 && (nevts==0) // sent whole file
uci1 27:efc4d654b139 152 && (inf!=SnSDUtils::GetCurFile()) ) { // not current file
uci1 27:efc4d654b139 153 if ( curConf.IsDeletingFiles() // want to delete
uci1 27:efc4d654b139 154 && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received
uci1 27:efc4d654b139 155
uci1 27:efc4d654b139 156 // delete it
uci1 21:ce51bb0ba4a5 157 #ifdef DEBUG
uci1 27:efc4d654b139 158 printf("deleting file %p, %s\r\n",inf,infn);
uci1 21:ce51bb0ba4a5 159 #endif
uci1 27:efc4d654b139 160 SnSDUtilsWhisperer::DeleteFile(inf, infn);
uci1 27:efc4d654b139 161 didDel=true;
uci1 27:efc4d654b139 162 } else if ( hndshk==SnHeaderFrame::kHnShOkStopCode ) {
uci1 27:efc4d654b139 163 res = SnCommWin::kOkStopComm;
uci1 27:efc4d654b139 164 } else if ( (hndshk==SnHeaderFrame::kHnShOkDelAlCode) ||
uci1 27:efc4d654b139 165 (hndshk==SnHeaderFrame::kHnShOkDelRnCode) ) {
uci1 27:efc4d654b139 166 if ( GetDeleteAllConfirmCode(curConf,
uci1 27:efc4d654b139 167 kDelAllConfCodeLen, handshakeTimeout,
uci1 27:efc4d654b139 168 genBuf, bsize) ) {
uci1 27:efc4d654b139 169 if (hndshk==SnHeaderFrame::kHnShOkDelAlCode) {
uci1 27:efc4d654b139 170 SnSDUtilsWhisperer::DeleteAllFiles();
uci1 27:efc4d654b139 171 } else if (hndshk==SnHeaderFrame::kHnShOkDelRnCode) {
uci1 27:efc4d654b139 172 SnSDUtilsWhisperer::DeleteFilesOfRun(hndshkLen);
uci1 27:efc4d654b139 173 }
uci1 27:efc4d654b139 174 res = SnCommWin::kOkStopComm;
uci1 27:efc4d654b139 175 }
uci1 27:efc4d654b139 176 }
uci1 6:6f002d202f59 177 }
uci1 3:24c5f0f50bf1 178 }
uci1 2:e67f7c158087 179 }
uci1 2:e67f7c158087 180 }
uci1 12:d472f9811262 181 } else {
uci1 12:d472f9811262 182 #ifdef DEBUG
uci1 12:d472f9811262 183 printf("inf=0!\r\n");
uci1 12:d472f9811262 184 #endif
uci1 12:d472f9811262 185 }
uci1 12:d472f9811262 186 if ( (inf!=SnSDUtils::GetCurFile()) && (didDel==false) ) {
uci1 12:d472f9811262 187 SnSDUtils::CloseOutputFile(inf);
uci1 2:e67f7c158087 188 }
uci1 37:ff95e7070f26 189 if ( fComm->IsTimedOut(timeout_clock) ) {
uci1 25:57b2627fe756 190 if ( kFailTimeout < res ) {
uci1 25:57b2627fe756 191 res = kFailTimeout;
uci1 25:57b2627fe756 192 }
uci1 25:57b2627fe756 193 }
uci1 2:e67f7c158087 194 return res;
uci1 2:e67f7c158087 195 }
uci1 8:95a325df1f6b 196
uci1 27:efc4d654b139 197 bool SnCommWin::GetDeleteAllConfirmCode(const SnConfigFrame& conf,
uci1 27:efc4d654b139 198 const uint32_t length,
uci1 27:efc4d654b139 199 const uint32_t timeout,
uci1 27:efc4d654b139 200 char* const buf,
uci1 27:efc4d654b139 201 const uint32_t bsize) {
uci1 27:efc4d654b139 202 bool ret = false;
uci1 27:efc4d654b139 203 // ensure we don't wait forever
uci1 27:efc4d654b139 204 const uint32_t to = ((timeout>0) && (timeout<kAbsMaxTimer))
uci1 27:efc4d654b139 205 ? conf.GetTimeoutTime(time(0), timeout)
uci1 27:efc4d654b139 206 : conf.GetTimeoutTime(time(0), kListenTimeout);
uci1 27:efc4d654b139 207 #ifdef DEBUG
uci1 27:efc4d654b139 208 printf("GetDeleteAllConfirmCode, to=%u\r\n",to);
uci1 27:efc4d654b139 209 #endif
uci1 27:efc4d654b139 210 // better pull all the bytes no matter the buffer size
uci1 27:efc4d654b139 211 const uint32_t ll = (bsize<length) ? bsize : length;
uci1 27:efc4d654b139 212 int mlen = 0;
uci1 27:efc4d654b139 213 while ( mlen<length ) {
uci1 37:ff95e7070f26 214 mlen += fComm->ReceiveAll(buf, ll, to);
uci1 37:ff95e7070f26 215 if (fComm->IsTimedOut(to)) {
uci1 27:efc4d654b139 216 break;
uci1 27:efc4d654b139 217 }
uci1 27:efc4d654b139 218 }
uci1 27:efc4d654b139 219 if (mlen==length) {
uci1 27:efc4d654b139 220 const char* b = buf;
uci1 27:efc4d654b139 221 const char* c = kDelAllConfCodeStr;
uci1 27:efc4d654b139 222 bool match = true;
uci1 27:efc4d654b139 223 for (uint8_t i=0; (i<kDelAllConfCodeLen) && match; ++i, ++b, ++c) {
uci1 27:efc4d654b139 224 match = ((*b)==(*c));
uci1 27:efc4d654b139 225 }
uci1 27:efc4d654b139 226 ret = match;
uci1 27:efc4d654b139 227 }
uci1 27:efc4d654b139 228 return ret;
uci1 27:efc4d654b139 229 }
uci1 27:efc4d654b139 230
uci1 21:ce51bb0ba4a5 231 SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const SnConfigFrame& conf,
uci1 21:ce51bb0ba4a5 232 const uint32_t timeout,
uci1 8:95a325df1f6b 233 char* const buf,
uci1 8:95a325df1f6b 234 const uint32_t bsize,
uci1 27:efc4d654b139 235 uint8_t& hndShkCode,
uci1 27:efc4d654b139 236 uint32_t* hndShkLen) {
uci1 12:d472f9811262 237 // ensure we don't wait forever
uci1 21:ce51bb0ba4a5 238 const uint32_t to = ((timeout>0) && (timeout<kAbsMaxTimer))
uci1 21:ce51bb0ba4a5 239 ? conf.GetTimeoutTime(time(0), timeout)
uci1 21:ce51bb0ba4a5 240 : conf.GetTimeoutTime(time(0), kListenTimeout);
uci1 12:d472f9811262 241 #ifdef DEBUG
uci1 12:d472f9811262 242 printf("WaitHandshake, to=%u\r\n",to);
uci1 12:d472f9811262 243 #endif
uci1 8:95a325df1f6b 244
uci1 37:ff95e7070f26 245 const int mlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), to);
uci1 8:95a325df1f6b 246 if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) {
uci1 8:95a325df1f6b 247 uint32_t msgLen=0;
uci1 8:95a325df1f6b 248 const char* b = buf;
uci1 8:95a325df1f6b 249 SnHeaderFrame::ReadFrom(b, hndShkCode, msgLen);
uci1 12:d472f9811262 250 #ifdef DEBUG
uci1 12:d472f9811262 251 printf("got handshake code=%02x, len=%u\r\n",hndShkCode,msgLen);
uci1 12:d472f9811262 252 #endif
uci1 8:95a325df1f6b 253 if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) {
uci1 27:efc4d654b139 254 if (hndShkLen!=0) {
uci1 27:efc4d654b139 255 *hndShkLen = msgLen;
uci1 27:efc4d654b139 256 }
uci1 8:95a325df1f6b 257 return SnCommWin::kOkWithMsg;
uci1 8:95a325df1f6b 258 } else {
uci1 8:95a325df1f6b 259 // TODO: somehow handle unexpected message?
uci1 8:95a325df1f6b 260 return SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 261 }
uci1 8:95a325df1f6b 262 }
uci1 8:95a325df1f6b 263 return SnCommWin::kOkNoMsg;
uci1 8:95a325df1f6b 264 }
uci1 8:95a325df1f6b 265
uci1 21:ce51bb0ba4a5 266 SnCommWin::ECommWinResult SnCommWin::GetFilename(const uint32_t timeout,
uci1 21:ce51bb0ba4a5 267 char* const buf,
uci1 21:ce51bb0ba4a5 268 const uint32_t namelen) {
uci1 21:ce51bb0ba4a5 269 // called by GetConfig upon receipt of a kMbedFilenameCode
uci1 21:ce51bb0ba4a5 270
uci1 21:ce51bb0ba4a5 271 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 272 printf("GetMbedFile, to=%u\r\n",timeout);
uci1 21:ce51bb0ba4a5 273 #endif
uci1 37:ff95e7070f26 274 const int mlen = fComm->ReceiveAll(buf, namelen, timeout);
uci1 21:ce51bb0ba4a5 275 if (mlen>0 && static_cast<uint32_t>(mlen) == namelen) {
uci1 21:ce51bb0ba4a5 276 return SnCommWin::kOkWithMsg;
uci1 21:ce51bb0ba4a5 277 }
uci1 21:ce51bb0ba4a5 278 return SnCommWin::kUnexpectedRec;
uci1 21:ce51bb0ba4a5 279 }
uci1 21:ce51bb0ba4a5 280
uci1 21:ce51bb0ba4a5 281 SnCommWin::ECommWinResult SnCommWin::GetLocalFile(std::string fname,
uci1 21:ce51bb0ba4a5 282 char* const buf,
uci1 21:ce51bb0ba4a5 283 const uint32_t bsize,
uci1 21:ce51bb0ba4a5 284 const uint32_t timeout) {
uci1 21:ce51bb0ba4a5 285 // get a file and save it locally with the specified name
uci1 21:ce51bb0ba4a5 286
uci1 21:ce51bb0ba4a5 287 // get the header for the file
uci1 21:ce51bb0ba4a5 288 uint8_t mcode=0; uint32_t mlen=0;
uci1 21:ce51bb0ba4a5 289 SnCommWin::ECommWinResult res =
uci1 21:ce51bb0ba4a5 290 GetHeader(timeout, buf, bsize, mcode, mlen);
uci1 21:ce51bb0ba4a5 291 if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileCode) ) {
uci1 21:ce51bb0ba4a5 292 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 293 printf("Got mbed file header. File length = %u\r\n",mlen);
uci1 21:ce51bb0ba4a5 294 #endif
uci1 21:ce51bb0ba4a5 295 // got the header.. make the file..
uci1 21:ce51bb0ba4a5 296 // make the file name ALLCAPS, since LocalFileSystem will do it anyway
uci1 37:ff95e7070f26 297 SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end());
uci1 21:ce51bb0ba4a5 298 // now ensure the file name is 8.3 only -- for LocalFileSystem
uci1 21:ce51bb0ba4a5 299 const size_t ldlen = strlen(kLocalDir);
uci1 21:ce51bb0ba4a5 300 // 12 = 8.3 filename format, 1 for / and 1 for \0
uci1 21:ce51bb0ba4a5 301 const uint32_t fbs = ldlen+1+12+1;
uci1 21:ce51bb0ba4a5 302 char fnb[fbs];
uci1 36:87865913ae6f 303 memset(fnb, 0, sizeof(char)*fbs);
uci1 21:ce51bb0ba4a5 304 char* fb = fnb;
uci1 21:ce51bb0ba4a5 305 memcpy(fb, kLocalDir, ldlen); fb+=ldlen; // /local
uci1 21:ce51bb0ba4a5 306 memset(fb, '/', 1); fb+=1; // /
uci1 21:ce51bb0ba4a5 307 strncpy(fb, fname.c_str(), 8); fb+=8; // FILENAME
uci1 21:ce51bb0ba4a5 308 *fb = '.'; ++fb; // .
uci1 21:ce51bb0ba4a5 309 strncpy(fb, fname.c_str()+fname.size()-3, 3); // EXT
uci1 21:ce51bb0ba4a5 310 // all that just for the file name!
uci1 21:ce51bb0ba4a5 311 FILE* lf = fopen(fnb,"wb");
uci1 21:ce51bb0ba4a5 312 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 313 printf("tried to open file [%s]. lf=%p\r\n",fnb,(void*)lf);
uci1 21:ce51bb0ba4a5 314 #endif
uci1 21:ce51bb0ba4a5 315 // get all the data and dump it into the file
uci1 21:ce51bb0ba4a5 316 int b = 0, toget = 0;
uci1 21:ce51bb0ba4a5 317 while ( mlen>b ) {
uci1 37:ff95e7070f26 318 if (fComm->IsTimedOut(timeout)) {
uci1 21:ce51bb0ba4a5 319 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 320 printf("timeout while getting file\r\n");
uci1 21:ce51bb0ba4a5 321 #endif
uci1 21:ce51bb0ba4a5 322 res = kFailTimeout;
uci1 21:ce51bb0ba4a5 323 break;
uci1 21:ce51bb0ba4a5 324 }
uci1 21:ce51bb0ba4a5 325 toget = mlen - b;
uci1 21:ce51bb0ba4a5 326 if (toget>bsize) {
uci1 21:ce51bb0ba4a5 327 toget = bsize;
uci1 21:ce51bb0ba4a5 328 }
uci1 37:ff95e7070f26 329 const int got = fComm->ReceiveAll(buf, toget, timeout);
uci1 21:ce51bb0ba4a5 330 if (lf!=NULL) {
uci1 36:87865913ae6f 331 #ifdef DEBUG
uci1 36:87865913ae6f 332 printf("writing %d bytes to file\r\n", got);
uci1 36:87865913ae6f 333 #endif
uci1 21:ce51bb0ba4a5 334 SnBitUtils::WriteTo(lf, buf, got);
uci1 21:ce51bb0ba4a5 335 }
uci1 21:ce51bb0ba4a5 336 b += got;
uci1 21:ce51bb0ba4a5 337 }
uci1 21:ce51bb0ba4a5 338 uint32_t crc=0;
uci1 21:ce51bb0ba4a5 339 if (lf!=NULL) {
uci1 21:ce51bb0ba4a5 340 // calculate the crc from what's actually in the file
uci1 21:ce51bb0ba4a5 341 fclose(lf); // to flush it
uci1 36:87865913ae6f 342 #ifdef DEBUG
uci1 36:87865913ae6f 343 printf("fopen: %s\r\n",fnb);
uci1 36:87865913ae6f 344 #endif
uci1 21:ce51bb0ba4a5 345 lf = fopen(fnb,"rb");
uci1 21:ce51bb0ba4a5 346 fseek(lf, 0, SEEK_END);
uci1 21:ce51bb0ba4a5 347 int32_t fend = ftell(lf);
uci1 21:ce51bb0ba4a5 348 fseek(lf, 0, SEEK_SET);
uci1 21:ce51bb0ba4a5 349 char c;
uci1 21:ce51bb0ba4a5 350 for (int32_t i=0; i<fend; ++i) {
uci1 21:ce51bb0ba4a5 351 SnBitUtils::ReadFrom(lf, c);
uci1 21:ce51bb0ba4a5 352 if (feof(lf)==0 && ferror(lf)==0) {
uci1 38:9070c17536cd 353 crc = SnCRCUtils::update_crc32_xfer(crc, c);
uci1 21:ce51bb0ba4a5 354 } else {
uci1 21:ce51bb0ba4a5 355 break;
uci1 21:ce51bb0ba4a5 356 }
uci1 21:ce51bb0ba4a5 357 }
uci1 21:ce51bb0ba4a5 358 fclose(lf);
uci1 36:87865913ae6f 359 #ifdef DEBUG
uci1 36:87865913ae6f 360 printf("closed file. crc = %u\r\n", crc);
uci1 36:87865913ae6f 361 #endif
uci1 21:ce51bb0ba4a5 362 }
uci1 36:87865913ae6f 363 #ifdef DEBUG
uci1 36:87865913ae6f 364 printf("mlen=%u, b=%d, eq=%s\r\n",mlen,b,
uci1 36:87865913ae6f 365 (mlen==b)?"true":"false");
uci1 36:87865913ae6f 366 #endif
uci1 21:ce51bb0ba4a5 367 if ( mlen!=b ) {
uci1 21:ce51bb0ba4a5 368 if (res > kUnexpectedRec) {
uci1 21:ce51bb0ba4a5 369 res = kUnexpectedRec;
uci1 21:ce51bb0ba4a5 370 } // otherwise it's already worse
uci1 21:ce51bb0ba4a5 371 } else {
uci1 21:ce51bb0ba4a5 372 // check that the file is ok
uci1 21:ce51bb0ba4a5 373 // get the checksum
uci1 21:ce51bb0ba4a5 374 res = GetHeader(timeout, buf, bsize, mcode, mlen);
uci1 21:ce51bb0ba4a5 375 if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileChksCode) ) {
uci1 36:87865913ae6f 376 #ifdef DEBUG
uci1 36:87865913ae6f 377 printf("received chksum=%u, crc=%u, eq=%s\r\n",
uci1 36:87865913ae6f 378 mlen,crc,(crc==mlen)?"true":"false");
uci1 36:87865913ae6f 379 #endif
uci1 21:ce51bb0ba4a5 380 if (crc != mlen) {
uci1 36:87865913ae6f 381 #ifdef DEBUG
uci1 36:87865913ae6f 382 printf("checksums differ! unexpected.\r\n");
uci1 36:87865913ae6f 383 #endif
uci1 21:ce51bb0ba4a5 384 res = kUnexpectedRec; // important!
uci1 21:ce51bb0ba4a5 385 }
uci1 21:ce51bb0ba4a5 386 }
uci1 21:ce51bb0ba4a5 387 }
uci1 21:ce51bb0ba4a5 388 //TODO: REMOVE
uci1 21:ce51bb0ba4a5 389 /*
uci1 21:ce51bb0ba4a5 390 char dbg[250]; memset(dbg, 0, 250);
uci1 21:ce51bb0ba4a5 391 char* dd = buf;
uci1 21:ce51bb0ba4a5 392 sprintf(dbg,"lf=%s (%p), my crc=%d, b=%d, mlen=%d\r\n",fnb,(void*)lf,crc,crc,b,mlen);
uci1 21:ce51bb0ba4a5 393 SnHeaderFrame::WriteTo(dd, SnHeaderFrame::kStringCode, strlen(dbg));
uci1 21:ce51bb0ba4a5 394 strcpy(buf+SnHeaderFrame::SizeOf(),dbg);
uci1 21:ce51bb0ba4a5 395 SendAll(buf, SnHeaderFrame::SizeOf() + strlen(dbg), timeout);
uci1 21:ce51bb0ba4a5 396 */
uci1 21:ce51bb0ba4a5 397 ///////////
uci1 21:ce51bb0ba4a5 398 if (lf!=NULL) {
uci1 21:ce51bb0ba4a5 399 if ( res<kOkWithMsg ) {
uci1 21:ce51bb0ba4a5 400 // timeout, bad checksum, something else bad?
uci1 36:87865913ae6f 401 #ifdef DEBUG
uci1 36:87865913ae6f 402 printf("removing %s\r\n",fnb);
uci1 36:87865913ae6f 403 #endif
uci1 21:ce51bb0ba4a5 404 remove(fnb); // delete the file
uci1 21:ce51bb0ba4a5 405 } else {
uci1 21:ce51bb0ba4a5 406 // if we got a new program, remove the old ones.
uci1 21:ce51bb0ba4a5 407 // (schedule myself for immediate de-resolution)
uci1 21:ce51bb0ba4a5 408 //
uci1 21:ce51bb0ba4a5 409 // first just count how many we would save
uci1 21:ce51bb0ba4a5 410 const uint16_t nSavedBins = LoopLocalDirBinFiles(false, fname);
uci1 36:87865913ae6f 411 #ifdef DEBUG
uci1 36:87865913ae6f 412 printf("nSavedBins=%hu",nSavedBins);
uci1 36:87865913ae6f 413 #endif
uci1 21:ce51bb0ba4a5 414 if (nSavedBins==1) {
uci1 21:ce51bb0ba4a5 415 // ok. new program will be the only one. remove the others
uci1 21:ce51bb0ba4a5 416 // hope the new program works!
uci1 21:ce51bb0ba4a5 417 LoopLocalDirBinFiles(true, fname);
uci1 36:87865913ae6f 418
uci1 36:87865913ae6f 419 #ifdef DEBUG
uci1 36:87865913ae6f 420 printf("rebooting\r\n");
uci1 36:87865913ae6f 421 #endif
uci1 21:ce51bb0ba4a5 422 // goodbye cruel world, it's over. walk on by...
uci1 21:ce51bb0ba4a5 423 mbed_reset();
uci1 21:ce51bb0ba4a5 424 }
uci1 21:ce51bb0ba4a5 425 }
uci1 21:ce51bb0ba4a5 426 }
uci1 21:ce51bb0ba4a5 427 }
uci1 21:ce51bb0ba4a5 428
uci1 21:ce51bb0ba4a5 429 return res;
uci1 21:ce51bb0ba4a5 430 }
uci1 21:ce51bb0ba4a5 431
uci1 21:ce51bb0ba4a5 432 int16_t SnCommWin::LoopLocalDirBinFiles(const bool doRemove,
uci1 21:ce51bb0ba4a5 433 const std::string& fname) {
uci1 21:ce51bb0ba4a5 434 // loop over the local directory, count the BIN files that match 'fname'
uci1 21:ce51bb0ba4a5 435 //
uci1 21:ce51bb0ba4a5 436 // fname is assumed to be all caps already!
uci1 21:ce51bb0ba4a5 437 //
uci1 21:ce51bb0ba4a5 438 // if doRemove==true, will actually delete non-matching files
uci1 21:ce51bb0ba4a5 439 //
uci1 21:ce51bb0ba4a5 440 // return number of non-matching files
uci1 21:ce51bb0ba4a5 441 uint16_t nSavedBins = 0;
uci1 21:ce51bb0ba4a5 442 DIR* d( opendir(kLocalDir) );
uci1 21:ce51bb0ba4a5 443 struct dirent* dent;
uci1 21:ce51bb0ba4a5 444 if ( d!=NULL ) {
uci1 21:ce51bb0ba4a5 445 while ( (dent = readdir(d))!=NULL ) {
uci1 21:ce51bb0ba4a5 446 const size_t flen = strlen(dent->d_name);
uci1 21:ce51bb0ba4a5 447 const char* dext = dent->d_name + flen - 4;
uci1 21:ce51bb0ba4a5 448 if ( strncmp(dext,".BIN",4)==0 || strncmp(dext,".bin",4)==0 ) {
uci1 21:ce51bb0ba4a5 449 // ends with .BIN
uci1 21:ce51bb0ba4a5 450 if (strncmp(dent->d_name, fname.c_str(), fname.size())!=0) {
uci1 21:ce51bb0ba4a5 451 // some other mbed program than our new one
uci1 21:ce51bb0ba4a5 452 std::string dfn(kLocalDir);
uci1 21:ce51bb0ba4a5 453 dfn += "/";
uci1 21:ce51bb0ba4a5 454 dfn += dent->d_name;
uci1 21:ce51bb0ba4a5 455 if (doRemove) {
uci1 21:ce51bb0ba4a5 456 remove(dfn.c_str());
uci1 21:ce51bb0ba4a5 457 }
uci1 21:ce51bb0ba4a5 458 } else {
uci1 21:ce51bb0ba4a5 459 ++nSavedBins;
uci1 21:ce51bb0ba4a5 460 }
uci1 21:ce51bb0ba4a5 461 } // else not an mbed program
uci1 21:ce51bb0ba4a5 462 }
uci1 21:ce51bb0ba4a5 463 closedir(d);
uci1 21:ce51bb0ba4a5 464 }
uci1 21:ce51bb0ba4a5 465 return nSavedBins;
uci1 21:ce51bb0ba4a5 466 }
uci1 21:ce51bb0ba4a5 467
uci1 21:ce51bb0ba4a5 468 SnCommWin::ECommWinResult SnCommWin::GetHeader(const uint32_t timeOut,
uci1 21:ce51bb0ba4a5 469 char* const buf,
uci1 21:ce51bb0ba4a5 470 const uint32_t bsize,
uci1 21:ce51bb0ba4a5 471 uint8_t& mcode,
uci1 21:ce51bb0ba4a5 472 uint32_t& mlen) {
uci1 21:ce51bb0ba4a5 473 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 21:ce51bb0ba4a5 474 if (bsize>=SnHeaderFrame::kMaxSizeOf) {
uci1 21:ce51bb0ba4a5 475 // get header
uci1 37:ff95e7070f26 476 const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut);
uci1 21:ce51bb0ba4a5 477 if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) {
uci1 21:ce51bb0ba4a5 478 mcode=0;
uci1 21:ce51bb0ba4a5 479 mlen=0;
uci1 21:ce51bb0ba4a5 480 const char* b = buf;
uci1 21:ce51bb0ba4a5 481 SnHeaderFrame::ReadFrom(b, mcode, mlen);
uci1 21:ce51bb0ba4a5 482 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 483 printf("mcode=%02x, mlen=%u\r\n", mcode, mlen);
uci1 21:ce51bb0ba4a5 484 #endif
uci1 21:ce51bb0ba4a5 485 res = SnCommWin::kOkWithMsg;
uci1 21:ce51bb0ba4a5 486 }
uci1 21:ce51bb0ba4a5 487 }
uci1 21:ce51bb0ba4a5 488 return res;
uci1 21:ce51bb0ba4a5 489 }
uci1 21:ce51bb0ba4a5 490
uci1 8:95a325df1f6b 491 SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf,
uci1 8:95a325df1f6b 492 const uint32_t timeOut,
uci1 8:95a325df1f6b 493 char* const confBuf,
uci1 8:95a325df1f6b 494 const uint32_t bsize) {
uci1 8:95a325df1f6b 495 // confBuf assumed to alread be of allocated size
uci1 8:95a325df1f6b 496
uci1 12:d472f9811262 497 #ifdef DEBUG
uci1 8:95a325df1f6b 498 printf("GetConfig, to=%u\r\n",timeOut);
uci1 12:d472f9811262 499 #endif
uci1 8:95a325df1f6b 500
uci1 8:95a325df1f6b 501 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 21:ce51bb0ba4a5 502 if (bsize>=SnConfigFrame::kMaxSizeOf) {
uci1 8:95a325df1f6b 503 // get header
uci1 21:ce51bb0ba4a5 504 uint8_t mcode=0; uint32_t mlen=0;
uci1 21:ce51bb0ba4a5 505 res = GetHeader(timeOut, confBuf, bsize, mcode, mlen);
uci1 21:ce51bb0ba4a5 506 if (res>=SnCommWin::kOkWithMsg) {
uci1 12:d472f9811262 507 if (mcode==SnHeaderFrame::kNoConfigCode) {
uci1 12:d472f9811262 508 // no config to get
uci1 16:744ce85aede2 509 res = SnCommWin::kOkWthMsgNoConf;
uci1 21:ce51bb0ba4a5 510 } else if (mcode==SnHeaderFrame::kMbedFilenameCode) {
uci1 21:ce51bb0ba4a5 511 res = GetFilename(timeOut, confBuf, mlen);
uci1 21:ce51bb0ba4a5 512 if (res>kAllFails) {
uci1 21:ce51bb0ba4a5 513 std::string fname(confBuf, mlen); // the filename
uci1 21:ce51bb0ba4a5 514 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 515 printf("got filename = [%s]\r\n", fname.c_str());
uci1 21:ce51bb0ba4a5 516 #endif
uci1 21:ce51bb0ba4a5 517 res = GetLocalFile(fname, confBuf, bsize, timeOut);
uci1 21:ce51bb0ba4a5 518 }
uci1 12:d472f9811262 519 } else if (mcode!=SnHeaderFrame::kConfigCode) {
uci1 8:95a325df1f6b 520 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 521 } else {
uci1 8:95a325df1f6b 522 // get config
uci1 37:ff95e7070f26 523 const int clen = fComm->ReceiveAll(confBuf, mlen, timeOut);
uci1 8:95a325df1f6b 524 if (clen>0 && static_cast<uint32_t>(clen)==mlen) {
uci1 21:ce51bb0ba4a5 525 const char* b = confBuf;
uci1 8:95a325df1f6b 526 conf.ReadFrom(b);
uci1 16:744ce85aede2 527 res = SnCommWin::kOkWithMsg;
uci1 8:95a325df1f6b 528 } else {
uci1 8:95a325df1f6b 529 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 530 }
uci1 8:95a325df1f6b 531 }
uci1 8:95a325df1f6b 532 } else {
uci1 21:ce51bb0ba4a5 533 // not a problem if we get nothing (or no config)
uci1 16:744ce85aede2 534 res = SnCommWin::kOkNoMsg;
uci1 8:95a325df1f6b 535 }
uci1 8:95a325df1f6b 536 }
uci1 8:95a325df1f6b 537 return res;
uci1 8:95a325df1f6b 538 }
uci1 8:95a325df1f6b 539
uci1 8:95a325df1f6b 540 SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf,
uci1 8:95a325df1f6b 541 const SnEventFrame& evt,
uci1 8:95a325df1f6b 542 const SnPowerFrame& pow,
uci1 10:3c93db1cfb12 543 const uint16_t seq,
uci1 10:3c93db1cfb12 544 const float thmrate,
uci1 10:3c93db1cfb12 545 const float evtrate,
uci1 8:95a325df1f6b 546 char* const genBuf,
uci1 8:95a325df1f6b 547 const uint32_t timeout_clock) {
uci1 8:95a325df1f6b 548 // TODO: check if connected?
uci1 12:d472f9811262 549 #ifdef DEBUG
uci1 12:d472f9811262 550 printf("########### Send Status\r\n");
uci1 12:d472f9811262 551 #endif
uci1 21:ce51bb0ba4a5 552 uint8_t loseLSB=0, loseMSB=0;
uci1 21:ce51bb0ba4a5 553 uint16_t wvBase=0;
uci1 21:ce51bb0ba4a5 554 conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase);
uci1 8:95a325df1f6b 555 const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf);
uci1 8:95a325df1f6b 556 char* b = genBuf;
uci1 8:95a325df1f6b 557 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize);
uci1 21:ce51bb0ba4a5 558 SnStatusFrame::WriteTo(b, conf, evt, genBuf, seq, thmrate, evtrate,
uci1 21:ce51bb0ba4a5 559 loseLSB, loseMSB, wvBase);
uci1 10:3c93db1cfb12 560 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 10:3c93db1cfb12 561 pow.SizeOf(SnPowerFrame::kIOvers));
uci1 10:3c93db1cfb12 562 pow.WriteTo(b);
uci1 10:3c93db1cfb12 563 int msiz = b-genBuf;
uci1 18:55f1581f2ee4 564 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 565 printf("calling SendAll (status)\r\n");
uci1 18:55f1581f2ee4 566 #endif
uci1 37:ff95e7070f26 567 int mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 568 #ifdef DEBUG
uci1 8:95a325df1f6b 569 printf("status frame:\r\n");
uci1 15:f2569d8e4176 570 for (uint32_t i=0; i<msiz; i++) {
uci1 8:95a325df1f6b 571 printf("%02X ",genBuf[i]);
uci1 8:95a325df1f6b 572 }
uci1 8:95a325df1f6b 573 printf("\r\n");
uci1 8:95a325df1f6b 574 #endif
uci1 8:95a325df1f6b 575 if (mlen==msiz) {
uci1 12:d472f9811262 576 #ifdef DEBUG
uci1 10:3c93db1cfb12 577 printf("status+power sent\r\n");
uci1 12:d472f9811262 578 #endif
uci1 21:ce51bb0ba4a5 579 // event compression parameters must be the same
uci1 21:ce51bb0ba4a5 580 // as those sent in the status update
uci1 8:95a325df1f6b 581 b = genBuf;
uci1 10:3c93db1cfb12 582 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode,
uci1 21:ce51bb0ba4a5 583 evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB));
uci1 21:ce51bb0ba4a5 584 b = evt.WriteTo(b, loseLSB, loseMSB, wvBase);
uci1 21:ce51bb0ba4a5 585 msiz = b-genBuf;
uci1 16:744ce85aede2 586 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 587 printf("calling SendAll (event) %d:\r\n",msiz);
uci1 21:ce51bb0ba4a5 588 for (uint32_t i=0; i<msiz; i++) {
uci1 21:ce51bb0ba4a5 589 printf("%02X ",genBuf[i]);
uci1 21:ce51bb0ba4a5 590 }
uci1 21:ce51bb0ba4a5 591 printf("\r\n");
uci1 21:ce51bb0ba4a5 592 #endif
uci1 37:ff95e7070f26 593 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 21:ce51bb0ba4a5 594 if (mlen==msiz) {
uci1 21:ce51bb0ba4a5 595 return SnCommWin::kOkMsgSent;
uci1 21:ce51bb0ba4a5 596 }
uci1 8:95a325df1f6b 597 }
uci1 8:95a325df1f6b 598 return SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 599 }
uci1 8:95a325df1f6b 600
uci1 27:efc4d654b139 601
uci1 23:ccf39298f205 602 SnCommWin::ECommWinResult SnCommWin::SendString(const char* str,
uci1 23:ccf39298f205 603 const uint32_t timeout) {
uci1 23:ccf39298f205 604 #ifdef DEBUG
uci1 23:ccf39298f205 605 printf("SnCommWin::SendString %s\r\n",str);
uci1 23:ccf39298f205 606 #endif
uci1 37:ff95e7070f26 607 const bool ok = fComm->SendString(str, timeout);
uci1 37:ff95e7070f26 608 return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 23:ccf39298f205 609 }
uci1 23:ccf39298f205 610
uci1 12:d472f9811262 611 SnCommWin::ECommWinResult SnCommWin::SendFilename(const char* inf,
uci1 8:95a325df1f6b 612 char* const genBuf,
uci1 8:95a325df1f6b 613 const uint32_t timeout_clock) {
uci1 12:d472f9811262 614 #ifdef DEBUG
uci1 12:d472f9811262 615 printf("SnCommWin::SendFilename %s\r\n",inf);
uci1 12:d472f9811262 616 #endif
uci1 16:744ce85aede2 617
uci1 12:d472f9811262 618 // remove the directory
uci1 12:d472f9811262 619 const char* fn = strrchr(inf, '/');
uci1 12:d472f9811262 620 if (fn==0) {
uci1 12:d472f9811262 621 // no directory
uci1 12:d472f9811262 622 fn = inf;
uci1 12:d472f9811262 623 } else if (fn!=inf) {
uci1 12:d472f9811262 624 ++fn; // move past the '/' if it was found
uci1 12:d472f9811262 625 }
uci1 12:d472f9811262 626 #ifdef DEBUG
uci1 12:d472f9811262 627 printf("send %s\r\n",fn);
uci1 12:d472f9811262 628 #endif
uci1 8:95a325df1f6b 629 const size_t flen = strlen(fn);
uci1 8:95a325df1f6b 630 char* b = genBuf;
uci1 8:95a325df1f6b 631 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen);
uci1 12:d472f9811262 632 b = SnBitUtils::WriteTo(b, fn, flen);
uci1 12:d472f9811262 633 const int msiz = b-genBuf;
uci1 37:ff95e7070f26 634 const int mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 635 #ifdef DEBUG
uci1 12:d472f9811262 636 printf("time = %u, timeout = %u\r\n",time(0), timeout_clock);
uci1 12:d472f9811262 637 #endif
uci1 8:95a325df1f6b 638 return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 639 }
uci1 8:95a325df1f6b 640
uci1 12:d472f9811262 641 int SnCommWin::SendFileBlock(FILE* inf,
uci1 12:d472f9811262 642 const uint8_t blockHeaderCode,
uci1 12:d472f9811262 643 const uint32_t blockSize,
uci1 12:d472f9811262 644 char* const genBuf,
uci1 12:d472f9811262 645 const uint32_t timeout) {
uci1 12:d472f9811262 646 // sends the block from the file. does not check if the file has sufficient bytes
uci1 12:d472f9811262 647 // this should be done before calling this function
uci1 12:d472f9811262 648 // (where the number of bytes in the file can be cached)
uci1 12:d472f9811262 649 char* b = genBuf;
uci1 12:d472f9811262 650 SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize);
uci1 12:d472f9811262 651 SnBitUtils::ReadFrom(inf, b, blockSize);
uci1 12:d472f9811262 652 #ifdef DEBUG
uci1 12:d472f9811262 653 printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf());
uci1 12:d472f9811262 654 #endif
uci1 37:ff95e7070f26 655 return fComm->SendAll(genBuf, blockSize+SnHeaderFrame::SizeOf(), timeout);
uci1 12:d472f9811262 656 }
uci1 12:d472f9811262 657
uci1 12:d472f9811262 658 SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf,
uci1 12:d472f9811262 659 const SnConfigFrame& curConf,
uci1 12:d472f9811262 660 SnEventFrame& evt,
uci1 12:d472f9811262 661 SnPowerFrame& pow,
uci1 12:d472f9811262 662 char* const genBuf,
uci1 12:d472f9811262 663 uint32_t nevts,
uci1 12:d472f9811262 664 const uint32_t timeout_clock) {
uci1 12:d472f9811262 665 // firstEvt==0 ==> start at beginning
uci1 12:d472f9811262 666 // nevts==0 ==> all events
uci1 12:d472f9811262 667
uci1 12:d472f9811262 668 #ifdef DEBUG
uci1 12:d472f9811262 669 printf("SendFileContents (byte streaming)\r\n");
uci1 12:d472f9811262 670 #endif
uci1 12:d472f9811262 671
uci1 12:d472f9811262 672 // store position in file so we can go back to it afterwards
uci1 12:d472f9811262 673 // this is probably not necessary, because if this file is open
uci1 12:d472f9811262 674 // in write-only mode, we're screwed..
uci1 12:d472f9811262 675 const int fpos = ftell(inf);
uci1 12:d472f9811262 676 if (fpos>0) {
uci1 12:d472f9811262 677 fseek(inf, 0, SEEK_SET);
uci1 12:d472f9811262 678 }
uci1 12:d472f9811262 679 // how many bytes?
uci1 12:d472f9811262 680 fseek(inf, 0, SEEK_END); // go to end
uci1 12:d472f9811262 681 const int fend = ftell(inf);
uci1 12:d472f9811262 682 fseek(inf, 0, SEEK_SET); // go to start
uci1 12:d472f9811262 683 #ifdef DEBUG
uci1 12:d472f9811262 684 printf("fend=%d\r\n",fend);
uci1 12:d472f9811262 685 printf("fpos=%d\r\n",fpos);
uci1 12:d472f9811262 686 #endif
uci1 12:d472f9811262 687
uci1 12:d472f9811262 688 // variables used when sending data
uci1 12:d472f9811262 689 char* b = genBuf;
uci1 12:d472f9811262 690 int msiz, mlen;
uci1 21:ce51bb0ba4a5 691 // count number of events / power readings sent
uci1 21:ce51bb0ba4a5 692 uint32_t evtsSent=0, powsSent=0;
uci1 12:d472f9811262 693
uci1 12:d472f9811262 694 // first is the file header, which has no SnHeaderFrame
uci1 12:d472f9811262 695 msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers);
uci1 12:d472f9811262 696 bool ok = (ftell(inf)+msiz)<=fend;
uci1 12:d472f9811262 697 if (ok) {
uci1 12:d472f9811262 698 mlen = SendFileBlock(inf, SnHeaderFrame::kFileHeadrCode,
uci1 12:d472f9811262 699 msiz, genBuf, timeout_clock);
uci1 12:d472f9811262 700 ok &= (msiz+SnHeaderFrame::SizeOf())==mlen;
uci1 12:d472f9811262 701
uci1 12:d472f9811262 702 #ifdef DEBUG
uci1 12:d472f9811262 703 printf("sent file header. ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 704 #endif
uci1 12:d472f9811262 705
uci1 12:d472f9811262 706 // the header info for each block
uci1 12:d472f9811262 707 uint8_t hcode;
uci1 12:d472f9811262 708 uint32_t hlen;
uci1 12:d472f9811262 709
uci1 12:d472f9811262 710 // now just loop through file and send each block
uci1 18:55f1581f2ee4 711 bool fok = true;
uci1 18:55f1581f2ee4 712 while ( fok
uci1 12:d472f9811262 713 && (feof(inf)==0)
uci1 12:d472f9811262 714 && (ferror(inf)==0)
uci1 12:d472f9811262 715 && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) {
uci1 12:d472f9811262 716 // read the header for the block
uci1 12:d472f9811262 717 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 18:55f1581f2ee4 718 fok &= (ftell(inf)+hlen)<=fend;
uci1 12:d472f9811262 719 #ifdef DEBUG
uci1 18:55f1581f2ee4 720 printf("new block: hc=%02x, hl=%u, ftell=%d, fend=%d, "
uci1 18:55f1581f2ee4 721 "ok=%d, fok=%d\r\n",
uci1 18:55f1581f2ee4 722 hcode, hlen, ftell(inf), fend, (int)ok, (int)fok);
uci1 12:d472f9811262 723 #endif
uci1 12:d472f9811262 724 if (ok) {
uci1 12:d472f9811262 725 // send the block
uci1 12:d472f9811262 726 // TODO: repack events?
uci1 12:d472f9811262 727 msiz = hlen + SnHeaderFrame::SizeOf();
uci1 12:d472f9811262 728 mlen = SendFileBlock(inf, hcode, hlen, genBuf, timeout_clock);
uci1 12:d472f9811262 729 ok &= msiz==mlen;
uci1 12:d472f9811262 730 if (ok) {
uci1 12:d472f9811262 731 if (hcode==SnHeaderFrame::kEventCode) {
uci1 12:d472f9811262 732 ++evtsSent;
uci1 12:d472f9811262 733 if (nevts>0) {
uci1 12:d472f9811262 734 if (evtsSent>=nevts) {
uci1 12:d472f9811262 735 #ifdef DEBUG
uci1 12:d472f9811262 736 printf("send %u events. stop\r\n",evtsSent);
uci1 12:d472f9811262 737 #endif
uci1 12:d472f9811262 738 break;
uci1 12:d472f9811262 739 }
uci1 12:d472f9811262 740 }
uci1 12:d472f9811262 741 } else if (hcode==SnHeaderFrame::kPowerCode) {
uci1 12:d472f9811262 742 ++powsSent;
uci1 12:d472f9811262 743 }
uci1 12:d472f9811262 744 } else {
uci1 12:d472f9811262 745 #ifdef DEBUG
uci1 12:d472f9811262 746 printf("not ok when sending hcode=%02x\r\n",hcode);
uci1 12:d472f9811262 747 #endif
uci1 21:ce51bb0ba4a5 748 } // if ok after sending block
uci1 21:ce51bb0ba4a5 749 } // if ok after sending block header
uci1 21:ce51bb0ba4a5 750 } // loop over file contents
uci1 21:ce51bb0ba4a5 751 } else {
uci1 21:ce51bb0ba4a5 752 // otherwise file size not sufficient for file header
uci1 21:ce51bb0ba4a5 753 // so it's either 0 or corrupted.
uci1 21:ce51bb0ba4a5 754 // proceed as normal even tho no contents were sent.
uci1 21:ce51bb0ba4a5 755 // if we're deleting files, this one will be deleted.
uci1 21:ce51bb0ba4a5 756 ok = true;
uci1 21:ce51bb0ba4a5 757 }
uci1 12:d472f9811262 758 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 759 printf("loop done. ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 760 #endif
uci1 21:ce51bb0ba4a5 761
uci1 21:ce51bb0ba4a5 762 // send number of events sent
uci1 12:d472f9811262 763 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 764 printf("sending evtsSent (%u)\r\n",evtsSent);
uci1 12:d472f9811262 765 #endif
uci1 21:ce51bb0ba4a5 766 b = genBuf;
uci1 21:ce51bb0ba4a5 767 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t));
uci1 21:ce51bb0ba4a5 768 b = SnBitUtils::WriteTo(b, evtsSent);
uci1 21:ce51bb0ba4a5 769 msiz = b - genBuf;
uci1 37:ff95e7070f26 770 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 21:ce51bb0ba4a5 771 ok &= msiz==mlen;
uci1 18:55f1581f2ee4 772
uci1 18:55f1581f2ee4 773 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 774 printf("ok=%d (msiz=%d, mlen=%d)\r\n", (int)ok, msiz, mlen);
uci1 18:55f1581f2ee4 775 #endif
uci1 21:ce51bb0ba4a5 776
uci1 21:ce51bb0ba4a5 777 // send number of power readings sent
uci1 12:d472f9811262 778 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 779 printf("sending powsSent (%u)\r\n",powsSent);
uci1 12:d472f9811262 780 #endif
uci1 21:ce51bb0ba4a5 781 b = genBuf;
uci1 21:ce51bb0ba4a5 782 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t));
uci1 21:ce51bb0ba4a5 783 b = SnBitUtils::WriteTo(b, powsSent);
uci1 21:ce51bb0ba4a5 784 msiz = b - genBuf;
uci1 37:ff95e7070f26 785 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 21:ce51bb0ba4a5 786 ok &= msiz==mlen;
uci1 21:ce51bb0ba4a5 787
uci1 18:55f1581f2ee4 788 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 789 printf("ok=%d (msiz=%d, mlen=%d)\r\n", (int)ok, msiz, mlen);
uci1 18:55f1581f2ee4 790 #endif
uci1 12:d472f9811262 791
uci1 12:d472f9811262 792
uci1 12:d472f9811262 793 // put file position back
uci1 12:d472f9811262 794 fseek(inf, fpos, SEEK_SET);
uci1 12:d472f9811262 795
uci1 12:d472f9811262 796 #ifdef DEBUG
uci1 12:d472f9811262 797 printf("end contents: ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 798 #endif
uci1 12:d472f9811262 799
uci1 12:d472f9811262 800 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 12:d472f9811262 801 }
uci1 12:d472f9811262 802
uci1 12:d472f9811262 803 /*
uci1 8:95a325df1f6b 804 SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf,
uci1 8:95a325df1f6b 805 const SnConfigFrame& curConf,
uci1 8:95a325df1f6b 806 SnEventFrame& evt,
uci1 8:95a325df1f6b 807 SnPowerFrame& pow,
uci1 8:95a325df1f6b 808 char* const genBuf,
uci1 8:95a325df1f6b 809 uint32_t nevts,
uci1 8:95a325df1f6b 810 const uint32_t timeout_clock,
uci1 8:95a325df1f6b 811 const uint32_t firstEvt) {
uci1 12:d472f9811262 812 printf("comm win send file contents\r\n");
uci1 8:95a325df1f6b 813 // firstEvt==0 ==> start at beginning
uci1 8:95a325df1f6b 814 // nevts==0 ==> all events
uci1 8:95a325df1f6b 815
uci1 8:95a325df1f6b 816 const int fpos = ftell(inf);
uci1 8:95a325df1f6b 817 if (fpos>0) {
uci1 8:95a325df1f6b 818 fseek(inf, 0, SEEK_SET);
uci1 8:95a325df1f6b 819 }
uci1 12:d472f9811262 820 // how many bytes?
uci1 12:d472f9811262 821 fseek(inf, 0, SEEK_END); // go to end
uci1 12:d472f9811262 822 const int fend = ftell(inf);
uci1 12:d472f9811262 823 fseek(inf, 0, SEEK_SET); // go to start
uci1 12:d472f9811262 824 printf("fend=%d\r\n",fend);
uci1 8:95a325df1f6b 825 printf("fpos=%d\r\n",fpos);
uci1 8:95a325df1f6b 826 // get file header
uci1 8:95a325df1f6b 827 uint64_t macadr;
uci1 8:95a325df1f6b 828 uint32_t run;
uci1 8:95a325df1f6b 829 uint16_t seq;
uci1 12:d472f9811262 830 bool ok = (ftell(inf)+SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers))<=fend;
uci1 12:d472f9811262 831 if (ok) {
uci1 12:d472f9811262 832 SnSDUtils::ReadFileHeader(inf, macadr, run, seq, &pow);
uci1 12:d472f9811262 833 printf("read file header: run %u, seq %hu\r\n",run,seq);
uci1 12:d472f9811262 834 }
uci1 12:d472f9811262 835
uci1 12:d472f9811262 836 printf(">>> file hdr read ftell(inf)=%d\r\n",ftell(inf));
uci1 8:95a325df1f6b 837
uci1 8:95a325df1f6b 838 // check block
uci1 8:95a325df1f6b 839 uint8_t hcode;
uci1 8:95a325df1f6b 840 uint32_t hlen;
uci1 12:d472f9811262 841 ok &= (ftell(inf)+SnHeaderFrame::SizeOf())<=fend;
uci1 12:d472f9811262 842 if (ok) {
uci1 12:d472f9811262 843 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 12:d472f9811262 844 printf("read block hd %02x, len %u\r\n",hcode,hlen);
uci1 12:d472f9811262 845 if (hcode==SnHeaderFrame::kPowerCode) {
uci1 12:d472f9811262 846 ok &= (ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend;
uci1 12:d472f9811262 847 if (ok) {
uci1 12:d472f9811262 848 pow.ReadFrom(inf);
uci1 12:d472f9811262 849 printf("read power: v1=%g, v2=%g\r\n",pow.GetAveV1(), pow.GetAveV2());
uci1 12:d472f9811262 850 // now read the next header
uci1 12:d472f9811262 851 ok &= (ftell(inf)+SnHeaderFrame::SizeOf())<=fend;
uci1 12:d472f9811262 852 if (ok) {
uci1 12:d472f9811262 853 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 12:d472f9811262 854 printf("read new header: hc %02x, hlen=%u\r\n",hcode,hlen);
uci1 12:d472f9811262 855 }
uci1 12:d472f9811262 856 }
uci1 12:d472f9811262 857 }
uci1 8:95a325df1f6b 858 }
uci1 8:95a325df1f6b 859
uci1 12:d472f9811262 860 printf(">>> power tried ftell(inf)=%d\r\n",ftell(inf));
uci1 12:d472f9811262 861
uci1 12:d472f9811262 862 printf("hcode=%02x\r\n",hcode);
uci1 12:d472f9811262 863 printf("ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 864 printf("feof=%d\r\n",feof(inf));
uci1 12:d472f9811262 865 printf("ferror=%d\r\n",ferror(inf));
uci1 12:d472f9811262 866 printf("%d+%u <= %d ? %d\r\n",ftell(inf),hlen,fend,
uci1 12:d472f9811262 867 (int)((ftell(inf)+hlen)<=fend));
uci1 12:d472f9811262 868
uci1 8:95a325df1f6b 869 // TODO: check memory for temporary config/event frames?
uci1 8:95a325df1f6b 870 // get config
uci1 8:95a325df1f6b 871 while ((hcode!=SnHeaderFrame::kConfigCode)
uci1 12:d472f9811262 872 && ok
uci1 8:95a325df1f6b 873 && (feof(inf)==0)
uci1 12:d472f9811262 874 && (ferror(inf)==0)
uci1 12:d472f9811262 875 && ((ftell(inf)+hlen) <= fend) ) {
uci1 12:d472f9811262 876 printf("skip: ftell=%d, hlen=%u\r\n",ftell(inf),hlen);
uci1 8:95a325df1f6b 877 fseek(inf, hlen, SEEK_CUR); // skip this block
uci1 12:d472f9811262 878 ok = (ftell(inf)+SnHeaderFrame::SizeOf())<=fend;
uci1 12:d472f9811262 879 if (ok) {
uci1 12:d472f9811262 880 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 12:d472f9811262 881 printf("hcs >>> ftell(inf)=%d, hcode=%02x, hlen=%u\r\n",ftell(inf),hcode,hlen);
uci1 12:d472f9811262 882 printf("feof=%d, ferror=%d\r\n",feof(inf),ferror(inf));
uci1 12:d472f9811262 883 }
uci1 8:95a325df1f6b 884 }
uci1 8:95a325df1f6b 885
uci1 12:d472f9811262 886 printf("checking ftell\r\n");
uci1 12:d472f9811262 887 bool gotConf = false;
uci1 12:d472f9811262 888 ok = (ftell(inf)+hlen) <= fend;
uci1 12:d472f9811262 889 printf("making temp config\r\n");
uci1 12:d472f9811262 890 SnConfigFrame conf; // TODO: SOMETIMES STALLS HERE!
uci1 12:d472f9811262 891 printf("trying to read\r\n");
uci1 12:d472f9811262 892 if (ok && (hcode==SnHeaderFrame::kConfigCode)) {
uci1 12:d472f9811262 893 printf("reading config from ftell=%d, hcode=%02x, hlen=%u, ok=%d\r\n",
uci1 12:d472f9811262 894 ftell(inf), hcode, hlen, (int)ok);
uci1 12:d472f9811262 895 conf.ReadFrom(inf);
uci1 12:d472f9811262 896 gotConf = (ftell(inf) <= fend) && (feof(inf)==0) && (ferror(inf)==0);
uci1 12:d472f9811262 897 }
uci1 12:d472f9811262 898 printf(">>> conf. ftell(inf)=%d, gotconf=%d\r\n",ftell(inf),(int)gotConf);
uci1 8:95a325df1f6b 899
uci1 8:95a325df1f6b 900 char* b = genBuf;
uci1 12:d472f9811262 901 int msiz, mlen;
uci1 12:d472f9811262 902 if (ok && gotConf) {
uci1 12:d472f9811262 903 // get event size
uci1 12:d472f9811262 904 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 12:d472f9811262 905 uint16_t sWvBase=0;
uci1 12:d472f9811262 906 curConf.GetPackParsFor(GetCommType(), sLoseLSB, sLoseMSB, sWvBase);
uci1 12:d472f9811262 907
uci1 12:d472f9811262 908 // size of event in file
uci1 12:d472f9811262 909 const uint32_t esize = SnEventFrame::SizeOf(SnEventFrame::kIOVers,
uci1 12:d472f9811262 910 conf.GetWvLoseLSB(),
uci1 12:d472f9811262 911 conf.GetWvLoseMSB());
uci1 12:d472f9811262 912
uci1 12:d472f9811262 913 printf("esize=%u\r\n",esize);
uci1 12:d472f9811262 914
uci1 12:d472f9811262 915 // number of events / power readings we will send
uci1 12:d472f9811262 916 uint32_t evtsSent=0, powsSent=0;
uci1 8:95a325df1f6b 917 b = genBuf;
uci1 12:d472f9811262 918 // send the file header
uci1 12:d472f9811262 919 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileHeadrCode,
uci1 12:d472f9811262 920 SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers));
uci1 12:d472f9811262 921 SnSDUtils::WriteFileHeader(b, macadr, run, seq);
uci1 8:95a325df1f6b 922 msiz = b-genBuf;
uci1 8:95a325df1f6b 923 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 924 if (mlen==msiz) {
uci1 12:d472f9811262 925 // send power header
uci1 8:95a325df1f6b 926 b = genBuf;
uci1 12:d472f9811262 927 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 12:d472f9811262 928 pow.SizeOf(SnPowerFrame::kIOvers));
uci1 12:d472f9811262 929 pow.WriteTo(b);
uci1 8:95a325df1f6b 930 msiz = b-genBuf;
uci1 8:95a325df1f6b 931 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 932 ++powsSent;
uci1 8:95a325df1f6b 933 if (mlen==msiz) {
uci1 12:d472f9811262 934 // send the config
uci1 12:d472f9811262 935 b = genBuf;
uci1 12:d472f9811262 936 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfigCode,
uci1 12:d472f9811262 937 conf.SizeOf(SnConfigFrame::kIOVers));
uci1 12:d472f9811262 938 conf.WriteTo(b);
uci1 12:d472f9811262 939 msiz = b-genBuf;
uci1 12:d472f9811262 940 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 941 if (mlen==msiz) {
uci1 12:d472f9811262 942 ok = true;
uci1 12:d472f9811262 943 }
uci1 8:95a325df1f6b 944 }
uci1 8:95a325df1f6b 945 }
uci1 12:d472f9811262 946
uci1 12:d472f9811262 947 printf("ok=%d, nevts=%u\r\n",(int)ok,nevts);
uci1 8:95a325df1f6b 948
uci1 12:d472f9811262 949 if (ok) {
uci1 12:d472f9811262 950 // size of event sent over afar
uci1 12:d472f9811262 951 const uint32_t ssize = SnEventFrame::SizeOf(SnEventFrame::kIOVers,
uci1 12:d472f9811262 952 sLoseLSB, sLoseMSB);
uci1 12:d472f9811262 953
uci1 12:d472f9811262 954 // loop over blocks. send event & power frames
uci1 12:d472f9811262 955 // until EOF or nevets have been sent
uci1 12:d472f9811262 956 uint8_t hc; uint32_t hl;
uci1 12:d472f9811262 957 while (ok && (feof(inf)==0)
uci1 12:d472f9811262 958 && (ferror(inf)==0)
uci1 12:d472f9811262 959 && (ftell(inf)<fend) ) { // feof doesn't seem to work
uci1 12:d472f9811262 960 printf(">>> ftell(inf)=%d\r\n",ftell(inf));
uci1 12:d472f9811262 961 if ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) {
uci1 12:d472f9811262 962 SnSDUtils::ReadBlockHeader(inf, hc, hl);
uci1 12:d472f9811262 963 if (hc==SnHeaderFrame::kEventCode) {
uci1 12:d472f9811262 964 if ((ftell(inf)+ssize)<=fend) {
uci1 12:d472f9811262 965 evt.ReadFrom(inf, genBuf,
uci1 12:d472f9811262 966 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
uci1 12:d472f9811262 967 conf.GetWvBaseline());
uci1 12:d472f9811262 968 if (evt.GetEvtNum()>=firstEvt) {
uci1 12:d472f9811262 969 ++evtsSent;
uci1 12:d472f9811262 970 printf("sending evt %u\r\n",evtsSent);
uci1 12:d472f9811262 971 // must be after evt.Read, since that uses the buffer
uci1 12:d472f9811262 972 b = genBuf;
uci1 12:d472f9811262 973 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize);
uci1 12:d472f9811262 974 evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase); // will repack if necessary
uci1 12:d472f9811262 975 msiz = b-genBuf; //SnHeaderFrame::SizeOf()+ssize;
uci1 12:d472f9811262 976 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 977 ok &= mlen==msiz;
uci1 12:d472f9811262 978 }
uci1 12:d472f9811262 979 } else {
uci1 12:d472f9811262 980 printf("event header, but not enough bytes in file\r\n");
uci1 12:d472f9811262 981 ok = false;
uci1 12:d472f9811262 982 }
uci1 12:d472f9811262 983 } else if (hc==SnHeaderFrame::kPowerCode) {
uci1 12:d472f9811262 984 if ((ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend) {
uci1 12:d472f9811262 985 ++powsSent;
uci1 12:d472f9811262 986 pow.ReadFrom(inf);
uci1 12:d472f9811262 987 printf("sending power frame %u, v1=%g, v2=%g, t=%u\r\n",
uci1 12:d472f9811262 988 powsSent, pow.GetAveV1(), pow.GetAveV2(), pow.GetTime());
uci1 12:d472f9811262 989 b = genBuf;
uci1 12:d472f9811262 990 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 12:d472f9811262 991 SnPowerFrame::SizeOf(SnPowerFrame::kIOvers));
uci1 12:d472f9811262 992 pow.WriteTo(b);
uci1 12:d472f9811262 993 msiz = b-genBuf;
uci1 12:d472f9811262 994 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 995 ok &= mlen==msiz;
uci1 12:d472f9811262 996 } else {
uci1 12:d472f9811262 997 printf("power header, but not enough bytes in file\r\n");
uci1 12:d472f9811262 998 ok = false;
uci1 12:d472f9811262 999 }
uci1 12:d472f9811262 1000 } else {
uci1 12:d472f9811262 1001 printf("unhandled block. hc=%hhu, hl=%u\r\n",
uci1 12:d472f9811262 1002 hc, hl);
uci1 12:d472f9811262 1003 if ((ftell(inf)+hl)<=fend) {
uci1 12:d472f9811262 1004 fseek(inf, hl, SEEK_CUR); // skip this block
uci1 12:d472f9811262 1005 } else {
uci1 12:d472f9811262 1006 printf("not enough bytes in file to skip block\r\n");
uci1 12:d472f9811262 1007 ok = false;
uci1 12:d472f9811262 1008 }
uci1 8:95a325df1f6b 1009 }
uci1 8:95a325df1f6b 1010 } else {
uci1 12:d472f9811262 1011 printf("not enough bytes in file to read header\r\n");
uci1 12:d472f9811262 1012 printf("feof=%d\r\n",feof(inf));
uci1 12:d472f9811262 1013 printf("ftell(inf)=%d, hdr size=%u, fend=%u\r\n",
uci1 12:d472f9811262 1014 (ftell(inf)), SnHeaderFrame::SizeOf(), fend);
uci1 8:95a325df1f6b 1015 ok = false;
uci1 8:95a325df1f6b 1016 }
uci1 8:95a325df1f6b 1017 }
uci1 12:d472f9811262 1018 // send number of events sent
uci1 12:d472f9811262 1019 b = genBuf;
uci1 12:d472f9811262 1020 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t));
uci1 12:d472f9811262 1021 b = SnBitUtils::WriteTo(b, evtsSent);
uci1 12:d472f9811262 1022 msiz = b - genBuf;
uci1 12:d472f9811262 1023 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 1024 ok &= msiz==mlen;
uci1 12:d472f9811262 1025 // send number of power readings sent
uci1 12:d472f9811262 1026 b = genBuf;
uci1 12:d472f9811262 1027 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t));
uci1 12:d472f9811262 1028 b = SnBitUtils::WriteTo(b, powsSent);
uci1 12:d472f9811262 1029 msiz = b - genBuf;
uci1 12:d472f9811262 1030 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 12:d472f9811262 1031 ok &= msiz==mlen;
uci1 8:95a325df1f6b 1032 }
uci1 12:d472f9811262 1033 } else {
uci1 12:d472f9811262 1034 printf("failed to get config from file\r\n");
uci1 12:d472f9811262 1035 // send corrupted file handshake
uci1 8:95a325df1f6b 1036 b = genBuf;
uci1 12:d472f9811262 1037 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileReadFailCode, 0u);
uci1 12:d472f9811262 1038 msiz = b-genBuf;
uci1 8:95a325df1f6b 1039 mlen = SendAll(genBuf, msiz, timeout_clock);
uci1 8:95a325df1f6b 1040 ok &= msiz==mlen;
uci1 8:95a325df1f6b 1041 }
uci1 12:d472f9811262 1042
uci1 8:95a325df1f6b 1043 // put file position back
uci1 8:95a325df1f6b 1044 fseek(inf, fpos, SEEK_SET);
uci1 8:95a325df1f6b 1045
uci1 8:95a325df1f6b 1046 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 1047 }
uci1 12:d472f9811262 1048 */