Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.cpp@116:8099b754fbb4, 2016-05-03 (annotated)
- Committer:
- uci1
- Date:
- Tue May 03 02:01:35 2016 +0000
- Revision:
- 116:8099b754fbb4
- Parent:
- 98:ce72ef143b9b
- Child:
- 122:c1b5023eac69
One program for all stns via UID/MAC lookup table or generation. Status sends number trg/evt and livetime, not rates. Add 512 sample evt and RFFT-LUTs. Add L1Scaledown trg bit. Allow skip SST reset at start. Fix dt at end of seq. End of comm signal.
Who changed what in which revision?
User | Revision | Line number | New 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 | 21:ce51bb0ba4a5 | 450 | SnCommWin::ECommWinResult SnCommWin::GetLocalFile(std::string fname, |
uci1 | 21:ce51bb0ba4a5 | 451 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 452 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 453 | const uint32_t timeout) { |
uci1 | 21:ce51bb0ba4a5 | 454 | // get a file and save it locally with the specified name |
uci1 | 21:ce51bb0ba4a5 | 455 | |
uci1 | 64:6c7a316eafad | 456 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 457 | |
uci1 | 21:ce51bb0ba4a5 | 458 | // get the header for the file |
uci1 | 21:ce51bb0ba4a5 | 459 | uint8_t mcode=0; uint32_t mlen=0; |
uci1 | 21:ce51bb0ba4a5 | 460 | SnCommWin::ECommWinResult res = |
uci1 | 21:ce51bb0ba4a5 | 461 | GetHeader(timeout, buf, bsize, mcode, mlen); |
uci1 | 54:ea1234a44fe8 | 462 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 463 | printf("Got header code %x, len %u\r\n",mcode,mlen); |
uci1 | 54:ea1234a44fe8 | 464 | #endif |
uci1 | 54:ea1234a44fe8 | 465 | |
uci1 | 21:ce51bb0ba4a5 | 466 | if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileCode) ) { |
uci1 | 21:ce51bb0ba4a5 | 467 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 468 | printf("Got mbed file header. File length = %u\r\n",mlen); |
uci1 | 21:ce51bb0ba4a5 | 469 | #endif |
uci1 | 21:ce51bb0ba4a5 | 470 | // got the header.. make the file.. |
uci1 | 21:ce51bb0ba4a5 | 471 | // make the file name ALLCAPS, since LocalFileSystem will do it anyway |
uci1 | 37:ff95e7070f26 | 472 | SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end()); |
uci1 | 21:ce51bb0ba4a5 | 473 | // now ensure the file name is 8.3 only -- for LocalFileSystem |
uci1 | 21:ce51bb0ba4a5 | 474 | const size_t ldlen = strlen(kLocalDir); |
uci1 | 21:ce51bb0ba4a5 | 475 | // 12 = 8.3 filename format, 1 for / and 1 for \0 |
uci1 | 21:ce51bb0ba4a5 | 476 | const uint32_t fbs = ldlen+1+12+1; |
uci1 | 21:ce51bb0ba4a5 | 477 | char fnb[fbs]; |
uci1 | 36:87865913ae6f | 478 | memset(fnb, 0, sizeof(char)*fbs); |
uci1 | 64:6c7a316eafad | 479 | |
uci1 | 64:6c7a316eafad | 480 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 481 | |
uci1 | 40:1324da35afd4 | 482 | // find the extension |
uci1 | 40:1324da35afd4 | 483 | const size_t xdot = fname.rfind('.'); |
uci1 | 40:1324da35afd4 | 484 | if (xdot!=string::npos && (fname.size()==(4+xdot))) { |
uci1 | 40:1324da35afd4 | 485 | const size_t fnpl = (xdot>8) ? 8 : xdot; |
uci1 | 40:1324da35afd4 | 486 | char* fb = fnb; |
uci1 | 40:1324da35afd4 | 487 | memcpy(fb, kLocalDir, ldlen); fb+=ldlen; // /local |
uci1 | 40:1324da35afd4 | 488 | *fb = '/'; ++fb; // / |
uci1 | 40:1324da35afd4 | 489 | strncpy(fb, fname.c_str(), fnpl); fb+=fnpl; // FILENAME |
uci1 | 40:1324da35afd4 | 490 | *fb = '.'; ++fb; // . |
uci1 | 40:1324da35afd4 | 491 | strncpy(fb, fname.c_str()+xdot+1, 3); // EXT |
uci1 | 64:6c7a316eafad | 492 | |
uci1 | 64:6c7a316eafad | 493 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 494 | |
uci1 | 40:1324da35afd4 | 495 | // all that just for the file name! |
uci1 | 40:1324da35afd4 | 496 | FILE* lf = fopen(fnb,"wb"); |
uci1 | 21:ce51bb0ba4a5 | 497 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 498 | printf("tried to open file [%s]. lf=%p\r\n",fnb,(void*)lf); |
uci1 | 21:ce51bb0ba4a5 | 499 | #endif |
uci1 | 40:1324da35afd4 | 500 | // get all the data and dump it into the file |
uci1 | 40:1324da35afd4 | 501 | int b = 0, toget = 0; |
uci1 | 40:1324da35afd4 | 502 | while ( mlen>b ) { |
uci1 | 40:1324da35afd4 | 503 | if (fComm->IsTimedOut(timeout)) { |
uci1 | 36:87865913ae6f | 504 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 505 | printf("timeout while getting file\r\n"); |
uci1 | 36:87865913ae6f | 506 | #endif |
uci1 | 40:1324da35afd4 | 507 | res = kFailTimeout; |
uci1 | 21:ce51bb0ba4a5 | 508 | break; |
uci1 | 21:ce51bb0ba4a5 | 509 | } |
uci1 | 40:1324da35afd4 | 510 | toget = mlen - b; |
uci1 | 40:1324da35afd4 | 511 | if (toget>bsize) { |
uci1 | 40:1324da35afd4 | 512 | toget = bsize; |
uci1 | 40:1324da35afd4 | 513 | } |
uci1 | 40:1324da35afd4 | 514 | const int got = fComm->ReceiveAll(buf, toget, timeout); |
uci1 | 40:1324da35afd4 | 515 | if (lf!=NULL) { |
uci1 | 36:87865913ae6f | 516 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 517 | printf("writing %d bytes to file\r\n", got); |
uci1 | 36:87865913ae6f | 518 | #endif |
uci1 | 64:6c7a316eafad | 519 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 520 | |
uci1 | 40:1324da35afd4 | 521 | SnBitUtils::WriteTo(lf, buf, got); |
uci1 | 40:1324da35afd4 | 522 | } |
uci1 | 40:1324da35afd4 | 523 | b += got; |
uci1 | 40:1324da35afd4 | 524 | } // file data receive loop |
uci1 | 64:6c7a316eafad | 525 | |
uci1 | 64:6c7a316eafad | 526 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 527 | |
uci1 | 40:1324da35afd4 | 528 | uint32_t crc=0; |
uci1 | 40:1324da35afd4 | 529 | if (lf!=NULL) { |
uci1 | 40:1324da35afd4 | 530 | // calculate the crc from what's actually in the file |
uci1 | 40:1324da35afd4 | 531 | fclose(lf); // to flush it |
uci1 | 36:87865913ae6f | 532 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 533 | printf("fopen: %s\r\n",fnb); |
uci1 | 36:87865913ae6f | 534 | #endif |
uci1 | 40:1324da35afd4 | 535 | lf = fopen(fnb,"rb"); |
uci1 | 76:f8383f0292c2 | 536 | if (lf!=0) { |
uci1 | 76:f8383f0292c2 | 537 | fseek(lf, 0, SEEK_END); |
uci1 | 76:f8383f0292c2 | 538 | int32_t fend = ftell(lf); |
uci1 | 76:f8383f0292c2 | 539 | fseek(lf, 0, SEEK_SET); |
uci1 | 76:f8383f0292c2 | 540 | char c; |
uci1 | 76:f8383f0292c2 | 541 | for (int32_t i=0; i<fend; ++i) { |
uci1 | 76:f8383f0292c2 | 542 | |
uci1 | 76:f8383f0292c2 | 543 | Watchdog::kick(); // don't reset |
uci1 | 76:f8383f0292c2 | 544 | |
uci1 | 76:f8383f0292c2 | 545 | SnBitUtils::ReadFrom(lf, c); |
uci1 | 76:f8383f0292c2 | 546 | if (feof(lf)==0 && ferror(lf)==0) { |
uci1 | 76:f8383f0292c2 | 547 | crc = SnCRCUtils::update_crc32_xfer(crc, c); |
uci1 | 76:f8383f0292c2 | 548 | } else { |
uci1 | 76:f8383f0292c2 | 549 | break; |
uci1 | 76:f8383f0292c2 | 550 | } |
uci1 | 40:1324da35afd4 | 551 | } |
uci1 | 76:f8383f0292c2 | 552 | fclose(lf); |
uci1 | 21:ce51bb0ba4a5 | 553 | } |
uci1 | 40:1324da35afd4 | 554 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 555 | printf("closed file. crc = %u\r\n", crc); |
uci1 | 40:1324da35afd4 | 556 | #endif |
uci1 | 40:1324da35afd4 | 557 | } // if output file not 0 |
uci1 | 40:1324da35afd4 | 558 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 559 | printf("mlen=%u, b=%d, eq=%s\r\n",mlen,b, |
uci1 | 40:1324da35afd4 | 560 | (mlen==b)?"true":"false"); |
uci1 | 40:1324da35afd4 | 561 | #endif |
uci1 | 40:1324da35afd4 | 562 | if ( mlen!=b ) { |
uci1 | 40:1324da35afd4 | 563 | if (res > kUnexpectedRec) { |
uci1 | 40:1324da35afd4 | 564 | res = kUnexpectedRec; |
uci1 | 40:1324da35afd4 | 565 | } // otherwise it's already worse |
uci1 | 40:1324da35afd4 | 566 | } else { |
uci1 | 64:6c7a316eafad | 567 | |
uci1 | 64:6c7a316eafad | 568 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 569 | |
uci1 | 40:1324da35afd4 | 570 | // check that the file is ok |
uci1 | 40:1324da35afd4 | 571 | // get the checksum |
uci1 | 40:1324da35afd4 | 572 | res = GetHeader(timeout, buf, bsize, mcode, mlen); |
uci1 | 40:1324da35afd4 | 573 | if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileChksCode) ) { |
uci1 | 40:1324da35afd4 | 574 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 575 | printf("received chksum=%u, crc=%u, eq=%s\r\n", |
uci1 | 40:1324da35afd4 | 576 | mlen,crc,(crc==mlen)?"true":"false"); |
uci1 | 40:1324da35afd4 | 577 | #endif |
uci1 | 40:1324da35afd4 | 578 | if (crc != mlen) { |
uci1 | 36:87865913ae6f | 579 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 580 | printf("checksums differ! unexpected.\r\n"); |
uci1 | 40:1324da35afd4 | 581 | #endif |
uci1 | 40:1324da35afd4 | 582 | res = kUnexpectedRec; // important! |
uci1 | 40:1324da35afd4 | 583 | } |
uci1 | 40:1324da35afd4 | 584 | } |
uci1 | 40:1324da35afd4 | 585 | } // end get remote file checksum |
uci1 | 40:1324da35afd4 | 586 | |
uci1 | 40:1324da35afd4 | 587 | if (lf!=NULL) { |
uci1 | 40:1324da35afd4 | 588 | if ( res<kOkWithMsg ) { |
uci1 | 40:1324da35afd4 | 589 | // timeout, bad checksum, something else bad? |
uci1 | 40:1324da35afd4 | 590 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 591 | printf("removing %s\r\n",fnb); |
uci1 | 36:87865913ae6f | 592 | #endif |
uci1 | 40:1324da35afd4 | 593 | remove(fnb); // delete the file |
uci1 | 40:1324da35afd4 | 594 | } else { |
uci1 | 40:1324da35afd4 | 595 | // if we got a new program, remove the old ones. |
uci1 | 40:1324da35afd4 | 596 | // (schedule myself for immediate de-resolution) |
uci1 | 40:1324da35afd4 | 597 | // |
uci1 | 40:1324da35afd4 | 598 | // first just count how many we would save |
uci1 | 40:1324da35afd4 | 599 | const uint16_t nSavedBins = LoopLocalDirBinFiles(false, fname); |
uci1 | 36:87865913ae6f | 600 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 601 | printf("nSavedBins=%hu",nSavedBins); |
uci1 | 36:87865913ae6f | 602 | #endif |
uci1 | 40:1324da35afd4 | 603 | if (nSavedBins==1) { |
uci1 | 40:1324da35afd4 | 604 | // ok. new program will be the only one. remove the others |
uci1 | 40:1324da35afd4 | 605 | // hope the new program works! |
uci1 | 40:1324da35afd4 | 606 | LoopLocalDirBinFiles(true, fname); |
uci1 | 36:87865913ae6f | 607 | |
uci1 | 36:87865913ae6f | 608 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 609 | printf("rebooting\r\n"); |
uci1 | 36:87865913ae6f | 610 | #endif |
uci1 | 40:1324da35afd4 | 611 | // goodbye cruel world, it's over. walk on by... |
uci1 | 40:1324da35afd4 | 612 | mbed_reset(); |
uci1 | 40:1324da35afd4 | 613 | } |
uci1 | 40:1324da35afd4 | 614 | } // end check new program block |
uci1 | 40:1324da35afd4 | 615 | } // if output file not 0 |
uci1 | 40:1324da35afd4 | 616 | } else { |
uci1 | 40:1324da35afd4 | 617 | // filename mangled. either doesn't contain a '.' |
uci1 | 40:1324da35afd4 | 618 | // or the extension is longer than 3 letters |
uci1 | 54:ea1234a44fe8 | 619 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 620 | printf("filname mangled. (%s) size=%u, xdot=%u", |
uci1 | 54:ea1234a44fe8 | 621 | fname.c_str(), fname.size(), xdot); |
uci1 | 54:ea1234a44fe8 | 622 | #endif |
uci1 | 40:1324da35afd4 | 623 | res = SnCommWin::kUnexpectedRec; |
uci1 | 21:ce51bb0ba4a5 | 624 | } |
uci1 | 40:1324da35afd4 | 625 | } // if header ok |
uci1 | 54:ea1234a44fe8 | 626 | |
uci1 | 21:ce51bb0ba4a5 | 627 | return res; |
uci1 | 21:ce51bb0ba4a5 | 628 | } |
uci1 | 21:ce51bb0ba4a5 | 629 | |
uci1 | 21:ce51bb0ba4a5 | 630 | int16_t SnCommWin::LoopLocalDirBinFiles(const bool doRemove, |
uci1 | 21:ce51bb0ba4a5 | 631 | const std::string& fname) { |
uci1 | 21:ce51bb0ba4a5 | 632 | // loop over the local directory, count the BIN files that match 'fname' |
uci1 | 21:ce51bb0ba4a5 | 633 | // |
uci1 | 21:ce51bb0ba4a5 | 634 | // fname is assumed to be all caps already! |
uci1 | 21:ce51bb0ba4a5 | 635 | // |
uci1 | 21:ce51bb0ba4a5 | 636 | // if doRemove==true, will actually delete non-matching files |
uci1 | 21:ce51bb0ba4a5 | 637 | // |
uci1 | 21:ce51bb0ba4a5 | 638 | // return number of non-matching files |
uci1 | 64:6c7a316eafad | 639 | |
uci1 | 64:6c7a316eafad | 640 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 641 | |
uci1 | 21:ce51bb0ba4a5 | 642 | uint16_t nSavedBins = 0; |
uci1 | 21:ce51bb0ba4a5 | 643 | DIR* d( opendir(kLocalDir) ); |
uci1 | 21:ce51bb0ba4a5 | 644 | struct dirent* dent; |
uci1 | 21:ce51bb0ba4a5 | 645 | if ( d!=NULL ) { |
uci1 | 21:ce51bb0ba4a5 | 646 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 64:6c7a316eafad | 647 | |
uci1 | 64:6c7a316eafad | 648 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 649 | |
uci1 | 21:ce51bb0ba4a5 | 650 | const size_t flen = strlen(dent->d_name); |
uci1 | 21:ce51bb0ba4a5 | 651 | const char* dext = dent->d_name + flen - 4; |
uci1 | 21:ce51bb0ba4a5 | 652 | if ( strncmp(dext,".BIN",4)==0 || strncmp(dext,".bin",4)==0 ) { |
uci1 | 21:ce51bb0ba4a5 | 653 | // ends with .BIN |
uci1 | 21:ce51bb0ba4a5 | 654 | if (strncmp(dent->d_name, fname.c_str(), fname.size())!=0) { |
uci1 | 21:ce51bb0ba4a5 | 655 | // some other mbed program than our new one |
uci1 | 21:ce51bb0ba4a5 | 656 | std::string dfn(kLocalDir); |
uci1 | 21:ce51bb0ba4a5 | 657 | dfn += "/"; |
uci1 | 21:ce51bb0ba4a5 | 658 | dfn += dent->d_name; |
uci1 | 21:ce51bb0ba4a5 | 659 | if (doRemove) { |
uci1 | 21:ce51bb0ba4a5 | 660 | remove(dfn.c_str()); |
uci1 | 21:ce51bb0ba4a5 | 661 | } |
uci1 | 21:ce51bb0ba4a5 | 662 | } else { |
uci1 | 21:ce51bb0ba4a5 | 663 | ++nSavedBins; |
uci1 | 21:ce51bb0ba4a5 | 664 | } |
uci1 | 21:ce51bb0ba4a5 | 665 | } // else not an mbed program |
uci1 | 21:ce51bb0ba4a5 | 666 | } |
uci1 | 21:ce51bb0ba4a5 | 667 | closedir(d); |
uci1 | 21:ce51bb0ba4a5 | 668 | } |
uci1 | 21:ce51bb0ba4a5 | 669 | return nSavedBins; |
uci1 | 21:ce51bb0ba4a5 | 670 | } |
uci1 | 21:ce51bb0ba4a5 | 671 | |
uci1 | 21:ce51bb0ba4a5 | 672 | SnCommWin::ECommWinResult SnCommWin::GetHeader(const uint32_t timeOut, |
uci1 | 21:ce51bb0ba4a5 | 673 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 674 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 675 | uint8_t& mcode, |
uci1 | 21:ce51bb0ba4a5 | 676 | uint32_t& mlen) { |
uci1 | 21:ce51bb0ba4a5 | 677 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 64:6c7a316eafad | 678 | |
uci1 | 64:6c7a316eafad | 679 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 680 | |
uci1 | 21:ce51bb0ba4a5 | 681 | if (bsize>=SnHeaderFrame::kMaxSizeOf) { |
uci1 | 21:ce51bb0ba4a5 | 682 | // get header |
uci1 | 37:ff95e7070f26 | 683 | const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut); |
uci1 | 21:ce51bb0ba4a5 | 684 | if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) { |
uci1 | 21:ce51bb0ba4a5 | 685 | mcode=0; |
uci1 | 21:ce51bb0ba4a5 | 686 | mlen=0; |
uci1 | 21:ce51bb0ba4a5 | 687 | const char* b = buf; |
uci1 | 21:ce51bb0ba4a5 | 688 | SnHeaderFrame::ReadFrom(b, mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 689 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 690 | printf("mcode=%02x, mlen=%u\r\n", mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 691 | #endif |
uci1 | 21:ce51bb0ba4a5 | 692 | res = SnCommWin::kOkWithMsg; |
uci1 | 21:ce51bb0ba4a5 | 693 | } |
uci1 | 21:ce51bb0ba4a5 | 694 | } |
uci1 | 21:ce51bb0ba4a5 | 695 | return res; |
uci1 | 21:ce51bb0ba4a5 | 696 | } |
uci1 | 21:ce51bb0ba4a5 | 697 | |
uci1 | 8:95a325df1f6b | 698 | SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 699 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 700 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 701 | const uint32_t bsize) { |
uci1 | 8:95a325df1f6b | 702 | // confBuf assumed to alread be of allocated size |
uci1 | 8:95a325df1f6b | 703 | |
uci1 | 12:d472f9811262 | 704 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 705 | printf("GetConfig, to=%u\r\n",timeOut); |
uci1 | 12:d472f9811262 | 706 | #endif |
uci1 | 8:95a325df1f6b | 707 | |
uci1 | 64:6c7a316eafad | 708 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 709 | |
uci1 | 8:95a325df1f6b | 710 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 21:ce51bb0ba4a5 | 711 | if (bsize>=SnConfigFrame::kMaxSizeOf) { |
uci1 | 8:95a325df1f6b | 712 | // get header |
uci1 | 21:ce51bb0ba4a5 | 713 | uint8_t mcode=0; uint32_t mlen=0; |
uci1 | 21:ce51bb0ba4a5 | 714 | res = GetHeader(timeOut, confBuf, bsize, mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 715 | if (res>=SnCommWin::kOkWithMsg) { |
uci1 | 12:d472f9811262 | 716 | if (mcode==SnHeaderFrame::kNoConfigCode) { |
uci1 | 12:d472f9811262 | 717 | // no config to get |
uci1 | 16:744ce85aede2 | 718 | res = SnCommWin::kOkWthMsgNoConf; |
uci1 | 21:ce51bb0ba4a5 | 719 | } else if (mcode==SnHeaderFrame::kMbedFilenameCode) { |
uci1 | 21:ce51bb0ba4a5 | 720 | res = GetFilename(timeOut, confBuf, mlen); |
uci1 | 21:ce51bb0ba4a5 | 721 | if (res>kAllFails) { |
uci1 | 54:ea1234a44fe8 | 722 | std::string fnameb(confBuf, mlen); // the filename |
uci1 | 54:ea1234a44fe8 | 723 | std::string fname(fnameb.c_str()); // mlen may include the \0 which would be counted in size() |
uci1 | 21:ce51bb0ba4a5 | 724 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 725 | printf("got filename = [%s] (%u)\r\n", fname.c_str(), fname.size()); |
uci1 | 21:ce51bb0ba4a5 | 726 | #endif |
uci1 | 21:ce51bb0ba4a5 | 727 | res = GetLocalFile(fname, confBuf, bsize, timeOut); |
uci1 | 21:ce51bb0ba4a5 | 728 | } |
uci1 | 12:d472f9811262 | 729 | } else if (mcode!=SnHeaderFrame::kConfigCode) { |
uci1 | 8:95a325df1f6b | 730 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 731 | } else { |
uci1 | 8:95a325df1f6b | 732 | // get config |
uci1 | 37:ff95e7070f26 | 733 | const int clen = fComm->ReceiveAll(confBuf, mlen, timeOut); |
uci1 | 8:95a325df1f6b | 734 | if (clen>0 && static_cast<uint32_t>(clen)==mlen) { |
uci1 | 40:1324da35afd4 | 735 | const uint8_t Rv = SnConfigFrame::PeekIOversion(confBuf); |
uci1 | 40:1324da35afd4 | 736 | if (SnConfigFrame::IsIOversionOk(Rv)) { |
uci1 | 40:1324da35afd4 | 737 | const char* b = confBuf; |
uci1 | 40:1324da35afd4 | 738 | conf.ReadFrom(b); |
uci1 | 40:1324da35afd4 | 739 | res = SnCommWin::kOkWithMsg; |
uci1 | 40:1324da35afd4 | 740 | } else { |
uci1 | 40:1324da35afd4 | 741 | res = SnCommWin::kUnexpectedRec; |
uci1 | 40:1324da35afd4 | 742 | char s[256]; |
uci1 | 40:1324da35afd4 | 743 | sprintf(s,"Cannot accept config version [%hhu]. " |
uci1 | 40:1324da35afd4 | 744 | "Expect [%hhu].",Rv,SnConfigFrame::kIOVers); |
uci1 | 40:1324da35afd4 | 745 | SendString(s, timeOut); |
uci1 | 40:1324da35afd4 | 746 | } |
uci1 | 8:95a325df1f6b | 747 | } else { |
uci1 | 8:95a325df1f6b | 748 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 749 | } |
uci1 | 8:95a325df1f6b | 750 | } |
uci1 | 8:95a325df1f6b | 751 | } else { |
uci1 | 21:ce51bb0ba4a5 | 752 | // not a problem if we get nothing (or no config) |
uci1 | 16:744ce85aede2 | 753 | res = SnCommWin::kOkNoMsg; |
uci1 | 8:95a325df1f6b | 754 | } |
uci1 | 8:95a325df1f6b | 755 | } |
uci1 | 8:95a325df1f6b | 756 | return res; |
uci1 | 8:95a325df1f6b | 757 | } |
uci1 | 8:95a325df1f6b | 758 | |
uci1 | 40:1324da35afd4 | 759 | int32_t SnCommWin::SendHndshkReq(char* const genBuf, |
uci1 | 40:1324da35afd4 | 760 | const uint32_t timeout_clock) { |
uci1 | 40:1324da35afd4 | 761 | // send a request demanding a handshake |
uci1 | 40:1324da35afd4 | 762 | // as we will be waiting for a response after this, |
uci1 | 40:1324da35afd4 | 763 | // FinishSending is also called |
uci1 | 64:6c7a316eafad | 764 | |
uci1 | 64:6c7a316eafad | 765 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 766 | |
uci1 | 40:1324da35afd4 | 767 | char* b = genBuf; |
uci1 | 40:1324da35afd4 | 768 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kHnShDemandCode, 0); |
uci1 | 40:1324da35afd4 | 769 | const uint32_t bytesToBeSent = b-genBuf; |
uci1 | 40:1324da35afd4 | 770 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 40:1324da35afd4 | 771 | const int32_t flen = fComm->FinishSending(timeout_clock); |
uci1 | 40:1324da35afd4 | 772 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 773 | printf("SendHndshkReq: time = %u, timeout = %u, mlen=%d, flen=%d\r\n", |
uci1 | 40:1324da35afd4 | 774 | time(0), timeout_clock, mlen, flen); |
uci1 | 40:1324da35afd4 | 775 | #endif |
uci1 | 40:1324da35afd4 | 776 | return mlen+flen; |
uci1 | 40:1324da35afd4 | 777 | } |
uci1 | 40:1324da35afd4 | 778 | |
uci1 | 41:d6f5e2f09e07 | 779 | SnCommWin::ECommWinResult SnCommWin::SendSignalStrength(char* const genBuf, |
uci1 | 41:d6f5e2f09e07 | 780 | SnSignalStrengthFrame& sigstr, |
uci1 | 41:d6f5e2f09e07 | 781 | const uint32_t timeout_clock) { |
uci1 | 41:d6f5e2f09e07 | 782 | // try to get the signal strength and send it if possible |
uci1 | 64:6c7a316eafad | 783 | |
uci1 | 64:6c7a316eafad | 784 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 785 | |
uci1 | 41:d6f5e2f09e07 | 786 | float ss; |
uci1 | 41:d6f5e2f09e07 | 787 | if ( fComm->CheckSignalStrength(timeout_clock, ss) ) { |
uci1 | 41:d6f5e2f09e07 | 788 | char* b = genBuf; |
uci1 | 41:d6f5e2f09e07 | 789 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kSignalStrCode, sigstr.SizeOf()); |
uci1 | 41:d6f5e2f09e07 | 790 | sigstr.SetSigStr(ss, time(0)); |
uci1 | 41:d6f5e2f09e07 | 791 | sigstr.SetCommType( static_cast<uint8_t>(GetCommType()) ); |
uci1 | 41:d6f5e2f09e07 | 792 | sigstr.WriteTo(b); |
uci1 | 41:d6f5e2f09e07 | 793 | const uint32_t bytesToBeSent = b - genBuf; |
uci1 | 41:d6f5e2f09e07 | 794 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 795 | const int32_t flen = fComm->FinishSending(timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 796 | if (bytesToBeSent==(mlen+flen)) { |
uci1 | 41:d6f5e2f09e07 | 797 | return SnCommWin::kOkMsgSent; |
uci1 | 41:d6f5e2f09e07 | 798 | } else { |
uci1 | 41:d6f5e2f09e07 | 799 | return SnCommWin::kFailPartSent; |
uci1 | 41:d6f5e2f09e07 | 800 | } |
uci1 | 41:d6f5e2f09e07 | 801 | } |
uci1 | 41:d6f5e2f09e07 | 802 | return SnCommWin::kFailNoneSent; |
uci1 | 41:d6f5e2f09e07 | 803 | } |
uci1 | 41:d6f5e2f09e07 | 804 | |
uci1 | 84:80b15993944e | 805 | bool SnCommWin::WriteStatusDataPackHeaderTo(char*& b, |
uci1 | 84:80b15993944e | 806 | const uint32_t payloadLen) { |
uci1 | 84:80b15993944e | 807 | return CallHeaderWriteTo(b, |
uci1 | 84:80b15993944e | 808 | SnHeaderFrame::kStatusDataPack, |
uci1 | 84:80b15993944e | 809 | SnHeaderFrame::SizeOf() // this is the header of the packed data, not the kStatusDataPack hdr itself |
uci1 | 84:80b15993944e | 810 | + payloadLen); |
uci1 | 84:80b15993944e | 811 | } |
uci1 | 84:80b15993944e | 812 | |
uci1 | 84:80b15993944e | 813 | bool SnCommWin::CallHeaderWriteTo(char*& b, |
uci1 | 84:80b15993944e | 814 | const uint8_t msgCode, |
uci1 | 84:80b15993944e | 815 | const uint32_t msgLen) { |
uci1 | 84:80b15993944e | 816 | // to call fro SnCommWin.h where we don't include SnHeaderFrame |
uci1 | 84:80b15993944e | 817 | return SnHeaderFrame::WriteTo(b, msgCode, msgLen); |
uci1 | 84:80b15993944e | 818 | } |
uci1 | 84:80b15993944e | 819 | |
uci1 | 84:80b15993944e | 820 | int32_t SnCommWin::CallSendAll(const char* const data, |
uci1 | 84:80b15993944e | 821 | const uint32_t length, |
uci1 | 84:80b15993944e | 822 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 823 | // to call fro SnCommWin.h where we don't include SnCommPeripheral |
uci1 | 84:80b15993944e | 824 | if (fComm!=0) { |
uci1 | 84:80b15993944e | 825 | return fComm->SendAll(data, length, timeout_clock); |
uci1 | 84:80b15993944e | 826 | } |
uci1 | 84:80b15993944e | 827 | return 0; |
uci1 | 84:80b15993944e | 828 | } |
uci1 | 84:80b15993944e | 829 | |
uci1 | 8:95a325df1f6b | 830 | SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf, |
uci1 | 84:80b15993944e | 831 | const SnPowerFrame& pow, // com win power |
uci1 | 84:80b15993944e | 832 | const SnEventFrame& stEvent, |
uci1 | 10:3c93db1cfb12 | 833 | const uint16_t seq, |
uci1 | 116:8099b754fbb4 | 834 | const uint32_t numThmTrigs, |
uci1 | 116:8099b754fbb4 | 835 | const uint32_t numSavedEvts, |
uci1 | 116:8099b754fbb4 | 836 | const float seqlive, |
uci1 | 56:0bba0ef15697 | 837 | const uint32_t powerOnTime, |
uci1 | 84:80b15993944e | 838 | const SnTempFrame& temper, // com win temp |
uci1 | 8:95a325df1f6b | 839 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 840 | const uint32_t timeout_clock) { |
uci1 | 8:95a325df1f6b | 841 | // TODO: check if connected? |
uci1 | 12:d472f9811262 | 842 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 843 | printf("########### Send Status\r\n"); |
uci1 | 12:d472f9811262 | 844 | #endif |
uci1 | 64:6c7a316eafad | 845 | |
uci1 | 64:6c7a316eafad | 846 | Watchdog::kick(); // don't reset |
uci1 | 84:80b15993944e | 847 | |
uci1 | 84:80b15993944e | 848 | int32_t toBeSent(0), byteSent(0), sendNow(0); |
uci1 | 21:ce51bb0ba4a5 | 849 | uint8_t loseLSB=0, loseMSB=0; |
uci1 | 21:ce51bb0ba4a5 | 850 | uint16_t wvBase=0; |
uci1 | 21:ce51bb0ba4a5 | 851 | conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase); |
uci1 | 8:95a325df1f6b | 852 | const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf); |
uci1 | 8:95a325df1f6b | 853 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 854 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize); |
uci1 | 116:8099b754fbb4 | 855 | SnStatusFrame::WriteTo(b, conf, seq, |
uci1 | 116:8099b754fbb4 | 856 | numThmTrigs, numSavedEvts, seqlive, |
uci1 | 56:0bba0ef15697 | 857 | powerOnTime, temper, |
uci1 | 21:ce51bb0ba4a5 | 858 | loseLSB, loseMSB, wvBase); |
uci1 | 10:3c93db1cfb12 | 859 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 10:3c93db1cfb12 | 860 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 10:3c93db1cfb12 | 861 | pow.WriteTo(b); |
uci1 | 84:80b15993944e | 862 | sendNow = b-genBuf; |
uci1 | 84:80b15993944e | 863 | toBeSent = sendNow; |
uci1 | 18:55f1581f2ee4 | 864 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 865 | printf("calling SendAll (status)\r\n"); |
uci1 | 18:55f1581f2ee4 | 866 | #endif |
uci1 | 84:80b15993944e | 867 | byteSent = fComm->SendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 868 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 869 | printf("SendAll returned %d\r\n",byteSent); |
uci1 | 84:80b15993944e | 870 | #endif |
uci1 | 84:80b15993944e | 871 | |
uci1 | 84:80b15993944e | 872 | //byteSent += fComm->FinishSending(timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 873 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 874 | printf("byteSent=%d toBeSent=%d\r\n",byteSent,toBeSent); |
uci1 | 84:80b15993944e | 875 | printf("status+power frame:\r\n"); |
uci1 | 84:80b15993944e | 876 | for (uint32_t i=0; i<sendNow; i++) { |
uci1 | 84:80b15993944e | 877 | printf("%02X ",genBuf[i]); |
uci1 | 84:80b15993944e | 878 | } |
uci1 | 84:80b15993944e | 879 | printf("\r\n"); |
uci1 | 41:d6f5e2f09e07 | 880 | #endif |
uci1 | 84:80b15993944e | 881 | |
uci1 | 84:80b15993944e | 882 | Watchdog::kick(); // don't reset |
uci1 | 84:80b15993944e | 883 | |
uci1 | 84:80b15993944e | 884 | // finally, mark the end of the status update |
uci1 | 84:80b15993944e | 885 | b = genBuf; |
uci1 | 84:80b15993944e | 886 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusUpdDone, 0); |
uci1 | 84:80b15993944e | 887 | sendNow = b-genBuf; |
uci1 | 84:80b15993944e | 888 | toBeSent += sendNow; |
uci1 | 8:95a325df1f6b | 889 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 890 | printf("calling SendAll (status update done)\r\n"); |
uci1 | 84:80b15993944e | 891 | #endif |
uci1 | 84:80b15993944e | 892 | byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 893 | |
uci1 | 84:80b15993944e | 894 | |
uci1 | 84:80b15993944e | 895 | /* |
uci1 | 84:80b15993944e | 896 | // event compression parameters must be the same |
uci1 | 84:80b15993944e | 897 | // as those sent in the status update |
uci1 | 84:80b15993944e | 898 | b = genBuf; |
uci1 | 84:80b15993944e | 899 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, |
uci1 | 84:80b15993944e | 900 | evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB)); |
uci1 | 84:80b15993944e | 901 | b = evt.WriteTo(b, loseLSB, loseMSB, wvBase); |
uci1 | 84:80b15993944e | 902 | msiz = b-genBuf; |
uci1 | 84:80b15993944e | 903 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 904 | printf("calling SendAll (event) %d:\r\n",msiz); |
uci1 | 15:f2569d8e4176 | 905 | for (uint32_t i=0; i<msiz; i++) { |
uci1 | 8:95a325df1f6b | 906 | printf("%02X ",genBuf[i]); |
uci1 | 8:95a325df1f6b | 907 | } |
uci1 | 8:95a325df1f6b | 908 | printf("\r\n"); |
uci1 | 8:95a325df1f6b | 909 | #endif |
uci1 | 84:80b15993944e | 910 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 84:80b15993944e | 911 | */ |
uci1 | 84:80b15993944e | 912 | |
uci1 | 84:80b15993944e | 913 | // finish sending whatever didn't make it out |
uci1 | 84:80b15993944e | 914 | byteSent += fComm->FinishSending(timeout_clock); |
uci1 | 84:80b15993944e | 915 | |
uci1 | 84:80b15993944e | 916 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 917 | printf("after FinishSending\r\n"); |
uci1 | 84:80b15993944e | 918 | printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent); |
uci1 | 84:80b15993944e | 919 | #endif |
uci1 | 84:80b15993944e | 920 | |
uci1 | 84:80b15993944e | 921 | Watchdog::kick(); // don't reset |
uci1 | 84:80b15993944e | 922 | |
uci1 | 84:80b15993944e | 923 | if (byteSent==toBeSent) { |
uci1 | 84:80b15993944e | 924 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 925 | printf("Sent all status bytes OK\r\n"); |
uci1 | 84:80b15993944e | 926 | #endif |
uci1 | 84:80b15993944e | 927 | return SnCommWin::kOkMsgSent; |
uci1 | 84:80b15993944e | 928 | } |
uci1 | 84:80b15993944e | 929 | |
uci1 | 12:d472f9811262 | 930 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 931 | printf("Status failed with part-sent\r\n"); |
uci1 | 84:80b15993944e | 932 | #endif |
uci1 | 84:80b15993944e | 933 | return SnCommWin::kFailPartSent; |
uci1 | 84:80b15993944e | 934 | } |
uci1 | 84:80b15993944e | 935 | |
uci1 | 84:80b15993944e | 936 | |
uci1 | 84:80b15993944e | 937 | SnCommWin::ECommWinResult SnCommWin::SendStatusData(const SnConfigFrame& conf, |
uci1 | 98:ce72ef143b9b | 938 | const SnConfigFrame& stConf, |
uci1 | 84:80b15993944e | 939 | const SnClockSetFrame& stTrgStartClk, |
uci1 | 84:80b15993944e | 940 | const SnClockSetFrame& stTrgStopClk, |
uci1 | 84:80b15993944e | 941 | const SnPowerFrame& stPower, |
uci1 | 84:80b15993944e | 942 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 943 | const SnTempFrame& stTemperature, |
uci1 | 84:80b15993944e | 944 | const SnHeartbeatFrame& stHeartbeat, |
uci1 | 84:80b15993944e | 945 | const bool stNewPower, |
uci1 | 84:80b15993944e | 946 | const bool stNewEvent, |
uci1 | 84:80b15993944e | 947 | const bool stNewHeartbeat, |
uci1 | 84:80b15993944e | 948 | const bool stNewTemperature, |
uci1 | 84:80b15993944e | 949 | char* const genBuf, |
uci1 | 84:80b15993944e | 950 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 951 | // |
uci1 | 84:80b15993944e | 952 | // |
uci1 | 84:80b15993944e | 953 | // check what data we are sending with the status frame. |
uci1 | 84:80b15993944e | 954 | // |
uci1 | 84:80b15993944e | 955 | // ordered like they appear in SnEvts files |
uci1 | 84:80b15993944e | 956 | // |
uci1 | 84:80b15993944e | 957 | // |
uci1 | 84:80b15993944e | 958 | |
uci1 | 84:80b15993944e | 959 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 960 | printf("########### Send Status Data\r\n"); |
uci1 | 12:d472f9811262 | 961 | #endif |
uci1 | 84:80b15993944e | 962 | |
uci1 | 84:80b15993944e | 963 | Watchdog::kick(); // don't reset |
uci1 | 84:80b15993944e | 964 | |
uci1 | 84:80b15993944e | 965 | int32_t toBeSent(0), byteSent(0), sendNow(0); |
uci1 | 84:80b15993944e | 966 | char* b = genBuf; |
uci1 | 84:80b15993944e | 967 | |
uci1 | 84:80b15993944e | 968 | // power reading first |
uci1 | 84:80b15993944e | 969 | if (conf.IsStatusSendingPowerReading() && stNewPower) { |
uci1 | 84:80b15993944e | 970 | SendStatusDataPack(stPower, |
uci1 | 84:80b15993944e | 971 | SnHeaderFrame::kPowerCode, |
uci1 | 84:80b15993944e | 972 | toBeSent, |
uci1 | 84:80b15993944e | 973 | byteSent, |
uci1 | 84:80b15993944e | 974 | genBuf, |
uci1 | 84:80b15993944e | 975 | timeout_clock); |
uci1 | 84:80b15993944e | 976 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 977 | printf("Send status power data. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 978 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 979 | #endif |
uci1 | 84:80b15993944e | 980 | } |
uci1 | 84:80b15993944e | 981 | |
uci1 | 84:80b15993944e | 982 | // config next |
uci1 | 84:80b15993944e | 983 | if (conf.IsStatusSendingConfig()) { |
uci1 | 98:ce72ef143b9b | 984 | SendStatusDataPack(stConf, |
uci1 | 84:80b15993944e | 985 | SnHeaderFrame::kConfigCode, |
uci1 | 84:80b15993944e | 986 | toBeSent, |
uci1 | 84:80b15993944e | 987 | byteSent, |
uci1 | 84:80b15993944e | 988 | genBuf, |
uci1 | 84:80b15993944e | 989 | timeout_clock); |
uci1 | 84:80b15993944e | 990 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 991 | printf("Send status config. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 992 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 993 | #endif |
uci1 | 84:80b15993944e | 994 | } |
uci1 | 84:80b15993944e | 995 | |
uci1 | 84:80b15993944e | 996 | // trigger start time next |
uci1 | 84:80b15993944e | 997 | if (conf.IsStatusSendingTrigTimes() ) { |
uci1 | 84:80b15993944e | 998 | SendStatusDataPack(stTrgStartClk, |
uci1 | 84:80b15993944e | 999 | SnHeaderFrame::kFileTrgStrtCode, |
uci1 | 84:80b15993944e | 1000 | toBeSent, |
uci1 | 84:80b15993944e | 1001 | byteSent, |
uci1 | 84:80b15993944e | 1002 | genBuf, |
uci1 | 84:80b15993944e | 1003 | timeout_clock); |
uci1 | 84:80b15993944e | 1004 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1005 | printf("Send status trigger start. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 1006 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 1007 | #endif |
uci1 | 84:80b15993944e | 1008 | } |
uci1 | 84:80b15993944e | 1009 | |
uci1 | 84:80b15993944e | 1010 | // event next |
uci1 | 84:80b15993944e | 1011 | if (conf.IsStatusSendingEvent() && stNewEvent) { |
uci1 | 84:80b15993944e | 1012 | // event has a different WriteTo |
uci1 | 8:95a325df1f6b | 1013 | b = genBuf; |
uci1 | 84:80b15993944e | 1014 | const uint32_t evtLen = stEvent.SizeOf(SnEventFrame::kIOVers, |
uci1 | 84:80b15993944e | 1015 | conf.GetWvLoseLSB(), |
uci1 | 84:80b15993944e | 1016 | conf.GetWvLoseMSB()); |
uci1 | 84:80b15993944e | 1017 | WriteStatusDataPackHeaderTo(b, evtLen); |
uci1 | 84:80b15993944e | 1018 | CallHeaderWriteTo(b, SnHeaderFrame::kEventCode, evtLen); |
uci1 | 84:80b15993944e | 1019 | b = stEvent.WriteTo(b, |
uci1 | 84:80b15993944e | 1020 | conf.GetWvLoseLSB(), |
uci1 | 84:80b15993944e | 1021 | conf.GetWvLoseMSB(), |
uci1 | 84:80b15993944e | 1022 | conf.GetWvBaseline()); |
uci1 | 84:80b15993944e | 1023 | sendNow = (b - genBuf); |
uci1 | 84:80b15993944e | 1024 | toBeSent += sendNow; |
uci1 | 84:80b15993944e | 1025 | byteSent += CallSendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 1026 | |
uci1 | 84:80b15993944e | 1027 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1028 | printf("Send status event. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 1029 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 1030 | #endif |
uci1 | 84:80b15993944e | 1031 | } |
uci1 | 84:80b15993944e | 1032 | |
uci1 | 84:80b15993944e | 1033 | // temperature next |
uci1 | 84:80b15993944e | 1034 | if (conf.IsStatusSendingTemperature() && stNewTemperature) { |
uci1 | 84:80b15993944e | 1035 | SendStatusDataPack(stTemperature, |
uci1 | 84:80b15993944e | 1036 | SnHeaderFrame::kTemperatureCode, |
uci1 | 84:80b15993944e | 1037 | toBeSent, |
uci1 | 84:80b15993944e | 1038 | byteSent, |
uci1 | 84:80b15993944e | 1039 | genBuf, |
uci1 | 84:80b15993944e | 1040 | timeout_clock); |
uci1 | 84:80b15993944e | 1041 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1042 | printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 1043 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 1044 | #endif |
uci1 | 84:80b15993944e | 1045 | } |
uci1 | 84:80b15993944e | 1046 | |
uci1 | 84:80b15993944e | 1047 | // heartbeat next |
uci1 | 84:80b15993944e | 1048 | if (conf.IsStatusSendingHeartbeat() && stNewHeartbeat) { |
uci1 | 84:80b15993944e | 1049 | SendStatusDataPack(stHeartbeat, |
uci1 | 84:80b15993944e | 1050 | SnHeaderFrame::kHeartbeatCode, |
uci1 | 84:80b15993944e | 1051 | toBeSent, |
uci1 | 84:80b15993944e | 1052 | byteSent, |
uci1 | 84:80b15993944e | 1053 | genBuf, |
uci1 | 84:80b15993944e | 1054 | timeout_clock); |
uci1 | 16:744ce85aede2 | 1055 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1056 | printf("Send status temperature. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 1057 | toBeSent, byteSent); |
uci1 | 84:80b15993944e | 1058 | #endif |
uci1 | 84:80b15993944e | 1059 | } |
uci1 | 84:80b15993944e | 1060 | |
uci1 | 84:80b15993944e | 1061 | // trigger stop time next |
uci1 | 84:80b15993944e | 1062 | if (conf.IsStatusSendingTrigTimes() ) { |
uci1 | 84:80b15993944e | 1063 | SendStatusDataPack(stTrgStopClk, |
uci1 | 84:80b15993944e | 1064 | SnHeaderFrame::kFileTrgStopCode, |
uci1 | 84:80b15993944e | 1065 | toBeSent, |
uci1 | 84:80b15993944e | 1066 | byteSent, |
uci1 | 84:80b15993944e | 1067 | genBuf, |
uci1 | 84:80b15993944e | 1068 | timeout_clock); |
uci1 | 84:80b15993944e | 1069 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1070 | printf("Send status trigger stop. toBeSent=%u, byteSent=%u\r\n", |
uci1 | 84:80b15993944e | 1071 | toBeSent, byteSent); |
uci1 | 21:ce51bb0ba4a5 | 1072 | #endif |
uci1 | 8:95a325df1f6b | 1073 | } |
uci1 | 84:80b15993944e | 1074 | |
uci1 | 84:80b15993944e | 1075 | // finally, mark the end of the status update |
uci1 | 84:80b15993944e | 1076 | if (toBeSent>0) { |
uci1 | 84:80b15993944e | 1077 | b = genBuf; |
uci1 | 84:80b15993944e | 1078 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusDatPakDone, 0); |
uci1 | 84:80b15993944e | 1079 | sendNow = b-genBuf; |
uci1 | 84:80b15993944e | 1080 | toBeSent += sendNow; |
uci1 | 84:80b15993944e | 1081 | #ifdef DEBUG |
uci1 | 116:8099b754fbb4 | 1082 | printf("calling SendAll (status pak done)\r\n"); |
uci1 | 116:8099b754fbb4 | 1083 | printf("timed out = %s\r\n", (fComm->IsTimedOut(timeout_clock) ? "true" : "false")); |
uci1 | 116:8099b754fbb4 | 1084 | printf("time(0)=%u, timeout_clock=%u\r\n", time(0), timeout_clock); |
uci1 | 84:80b15993944e | 1085 | #endif |
uci1 | 84:80b15993944e | 1086 | byteSent += fComm->SendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 1087 | |
uci1 | 116:8099b754fbb4 | 1088 | #ifdef DEBUG |
uci1 | 116:8099b754fbb4 | 1089 | printf("calling FinishSending (status pak done)\r\n"); |
uci1 | 116:8099b754fbb4 | 1090 | #endif |
uci1 | 84:80b15993944e | 1091 | // finish sending whatever didn't make it out |
uci1 | 84:80b15993944e | 1092 | byteSent += fComm->FinishSending(timeout_clock); |
uci1 | 84:80b15993944e | 1093 | |
uci1 | 84:80b15993944e | 1094 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1095 | printf("after FinishSending status data\r\n"); |
uci1 | 84:80b15993944e | 1096 | #endif |
uci1 | 84:80b15993944e | 1097 | } // if toBeSent>0 |
uci1 | 84:80b15993944e | 1098 | |
uci1 | 84:80b15993944e | 1099 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1100 | printf("byteSent=%u, toBeSent=%u\r\n", byteSent, toBeSent); |
uci1 | 116:8099b754fbb4 | 1101 | printf("timed out = %s\r\n", (fComm->IsTimedOut(timeout_clock) ? "true" : "false")); |
uci1 | 116:8099b754fbb4 | 1102 | printf("time(0)=%u, timeout_clock=%u\r\n", time(0), timeout_clock); |
uci1 | 84:80b15993944e | 1103 | #endif |
uci1 | 84:80b15993944e | 1104 | |
uci1 | 84:80b15993944e | 1105 | if (byteSent==toBeSent) { |
uci1 | 84:80b15993944e | 1106 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1107 | printf("Sent all status data bytes OK\r\n"); |
uci1 | 84:80b15993944e | 1108 | #endif |
uci1 | 84:80b15993944e | 1109 | return SnCommWin::kOkMsgSent; |
uci1 | 84:80b15993944e | 1110 | } |
uci1 | 84:80b15993944e | 1111 | |
uci1 | 84:80b15993944e | 1112 | #ifdef DEBUG |
uci1 | 84:80b15993944e | 1113 | printf("Status data failed with part-sent\r\n"); |
uci1 | 84:80b15993944e | 1114 | #endif |
uci1 | 8:95a325df1f6b | 1115 | return SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 1116 | } |
uci1 | 8:95a325df1f6b | 1117 | |
uci1 | 27:efc4d654b139 | 1118 | |
uci1 | 23:ccf39298f205 | 1119 | SnCommWin::ECommWinResult SnCommWin::SendString(const char* str, |
uci1 | 23:ccf39298f205 | 1120 | const uint32_t timeout) { |
uci1 | 23:ccf39298f205 | 1121 | #ifdef DEBUG |
uci1 | 23:ccf39298f205 | 1122 | printf("SnCommWin::SendString %s\r\n",str); |
uci1 | 23:ccf39298f205 | 1123 | #endif |
uci1 | 64:6c7a316eafad | 1124 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1125 | |
uci1 | 37:ff95e7070f26 | 1126 | const bool ok = fComm->SendString(str, timeout); |
uci1 | 37:ff95e7070f26 | 1127 | return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 23:ccf39298f205 | 1128 | } |
uci1 | 23:ccf39298f205 | 1129 | |
uci1 | 40:1324da35afd4 | 1130 | int32_t SnCommWin::SendFilename(const char* inf, |
uci1 | 40:1324da35afd4 | 1131 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 1132 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 1133 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 1134 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1135 | printf("SnCommWin::SendFilename %s\r\n",inf); |
uci1 | 12:d472f9811262 | 1136 | #endif |
uci1 | 64:6c7a316eafad | 1137 | |
uci1 | 64:6c7a316eafad | 1138 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1139 | |
uci1 | 12:d472f9811262 | 1140 | // remove the directory |
uci1 | 12:d472f9811262 | 1141 | const char* fn = strrchr(inf, '/'); |
uci1 | 12:d472f9811262 | 1142 | if (fn==0) { |
uci1 | 12:d472f9811262 | 1143 | // no directory |
uci1 | 12:d472f9811262 | 1144 | fn = inf; |
uci1 | 12:d472f9811262 | 1145 | } else if (fn!=inf) { |
uci1 | 12:d472f9811262 | 1146 | ++fn; // move past the '/' if it was found |
uci1 | 12:d472f9811262 | 1147 | } |
uci1 | 12:d472f9811262 | 1148 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1149 | printf("send %s\r\n",fn); |
uci1 | 12:d472f9811262 | 1150 | #endif |
uci1 | 8:95a325df1f6b | 1151 | const size_t flen = strlen(fn); |
uci1 | 8:95a325df1f6b | 1152 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 1153 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen); |
uci1 | 12:d472f9811262 | 1154 | b = SnBitUtils::WriteTo(b, fn, flen); |
uci1 | 40:1324da35afd4 | 1155 | bytesToBeSent = b-genBuf; |
uci1 | 40:1324da35afd4 | 1156 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 12:d472f9811262 | 1157 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1158 | printf("time = %u, timeout = %u\r\n",time(0), timeout_clock); |
uci1 | 12:d472f9811262 | 1159 | #endif |
uci1 | 40:1324da35afd4 | 1160 | return mlen; |
uci1 | 8:95a325df1f6b | 1161 | } |
uci1 | 8:95a325df1f6b | 1162 | |
uci1 | 40:1324da35afd4 | 1163 | int32_t SnCommWin::SendFileBlock(FILE* inf, |
uci1 | 40:1324da35afd4 | 1164 | const uint8_t blockHeaderCode, |
uci1 | 40:1324da35afd4 | 1165 | const uint32_t blockSize, |
uci1 | 40:1324da35afd4 | 1166 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 1167 | int32_t& bytesToSend, |
uci1 | 40:1324da35afd4 | 1168 | const uint32_t timeout) { |
uci1 | 12:d472f9811262 | 1169 | // sends the block from the file. does not check if the file has sufficient bytes |
uci1 | 12:d472f9811262 | 1170 | // this should be done before calling this function |
uci1 | 12:d472f9811262 | 1171 | // (where the number of bytes in the file can be cached) |
uci1 | 40:1324da35afd4 | 1172 | // |
uci1 | 40:1324da35afd4 | 1173 | // bytesToSend will be set to the total bytes that should be delivered eventually |
uci1 | 40:1324da35afd4 | 1174 | // the number of bytes actually shipped out by SendAll is returned |
uci1 | 64:6c7a316eafad | 1175 | |
uci1 | 64:6c7a316eafad | 1176 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1177 | |
uci1 | 12:d472f9811262 | 1178 | char* b = genBuf; |
uci1 | 12:d472f9811262 | 1179 | SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize); |
uci1 | 12:d472f9811262 | 1180 | SnBitUtils::ReadFrom(inf, b, blockSize); |
uci1 | 12:d472f9811262 | 1181 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1182 | printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf()); |
uci1 | 12:d472f9811262 | 1183 | #endif |
uci1 | 40:1324da35afd4 | 1184 | bytesToSend = blockSize+SnHeaderFrame::SizeOf(); |
uci1 | 40:1324da35afd4 | 1185 | return fComm->SendAll(genBuf, bytesToSend, timeout); |
uci1 | 12:d472f9811262 | 1186 | } |
uci1 | 12:d472f9811262 | 1187 | |
uci1 | 40:1324da35afd4 | 1188 | int32_t SnCommWin::SendFileContents(FILE* inf, |
uci1 | 40:1324da35afd4 | 1189 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 1190 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 1191 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 1192 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 1193 | uint32_t nevts, |
uci1 | 40:1324da35afd4 | 1194 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 1195 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 1196 | // firstEvt==0 ==> start at beginning |
uci1 | 12:d472f9811262 | 1197 | // nevts==0 ==> all events |
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 | 12:d472f9811262 | 1201 | |
uci1 | 12:d472f9811262 | 1202 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1203 | printf("SendFileContents (byte streaming)\r\n"); |
uci1 | 12:d472f9811262 | 1204 | #endif |
uci1 | 12:d472f9811262 | 1205 | |
uci1 | 64:6c7a316eafad | 1206 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1207 | |
uci1 | 12:d472f9811262 | 1208 | // store position in file so we can go back to it afterwards |
uci1 | 12:d472f9811262 | 1209 | // this is probably not necessary, because if this file is open |
uci1 | 12:d472f9811262 | 1210 | // in write-only mode, we're screwed.. |
uci1 | 12:d472f9811262 | 1211 | const int fpos = ftell(inf); |
uci1 | 12:d472f9811262 | 1212 | if (fpos>0) { |
uci1 | 12:d472f9811262 | 1213 | fseek(inf, 0, SEEK_SET); |
uci1 | 12:d472f9811262 | 1214 | } |
uci1 | 12:d472f9811262 | 1215 | // how many bytes? |
uci1 | 12:d472f9811262 | 1216 | fseek(inf, 0, SEEK_END); // go to end |
uci1 | 12:d472f9811262 | 1217 | const int fend = ftell(inf); |
uci1 | 12:d472f9811262 | 1218 | fseek(inf, 0, SEEK_SET); // go to start |
uci1 | 12:d472f9811262 | 1219 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1220 | printf("fend=%d\r\n",fend); |
uci1 | 12:d472f9811262 | 1221 | printf("fpos=%d\r\n",fpos); |
uci1 | 12:d472f9811262 | 1222 | #endif |
uci1 | 12:d472f9811262 | 1223 | |
uci1 | 12:d472f9811262 | 1224 | // variables used when sending data |
uci1 | 12:d472f9811262 | 1225 | char* b = genBuf; |
uci1 | 40:1324da35afd4 | 1226 | int32_t msiz(0), mlen(0), mtogo(0), tsent(0); |
uci1 | 21:ce51bb0ba4a5 | 1227 | // count number of events / power readings sent |
uci1 | 40:1324da35afd4 | 1228 | uint32_t evtsSent=0, powsSent=0, crc=0; |
uci1 | 12:d472f9811262 | 1229 | |
uci1 | 64:6c7a316eafad | 1230 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1231 | |
uci1 | 12:d472f9811262 | 1232 | // first is the file header, which has no SnHeaderFrame |
uci1 | 12:d472f9811262 | 1233 | msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers); |
uci1 | 12:d472f9811262 | 1234 | bool ok = (ftell(inf)+msiz)<=fend; |
uci1 | 12:d472f9811262 | 1235 | if (ok) { |
uci1 | 12:d472f9811262 | 1236 | mlen = SendFileBlock(inf, SnHeaderFrame::kFileHeadrCode, |
uci1 | 40:1324da35afd4 | 1237 | msiz, genBuf, mtogo, timeout_clock); |
uci1 | 40:1324da35afd4 | 1238 | //ok &= (msiz+SnHeaderFrame::SizeOf())==mlen; |
uci1 | 40:1324da35afd4 | 1239 | bytesToBeSent += mtogo; |
uci1 | 40:1324da35afd4 | 1240 | tsent += mlen; |
uci1 | 12:d472f9811262 | 1241 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1242 | printf("sent file header. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 1243 | #endif |
uci1 | 64:6c7a316eafad | 1244 | |
uci1 | 64:6c7a316eafad | 1245 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1246 | |
uci1 | 40:1324da35afd4 | 1247 | // calc crc w/o the header block of the file header, as |
uci1 | 40:1324da35afd4 | 1248 | // this does not get stored in the file |
uci1 | 40:1324da35afd4 | 1249 | crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf+SnHeaderFrame::SizeOf(), |
uci1 | 40:1324da35afd4 | 1250 | mtogo-SnHeaderFrame::SizeOf()); |
uci1 | 40:1324da35afd4 | 1251 | |
uci1 | 12:d472f9811262 | 1252 | // the header info for each block |
uci1 | 40:1324da35afd4 | 1253 | uint8_t hcode(0); |
uci1 | 40:1324da35afd4 | 1254 | uint32_t hlen(0); |
uci1 | 12:d472f9811262 | 1255 | |
uci1 | 12:d472f9811262 | 1256 | // now just loop through file and send each block |
uci1 | 18:55f1581f2ee4 | 1257 | bool fok = true; |
uci1 | 18:55f1581f2ee4 | 1258 | while ( fok |
uci1 | 12:d472f9811262 | 1259 | && (feof(inf)==0) |
uci1 | 12:d472f9811262 | 1260 | && (ferror(inf)==0) |
uci1 | 12:d472f9811262 | 1261 | && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) { |
uci1 | 64:6c7a316eafad | 1262 | |
uci1 | 64:6c7a316eafad | 1263 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1264 | |
uci1 | 12:d472f9811262 | 1265 | // read the header for the block |
uci1 | 12:d472f9811262 | 1266 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 18:55f1581f2ee4 | 1267 | fok &= (ftell(inf)+hlen)<=fend; |
uci1 | 12:d472f9811262 | 1268 | #ifdef DEBUG |
uci1 | 18:55f1581f2ee4 | 1269 | printf("new block: hc=%02x, hl=%u, ftell=%d, fend=%d, " |
uci1 | 18:55f1581f2ee4 | 1270 | "ok=%d, fok=%d\r\n", |
uci1 | 18:55f1581f2ee4 | 1271 | hcode, hlen, ftell(inf), fend, (int)ok, (int)fok); |
uci1 | 12:d472f9811262 | 1272 | #endif |
uci1 | 40:1324da35afd4 | 1273 | if (fok) { |
uci1 | 12:d472f9811262 | 1274 | // send the block |
uci1 | 12:d472f9811262 | 1275 | // TODO: repack events? |
uci1 | 40:1324da35afd4 | 1276 | mlen = SendFileBlock(inf, hcode, hlen, genBuf, |
uci1 | 40:1324da35afd4 | 1277 | mtogo, timeout_clock); |
uci1 | 40:1324da35afd4 | 1278 | bytesToBeSent += mtogo; |
uci1 | 40:1324da35afd4 | 1279 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 1280 | crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf, mtogo); |
uci1 | 40:1324da35afd4 | 1281 | |
uci1 | 40:1324da35afd4 | 1282 | if (hcode==SnHeaderFrame::kEventCode) { |
uci1 | 40:1324da35afd4 | 1283 | ++evtsSent; |
uci1 | 40:1324da35afd4 | 1284 | if (nevts>0) { |
uci1 | 40:1324da35afd4 | 1285 | if (evtsSent>=nevts) { |
uci1 | 12:d472f9811262 | 1286 | #ifdef DEBUG |
uci1 | 56:0bba0ef15697 | 1287 | printf("sent %u events. stop\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 1288 | #endif |
uci1 | 40:1324da35afd4 | 1289 | break; |
uci1 | 12:d472f9811262 | 1290 | } |
uci1 | 12:d472f9811262 | 1291 | } |
uci1 | 40:1324da35afd4 | 1292 | } else if (hcode==SnHeaderFrame::kPowerCode) { |
uci1 | 40:1324da35afd4 | 1293 | ++powsSent; |
uci1 | 40:1324da35afd4 | 1294 | } |
uci1 | 40:1324da35afd4 | 1295 | } // otherwise file size not sufficient for this block |
uci1 | 21:ce51bb0ba4a5 | 1296 | } // loop over file contents |
uci1 | 21:ce51bb0ba4a5 | 1297 | } else { |
uci1 | 21:ce51bb0ba4a5 | 1298 | // otherwise file size not sufficient for file header |
uci1 | 21:ce51bb0ba4a5 | 1299 | // so it's either 0 or corrupted. |
uci1 | 21:ce51bb0ba4a5 | 1300 | // proceed as normal even tho no contents were sent. |
uci1 | 21:ce51bb0ba4a5 | 1301 | // if we're deleting files, this one will be deleted. |
uci1 | 21:ce51bb0ba4a5 | 1302 | ok = true; |
uci1 | 21:ce51bb0ba4a5 | 1303 | } |
uci1 | 12:d472f9811262 | 1304 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 1305 | printf("loop done. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 1306 | #endif |
uci1 | 40:1324da35afd4 | 1307 | |
uci1 | 64:6c7a316eafad | 1308 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1309 | |
uci1 | 40:1324da35afd4 | 1310 | // send the crc |
uci1 | 40:1324da35afd4 | 1311 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 1312 | printf("sending crc (%u)\r\n",crc); |
uci1 | 40:1324da35afd4 | 1313 | #endif |
uci1 | 40:1324da35afd4 | 1314 | b = genBuf; |
uci1 | 40:1324da35afd4 | 1315 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kMbedFileChksCode, sizeof(uint32_t)); |
uci1 | 40:1324da35afd4 | 1316 | b = SnBitUtils::WriteTo(b, crc); |
uci1 | 40:1324da35afd4 | 1317 | msiz = b - genBuf; |
uci1 | 40:1324da35afd4 | 1318 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 1319 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 1320 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 1321 | |
uci1 | 40:1324da35afd4 | 1322 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 1323 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 40:1324da35afd4 | 1324 | #endif |
uci1 | 40:1324da35afd4 | 1325 | |
uci1 | 64:6c7a316eafad | 1326 | Watchdog::kick(); // don't reset |
uci1 | 21:ce51bb0ba4a5 | 1327 | |
uci1 | 21:ce51bb0ba4a5 | 1328 | // send number of events sent |
uci1 | 12:d472f9811262 | 1329 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 1330 | printf("sending evtsSent (%u)\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 1331 | #endif |
uci1 | 21:ce51bb0ba4a5 | 1332 | b = genBuf; |
uci1 | 21:ce51bb0ba4a5 | 1333 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); |
uci1 | 21:ce51bb0ba4a5 | 1334 | b = SnBitUtils::WriteTo(b, evtsSent); |
uci1 | 21:ce51bb0ba4a5 | 1335 | msiz = b - genBuf; |
uci1 | 37:ff95e7070f26 | 1336 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 1337 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 1338 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 1339 | |
uci1 | 18:55f1581f2ee4 | 1340 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 1341 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 18:55f1581f2ee4 | 1342 | #endif |
uci1 | 21:ce51bb0ba4a5 | 1343 | |
uci1 | 64:6c7a316eafad | 1344 | Watchdog::kick(); // don't reset |
uci1 | 64:6c7a316eafad | 1345 | |
uci1 | 21:ce51bb0ba4a5 | 1346 | // send number of power readings sent |
uci1 | 12:d472f9811262 | 1347 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 1348 | printf("sending powsSent (%u)\r\n",powsSent); |
uci1 | 12:d472f9811262 | 1349 | #endif |
uci1 | 21:ce51bb0ba4a5 | 1350 | b = genBuf; |
uci1 | 21:ce51bb0ba4a5 | 1351 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); |
uci1 | 21:ce51bb0ba4a5 | 1352 | b = SnBitUtils::WriteTo(b, powsSent); |
uci1 | 21:ce51bb0ba4a5 | 1353 | msiz = b - genBuf; |
uci1 | 37:ff95e7070f26 | 1354 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 1355 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 1356 | tsent += mlen; |
uci1 | 21:ce51bb0ba4a5 | 1357 | |
uci1 | 18:55f1581f2ee4 | 1358 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 1359 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 18:55f1581f2ee4 | 1360 | #endif |
uci1 | 12:d472f9811262 | 1361 | |
uci1 | 12:d472f9811262 | 1362 | // put file position back |
uci1 | 12:d472f9811262 | 1363 | fseek(inf, fpos, SEEK_SET); |
uci1 | 12:d472f9811262 | 1364 | |
uci1 | 12:d472f9811262 | 1365 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 1366 | printf("end contents: ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 1367 | #endif |
uci1 | 12:d472f9811262 | 1368 | |
uci1 | 40:1324da35afd4 | 1369 | return tsent; |
uci1 | 8:95a325df1f6b | 1370 | |
uci1 | 40:1324da35afd4 | 1371 | //return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 1372 | } |