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