Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Jul 20 19:04:02 2012 +0000
Revision:
1:e392595b4b76
Parent:
0:664899e0b988
Child:
2:e67f7c158087
many features checked and working. afar implemented. sending of data not yet tested. contains many debug prints

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 1:e392595b4b76 7
uci1 1:e392595b4b76 8 Websocket& SnCommAfar::GetWS() {
uci1 1:e392595b4b76 9 // only one make one socket
uci1 1:e392595b4b76 10 //static Websocket* ws = new Websocket("ws://snowflake.ps.uci.edu:6767/ws");
uci1 1:e392595b4b76 11 // TODO: fix DNS
uci1 1:e392595b4b76 12 // TODO: remove hardcoded IP addresses from Websocket.cpp
uci1 1:e392595b4b76 13 static Websocket* ws = new Websocket("ws://128.195.204.151:6767/ws");
uci1 1:e392595b4b76 14 return *ws;
uci1 1:e392595b4b76 15 }
uci1 1:e392595b4b76 16
uci1 1:e392595b4b76 17 bool SnCommAfar::Connect(const uint32_t timeout) {
uci1 1:e392595b4b76 18 bool isConn = GetWS().connected();
uci1 1:e392595b4b76 19 printf("connect: ct=%d, timeout=%u\r\n",time(0),timeout);
uci1 1:e392595b4b76 20 while ( (isConn==false) && ( time(0) < timeout) ) {
uci1 1:e392595b4b76 21 wait_ms(250);
uci1 1:e392595b4b76 22 #ifdef DEBUG
uci1 1:e392595b4b76 23 printf("connecting..\r\n");
uci1 1:e392595b4b76 24 DigitalOut led3(LED3);
uci1 1:e392595b4b76 25 led3=1; wait(0.2);
uci1 1:e392595b4b76 26 #endif
uci1 1:e392595b4b76 27 isConn = GetWS().connect(timeout);
uci1 1:e392595b4b76 28 #ifdef DEBUG
uci1 1:e392595b4b76 29 printf("isConn=%d\r\n",(int)isConn);
uci1 1:e392595b4b76 30 led3=0; wait(0.2);
uci1 1:e392595b4b76 31 #endif
uci1 1:e392595b4b76 32 }
uci1 1:e392595b4b76 33 return isConn;
uci1 1:e392595b4b76 34 }
uci1 1:e392595b4b76 35
uci1 1:e392595b4b76 36 SnCommWin::ECommWinResult SnCommAfar::OpenWindow(const uint32_t timeout,
uci1 1:e392595b4b76 37 const bool sendStatus,
uci1 1:e392595b4b76 38 const SnConfigFrame& conf,
uci1 1:e392595b4b76 39 const SnEventFrame& evt,
uci1 1:e392595b4b76 40 char* const evtBuf,
uci1 1:e392595b4b76 41 char* const statBuf) {
uci1 1:e392595b4b76 42 // timer = timer for full window
uci1 1:e392595b4b76 43 // timeout = number of seconds to try to connect (not full window)
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 1:e392595b4b76 51 ret = SendStatus(conf, evt, evtBuf, statBuf);
uci1 1:e392595b4b76 52 }
uci1 1:e392595b4b76 53
uci1 1:e392595b4b76 54 return ret;
uci1 1:e392595b4b76 55 }
uci1 1:e392595b4b76 56
uci1 1:e392595b4b76 57
uci1 1:e392595b4b76 58 SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf,
uci1 1:e392595b4b76 59 const uint32_t timeOut,
uci1 1:e392595b4b76 60 char* const confBuf) {
uci1 1:e392595b4b76 61 // confBuf assumed to alread be of allocated size
uci1 1:e392595b4b76 62
uci1 1:e392595b4b76 63 printf("GetConfig, to=%u\r\n",timeOut);
uci1 1:e392595b4b76 64
uci1 1:e392595b4b76 65 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 1:e392595b4b76 66
uci1 1:e392595b4b76 67 bool readConf = false;
uci1 1:e392595b4b76 68 while ( (readConf==false) && (time(0) < timeOut) ) {
uci1 1:e392595b4b76 69 //printf("trying to read config\r\n");
uci1 1:e392595b4b76 70 // TODO: check if read works (for binary)
uci1 1:e392595b4b76 71 readConf = GetWS().read(confBuf, timeOut);
uci1 1:e392595b4b76 72 if (readConf) {
uci1 1:e392595b4b76 73 const char* b = confBuf;
uci1 1:e392595b4b76 74 conf.ReadFrom(b);
uci1 1:e392595b4b76 75 res = SnCommWin::kOkWithMsg;
uci1 1:e392595b4b76 76 }
uci1 1:e392595b4b76 77 }
uci1 1:e392595b4b76 78 if (readConf==false) {
uci1 1:e392595b4b76 79 res = SnCommWin::kOkNoMsg;
uci1 1:e392595b4b76 80 }
uci1 1:e392595b4b76 81
uci1 1:e392595b4b76 82 return res;
uci1 1:e392595b4b76 83 }
uci1 1:e392595b4b76 84
uci1 1:e392595b4b76 85 SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf,
uci1 1:e392595b4b76 86 const SnEventFrame& evt,
uci1 1:e392595b4b76 87 char* const evtBuf,
uci1 1:e392595b4b76 88 char* const statBuf) {
uci1 1:e392595b4b76 89 // TODO: check if connected?
uci1 1:e392595b4b76 90 SnStatusFrame::WriteTo(statBuf, SnConfigFrame::kAfar, conf, evt, evtBuf);
uci1 1:e392595b4b76 91 #ifdef DEBUG
uci1 1:e392595b4b76 92 printf("status frame:\r\n");
uci1 1:e392595b4b76 93 for (uint32_t i=0; i<SnStatusFrame::SizeOf(conf); i++) {
uci1 1:e392595b4b76 94 printf("%02X ",statBuf[i]);
uci1 1:e392595b4b76 95 }
uci1 1:e392595b4b76 96 printf("\r\n");
uci1 1:e392595b4b76 97 #endif
uci1 1:e392595b4b76 98 GetWS().sendBinary(statBuf, SnStatusFrame::SizeOf(conf));
uci1 1:e392595b4b76 99 //base64enc(statBuf, SnStatusFrame::SizeOf(conf), gB64Buf);
uci1 1:e392595b4b76 100 //GetWS().send(gB64Buf);
uci1 1:e392595b4b76 101 printf("status sent\r\n");
uci1 1:e392595b4b76 102 return SnCommWin::kOkMsgSent;
uci1 1:e392595b4b76 103 }
uci1 1:e392595b4b76 104
uci1 1:e392595b4b76 105 SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) {
uci1 1:e392595b4b76 106 fseek(inf, 0, SEEK_END);
uci1 1:e392595b4b76 107 const uint32_t fsize = ftell(inf);
uci1 1:e392595b4b76 108 fseek(inf, 0, SEEK_SET);
uci1 1:e392595b4b76 109 const bool ok = GetWS().sendBinary(inf, fsize);
uci1 1:e392595b4b76 110 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 1:e392595b4b76 111 }
uci1 1:e392595b4b76 112
uci1 1:e392595b4b76 113 SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf,
uci1 1:e392595b4b76 114 const SnConfigFrame& curConf,
uci1 1:e392595b4b76 115 SnEventFrame& evt,
uci1 1:e392595b4b76 116 char* const evtBuf,
uci1 1:e392595b4b76 117 char* const confBuf,
uci1 1:e392595b4b76 118 const uint32_t nevts,
uci1 1:e392595b4b76 119 const uint32_t firstEvt) {
uci1 1:e392595b4b76 120 // firstEvt==0 ==> start at beginning
uci1 1:e392595b4b76 121 // nevts==0 ==> NO events! (see SnCommWin::SendData
uci1 1:e392595b4b76 122 // for the fcn to send the full file)
uci1 1:e392595b4b76 123
uci1 1:e392595b4b76 124 // TODO: check memory for temporary config/event frames?
uci1 1:e392595b4b76 125 SnConfigFrame conf;
uci1 1:e392595b4b76 126 conf.ReadFrom(inf);
uci1 1:e392595b4b76 127 char* b = confBuf;
uci1 1:e392595b4b76 128 conf.WriteTo(b);
uci1 1:e392595b4b76 129 bool ok = GetWS().sendBinary(confBuf, conf.SizeOf());
uci1 1:e392595b4b76 130
uci1 1:e392595b4b76 131 if (ok) {
uci1 1:e392595b4b76 132 uint8_t sLoseLSB=0, sLoseMSB=0;
uci1 1:e392595b4b76 133 uint16_t sWvBase=0;
uci1 1:e392595b4b76 134 conf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase);
uci1 1:e392595b4b76 135 // do we have to unpack & repack events?
uci1 1:e392595b4b76 136 const bool repack = (sLoseLSB != conf.GetWvLoseLSB())
uci1 1:e392595b4b76 137 && (sLoseMSB != conf.GetWvLoseMSB())
uci1 1:e392595b4b76 138 && (sWvBase != conf.GetWvBaseline());
uci1 1:e392595b4b76 139
uci1 1:e392595b4b76 140 // size of event in file
uci1 1:e392595b4b76 141 const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(),
uci1 1:e392595b4b76 142 conf.GetWvLoseMSB());
uci1 1:e392595b4b76 143 // move up to first event
uci1 1:e392595b4b76 144 fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET);
uci1 1:e392595b4b76 145 if (repack) {
uci1 1:e392595b4b76 146 // repack ==> send event by event
uci1 1:e392595b4b76 147 // size of event sent over afar
uci1 1:e392595b4b76 148 const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB);
uci1 1:e392595b4b76 149 for (uint32_t i=0; (i<nevts) && ok; i++) {
uci1 1:e392595b4b76 150 evt.ReadFrom(inf, evtBuf,
uci1 1:e392595b4b76 151 conf.GetWvLoseLSB(), conf.GetWvLoseMSB(),
uci1 1:e392595b4b76 152 conf.GetWvBaseline());
uci1 1:e392595b4b76 153 evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase);
uci1 1:e392595b4b76 154 ok = GetWS().sendBinary(evtBuf, ssize);
uci1 1:e392595b4b76 155 }
uci1 1:e392595b4b76 156 } else {
uci1 1:e392595b4b76 157 // no repacking ==> just send the bytes from the file
uci1 1:e392595b4b76 158 ok = GetWS().sendBinary(inf, nevts*esize);
uci1 1:e392595b4b76 159 }
uci1 1:e392595b4b76 160 }
uci1 1:e392595b4b76 161 return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent;
uci1 1:e392595b4b76 162 }