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