Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Nov 28 07:42:57 2014 +0000
Revision:
64:6c7a316eafad
Parent:
63:4820a4460f00
Child:
65:2cb3e99ce466
add watchdog kicks to SnCommWin and SnSDUtils

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