Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Jun 05 17:29:31 2019 +0000
Revision:
125:ce4045184366
Parent:
122:c1b5023eac69
Added SnRateListner proto-class, publishing this version of the code in order to enable exporting of most recent features.

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 41:d6f5e2f09e07 8 #include "SnSignalStrengthFrame.h"
uci1 2:e67f7c158087 9 #include "SnSDUtils.h"
uci1 16:744ce85aede2 10 #include "SnConstants.h"
uci1 21:ce51bb0ba4a5 11 #include "SnCRCUtils.h"
uci1 37:ff95e7070f26 12 #include "SnCommPeripheral.h"
uci1 84:80b15993944e 13 #include "SnClockSetFrame.h"
uci1 84:80b15993944e 14 #include "SnHeartbeatFrame.h"
uci1 21:ce51bb0ba4a5 15
uci1 21:ce51bb0ba4a5 16 #include <algorithm>
uci1 21:ce51bb0ba4a5 17 #include <ctype.h>
uci1 21:ce51bb0ba4a5 18 extern "C" void mbed_reset();
uci1 16:744ce85aede2 19
uci1 65:2cb3e99ce466 20 //#define DEBUG
uci1 16:744ce85aede2 21
uci1 27:efc4d654b139 22 const char* SnCommWin::kLocalDir = "/local";
uci1 27:efc4d654b139 23 const char* SnCommWin::kDelAllConfCodeStr = "fj2io32FIJ(#jd;;.O@++/]ewavk2[49ojv";
uci1 27:efc4d654b139 24 const uint8_t SnCommWin::kDelAllConfCodeLen = 35; // length of the above string
uci1 25:57b2627fe756 25
uci1 37:ff95e7070f26 26 SnCommWin::SnCommWin(SnCommPeripheral* p) :
uci1 56:0bba0ef15697 27 fSendingInHandshake(false), fComm(p) {
uci1 37:ff95e7070f26 28 if (p==0) {
uci1 37:ff95e7070f26 29 error("SnCommWin - must have peripheral! Received 0.\r\n");
uci1 37:ff95e7070f26 30 }
uci1 37:ff95e7070f26 31 }
uci1 37:ff95e7070f26 32
uci1 37:ff95e7070f26 33 SnCommWin::~SnCommWin() {
uci1 37:ff95e7070f26 34 delete fComm;
uci1 37:ff95e7070f26 35 }
uci1 37:ff95e7070f26 36
uci1 40:1324da35afd4 37 bool SnCommWin::TrySetSysTimeUnix(const uint32_t timeout,
uci1 40:1324da35afd4 38 uint32_t& prvTime,
uci1 40:1324da35afd4 39 uint32_t& setTime) {
uci1 40:1324da35afd4 40 return (fComm!=0) ? (fComm->TrySetSysTimeUnix(timeout,prvTime,setTime))
uci1 40:1324da35afd4 41 : false;
uci1 37:ff95e7070f26 42 }
uci1 37:ff95e7070f26 43
uci1 37:ff95e7070f26 44 bool SnCommWin::Connect(const uint32_t timeout) {
uci1 64:6c7a316eafad 45 Watchdog::kick(); // don't reset
uci1 37:ff95e7070f26 46 return (fComm!=0) ? (fComm->Connect(timeout)) : false;
uci1 37:ff95e7070f26 47 }
uci1 37:ff95e7070f26 48
uci1 116:8099b754fbb4 49 bool SnCommWin::CloseConn(const uint32_t timeout,
uci1 116:8099b754fbb4 50 char* const genBuf/*=0*/,
uci1 116:8099b754fbb4 51 const bool sendCloseSignal/*=false*/) {
uci1 64:6c7a316eafad 52 Watchdog::kick(); // don't reset
uci1 116:8099b754fbb4 53 bool closeOk = false;
uci1 116:8099b754fbb4 54 if (fComm!=0) {
uci1 116:8099b754fbb4 55 if (sendCloseSignal && (genBuf!=0)) {
uci1 116:8099b754fbb4 56 char* b = genBuf;
uci1 116:8099b754fbb4 57 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kCommSignOffCode, 0);
uci1 116:8099b754fbb4 58 const uint32_t bytesToBeSent = b-genBuf;
uci1 116:8099b754fbb4 59 const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout);
uci1 116:8099b754fbb4 60 const int32_t flen = fComm->FinishSending(timeout);
uci1 116:8099b754fbb4 61 closeOk = (bytesToBeSent==(mlen+flen));
uci1 116:8099b754fbb4 62 } else {
uci1 116:8099b754fbb4 63 closeOk = true;
uci1 116:8099b754fbb4 64 }
uci1 116:8099b754fbb4 65 closeOk &= fComm->CloseConn(timeout);
uci1 116:8099b754fbb4 66 }
uci1 116:8099b754fbb4 67 return closeOk;
uci1 12:d472f9811262 68 }
uci1 12:d472f9811262 69
uci1 40:1324da35afd4 70 bool SnCommWin::PowerDown(const uint32_t timeout) {
uci1 64:6c7a316eafad 71 Watchdog::kick(); // don't reset
uci1 40:1324da35afd4 72 return (fComm!=0) ? (fComm->PowerDown(timeout)) : false;
uci1 40:1324da35afd4 73 }
uci1 40:1324da35afd4 74
uci1 2:e67f7c158087 75 SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf,
uci1 2:e67f7c158087 76 SnEventFrame& evt,
uci1 8:95a325df1f6b 77 SnPowerFrame& pow,
uci1 2:e67f7c158087 78 char* const genBuf,
uci1 3:24c5f0f50bf1 79 const uint32_t bsize,
uci1 40:1324da35afd4 80 const uint32_t timeout) {
uci1 12:d472f9811262 81 #ifdef DEBUG
uci1 3:24c5f0f50bf1 82 printf("SnCommWin::SendData\r\n");
uci1 12:d472f9811262 83 #endif
uci1 64:6c7a316eafad 84 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 85
uci1 2:e67f7c158087 86 ECommWinResult res = kUndefFail;
uci1 15:f2569d8e4176 87 if ( (GetCommType() != SnConfigFrame::kIrid) ||
uci1 15:f2569d8e4176 88 (conf.IsForcingSBDdata()) ) {
uci1 15:f2569d8e4176 89 // only send large amounts if we're not communicating by Iridum,
uci1 15:f2569d8e4176 90 // or if we are being forced to send the data over SBD
uci1 15:f2569d8e4176 91 if (conf.IsSendingAllFiles()) {
uci1 12:d472f9811262 92 #ifdef DEBUG
uci1 15:f2569d8e4176 93 printf("sending all files\r\n");
uci1 12:d472f9811262 94 #endif
uci1 15:f2569d8e4176 95 res = SnSDUtils::SendAllFiles(this, timeout,
uci1 40:1324da35afd4 96 genBuf, bsize, conf, evt, pow);
uci1 40:1324da35afd4 97 // handshakes handled after each file, in SendDataFromFile
uci1 15:f2569d8e4176 98 } else {
uci1 15:f2569d8e4176 99 if (conf.GetCommSendData()==0) {
uci1 12:d472f9811262 100 #ifdef DEBUG
uci1 15:f2569d8e4176 101 printf("no data to send\r\n");
uci1 12:d472f9811262 102 #endif
uci1 15:f2569d8e4176 103 res = kOkNoMsg;
uci1 56:0bba0ef15697 104 } else if (conf.IsSendingFilesRunSeqList()) {
uci1 56:0bba0ef15697 105 #ifdef DEBUG
uci1 56:0bba0ef15697 106 printf ("calling SendFilesInRunSeqList\r\n");
uci1 56:0bba0ef15697 107 #endif
uci1 56:0bba0ef15697 108 res = SnSDUtils::SendFilesInRunSeqList(
uci1 56:0bba0ef15697 109 this, timeout,
uci1 56:0bba0ef15697 110 genBuf, bsize, conf, evt, pow);
uci1 56:0bba0ef15697 111 #ifdef DEBUG
uci1 56:0bba0ef15697 112 printf ("res = %d after SendFilesInRunSeqList\r\n",
uci1 56:0bba0ef15697 113 static_cast<int>(res));
uci1 56:0bba0ef15697 114 #endif
uci1 15:f2569d8e4176 115 } else {
uci1 56:0bba0ef15697 116 // send most recent file or a few events from that file
uci1 15:f2569d8e4176 117 const uint32_t nev = (conf.GetCommSendData()>0) ?
uci1 15:f2569d8e4176 118 conf.GetCommSendData() // send N events
uci1 15:f2569d8e4176 119 : 0u; // send all events in last file
uci1 12:d472f9811262 120 #ifdef DEBUG
uci1 40:1324da35afd4 121 printf("calling SendDataFromFile. f=%s, nev=%u\r\n",
uci1 40:1324da35afd4 122 SnSDUtils::GetCurFileName(), nev);
uci1 12:d472f9811262 123 #endif
uci1 40:1324da35afd4 124 uint8_t hndcode(0);
uci1 40:1324da35afd4 125 uint32_t hndlen(0);
uci1 40:1324da35afd4 126 res = SendDataFromFile(SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(),
uci1 40:1324da35afd4 127 conf, evt, pow, genBuf, bsize, nev, timeout,
uci1 40:1324da35afd4 128 &hndcode, &hndlen);
uci1 40:1324da35afd4 129 // handshake handled in SendData
uci1 12:d472f9811262 130 #ifdef DEBUG
uci1 15:f2569d8e4176 131 printf("after send data cur file, res=%d\r\n",(int)res);
uci1 12:d472f9811262 132 #endif
uci1 15:f2569d8e4176 133 }
uci1 2:e67f7c158087 134 }
uci1 15:f2569d8e4176 135 } else {
uci1 15:f2569d8e4176 136 return kOkNoMsg;
uci1 2:e67f7c158087 137 }
uci1 2:e67f7c158087 138 return res;
uci1 2:e67f7c158087 139 }
uci1 2:e67f7c158087 140
uci1 40:1324da35afd4 141 SnCommWin::ECommWinResult SnCommWin::SendDataFromFile(FILE* inf, const char* infn,
uci1 2:e67f7c158087 142 const SnConfigFrame& curConf,
uci1 2:e67f7c158087 143 SnEventFrame& evt,
uci1 8:95a325df1f6b 144 SnPowerFrame& pow,
uci1 2:e67f7c158087 145 char* const genBuf,
uci1 6:6f002d202f59 146 const uint32_t bsize,
uci1 2:e67f7c158087 147 const uint32_t nevts,
uci1 40:1324da35afd4 148 const uint32_t timeout,
uci1 40:1324da35afd4 149 uint8_t* hndcode,
uci1 40:1324da35afd4 150 uint32_t* hndlen) {
uci1 2:e67f7c158087 151 // nevts==0 ==> send all events
uci1 21:ce51bb0ba4a5 152 // handshakeTimeout is never a clock time -- always a delta(t)
uci1 2:e67f7c158087 153
uci1 12:d472f9811262 154 #ifdef DEBUG
uci1 40:1324da35afd4 155 printf("SnCommWin::SendDataFromFile file (%p), fn=%s\r\n",inf,infn);
uci1 12:d472f9811262 156 #endif
uci1 64:6c7a316eafad 157 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 158
uci1 2:e67f7c158087 159 ECommWinResult res = kUndefFail;
uci1 12:d472f9811262 160 bool didDel = false;
uci1 40:1324da35afd4 161
uci1 40:1324da35afd4 162 // allow up to the full window if we're not obeying timeout
uci1 40:1324da35afd4 163 const uint32_t timeout_clock = curConf.IsObeyingTimeout() ?
uci1 40:1324da35afd4 164 timeout :
uci1 40:1324da35afd4 165 curConf.GetTimeoutTime(time(0),
uci1 40:1324da35afd4 166 curConf.GetCommWinDuration());
uci1 40:1324da35afd4 167
uci1 40:1324da35afd4 168
uci1 2:e67f7c158087 169 if (inf!=0) {
uci1 12:d472f9811262 170 #ifdef DEBUG
uci1 12:d472f9811262 171 printf("sending file name %s\r\n",infn);
uci1 12:d472f9811262 172 #endif
uci1 40:1324da35afd4 173
uci1 6:6f002d202f59 174 // send the file name
uci1 40:1324da35afd4 175 int32_t btogo(0), ttogo(0);
uci1 40:1324da35afd4 176 int32_t bsent = SendFilename(infn, genBuf, btogo, timeout_clock);
uci1 40:1324da35afd4 177 if (genBuf!=0) {
uci1 12:d472f9811262 178 #ifdef DEBUG
uci1 40:1324da35afd4 179 printf("calling send file contents\r\n");
uci1 12:d472f9811262 180 #endif
uci1 16:744ce85aede2 181
uci1 40:1324da35afd4 182 // send the contents
uci1 40:1324da35afd4 183 bsent += SendFileContents(inf, curConf, evt, pow, genBuf,
uci1 40:1324da35afd4 184 nevts, ttogo, timeout_clock);
uci1 40:1324da35afd4 185 btogo += ttogo;
uci1 40:1324da35afd4 186 bsent += fComm->FinishSending(timeout_clock);
uci1 40:1324da35afd4 187 res = (bsent==btogo) ? SnCommWin::kOkMsgSent
uci1 40:1324da35afd4 188 : SnCommWin::kFailPartSent;
uci1 12:d472f9811262 189 #ifdef DEBUG
uci1 40:1324da35afd4 190 printf("res=%d (fails=%d)\r\n",(int)res,(int)kAllFails);
uci1 12:d472f9811262 191 #endif
uci1 40:1324da35afd4 192 if (res>kAllFails) {
uci1 12:d472f9811262 193 #ifdef DEBUG
uci1 40:1324da35afd4 194 printf("calling wait handshake\r\n");
uci1 12:d472f9811262 195 #endif
uci1 64:6c7a316eafad 196 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 197
uci1 40:1324da35afd4 198 // wait for handshake
uci1 40:1324da35afd4 199 uint8_t hndshk(0); uint32_t hndshkLen(0);
uci1 40:1324da35afd4 200 res = WaitHandshake(curConf, timeout_clock, genBuf, bsize, hndshk,
uci1 40:1324da35afd4 201 &hndshkLen);
uci1 40:1324da35afd4 202 if (hndcode!=0) {
uci1 40:1324da35afd4 203 *hndcode = hndshk;
uci1 40:1324da35afd4 204 }
uci1 40:1324da35afd4 205 if (hndlen!=0) {
uci1 40:1324da35afd4 206 *hndlen = hndshkLen;
uci1 40:1324da35afd4 207 }
uci1 26:b3180073c8ec 208 #ifdef DEBUG
uci1 40:1324da35afd4 209 printf("res=%d, nevts=%u, isdel=%d, inf=%p, curfile=%p, hndshk=%02x\r\n",
uci1 40:1324da35afd4 210 res, nevts, curConf.IsDeletingFiles(), inf,
uci1 40:1324da35afd4 211 SnSDUtils::GetCurFile(), hndshk);
uci1 21:ce51bb0ba4a5 212 #endif
uci1 40:1324da35afd4 213 if (SnCommWin::kOkWithMsg==res) {
uci1 56:0bba0ef15697 214 res = HandleHandshake(
uci1 56:0bba0ef15697 215 //SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(),
uci1 56:0bba0ef15697 216 inf, infn,
uci1 40:1324da35afd4 217 curConf, evt, pow, genBuf, bsize, timeout_clock,
uci1 40:1324da35afd4 218 hndshk, hndshkLen, &nevts);
uci1 40:1324da35afd4 219 didDel = (SnCommWin::kOkWthMsgDidDel==res);
uci1 40:1324da35afd4 220 }
uci1 2:e67f7c158087 221 }
uci1 40:1324da35afd4 222 } // if genBuf!=0
uci1 12:d472f9811262 223 } else {
uci1 12:d472f9811262 224 #ifdef DEBUG
uci1 12:d472f9811262 225 printf("inf=0!\r\n");
uci1 12:d472f9811262 226 #endif
uci1 56:0bba0ef15697 227 res = kOkNoMsg;
uci1 12:d472f9811262 228 }
uci1 12:d472f9811262 229 if ( (inf!=SnSDUtils::GetCurFile()) && (didDel==false) ) {
uci1 12:d472f9811262 230 SnSDUtils::CloseOutputFile(inf);
uci1 2:e67f7c158087 231 }
uci1 37:ff95e7070f26 232 if ( fComm->IsTimedOut(timeout_clock) ) {
uci1 25:57b2627fe756 233 if ( kFailTimeout < res ) {
uci1 25:57b2627fe756 234 res = kFailTimeout;
uci1 25:57b2627fe756 235 }
uci1 25:57b2627fe756 236 }
uci1 2:e67f7c158087 237 return res;
uci1 2:e67f7c158087 238 }
uci1 8:95a325df1f6b 239
uci1 40:1324da35afd4 240 SnCommWin::ECommWinResult SnCommWin::HandleHandshake(FILE* inf, const char* infn,
uci1 40:1324da35afd4 241 const SnConfigFrame& curConf,
uci1 40:1324da35afd4 242 SnEventFrame& evt,
uci1 40:1324da35afd4 243 SnPowerFrame& pow,
uci1 40:1324da35afd4 244 char* const genBuf,
uci1 40:1324da35afd4 245 const uint32_t bsize,
uci1 40:1324da35afd4 246 const uint32_t handshakeTimeout,
uci1 40:1324da35afd4 247 const uint8_t hndshk,
uci1 40:1324da35afd4 248 const uint32_t hndlen,
uci1 40:1324da35afd4 249 const uint32_t* nevts) {
uci1 40:1324da35afd4 250
uci1 40:1324da35afd4 251 SnCommWin::ECommWinResult res = SnCommWin::kUnexpectedRec;
uci1 40:1324da35afd4 252
uci1 64:6c7a316eafad 253 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 254
uci1 40:1324da35afd4 255 if ( (hndshk==SnHeaderFrame::kHnShNoReplyCode)
uci1 40:1324da35afd4 256 || (hndshk==SnHeaderFrame::kHnShOkPartlCode) ) {
uci1 40:1324da35afd4 257 // nothing to do
uci1 40:1324da35afd4 258 res = SnCommWin::kOkWithMsg;
uci1 40:1324da35afd4 259 } else if (hndshk==SnHeaderFrame::kHnShFailNonCode) {
uci1 40:1324da35afd4 260 res = SnCommWin::kFailNoneSent;
uci1 40:1324da35afd4 261 } else if (hndshk==SnHeaderFrame::kHnShFailPrtCode) {
uci1 40:1324da35afd4 262 res = SnCommWin::kFailPartSent;
uci1 40:1324da35afd4 263 } else if (hndshk==SnHeaderFrame::kHnShOkComplCode) {
uci1 40:1324da35afd4 264 // file received
uci1 56:0bba0ef15697 265 #ifdef DEBUG
uci1 56:0bba0ef15697 266 printf("HnShOkCompl: isdel=%d, isCurFil=%d (%p,%p), nevts_ptr=%p\r\n",
uci1 56:0bba0ef15697 267 (int)(curConf.IsDeletingFiles()),
uci1 56:0bba0ef15697 268 (int)(inf==SnSDUtils::GetCurFile()),
uci1 56:0bba0ef15697 269 (void*)inf, (void*)(SnSDUtils::GetCurFile()),
uci1 56:0bba0ef15697 270 (void*)(nevts));
uci1 56:0bba0ef15697 271 if (nevts!=0) {
uci1 56:0bba0ef15697 272 printf("*nevts=%u\r\n",*nevts);
uci1 56:0bba0ef15697 273 }
uci1 56:0bba0ef15697 274 #endif
uci1 40:1324da35afd4 275 if ( (curConf.IsDeletingFiles())
uci1 40:1324da35afd4 276 && (inf!=SnSDUtils::GetCurFile())
uci1 40:1324da35afd4 277 && ((nevts!=0) && (*nevts==0)) ) { // whole file was received
uci1 40:1324da35afd4 278 // delete this file
uci1 40:1324da35afd4 279 #ifdef DEBUG
uci1 40:1324da35afd4 280 printf("deleting file %p, %s\r\n",inf,infn);
uci1 40:1324da35afd4 281 #endif
uci1 40:1324da35afd4 282 SnSDUtilsWhisperer::DeleteFile(inf, infn);
uci1 40:1324da35afd4 283 res = SnCommWin::kOkWthMsgDidDel;
uci1 40:1324da35afd4 284 } else {
uci1 40:1324da35afd4 285 res = SnCommWin::kOkWithMsg;
uci1 40:1324da35afd4 286 }
uci1 40:1324da35afd4 287 } else if ( hndshk==SnHeaderFrame::kHnShOkStopCode ) {
uci1 40:1324da35afd4 288 res = SnCommWin::kOkStopComm;
uci1 40:1324da35afd4 289 } else if ( (hndshk==SnHeaderFrame::kHnShOkDelAlCode) ||
uci1 40:1324da35afd4 290 (hndshk==SnHeaderFrame::kHnShOkDelRnCode) ) {
uci1 40:1324da35afd4 291 if ( GetDeleteAllConfirmCode(curConf,
uci1 40:1324da35afd4 292 kDelAllConfCodeLen,
uci1 40:1324da35afd4 293 handshakeTimeout,
uci1 40:1324da35afd4 294 genBuf, bsize) ) {
uci1 40:1324da35afd4 295 if (hndshk==SnHeaderFrame::kHnShOkDelAlCode) {
uci1 40:1324da35afd4 296 #ifdef DEBUG
uci1 40:1324da35afd4 297 printf("delete all\r\n");
uci1 40:1324da35afd4 298 #endif
uci1 40:1324da35afd4 299 SnSDUtilsWhisperer::DeleteAllFiles();
uci1 40:1324da35afd4 300 } else if (hndshk==SnHeaderFrame::kHnShOkDelRnCode) {
uci1 40:1324da35afd4 301 #ifdef DEBUG
uci1 40:1324da35afd4 302 printf("delete run %u\r\n",hndlen);
uci1 40:1324da35afd4 303 #endif
uci1 40:1324da35afd4 304 SnSDUtilsWhisperer::DeleteFilesOfRun(hndlen);
uci1 40:1324da35afd4 305 }
uci1 40:1324da35afd4 306 res = SnCommWin::kOkStopComm;
uci1 40:1324da35afd4 307 }
uci1 56:0bba0ef15697 308 } else if ( hndshk==SnHeaderFrame::kHnShOkClRSLCode ) {
uci1 56:0bba0ef15697 309 #ifdef DEBUG
uci1 56:0bba0ef15697 310 printf("clear run seq list requested\r\n");
uci1 56:0bba0ef15697 311 #endif
uci1 76:f8383f0292c2 312 SnSDUtils::ClearRunSeqList(curConf.IsSendingFilesRunSeqList());
uci1 40:1324da35afd4 313 } else if ( hndshk==SnHeaderFrame::kHnShOkReqRnCode ) {
uci1 40:1324da35afd4 314 #ifdef DEBUG
uci1 40:1324da35afd4 315 printf("run %u requested\r\n",hndlen);
uci1 40:1324da35afd4 316 #endif
uci1 56:0bba0ef15697 317 if (fSendingInHandshake==false) {
uci1 56:0bba0ef15697 318 // prevent another SendAllOfRun handshake request until we're done
uci1 56:0bba0ef15697 319 // (allowing it could lead to a stack overflow)
uci1 56:0bba0ef15697 320 fSendingInHandshake = true;
uci1 56:0bba0ef15697 321 SnSDUtils::SendAllOfRun(this, handshakeTimeout,
uci1 56:0bba0ef15697 322 genBuf, bsize, curConf, evt, pow,
uci1 56:0bba0ef15697 323 hndlen);
uci1 56:0bba0ef15697 324 fSendingInHandshake = false;
uci1 56:0bba0ef15697 325 res = SnCommWin::kOkStopComm;
uci1 56:0bba0ef15697 326 }
uci1 56:0bba0ef15697 327 } else if ( hndshk==SnHeaderFrame::kHnShOkRqPtRnCode ) {
uci1 56:0bba0ef15697 328 #ifdef DEBUG
uci1 56:0bba0ef15697 329 printf("part of run %u requested\r\n",hndlen);
uci1 56:0bba0ef15697 330 #endif
uci1 56:0bba0ef15697 331 // need the seq limits -- another handshake message
uci1 56:0bba0ef15697 332 SendHndshkReq(genBuf, handshakeTimeout);
uci1 56:0bba0ef15697 333 uint8_t shndshk(0); uint32_t shndshkLen(0);
uci1 56:0bba0ef15697 334 res = WaitHandshake(curConf, handshakeTimeout, genBuf, bsize, shndshk,
uci1 56:0bba0ef15697 335 &shndshkLen);
uci1 56:0bba0ef15697 336 if (SnCommWin::kOkWithMsg==res) {
uci1 56:0bba0ef15697 337 if ( shndshk==SnHeaderFrame::kHnShOkRqPtSqCode ) {
uci1 56:0bba0ef15697 338 // ok got the min/max seq's as expected
uci1 56:0bba0ef15697 339 const uint16_t minseq = GetMinSeqFrom(shndshkLen);
uci1 56:0bba0ef15697 340 const uint16_t maxseq = GetMaxSeqFrom(shndshkLen);
uci1 56:0bba0ef15697 341 #ifdef DEBUG
uci1 56:0bba0ef15697 342 printf("[minseq=%hu, maxseq=%hu]\r\n",minseq,maxseq);
uci1 56:0bba0ef15697 343 #endif
uci1 56:0bba0ef15697 344 if (fSendingInHandshake==false) {
uci1 56:0bba0ef15697 345 // prevent another SendAllOfRun handshake request
uci1 56:0bba0ef15697 346 // until we're done
uci1 56:0bba0ef15697 347 // (allowing it could lead to a stack overflow)
uci1 56:0bba0ef15697 348 fSendingInHandshake = true;
uci1 56:0bba0ef15697 349 SnSDUtils::SendPartOfRun(this, handshakeTimeout,
uci1 56:0bba0ef15697 350 genBuf, bsize, curConf, evt, pow,
uci1 56:0bba0ef15697 351 hndlen, // the run num
uci1 56:0bba0ef15697 352 minseq, maxseq);
uci1 56:0bba0ef15697 353 fSendingInHandshake = false;
uci1 56:0bba0ef15697 354 }
uci1 56:0bba0ef15697 355 }
uci1 56:0bba0ef15697 356 }
uci1 40:1324da35afd4 357 res = SnCommWin::kOkStopComm;
uci1 40:1324da35afd4 358 }
uci1 40:1324da35afd4 359
uci1 40:1324da35afd4 360 return res;
uci1 40:1324da35afd4 361 }
uci1 40:1324da35afd4 362
uci1 40:1324da35afd4 363
uci1 27:efc4d654b139 364 bool SnCommWin::GetDeleteAllConfirmCode(const SnConfigFrame& conf,
uci1 27:efc4d654b139 365 const uint32_t length,
uci1 27:efc4d654b139 366 const uint32_t timeout,
uci1 27:efc4d654b139 367 char* const buf,
uci1 27:efc4d654b139 368 const uint32_t bsize) {
uci1 27:efc4d654b139 369 bool ret = false;
uci1 27:efc4d654b139 370 #ifdef DEBUG
uci1 40:1324da35afd4 371 printf("GetDeleteAllConfirmCode, timeout=%u\r\n",timeout);
uci1 27:efc4d654b139 372 #endif
uci1 64:6c7a316eafad 373 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 374
uci1 27:efc4d654b139 375 // better pull all the bytes no matter the buffer size
uci1 27:efc4d654b139 376 const uint32_t ll = (bsize<length) ? bsize : length;
uci1 27:efc4d654b139 377 int mlen = 0;
uci1 27:efc4d654b139 378 while ( mlen<length ) {
uci1 40:1324da35afd4 379 mlen += fComm->ReceiveAll(buf, ll, timeout);
uci1 40:1324da35afd4 380 if (fComm->IsTimedOut(timeout)) {
uci1 27:efc4d654b139 381 break;
uci1 27:efc4d654b139 382 }
uci1 27:efc4d654b139 383 }
uci1 64:6c7a316eafad 384
uci1 64:6c7a316eafad 385 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 386
uci1 27:efc4d654b139 387 if (mlen==length) {
uci1 27:efc4d654b139 388 const char* b = buf;
uci1 27:efc4d654b139 389 const char* c = kDelAllConfCodeStr;
uci1 27:efc4d654b139 390 bool match = true;
uci1 27:efc4d654b139 391 for (uint8_t i=0; (i<kDelAllConfCodeLen) && match; ++i, ++b, ++c) {
uci1 27:efc4d654b139 392 match = ((*b)==(*c));
uci1 27:efc4d654b139 393 }
uci1 27:efc4d654b139 394 ret = match;
uci1 27:efc4d654b139 395 }
uci1 27:efc4d654b139 396 return ret;
uci1 27:efc4d654b139 397 }
uci1 27:efc4d654b139 398
uci1 21:ce51bb0ba4a5 399 SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const SnConfigFrame& conf,
uci1 21:ce51bb0ba4a5 400 const uint32_t timeout,
uci1 8:95a325df1f6b 401 char* const buf,
uci1 8:95a325df1f6b 402 const uint32_t bsize,
uci1 27:efc4d654b139 403 uint8_t& hndShkCode,
uci1 27:efc4d654b139 404 uint32_t* hndShkLen) {
uci1 12:d472f9811262 405 #ifdef DEBUG
uci1 40:1324da35afd4 406 printf("WaitHandshake, timeout=%u\r\n",timeout);
uci1 12:d472f9811262 407 #endif
uci1 8:95a325df1f6b 408
uci1 64:6c7a316eafad 409 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 410
uci1 40:1324da35afd4 411 const int mlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeout);
uci1 8:95a325df1f6b 412 if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) {
uci1 8:95a325df1f6b 413 uint32_t msgLen=0;
uci1 8:95a325df1f6b 414 const char* b = buf;
uci1 8:95a325df1f6b 415 SnHeaderFrame::ReadFrom(b, hndShkCode, msgLen);
uci1 12:d472f9811262 416 #ifdef DEBUG
uci1 12:d472f9811262 417 printf("got handshake code=%02x, len=%u\r\n",hndShkCode,msgLen);
uci1 12:d472f9811262 418 #endif
uci1 8:95a325df1f6b 419 if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) {
uci1 27:efc4d654b139 420 if (hndShkLen!=0) {
uci1 27:efc4d654b139 421 *hndShkLen = msgLen;
uci1 27:efc4d654b139 422 }
uci1 8:95a325df1f6b 423 return SnCommWin::kOkWithMsg;
uci1 8:95a325df1f6b 424 } else {
uci1 8:95a325df1f6b 425 // TODO: somehow handle unexpected message?
uci1 8:95a325df1f6b 426 return SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 427 }
uci1 8:95a325df1f6b 428 }
uci1 8:95a325df1f6b 429 return SnCommWin::kOkNoMsg;
uci1 8:95a325df1f6b 430 }
uci1 8:95a325df1f6b 431
uci1 21:ce51bb0ba4a5 432 SnCommWin::ECommWinResult SnCommWin::GetFilename(const uint32_t timeout,
uci1 21:ce51bb0ba4a5 433 char* const buf,
uci1 21:ce51bb0ba4a5 434 const uint32_t namelen) {
uci1 21:ce51bb0ba4a5 435 // called by GetConfig upon receipt of a kMbedFilenameCode
uci1 21:ce51bb0ba4a5 436
uci1 21:ce51bb0ba4a5 437 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 438 printf("GetMbedFile, to=%u\r\n",timeout);
uci1 21:ce51bb0ba4a5 439 #endif
uci1 64:6c7a316eafad 440
uci1 64:6c7a316eafad 441 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 442
uci1 37:ff95e7070f26 443 const int mlen = fComm->ReceiveAll(buf, namelen, timeout);
uci1 21:ce51bb0ba4a5 444 if (mlen>0 && static_cast<uint32_t>(mlen) == namelen) {
uci1 21:ce51bb0ba4a5 445 return SnCommWin::kOkWithMsg;
uci1 21:ce51bb0ba4a5 446 }
uci1 21:ce51bb0ba4a5 447 return SnCommWin::kUnexpectedRec;
uci1 21:ce51bb0ba4a5 448 }
uci1 21:ce51bb0ba4a5 449
uci1 122:c1b5023eac69 450 bool SnCommWin::BuildLocalFileName(std::string fname,
uci1 122:c1b5023eac69 451 const char* const dir,
uci1 122:c1b5023eac69 452 std::string& lfname) {
uci1 122:c1b5023eac69 453 // make the file name ALLCAPS, since LocalFileSystem will do it anyway
uci1 122:c1b5023eac69 454 SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end());
uci1 122:c1b5023eac69 455 // now ensure the file name is 8.3 only -- for LocalFileSystem
uci1 122:c1b5023eac69 456 const size_t ldlen = strlen(dir);
uci1 122:c1b5023eac69 457 // 12 = 8.3 filename format, 1 for / and 1 for \0
uci1 122:c1b5023eac69 458 const uint32_t fbs = ldlen+1+12+1;
uci1 122:c1b5023eac69 459 char fnb[fbs];
uci1 122:c1b5023eac69 460 memset(fnb, 0, sizeof(char)*fbs);
uci1 122:c1b5023eac69 461
uci1 122:c1b5023eac69 462 Watchdog::kick(); // don't reset
uci1 122:c1b5023eac69 463
uci1 122:c1b5023eac69 464 // find the extension
uci1 122:c1b5023eac69 465 const size_t xdot = fname.rfind('.');
uci1 122:c1b5023eac69 466 if (xdot!=string::npos && (fname.size()==(4+xdot))) {
uci1 122:c1b5023eac69 467 const size_t fnpl = (xdot>8) ? 8 : xdot;
uci1 122:c1b5023eac69 468 char* fb = fnb;
uci1 122:c1b5023eac69 469 memcpy(fb, dir, ldlen); fb+=ldlen; // /local
uci1 122:c1b5023eac69 470 *fb = '/'; ++fb; // /
uci1 122:c1b5023eac69 471 strncpy(fb, fname.c_str(), fnpl); fb+=fnpl; // FILENAME
uci1 122:c1b5023eac69 472 *fb = '.'; ++fb; // .
uci1 122:c1b5023eac69 473 strncpy(fb, fname.c_str()+xdot+1, 3); // EXT
uci1 122:c1b5023eac69 474 // success
uci1 122:c1b5023eac69 475 lfname = fnb;
uci1 122:c1b5023eac69 476 return true;
uci1 122:c1b5023eac69 477 }
uci1 122:c1b5023eac69 478 #ifdef DEBUG
uci1 122:c1b5023eac69 479 printf("filname mangled. (%s) size=%u, xdot=%u",
uci1 122:c1b5023eac69 480 fname.c_str(), fname.size(), xdot);
uci1 122:c1b5023eac69 481 #endif
uci1 122:c1b5023eac69 482 return false; // fail
uci1 122:c1b5023eac69 483 }
uci1 122:c1b5023eac69 484
uci1 21:ce51bb0ba4a5 485 SnCommWin::ECommWinResult SnCommWin::GetLocalFile(std::string fname,
uci1 21:ce51bb0ba4a5 486 char* const buf,
uci1 21:ce51bb0ba4a5 487 const uint32_t bsize,
uci1 122:c1b5023eac69 488 const uint32_t timeout,
uci1 122:c1b5023eac69 489 std::string& lfname) {
uci1 21:ce51bb0ba4a5 490 // get a file and save it locally with the specified name
uci1 21:ce51bb0ba4a5 491
uci1 64:6c7a316eafad 492 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 493
uci1 21:ce51bb0ba4a5 494 // get the header for the file
uci1 21:ce51bb0ba4a5 495 uint8_t mcode=0; uint32_t mlen=0;
uci1 21:ce51bb0ba4a5 496 SnCommWin::ECommWinResult res =
uci1 21:ce51bb0ba4a5 497 GetHeader(timeout, buf, bsize, mcode, mlen);
uci1 54:ea1234a44fe8 498 #ifdef DEBUG
uci1 54:ea1234a44fe8 499 printf("Got header code %x, len %u\r\n",mcode,mlen);
uci1 54:ea1234a44fe8 500 #endif
uci1 54:ea1234a44fe8 501
uci1 21:ce51bb0ba4a5 502 if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileCode) ) {
uci1 21:ce51bb0ba4a5 503 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 504 printf("Got mbed file header. File length = %u\r\n",mlen);
uci1 21:ce51bb0ba4a5 505 #endif
uci1 21:ce51bb0ba4a5 506 // got the header.. make the file..
uci1 122:c1b5023eac69 507 if ( BuildLocalFileName(fname, kLocalDir, lfname) ) {
uci1 64:6c7a316eafad 508 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 509
uci1 40:1324da35afd4 510 // all that just for the file name!
uci1 122:c1b5023eac69 511 FILE* lf = fopen(lfname.c_str(),"wb");
uci1 21:ce51bb0ba4a5 512 #ifdef DEBUG
uci1 122:c1b5023eac69 513 printf("tried to open file [%s]. lf=%p\r\n",lfname.c_str(),(void*)lf);
uci1 21:ce51bb0ba4a5 514 #endif
uci1 40:1324da35afd4 515 // get all the data and dump it into the file
uci1 40:1324da35afd4 516 int b = 0, toget = 0;
uci1 40:1324da35afd4 517 while ( mlen>b ) {
uci1 40:1324da35afd4 518 if (fComm->IsTimedOut(timeout)) {
uci1 36:87865913ae6f 519 #ifdef DEBUG
uci1 40:1324da35afd4 520 printf("timeout while getting file\r\n");
uci1 36:87865913ae6f 521 #endif
uci1 40:1324da35afd4 522 res = kFailTimeout;
uci1 21:ce51bb0ba4a5 523 break;
uci1 21:ce51bb0ba4a5 524 }
uci1 40:1324da35afd4 525 toget = mlen - b;
uci1 40:1324da35afd4 526 if (toget>bsize) {
uci1 40:1324da35afd4 527 toget = bsize;
uci1 40:1324da35afd4 528 }
uci1 40:1324da35afd4 529 const int got = fComm->ReceiveAll(buf, toget, timeout);
uci1 40:1324da35afd4 530 if (lf!=NULL) {
uci1 36:87865913ae6f 531 #ifdef DEBUG
uci1 40:1324da35afd4 532 printf("writing %d bytes to file\r\n", got);
uci1 36:87865913ae6f 533 #endif
uci1 64:6c7a316eafad 534 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 535
uci1 40:1324da35afd4 536 SnBitUtils::WriteTo(lf, buf, got);
uci1 40:1324da35afd4 537 }
uci1 40:1324da35afd4 538 b += got;
uci1 40:1324da35afd4 539 } // file data receive loop
uci1 64:6c7a316eafad 540
uci1 64:6c7a316eafad 541 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 542
uci1 40:1324da35afd4 543 uint32_t crc=0;
uci1 40:1324da35afd4 544 if (lf!=NULL) {
uci1 40:1324da35afd4 545 // calculate the crc from what's actually in the file
uci1 40:1324da35afd4 546 fclose(lf); // to flush it
uci1 36:87865913ae6f 547 #ifdef DEBUG
uci1 122:c1b5023eac69 548 printf("fopen: %s\r\n",lfname.c_str());
uci1 36:87865913ae6f 549 #endif
uci1 122:c1b5023eac69 550 lf = fopen(lfname.c_str(),"rb");
uci1 76:f8383f0292c2 551 if (lf!=0) {
uci1 76:f8383f0292c2 552 fseek(lf, 0, SEEK_END);
uci1 76:f8383f0292c2 553 int32_t fend = ftell(lf);
uci1 76:f8383f0292c2 554 fseek(lf, 0, SEEK_SET);
uci1 76:f8383f0292c2 555 char c;
uci1 76:f8383f0292c2 556 for (int32_t i=0; i<fend; ++i) {
uci1 76:f8383f0292c2 557
uci1 76:f8383f0292c2 558 Watchdog::kick(); // don't reset
uci1 76:f8383f0292c2 559
uci1 76:f8383f0292c2 560 SnBitUtils::ReadFrom(lf, c);
uci1 76:f8383f0292c2 561 if (feof(lf)==0 && ferror(lf)==0) {
uci1 76:f8383f0292c2 562 crc = SnCRCUtils::update_crc32_xfer(crc, c);
uci1 76:f8383f0292c2 563 } else {
uci1 76:f8383f0292c2 564 break;
uci1 76:f8383f0292c2 565 }
uci1 40:1324da35afd4 566 }
uci1 76:f8383f0292c2 567 fclose(lf);
uci1 21:ce51bb0ba4a5 568 }
uci1 40:1324da35afd4 569 #ifdef DEBUG
uci1 40:1324da35afd4 570 printf("closed file. crc = %u\r\n", crc);
uci1 40:1324da35afd4 571 #endif
uci1 40:1324da35afd4 572 } // if output file not 0
uci1 40:1324da35afd4 573 #ifdef DEBUG
uci1 40:1324da35afd4 574 printf("mlen=%u, b=%d, eq=%s\r\n",mlen,b,
uci1 40:1324da35afd4 575 (mlen==b)?"true":"false");
uci1 40:1324da35afd4 576 #endif
uci1 40:1324da35afd4 577 if ( mlen!=b ) {
uci1 40:1324da35afd4 578 if (res > kUnexpectedRec) {
uci1 40:1324da35afd4 579 res = kUnexpectedRec;
uci1 40:1324da35afd4 580 } // otherwise it's already worse
uci1 40:1324da35afd4 581 } else {
uci1 64:6c7a316eafad 582
uci1 64:6c7a316eafad 583 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 584
uci1 40:1324da35afd4 585 // check that the file is ok
uci1 40:1324da35afd4 586 // get the checksum
uci1 40:1324da35afd4 587 res = GetHeader(timeout, buf, bsize, mcode, mlen);
uci1 40:1324da35afd4 588 if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileChksCode) ) {
uci1 40:1324da35afd4 589 #ifdef DEBUG
uci1 40:1324da35afd4 590 printf("received chksum=%u, crc=%u, eq=%s\r\n",
uci1 40:1324da35afd4 591 mlen,crc,(crc==mlen)?"true":"false");
uci1 40:1324da35afd4 592 #endif
uci1 40:1324da35afd4 593 if (crc != mlen) {
uci1 36:87865913ae6f 594 #ifdef DEBUG
uci1 40:1324da35afd4 595 printf("checksums differ! unexpected.\r\n");
uci1 40:1324da35afd4 596 #endif
uci1 40:1324da35afd4 597 res = kUnexpectedRec; // important!
uci1 40:1324da35afd4 598 }
uci1 40:1324da35afd4 599 }
uci1 40:1324da35afd4 600 } // end get remote file checksum
uci1 40:1324da35afd4 601
uci1 40:1324da35afd4 602 if (lf!=NULL) {
uci1 40:1324da35afd4 603 if ( res<kOkWithMsg ) {
uci1 40:1324da35afd4 604 // timeout, bad checksum, something else bad?
uci1 40:1324da35afd4 605 #ifdef DEBUG
uci1 122:c1b5023eac69 606 printf("removing %s\r\n",lfname.c_str());
uci1 36:87865913ae6f 607 #endif
uci1 122:c1b5023eac69 608 remove(lfname.c_str()); // delete the file
uci1 40:1324da35afd4 609 } else {
uci1 40:1324da35afd4 610 // if we got a new program, remove the old ones.
uci1 40:1324da35afd4 611 // (schedule myself for immediate de-resolution)
uci1 40:1324da35afd4 612 //
uci1 40:1324da35afd4 613 // first just count how many we would save
uci1 40:1324da35afd4 614 const uint16_t nSavedBins = LoopLocalDirBinFiles(false, fname);
uci1 36:87865913ae6f 615 #ifdef DEBUG
uci1 40:1324da35afd4 616 printf("nSavedBins=%hu",nSavedBins);
uci1 36:87865913ae6f 617 #endif
uci1 40:1324da35afd4 618 if (nSavedBins==1) {
uci1 40:1324da35afd4 619 // ok. new program will be the only one. remove the others
uci1 40:1324da35afd4 620 // hope the new program works!
uci1 40:1324da35afd4 621 LoopLocalDirBinFiles(true, fname);
uci1 36:87865913ae6f 622
uci1 36:87865913ae6f 623 #ifdef DEBUG
uci1 40:1324da35afd4 624 printf("rebooting\r\n");
uci1 36:87865913ae6f 625 #endif
uci1 40:1324da35afd4 626 // goodbye cruel world, it's over. walk on by...
uci1 40:1324da35afd4 627 mbed_reset();
uci1 40:1324da35afd4 628 }
uci1 40:1324da35afd4 629 } // end check new program block
uci1 40:1324da35afd4 630 } // if output file not 0
uci1 40:1324da35afd4 631 } else {
uci1 40:1324da35afd4 632 // filename mangled. either doesn't contain a '.'
uci1 40:1324da35afd4 633 // or the extension is longer than 3 letters
uci1 40:1324da35afd4 634 res = SnCommWin::kUnexpectedRec;
uci1 21:ce51bb0ba4a5 635 }
uci1 40:1324da35afd4 636 } // if header ok
uci1 54:ea1234a44fe8 637
uci1 21:ce51bb0ba4a5 638 return res;
uci1 21:ce51bb0ba4a5 639 }
uci1 21:ce51bb0ba4a5 640
uci1 21:ce51bb0ba4a5 641 int16_t SnCommWin::LoopLocalDirBinFiles(const bool doRemove,
uci1 21:ce51bb0ba4a5 642 const std::string& fname) {
uci1 21:ce51bb0ba4a5 643 // loop over the local directory, count the BIN files that match 'fname'
uci1 21:ce51bb0ba4a5 644 //
uci1 21:ce51bb0ba4a5 645 // fname is assumed to be all caps already!
uci1 21:ce51bb0ba4a5 646 //
uci1 21:ce51bb0ba4a5 647 // if doRemove==true, will actually delete non-matching files
uci1 21:ce51bb0ba4a5 648 //
uci1 21:ce51bb0ba4a5 649 // return number of non-matching files
uci1 64:6c7a316eafad 650
uci1 64:6c7a316eafad 651 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 652
uci1 21:ce51bb0ba4a5 653 uint16_t nSavedBins = 0;
uci1 21:ce51bb0ba4a5 654 DIR* d( opendir(kLocalDir) );
uci1 21:ce51bb0ba4a5 655 struct dirent* dent;
uci1 21:ce51bb0ba4a5 656 if ( d!=NULL ) {
uci1 21:ce51bb0ba4a5 657 while ( (dent = readdir(d))!=NULL ) {
uci1 64:6c7a316eafad 658
uci1 64:6c7a316eafad 659 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 660
uci1 21:ce51bb0ba4a5 661 const size_t flen = strlen(dent->d_name);
uci1 21:ce51bb0ba4a5 662 const char* dext = dent->d_name + flen - 4;
uci1 21:ce51bb0ba4a5 663 if ( strncmp(dext,".BIN",4)==0 || strncmp(dext,".bin",4)==0 ) {
uci1 21:ce51bb0ba4a5 664 // ends with .BIN
uci1 21:ce51bb0ba4a5 665 if (strncmp(dent->d_name, fname.c_str(), fname.size())!=0) {
uci1 21:ce51bb0ba4a5 666 // some other mbed program than our new one
uci1 21:ce51bb0ba4a5 667 std::string dfn(kLocalDir);
uci1 21:ce51bb0ba4a5 668 dfn += "/";
uci1 21:ce51bb0ba4a5 669 dfn += dent->d_name;
uci1 21:ce51bb0ba4a5 670 if (doRemove) {
uci1 21:ce51bb0ba4a5 671 remove(dfn.c_str());
uci1 21:ce51bb0ba4a5 672 }
uci1 21:ce51bb0ba4a5 673 } else {
uci1 21:ce51bb0ba4a5 674 ++nSavedBins;
uci1 21:ce51bb0ba4a5 675 }
uci1 21:ce51bb0ba4a5 676 } // else not an mbed program
uci1 21:ce51bb0ba4a5 677 }
uci1 21:ce51bb0ba4a5 678 closedir(d);
uci1 21:ce51bb0ba4a5 679 }
uci1 21:ce51bb0ba4a5 680 return nSavedBins;
uci1 21:ce51bb0ba4a5 681 }
uci1 21:ce51bb0ba4a5 682
uci1 21:ce51bb0ba4a5 683 SnCommWin::ECommWinResult SnCommWin::GetHeader(const uint32_t timeOut,
uci1 21:ce51bb0ba4a5 684 char* const buf,
uci1 21:ce51bb0ba4a5 685 const uint32_t bsize,
uci1 21:ce51bb0ba4a5 686 uint8_t& mcode,
uci1 21:ce51bb0ba4a5 687 uint32_t& mlen) {
uci1 21:ce51bb0ba4a5 688 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 64:6c7a316eafad 689
uci1 64:6c7a316eafad 690 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 691
uci1 21:ce51bb0ba4a5 692 if (bsize>=SnHeaderFrame::kMaxSizeOf) {
uci1 21:ce51bb0ba4a5 693 // get header
uci1 37:ff95e7070f26 694 const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut);
uci1 21:ce51bb0ba4a5 695 if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) {
uci1 21:ce51bb0ba4a5 696 mcode=0;
uci1 21:ce51bb0ba4a5 697 mlen=0;
uci1 21:ce51bb0ba4a5 698 const char* b = buf;
uci1 21:ce51bb0ba4a5 699 SnHeaderFrame::ReadFrom(b, mcode, mlen);
uci1 21:ce51bb0ba4a5 700 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 701 printf("mcode=%02x, mlen=%u\r\n", mcode, mlen);
uci1 21:ce51bb0ba4a5 702 #endif
uci1 21:ce51bb0ba4a5 703 res = SnCommWin::kOkWithMsg;
uci1 21:ce51bb0ba4a5 704 }
uci1 21:ce51bb0ba4a5 705 }
uci1 21:ce51bb0ba4a5 706 return res;
uci1 21:ce51bb0ba4a5 707 }
uci1 21:ce51bb0ba4a5 708
uci1 8:95a325df1f6b 709 SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf,
uci1 8:95a325df1f6b 710 const uint32_t timeOut,
uci1 8:95a325df1f6b 711 char* const confBuf,
uci1 8:95a325df1f6b 712 const uint32_t bsize) {
uci1 8:95a325df1f6b 713 // confBuf assumed to alread be of allocated size
uci1 8:95a325df1f6b 714
uci1 12:d472f9811262 715 #ifdef DEBUG
uci1 8:95a325df1f6b 716 printf("GetConfig, to=%u\r\n",timeOut);
uci1 12:d472f9811262 717 #endif
uci1 8:95a325df1f6b 718
uci1 64:6c7a316eafad 719 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 720
uci1 8:95a325df1f6b 721 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 21:ce51bb0ba4a5 722 if (bsize>=SnConfigFrame::kMaxSizeOf) {
uci1 8:95a325df1f6b 723 // get header
uci1 21:ce51bb0ba4a5 724 uint8_t mcode=0; uint32_t mlen=0;
uci1 21:ce51bb0ba4a5 725 res = GetHeader(timeOut, confBuf, bsize, mcode, mlen);
uci1 21:ce51bb0ba4a5 726 if (res>=SnCommWin::kOkWithMsg) {
uci1 12:d472f9811262 727 if (mcode==SnHeaderFrame::kNoConfigCode) {
uci1 12:d472f9811262 728 // no config to get
uci1 16:744ce85aede2 729 res = SnCommWin::kOkWthMsgNoConf;
uci1 21:ce51bb0ba4a5 730 } else if (mcode==SnHeaderFrame::kMbedFilenameCode) {
uci1 21:ce51bb0ba4a5 731 res = GetFilename(timeOut, confBuf, mlen);
uci1 21:ce51bb0ba4a5 732 if (res>kAllFails) {
uci1 122:c1b5023eac69 733 std::string fname(confBuf, mlen); // the filename
uci1 122:c1b5023eac69 734 fname = fname.c_str(); // mlen may include the \0 which would be counted in size()
uci1 21:ce51bb0ba4a5 735 #ifdef DEBUG
uci1 54:ea1234a44fe8 736 printf("got filename = [%s] (%u)\r\n", fname.c_str(), fname.size());
uci1 21:ce51bb0ba4a5 737 #endif
uci1 122:c1b5023eac69 738 std::string lfname;
uci1 122:c1b5023eac69 739 res = GetLocalFile(fname, confBuf, bsize, timeOut, lfname);
uci1 122:c1b5023eac69 740
uci1 122:c1b5023eac69 741 #ifdef LOAD_DEFAULT_CONFIG_FROM_SD
uci1 122:c1b5023eac69 742 const size_t flen = strlen(fname.c_str());
uci1 122:c1b5023eac69 743 const char* dext = fname.c_str() + flen - 4;
uci1 122:c1b5023eac69 744 if ( strncmp(dext,".BIN",4)!=0 || strncmp(dext,".bin",4)!=0 ) {
uci1 122:c1b5023eac69 745 // does NOT end with .BIN
uci1 122:c1b5023eac69 746 // copy it to the SD card
uci1 122:c1b5023eac69 747 std::string sdfname;
uci1 122:c1b5023eac69 748 if ( BuildLocalFileName(fname, SnSDUtils::kSDdir, sdfname) ) {
uci1 122:c1b5023eac69 749 SnSDUtils::CopyFileToSD(lfname.c_str(), sdfname.c_str(),
uci1 122:c1b5023eac69 750 confBuf, bsize);
uci1 122:c1b5023eac69 751 }
uci1 122:c1b5023eac69 752 }
uci1 122:c1b5023eac69 753 #endif
uci1 21:ce51bb0ba4a5 754 }
uci1 12:d472f9811262 755 } else if (mcode!=SnHeaderFrame::kConfigCode) {
uci1 8:95a325df1f6b 756 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 757 } else {
uci1 8:95a325df1f6b 758 // get config
uci1 37:ff95e7070f26 759 const int clen = fComm->ReceiveAll(confBuf, mlen, timeOut);
uci1 8:95a325df1f6b 760 if (clen>0 && static_cast<uint32_t>(clen)==mlen) {
uci1 40:1324da35afd4 761 const uint8_t Rv = SnConfigFrame::PeekIOversion(confBuf);
uci1 40:1324da35afd4 762 if (SnConfigFrame::IsIOversionOk(Rv)) {
uci1 40:1324da35afd4 763 const char* b = confBuf;
uci1 40:1324da35afd4 764 conf.ReadFrom(b);
uci1 40:1324da35afd4 765 res = SnCommWin::kOkWithMsg;
uci1 40:1324da35afd4 766 } else {
uci1 40:1324da35afd4 767 res = SnCommWin::kUnexpectedRec;
uci1 40:1324da35afd4 768 char s[256];
uci1 40:1324da35afd4 769 sprintf(s,"Cannot accept config version [%hhu]. "
uci1 40:1324da35afd4 770 "Expect [%hhu].",Rv,SnConfigFrame::kIOVers);
uci1 40:1324da35afd4 771 SendString(s, timeOut);
uci1 40:1324da35afd4 772 }
uci1 8:95a325df1f6b 773 } else {
uci1 8:95a325df1f6b 774 res = SnCommWin::kUnexpectedRec;
uci1 8:95a325df1f6b 775 }
uci1 8:95a325df1f6b 776 }
uci1 8:95a325df1f6b 777 } else {
uci1 21:ce51bb0ba4a5 778 // not a problem if we get nothing (or no config)
uci1 16:744ce85aede2 779 res = SnCommWin::kOkNoMsg;
uci1 8:95a325df1f6b 780 }
uci1 8:95a325df1f6b 781 }
uci1 8:95a325df1f6b 782 return res;
uci1 8:95a325df1f6b 783 }
uci1 8:95a325df1f6b 784
uci1 40:1324da35afd4 785 int32_t SnCommWin::SendHndshkReq(char* const genBuf,
uci1 40:1324da35afd4 786 const uint32_t timeout_clock) {
uci1 40:1324da35afd4 787 // send a request demanding a handshake
uci1 40:1324da35afd4 788 // as we will be waiting for a response after this,
uci1 40:1324da35afd4 789 // FinishSending is also called
uci1 64:6c7a316eafad 790
uci1 64:6c7a316eafad 791 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 792
uci1 40:1324da35afd4 793 char* b = genBuf;
uci1 40:1324da35afd4 794 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kHnShDemandCode, 0);
uci1 40:1324da35afd4 795 const uint32_t bytesToBeSent = b-genBuf;
uci1 40:1324da35afd4 796 const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock);
uci1 40:1324da35afd4 797 const int32_t flen = fComm->FinishSending(timeout_clock);
uci1 40:1324da35afd4 798 #ifdef DEBUG
uci1 40:1324da35afd4 799 printf("SendHndshkReq: time = %u, timeout = %u, mlen=%d, flen=%d\r\n",
uci1 40:1324da35afd4 800 time(0), timeout_clock, mlen, flen);
uci1 40:1324da35afd4 801 #endif
uci1 40:1324da35afd4 802 return mlen+flen;
uci1 40:1324da35afd4 803 }
uci1 40:1324da35afd4 804
uci1 41:d6f5e2f09e07 805 SnCommWin::ECommWinResult SnCommWin::SendSignalStrength(char* const genBuf,
uci1 41:d6f5e2f09e07 806 SnSignalStrengthFrame& sigstr,
uci1 41:d6f5e2f09e07 807 const uint32_t timeout_clock) {
uci1 41:d6f5e2f09e07 808 // try to get the signal strength and send it if possible
uci1 64:6c7a316eafad 809
uci1 64:6c7a316eafad 810 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 811
uci1 41:d6f5e2f09e07 812 float ss;
uci1 41:d6f5e2f09e07 813 if ( fComm->CheckSignalStrength(timeout_clock, ss) ) {
uci1 41:d6f5e2f09e07 814 char* b = genBuf;
uci1 41:d6f5e2f09e07 815 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kSignalStrCode, sigstr.SizeOf());
uci1 41:d6f5e2f09e07 816 sigstr.SetSigStr(ss, time(0));
uci1 41:d6f5e2f09e07 817 sigstr.SetCommType( static_cast<uint8_t>(GetCommType()) );
uci1 41:d6f5e2f09e07 818 sigstr.WriteTo(b);
uci1 41:d6f5e2f09e07 819 const uint32_t bytesToBeSent = b - genBuf;
uci1 41:d6f5e2f09e07 820 const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock);
uci1 41:d6f5e2f09e07 821 const int32_t flen = fComm->FinishSending(timeout_clock);
uci1 41:d6f5e2f09e07 822 if (bytesToBeSent==(mlen+flen)) {
uci1 41:d6f5e2f09e07 823 return SnCommWin::kOkMsgSent;
uci1 41:d6f5e2f09e07 824 } else {
uci1 41:d6f5e2f09e07 825 return SnCommWin::kFailPartSent;
uci1 41:d6f5e2f09e07 826 }
uci1 41:d6f5e2f09e07 827 }
uci1 41:d6f5e2f09e07 828 return SnCommWin::kFailNoneSent;
uci1 41:d6f5e2f09e07 829 }
uci1 41:d6f5e2f09e07 830
uci1 84:80b15993944e 831 bool SnCommWin::WriteStatusDataPackHeaderTo(char*& b,
uci1 84:80b15993944e 832 const uint32_t payloadLen) {
uci1 84:80b15993944e 833 return CallHeaderWriteTo(b,
uci1 84:80b15993944e 834 SnHeaderFrame::kStatusDataPack,
uci1 84:80b15993944e 835 SnHeaderFrame::SizeOf() // this is the header of the packed data, not the kStatusDataPack hdr itself
uci1 84:80b15993944e 836 + payloadLen);
uci1 84:80b15993944e 837 }
uci1 84:80b15993944e 838
uci1 84:80b15993944e 839 bool SnCommWin::CallHeaderWriteTo(char*& b,
uci1 84:80b15993944e 840 const uint8_t msgCode,
uci1 84:80b15993944e 841 const uint32_t msgLen) {
uci1 84:80b15993944e 842 // to call fro SnCommWin.h where we don't include SnHeaderFrame
uci1 84:80b15993944e 843 return SnHeaderFrame::WriteTo(b, msgCode, msgLen);
uci1 84:80b15993944e 844 }
uci1 84:80b15993944e 845
uci1 84:80b15993944e 846 int32_t SnCommWin::CallSendAll(const char* const data,
uci1 84:80b15993944e 847 const uint32_t length,
uci1 84:80b15993944e 848 const uint32_t timeout_clock) {
uci1 84:80b15993944e 849 // to call fro SnCommWin.h where we don't include SnCommPeripheral
uci1 84:80b15993944e 850 if (fComm!=0) {
uci1 84:80b15993944e 851 return fComm->SendAll(data, length, timeout_clock);
uci1 84:80b15993944e 852 }
uci1 84:80b15993944e 853 return 0;
uci1 84:80b15993944e 854 }
uci1 84:80b15993944e 855
uci1 8:95a325df1f6b 856 SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf,
uci1 84:80b15993944e 857 const SnPowerFrame& pow, // com win power
uci1 84:80b15993944e 858 const SnEventFrame& stEvent,
uci1 10:3c93db1cfb12 859 const uint16_t seq,
uci1 116:8099b754fbb4 860 const uint32_t numThmTrigs,
uci1 116:8099b754fbb4 861 const uint32_t numSavedEvts,
uci1 116:8099b754fbb4 862 const float seqlive,
uci1 56:0bba0ef15697 863 const uint32_t powerOnTime,
uci1 84:80b15993944e 864 const SnTempFrame& temper, // com win temp
uci1 8:95a325df1f6b 865 char* const genBuf,
uci1 8:95a325df1f6b 866 const uint32_t timeout_clock) {
uci1 8:95a325df1f6b 867 // TODO: check if connected?
uci1 12:d472f9811262 868 #ifdef DEBUG
uci1 12:d472f9811262 869 printf("########### Send Status\r\n");
uci1 12:d472f9811262 870 #endif
uci1 64:6c7a316eafad 871
uci1 64:6c7a316eafad 872 Watchdog::kick(); // don't reset
uci1 84:80b15993944e 873
uci1 84:80b15993944e 874 int32_t toBeSent(0), byteSent(0), sendNow(0);
uci1 21:ce51bb0ba4a5 875 uint8_t loseLSB=0, loseMSB=0;
uci1 21:ce51bb0ba4a5 876 uint16_t wvBase=0;
uci1 21:ce51bb0ba4a5 877 conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase);
uci1 8:95a325df1f6b 878 const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf);
uci1 8:95a325df1f6b 879 char* b = genBuf;
uci1 8:95a325df1f6b 880 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize);
uci1 116:8099b754fbb4 881 SnStatusFrame::WriteTo(b, conf, seq,
uci1 116:8099b754fbb4 882 numThmTrigs, numSavedEvts, seqlive,
uci1 56:0bba0ef15697 883 powerOnTime, temper,
uci1 21:ce51bb0ba4a5 884 loseLSB, loseMSB, wvBase);
uci1 10:3c93db1cfb12 885 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode,
uci1 10:3c93db1cfb12 886 pow.SizeOf(SnPowerFrame::kIOvers));
uci1 10:3c93db1cfb12 887 pow.WriteTo(b);
uci1 84:80b15993944e 888 sendNow = b-genBuf;
uci1 84:80b15993944e 889 toBeSent = sendNow;
uci1 18:55f1581f2ee4 890 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 891 printf("calling SendAll (status)\r\n");
uci1 18:55f1581f2ee4 892 #endif
uci1 84:80b15993944e 893 byteSent = fComm->SendAll(genBuf, sendNow, timeout_clock);
uci1 84:80b15993944e 894 #ifdef DEBUG
uci1 84:80b15993944e 895 printf("SendAll returned %d\r\n",byteSent);
uci1 84:80b15993944e 896 #endif
uci1 84:80b15993944e 897
uci1 84:80b15993944e 898 //byteSent += fComm->FinishSending(timeout_clock);
uci1 41:d6f5e2f09e07 899 #ifdef DEBUG
uci1 84:80b15993944e 900 printf("byteSent=%d toBeSent=%d\r\n",byteSent,toBeSent);
uci1 84:80b15993944e 901 printf("status+power frame:\r\n");
uci1 84:80b15993944e 902 for (uint32_t i=0; i<sendNow; i++) {
uci1 84:80b15993944e 903 printf("%02X ",genBuf[i]);
uci1 84:80b15993944e 904 }
uci1 84:80b15993944e 905 printf("\r\n");
uci1 41:d6f5e2f09e07 906 #endif
uci1 84:80b15993944e 907
uci1 84:80b15993944e 908 Watchdog::kick(); // don't reset
uci1 84:80b15993944e 909
uci1 84:80b15993944e 910 // finally, mark the end of the status update
uci1 84:80b15993944e 911 b = genBuf;
uci1 84:80b15993944e 912 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusUpdDone, 0);
uci1 84:80b15993944e 913 sendNow = b-genBuf;
uci1 84:80b15993944e 914 toBeSent += sendNow;
uci1 8:95a325df1f6b 915 #ifdef DEBUG
uci1 84:80b15993944e 916 printf("calling SendAll (status update done)\r\n");
uci1 84:80b15993944e 917 #endif
uci1 84:80b15993944e 918 byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock);
uci1 84:80b15993944e 919
uci1 84:80b15993944e 920
uci1 84:80b15993944e 921 /*
uci1 84:80b15993944e 922 // event compression parameters must be the same
uci1 84:80b15993944e 923 // as those sent in the status update
uci1 84:80b15993944e 924 b = genBuf;
uci1 84:80b15993944e 925 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode,
uci1 84:80b15993944e 926 evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB));
uci1 84:80b15993944e 927 b = evt.WriteTo(b, loseLSB, loseMSB, wvBase);
uci1 84:80b15993944e 928 msiz = b-genBuf;
uci1 84:80b15993944e 929 #ifdef DEBUG
uci1 84:80b15993944e 930 printf("calling SendAll (event) %d:\r\n",msiz);
uci1 15:f2569d8e4176 931 for (uint32_t i=0; i<msiz; i++) {
uci1 8:95a325df1f6b 932 printf("%02X ",genBuf[i]);
uci1 8:95a325df1f6b 933 }
uci1 8:95a325df1f6b 934 printf("\r\n");
uci1 8:95a325df1f6b 935 #endif
uci1 84:80b15993944e 936 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 84:80b15993944e 937 */
uci1 84:80b15993944e 938
uci1 84:80b15993944e 939 // finish sending whatever didn't make it out
uci1 84:80b15993944e 940 byteSent += fComm->FinishSending(timeout_clock);
uci1 84:80b15993944e 941
uci1 84:80b15993944e 942 #ifdef DEBUG
uci1 84:80b15993944e 943 printf("after FinishSending\r\n");
uci1 84:80b15993944e 944 printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent);
uci1 84:80b15993944e 945 #endif
uci1 84:80b15993944e 946
uci1 84:80b15993944e 947 Watchdog::kick(); // don't reset
uci1 84:80b15993944e 948
uci1 84:80b15993944e 949 if (byteSent==toBeSent) {
uci1 84:80b15993944e 950 #ifdef DEBUG
uci1 84:80b15993944e 951 printf("Sent all status bytes OK\r\n");
uci1 84:80b15993944e 952 #endif
uci1 84:80b15993944e 953 return SnCommWin::kOkMsgSent;
uci1 84:80b15993944e 954 }
uci1 84:80b15993944e 955
uci1 12:d472f9811262 956 #ifdef DEBUG
uci1 84:80b15993944e 957 printf("Status failed with part-sent\r\n");
uci1 84:80b15993944e 958 #endif
uci1 84:80b15993944e 959 return SnCommWin::kFailPartSent;
uci1 84:80b15993944e 960 }
uci1 84:80b15993944e 961
uci1 84:80b15993944e 962
uci1 84:80b15993944e 963 SnCommWin::ECommWinResult SnCommWin::SendStatusData(const SnConfigFrame& conf,
uci1 98:ce72ef143b9b 964 const SnConfigFrame& stConf,
uci1 84:80b15993944e 965 const SnClockSetFrame& stTrgStartClk,
uci1 84:80b15993944e 966 const SnClockSetFrame& stTrgStopClk,
uci1 84:80b15993944e 967 const SnPowerFrame& stPower,
uci1 84:80b15993944e 968 const SnEventFrame& stEvent,
uci1 84:80b15993944e 969 const SnTempFrame& stTemperature,
uci1 84:80b15993944e 970 const SnHeartbeatFrame& stHeartbeat,
uci1 84:80b15993944e 971 const bool stNewPower,
uci1 84:80b15993944e 972 const bool stNewEvent,
uci1 84:80b15993944e 973 const bool stNewHeartbeat,
uci1 84:80b15993944e 974 const bool stNewTemperature,
uci1 84:80b15993944e 975 char* const genBuf,
uci1 84:80b15993944e 976 const uint32_t timeout_clock) {
uci1 84:80b15993944e 977 //
uci1 84:80b15993944e 978 //
uci1 84:80b15993944e 979 // check what data we are sending with the status frame.
uci1 84:80b15993944e 980 //
uci1 84:80b15993944e 981 // ordered like they appear in SnEvts files
uci1 84:80b15993944e 982 //
uci1 84:80b15993944e 983 //
uci1 84:80b15993944e 984
uci1 84:80b15993944e 985 #ifdef DEBUG
uci1 84:80b15993944e 986 printf("########### Send Status Data\r\n");
uci1 12:d472f9811262 987 #endif
uci1 84:80b15993944e 988
uci1 84:80b15993944e 989 Watchdog::kick(); // don't reset
uci1 84:80b15993944e 990
uci1 84:80b15993944e 991 int32_t toBeSent(0), byteSent(0), sendNow(0);
uci1 84:80b15993944e 992 char* b = genBuf;
uci1 84:80b15993944e 993
uci1 84:80b15993944e 994 // power reading first
uci1 84:80b15993944e 995 if (conf.IsStatusSendingPowerReading() && stNewPower) {
uci1 84:80b15993944e 996 SendStatusDataPack(stPower,
uci1 84:80b15993944e 997 SnHeaderFrame::kPowerCode,
uci1 84:80b15993944e 998 toBeSent,
uci1 84:80b15993944e 999 byteSent,
uci1 84:80b15993944e 1000 genBuf,
uci1 84:80b15993944e 1001 timeout_clock);
uci1 84:80b15993944e 1002 #ifdef DEBUG
uci1 84:80b15993944e 1003 printf("Send status power data. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1004 toBeSent, byteSent);
uci1 84:80b15993944e 1005 #endif
uci1 84:80b15993944e 1006 }
uci1 84:80b15993944e 1007
uci1 84:80b15993944e 1008 // config next
uci1 84:80b15993944e 1009 if (conf.IsStatusSendingConfig()) {
uci1 98:ce72ef143b9b 1010 SendStatusDataPack(stConf,
uci1 84:80b15993944e 1011 SnHeaderFrame::kConfigCode,
uci1 84:80b15993944e 1012 toBeSent,
uci1 84:80b15993944e 1013 byteSent,
uci1 84:80b15993944e 1014 genBuf,
uci1 84:80b15993944e 1015 timeout_clock);
uci1 84:80b15993944e 1016 #ifdef DEBUG
uci1 84:80b15993944e 1017 printf("Send status config. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1018 toBeSent, byteSent);
uci1 84:80b15993944e 1019 #endif
uci1 84:80b15993944e 1020 }
uci1 84:80b15993944e 1021
uci1 84:80b15993944e 1022 // trigger start time next
uci1 84:80b15993944e 1023 if (conf.IsStatusSendingTrigTimes() ) {
uci1 84:80b15993944e 1024 SendStatusDataPack(stTrgStartClk,
uci1 84:80b15993944e 1025 SnHeaderFrame::kFileTrgStrtCode,
uci1 84:80b15993944e 1026 toBeSent,
uci1 84:80b15993944e 1027 byteSent,
uci1 84:80b15993944e 1028 genBuf,
uci1 84:80b15993944e 1029 timeout_clock);
uci1 84:80b15993944e 1030 #ifdef DEBUG
uci1 84:80b15993944e 1031 printf("Send status trigger start. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1032 toBeSent, byteSent);
uci1 84:80b15993944e 1033 #endif
uci1 84:80b15993944e 1034 }
uci1 84:80b15993944e 1035
uci1 84:80b15993944e 1036 // event next
uci1 84:80b15993944e 1037 if (conf.IsStatusSendingEvent() && stNewEvent) {
uci1 84:80b15993944e 1038 // event has a different WriteTo
uci1 8:95a325df1f6b 1039 b = genBuf;
uci1 84:80b15993944e 1040 const uint32_t evtLen = stEvent.SizeOf(SnEventFrame::kIOVers,
uci1 84:80b15993944e 1041 conf.GetWvLoseLSB(),
uci1 84:80b15993944e 1042 conf.GetWvLoseMSB());
uci1 84:80b15993944e 1043 WriteStatusDataPackHeaderTo(b, evtLen);
uci1 84:80b15993944e 1044 CallHeaderWriteTo(b, SnHeaderFrame::kEventCode, evtLen);
uci1 84:80b15993944e 1045 b = stEvent.WriteTo(b,
uci1 84:80b15993944e 1046 conf.GetWvLoseLSB(),
uci1 84:80b15993944e 1047 conf.GetWvLoseMSB(),
uci1 84:80b15993944e 1048 conf.GetWvBaseline());
uci1 84:80b15993944e 1049 sendNow = (b - genBuf);
uci1 84:80b15993944e 1050 toBeSent += sendNow;
uci1 84:80b15993944e 1051 byteSent += CallSendAll(genBuf, sendNow, timeout_clock);
uci1 84:80b15993944e 1052
uci1 84:80b15993944e 1053 #ifdef DEBUG
uci1 84:80b15993944e 1054 printf("Send status event. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1055 toBeSent, byteSent);
uci1 84:80b15993944e 1056 #endif
uci1 84:80b15993944e 1057 }
uci1 84:80b15993944e 1058
uci1 84:80b15993944e 1059 // temperature next
uci1 84:80b15993944e 1060 if (conf.IsStatusSendingTemperature() && stNewTemperature) {
uci1 84:80b15993944e 1061 SendStatusDataPack(stTemperature,
uci1 84:80b15993944e 1062 SnHeaderFrame::kTemperatureCode,
uci1 84:80b15993944e 1063 toBeSent,
uci1 84:80b15993944e 1064 byteSent,
uci1 84:80b15993944e 1065 genBuf,
uci1 84:80b15993944e 1066 timeout_clock);
uci1 84:80b15993944e 1067 #ifdef DEBUG
uci1 84:80b15993944e 1068 printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1069 toBeSent, byteSent);
uci1 84:80b15993944e 1070 #endif
uci1 84:80b15993944e 1071 }
uci1 84:80b15993944e 1072
uci1 84:80b15993944e 1073 // heartbeat next
uci1 84:80b15993944e 1074 if (conf.IsStatusSendingHeartbeat() && stNewHeartbeat) {
uci1 84:80b15993944e 1075 SendStatusDataPack(stHeartbeat,
uci1 84:80b15993944e 1076 SnHeaderFrame::kHeartbeatCode,
uci1 84:80b15993944e 1077 toBeSent,
uci1 84:80b15993944e 1078 byteSent,
uci1 84:80b15993944e 1079 genBuf,
uci1 84:80b15993944e 1080 timeout_clock);
uci1 16:744ce85aede2 1081 #ifdef DEBUG
uci1 84:80b15993944e 1082 printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1083 toBeSent, byteSent);
uci1 84:80b15993944e 1084 #endif
uci1 84:80b15993944e 1085 }
uci1 84:80b15993944e 1086
uci1 84:80b15993944e 1087 // trigger stop time next
uci1 84:80b15993944e 1088 if (conf.IsStatusSendingTrigTimes() ) {
uci1 84:80b15993944e 1089 SendStatusDataPack(stTrgStopClk,
uci1 84:80b15993944e 1090 SnHeaderFrame::kFileTrgStopCode,
uci1 84:80b15993944e 1091 toBeSent,
uci1 84:80b15993944e 1092 byteSent,
uci1 84:80b15993944e 1093 genBuf,
uci1 84:80b15993944e 1094 timeout_clock);
uci1 84:80b15993944e 1095 #ifdef DEBUG
uci1 84:80b15993944e 1096 printf("Send status trigger stop. toBeSent=%u, byteSent=%u\r\n",
uci1 84:80b15993944e 1097 toBeSent, byteSent);
uci1 21:ce51bb0ba4a5 1098 #endif
uci1 8:95a325df1f6b 1099 }
uci1 84:80b15993944e 1100
uci1 84:80b15993944e 1101 // finally, mark the end of the status update
uci1 84:80b15993944e 1102 if (toBeSent>0) {
uci1 84:80b15993944e 1103 b = genBuf;
uci1 84:80b15993944e 1104 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusDatPakDone, 0);
uci1 84:80b15993944e 1105 sendNow = b-genBuf;
uci1 84:80b15993944e 1106 toBeSent += sendNow;
uci1 84:80b15993944e 1107 #ifdef DEBUG
uci1 116:8099b754fbb4 1108 printf("calling SendAll (status pak done)\r\n");
uci1 116:8099b754fbb4 1109 printf("timed out = %s\r\n", (fComm->IsTimedOut(timeout_clock) ? "true" : "false"));
uci1 116:8099b754fbb4 1110 printf("time(0)=%u, timeout_clock=%u\r\n", time(0), timeout_clock);
uci1 84:80b15993944e 1111 #endif
uci1 84:80b15993944e 1112 byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock);
uci1 84:80b15993944e 1113
uci1 116:8099b754fbb4 1114 #ifdef DEBUG
uci1 116:8099b754fbb4 1115 printf("calling FinishSending (status pak done)\r\n");
uci1 116:8099b754fbb4 1116 #endif
uci1 84:80b15993944e 1117 // finish sending whatever didn't make it out
uci1 84:80b15993944e 1118 byteSent += fComm->FinishSending(timeout_clock);
uci1 84:80b15993944e 1119
uci1 84:80b15993944e 1120 #ifdef DEBUG
uci1 84:80b15993944e 1121 printf("after FinishSending status data\r\n");
uci1 84:80b15993944e 1122 #endif
uci1 84:80b15993944e 1123 } // if toBeSent>0
uci1 84:80b15993944e 1124
uci1 84:80b15993944e 1125 #ifdef DEBUG
uci1 84:80b15993944e 1126 printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent);
uci1 116:8099b754fbb4 1127 printf("timed out = %s\r\n", (fComm->IsTimedOut(timeout_clock) ? "true" : "false"));
uci1 116:8099b754fbb4 1128 printf("time(0)=%u, timeout_clock=%u\r\n", time(0), timeout_clock);
uci1 84:80b15993944e 1129 #endif
uci1 84:80b15993944e 1130
uci1 84:80b15993944e 1131 if (byteSent==toBeSent) {
uci1 84:80b15993944e 1132 #ifdef DEBUG
uci1 84:80b15993944e 1133 printf("Sent all status data bytes OK\r\n");
uci1 84:80b15993944e 1134 #endif
uci1 84:80b15993944e 1135 return SnCommWin::kOkMsgSent;
uci1 84:80b15993944e 1136 }
uci1 84:80b15993944e 1137
uci1 84:80b15993944e 1138 #ifdef DEBUG
uci1 84:80b15993944e 1139 printf("Status data failed with part-sent\r\n");
uci1 84:80b15993944e 1140 #endif
uci1 8:95a325df1f6b 1141 return SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 1142 }
uci1 8:95a325df1f6b 1143
uci1 27:efc4d654b139 1144
uci1 23:ccf39298f205 1145 SnCommWin::ECommWinResult SnCommWin::SendString(const char* str,
uci1 23:ccf39298f205 1146 const uint32_t timeout) {
uci1 23:ccf39298f205 1147 #ifdef DEBUG
uci1 23:ccf39298f205 1148 printf("SnCommWin::SendString %s\r\n",str);
uci1 23:ccf39298f205 1149 #endif
uci1 64:6c7a316eafad 1150 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1151
uci1 37:ff95e7070f26 1152 const bool ok = fComm->SendString(str, timeout);
uci1 37:ff95e7070f26 1153 return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 23:ccf39298f205 1154 }
uci1 23:ccf39298f205 1155
uci1 40:1324da35afd4 1156 int32_t SnCommWin::SendFilename(const char* inf,
uci1 40:1324da35afd4 1157 char* const genBuf,
uci1 40:1324da35afd4 1158 int32_t& bytesToBeSent,
uci1 40:1324da35afd4 1159 const uint32_t timeout_clock) {
uci1 12:d472f9811262 1160 #ifdef DEBUG
uci1 12:d472f9811262 1161 printf("SnCommWin::SendFilename %s\r\n",inf);
uci1 12:d472f9811262 1162 #endif
uci1 64:6c7a316eafad 1163
uci1 64:6c7a316eafad 1164 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1165
uci1 12:d472f9811262 1166 // remove the directory
uci1 12:d472f9811262 1167 const char* fn = strrchr(inf, '/');
uci1 12:d472f9811262 1168 if (fn==0) {
uci1 12:d472f9811262 1169 // no directory
uci1 12:d472f9811262 1170 fn = inf;
uci1 12:d472f9811262 1171 } else if (fn!=inf) {
uci1 12:d472f9811262 1172 ++fn; // move past the '/' if it was found
uci1 12:d472f9811262 1173 }
uci1 12:d472f9811262 1174 #ifdef DEBUG
uci1 12:d472f9811262 1175 printf("send %s\r\n",fn);
uci1 12:d472f9811262 1176 #endif
uci1 8:95a325df1f6b 1177 const size_t flen = strlen(fn);
uci1 8:95a325df1f6b 1178 char* b = genBuf;
uci1 8:95a325df1f6b 1179 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen);
uci1 12:d472f9811262 1180 b = SnBitUtils::WriteTo(b, fn, flen);
uci1 40:1324da35afd4 1181 bytesToBeSent = b-genBuf;
uci1 40:1324da35afd4 1182 const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock);
uci1 12:d472f9811262 1183 #ifdef DEBUG
uci1 12:d472f9811262 1184 printf("time = %u, timeout = %u\r\n",time(0), timeout_clock);
uci1 12:d472f9811262 1185 #endif
uci1 40:1324da35afd4 1186 return mlen;
uci1 8:95a325df1f6b 1187 }
uci1 8:95a325df1f6b 1188
uci1 40:1324da35afd4 1189 int32_t SnCommWin::SendFileBlock(FILE* inf,
uci1 40:1324da35afd4 1190 const uint8_t blockHeaderCode,
uci1 40:1324da35afd4 1191 const uint32_t blockSize,
uci1 40:1324da35afd4 1192 char* const genBuf,
uci1 40:1324da35afd4 1193 int32_t& bytesToSend,
uci1 40:1324da35afd4 1194 const uint32_t timeout) {
uci1 12:d472f9811262 1195 // sends the block from the file. does not check if the file has sufficient bytes
uci1 12:d472f9811262 1196 // this should be done before calling this function
uci1 12:d472f9811262 1197 // (where the number of bytes in the file can be cached)
uci1 40:1324da35afd4 1198 //
uci1 40:1324da35afd4 1199 // bytesToSend will be set to the total bytes that should be delivered eventually
uci1 40:1324da35afd4 1200 // the number of bytes actually shipped out by SendAll is returned
uci1 64:6c7a316eafad 1201
uci1 64:6c7a316eafad 1202 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1203
uci1 12:d472f9811262 1204 char* b = genBuf;
uci1 12:d472f9811262 1205 SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize);
uci1 12:d472f9811262 1206 SnBitUtils::ReadFrom(inf, b, blockSize);
uci1 12:d472f9811262 1207 #ifdef DEBUG
uci1 12:d472f9811262 1208 printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf());
uci1 12:d472f9811262 1209 #endif
uci1 40:1324da35afd4 1210 bytesToSend = blockSize+SnHeaderFrame::SizeOf();
uci1 40:1324da35afd4 1211 return fComm->SendAll(genBuf, bytesToSend, timeout);
uci1 12:d472f9811262 1212 }
uci1 12:d472f9811262 1213
uci1 40:1324da35afd4 1214 int32_t SnCommWin::SendFileContents(FILE* inf,
uci1 40:1324da35afd4 1215 const SnConfigFrame& curConf,
uci1 40:1324da35afd4 1216 SnEventFrame& evt,
uci1 40:1324da35afd4 1217 SnPowerFrame& pow,
uci1 40:1324da35afd4 1218 char* const genBuf,
uci1 40:1324da35afd4 1219 uint32_t nevts,
uci1 40:1324da35afd4 1220 int32_t& bytesToBeSent,
uci1 40:1324da35afd4 1221 const uint32_t timeout_clock) {
uci1 12:d472f9811262 1222 // firstEvt==0 ==> start at beginning
uci1 12:d472f9811262 1223 // nevts==0 ==> all events
uci1 40:1324da35afd4 1224 //
uci1 40:1324da35afd4 1225 // bytesToSend will be set to the total bytes that should be delivered eventually
uci1 40:1324da35afd4 1226 // the number of bytes actually shipped out by SendAll is returned
uci1 12:d472f9811262 1227
uci1 12:d472f9811262 1228 #ifdef DEBUG
uci1 12:d472f9811262 1229 printf("SendFileContents (byte streaming)\r\n");
uci1 12:d472f9811262 1230 #endif
uci1 12:d472f9811262 1231
uci1 64:6c7a316eafad 1232 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1233
uci1 12:d472f9811262 1234 // store position in file so we can go back to it afterwards
uci1 12:d472f9811262 1235 // this is probably not necessary, because if this file is open
uci1 12:d472f9811262 1236 // in write-only mode, we're screwed..
uci1 12:d472f9811262 1237 const int fpos = ftell(inf);
uci1 12:d472f9811262 1238 if (fpos>0) {
uci1 12:d472f9811262 1239 fseek(inf, 0, SEEK_SET);
uci1 12:d472f9811262 1240 }
uci1 12:d472f9811262 1241 // how many bytes?
uci1 12:d472f9811262 1242 fseek(inf, 0, SEEK_END); // go to end
uci1 12:d472f9811262 1243 const int fend = ftell(inf);
uci1 12:d472f9811262 1244 fseek(inf, 0, SEEK_SET); // go to start
uci1 12:d472f9811262 1245 #ifdef DEBUG
uci1 12:d472f9811262 1246 printf("fend=%d\r\n",fend);
uci1 12:d472f9811262 1247 printf("fpos=%d\r\n",fpos);
uci1 12:d472f9811262 1248 #endif
uci1 12:d472f9811262 1249
uci1 12:d472f9811262 1250 // variables used when sending data
uci1 12:d472f9811262 1251 char* b = genBuf;
uci1 40:1324da35afd4 1252 int32_t msiz(0), mlen(0), mtogo(0), tsent(0);
uci1 21:ce51bb0ba4a5 1253 // count number of events / power readings sent
uci1 40:1324da35afd4 1254 uint32_t evtsSent=0, powsSent=0, crc=0;
uci1 12:d472f9811262 1255
uci1 64:6c7a316eafad 1256 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1257
uci1 12:d472f9811262 1258 // first is the file header, which has no SnHeaderFrame
uci1 12:d472f9811262 1259 msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers);
uci1 12:d472f9811262 1260 bool ok = (ftell(inf)+msiz)<=fend;
uci1 12:d472f9811262 1261 if (ok) {
uci1 12:d472f9811262 1262 mlen = SendFileBlock(inf, SnHeaderFrame::kFileHeadrCode,
uci1 40:1324da35afd4 1263 msiz, genBuf, mtogo, timeout_clock);
uci1 40:1324da35afd4 1264 //ok &= (msiz+SnHeaderFrame::SizeOf())==mlen;
uci1 40:1324da35afd4 1265 bytesToBeSent += mtogo;
uci1 40:1324da35afd4 1266 tsent += mlen;
uci1 12:d472f9811262 1267 #ifdef DEBUG
uci1 12:d472f9811262 1268 printf("sent file header. ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 1269 #endif
uci1 64:6c7a316eafad 1270
uci1 64:6c7a316eafad 1271 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1272
uci1 40:1324da35afd4 1273 // calc crc w/o the header block of the file header, as
uci1 40:1324da35afd4 1274 // this does not get stored in the file
uci1 40:1324da35afd4 1275 crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf+SnHeaderFrame::SizeOf(),
uci1 40:1324da35afd4 1276 mtogo-SnHeaderFrame::SizeOf());
uci1 40:1324da35afd4 1277
uci1 12:d472f9811262 1278 // the header info for each block
uci1 40:1324da35afd4 1279 uint8_t hcode(0);
uci1 40:1324da35afd4 1280 uint32_t hlen(0);
uci1 12:d472f9811262 1281
uci1 12:d472f9811262 1282 // now just loop through file and send each block
uci1 18:55f1581f2ee4 1283 bool fok = true;
uci1 18:55f1581f2ee4 1284 while ( fok
uci1 12:d472f9811262 1285 && (feof(inf)==0)
uci1 12:d472f9811262 1286 && (ferror(inf)==0)
uci1 12:d472f9811262 1287 && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) {
uci1 64:6c7a316eafad 1288
uci1 64:6c7a316eafad 1289 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1290
uci1 12:d472f9811262 1291 // read the header for the block
uci1 12:d472f9811262 1292 SnSDUtils::ReadBlockHeader(inf, hcode, hlen);
uci1 18:55f1581f2ee4 1293 fok &= (ftell(inf)+hlen)<=fend;
uci1 12:d472f9811262 1294 #ifdef DEBUG
uci1 18:55f1581f2ee4 1295 printf("new block: hc=%02x, hl=%u, ftell=%d, fend=%d, "
uci1 18:55f1581f2ee4 1296 "ok=%d, fok=%d\r\n",
uci1 18:55f1581f2ee4 1297 hcode, hlen, ftell(inf), fend, (int)ok, (int)fok);
uci1 12:d472f9811262 1298 #endif
uci1 40:1324da35afd4 1299 if (fok) {
uci1 12:d472f9811262 1300 // send the block
uci1 12:d472f9811262 1301 // TODO: repack events?
uci1 40:1324da35afd4 1302 mlen = SendFileBlock(inf, hcode, hlen, genBuf,
uci1 40:1324da35afd4 1303 mtogo, timeout_clock);
uci1 40:1324da35afd4 1304 bytesToBeSent += mtogo;
uci1 40:1324da35afd4 1305 tsent += mlen;
uci1 40:1324da35afd4 1306 crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf, mtogo);
uci1 40:1324da35afd4 1307
uci1 40:1324da35afd4 1308 if (hcode==SnHeaderFrame::kEventCode) {
uci1 40:1324da35afd4 1309 ++evtsSent;
uci1 40:1324da35afd4 1310 if (nevts>0) {
uci1 40:1324da35afd4 1311 if (evtsSent>=nevts) {
uci1 12:d472f9811262 1312 #ifdef DEBUG
uci1 56:0bba0ef15697 1313 printf("sent %u events. stop\r\n",evtsSent);
uci1 12:d472f9811262 1314 #endif
uci1 40:1324da35afd4 1315 break;
uci1 12:d472f9811262 1316 }
uci1 12:d472f9811262 1317 }
uci1 40:1324da35afd4 1318 } else if (hcode==SnHeaderFrame::kPowerCode) {
uci1 40:1324da35afd4 1319 ++powsSent;
uci1 40:1324da35afd4 1320 }
uci1 40:1324da35afd4 1321 } // otherwise file size not sufficient for this block
uci1 21:ce51bb0ba4a5 1322 } // loop over file contents
uci1 21:ce51bb0ba4a5 1323 } else {
uci1 21:ce51bb0ba4a5 1324 // otherwise file size not sufficient for file header
uci1 21:ce51bb0ba4a5 1325 // so it's either 0 or corrupted.
uci1 21:ce51bb0ba4a5 1326 // proceed as normal even tho no contents were sent.
uci1 21:ce51bb0ba4a5 1327 // if we're deleting files, this one will be deleted.
uci1 21:ce51bb0ba4a5 1328 ok = true;
uci1 21:ce51bb0ba4a5 1329 }
uci1 12:d472f9811262 1330 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1331 printf("loop done. ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 1332 #endif
uci1 40:1324da35afd4 1333
uci1 64:6c7a316eafad 1334 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1335
uci1 40:1324da35afd4 1336 // send the crc
uci1 40:1324da35afd4 1337 #ifdef DEBUG
uci1 40:1324da35afd4 1338 printf("sending crc (%u)\r\n",crc);
uci1 40:1324da35afd4 1339 #endif
uci1 40:1324da35afd4 1340 b = genBuf;
uci1 40:1324da35afd4 1341 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kMbedFileChksCode, sizeof(uint32_t));
uci1 40:1324da35afd4 1342 b = SnBitUtils::WriteTo(b, crc);
uci1 40:1324da35afd4 1343 msiz = b - genBuf;
uci1 40:1324da35afd4 1344 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 40:1324da35afd4 1345 bytesToBeSent += msiz;
uci1 40:1324da35afd4 1346 tsent += mlen;
uci1 40:1324da35afd4 1347
uci1 40:1324da35afd4 1348 #ifdef DEBUG
uci1 40:1324da35afd4 1349 printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen);
uci1 40:1324da35afd4 1350 #endif
uci1 40:1324da35afd4 1351
uci1 64:6c7a316eafad 1352 Watchdog::kick(); // don't reset
uci1 21:ce51bb0ba4a5 1353
uci1 21:ce51bb0ba4a5 1354 // send number of events sent
uci1 12:d472f9811262 1355 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1356 printf("sending evtsSent (%u)\r\n",evtsSent);
uci1 12:d472f9811262 1357 #endif
uci1 21:ce51bb0ba4a5 1358 b = genBuf;
uci1 21:ce51bb0ba4a5 1359 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t));
uci1 21:ce51bb0ba4a5 1360 b = SnBitUtils::WriteTo(b, evtsSent);
uci1 21:ce51bb0ba4a5 1361 msiz = b - genBuf;
uci1 37:ff95e7070f26 1362 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 40:1324da35afd4 1363 bytesToBeSent += msiz;
uci1 40:1324da35afd4 1364 tsent += mlen;
uci1 40:1324da35afd4 1365
uci1 18:55f1581f2ee4 1366 #ifdef DEBUG
uci1 40:1324da35afd4 1367 printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen);
uci1 18:55f1581f2ee4 1368 #endif
uci1 21:ce51bb0ba4a5 1369
uci1 64:6c7a316eafad 1370 Watchdog::kick(); // don't reset
uci1 64:6c7a316eafad 1371
uci1 21:ce51bb0ba4a5 1372 // send number of power readings sent
uci1 12:d472f9811262 1373 #ifdef DEBUG
uci1 21:ce51bb0ba4a5 1374 printf("sending powsSent (%u)\r\n",powsSent);
uci1 12:d472f9811262 1375 #endif
uci1 21:ce51bb0ba4a5 1376 b = genBuf;
uci1 21:ce51bb0ba4a5 1377 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t));
uci1 21:ce51bb0ba4a5 1378 b = SnBitUtils::WriteTo(b, powsSent);
uci1 21:ce51bb0ba4a5 1379 msiz = b - genBuf;
uci1 37:ff95e7070f26 1380 mlen = fComm->SendAll(genBuf, msiz, timeout_clock);
uci1 40:1324da35afd4 1381 bytesToBeSent += msiz;
uci1 40:1324da35afd4 1382 tsent += mlen;
uci1 21:ce51bb0ba4a5 1383
uci1 18:55f1581f2ee4 1384 #ifdef DEBUG
uci1 40:1324da35afd4 1385 printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen);
uci1 18:55f1581f2ee4 1386 #endif
uci1 12:d472f9811262 1387
uci1 12:d472f9811262 1388 // put file position back
uci1 12:d472f9811262 1389 fseek(inf, fpos, SEEK_SET);
uci1 12:d472f9811262 1390
uci1 12:d472f9811262 1391 #ifdef DEBUG
uci1 12:d472f9811262 1392 printf("end contents: ok=%d\r\n",(int)ok);
uci1 12:d472f9811262 1393 #endif
uci1 12:d472f9811262 1394
uci1 40:1324da35afd4 1395 return tsent;
uci1 8:95a325df1f6b 1396
uci1 40:1324da35afd4 1397 //return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 8:95a325df1f6b 1398 }