Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Tue Jul 31 04:59:16 2012 +0000
Revision:
3:24c5f0f50bf1
Parent:
2:e67f7c158087
Child:
4:a91682e19d6b
Test bench version. Communications not completed. Debugging output present. But will read the local config file and save events that can be used for testing.

Who changed what in which revision?

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