Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Jun 05 17:29:31 2019 +0000
Revision:
125:ce4045184366
Parent:
6:6f002d202f59
Added SnRateListner proto-class, publishing this version of the code in order to enable exporting of most recent features.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 6:6f002d202f59 1 /*
uci1 1:e392595b4b76 2 #include "SnCommAfar.h"
uci1 1:e392595b4b76 3
uci1 1:e392595b4b76 4 #include "Websocket.h"
uci1 1:e392595b4b76 5 #include "SnConfigFrame.h"
uci1 1:e392595b4b76 6 #include "SnEventFrame.h"
uci1 1:e392595b4b76 7 #include "SnStatusFrame.h"
uci1 2:e67f7c158087 8 #include "SnHeaderFrame.h"
uci1 2:e67f7c158087 9 #include "SnSDUtils.h"
uci1 1:e392595b4b76 10
uci1 1:e392595b4b76 11 Websocket& SnCommAfar::GetWS() {
uci1 1:e392595b4b76 12 // only one make one socket
uci1 1:e392595b4b76 13 //static Websocket* ws = new Websocket("ws://snowflake.ps.uci.edu:6767/ws");
uci1 1:e392595b4b76 14 // TODO: fix DNS
uci1 1:e392595b4b76 15 // TODO: remove hardcoded IP addresses from Websocket.cpp
uci1 2:e67f7c158087 16 static Websocket* ws = new Websocket("ws://128.195.204.151:6776/ws");
uci1 1:e392595b4b76 17 return *ws;
uci1 1:e392595b4b76 18 }
uci1 1:e392595b4b76 19
uci1 1:e392595b4b76 20 bool SnCommAfar::Connect(const uint32_t timeout) {
uci1 1:e392595b4b76 21 bool isConn = GetWS().connected();
uci1 1:e392595b4b76 22 printf("connect: ct=%d, timeout=%u\r\n",time(0),timeout);
uci1 1:e392595b4b76 23 while ( (isConn==false) && ( time(0) < timeout) ) {
uci1 1:e392595b4b76 24 wait_ms(250);
uci1 1:e392595b4b76 25 #ifdef DEBUG
uci1 1:e392595b4b76 26 printf("connecting..\r\n");
uci1 1:e392595b4b76 27 DigitalOut led3(LED3);
uci1 1:e392595b4b76 28 led3=1; wait(0.2);
uci1 1:e392595b4b76 29 #endif
uci1 1:e392595b4b76 30 isConn = GetWS().connect(timeout);
uci1 1:e392595b4b76 31 #ifdef DEBUG
uci1 1:e392595b4b76 32 printf("isConn=%d\r\n",(int)isConn);
uci1 1:e392595b4b76 33 led3=0; wait(0.2);
uci1 1:e392595b4b76 34 #endif
uci1 1:e392595b4b76 35 }
uci1 1:e392595b4b76 36 return isConn;
uci1 1:e392595b4b76 37 }
uci1 1:e392595b4b76 38
uci1 1:e392595b4b76 39 SnCommWin::ECommWinResult SnCommAfar::OpenWindow(const uint32_t timeout,
uci1 1:e392595b4b76 40 const bool sendStatus,
uci1 1:e392595b4b76 41 const SnConfigFrame& conf,
uci1 1:e392595b4b76 42 const SnEventFrame& evt,
uci1 2:e67f7c158087 43 char* const genBuf) {
uci1 1:e392595b4b76 44
uci1 1:e392595b4b76 45 const bool canCon = Connect(timeout);
uci1 1:e392595b4b76 46
uci1 1:e392595b4b76 47 SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
uci1 1:e392595b4b76 48 : SnCommWin::kCanNotConnect;
uci1 1:e392595b4b76 49
uci1 1:e392595b4b76 50 if (canCon && sendStatus) {
uci1 2:e67f7c158087 51 ret = SendStatus(conf, evt, genBuf);
uci1 1:e392595b4b76 52 }
uci1 1:e392595b4b76 53
uci1 1:e392595b4b76 54 return ret;
uci1 1:e392595b4b76 55 }
uci1 1:e392595b4b76 56
uci1 3:24c5f0f50bf1 57 SnCommWin::ECommWinResult SnCommAfar::WaitHandshake(const uint32_t timeout,
uci1 3:24c5f0f50bf1 58 char* const buf,
uci1 3:24c5f0f50bf1 59 const uint32_t bsize) {
uci1 3:24c5f0f50bf1 60 printf("WaitHandshake, to=%u\r\n",timeout);
uci1 3:24c5f0f50bf1 61
uci1 3:24c5f0f50bf1 62 uint32_t mlen=0; // this message length
uci1 3:24c5f0f50bf1 63 const bool rd = GetWS().read(buf, mlen, bsize, timeout, fB64buf, fbblen);
uci1 3:24c5f0f50bf1 64 if (rd) {
uci1 3:24c5f0f50bf1 65 uint32_t msgLen=0;
uci1 3:24c5f0f50bf1 66 uint8_t msgCode=0;
uci1 3:24c5f0f50bf1 67 const char* b = buf;
uci1 3:24c5f0f50bf1 68 SnHeaderFrame::ReadFrom(b, msgCode, msgLen);
uci1 3:24c5f0f50bf1 69 if (msgCode==SnHeaderFrame::kHandshakeCode) {
uci1 3:24c5f0f50bf1 70 return SnCommWin::kOkWithMsg;
uci1 3:24c5f0f50bf1 71 } else {
uci1 3:24c5f0f50bf1 72 // TODO: somehow handle unexpected message?
uci1 3:24c5f0f50bf1 73 return SnCommWin::kUnexpectedRec;
uci1 3:24c5f0f50bf1 74 }
uci1 3:24c5f0f50bf1 75 }
uci1 3:24c5f0f50bf1 76 return SnCommWin::kOkNoMsg;
uci1 3:24c5f0f50bf1 77 }
uci1 1:e392595b4b76 78
uci1 1:e392595b4b76 79 SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf,
uci1 1:e392595b4b76 80 const uint32_t timeOut,
uci1 3:24c5f0f50bf1 81 char* const confBuf,
uci1 3:24c5f0f50bf1 82 const uint32_t bsize) {
uci1 1:e392595b4b76 83 // confBuf assumed to alread be of allocated size
uci1 1:e392595b4b76 84
uci1 1:e392595b4b76 85 printf("GetConfig, to=%u\r\n",timeOut);
uci1 1:e392595b4b76 86
uci1 1:e392595b4b76 87 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 1:e392595b4b76 88
uci1 2:e67f7c158087 89 bool redConf = false;
uci1 2:e67f7c158087 90 bool readHeader = true;
uci1 2:e67f7c158087 91 uint8_t mcode=0x0;
uci1 2:e67f7c158087 92 uint32_t mlen=0; // length of message specified in header
uci1 2:e67f7c158087 93 uint32_t mread=0; // length of messages received so far
uci1 2:e67f7c158087 94 const char* b = confBuf;
uci1 2:e67f7c158087 95 while ( (redConf==false) && (time(0) < timeOut) ) {
uci1 2:e67f7c158087 96 uint32_t msglen=0; // this message length
uci1 3:24c5f0f50bf1 97 redConf = GetWS().read(confBuf+mread, msglen, bsize, timeOut, fB64buf, fbblen);
uci1 2:e67f7c158087 98 mread += msglen;
uci1 2:e67f7c158087 99 if (redConf) {
uci1 2:e67f7c158087 100 if (readHeader && mread>=SnHeaderFrame::SizeOf()) {
uci1 3:24c5f0f50bf1 101 printf("read head from %x\r\n",b);
uci1 2:e67f7c158087 102 SnHeaderFrame::ReadFrom(b, mcode, mlen);
uci1 2:e67f7c158087 103 // TODO: check that mcode is kConfigCode? What to do if not?
uci1 3:24c5f0f50bf1 104 printf("mcode=%02x, mlen=%u\r\n", mcode, mlen);
uci1 2:e67f7c158087 105 readHeader=false;
uci1 2:e67f7c158087 106 }
uci1 3:24c5f0f50bf1 107 printf("mread=%u, mlen=%u\r\n", mread, mlen);
uci1 3:24c5f0f50bf1 108 if (readHeader==false &&
uci1 3:24c5f0f50bf1 109 ((mread-SnHeaderFrame::SizeOf())>=mlen)) {
uci1 3:24c5f0f50bf1 110 printf("read config from %x\r\n",b);
uci1 2:e67f7c158087 111 conf.ReadFrom(b);
uci1 3:24c5f0f50bf1 112 printf("after read conf, b=%x (%d)\r\n",
uci1 3:24c5f0f50bf1 113 b, int(b-confBuf));
uci1 2:e67f7c158087 114 res = SnCommWin::kOkWithMsg;
uci1 2:e67f7c158087 115 } else {
uci1 2:e67f7c158087 116 redConf=false;
uci1 2:e67f7c158087 117 }
uci1 1:e392595b4b76 118 }
uci1 1:e392595b4b76 119 }
uci1 2:e67f7c158087 120 if (redConf==false) {
uci1 1:e392595b4b76 121 res = SnCommWin::kOkNoMsg;
uci1 1:e392595b4b76 122 }
uci1 1:e392595b4b76 123
uci1 1:e392595b4b76 124 return res;
uci1 1:e392595b4b76 125 }
uci1 1:e392595b4b76 126
uci1 1:e392595b4b76 127 SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf,
uci1 1:e392595b4b76 128 const SnEventFrame& evt,
uci1 2:e67f7c158087 129 char* const genBuf) {
uci1 1:e392595b4b76 130 // TODO: check if connected?
uci1 2:e67f7c158087 131 const uint32_t ssize = SnStatusFrame::SizeOf(conf);
uci1 3:24c5f0f50bf1 132 char* b = genBuf;
uci1 3:24c5f0f50bf1 133 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kStatusCode, ssize);
uci1 3:24c5f0f50bf1 134 SnStatusFrame::WriteTo(b, SnConfigFrame::kAfar, conf, evt, genBuf);
uci1 1:e392595b4b76 135 #ifdef DEBUG
uci1 1:e392595b4b76 136 printf("status frame:\r\n");
uci1 2:e67f7c158087 137 for (uint32_t i=0; i<msize; i++) {
uci1 2:e67f7c158087 138 printf("%02X ",genBuf[i]);
uci1 1:e392595b4b76 139 }
uci1 1:e392595b4b76 140 printf("\r\n");
uci1 1:e392595b4b76 141 #endif
uci1 2:e67f7c158087 142 const bool ok = GetWS().sendBinary(
uci1 3:24c5f0f50bf1 143 genBuf, b-genBuf, fB64buf);
uci1 1:e392595b4b76 144 printf("status sent\r\n");
uci1 2:e67f7c158087 145 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 1:e392595b4b76 146 }
uci1 1:e392595b4b76 147
uci1 3:24c5f0f50bf1 148 SnCommWin::ECommWinResult SnCommAfar::SendFilename(const char* fn, char* const genBuf) {
uci1 3:24c5f0f50bf1 149 printf("afar send filename %s\r\n",fn);
uci1 3:24c5f0f50bf1 150 const size_t flen = strlen(fn);
uci1 3:24c5f0f50bf1 151 char* b = genBuf;
uci1 3:24c5f0f50bf1 152 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kFilenameCode, flen);
uci1 3:24c5f0f50bf1 153 sprintf(b, "%s", fn);
uci1 3:24c5f0f50bf1 154 const bool ok = GetWS().sendBinary(
uci1 3:24c5f0f50bf1 155 genBuf, SnHeaderFrame::SizeOf()+flen, fB64buf);
uci1 3:24c5f0f50bf1 156 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 3:24c5f0f50bf1 157 }
uci1 3:24c5f0f50bf1 158
uci1 1:e392595b4b76 159 SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) {
uci1 3:24c5f0f50bf1 160 printf("afar send whole file\r\n");
uci1 1:e392595b4b76 161 fseek(inf, 0, SEEK_END);
uci1 2:e67f7c158087 162 const int32_t fsize = ftell(inf);
uci1 1:e392595b4b76 163 fseek(inf, 0, SEEK_SET);
uci1 2:e67f7c158087 164 if (fsize<0) {
uci1 2:e67f7c158087 165 return SnCommWin::kFailNoneSent;
uci1 2:e67f7c158087 166 } else {
uci1 2:e67f7c158087 167 // send file data with header
uci1 2:e67f7c158087 168 const bool ok = GetWS().sendBinary(
uci1 2:e67f7c158087 169 SnHeaderFrame::GetHdBuf(SnHeaderFrame::kFileCode, fsize),
uci1 2:e67f7c158087 170 SnHeaderFrame::SizeOf(),
uci1 3:24c5f0f50bf1 171 inf, fsize,
uci1 3:24c5f0f50bf1 172 fB64buf);
uci1 2:e67f7c158087 173 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 2:e67f7c158087 174 }
uci1 1:e392595b4b76 175 }
uci1 1:e392595b4b76 176
uci1 1:e392595b4b76 177 SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf,
uci1 1:e392595b4b76 178 const SnConfigFrame& curConf,
uci1 1:e392595b4b76 179 SnEventFrame& evt,
uci1 2:e67f7c158087 180 char* const genBuf,
uci1 1:e392595b4b76 181 const uint32_t nevts,
uci1 1:e392595b4b76 182 const uint32_t firstEvt) {
uci1 3:24c5f0f50bf1 183 printf("afar send conf and events\r\n");
uci1 1:e392595b4b76 184 // firstEvt==0 ==> start at beginning
uci1 1:e392595b4b76 185 // nevts==0 ==> NO events! (see SnCommWin::SendData
uci1 1:e392595b4b76 186 // for the fcn to send the full file)
uci1 3:24c5f0f50bf1 187
uci1 3:24c5f0f50bf1 188 const int fpos = ftell(inf);
uci1 3:24c5f0f50bf1 189 if (fpos>0) {
uci1 3:24c5f0f50bf1 190 fseek(inf, 0, SEEK_SET);
uci1 3:24c5f0f50bf1 191 }
uci1 3:24c5f0f50bf1 192
uci1 3:24c5f0f50bf1 193 printf("fpos=%d\r\n",fpos);
uci1 3:24c5f0f50bf1 194
uci1 1:e392595b4b76 195 // TODO: check memory for temporary config/event frames?
uci1 2:e67f7c158087 196 uint64_t macadr;
uci1 2:e67f7c158087 197 uint32_t run;
uci1 4:a91682e19d6b 198 uint16_t seq, v1, v2;
uci1 4:a91682e19d6b 199 SnSDUtils::ReadFileHeader(inf, macadr, run, seq, v1, v2);
uci1 1:e392595b4b76 200 SnConfigFrame conf;
uci1 1:e392595b4b76 201 conf.ReadFrom(inf);
uci1 2:e67f7c158087 202
uci1 2:e67f7c158087 203 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 2:e67f7c158087 204 uint16_t sWvBase=0;
uci1 2:e67f7c158087 205 curConf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase);
uci1 2:e67f7c158087 206 // do we have to unpack & repack events?
uci1 2:e67f7c158087 207 const bool repack = (sLoseLSB != conf.GetWvLoseLSB())
uci1 2:e67f7c158087 208 && (sLoseMSB != conf.GetWvLoseMSB())
uci1 2:e67f7c158087 209 && (sWvBase != conf.GetWvBaseline());
uci1 2:e67f7c158087 210
uci1 2:e67f7c158087 211 // size of event in file
uci1 2:e67f7c158087 212 const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
uci1 2:e67f7c158087 213 conf.GetWvLoseMSB());
uci1 3:24c5f0f50bf1 214
uci1 3:24c5f0f50bf1 215 printf("repack=%d, esize=%u\r\n",(int)repack,esize);
uci1 2:e67f7c158087 216
uci1 2:e67f7c158087 217 // send the header and conf
uci1 2:e67f7c158087 218 char* b = genBuf;
uci1 3:24c5f0f50bf1 219 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kConfAndEvtsCode, conf.SizeOf());
uci1 1:e392595b4b76 220 conf.WriteTo(b);
uci1 3:24c5f0f50bf1 221
uci1 2:e67f7c158087 222 bool ok = GetWS().sendBinary(
uci1 3:24c5f0f50bf1 223 genBuf, conf.SizeOf()+SnHeaderFrame::SizeOf(),
uci1 3:24c5f0f50bf1 224 fB64buf);
uci1 3:24c5f0f50bf1 225
uci1 3:24c5f0f50bf1 226 printf("ok=%d, nevts=%u\r\n",(int)ok,nevts);
uci1 2:e67f7c158087 227
uci1 2:e67f7c158087 228 // size of event sent over afar
uci1 2:e67f7c158087 229 const uint32_t ssize = (repack) ?
uci1 2:e67f7c158087 230 SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) :
uci1 2:e67f7c158087 231 esize;
uci1 2:e67f7c158087 232
uci1 3:24c5f0f50bf1 233 // move up to first event
uci1 3:24c5f0f50bf1 234 if (firstEvt>0) {
uci1 3:24c5f0f50bf1 235 printf("skip ahead %d\r\n",firstEvt*esize);
uci1 3:24c5f0f50bf1 236 fseek(inf, (firstEvt*esize), SEEK_CUR);
uci1 3:24c5f0f50bf1 237 }
uci1 3:24c5f0f50bf1 238
uci1 3:24c5f0f50bf1 239 // get file size
uci1 3:24c5f0f50bf1 240 const int fcur = ftell(inf);
uci1 3:24c5f0f50bf1 241 fseek(inf, 0, SEEK_END);
uci1 3:24c5f0f50bf1 242 const int fend = ftell(inf);
uci1 3:24c5f0f50bf1 243 fseek(inf, fcur, SEEK_SET);
uci1 3:24c5f0f50bf1 244
uci1 2:e67f7c158087 245 for (uint32_t i=0; (i<nevts) && ok; i++) {
uci1 3:24c5f0f50bf1 246 if (feof(inf)==0 && ferror(inf)==0 && ((ftell(inf)+ssize)<=fend)) {
uci1 3:24c5f0f50bf1 247 printf("sending evt %u\r\n",i);
uci1 3:24c5f0f50bf1 248 if (repack) {
uci1 3:24c5f0f50bf1 249 evt.ReadFrom(inf, genBuf,
uci1 3:24c5f0f50bf1 250 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
uci1 3:24c5f0f50bf1 251 conf.GetWvBaseline());
uci1 3:24c5f0f50bf1 252 // must be after evt.Read, since that uses the buffer
uci1 3:24c5f0f50bf1 253 b = genBuf;
uci1 3:24c5f0f50bf1 254 SnHeaderFrame::WriteTo(b, SnHeaderFrame::kEventCode, ssize);
uci1 3:24c5f0f50bf1 255 evt.WriteTo(b, sLoseLSB, sLoseMSB, sWvBase);
uci1 3:24c5f0f50bf1 256 ok &= GetWS().sendBinary(
uci1 3:24c5f0f50bf1 257 genBuf, ssize+SnHeaderFrame::SizeOf(),
uci1 3:24c5f0f50bf1 258 fB64buf);
uci1 3:24c5f0f50bf1 259 } else {
uci1 3:24c5f0f50bf1 260 ok &= GetWS().sendBinary(
uci1 3:24c5f0f50bf1 261 SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode,
uci1 3:24c5f0f50bf1 262 ssize),
uci1 3:24c5f0f50bf1 263 SnHeaderFrame::SizeOf(),
uci1 3:24c5f0f50bf1 264 inf, ssize,
uci1 3:24c5f0f50bf1 265 fB64buf);
uci1 3:24c5f0f50bf1 266 }
uci1 1:e392595b4b76 267 } else {
uci1 3:24c5f0f50bf1 268 printf("cannot send requested event\r\n");
uci1 3:24c5f0f50bf1 269 ok=false;
uci1 1:e392595b4b76 270 }
uci1 1:e392595b4b76 271 }
uci1 3:24c5f0f50bf1 272
uci1 3:24c5f0f50bf1 273 fseek(inf, fpos, SEEK_SET);
uci1 3:24c5f0f50bf1 274
uci1 1:e392595b4b76 275 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 1:e392595b4b76 276 }
uci1 6:6f002d202f59 277 */