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