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