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