Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.cpp@10:3c93db1cfb12, 2012-08-10 (annotated)
- Committer:
- uci1
- Date:
- Fri Aug 10 18:35:43 2012 +0000
- Revision:
- 10:3c93db1cfb12
- Parent:
- 8:95a325df1f6b
- Child:
- 12:d472f9811262
Ensure that lockRegisters (p20) is 0 before accessing the SD card, for example when closing the file. This fixes an issue seen when running in power count mode. Communications still under dev. Many debug outputs.
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 | 2:e67f7c158087 | 10 | SnCommWin::ECommWinResult SnCommWin::SendData(SnConfigFrame& conf, |
uci1 | 2:e67f7c158087 | 11 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 12 | SnPowerFrame& pow, |
uci1 | 2:e67f7c158087 | 13 | char* const genBuf, |
uci1 | 3:24c5f0f50bf1 | 14 | const uint32_t bsize, |
uci1 | 2:e67f7c158087 | 15 | const uint32_t timeout) { |
uci1 | 3:24c5f0f50bf1 | 16 | printf("SnCommWin::SendData\r\n"); |
uci1 | 2:e67f7c158087 | 17 | ECommWinResult res = kUndefFail; |
uci1 | 6:6f002d202f59 | 18 | const uint32_t to = (conf.IsObeyingTimeout()) ? timeout : kSecsPerYear; |
uci1 | 2:e67f7c158087 | 19 | if (conf.IsSendingAllFiles()) { |
uci1 | 3:24c5f0f50bf1 | 20 | printf("sending all files\r\n"); |
uci1 | 6:6f002d202f59 | 21 | res = SnSDUtils::SendAllFiles(this, conf.IsDeletingFiles(), to, |
uci1 | 8:95a325df1f6b | 22 | genBuf, bsize, conf, evt, pow); |
uci1 | 2:e67f7c158087 | 23 | } else { |
uci1 | 2:e67f7c158087 | 24 | if (conf.GetCommSendData()==0) { |
uci1 | 3:24c5f0f50bf1 | 25 | printf("no data to send\r\n"); |
uci1 | 2:e67f7c158087 | 26 | res = kOkNoMsg; |
uci1 | 2:e67f7c158087 | 27 | } else { |
uci1 | 2:e67f7c158087 | 28 | const uint32_t nev = (conf.GetCommSendData()>0) ? |
uci1 | 3:24c5f0f50bf1 | 29 | conf.GetCommSendData() // send N events |
uci1 | 3:24c5f0f50bf1 | 30 | : 0u; // send all events in last file |
uci1 | 2:e67f7c158087 | 31 | res = SendData(SnSDUtils::GetCurFile(), SnSDUtils::GetCurFileName(), |
uci1 | 8:95a325df1f6b | 32 | conf, evt, pow, genBuf, bsize, nev, to); |
uci1 | 3:24c5f0f50bf1 | 33 | printf("after send data cur file, res=%d\r\n",(int)res); |
uci1 | 2:e67f7c158087 | 34 | } |
uci1 | 2:e67f7c158087 | 35 | } |
uci1 | 2:e67f7c158087 | 36 | return res; |
uci1 | 2:e67f7c158087 | 37 | } |
uci1 | 2:e67f7c158087 | 38 | |
uci1 | 2:e67f7c158087 | 39 | SnCommWin::ECommWinResult SnCommWin::SendData(FILE* inf, const char* infn, |
uci1 | 2:e67f7c158087 | 40 | const SnConfigFrame& curConf, |
uci1 | 2:e67f7c158087 | 41 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 42 | SnPowerFrame& pow, |
uci1 | 2:e67f7c158087 | 43 | char* const genBuf, |
uci1 | 6:6f002d202f59 | 44 | const uint32_t bsize, |
uci1 | 2:e67f7c158087 | 45 | const uint32_t nevts, |
uci1 | 6:6f002d202f59 | 46 | const uint32_t timeout_clock, |
uci1 | 2:e67f7c158087 | 47 | const uint32_t firstEvt) { |
uci1 | 2:e67f7c158087 | 48 | // nevts==0 ==> send all events |
uci1 | 2:e67f7c158087 | 49 | |
uci1 | 3:24c5f0f50bf1 | 50 | printf("SnCommWin::SendData cur file\r\n"); |
uci1 | 2:e67f7c158087 | 51 | ECommWinResult res = kUndefFail; |
uci1 | 2:e67f7c158087 | 52 | if (inf!=0) { |
uci1 | 6:6f002d202f59 | 53 | // send the file name |
uci1 | 6:6f002d202f59 | 54 | res = SendFilename(infn, genBuf, timeout_clock); |
uci1 | 3:24c5f0f50bf1 | 55 | if (res>kAllFails) { |
uci1 | 6:6f002d202f59 | 56 | if (genBuf!=0) { |
uci1 | 6:6f002d202f59 | 57 | // send the contents |
uci1 | 8:95a325df1f6b | 58 | res = SendFileContents(inf, curConf, evt, pow, genBuf, |
uci1 | 6:6f002d202f59 | 59 | nevts, timeout_clock, firstEvt); |
uci1 | 6:6f002d202f59 | 60 | if (res>SnCommWin::kAllFails) { |
uci1 | 6:6f002d202f59 | 61 | // wait for handshake |
uci1 | 6:6f002d202f59 | 62 | uint8_t hndshk=0; |
uci1 | 6:6f002d202f59 | 63 | res = WaitHandshake(timeout_clock, genBuf, bsize, hndshk); |
uci1 | 6:6f002d202f59 | 64 | if ( (res==SnCommWin::kOkWithMsg) // got handshake |
uci1 | 6:6f002d202f59 | 65 | && (nevts==0) // sent whole file |
uci1 | 6:6f002d202f59 | 66 | && curConf.IsDeletingFiles() // want to delete |
uci1 | 6:6f002d202f59 | 67 | //&& (strcmp(infn, SnSDUtils::GetCurFileName()!=0) // not current file (TODO?) |
uci1 | 6:6f002d202f59 | 68 | && (hndshk==SnHeaderFrame::kHnShOkComplCode)) { // whole file received |
uci1 | 6:6f002d202f59 | 69 | // delete it |
uci1 | 6:6f002d202f59 | 70 | SnSDUtilsWhisperer::DeleteFile(inf, infn); |
uci1 | 6:6f002d202f59 | 71 | } |
uci1 | 3:24c5f0f50bf1 | 72 | } |
uci1 | 2:e67f7c158087 | 73 | } |
uci1 | 2:e67f7c158087 | 74 | } |
uci1 | 2:e67f7c158087 | 75 | } |
uci1 | 2:e67f7c158087 | 76 | return res; |
uci1 | 2:e67f7c158087 | 77 | } |
uci1 | 8:95a325df1f6b | 78 | |
uci1 | 8:95a325df1f6b | 79 | SnCommWin::ECommWinResult SnCommWin::WaitHandshake(const uint32_t timeout, |
uci1 | 8:95a325df1f6b | 80 | char* const buf, |
uci1 | 8:95a325df1f6b | 81 | const uint32_t bsize, |
uci1 | 8:95a325df1f6b | 82 | uint8_t& hndShkCode) { |
uci1 | 8:95a325df1f6b | 83 | printf("WaitHandshake, to=%u\r\n",timeout); |
uci1 | 8:95a325df1f6b | 84 | |
uci1 | 8:95a325df1f6b | 85 | const int mlen = ReceiveAll(buf, SnHeaderFrame::SizeOf(), timeout); |
uci1 | 8:95a325df1f6b | 86 | if (mlen>0 && static_cast<uint32_t>(mlen) == SnHeaderFrame::SizeOf()) { |
uci1 | 8:95a325df1f6b | 87 | uint32_t msgLen=0; |
uci1 | 8:95a325df1f6b | 88 | const char* b = buf; |
uci1 | 8:95a325df1f6b | 89 | SnHeaderFrame::ReadFrom(b, hndShkCode, msgLen); |
uci1 | 8:95a325df1f6b | 90 | if ((hndShkCode & SnHeaderFrame::kHndShkBits)!=0) { |
uci1 | 8:95a325df1f6b | 91 | return SnCommWin::kOkWithMsg; |
uci1 | 8:95a325df1f6b | 92 | } else { |
uci1 | 8:95a325df1f6b | 93 | // TODO: somehow handle unexpected message? |
uci1 | 8:95a325df1f6b | 94 | return SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 95 | } |
uci1 | 8:95a325df1f6b | 96 | } |
uci1 | 8:95a325df1f6b | 97 | return SnCommWin::kOkNoMsg; |
uci1 | 8:95a325df1f6b | 98 | } |
uci1 | 8:95a325df1f6b | 99 | |
uci1 | 8:95a325df1f6b | 100 | SnCommWin::ECommWinResult SnCommWin::GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 101 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 102 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 103 | const uint32_t bsize) { |
uci1 | 8:95a325df1f6b | 104 | // confBuf assumed to alread be of allocated size |
uci1 | 8:95a325df1f6b | 105 | |
uci1 | 8:95a325df1f6b | 106 | printf("GetConfig, to=%u\r\n",timeOut); |
uci1 | 8:95a325df1f6b | 107 | |
uci1 | 8:95a325df1f6b | 108 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 109 | if (bsize<SnHeaderFrame::kMaxSizeOf || bsize<SnConfigFrame::kMaxSizeOf) { |
uci1 | 8:95a325df1f6b | 110 | res = SnCommWin::kUndefFail; |
uci1 | 8:95a325df1f6b | 111 | } else { |
uci1 | 8:95a325df1f6b | 112 | // get header |
uci1 | 8:95a325df1f6b | 113 | const int hlen = ReceiveAll(confBuf, SnHeaderFrame::SizeOf(), timeOut); |
uci1 | 8:95a325df1f6b | 114 | if (hlen>0 && static_cast<uint32_t>(hlen)==SnHeaderFrame::SizeOf()) { |
uci1 | 8:95a325df1f6b | 115 | uint8_t mcode=0; uint32_t mlen=0; |
uci1 | 8:95a325df1f6b | 116 | const char* b = confBuf; |
uci1 | 8:95a325df1f6b | 117 | SnHeaderFrame::ReadFrom(b, mcode, mlen); |
uci1 | 8:95a325df1f6b | 118 | printf("mcode=%02x, mlen=%u\r\n", mcode, mlen); |
uci1 | 8:95a325df1f6b | 119 | if (mcode!=SnHeaderFrame::kConfigCode) { |
uci1 | 8:95a325df1f6b | 120 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 121 | } else { |
uci1 | 8:95a325df1f6b | 122 | // get config |
uci1 | 8:95a325df1f6b | 123 | const int clen = ReceiveAll(confBuf, mlen, timeOut); |
uci1 | 8:95a325df1f6b | 124 | if (clen>0 && static_cast<uint32_t>(clen)==mlen) { |
uci1 | 8:95a325df1f6b | 125 | b = confBuf; |
uci1 | 8:95a325df1f6b | 126 | conf.ReadFrom(b); |
uci1 | 8:95a325df1f6b | 127 | res = SnCommWin::kOkWithMsg; |
uci1 | 8:95a325df1f6b | 128 | } else { |
uci1 | 8:95a325df1f6b | 129 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 130 | } |
uci1 | 8:95a325df1f6b | 131 | } |
uci1 | 8:95a325df1f6b | 132 | } else { |
uci1 | 8:95a325df1f6b | 133 | res = SnCommWin::kUnexpectedRec; |
uci1 | 8:95a325df1f6b | 134 | } |
uci1 | 8:95a325df1f6b | 135 | } |
uci1 | 8:95a325df1f6b | 136 | return res; |
uci1 | 8:95a325df1f6b | 137 | } |
uci1 | 8:95a325df1f6b | 138 | |
uci1 | 8:95a325df1f6b | 139 | SnCommWin::ECommWinResult SnCommWin::SendStatus(const SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 140 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 141 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 142 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 143 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 144 | const float evtrate, |
uci1 | 8:95a325df1f6b | 145 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 146 | const uint32_t timeout_clock) { |
uci1 | 8:95a325df1f6b | 147 | // TODO: check if connected? |
uci1 | 8:95a325df1f6b | 148 | const uint32_t ssize = SnStatusFrame::SizeOf(SnStatusFrame::kIOVers, conf); |
uci1 | 8:95a325df1f6b | 149 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 150 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize); |
uci1 | 10:3c93db1cfb12 | 151 | SnStatusFrame::WriteTo(b, GetCommType(), conf, evt, genBuf, seq, thmrate, evtrate); |
uci1 | 10:3c93db1cfb12 | 152 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 10:3c93db1cfb12 | 153 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 10:3c93db1cfb12 | 154 | pow.WriteTo(b); |
uci1 | 10:3c93db1cfb12 | 155 | int msiz = b-genBuf; |
uci1 | 10:3c93db1cfb12 | 156 | int mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 157 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 158 | printf("status frame:\r\n"); |
uci1 | 8:95a325df1f6b | 159 | for (uint32_t i=0; i<msize; i++) { |
uci1 | 8:95a325df1f6b | 160 | printf("%02X ",genBuf[i]); |
uci1 | 8:95a325df1f6b | 161 | } |
uci1 | 8:95a325df1f6b | 162 | printf("\r\n"); |
uci1 | 8:95a325df1f6b | 163 | #endif |
uci1 | 10:3c93db1cfb12 | 164 | msiz = b-genBuf; |
uci1 | 10:3c93db1cfb12 | 165 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 166 | if (mlen==msiz) { |
uci1 | 10:3c93db1cfb12 | 167 | printf("status+power sent\r\n"); |
uci1 | 8:95a325df1f6b | 168 | b = genBuf; |
uci1 | 10:3c93db1cfb12 | 169 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, |
uci1 | 10:3c93db1cfb12 | 170 | evt.SizeOf(SnEventFrame::kIOVers, |
uci1 | 10:3c93db1cfb12 | 171 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB())); |
uci1 | 10:3c93db1cfb12 | 172 | evt.WriteTo(b, conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 10:3c93db1cfb12 | 173 | conf.GetWvBaseline()); |
uci1 | 10:3c93db1cfb12 | 174 | return SnCommWin::kOkMsgSent; |
uci1 | 8:95a325df1f6b | 175 | } |
uci1 | 8:95a325df1f6b | 176 | return SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 177 | } |
uci1 | 8:95a325df1f6b | 178 | |
uci1 | 8:95a325df1f6b | 179 | SnCommWin::ECommWinResult SnCommWin::SendFilename(const char* fn, |
uci1 | 8:95a325df1f6b | 180 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 181 | const uint32_t timeout_clock) { |
uci1 | 8:95a325df1f6b | 182 | printf("afar send filename %s\r\n",fn); |
uci1 | 8:95a325df1f6b | 183 | const size_t flen = strlen(fn); |
uci1 | 8:95a325df1f6b | 184 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 185 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen); |
uci1 | 8:95a325df1f6b | 186 | sprintf(b, "%s", fn); |
uci1 | 8:95a325df1f6b | 187 | const int msiz = SnHeaderFrame::SizeOf()+flen; |
uci1 | 8:95a325df1f6b | 188 | const int mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 189 | return (msiz==mlen) ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 190 | } |
uci1 | 8:95a325df1f6b | 191 | |
uci1 | 8:95a325df1f6b | 192 | SnCommWin::ECommWinResult SnCommWin::SendFileContents(FILE* inf, |
uci1 | 8:95a325df1f6b | 193 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 194 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 195 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 196 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 197 | uint32_t nevts, |
uci1 | 8:95a325df1f6b | 198 | const uint32_t timeout_clock, |
uci1 | 8:95a325df1f6b | 199 | const uint32_t firstEvt) { |
uci1 | 8:95a325df1f6b | 200 | printf("comm win send conf and events\r\n"); |
uci1 | 8:95a325df1f6b | 201 | // firstEvt==0 ==> start at beginning |
uci1 | 8:95a325df1f6b | 202 | // nevts==0 ==> all events |
uci1 | 8:95a325df1f6b | 203 | |
uci1 | 8:95a325df1f6b | 204 | const int fpos = ftell(inf); |
uci1 | 8:95a325df1f6b | 205 | if (fpos>0) { |
uci1 | 8:95a325df1f6b | 206 | fseek(inf, 0, SEEK_SET); |
uci1 | 8:95a325df1f6b | 207 | } |
uci1 | 8:95a325df1f6b | 208 | |
uci1 | 8:95a325df1f6b | 209 | printf("fpos=%d\r\n",fpos); |
uci1 | 8:95a325df1f6b | 210 | |
uci1 | 8:95a325df1f6b | 211 | // get file header |
uci1 | 8:95a325df1f6b | 212 | uint64_t macadr; |
uci1 | 8:95a325df1f6b | 213 | uint32_t run; |
uci1 | 8:95a325df1f6b | 214 | uint16_t seq; |
uci1 | 8:95a325df1f6b | 215 | SnSDUtils::ReadFileHeader(inf, macadr, run, seq, &pow); |
uci1 | 8:95a325df1f6b | 216 | |
uci1 | 8:95a325df1f6b | 217 | // check block |
uci1 | 8:95a325df1f6b | 218 | uint8_t hcode; |
uci1 | 8:95a325df1f6b | 219 | uint32_t hlen; |
uci1 | 8:95a325df1f6b | 220 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 8:95a325df1f6b | 221 | if (hcode==SnHeaderFrame::kPowerCode) { |
uci1 | 8:95a325df1f6b | 222 | pow.ReadFrom(inf); |
uci1 | 8:95a325df1f6b | 223 | } |
uci1 | 8:95a325df1f6b | 224 | |
uci1 | 8:95a325df1f6b | 225 | // TODO: check memory for temporary config/event frames? |
uci1 | 8:95a325df1f6b | 226 | // get config |
uci1 | 8:95a325df1f6b | 227 | while ((hcode!=SnHeaderFrame::kConfigCode) |
uci1 | 8:95a325df1f6b | 228 | && (feof(inf)==0) |
uci1 | 8:95a325df1f6b | 229 | && (ferror(inf)==0) ) { |
uci1 | 8:95a325df1f6b | 230 | fseek(inf, hlen, SEEK_CUR); // skip this block |
uci1 | 8:95a325df1f6b | 231 | SnSDUtils::ReadBlockHeader(inf, hcode, hlen); |
uci1 | 8:95a325df1f6b | 232 | } |
uci1 | 8:95a325df1f6b | 233 | SnConfigFrame conf; |
uci1 | 8:95a325df1f6b | 234 | conf.ReadFrom(inf); |
uci1 | 8:95a325df1f6b | 235 | |
uci1 | 8:95a325df1f6b | 236 | // get event size |
uci1 | 8:95a325df1f6b | 237 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 8:95a325df1f6b | 238 | uint16_t sWvBase=0; |
uci1 | 8:95a325df1f6b | 239 | curConf.GetPackParsFor(GetCommType(), sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 8:95a325df1f6b | 240 | |
uci1 | 8:95a325df1f6b | 241 | // size of event in file |
uci1 | 8:95a325df1f6b | 242 | const uint32_t esize = SnEventFrame::SizeOf(SnEventFrame::kIOVers, |
uci1 | 8:95a325df1f6b | 243 | conf.GetWvLoseLSB(), |
uci1 | 8:95a325df1f6b | 244 | conf.GetWvLoseMSB()); |
uci1 | 8:95a325df1f6b | 245 | |
uci1 | 8:95a325df1f6b | 246 | printf("esize=%u\r\n",esize); |
uci1 | 8:95a325df1f6b | 247 | |
uci1 | 8:95a325df1f6b | 248 | // how many more bytes? |
uci1 | 8:95a325df1f6b | 249 | const int fcur = ftell(inf); |
uci1 | 8:95a325df1f6b | 250 | fseek(inf, 0, SEEK_END); // go to end |
uci1 | 8:95a325df1f6b | 251 | const int fend = ftell(inf); |
uci1 | 8:95a325df1f6b | 252 | fseek(inf, fcur, SEEK_SET); // go back |
uci1 | 8:95a325df1f6b | 253 | |
uci1 | 8:95a325df1f6b | 254 | bool ok = false; |
uci1 | 8:95a325df1f6b | 255 | char* b = genBuf; |
uci1 | 8:95a325df1f6b | 256 | // send the file header |
uci1 | 8:95a325df1f6b | 257 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileHeadrCode, |
uci1 | 8:95a325df1f6b | 258 | SnSDUtils::SizeOfFileHeader(SnSDUtils::kIOvers)); |
uci1 | 8:95a325df1f6b | 259 | SnSDUtils::WriteFileHeader(b, macadr, run, seq); |
uci1 | 8:95a325df1f6b | 260 | int msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 261 | int mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 262 | if (mlen==msiz) { |
uci1 | 8:95a325df1f6b | 263 | // send power header |
uci1 | 8:95a325df1f6b | 264 | b = genBuf; |
uci1 | 8:95a325df1f6b | 265 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 8:95a325df1f6b | 266 | pow.SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 8:95a325df1f6b | 267 | pow.WriteTo(b); |
uci1 | 8:95a325df1f6b | 268 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 269 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 270 | if (mlen==msiz) { |
uci1 | 8:95a325df1f6b | 271 | // send the config |
uci1 | 8:95a325df1f6b | 272 | b = genBuf; |
uci1 | 8:95a325df1f6b | 273 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfigCode, |
uci1 | 8:95a325df1f6b | 274 | conf.SizeOf(SnConfigFrame::kIOVers)); |
uci1 | 8:95a325df1f6b | 275 | conf.WriteTo(b); |
uci1 | 8:95a325df1f6b | 276 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 277 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 278 | if (mlen==msiz) { |
uci1 | 8:95a325df1f6b | 279 | ok = true; |
uci1 | 8:95a325df1f6b | 280 | } |
uci1 | 8:95a325df1f6b | 281 | } |
uci1 | 8:95a325df1f6b | 282 | } |
uci1 | 8:95a325df1f6b | 283 | |
uci1 | 8:95a325df1f6b | 284 | printf("ok=%d, nevts=%u\r\n",(int)ok,nevts); |
uci1 | 8:95a325df1f6b | 285 | |
uci1 | 8:95a325df1f6b | 286 | if (ok) { |
uci1 | 8:95a325df1f6b | 287 | // size of event sent over afar |
uci1 | 8:95a325df1f6b | 288 | const uint32_t ssize = SnEventFrame::SizeOf(SnEventFrame::kIOVers, |
uci1 | 8:95a325df1f6b | 289 | sLoseLSB, sLoseMSB); |
uci1 | 8:95a325df1f6b | 290 | |
uci1 | 8:95a325df1f6b | 291 | // loop over blocks. send event & power frames |
uci1 | 8:95a325df1f6b | 292 | // until EOF or nevets have been sent |
uci1 | 8:95a325df1f6b | 293 | uint32_t evtsSent=0, powsSent=0; |
uci1 | 8:95a325df1f6b | 294 | uint8_t hc; uint32_t hl; |
uci1 | 8:95a325df1f6b | 295 | while (ok && (feof(inf)==0) |
uci1 | 8:95a325df1f6b | 296 | && (ferror(inf)==0) ) { |
uci1 | 8:95a325df1f6b | 297 | SnSDUtils::ReadBlockHeader(inf, hc, hl); |
uci1 | 8:95a325df1f6b | 298 | if (hc==SnHeaderFrame::kEventCode) { |
uci1 | 8:95a325df1f6b | 299 | if ((ftell(inf)+ssize)<=fend) { |
uci1 | 8:95a325df1f6b | 300 | evt.ReadFrom(inf, genBuf, |
uci1 | 8:95a325df1f6b | 301 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 8:95a325df1f6b | 302 | conf.GetWvBaseline()); |
uci1 | 8:95a325df1f6b | 303 | if (evt.GetEvtNum()>=firstEvt) { |
uci1 | 8:95a325df1f6b | 304 | ++evtsSent; |
uci1 | 8:95a325df1f6b | 305 | printf("sending evt %u\r\n",evtsSent); |
uci1 | 8:95a325df1f6b | 306 | // must be after evt.Read, since that uses the buffer |
uci1 | 8:95a325df1f6b | 307 | b = genBuf; |
uci1 | 8:95a325df1f6b | 308 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize); |
uci1 | 8:95a325df1f6b | 309 | evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase); // will repack if necessary |
uci1 | 8:95a325df1f6b | 310 | msiz = b-genBuf; //SnHeaderFrame::SizeOf()+ssize; |
uci1 | 8:95a325df1f6b | 311 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 312 | ok &= mlen==msiz; |
uci1 | 8:95a325df1f6b | 313 | } |
uci1 | 8:95a325df1f6b | 314 | } else { |
uci1 | 8:95a325df1f6b | 315 | printf("event header, but not enough bytes in file\r\n"); |
uci1 | 8:95a325df1f6b | 316 | ok = false; |
uci1 | 8:95a325df1f6b | 317 | } |
uci1 | 8:95a325df1f6b | 318 | } else if (hc==SnHeaderFrame::kPowerCode) { |
uci1 | 8:95a325df1f6b | 319 | if ((ftell(inf)+SnPowerFrame::SizeOf(SnPowerFrame::kIOvers))<=fend) { |
uci1 | 8:95a325df1f6b | 320 | ++powsSent; |
uci1 | 8:95a325df1f6b | 321 | pow.ReadFrom(inf); |
uci1 | 8:95a325df1f6b | 322 | printf("sending power frame %u, v1=%g, v2=%g, t=%u\r\n", |
uci1 | 8:95a325df1f6b | 323 | powsSent, pow.GetAveV1(), pow.GetAveV2(), pow.GetTime()); |
uci1 | 8:95a325df1f6b | 324 | b = genBuf; |
uci1 | 8:95a325df1f6b | 325 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kPowerCode, |
uci1 | 8:95a325df1f6b | 326 | SnPowerFrame::SizeOf(SnPowerFrame::kIOvers)); |
uci1 | 8:95a325df1f6b | 327 | pow.WriteTo(b); |
uci1 | 8:95a325df1f6b | 328 | msiz = b-genBuf; |
uci1 | 8:95a325df1f6b | 329 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 330 | ok &= mlen==msiz; |
uci1 | 8:95a325df1f6b | 331 | } else { |
uci1 | 8:95a325df1f6b | 332 | printf("power header, but not enough bytes in file\r\n"); |
uci1 | 8:95a325df1f6b | 333 | ok = false; |
uci1 | 8:95a325df1f6b | 334 | } |
uci1 | 8:95a325df1f6b | 335 | } else { |
uci1 | 8:95a325df1f6b | 336 | printf("unhandled block. hc=%hhu, hl=%u\r\n", |
uci1 | 8:95a325df1f6b | 337 | hc, hl); |
uci1 | 8:95a325df1f6b | 338 | if ((ftell(inf)+hl)<=fend) { |
uci1 | 8:95a325df1f6b | 339 | fseek(inf, hl, SEEK_CUR); // skip this block |
uci1 | 8:95a325df1f6b | 340 | } else { |
uci1 | 8:95a325df1f6b | 341 | printf("not enough bytes in file to skip block\r\n"); |
uci1 | 8:95a325df1f6b | 342 | ok = false; |
uci1 | 8:95a325df1f6b | 343 | } |
uci1 | 8:95a325df1f6b | 344 | } |
uci1 | 8:95a325df1f6b | 345 | } |
uci1 | 8:95a325df1f6b | 346 | // send number of events sent |
uci1 | 8:95a325df1f6b | 347 | b = genBuf; |
uci1 | 8:95a325df1f6b | 348 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNevtsCode, sizeof(uint32_t)); |
uci1 | 8:95a325df1f6b | 349 | b = SnBitUtils::WriteTo(b, evtsSent); |
uci1 | 8:95a325df1f6b | 350 | msiz = b - genBuf; |
uci1 | 8:95a325df1f6b | 351 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 352 | ok &= msiz==mlen; |
uci1 | 8:95a325df1f6b | 353 | // send number of power readings sent |
uci1 | 8:95a325df1f6b | 354 | b = genBuf; |
uci1 | 8:95a325df1f6b | 355 | SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFileNpwrsCode, sizeof(uint32_t)); |
uci1 | 8:95a325df1f6b | 356 | b = SnBitUtils::WriteTo(b, powsSent); |
uci1 | 8:95a325df1f6b | 357 | msiz = b -genBuf; |
uci1 | 8:95a325df1f6b | 358 | mlen = SendAll(genBuf, msiz, timeout_clock); |
uci1 | 8:95a325df1f6b | 359 | ok &= msiz==mlen; |
uci1 | 8:95a325df1f6b | 360 | } |
uci1 | 8:95a325df1f6b | 361 | |
uci1 | 8:95a325df1f6b | 362 | // put file position back |
uci1 | 8:95a325df1f6b | 363 | fseek(inf, fpos, SEEK_SET); |
uci1 | 8:95a325df1f6b | 364 | |
uci1 | 8:95a325df1f6b | 365 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 8:95a325df1f6b | 366 | } |