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