Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.cpp@12:d472f9811262, 2012-08-18 (annotated)
- Committer:
- uci1
- Date:
- Sat Aug 18 05:00:32 2012 +0000
- Revision:
- 12:d472f9811262
- Parent:
- 10:3c93db1cfb12
- Child:
- 13:7a1fb885a8e4
Fixed bug in throttling timers. Now evt stores dt of triggers but throttle works on dt of events. AfarTCP communication possibly done. Debug output put behind precompiler macros.
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 | 2:e67f7c158087 | 9 | |
uci1 | 12:d472f9811262 | 10 | bool SnCommWin::IsTimedOut(const uint32_t timeout_clock) const { |
uci1 | 12:d472f9811262 | 11 | if ( timeout_clock > 0 ) { |
uci1 | 12:d472f9811262 | 12 | if (time(0)>timeout_clock) { |
uci1 | 12:d472f9811262 | 13 | return true; |
uci1 | 12:d472f9811262 | 14 | } |
uci1 | 12:d472f9811262 | 15 | } |
uci1 | 12:d472f9811262 | 16 | return false; |
uci1 | 12:d472f9811262 | 17 | } |
uci1 | 12:d472f9811262 | 18 | |
uci1 | 2:e67f7c158087 | 19 | SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf, |
uci1 | 2:e67f7c158087 | 20 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 21 | SnPowerFrame& pow, |
uci1 | 2:e67f7c158087 | 22 | char* const genBuf, |
uci1 | 3:24c5f0f50bf1 | 23 | const uint32_t bsize, |
uci1 | 12:d472f9811262 | 24 | const uint32_t timeout, |
uci1 | 12:d472f9811262 | 25 | const uint32_t handshakeTimeout) { |
uci1 | 12:d472f9811262 | 26 | #ifdef DEBUG |
uci1 | 3:24c5f0f50bf1 | 27 | printf("SnCommWin::SendData\r\n"); |
uci1 | 12:d472f9811262 | 28 | #endif |
uci1 | 2:e67f7c158087 | 29 | ECommWinResult res = kUndefFail; |
uci1 | 2:e67f7c158087 | 30 | if (conf.IsSendingAllFiles()) { |
uci1 | 12:d472f9811262 | 31 | #ifdef DEBUG |
uci1 | 3:24c5f0f50bf1 | 32 | printf("sending all files\r\n"); |
uci1 | 12:d472f9811262 | 33 | #endif |
uci1 | 12:d472f9811262 | 34 | res = SnSDUtils::SendAllFiles(this, timeout, |
uci1 | 12:d472f9811262 | 35 | genBuf, bsize, conf, evt, pow, |
uci1 | 12:d472f9811262 | 36 | handshakeTimeout); |
uci1 | 2:e67f7c158087 | 37 | } else { |
uci1 | 2:e67f7c158087 | 38 | if (conf.GetCommSendData()==0) { |
uci1 | 12:d472f9811262 | 39 | #ifdef DEBUG |
uci1 | 3:24c5f0f50bf1 | 40 | printf("no data to send\r\n"); |
uci1 | 12:d472f9811262 | 41 | #endif |
uci1 | 2:e67f7c158087 | 42 | res = kOkNoMsg; |
uci1 | 2:e67f7c158087 | 43 | } else { |
uci1 | 2:e67f7c158087 | 44 | const uint32_t nev = (conf.GetCommSendData()>0) ? |
uci1 | 3:24c5f0f50bf1 | 45 | conf.GetCommSendData() // send N events |
uci1 | 3:24c5f0f50bf1 | 46 | : 0u; // send all events in last file |
uci1 | 12:d472f9811262 | 47 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 48 | printf("calling SendData. f=%x, nev=%u\r\n",SnSDUtils::GetCurFile(), nev); |
uci1 | 12:d472f9811262 | 49 | #endif |
uci1 | 2:e67f7c158087 | 50 | res = SendData(SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(), |
uci1 | 12:d472f9811262 | 51 | conf, evt, pow, genBuf, bsize, nev, timeout, handshakeTimeout); |
uci1 | 12:d472f9811262 | 52 | #ifdef DEBUG |
uci1 | 3:24c5f0f50bf1 | 53 | printf("after send data cur file, res=%d\r\n",(int)res); |
uci1 | 12:d472f9811262 | 54 | #endif |
uci1 | 2:e67f7c158087 | 55 | } |
uci1 | 2:e67f7c158087 | 56 | } |
uci1 | 2:e67f7c158087 | 57 | return res; |
uci1 | 2:e67f7c158087 | 58 | } |
uci1 | 2:e67f7c158087 | 59 | |
uci1 | 2:e67f7c158087 | 60 | SnCommWin::ECommWinResult SnCommWin::SendData(FILE* inf, const char* infn, |
uci1 | 2:e67f7c158087 | 61 | const SnConfigFrame& curConf, |
uci1 | 2:e67f7c158087 | 62 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 63 | SnPowerFrame& pow, |
uci1 | 2:e67f7c158087 | 64 | char* const genBuf, |
uci1 | 6:6f002d202f59 | 65 | const uint32_t bsize, |
uci1 | 2:e67f7c158087 | 66 | const uint32_t nevts, |
uci1 | 6:6f002d202f59 | 67 | const uint32_t timeout_clock, |
uci1 | 12:d472f9811262 | 68 | const uint32_t handshakeTimeout) { |
uci1 | 2:e67f7c158087 | 69 | // nevts==0 ==> send all events |
uci1 | 2:e67f7c158087 | 70 | |
uci1 | 12:d472f9811262 | 71 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 72 | printf("SnCommWin::SendData file (%p), fn=%s\r\n",inf,infn); |
uci1 | 12:d472f9811262 | 73 | #endif |
uci1 | 2:e67f7c158087 | 74 | ECommWinResult res = kUndefFail; |
uci1 | 12:d472f9811262 | 75 | bool didDel = false; |
uci1 | 2:e67f7c158087 | 76 | if (inf!=0) { |
uci1 | 12:d472f9811262 | 77 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 78 | printf("sending file name %s\r\n",infn); |
uci1 | 12:d472f9811262 | 79 | #endif |
uci1 | 6:6f002d202f59 | 80 | // send the file name |
uci1 | 6:6f002d202f59 | 81 | res = SendFilename(infn, genBuf, timeout_clock); |
uci1 | 3:24c5f0f50bf1 | 82 | if (res>kAllFails) { |
uci1 | 6:6f002d202f59 | 83 | if (genBuf!=0) { |
uci1 | 12:d472f9811262 | 84 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 85 | printf("calling send file contents\r\n"); |
uci1 | 12:d472f9811262 | 86 | #endif |
uci1 | 6:6f002d202f59 | 87 | // send the contents |
uci1 | 8:95a325df1f6b | 88 | res = SendFileContents(inf, curConf, evt, pow, genBuf, |
uci1 | 12:d472f9811262 | 89 | nevts, timeout_clock); |
uci1 | 12:d472f9811262 | 90 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 91 | printf("res=%d (fails=%d)\r\n",(int)res,(int)kAllFails); |
uci1 | 12:d472f9811262 | 92 | #endif |
uci1 | 12:d472f9811262 | 93 | if (res>kAllFails) { |
uci1 | 12:d472f9811262 | 94 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 95 | printf("calling wait handshake\r\n"); |
uci1 | 12:d472f9811262 | 96 | #endif |
uci1 | 6:6f002d202f59 | 97 | // wait for handshake |
uci1 | 6:6f002d202f59 | 98 | uint8_t hndshk=0; |
uci1 | 12:d472f9811262 | 99 | res = WaitHandshake(handshakeTimeout, genBuf, bsize, hndshk); |
uci1 | 6:6f002d202f59 | 100 | if ( (res==SnCommWin::kOkWithMsg) // got handshake |
uci1 | 6:6f002d202f59 | 101 | && (nevts==0) // sent whole file |
uci1 | 6:6f002d202f59 | 102 | && curConf.IsDeletingFiles() // want to delete |
uci1 | 12:d472f9811262 | 103 | && (inf!=SnSDUtils::GetCurFile()) // not current file |
uci1 | 6:6f002d202f59 | 104 | && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received |
uci1 | 6:6f002d202f59 | 105 | // delete it |
uci1 | 6:6f002d202f59 | 106 | SnSDUtilsWhisperer::DeleteFile(inf, infn); |
uci1 | 12:d472f9811262 | 107 | didDel=true; |
uci1 | 6:6f002d202f59 | 108 | } |
uci1 | 3:24c5f0f50bf1 | 109 | } |
uci1 | 2:e67f7c158087 | 110 | } |
uci1 | 2:e67f7c158087 | 111 | } |
uci1 | 12:d472f9811262 | 112 | } else { |
uci1 | 12:d472f9811262 | 113 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 114 | printf("inf=0!\r\n"); |
uci1 | 12:d472f9811262 | 115 | #endif |
uci1 | 12:d472f9811262 | 116 | } |
uci1 | 12:d472f9811262 | 117 | if ( (inf!=SnSDUtils::GetCurFile()) && (didDel==false) ) { |
uci1 | 12:d472f9811262 | 118 | SnSDUtils::CloseOutputFile(inf); |
uci1 | 2:e67f7c158087 | 119 | } |
uci1 | 2:e67f7c158087 | 120 | return res; |
uci1 | 2:e67f7c158087 | 121 | } |
uci1 | 8:95a325df1f6b | 122 | |
uci1 | 8:95a325df1f6b | 123 | SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const uint32_t timeout, |
uci1 | 8:95a325df1f6b | 124 | char* const buf, |
uci1 | 8:95a325df1f6b | 125 | const uint32_t bsize, |
uci1 | 8:95a325df1f6b | 126 | uint8_t& hndShkCode) { |
uci1 | 12:d472f9811262 | 127 | // ensure we don't wait forever |
uci1 | 12:d472f9811262 | 128 | const uint32_t to = (timeout>0) ? timeout : kListenTimeout; |
uci1 | 12:d472f9811262 | 129 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 130 | printf("WaitHandshake, to=%u\r\n",to); |
uci1 | 12:d472f9811262 | 131 | #endif |
uci1 | 8:95a325df1f6b | 132 | |
uci1 | 12:d472f9811262 | 133 | const int mlen = ReceiveAll(buf, SnHeaderFrame::SizeOf(), to); |
uci1 | 8:95a325df1f6b | 134 | if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) { |
uci1 | 8:95a325df1f6b | 135 | uint32_t msgLen=0; |
uci1 | 8:95a325df1f6b | 136 | const char* b = buf; |
uci1 | 8:95a325df1f6b | 137 | SnHeaderFrame::ReadFrom(b, hndShkCode, msgLen); |
uci1 | 12:d472f9811262 | 138 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 139 | printf("got handshake code=%02x, len=%u\r\n",hndShkCode,msgLen); |
uci1 | 12:d472f9811262 | 140 | #endif |
uci1 | 8:95a325df1f6b | 141 | if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) { |
uci1 | 8:95a325df1f6b | 142 | return SnCommWin::kOkWithMsg; |
uci1 | 8:95a325df1f6b | 143 | } else { |
uci1 | 8:95a325df1f6b | 144 | // TODO: somehow handle unexpected message? |
uci1 | 8:95a325df1f6b | 145 | return SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 146 | } |
uci1 | 8:95a325df1f6b | 147 | } |
uci1 | 8:95a325df1f6b | 148 | return SnCommWin::kOkNoMsg; |
uci1 | 8:95a325df1f6b | 149 | } |
uci1 | 8:95a325df1f6b | 150 | |
uci1 | 8:95a325df1f6b | 151 | SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 152 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 153 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 154 | const uint32_t bsize) { |
uci1 | 8:95a325df1f6b | 155 | // confBuf assumed to alread be of allocated size |
uci1 | 8:95a325df1f6b | 156 | |
uci1 | 12:d472f9811262 | 157 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 158 | printf("GetConfig, to=%u\r\n",timeOut); |
uci1 | 12:d472f9811262 | 159 | #endif |
uci1 | 8:95a325df1f6b | 160 | |
uci1 | 8:95a325df1f6b | 161 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 162 | if (bsize<SnHeaderFrame::kMaxSizeOf || bsize<SnConfigFrame::kMaxSizeOf) { |
uci1 | 8:95a325df1f6b | 163 | res = SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 164 | } else { |
uci1 | 8:95a325df1f6b | 165 | // get header |
uci1 | 8:95a325df1f6b | 166 | const int hlen = ReceiveAll(confBuf, SnHeaderFrame::SizeOf(), timeOut); |
uci1 | 8:95a325df1f6b | 167 | if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) { |
uci1 | 8:95a325df1f6b | 168 | uint8_t mcode=0; uint32_t mlen=0; |
uci1 | 8:95a325df1f6b | 169 | const char* b = confBuf; |
uci1 | 8:95a325df1f6b | 170 | SnHeaderFrame::ReadFrom(b, mcode, mlen); |
uci1 | 12:d472f9811262 | 171 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 172 | printf("mcode=%02x, mlen=%u\r\n", mcode, mlen); |
uci1 | 12:d472f9811262 | 173 | #endif |
uci1 | 12:d472f9811262 | 174 | if (mcode==SnHeaderFrame::kNoConfigCode) { |
uci1 | 12:d472f9811262 | 175 | // no config to get |
uci1 | 12:d472f9811262 | 176 | res = SnCommWin::kOkWithMsg; |
uci1 | 12:d472f9811262 | 177 | } else if (mcode!=SnHeaderFrame::kConfigCode) { |
uci1 | 8:95a325df1f6b | 178 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 179 | } else { |
uci1 | 8:95a325df1f6b | 180 | // get config |
uci1 | 8:95a325df1f6b | 181 | const int clen = ReceiveAll(confBuf, mlen, timeOut); |
uci1 | 8:95a325df1f6b | 182 | if (clen>0 && static_cast<uint32_t>(clen)==mlen) { |
uci1 | 8:95a325df1f6b | 183 | b = confBuf; |
uci1 | 8:95a325df1f6b | 184 | conf.ReadFrom(b); |
uci1 | 12:d472f9811262 | 185 | res = SnCommWin::kOkWthMsgNoConf; |
uci1 | 8:95a325df1f6b | 186 | } else { |
uci1 | 8:95a325df1f6b | 187 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 188 | } |
uci1 | 8:95a325df1f6b | 189 | } |
uci1 | 8:95a325df1f6b | 190 | } else { |
uci1 | 8:95a325df1f6b | 191 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 192 | } |
uci1 | 8:95a325df1f6b | 193 | } |
uci1 | 8:95a325df1f6b | 194 | return res; |
uci1 | 8:95a325df1f6b | 195 | } |
uci1 | 8:95a325df1f6b | 196 | |
uci1 | 8:95a325df1f6b | 197 | SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 198 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 199 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 200 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 201 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 202 | const float evtrate, |
uci1 | 8:95a325df1f6b | 203 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 204 | const uint32_t timeout_clock) { |
uci1 | 8:95a325df1f6b | 205 | // TODO: check if connected? |
uci1 | 12:d472f9811262 | 206 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 207 | printf("########### Send Status\r\n"); |
uci1 | 12:d472f9811262 | 208 | #endif |
uci1 | 8:95a325df1f6b | 209 | const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf); |
uci1 | 8:95a325df1f6b | 210 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 211 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize); |
uci1 | 10:3c93db1cfb12 | 212 | SnStatusFrame::WriteTo(b, GetCommType(), conf, evt, genBuf, seq, thmrate, evtrate); |
uci1 | 10:3c93db1cfb12 | 213 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 10:3c93db1cfb12 | 214 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 10:3c93db1cfb12 | 215 | pow.WriteTo(b); |
uci1 | 10:3c93db1cfb12 | 216 | int msiz = b-genBuf; |
uci1 | 10:3c93db1cfb12 | 217 | int mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 218 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 219 | printf("status frame:\r\n"); |
uci1 | 8:95a325df1f6b | 220 | for (uint32_t i=0; i<msize; i++) { |
uci1 | 8:95a325df1f6b | 221 | printf("%02X ",genBuf[i]); |
uci1 | 8:95a325df1f6b | 222 | } |
uci1 | 8:95a325df1f6b | 223 | printf("\r\n"); |
uci1 | 8:95a325df1f6b | 224 | #endif |
uci1 | 10:3c93db1cfb12 | 225 | msiz = b-genBuf; |
uci1 | 10:3c93db1cfb12 | 226 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 227 | if (mlen==msiz) { |
uci1 | 12:d472f9811262 | 228 | #ifdef DEBUG |
uci1 | 10:3c93db1cfb12 | 229 | printf("status+power sent\r\n"); |
uci1 | 12:d472f9811262 | 230 | #endif |
uci1 | 8:95a325df1f6b | 231 | b = genBuf; |
uci1 | 10:3c93db1cfb12 | 232 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, |
uci1 | 10:3c93db1cfb12 | 233 | evt.SizeOf(SnEventFrame::kIOVers, |
uci1 | 10:3c93db1cfb12 | 234 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB())); |
uci1 | 10:3c93db1cfb12 | 235 | evt.WriteTo(b, conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 10:3c93db1cfb12 | 236 | conf.GetWvBaseline()); |
uci1 | 10:3c93db1cfb12 | 237 | return SnCommWin::kOkMsgSent; |
uci1 | 8:95a325df1f6b | 238 | } |
uci1 | 8:95a325df1f6b | 239 | return SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 240 | } |
uci1 | 8:95a325df1f6b | 241 | |
uci1 | 12:d472f9811262 | 242 | SnCommWin::ECommWinResult SnCommWin::SendFilename(const char* inf, |
uci1 | 8:95a325df1f6b | 243 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 244 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 245 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 246 | printf("SnCommWin::SendFilename %s\r\n",inf); |
uci1 | 12:d472f9811262 | 247 | #endif |
uci1 | 12:d472f9811262 | 248 | // remove the directory |
uci1 | 12:d472f9811262 | 249 | const char* fn = strrchr(inf, '/'); |
uci1 | 12:d472f9811262 | 250 | if (fn==0) { |
uci1 | 12:d472f9811262 | 251 | // no directory |
uci1 | 12:d472f9811262 | 252 | fn = inf; |
uci1 | 12:d472f9811262 | 253 | } else if (fn!=inf) { |
uci1 | 12:d472f9811262 | 254 | ++fn; // move past the '/' if it was found |
uci1 | 12:d472f9811262 | 255 | } |
uci1 | 12:d472f9811262 | 256 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 257 | printf("send %s\r\n",fn); |
uci1 | 12:d472f9811262 | 258 | #endif |
uci1 | 8:95a325df1f6b | 259 | const size_t flen = strlen(fn); |
uci1 | 8:95a325df1f6b | 260 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 261 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen); |
uci1 | 12:d472f9811262 | 262 | b = SnBitUtils::WriteTo(b, fn, flen); |
uci1 | 12:d472f9811262 | 263 | const int msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 264 | const int mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 265 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 266 | printf("time = %u, timeout = %u\r\n",time(0), timeout_clock); |
uci1 | 12:d472f9811262 | 267 | #endif |
uci1 | 8:95a325df1f6b | 268 | return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 269 | } |
uci1 | 8:95a325df1f6b | 270 | |
uci1 | 12:d472f9811262 | 271 | int SnCommWin::SendFileBlock(FILE* inf, |
uci1 | 12:d472f9811262 | 272 | const uint8_t blockHeaderCode, |
uci1 | 12:d472f9811262 | 273 | const uint32_t blockSize, |
uci1 | 12:d472f9811262 | 274 | char* const genBuf, |
uci1 | 12:d472f9811262 | 275 | const uint32_t timeout) { |
uci1 | 12:d472f9811262 | 276 | // sends the block from the file. does not check if the file has sufficient bytes |
uci1 | 12:d472f9811262 | 277 | // this should be done before calling this function |
uci1 | 12:d472f9811262 | 278 | // (where the number of bytes in the file can be cached) |
uci1 | 12:d472f9811262 | 279 | char* b = genBuf; |
uci1 | 12:d472f9811262 | 280 | SnHeaderFrame::WriteTo(b, blockHeaderCode, blockSize); |
uci1 | 12:d472f9811262 | 281 | SnBitUtils::ReadFrom(inf, b, blockSize); |
uci1 | 12:d472f9811262 | 282 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 283 | printf("Sending block hc %02x, len=%u\r\n",blockHeaderCode,blockSize+SnHeaderFrame::SizeOf()); |
uci1 | 12:d472f9811262 | 284 | #endif |
uci1 | 12:d472f9811262 | 285 | return SendAll(genBuf, blockSize+SnHeaderFrame::SizeOf(), timeout); |
uci1 | 12:d472f9811262 | 286 | } |
uci1 | 12:d472f9811262 | 287 | |
uci1 | 12:d472f9811262 | 288 | SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf, |
uci1 | 12:d472f9811262 | 289 | const SnConfigFrame& curConf, |
uci1 | 12:d472f9811262 | 290 | SnEventFrame& evt, |
uci1 | 12:d472f9811262 | 291 | SnPowerFrame& pow, |
uci1 | 12:d472f9811262 | 292 | char* const genBuf, |
uci1 | 12:d472f9811262 | 293 | uint32_t nevts, |
uci1 | 12:d472f9811262 | 294 | const uint32_t timeout_clock) { |
uci1 | 12:d472f9811262 | 295 | // firstEvt==0 ==> start at beginning |
uci1 | 12:d472f9811262 | 296 | // nevts==0 ==> all events |
uci1 | 12:d472f9811262 | 297 | |
uci1 | 12:d472f9811262 | 298 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 299 | printf("SendFileContents (byte streaming)\r\n"); |
uci1 | 12:d472f9811262 | 300 | #endif |
uci1 | 12:d472f9811262 | 301 | |
uci1 | 12:d472f9811262 | 302 | // store position in file so we can go back to it afterwards |
uci1 | 12:d472f9811262 | 303 | // this is probably not necessary, because if this file is open |
uci1 | 12:d472f9811262 | 304 | // in write-only mode, we're screwed.. |
uci1 | 12:d472f9811262 | 305 | const int fpos = ftell(inf); |
uci1 | 12:d472f9811262 | 306 | if (fpos>0) { |
uci1 | 12:d472f9811262 | 307 | fseek(inf, 0, SEEK_SET); |
uci1 | 12:d472f9811262 | 308 | } |
uci1 | 12:d472f9811262 | 309 | // how many bytes? |
uci1 | 12:d472f9811262 | 310 | fseek(inf, 0, SEEK_END); // go to end |
uci1 | 12:d472f9811262 | 311 | const int fend = ftell(inf); |
uci1 | 12:d472f9811262 | 312 | fseek(inf, 0, SEEK_SET); // go to start |
uci1 | 12:d472f9811262 | 313 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 314 | printf("fend=%d\r\n",fend); |
uci1 | 12:d472f9811262 | 315 | printf("fpos=%d\r\n",fpos); |
uci1 | 12:d472f9811262 | 316 | #endif |
uci1 | 12:d472f9811262 | 317 | |
uci1 | 12:d472f9811262 | 318 | // variables used when sending data |
uci1 | 12:d472f9811262 | 319 | char* b = genBuf; |
uci1 | 12:d472f9811262 | 320 | int msiz, mlen; |
uci1 | 12:d472f9811262 | 321 | |
uci1 | 12:d472f9811262 | 322 | // first is the file header, which has no SnHeaderFrame |
uci1 | 12:d472f9811262 | 323 | msiz = SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers); |
uci1 | 12:d472f9811262 | 324 | bool ok = (ftell(inf)+msiz)<=fend; |
uci1 | 12:d472f9811262 | 325 | if (ok) { |
uci1 | 12:d472f9811262 | 326 | mlen = SendFileBlock(inf, SnHeaderFrame::kFileHeadrCode, |
uci1 | 12:d472f9811262 | 327 | msiz, genBuf, timeout_clock); |
uci1 | 12:d472f9811262 | 328 | ok &= (msiz+SnHeaderFrame::SizeOf())==mlen; |
uci1 | 12:d472f9811262 | 329 | |
uci1 | 12:d472f9811262 | 330 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 331 | printf("sent file header. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 332 | #endif |
uci1 | 12:d472f9811262 | 333 | |
uci1 | 12:d472f9811262 | 334 | // the header info for each block |
uci1 | 12:d472f9811262 | 335 | uint8_t hcode; |
uci1 | 12:d472f9811262 | 336 | uint32_t hlen; |
uci1 | 12:d472f9811262 | 337 | |
uci1 | 12:d472f9811262 | 338 | // count number of events / power readings sent |
uci1 | 12:d472f9811262 | 339 | uint32_t evtsSent=0, powsSent=0; |
uci1 | 12:d472f9811262 | 340 | |
uci1 | 12:d472f9811262 | 341 | // now just loop through file and send each block |
uci1 | 12:d472f9811262 | 342 | while ( ok |
uci1 | 12:d472f9811262 | 343 | && (feof(inf)==0) |
uci1 | 12:d472f9811262 | 344 | && (ferror(inf)==0) |
uci1 | 12:d472f9811262 | 345 | && ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) ) { |
uci1 | 12:d472f9811262 | 346 | // read the header for the block |
uci1 | 12:d472f9811262 | 347 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 12:d472f9811262 | 348 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 349 | printf("new block: hc=%02x, hl=%u, ftell=%d, fend=%d\r\n", |
uci1 | 12:d472f9811262 | 350 | hcode, hlen, ftell(inf), fend); |
uci1 | 12:d472f9811262 | 351 | #endif |
uci1 | 12:d472f9811262 | 352 | ok &= (ftell(inf)+hlen)<=fend; |
uci1 | 12:d472f9811262 | 353 | if (ok) { |
uci1 | 12:d472f9811262 | 354 | // send the block |
uci1 | 12:d472f9811262 | 355 | // TODO: repack events? |
uci1 | 12:d472f9811262 | 356 | msiz = hlen + SnHeaderFrame::SizeOf(); |
uci1 | 12:d472f9811262 | 357 | mlen = SendFileBlock(inf, hcode, hlen, genBuf, timeout_clock); |
uci1 | 12:d472f9811262 | 358 | ok &= msiz==mlen; |
uci1 | 12:d472f9811262 | 359 | if (ok) { |
uci1 | 12:d472f9811262 | 360 | if (hcode==SnHeaderFrame::kEventCode) { |
uci1 | 12:d472f9811262 | 361 | ++evtsSent; |
uci1 | 12:d472f9811262 | 362 | if (nevts>0) { |
uci1 | 12:d472f9811262 | 363 | if (evtsSent>=nevts) { |
uci1 | 12:d472f9811262 | 364 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 365 | printf("send %u events. stop\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 366 | #endif |
uci1 | 12:d472f9811262 | 367 | break; |
uci1 | 12:d472f9811262 | 368 | } |
uci1 | 12:d472f9811262 | 369 | } |
uci1 | 12:d472f9811262 | 370 | } else if (hcode==SnHeaderFrame::kPowerCode) { |
uci1 | 12:d472f9811262 | 371 | ++powsSent; |
uci1 | 12:d472f9811262 | 372 | } |
uci1 | 12:d472f9811262 | 373 | } else { |
uci1 | 12:d472f9811262 | 374 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 375 | printf("not ok when sending hcode=%02x\r\n",hcode); |
uci1 | 12:d472f9811262 | 376 | #endif |
uci1 | 12:d472f9811262 | 377 | } |
uci1 | 12:d472f9811262 | 378 | } |
uci1 | 12:d472f9811262 | 379 | } |
uci1 | 12:d472f9811262 | 380 | |
uci1 | 12:d472f9811262 | 381 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 382 | printf("loop done. ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 383 | #endif |
uci1 | 12:d472f9811262 | 384 | |
uci1 | 12:d472f9811262 | 385 | // send number of events sent |
uci1 | 12:d472f9811262 | 386 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 387 | printf("sending evtsSent (%u)\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 388 | #endif |
uci1 | 12:d472f9811262 | 389 | b = genBuf; |
uci1 | 12:d472f9811262 | 390 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); |
uci1 | 12:d472f9811262 | 391 | b = SnBitUtils::WriteTo(b, evtsSent); |
uci1 | 12:d472f9811262 | 392 | msiz = b - genBuf; |
uci1 | 12:d472f9811262 | 393 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 394 | ok &= msiz==mlen; |
uci1 | 12:d472f9811262 | 395 | |
uci1 | 12:d472f9811262 | 396 | // send number of power readings sent |
uci1 | 12:d472f9811262 | 397 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 398 | printf("sending powsSent (%u)\r\n",powsSent); |
uci1 | 12:d472f9811262 | 399 | #endif |
uci1 | 12:d472f9811262 | 400 | b = genBuf; |
uci1 | 12:d472f9811262 | 401 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); |
uci1 | 12:d472f9811262 | 402 | b = SnBitUtils::WriteTo(b, powsSent); |
uci1 | 12:d472f9811262 | 403 | msiz = b - genBuf; |
uci1 | 12:d472f9811262 | 404 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 405 | ok &= msiz==mlen; |
uci1 | 12:d472f9811262 | 406 | |
uci1 | 12:d472f9811262 | 407 | } |
uci1 | 12:d472f9811262 | 408 | |
uci1 | 12:d472f9811262 | 409 | |
uci1 | 12:d472f9811262 | 410 | // put file position back |
uci1 | 12:d472f9811262 | 411 | fseek(inf, fpos, SEEK_SET); |
uci1 | 12:d472f9811262 | 412 | |
uci1 | 12:d472f9811262 | 413 | #ifdef DEBUG |
uci1 | 12:d472f9811262 | 414 | printf("end contents: ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 415 | #endif |
uci1 | 12:d472f9811262 | 416 | |
uci1 | 12:d472f9811262 | 417 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 12:d472f9811262 | 418 | } |
uci1 | 12:d472f9811262 | 419 | |
uci1 | 12:d472f9811262 | 420 | /* |
uci1 | 8:95a325df1f6b | 421 | SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf, |
uci1 | 8:95a325df1f6b | 422 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 423 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 424 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 425 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 426 | uint32_t nevts, |
uci1 | 8:95a325df1f6b | 427 | const uint32_t timeout_clock, |
uci1 | 8:95a325df1f6b | 428 | const uint32_t firstEvt) { |
uci1 | 12:d472f9811262 | 429 | printf("comm win send file contents\r\n"); |
uci1 | 8:95a325df1f6b | 430 | // firstEvt==0 ==> start at beginning |
uci1 | 8:95a325df1f6b | 431 | // nevts==0 ==> all events |
uci1 | 8:95a325df1f6b | 432 | |
uci1 | 8:95a325df1f6b | 433 | const int fpos = ftell(inf); |
uci1 | 8:95a325df1f6b | 434 | if (fpos>0) { |
uci1 | 8:95a325df1f6b | 435 | fseek(inf, 0, SEEK_SET); |
uci1 | 8:95a325df1f6b | 436 | } |
uci1 | 12:d472f9811262 | 437 | // how many bytes? |
uci1 | 12:d472f9811262 | 438 | fseek(inf, 0, SEEK_END); // go to end |
uci1 | 12:d472f9811262 | 439 | const int fend = ftell(inf); |
uci1 | 12:d472f9811262 | 440 | fseek(inf, 0, SEEK_SET); // go to start |
uci1 | 12:d472f9811262 | 441 | printf("fend=%d\r\n",fend); |
uci1 | 8:95a325df1f6b | 442 | printf("fpos=%d\r\n",fpos); |
uci1 | 8:95a325df1f6b | 443 | // get file header |
uci1 | 8:95a325df1f6b | 444 | uint64_t macadr; |
uci1 | 8:95a325df1f6b | 445 | uint32_t run; |
uci1 | 8:95a325df1f6b | 446 | uint16_t seq; |
uci1 | 12:d472f9811262 | 447 | bool ok = (ftell(inf)+SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers))<=fend; |
uci1 | 12:d472f9811262 | 448 | if (ok) { |
uci1 | 12:d472f9811262 | 449 | SnSDUtils::ReadFileHeader(inf, macadr, run, seq, &pow); |
uci1 | 12:d472f9811262 | 450 | printf("read file header: run %u, seq %hu\r\n",run,seq); |
uci1 | 12:d472f9811262 | 451 | } |
uci1 | 12:d472f9811262 | 452 | |
uci1 | 12:d472f9811262 | 453 | printf(">>> file hdr read ftell(inf)=%d\r\n",ftell(inf)); |
uci1 | 8:95a325df1f6b | 454 | |
uci1 | 8:95a325df1f6b | 455 | // check block |
uci1 | 8:95a325df1f6b | 456 | uint8_t hcode; |
uci1 | 8:95a325df1f6b | 457 | uint32_t hlen; |
uci1 | 12:d472f9811262 | 458 | ok &= (ftell(inf)+SnHeaderFrame::SizeOf())<=fend; |
uci1 | 12:d472f9811262 | 459 | if (ok) { |
uci1 | 12:d472f9811262 | 460 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 12:d472f9811262 | 461 | printf("read block hd %02x, len %u\r\n",hcode,hlen); |
uci1 | 12:d472f9811262 | 462 | if (hcode==SnHeaderFrame::kPowerCode) { |
uci1 | 12:d472f9811262 | 463 | ok &= (ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend; |
uci1 | 12:d472f9811262 | 464 | if (ok) { |
uci1 | 12:d472f9811262 | 465 | pow.ReadFrom(inf); |
uci1 | 12:d472f9811262 | 466 | printf("read power: v1=%g, v2=%g\r\n",pow.GetAveV1(), pow.GetAveV2()); |
uci1 | 12:d472f9811262 | 467 | // now read the next header |
uci1 | 12:d472f9811262 | 468 | ok &= (ftell(inf)+SnHeaderFrame::SizeOf())<=fend; |
uci1 | 12:d472f9811262 | 469 | if (ok) { |
uci1 | 12:d472f9811262 | 470 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 12:d472f9811262 | 471 | printf("read new header: hc %02x, hlen=%u\r\n",hcode,hlen); |
uci1 | 12:d472f9811262 | 472 | } |
uci1 | 12:d472f9811262 | 473 | } |
uci1 | 12:d472f9811262 | 474 | } |
uci1 | 8:95a325df1f6b | 475 | } |
uci1 | 8:95a325df1f6b | 476 | |
uci1 | 12:d472f9811262 | 477 | printf(">>> power tried ftell(inf)=%d\r\n",ftell(inf)); |
uci1 | 12:d472f9811262 | 478 | |
uci1 | 12:d472f9811262 | 479 | printf("hcode=%02x\r\n",hcode); |
uci1 | 12:d472f9811262 | 480 | printf("ok=%d\r\n",(int)ok); |
uci1 | 12:d472f9811262 | 481 | printf("feof=%d\r\n",feof(inf)); |
uci1 | 12:d472f9811262 | 482 | printf("ferror=%d\r\n",ferror(inf)); |
uci1 | 12:d472f9811262 | 483 | printf("%d+%u <= %d ? %d\r\n",ftell(inf),hlen,fend, |
uci1 | 12:d472f9811262 | 484 | (int)((ftell(inf)+hlen)<=fend)); |
uci1 | 12:d472f9811262 | 485 | |
uci1 | 8:95a325df1f6b | 486 | // TODO: check memory for temporary config/event frames? |
uci1 | 8:95a325df1f6b | 487 | // get config |
uci1 | 8:95a325df1f6b | 488 | while ((hcode!=SnHeaderFrame::kConfigCode) |
uci1 | 12:d472f9811262 | 489 | && ok |
uci1 | 8:95a325df1f6b | 490 | && (feof(inf)==0) |
uci1 | 12:d472f9811262 | 491 | && (ferror(inf)==0) |
uci1 | 12:d472f9811262 | 492 | && ((ftell(inf)+hlen) <= fend) ) { |
uci1 | 12:d472f9811262 | 493 | printf("skip: ftell=%d, hlen=%u\r\n",ftell(inf),hlen); |
uci1 | 8:95a325df1f6b | 494 | fseek(inf, hlen, SEEK_CUR); // skip this block |
uci1 | 12:d472f9811262 | 495 | ok = (ftell(inf)+SnHeaderFrame::SizeOf())<=fend; |
uci1 | 12:d472f9811262 | 496 | if (ok) { |
uci1 | 12:d472f9811262 | 497 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 12:d472f9811262 | 498 | printf("hcs >>> ftell(inf)=%d, hcode=%02x, hlen=%u\r\n",ftell(inf),hcode,hlen); |
uci1 | 12:d472f9811262 | 499 | printf("feof=%d, ferror=%d\r\n",feof(inf),ferror(inf)); |
uci1 | 12:d472f9811262 | 500 | } |
uci1 | 8:95a325df1f6b | 501 | } |
uci1 | 8:95a325df1f6b | 502 | |
uci1 | 12:d472f9811262 | 503 | printf("checking ftell\r\n"); |
uci1 | 12:d472f9811262 | 504 | bool gotConf = false; |
uci1 | 12:d472f9811262 | 505 | ok = (ftell(inf)+hlen) <= fend; |
uci1 | 12:d472f9811262 | 506 | printf("making temp config\r\n"); |
uci1 | 12:d472f9811262 | 507 | SnConfigFrame conf; // TODO: SOMETIMES STALLS HERE! |
uci1 | 12:d472f9811262 | 508 | printf("trying to read\r\n"); |
uci1 | 12:d472f9811262 | 509 | if (ok && (hcode==SnHeaderFrame::kConfigCode)) { |
uci1 | 12:d472f9811262 | 510 | printf("reading config from ftell=%d, hcode=%02x, hlen=%u, ok=%d\r\n", |
uci1 | 12:d472f9811262 | 511 | ftell(inf), hcode, hlen, (int)ok); |
uci1 | 12:d472f9811262 | 512 | conf.ReadFrom(inf); |
uci1 | 12:d472f9811262 | 513 | gotConf = (ftell(inf) <= fend) && (feof(inf)==0) && (ferror(inf)==0); |
uci1 | 12:d472f9811262 | 514 | } |
uci1 | 12:d472f9811262 | 515 | printf(">>> conf. ftell(inf)=%d, gotconf=%d\r\n",ftell(inf),(int)gotConf); |
uci1 | 8:95a325df1f6b | 516 | |
uci1 | 8:95a325df1f6b | 517 | char* b = genBuf; |
uci1 | 12:d472f9811262 | 518 | int msiz, mlen; |
uci1 | 12:d472f9811262 | 519 | if (ok && gotConf) { |
uci1 | 12:d472f9811262 | 520 | // get event size |
uci1 | 12:d472f9811262 | 521 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 12:d472f9811262 | 522 | uint16_t sWvBase=0; |
uci1 | 12:d472f9811262 | 523 | curConf.GetPackParsFor(GetCommType(), sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 12:d472f9811262 | 524 | |
uci1 | 12:d472f9811262 | 525 | // size of event in file |
uci1 | 12:d472f9811262 | 526 | const uint32_t esize = SnEventFrame::SizeOf(SnEventFrame::kIOVers, |
uci1 | 12:d472f9811262 | 527 | conf.GetWvLoseLSB(), |
uci1 | 12:d472f9811262 | 528 | conf.GetWvLoseMSB()); |
uci1 | 12:d472f9811262 | 529 | |
uci1 | 12:d472f9811262 | 530 | printf("esize=%u\r\n",esize); |
uci1 | 12:d472f9811262 | 531 | |
uci1 | 12:d472f9811262 | 532 | // number of events / power readings we will send |
uci1 | 12:d472f9811262 | 533 | uint32_t evtsSent=0, powsSent=0; |
uci1 | 8:95a325df1f6b | 534 | b = genBuf; |
uci1 | 12:d472f9811262 | 535 | // send the file header |
uci1 | 12:d472f9811262 | 536 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileHeadrCode, |
uci1 | 12:d472f9811262 | 537 | SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers)); |
uci1 | 12:d472f9811262 | 538 | SnSDUtils::WriteFileHeader(b, macadr, run, seq); |
uci1 | 8:95a325df1f6b | 539 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 540 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 541 | if (mlen==msiz) { |
uci1 | 12:d472f9811262 | 542 | // send power header |
uci1 | 8:95a325df1f6b | 543 | b = genBuf; |
uci1 | 12:d472f9811262 | 544 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 12:d472f9811262 | 545 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 12:d472f9811262 | 546 | pow.WriteTo(b); |
uci1 | 8:95a325df1f6b | 547 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 548 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 549 | ++powsSent; |
uci1 | 8:95a325df1f6b | 550 | if (mlen==msiz) { |
uci1 | 12:d472f9811262 | 551 | // send the config |
uci1 | 12:d472f9811262 | 552 | b = genBuf; |
uci1 | 12:d472f9811262 | 553 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfigCode, |
uci1 | 12:d472f9811262 | 554 | conf.SizeOf(SnConfigFrame::kIOVers)); |
uci1 | 12:d472f9811262 | 555 | conf.WriteTo(b); |
uci1 | 12:d472f9811262 | 556 | msiz = b-genBuf; |
uci1 | 12:d472f9811262 | 557 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 558 | if (mlen==msiz) { |
uci1 | 12:d472f9811262 | 559 | ok = true; |
uci1 | 12:d472f9811262 | 560 | } |
uci1 | 8:95a325df1f6b | 561 | } |
uci1 | 8:95a325df1f6b | 562 | } |
uci1 | 12:d472f9811262 | 563 | |
uci1 | 12:d472f9811262 | 564 | printf("ok=%d, nevts=%u\r\n",(int)ok,nevts); |
uci1 | 8:95a325df1f6b | 565 | |
uci1 | 12:d472f9811262 | 566 | if (ok) { |
uci1 | 12:d472f9811262 | 567 | // size of event sent over afar |
uci1 | 12:d472f9811262 | 568 | const uint32_t ssize = SnEventFrame::SizeOf(SnEventFrame::kIOVers, |
uci1 | 12:d472f9811262 | 569 | sLoseLSB, sLoseMSB); |
uci1 | 12:d472f9811262 | 570 | |
uci1 | 12:d472f9811262 | 571 | // loop over blocks. send event & power frames |
uci1 | 12:d472f9811262 | 572 | // until EOF or nevets have been sent |
uci1 | 12:d472f9811262 | 573 | uint8_t hc; uint32_t hl; |
uci1 | 12:d472f9811262 | 574 | while (ok && (feof(inf)==0) |
uci1 | 12:d472f9811262 | 575 | && (ferror(inf)==0) |
uci1 | 12:d472f9811262 | 576 | && (ftell(inf)<fend) ) { // feof doesn't seem to work |
uci1 | 12:d472f9811262 | 577 | printf(">>> ftell(inf)=%d\r\n",ftell(inf)); |
uci1 | 12:d472f9811262 | 578 | if ((ftell(inf)+SnHeaderFrame::SizeOf())<=fend) { |
uci1 | 12:d472f9811262 | 579 | SnSDUtils::ReadBlockHeader(inf, hc, hl); |
uci1 | 12:d472f9811262 | 580 | if (hc==SnHeaderFrame::kEventCode) { |
uci1 | 12:d472f9811262 | 581 | if ((ftell(inf)+ssize)<=fend) { |
uci1 | 12:d472f9811262 | 582 | evt.ReadFrom(inf, genBuf, |
uci1 | 12:d472f9811262 | 583 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 12:d472f9811262 | 584 | conf.GetWvBaseline()); |
uci1 | 12:d472f9811262 | 585 | if (evt.GetEvtNum()>=firstEvt) { |
uci1 | 12:d472f9811262 | 586 | ++evtsSent; |
uci1 | 12:d472f9811262 | 587 | printf("sending evt %u\r\n",evtsSent); |
uci1 | 12:d472f9811262 | 588 | // must be after evt.Read, since that uses the buffer |
uci1 | 12:d472f9811262 | 589 | b = genBuf; |
uci1 | 12:d472f9811262 | 590 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize); |
uci1 | 12:d472f9811262 | 591 | evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase); // will repack if necessary |
uci1 | 12:d472f9811262 | 592 | msiz = b-genBuf; //SnHeaderFrame::SizeOf()+ssize; |
uci1 | 12:d472f9811262 | 593 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 594 | ok &= mlen==msiz; |
uci1 | 12:d472f9811262 | 595 | } |
uci1 | 12:d472f9811262 | 596 | } else { |
uci1 | 12:d472f9811262 | 597 | printf("event header, but not enough bytes in file\r\n"); |
uci1 | 12:d472f9811262 | 598 | ok = false; |
uci1 | 12:d472f9811262 | 599 | } |
uci1 | 12:d472f9811262 | 600 | } else if (hc==SnHeaderFrame::kPowerCode) { |
uci1 | 12:d472f9811262 | 601 | if ((ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend) { |
uci1 | 12:d472f9811262 | 602 | ++powsSent; |
uci1 | 12:d472f9811262 | 603 | pow.ReadFrom(inf); |
uci1 | 12:d472f9811262 | 604 | printf("sending power frame %u, v1=%g, v2=%g, t=%u\r\n", |
uci1 | 12:d472f9811262 | 605 | powsSent, pow.GetAveV1(), pow.GetAveV2(), pow.GetTime()); |
uci1 | 12:d472f9811262 | 606 | b = genBuf; |
uci1 | 12:d472f9811262 | 607 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 12:d472f9811262 | 608 | SnPowerFrame::SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 12:d472f9811262 | 609 | pow.WriteTo(b); |
uci1 | 12:d472f9811262 | 610 | msiz = b-genBuf; |
uci1 | 12:d472f9811262 | 611 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 612 | ok &= mlen==msiz; |
uci1 | 12:d472f9811262 | 613 | } else { |
uci1 | 12:d472f9811262 | 614 | printf("power header, but not enough bytes in file\r\n"); |
uci1 | 12:d472f9811262 | 615 | ok = false; |
uci1 | 12:d472f9811262 | 616 | } |
uci1 | 12:d472f9811262 | 617 | } else { |
uci1 | 12:d472f9811262 | 618 | printf("unhandled block. hc=%hhu, hl=%u\r\n", |
uci1 | 12:d472f9811262 | 619 | hc, hl); |
uci1 | 12:d472f9811262 | 620 | if ((ftell(inf)+hl)<=fend) { |
uci1 | 12:d472f9811262 | 621 | fseek(inf, hl, SEEK_CUR); // skip this block |
uci1 | 12:d472f9811262 | 622 | } else { |
uci1 | 12:d472f9811262 | 623 | printf("not enough bytes in file to skip block\r\n"); |
uci1 | 12:d472f9811262 | 624 | ok = false; |
uci1 | 12:d472f9811262 | 625 | } |
uci1 | 8:95a325df1f6b | 626 | } |
uci1 | 8:95a325df1f6b | 627 | } else { |
uci1 | 12:d472f9811262 | 628 | printf("not enough bytes in file to read header\r\n"); |
uci1 | 12:d472f9811262 | 629 | printf("feof=%d\r\n",feof(inf)); |
uci1 | 12:d472f9811262 | 630 | printf("ftell(inf)=%d, hdr size=%u, fend=%u\r\n", |
uci1 | 12:d472f9811262 | 631 | (ftell(inf)), SnHeaderFrame::SizeOf(), fend); |
uci1 | 8:95a325df1f6b | 632 | ok = false; |
uci1 | 8:95a325df1f6b | 633 | } |
uci1 | 8:95a325df1f6b | 634 | } |
uci1 | 12:d472f9811262 | 635 | // send number of events sent |
uci1 | 12:d472f9811262 | 636 | b = genBuf; |
uci1 | 12:d472f9811262 | 637 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); |
uci1 | 12:d472f9811262 | 638 | b = SnBitUtils::WriteTo(b, evtsSent); |
uci1 | 12:d472f9811262 | 639 | msiz = b - genBuf; |
uci1 | 12:d472f9811262 | 640 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 641 | ok &= msiz==mlen; |
uci1 | 12:d472f9811262 | 642 | // send number of power readings sent |
uci1 | 12:d472f9811262 | 643 | b = genBuf; |
uci1 | 12:d472f9811262 | 644 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); |
uci1 | 12:d472f9811262 | 645 | b = SnBitUtils::WriteTo(b, powsSent); |
uci1 | 12:d472f9811262 | 646 | msiz = b - genBuf; |
uci1 | 12:d472f9811262 | 647 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 12:d472f9811262 | 648 | ok &= msiz==mlen; |
uci1 | 8:95a325df1f6b | 649 | } |
uci1 | 12:d472f9811262 | 650 | } else { |
uci1 | 12:d472f9811262 | 651 | printf("failed to get config from file\r\n"); |
uci1 | 12:d472f9811262 | 652 | // send corrupted file handshake |
uci1 | 8:95a325df1f6b | 653 | b = genBuf; |
uci1 | 12:d472f9811262 | 654 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileReadFailCode, 0u); |
uci1 | 12:d472f9811262 | 655 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 656 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 657 | ok &= msiz==mlen; |
uci1 | 8:95a325df1f6b | 658 | } |
uci1 | 12:d472f9811262 | 659 | |
uci1 | 8:95a325df1f6b | 660 | // put file position back |
uci1 | 8:95a325df1f6b | 661 | fseek(inf, fpos, SEEK_SET); |
uci1 | 8:95a325df1f6b | 662 | |
uci1 | 8:95a325df1f6b | 663 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 664 | } |
uci1 | 12:d472f9811262 | 665 | */ |