Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.cpp@54:ea1234a44fe8, 2013-12-15 (annotated)
- Committer:
- uci1
- Date:
- Sun Dec 15 05:25:40 2013 +0000
- Revision:
- 54:ea1234a44fe8
- Parent:
- 41:d6f5e2f09e07
- Child:
- 56:0bba0ef15697
Like Rev53 but fixed a bug preventing the default config from being changed remotely. Also fixed the hard coded config to have thermal triggers disabled.
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 | 54:ea1234a44fe8 | 386 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 387 | printf("Got header code %x, len %u\r\n",mcode,mlen); |
uci1 | 54:ea1234a44fe8 | 388 | #endif |
uci1 | 54:ea1234a44fe8 | 389 | |
uci1 | 21:ce51bb0ba4a5 | 390 | if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileCode) ) { |
uci1 | 21:ce51bb0ba4a5 | 391 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 392 | printf("Got mbed file header. File length = %u\r\n",mlen); |
uci1 | 21:ce51bb0ba4a5 | 393 | #endif |
uci1 | 21:ce51bb0ba4a5 | 394 | // got the header.. make the file.. |
uci1 | 21:ce51bb0ba4a5 | 395 | // make the file name ALLCAPS, since LocalFileSystem will do it anyway |
uci1 | 37:ff95e7070f26 | 396 | SnCommPeripheral::CapitalizeInPlace(fname.begin(), fname.end()); |
uci1 | 21:ce51bb0ba4a5 | 397 | // now ensure the file name is 8.3 only -- for LocalFileSystem |
uci1 | 21:ce51bb0ba4a5 | 398 | const size_t ldlen = strlen(kLocalDir); |
uci1 | 21:ce51bb0ba4a5 | 399 | // 12 = 8.3 filename format, 1 for / and 1 for \0 |
uci1 | 21:ce51bb0ba4a5 | 400 | const uint32_t fbs = ldlen+1+12+1; |
uci1 | 21:ce51bb0ba4a5 | 401 | char fnb[fbs]; |
uci1 | 36:87865913ae6f | 402 | memset(fnb, 0, sizeof(char)*fbs); |
uci1 | 40:1324da35afd4 | 403 | // find the extension |
uci1 | 40:1324da35afd4 | 404 | const size_t xdot = fname.rfind('.'); |
uci1 | 40:1324da35afd4 | 405 | if (xdot!=string::npos && (fname.size()==(4+xdot))) { |
uci1 | 40:1324da35afd4 | 406 | const size_t fnpl = (xdot>8) ? 8 : xdot; |
uci1 | 40:1324da35afd4 | 407 | char* fb = fnb; |
uci1 | 40:1324da35afd4 | 408 | memcpy(fb, kLocalDir, ldlen); fb+=ldlen; // /local |
uci1 | 40:1324da35afd4 | 409 | *fb = '/'; ++fb; // / |
uci1 | 40:1324da35afd4 | 410 | strncpy(fb, fname.c_str(), fnpl); fb+=fnpl; // FILENAME |
uci1 | 40:1324da35afd4 | 411 | *fb = '.'; ++fb; // . |
uci1 | 40:1324da35afd4 | 412 | strncpy(fb, fname.c_str()+xdot+1, 3); // EXT |
uci1 | 40:1324da35afd4 | 413 | // all that just for the file name! |
uci1 | 40:1324da35afd4 | 414 | FILE* lf = fopen(fnb,"wb"); |
uci1 | 21:ce51bb0ba4a5 | 415 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 416 | printf("tried to open file [%s]. lf=%p\r\n",fnb,(void*)lf); |
uci1 | 21:ce51bb0ba4a5 | 417 | #endif |
uci1 | 40:1324da35afd4 | 418 | // get all the data and dump it into the file |
uci1 | 40:1324da35afd4 | 419 | int b = 0, toget = 0; |
uci1 | 40:1324da35afd4 | 420 | while ( mlen>b ) { |
uci1 | 40:1324da35afd4 | 421 | if (fComm->IsTimedOut(timeout)) { |
uci1 | 36:87865913ae6f | 422 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 423 | printf("timeout while getting file\r\n"); |
uci1 | 36:87865913ae6f | 424 | #endif |
uci1 | 40:1324da35afd4 | 425 | res = kFailTimeout; |
uci1 | 21:ce51bb0ba4a5 | 426 | break; |
uci1 | 21:ce51bb0ba4a5 | 427 | } |
uci1 | 40:1324da35afd4 | 428 | toget = mlen - b; |
uci1 | 40:1324da35afd4 | 429 | if (toget>bsize) { |
uci1 | 40:1324da35afd4 | 430 | toget = bsize; |
uci1 | 40:1324da35afd4 | 431 | } |
uci1 | 40:1324da35afd4 | 432 | const int got = fComm->ReceiveAll(buf, toget, timeout); |
uci1 | 40:1324da35afd4 | 433 | if (lf!=NULL) { |
uci1 | 36:87865913ae6f | 434 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 435 | printf("writing %d bytes to file\r\n", got); |
uci1 | 36:87865913ae6f | 436 | #endif |
uci1 | 40:1324da35afd4 | 437 | SnBitUtils::WriteTo(lf, buf, got); |
uci1 | 40:1324da35afd4 | 438 | } |
uci1 | 40:1324da35afd4 | 439 | b += got; |
uci1 | 40:1324da35afd4 | 440 | } // file data receive loop |
uci1 | 40:1324da35afd4 | 441 | uint32_t crc=0; |
uci1 | 40:1324da35afd4 | 442 | if (lf!=NULL) { |
uci1 | 40:1324da35afd4 | 443 | // calculate the crc from what's actually in the file |
uci1 | 40:1324da35afd4 | 444 | fclose(lf); // to flush it |
uci1 | 36:87865913ae6f | 445 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 446 | printf("fopen: %s\r\n",fnb); |
uci1 | 36:87865913ae6f | 447 | #endif |
uci1 | 40:1324da35afd4 | 448 | lf = fopen(fnb,"rb"); |
uci1 | 40:1324da35afd4 | 449 | fseek(lf, 0, SEEK_END); |
uci1 | 40:1324da35afd4 | 450 | int32_t fend = ftell(lf); |
uci1 | 40:1324da35afd4 | 451 | fseek(lf, 0, SEEK_SET); |
uci1 | 40:1324da35afd4 | 452 | char c; |
uci1 | 40:1324da35afd4 | 453 | for (int32_t i=0; i<fend; ++i) { |
uci1 | 40:1324da35afd4 | 454 | SnBitUtils::ReadFrom(lf, c); |
uci1 | 40:1324da35afd4 | 455 | if (feof(lf)==0 && ferror(lf)==0) { |
uci1 | 40:1324da35afd4 | 456 | crc = SnCRCUtils::update_crc32_xfer(crc, c); |
uci1 | 40:1324da35afd4 | 457 | } else { |
uci1 | 40:1324da35afd4 | 458 | break; |
uci1 | 40:1324da35afd4 | 459 | } |
uci1 | 21:ce51bb0ba4a5 | 460 | } |
uci1 | 40:1324da35afd4 | 461 | fclose(lf); |
uci1 | 40:1324da35afd4 | 462 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 463 | printf("closed file. crc = %u\r\n", crc); |
uci1 | 40:1324da35afd4 | 464 | #endif |
uci1 | 40:1324da35afd4 | 465 | } // if output file not 0 |
uci1 | 40:1324da35afd4 | 466 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 467 | printf("mlen=%u, b=%d, eq=%s\r\n",mlen,b, |
uci1 | 40:1324da35afd4 | 468 | (mlen==b)?"true":"false"); |
uci1 | 40:1324da35afd4 | 469 | #endif |
uci1 | 40:1324da35afd4 | 470 | if ( mlen!=b ) { |
uci1 | 40:1324da35afd4 | 471 | if (res > kUnexpectedRec) { |
uci1 | 40:1324da35afd4 | 472 | res = kUnexpectedRec; |
uci1 | 40:1324da35afd4 | 473 | } // otherwise it's already worse |
uci1 | 40:1324da35afd4 | 474 | } else { |
uci1 | 40:1324da35afd4 | 475 | // check that the file is ok |
uci1 | 40:1324da35afd4 | 476 | // get the checksum |
uci1 | 40:1324da35afd4 | 477 | res = GetHeader(timeout, buf, bsize, mcode, mlen); |
uci1 | 40:1324da35afd4 | 478 | if ( (res>=kOkWithMsg) && (mcode==SnHeaderFrame::kMbedFileChksCode) ) { |
uci1 | 40:1324da35afd4 | 479 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 480 | printf("received chksum=%u, crc=%u, eq=%s\r\n", |
uci1 | 40:1324da35afd4 | 481 | mlen,crc,(crc==mlen)?"true":"false"); |
uci1 | 40:1324da35afd4 | 482 | #endif |
uci1 | 40:1324da35afd4 | 483 | if (crc != mlen) { |
uci1 | 36:87865913ae6f | 484 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 485 | printf("checksums differ! unexpected.\r\n"); |
uci1 | 40:1324da35afd4 | 486 | #endif |
uci1 | 40:1324da35afd4 | 487 | res = kUnexpectedRec; // important! |
uci1 | 40:1324da35afd4 | 488 | } |
uci1 | 40:1324da35afd4 | 489 | } |
uci1 | 40:1324da35afd4 | 490 | } // end get remote file checksum |
uci1 | 40:1324da35afd4 | 491 | |
uci1 | 40:1324da35afd4 | 492 | if (lf!=NULL) { |
uci1 | 40:1324da35afd4 | 493 | if ( res<kOkWithMsg ) { |
uci1 | 40:1324da35afd4 | 494 | // timeout, bad checksum, something else bad? |
uci1 | 40:1324da35afd4 | 495 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 496 | printf("removing %s\r\n",fnb); |
uci1 | 36:87865913ae6f | 497 | #endif |
uci1 | 40:1324da35afd4 | 498 | remove(fnb); // delete the file |
uci1 | 40:1324da35afd4 | 499 | } else { |
uci1 | 40:1324da35afd4 | 500 | // if we got a new program, remove the old ones. |
uci1 | 40:1324da35afd4 | 501 | // (schedule myself for immediate de-resolution) |
uci1 | 40:1324da35afd4 | 502 | // |
uci1 | 40:1324da35afd4 | 503 | // first just count how many we would save |
uci1 | 40:1324da35afd4 | 504 | const uint16_t nSavedBins = LoopLocalDirBinFiles(false, fname); |
uci1 | 36:87865913ae6f | 505 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 506 | printf("nSavedBins=%hu",nSavedBins); |
uci1 | 36:87865913ae6f | 507 | #endif |
uci1 | 40:1324da35afd4 | 508 | if (nSavedBins==1) { |
uci1 | 40:1324da35afd4 | 509 | // ok. new program will be the only one. remove the others |
uci1 | 40:1324da35afd4 | 510 | // hope the new program works! |
uci1 | 40:1324da35afd4 | 511 | LoopLocalDirBinFiles(true, fname); |
uci1 | 36:87865913ae6f | 512 | |
uci1 | 36:87865913ae6f | 513 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 514 | printf("rebooting\r\n"); |
uci1 | 36:87865913ae6f | 515 | #endif |
uci1 | 40:1324da35afd4 | 516 | // goodbye cruel world, it's over. walk on by... |
uci1 | 40:1324da35afd4 | 517 | mbed_reset(); |
uci1 | 40:1324da35afd4 | 518 | } |
uci1 | 40:1324da35afd4 | 519 | } // end check new program block |
uci1 | 40:1324da35afd4 | 520 | } // if output file not 0 |
uci1 | 40:1324da35afd4 | 521 | } else { |
uci1 | 40:1324da35afd4 | 522 | // filename mangled. either doesn't contain a '.' |
uci1 | 40:1324da35afd4 | 523 | // or the extension is longer than 3 letters |
uci1 | 54:ea1234a44fe8 | 524 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 525 | printf("filname mangled. (%s) size=%u, xdot=%u", |
uci1 | 54:ea1234a44fe8 | 526 | fname.c_str(), fname.size(), xdot); |
uci1 | 54:ea1234a44fe8 | 527 | #endif |
uci1 | 40:1324da35afd4 | 528 | res = SnCommWin::kUnexpectedRec; |
uci1 | 21:ce51bb0ba4a5 | 529 | } |
uci1 | 40:1324da35afd4 | 530 | } // if header ok |
uci1 | 54:ea1234a44fe8 | 531 | |
uci1 | 21:ce51bb0ba4a5 | 532 | return res; |
uci1 | 21:ce51bb0ba4a5 | 533 | } |
uci1 | 21:ce51bb0ba4a5 | 534 | |
uci1 | 21:ce51bb0ba4a5 | 535 | int16_t SnCommWin::LoopLocalDirBinFiles(const bool doRemove, |
uci1 | 21:ce51bb0ba4a5 | 536 | const std::string& fname) { |
uci1 | 21:ce51bb0ba4a5 | 537 | // loop over the local directory, count the BIN files that match 'fname' |
uci1 | 21:ce51bb0ba4a5 | 538 | // |
uci1 | 21:ce51bb0ba4a5 | 539 | // fname is assumed to be all caps already! |
uci1 | 21:ce51bb0ba4a5 | 540 | // |
uci1 | 21:ce51bb0ba4a5 | 541 | // if doRemove==true, will actually delete non-matching files |
uci1 | 21:ce51bb0ba4a5 | 542 | // |
uci1 | 21:ce51bb0ba4a5 | 543 | // return number of non-matching files |
uci1 | 21:ce51bb0ba4a5 | 544 | uint16_t nSavedBins = 0; |
uci1 | 21:ce51bb0ba4a5 | 545 | DIR* d( opendir(kLocalDir) ); |
uci1 | 21:ce51bb0ba4a5 | 546 | struct dirent* dent; |
uci1 | 21:ce51bb0ba4a5 | 547 | if ( d!=NULL ) { |
uci1 | 21:ce51bb0ba4a5 | 548 | while ( (dent = readdir(d))!=NULL ) { |
uci1 | 21:ce51bb0ba4a5 | 549 | const size_t flen = strlen(dent->d_name); |
uci1 | 21:ce51bb0ba4a5 | 550 | const char* dext = dent->d_name + flen - 4; |
uci1 | 21:ce51bb0ba4a5 | 551 | if ( strncmp(dext,".BIN",4)==0 || strncmp(dext,".bin",4)==0 ) { |
uci1 | 21:ce51bb0ba4a5 | 552 | // ends with .BIN |
uci1 | 21:ce51bb0ba4a5 | 553 | if (strncmp(dent->d_name, fname.c_str(), fname.size())!=0) { |
uci1 | 21:ce51bb0ba4a5 | 554 | // some other mbed program than our new one |
uci1 | 21:ce51bb0ba4a5 | 555 | std::string dfn(kLocalDir); |
uci1 | 21:ce51bb0ba4a5 | 556 | dfn += "/"; |
uci1 | 21:ce51bb0ba4a5 | 557 | dfn += dent->d_name; |
uci1 | 21:ce51bb0ba4a5 | 558 | if (doRemove) { |
uci1 | 21:ce51bb0ba4a5 | 559 | remove(dfn.c_str()); |
uci1 | 21:ce51bb0ba4a5 | 560 | } |
uci1 | 21:ce51bb0ba4a5 | 561 | } else { |
uci1 | 21:ce51bb0ba4a5 | 562 | ++nSavedBins; |
uci1 | 21:ce51bb0ba4a5 | 563 | } |
uci1 | 21:ce51bb0ba4a5 | 564 | } // else not an mbed program |
uci1 | 21:ce51bb0ba4a5 | 565 | } |
uci1 | 21:ce51bb0ba4a5 | 566 | closedir(d); |
uci1 | 21:ce51bb0ba4a5 | 567 | } |
uci1 | 21:ce51bb0ba4a5 | 568 | return nSavedBins; |
uci1 | 21:ce51bb0ba4a5 | 569 | } |
uci1 | 21:ce51bb0ba4a5 | 570 | |
uci1 | 21:ce51bb0ba4a5 | 571 | SnCommWin::ECommWinResult SnCommWin::GetHeader(const uint32_t timeOut, |
uci1 | 21:ce51bb0ba4a5 | 572 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 573 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 574 | uint8_t& mcode, |
uci1 | 21:ce51bb0ba4a5 | 575 | uint32_t& mlen) { |
uci1 | 21:ce51bb0ba4a5 | 576 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 21:ce51bb0ba4a5 | 577 | if (bsize>=SnHeaderFrame::kMaxSizeOf) { |
uci1 | 21:ce51bb0ba4a5 | 578 | // get header |
uci1 | 37:ff95e7070f26 | 579 | const int hlen = fComm->ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeOut); |
uci1 | 21:ce51bb0ba4a5 | 580 | if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) { |
uci1 | 21:ce51bb0ba4a5 | 581 | mcode=0; |
uci1 | 21:ce51bb0ba4a5 | 582 | mlen=0; |
uci1 | 21:ce51bb0ba4a5 | 583 | const char* b = buf; |
uci1 | 21:ce51bb0ba4a5 | 584 | SnHeaderFrame::ReadFrom(b, mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 585 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 586 | printf("mcode=%02x, mlen=%u\r\n", mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 587 | #endif |
uci1 | 21:ce51bb0ba4a5 | 588 | res = SnCommWin::kOkWithMsg; |
uci1 | 21:ce51bb0ba4a5 | 589 | } |
uci1 | 21:ce51bb0ba4a5 | 590 | } |
uci1 | 21:ce51bb0ba4a5 | 591 | return res; |
uci1 | 21:ce51bb0ba4a5 | 592 | } |
uci1 | 21:ce51bb0ba4a5 | 593 | |
uci1 | 8:95a325df1f6b | 594 | SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 595 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 596 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 597 | const uint32_t bsize) { |
uci1 | 8:95a325df1f6b | 598 | // confBuf assumed to alread be of allocated size |
uci1 | 8:95a325df1f6b | 599 | |
uci1 | 12:d472f9811262 | 600 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 601 | printf("GetConfig, to=%u\r\n",timeOut); |
uci1 | 12:d472f9811262 | 602 | #endif |
uci1 | 8:95a325df1f6b | 603 | |
uci1 | 8:95a325df1f6b | 604 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 21:ce51bb0ba4a5 | 605 | if (bsize>=SnConfigFrame::kMaxSizeOf) { |
uci1 | 8:95a325df1f6b | 606 | // get header |
uci1 | 21:ce51bb0ba4a5 | 607 | uint8_t mcode=0; uint32_t mlen=0; |
uci1 | 21:ce51bb0ba4a5 | 608 | res = GetHeader(timeOut, confBuf, bsize, mcode, mlen); |
uci1 | 21:ce51bb0ba4a5 | 609 | if (res>=SnCommWin::kOkWithMsg) { |
uci1 | 12:d472f9811262 | 610 | if (mcode==SnHeaderFrame::kNoConfigCode) { |
uci1 | 12:d472f9811262 | 611 | // no config to get |
uci1 | 16:744ce85aede2 | 612 | res = SnCommWin::kOkWthMsgNoConf; |
uci1 | 21:ce51bb0ba4a5 | 613 | } else if (mcode==SnHeaderFrame::kMbedFilenameCode) { |
uci1 | 21:ce51bb0ba4a5 | 614 | res = GetFilename(timeOut, confBuf, mlen); |
uci1 | 21:ce51bb0ba4a5 | 615 | if (res>kAllFails) { |
uci1 | 54:ea1234a44fe8 | 616 | std::string fnameb(confBuf, mlen); // the filename |
uci1 | 54:ea1234a44fe8 | 617 | std::string fname(fnameb.c_str()); // mlen may include the \0 which would be counted in size() |
uci1 | 21:ce51bb0ba4a5 | 618 | #ifdef DEBUG |
uci1 | 54:ea1234a44fe8 | 619 | printf("got filename = [%s] (%u)\r\n", fname.c_str(), fname.size()); |
uci1 | 21:ce51bb0ba4a5 | 620 | #endif |
uci1 | 21:ce51bb0ba4a5 | 621 | res = GetLocalFile(fname, confBuf, bsize, timeOut); |
uci1 | 21:ce51bb0ba4a5 | 622 | } |
uci1 | 12:d472f9811262 | 623 | } else if (mcode!=SnHeaderFrame::kConfigCode) { |
uci1 | 8:95a325df1f6b | 624 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 625 | } else { |
uci1 | 8:95a325df1f6b | 626 | // get config |
uci1 | 37:ff95e7070f26 | 627 | const int clen = fComm->ReceiveAll(confBuf, mlen, timeOut); |
uci1 | 8:95a325df1f6b | 628 | if (clen>0 && static_cast<uint32_t>(clen)==mlen) { |
uci1 | 40:1324da35afd4 | 629 | const uint8_t Rv = SnConfigFrame::PeekIOversion(confBuf); |
uci1 | 40:1324da35afd4 | 630 | if (SnConfigFrame::IsIOversionOk(Rv)) { |
uci1 | 40:1324da35afd4 | 631 | const char* b = confBuf; |
uci1 | 40:1324da35afd4 | 632 | conf.ReadFrom(b); |
uci1 | 40:1324da35afd4 | 633 | res = SnCommWin::kOkWithMsg; |
uci1 | 40:1324da35afd4 | 634 | } else { |
uci1 | 40:1324da35afd4 | 635 | res = SnCommWin::kUnexpectedRec; |
uci1 | 40:1324da35afd4 | 636 | char s[256]; |
uci1 | 40:1324da35afd4 | 637 | sprintf(s,"Cannot accept config version [%hhu]. " |
uci1 | 40:1324da35afd4 | 638 | "Expect [%hhu].",Rv,SnConfigFrame::kIOVers); |
uci1 | 40:1324da35afd4 | 639 | SendString(s, timeOut); |
uci1 | 40:1324da35afd4 | 640 | } |
uci1 | 8:95a325df1f6b | 641 | } else { |
uci1 | 8:95a325df1f6b | 642 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 643 | } |
uci1 | 8:95a325df1f6b | 644 | } |
uci1 | 8:95a325df1f6b | 645 | } else { |
uci1 | 21:ce51bb0ba4a5 | 646 | // not a problem if we get nothing (or no config) |
uci1 | 16:744ce85aede2 | 647 | res = SnCommWin::kOkNoMsg; |
uci1 | 8:95a325df1f6b | 648 | } |
uci1 | 8:95a325df1f6b | 649 | } |
uci1 | 8:95a325df1f6b | 650 | return res; |
uci1 | 8:95a325df1f6b | 651 | } |
uci1 | 8:95a325df1f6b | 652 | |
uci1 | 40:1324da35afd4 | 653 | int32_t SnCommWin::SendHndshkReq(char* const genBuf, |
uci1 | 40:1324da35afd4 | 654 | const uint32_t timeout_clock) { |
uci1 | 40:1324da35afd4 | 655 | // send a request demanding a handshake |
uci1 | 40:1324da35afd4 | 656 | // as we will be waiting for a response after this, |
uci1 | 40:1324da35afd4 | 657 | // FinishSending is also called |
uci1 | 40:1324da35afd4 | 658 | char* b = genBuf; |
uci1 | 40:1324da35afd4 | 659 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kHnShDemandCode, 0); |
uci1 | 40:1324da35afd4 | 660 | const uint32_t bytesToBeSent = b-genBuf; |
uci1 | 40:1324da35afd4 | 661 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 40:1324da35afd4 | 662 | const int32_t flen = fComm->FinishSending(timeout_clock); |
uci1 | 40:1324da35afd4 | 663 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 664 | printf("SendHndshkReq: time = %u, timeout = %u, mlen=%d, flen=%d\r\n", |
uci1 | 40:1324da35afd4 | 665 | time(0), timeout_clock, mlen, flen); |
uci1 | 40:1324da35afd4 | 666 | #endif |
uci1 | 40:1324da35afd4 | 667 | return mlen+flen; |
uci1 | 40:1324da35afd4 | 668 | } |
uci1 | 40:1324da35afd4 | 669 | |
uci1 | 41:d6f5e2f09e07 | 670 | SnCommWin::ECommWinResult SnCommWin::SendSignalStrength(char* const genBuf, |
uci1 | 41:d6f5e2f09e07 | 671 | SnSignalStrengthFrame& sigstr, |
uci1 | 41:d6f5e2f09e07 | 672 | const uint32_t timeout_clock) { |
uci1 | 41:d6f5e2f09e07 | 673 | // try to get the signal strength and send it if possible |
uci1 | 41:d6f5e2f09e07 | 674 | float ss; |
uci1 | 41:d6f5e2f09e07 | 675 | if ( fComm->CheckSignalStrength(timeout_clock, ss) ) { |
uci1 | 41:d6f5e2f09e07 | 676 | char* b = genBuf; |
uci1 | 41:d6f5e2f09e07 | 677 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kSignalStrCode, sigstr.SizeOf()); |
uci1 | 41:d6f5e2f09e07 | 678 | sigstr.SetSigStr(ss, time(0)); |
uci1 | 41:d6f5e2f09e07 | 679 | sigstr.SetCommType( static_cast<uint8_t>(GetCommType()) ); |
uci1 | 41:d6f5e2f09e07 | 680 | sigstr.WriteTo(b); |
uci1 | 41:d6f5e2f09e07 | 681 | const uint32_t bytesToBeSent = b - genBuf; |
uci1 | 41:d6f5e2f09e07 | 682 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 683 | const int32_t flen = fComm->FinishSending(timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 684 | if (bytesToBeSent==(mlen+flen)) { |
uci1 | 41:d6f5e2f09e07 | 685 | return SnCommWin::kOkMsgSent; |
uci1 | 41:d6f5e2f09e07 | 686 | } else { |
uci1 | 41:d6f5e2f09e07 | 687 | return SnCommWin::kFailPartSent; |
uci1 | 41:d6f5e2f09e07 | 688 | } |
uci1 | 41:d6f5e2f09e07 | 689 | } |
uci1 | 41:d6f5e2f09e07 | 690 | return SnCommWin::kFailNoneSent; |
uci1 | 41:d6f5e2f09e07 | 691 | } |
uci1 | 41:d6f5e2f09e07 | 692 | |
uci1 | 8:95a325df1f6b | 693 | SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 694 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 695 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 696 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 697 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 698 | const float evtrate, |
uci1 | 8:95a325df1f6b | 699 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 700 | const uint32_t timeout_clock) { |
uci1 | 8:95a325df1f6b | 701 | // TODO: check if connected? |
uci1 | 12:d472f9811262 | 702 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 703 | printf("########### Send Status\r\n"); |
uci1 | 12:d472f9811262 | 704 | #endif |
uci1 | 21:ce51bb0ba4a5 | 705 | uint8_t loseLSB=0, loseMSB=0; |
uci1 | 21:ce51bb0ba4a5 | 706 | uint16_t wvBase=0; |
uci1 | 21:ce51bb0ba4a5 | 707 | conf.GetPackParsFor(GetCommType(), loseLSB, loseMSB, wvBase); |
uci1 | 8:95a325df1f6b | 708 | const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf); |
uci1 | 8:95a325df1f6b | 709 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 710 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize); |
uci1 | 21:ce51bb0ba4a5 | 711 | SnStatusFrame::WriteTo(b, conf, evt, genBuf, seq, thmrate, evtrate, |
uci1 | 21:ce51bb0ba4a5 | 712 | loseLSB, loseMSB, wvBase); |
uci1 | 10:3c93db1cfb12 | 713 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 10:3c93db1cfb12 | 714 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 10:3c93db1cfb12 | 715 | pow.WriteTo(b); |
uci1 | 40:1324da35afd4 | 716 | int32_t msiz = b-genBuf; |
uci1 | 18:55f1581f2ee4 | 717 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 718 | printf("calling SendAll (status)\r\n"); |
uci1 | 18:55f1581f2ee4 | 719 | #endif |
uci1 | 40:1324da35afd4 | 720 | int32_t mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 721 | #ifdef DEBUG |
uci1 | 41:d6f5e2f09e07 | 722 | printf("SendAll returned %d\r\n",mlen); |
uci1 | 41:d6f5e2f09e07 | 723 | #endif |
uci1 | 40:1324da35afd4 | 724 | mlen += fComm->FinishSending(timeout_clock); |
uci1 | 8:95a325df1f6b | 725 | #ifdef DEBUG |
uci1 | 41:d6f5e2f09e07 | 726 | printf("mlen=%d after FinishSending. msiz=%d\r\n",mlen,msiz); |
uci1 | 8:95a325df1f6b | 727 | printf("status frame:\r\n"); |
uci1 | 15:f2569d8e4176 | 728 | for (uint32_t i=0; i<msiz; i++) { |
uci1 | 8:95a325df1f6b | 729 | printf("%02X ",genBuf[i]); |
uci1 | 8:95a325df1f6b | 730 | } |
uci1 | 8:95a325df1f6b | 731 | printf("\r\n"); |
uci1 | 8:95a325df1f6b | 732 | #endif |
uci1 | 8:95a325df1f6b | 733 | if (mlen==msiz) { |
uci1 | 12:d472f9811262 | 734 | #ifdef DEBUG |
uci1 | 10:3c93db1cfb12 | 735 | printf("status+power sent\r\n"); |
uci1 | 12:d472f9811262 | 736 | #endif |
uci1 | 21:ce51bb0ba4a5 | 737 | // event compression parameters must be the same |
uci1 | 21:ce51bb0ba4a5 | 738 | // as those sent in the status update |
uci1 | 8:95a325df1f6b | 739 | b = genBuf; |
uci1 | 10:3c93db1cfb12 | 740 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, |
uci1 | 21:ce51bb0ba4a5 | 741 | evt.SizeOf(SnEventFrame::kIOVers, loseLSB, loseMSB)); |
uci1 | 21:ce51bb0ba4a5 | 742 | b = evt.WriteTo(b, loseLSB, loseMSB, wvBase); |
uci1 | 21:ce51bb0ba4a5 | 743 | msiz = b-genBuf; |
uci1 | 16:744ce85aede2 | 744 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 745 | printf("calling SendAll (event) %d:\r\n",msiz); |
uci1 | 21:ce51bb0ba4a5 | 746 | for (uint32_t i=0; i<msiz; i++) { |
uci1 | 21:ce51bb0ba4a5 | 747 | printf("%02X ",genBuf[i]); |
uci1 | 21:ce51bb0ba4a5 | 748 | } |
uci1 | 21:ce51bb0ba4a5 | 749 | printf("\r\n"); |
uci1 | 21:ce51bb0ba4a5 | 750 | #endif |
uci1 | 37:ff95e7070f26 | 751 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 752 | mlen += fComm->FinishSending(timeout_clock); |
uci1 | 21:ce51bb0ba4a5 | 753 | if (mlen==msiz) { |
uci1 | 21:ce51bb0ba4a5 | 754 | return SnCommWin::kOkMsgSent; |
uci1 | 21:ce51bb0ba4a5 | 755 | } |
uci1 | 8:95a325df1f6b | 756 | } |
uci1 | 8:95a325df1f6b | 757 | return SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 758 | } |
uci1 | 8:95a325df1f6b | 759 | |
uci1 | 27:efc4d654b139 | 760 | |
uci1 | 23:ccf39298f205 | 761 | SnCommWin::ECommWinResult SnCommWin::SendString(const char* str, |
uci1 | 23:ccf39298f205 | 762 | const uint32_t timeout) { |
uci1 | 23:ccf39298f205 | 763 | #ifdef DEBUG |
uci1 | 23:ccf39298f205 | 764 | printf("SnCommWin::SendString %s\r\n",str); |
uci1 | 23:ccf39298f205 | 765 | #endif |
uci1 | 37:ff95e7070f26 | 766 | const bool ok = fComm->SendString(str, timeout); |
uci1 | 37:ff95e7070f26 | 767 | return (ok) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 23:ccf39298f205 | 768 | } |
uci1 | 23:ccf39298f205 | 769 | |
uci1 | 40:1324da35afd4 | 770 | int32_t SnCommWin::SendFilename(const char* inf, |
uci1 | 40:1324da35afd4 | 771 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 772 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 773 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 774 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 775 | printf("SnCommWin::SendFilename %s\r\n",inf); |
uci1 | 12:d472f9811262 | 776 | #endif |
uci1 | 16:744ce85aede2 | 777 | |
uci1 | 12:d472f9811262 | 778 | // remove the directory |
uci1 | 12:d472f9811262 | 779 | const char* fn = strrchr(inf, '/'); |
uci1 | 12:d472f9811262 | 780 | if (fn==0) { |
uci1 | 12:d472f9811262 | 781 | // no directory |
uci1 | 12:d472f9811262 | 782 | fn = inf; |
uci1 | 12:d472f9811262 | 783 | } else if (fn!=inf) { |
uci1 | 12:d472f9811262 | 784 | ++fn; // move past the '/' if it was found |
uci1 | 12:d472f9811262 | 785 | } |
uci1 | 12:d472f9811262 | 786 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 787 | printf("send %s\r\n",fn); |
uci1 | 12:d472f9811262 | 788 | #endif |
uci1 | 8:95a325df1f6b | 789 | const size_t flen = strlen(fn); |
uci1 | 8:95a325df1f6b | 790 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 791 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen); |
uci1 | 12:d472f9811262 | 792 | b = SnBitUtils::WriteTo(b, fn, flen); |
uci1 | 40:1324da35afd4 | 793 | bytesToBeSent = b-genBuf; |
uci1 | 40:1324da35afd4 | 794 | const int32_t mlen = fComm->SendAll(genBuf, bytesToBeSent, timeout_clock); |
uci1 | 12:d472f9811262 | 795 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 796 | printf("time = %u, timeout = %u\r\n",time(0), timeout_clock); |
uci1 | 12:d472f9811262 | 797 | #endif |
uci1 | 40:1324da35afd4 | 798 | return mlen; |
uci1 | 8:95a325df1f6b | 799 | } |
uci1 | 8:95a325df1f6b | 800 | |
uci1 | 40:1324da35afd4 | 801 | int32_t SnCommWin::SendFileBlock(FILE* inf, |
uci1 | 40:1324da35afd4 | 802 | const uint8_t blockHeaderCode, |
uci1 | 40:1324da35afd4 | 803 | const uint32_t blockSize, |
uci1 | 40:1324da35afd4 | 804 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 805 | int32_t& bytesToSend, |
uci1 | 40:1324da35afd4 | 806 | const uint32_t timeout) { |
uci1 | 12:d472f9811262 | 807 | // sends the block from the file. does not check if the file has sufficient bytes |
uci1 | 12:d472f9811262 | 808 | // this should be done before calling this function |
uci1 | 12:d472f9811262 | 809 | // (where the number of bytes in the file can be cached) |
uci1 | 40:1324da35afd4 | 810 | // |
uci1 | 40:1324da35afd4 | 811 | // bytesToSend will be set to the total bytes that should be delivered eventually |
uci1 | 40:1324da35afd4 | 812 | // the number of bytes actually shipped out by SendAll is returned |
uci1 | 12:d472f9811262 | 813 | char* b = genBuf; |
uci1 | 12:d472f9811262 | 814 | SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize); |
uci1 | 12:d472f9811262 | 815 | SnBitUtils::ReadFrom(inf, b, blockSize); |
uci1 | 12:d472f9811262 | 816 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 817 | printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf()); |
uci1 | 12:d472f9811262 | 818 | #endif |
uci1 | 40:1324da35afd4 | 819 | bytesToSend = blockSize+SnHeaderFrame::SizeOf(); |
uci1 | 40:1324da35afd4 | 820 | return fComm->SendAll(genBuf, bytesToSend, timeout); |
uci1 | 12:d472f9811262 | 821 | } |
uci1 | 12:d472f9811262 | 822 | |
uci1 | 40:1324da35afd4 | 823 | int32_t SnCommWin::SendFileContents(FILE* inf, |
uci1 | 40:1324da35afd4 | 824 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 825 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 826 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 827 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 828 | uint32_t nevts, |
uci1 | 40:1324da35afd4 | 829 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 830 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 831 | // firstEvt==0 ==> start at beginning |
uci1 | 12:d472f9811262 | 832 | // nevts==0 ==> all events |
uci1 | 40:1324da35afd4 | 833 | // |
uci1 | 40:1324da35afd4 | 834 | // bytesToSend will be set to the total bytes that should be delivered eventually |
uci1 | 40:1324da35afd4 | 835 | // the number of bytes actually shipped out by SendAll is returned |
uci1 | 12:d472f9811262 | 836 | |
uci1 | 12:d472f9811262 | 837 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 838 | printf("SendFileContents (byte streaming)\r\n"); |
uci1 | 12:d472f9811262 | 839 | #endif |
uci1 | 12:d472f9811262 | 840 | |
uci1 | 12:d472f9811262 | 841 | // store position in file so we can go back to it afterwards |
uci1 | 12:d472f9811262 | 842 | // this is probably not necessary, because if this file is open |
uci1 | 12:d472f9811262 | 843 | // in write-only mode, we're screwed.. |
uci1 | 12:d472f9811262 | 844 | const int fpos = ftell(inf); |
uci1 | 12:d472f9811262 | 845 | if (fpos>0) { |
uci1 | 12:d472f9811262 | 846 | fseek(inf, 0, SEEK_SET); |
uci1 | 12:d472f9811262 | 847 | } |
uci1 | 12:d472f9811262 | 848 | // how many bytes? |
uci1 | 12:d472f9811262 | 849 | fseek(inf, 0, SEEK_END); // go to end |
uci1 | 12:d472f9811262 | 850 | const int fend = ftell(inf); |
uci1 | 12:d472f9811262 | 851 | fseek(inf, 0, SEEK_SET); // go to start |
uci1 | 12:d472f9811262 | 852 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 853 | printf("fend=%d\r\n",fend); |
uci1 | 12:d472f9811262 | 854 | printf("fpos=%d\r\n",fpos); |
uci1 | 12:d472f9811262 | 855 | #endif |
uci1 | 12:d472f9811262 | 856 | |
uci1 | 12:d472f9811262 | 857 | // variables used when sending data |
uci1 | 12:d472f9811262 | 858 | char* b = genBuf; |
uci1 | 40:1324da35afd4 | 859 | int32_t msiz(0), mlen(0), mtogo(0), tsent(0); |
uci1 | 21:ce51bb0ba4a5 | 860 | // count number of events / power readings sent |
uci1 | 40:1324da35afd4 | 861 | uint32_t evtsSent=0, powsSent=0, crc=0; |
uci1 | 12:d472f9811262 | 862 | |
uci1 | 12:d472f9811262 | 863 | // first is the file header, which has no SnHeaderFrame |
uci1 | 12:d472f9811262 | 864 | msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers); |
uci1 | 12:d472f9811262 | 865 | bool ok = (ftell(inf)+msiz)<=fend; |
uci1 | 12:d472f9811262 | 866 | if (ok) { |
uci1 | 12:d472f9811262 | 867 | mlen = SendFileBlock(inf, SnHeaderFrame::kFileHeadrCode, |
uci1 | 40:1324da35afd4 | 868 | msiz, genBuf, mtogo, timeout_clock); |
uci1 | 40:1324da35afd4 | 869 | //ok &= (msiz+SnHeaderFrame::SizeOf())==mlen; |
uci1 | 40:1324da35afd4 | 870 | bytesToBeSent += mtogo; |
uci1 | 40:1324da35afd4 | 871 | tsent += mlen; |
uci1 | 12:d472f9811262 | 872 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 873 | printf("sent file header. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 874 | #endif |
uci1 | 40:1324da35afd4 | 875 | // calc crc w/o the header block of the file header, as |
uci1 | 40:1324da35afd4 | 876 | // this does not get stored in the file |
uci1 | 40:1324da35afd4 | 877 | crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf+SnHeaderFrame::SizeOf(), |
uci1 | 40:1324da35afd4 | 878 | mtogo-SnHeaderFrame::SizeOf()); |
uci1 | 40:1324da35afd4 | 879 | |
uci1 | 12:d472f9811262 | 880 | // the header info for each block |
uci1 | 40:1324da35afd4 | 881 | uint8_t hcode(0); |
uci1 | 40:1324da35afd4 | 882 | uint32_t hlen(0); |
uci1 | 12:d472f9811262 | 883 | |
uci1 | 12:d472f9811262 | 884 | // now just loop through file and send each block |
uci1 | 18:55f1581f2ee4 | 885 | bool fok = true; |
uci1 | 18:55f1581f2ee4 | 886 | while ( fok |
uci1 | 12:d472f9811262 | 887 | && (feof(inf)==0) |
uci1 | 12:d472f9811262 | 888 | && (ferror(inf)==0) |
uci1 | 12:d472f9811262 | 889 | && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) { |
uci1 | 12:d472f9811262 | 890 | // read the header for the block |
uci1 | 12:d472f9811262 | 891 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 18:55f1581f2ee4 | 892 | fok &= (ftell(inf)+hlen)<=fend; |
uci1 | 12:d472f9811262 | 893 | #ifdef DEBUG |
uci1 | 18:55f1581f2ee4 | 894 | printf("new block: hc=%02x, hl=%u, ftell=%d, fend=%d, " |
uci1 | 18:55f1581f2ee4 | 895 | "ok=%d, fok=%d\r\n", |
uci1 | 18:55f1581f2ee4 | 896 | hcode, hlen, ftell(inf), fend, (int)ok, (int)fok); |
uci1 | 12:d472f9811262 | 897 | #endif |
uci1 | 40:1324da35afd4 | 898 | if (fok) { |
uci1 | 12:d472f9811262 | 899 | // send the block |
uci1 | 12:d472f9811262 | 900 | // TODO: repack events? |
uci1 | 40:1324da35afd4 | 901 | mlen = SendFileBlock(inf, hcode, hlen, genBuf, |
uci1 | 40:1324da35afd4 | 902 | mtogo, timeout_clock); |
uci1 | 40:1324da35afd4 | 903 | bytesToBeSent += mtogo; |
uci1 | 40:1324da35afd4 | 904 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 905 | crc = SnCRCUtils::GetUpdatedCRC32for(crc, genBuf, mtogo); |
uci1 | 40:1324da35afd4 | 906 | |
uci1 | 40:1324da35afd4 | 907 | if (hcode==SnHeaderFrame::kEventCode) { |
uci1 | 40:1324da35afd4 | 908 | ++evtsSent; |
uci1 | 40:1324da35afd4 | 909 | if (nevts>0) { |
uci1 | 40:1324da35afd4 | 910 | if (evtsSent>=nevts) { |
uci1 | 12:d472f9811262 | 911 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 912 | printf("send %u events. stop\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 913 | #endif |
uci1 | 40:1324da35afd4 | 914 | break; |
uci1 | 12:d472f9811262 | 915 | } |
uci1 | 12:d472f9811262 | 916 | } |
uci1 | 40:1324da35afd4 | 917 | } else if (hcode==SnHeaderFrame::kPowerCode) { |
uci1 | 40:1324da35afd4 | 918 | ++powsSent; |
uci1 | 40:1324da35afd4 | 919 | } |
uci1 | 40:1324da35afd4 | 920 | } // otherwise file size not sufficient for this block |
uci1 | 21:ce51bb0ba4a5 | 921 | } // loop over file contents |
uci1 | 21:ce51bb0ba4a5 | 922 | } else { |
uci1 | 21:ce51bb0ba4a5 | 923 | // otherwise file size not sufficient for file header |
uci1 | 21:ce51bb0ba4a5 | 924 | // so it's either 0 or corrupted. |
uci1 | 21:ce51bb0ba4a5 | 925 | // proceed as normal even tho no contents were sent. |
uci1 | 21:ce51bb0ba4a5 | 926 | // if we're deleting files, this one will be deleted. |
uci1 | 21:ce51bb0ba4a5 | 927 | ok = true; |
uci1 | 21:ce51bb0ba4a5 | 928 | } |
uci1 | 12:d472f9811262 | 929 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 930 | printf("loop done. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 931 | #endif |
uci1 | 40:1324da35afd4 | 932 | |
uci1 | 40:1324da35afd4 | 933 | // send the crc |
uci1 | 40:1324da35afd4 | 934 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 935 | printf("sending crc (%u)\r\n",crc); |
uci1 | 40:1324da35afd4 | 936 | #endif |
uci1 | 40:1324da35afd4 | 937 | b = genBuf; |
uci1 | 40:1324da35afd4 | 938 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kMbedFileChksCode, sizeof(uint32_t)); |
uci1 | 40:1324da35afd4 | 939 | b = SnBitUtils::WriteTo(b, crc); |
uci1 | 40:1324da35afd4 | 940 | msiz = b - genBuf; |
uci1 | 40:1324da35afd4 | 941 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 942 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 943 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 944 | |
uci1 | 40:1324da35afd4 | 945 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 946 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 40:1324da35afd4 | 947 | #endif |
uci1 | 40:1324da35afd4 | 948 | |
uci1 | 21:ce51bb0ba4a5 | 949 | |
uci1 | 21:ce51bb0ba4a5 | 950 | // send number of events sent |
uci1 | 12:d472f9811262 | 951 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 952 | printf("sending evtsSent (%u)\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 953 | #endif |
uci1 | 21:ce51bb0ba4a5 | 954 | b = genBuf; |
uci1 | 21:ce51bb0ba4a5 | 955 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); |
uci1 | 21:ce51bb0ba4a5 | 956 | b = SnBitUtils::WriteTo(b, evtsSent); |
uci1 | 21:ce51bb0ba4a5 | 957 | msiz = b - genBuf; |
uci1 | 37:ff95e7070f26 | 958 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 959 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 960 | tsent += mlen; |
uci1 | 40:1324da35afd4 | 961 | |
uci1 | 18:55f1581f2ee4 | 962 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 963 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 18:55f1581f2ee4 | 964 | #endif |
uci1 | 21:ce51bb0ba4a5 | 965 | |
uci1 | 21:ce51bb0ba4a5 | 966 | // send number of power readings sent |
uci1 | 12:d472f9811262 | 967 | #ifdef DEBUG |
uci1 | 21:ce51bb0ba4a5 | 968 | printf("sending powsSent (%u)\r\n",powsSent); |
uci1 | 12:d472f9811262 | 969 | #endif |
uci1 | 21:ce51bb0ba4a5 | 970 | b = genBuf; |
uci1 | 21:ce51bb0ba4a5 | 971 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); |
uci1 | 21:ce51bb0ba4a5 | 972 | b = SnBitUtils::WriteTo(b, powsSent); |
uci1 | 21:ce51bb0ba4a5 | 973 | msiz = b - genBuf; |
uci1 | 37:ff95e7070f26 | 974 | mlen = fComm->SendAll(genBuf, msiz, timeout_clock); |
uci1 | 40:1324da35afd4 | 975 | bytesToBeSent += msiz; |
uci1 | 40:1324da35afd4 | 976 | tsent += mlen; |
uci1 | 21:ce51bb0ba4a5 | 977 | |
uci1 | 18:55f1581f2ee4 | 978 | #ifdef DEBUG |
uci1 | 40:1324da35afd4 | 979 | printf("(msiz=%d, mlen=%d)\r\n", msiz, mlen); |
uci1 | 18:55f1581f2ee4 | 980 | #endif |
uci1 | 12:d472f9811262 | 981 | |
uci1 | 12:d472f9811262 | 982 | // put file position back |
uci1 | 12:d472f9811262 | 983 | fseek(inf, fpos, SEEK_SET); |
uci1 | 12:d472f9811262 | 984 | |
uci1 | 12:d472f9811262 | 985 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 986 | printf("end contents: ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 987 | #endif |
uci1 | 12:d472f9811262 | 988 | |
uci1 | 40:1324da35afd4 | 989 | return tsent; |
uci1 | 8:95a325df1f6b | 990 | |
uci1 | 40:1324da35afd4 | 991 | //return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 992 | } |