Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfar.cpp@2:e67f7c158087, 2012-07-24 (annotated)
- Committer:
- uci1
- Date:
- Tue Jul 24 02:07:23 2012 +0000
- Revision:
- 2:e67f7c158087
- Parent:
- 1:e392595b4b76
- Child:
- 3:24c5f0f50bf1
added header to i/o. still lots of debugging prints.
Who changed what in which revision?
User | Revision | Line number | New 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 | // timer = timer for full window |
uci1 | 1:e392595b4b76 | 44 | // timeout = number of seconds to try to connect (not full window) |
uci1 | 1:e392595b4b76 | 45 | |
uci1 | 1:e392595b4b76 | 46 | const bool canCon = Connect(timeout); |
uci1 | 1:e392595b4b76 | 47 | |
uci1 | 1:e392595b4b76 | 48 | SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected |
uci1 | 1:e392595b4b76 | 49 | : SnCommWin::kCanNotConnect; |
uci1 | 1:e392595b4b76 | 50 | |
uci1 | 1:e392595b4b76 | 51 | if (canCon && sendStatus) { |
uci1 | 2:e67f7c158087 | 52 | ret = SendStatus(conf, evt, genBuf); |
uci1 | 1:e392595b4b76 | 53 | } |
uci1 | 1:e392595b4b76 | 54 | |
uci1 | 1:e392595b4b76 | 55 | return ret; |
uci1 | 1:e392595b4b76 | 56 | } |
uci1 | 1:e392595b4b76 | 57 | |
uci1 | 1:e392595b4b76 | 58 | |
uci1 | 1:e392595b4b76 | 59 | SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf, |
uci1 | 1:e392595b4b76 | 60 | const uint32_t timeOut, |
uci1 | 1:e392595b4b76 | 61 | char* const confBuf) { |
uci1 | 1:e392595b4b76 | 62 | // confBuf assumed to alread be of allocated size |
uci1 | 1:e392595b4b76 | 63 | |
uci1 | 1:e392595b4b76 | 64 | printf("GetConfig, to=%u\r\n",timeOut); |
uci1 | 1:e392595b4b76 | 65 | |
uci1 | 1:e392595b4b76 | 66 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 1:e392595b4b76 | 67 | |
uci1 | 2:e67f7c158087 | 68 | bool redConf = false; |
uci1 | 2:e67f7c158087 | 69 | bool readHeader = true; |
uci1 | 2:e67f7c158087 | 70 | uint8_t mcode=0x0; |
uci1 | 2:e67f7c158087 | 71 | uint32_t mlen=0; // length of message specified in header |
uci1 | 2:e67f7c158087 | 72 | uint32_t mread=0; // length of messages received so far |
uci1 | 2:e67f7c158087 | 73 | const char* b = confBuf; |
uci1 | 2:e67f7c158087 | 74 | while ( (redConf==false) && (time(0) < timeOut) ) { |
uci1 | 2:e67f7c158087 | 75 | uint32_t msglen=0; // this message length |
uci1 | 2:e67f7c158087 | 76 | redConf = GetWS().read(confBuf+mread, msglen, timeOut); |
uci1 | 2:e67f7c158087 | 77 | mread += msglen; |
uci1 | 2:e67f7c158087 | 78 | if (redConf) { |
uci1 | 2:e67f7c158087 | 79 | if (readHeader && mread>=SnHeaderFrame::SizeOf()) { |
uci1 | 2:e67f7c158087 | 80 | SnHeaderFrame::ReadFrom(b, mcode, mlen); |
uci1 | 2:e67f7c158087 | 81 | // TODO: check that mcode is kConfigCode? What to do if not? |
uci1 | 2:e67f7c158087 | 82 | readHeader=false; |
uci1 | 2:e67f7c158087 | 83 | } |
uci1 | 2:e67f7c158087 | 84 | if (mread>=mlen) { |
uci1 | 2:e67f7c158087 | 85 | conf.ReadFrom(b); |
uci1 | 2:e67f7c158087 | 86 | res = SnCommWin::kOkWithMsg; |
uci1 | 2:e67f7c158087 | 87 | } else { |
uci1 | 2:e67f7c158087 | 88 | redConf=false; |
uci1 | 2:e67f7c158087 | 89 | } |
uci1 | 1:e392595b4b76 | 90 | } |
uci1 | 1:e392595b4b76 | 91 | } |
uci1 | 2:e67f7c158087 | 92 | if (redConf==false) { |
uci1 | 1:e392595b4b76 | 93 | res = SnCommWin::kOkNoMsg; |
uci1 | 1:e392595b4b76 | 94 | } |
uci1 | 1:e392595b4b76 | 95 | |
uci1 | 1:e392595b4b76 | 96 | return res; |
uci1 | 1:e392595b4b76 | 97 | } |
uci1 | 1:e392595b4b76 | 98 | |
uci1 | 1:e392595b4b76 | 99 | SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf, |
uci1 | 1:e392595b4b76 | 100 | const SnEventFrame& evt, |
uci1 | 2:e67f7c158087 | 101 | char* const genBuf) { |
uci1 | 1:e392595b4b76 | 102 | // TODO: check if connected? |
uci1 | 2:e67f7c158087 | 103 | const uint32_t ssize = SnStatusFrame::SizeOf(conf); |
uci1 | 2:e67f7c158087 | 104 | SnStatusFrame::WriteTo(genBuf, SnConfigFrame::kAfar, conf, evt, genBuf); |
uci1 | 1:e392595b4b76 | 105 | #ifdef DEBUG |
uci1 | 1:e392595b4b76 | 106 | printf("status frame:\r\n"); |
uci1 | 2:e67f7c158087 | 107 | for (uint32_t i=0; i<msize; i++) { |
uci1 | 2:e67f7c158087 | 108 | printf("%02X ",genBuf[i]); |
uci1 | 1:e392595b4b76 | 109 | } |
uci1 | 1:e392595b4b76 | 110 | printf("\r\n"); |
uci1 | 1:e392595b4b76 | 111 | #endif |
uci1 | 2:e67f7c158087 | 112 | const bool ok = GetWS().sendBinary( |
uci1 | 2:e67f7c158087 | 113 | SnHeaderFrame::GetHdBuf(SnHeaderFrame::kStatusCode, ssize), |
uci1 | 2:e67f7c158087 | 114 | SnHeaderFrame::SizeOf(), |
uci1 | 2:e67f7c158087 | 115 | genBuf, ssize); |
uci1 | 1:e392595b4b76 | 116 | printf("status sent\r\n"); |
uci1 | 2:e67f7c158087 | 117 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 1:e392595b4b76 | 118 | } |
uci1 | 1:e392595b4b76 | 119 | |
uci1 | 1:e392595b4b76 | 120 | SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) { |
uci1 | 1:e392595b4b76 | 121 | fseek(inf, 0, SEEK_END); |
uci1 | 2:e67f7c158087 | 122 | const int32_t fsize = ftell(inf); |
uci1 | 1:e392595b4b76 | 123 | fseek(inf, 0, SEEK_SET); |
uci1 | 2:e67f7c158087 | 124 | if (fsize<0) { |
uci1 | 2:e67f7c158087 | 125 | return SnCommWin::kFailNoneSent; |
uci1 | 2:e67f7c158087 | 126 | } else { |
uci1 | 2:e67f7c158087 | 127 | // send file data with header |
uci1 | 2:e67f7c158087 | 128 | const bool ok = GetWS().sendBinary( |
uci1 | 2:e67f7c158087 | 129 | SnHeaderFrame::GetHdBuf(SnHeaderFrame::kFileCode, fsize), |
uci1 | 2:e67f7c158087 | 130 | SnHeaderFrame::SizeOf(), |
uci1 | 2:e67f7c158087 | 131 | inf, fsize); |
uci1 | 2:e67f7c158087 | 132 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 2:e67f7c158087 | 133 | } |
uci1 | 1:e392595b4b76 | 134 | } |
uci1 | 1:e392595b4b76 | 135 | |
uci1 | 1:e392595b4b76 | 136 | SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf, |
uci1 | 1:e392595b4b76 | 137 | const SnConfigFrame& curConf, |
uci1 | 1:e392595b4b76 | 138 | SnEventFrame& evt, |
uci1 | 2:e67f7c158087 | 139 | char* const genBuf, |
uci1 | 1:e392595b4b76 | 140 | const uint32_t nevts, |
uci1 | 1:e392595b4b76 | 141 | const uint32_t firstEvt) { |
uci1 | 1:e392595b4b76 | 142 | // firstEvt==0 ==> start at beginning |
uci1 | 1:e392595b4b76 | 143 | // nevts==0 ==> NO events! (see SnCommWin::SendData |
uci1 | 1:e392595b4b76 | 144 | // for the fcn to send the full file) |
uci1 | 2:e67f7c158087 | 145 | |
uci1 | 1:e392595b4b76 | 146 | // TODO: check memory for temporary config/event frames? |
uci1 | 2:e67f7c158087 | 147 | uint64_t macadr; |
uci1 | 2:e67f7c158087 | 148 | uint32_t run; |
uci1 | 2:e67f7c158087 | 149 | uint16_t seq; |
uci1 | 2:e67f7c158087 | 150 | SnSDUtils::ReadFileHeader(inf, macadr, run, seq); |
uci1 | 1:e392595b4b76 | 151 | SnConfigFrame conf; |
uci1 | 1:e392595b4b76 | 152 | conf.ReadFrom(inf); |
uci1 | 2:e67f7c158087 | 153 | |
uci1 | 2:e67f7c158087 | 154 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 2:e67f7c158087 | 155 | uint16_t sWvBase=0; |
uci1 | 2:e67f7c158087 | 156 | curConf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 2:e67f7c158087 | 157 | // do we have to unpack & repack events? |
uci1 | 2:e67f7c158087 | 158 | const bool repack = (sLoseLSB != conf.GetWvLoseLSB()) |
uci1 | 2:e67f7c158087 | 159 | && (sLoseMSB != conf.GetWvLoseMSB()) |
uci1 | 2:e67f7c158087 | 160 | && (sWvBase != conf.GetWvBaseline()); |
uci1 | 2:e67f7c158087 | 161 | |
uci1 | 2:e67f7c158087 | 162 | // size of event in file |
uci1 | 2:e67f7c158087 | 163 | const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(), |
uci1 | 2:e67f7c158087 | 164 | conf.GetWvLoseMSB()); |
uci1 | 2:e67f7c158087 | 165 | // move up to first event |
uci1 | 2:e67f7c158087 | 166 | fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET); |
uci1 | 2:e67f7c158087 | 167 | |
uci1 | 2:e67f7c158087 | 168 | // send the header and conf |
uci1 | 2:e67f7c158087 | 169 | char* b = genBuf; |
uci1 | 1:e392595b4b76 | 170 | conf.WriteTo(b); |
uci1 | 2:e67f7c158087 | 171 | bool ok = GetWS().sendBinary( |
uci1 | 2:e67f7c158087 | 172 | SnHeaderFrame::GetHdBuf(SnHeaderFrame::kConfAndEvtsCode, |
uci1 | 2:e67f7c158087 | 173 | conf.SizeOf()), |
uci1 | 2:e67f7c158087 | 174 | SnHeaderFrame::SizeOf(), |
uci1 | 2:e67f7c158087 | 175 | genBuf, conf.SizeOf()); |
uci1 | 2:e67f7c158087 | 176 | |
uci1 | 2:e67f7c158087 | 177 | // size of event sent over afar |
uci1 | 2:e67f7c158087 | 178 | const uint32_t ssize = (repack) ? |
uci1 | 2:e67f7c158087 | 179 | SnEventFrame::SizeOf(sLoseLSB, sLoseMSB) : |
uci1 | 2:e67f7c158087 | 180 | esize; |
uci1 | 2:e67f7c158087 | 181 | |
uci1 | 2:e67f7c158087 | 182 | for (uint32_t i=0; (i<nevts) && ok; i++) { |
uci1 | 1:e392595b4b76 | 183 | if (repack) { |
uci1 | 2:e67f7c158087 | 184 | evt.ReadFrom(inf, genBuf, |
uci1 | 2:e67f7c158087 | 185 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 2:e67f7c158087 | 186 | conf.GetWvBaseline()); |
uci1 | 2:e67f7c158087 | 187 | evt.WriteTo(genBuf, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 2:e67f7c158087 | 188 | ok &= GetWS().sendBinary( |
uci1 | 2:e67f7c158087 | 189 | SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode, |
uci1 | 2:e67f7c158087 | 190 | ssize), |
uci1 | 2:e67f7c158087 | 191 | SnHeaderFrame::SizeOf(), |
uci1 | 2:e67f7c158087 | 192 | genBuf, ssize); |
uci1 | 1:e392595b4b76 | 193 | } else { |
uci1 | 2:e67f7c158087 | 194 | ok &= GetWS().sendBinary( |
uci1 | 2:e67f7c158087 | 195 | SnHeaderFrame::GetHdBuf(SnHeaderFrame::kEventCode, |
uci1 | 2:e67f7c158087 | 196 | ssize), |
uci1 | 2:e67f7c158087 | 197 | SnHeaderFrame::SizeOf(), |
uci1 | 2:e67f7c158087 | 198 | inf, ssize); |
uci1 | 1:e392595b4b76 | 199 | } |
uci1 | 1:e392595b4b76 | 200 | } |
uci1 | 1:e392595b4b76 | 201 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 1:e392595b4b76 | 202 | } |