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