Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfar.cpp@0:664899e0b988, 2012-06-30 (annotated)
- Committer:
- uci1
- Date:
- Sat Jun 30 02:03:51 2012 +0000
- Revision:
- 0:664899e0b988
- Child:
- 1:e392595b4b76
first version. SD card writing and data readout works. communications not tested.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | #include "SnCommAfar.h" |
uci1 | 0:664899e0b988 | 2 | |
uci1 | 0:664899e0b988 | 3 | #include "Websocket.h" |
uci1 | 0:664899e0b988 | 4 | #include "SnConfigFrame.h" |
uci1 | 0:664899e0b988 | 5 | #include "SnEventFrame.h" |
uci1 | 0:664899e0b988 | 6 | #include "SnStatusFrame.h" |
uci1 | 0:664899e0b988 | 7 | |
uci1 | 0:664899e0b988 | 8 | Websocket& SnCommAfar::GetWS() { |
uci1 | 0:664899e0b988 | 9 | // only one make one socket |
uci1 | 0:664899e0b988 | 10 | static Websocket* ws = new Websocket("ws://snowflake.ps.uci.edu:6767/ws"); |
uci1 | 0:664899e0b988 | 11 | return *ws; |
uci1 | 0:664899e0b988 | 12 | } |
uci1 | 0:664899e0b988 | 13 | |
uci1 | 0:664899e0b988 | 14 | bool SnCommAfar::Connect(Timer& timer, const uint32_t timeout) { |
uci1 | 0:664899e0b988 | 15 | bool isConn = GetWS().connected(); |
uci1 | 0:664899e0b988 | 16 | |
uci1 | 0:664899e0b988 | 17 | while ( (isConn==false) && (timer.read() < timeout) ) { |
uci1 | 0:664899e0b988 | 18 | wait_ms(250); |
uci1 | 0:664899e0b988 | 19 | |
uci1 | 0:664899e0b988 | 20 | isConn = GetWS().connect(&timer, timeout); |
uci1 | 0:664899e0b988 | 21 | } |
uci1 | 0:664899e0b988 | 22 | |
uci1 | 0:664899e0b988 | 23 | return isConn; |
uci1 | 0:664899e0b988 | 24 | } |
uci1 | 0:664899e0b988 | 25 | |
uci1 | 0:664899e0b988 | 26 | SnCommWin::ECommWinResult SnCommAfar::OpenWindow(Timer& timer, |
uci1 | 0:664899e0b988 | 27 | const uint32_t timeout, |
uci1 | 0:664899e0b988 | 28 | const bool sendStatus, |
uci1 | 0:664899e0b988 | 29 | const SnConfigFrame& conf, |
uci1 | 0:664899e0b988 | 30 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 31 | char* const evtBuf, |
uci1 | 0:664899e0b988 | 32 | char* const statBuf) { |
uci1 | 0:664899e0b988 | 33 | // timer = timer for full window |
uci1 | 0:664899e0b988 | 34 | // timeout = number of seconds to try to connect (not full window) |
uci1 | 0:664899e0b988 | 35 | |
uci1 | 0:664899e0b988 | 36 | const bool canCon = Connect(timer, timeout); |
uci1 | 0:664899e0b988 | 37 | |
uci1 | 0:664899e0b988 | 38 | SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected |
uci1 | 0:664899e0b988 | 39 | : SnCommWin::kCanNotConnect; |
uci1 | 0:664899e0b988 | 40 | |
uci1 | 0:664899e0b988 | 41 | if (canCon && sendStatus) { |
uci1 | 0:664899e0b988 | 42 | ret = SendStatus(conf, evt, evtBuf, statBuf); |
uci1 | 0:664899e0b988 | 43 | } |
uci1 | 0:664899e0b988 | 44 | |
uci1 | 0:664899e0b988 | 45 | return ret; |
uci1 | 0:664899e0b988 | 46 | } |
uci1 | 0:664899e0b988 | 47 | |
uci1 | 0:664899e0b988 | 48 | |
uci1 | 0:664899e0b988 | 49 | SnCommWin::ECommWinResult SnCommAfar::GetConfig(SnConfigFrame& conf, |
uci1 | 0:664899e0b988 | 50 | Timer& timer, |
uci1 | 0:664899e0b988 | 51 | const uint32_t timeOut, |
uci1 | 0:664899e0b988 | 52 | char* const confBuf) { |
uci1 | 0:664899e0b988 | 53 | // confBuf assumed to alread be of allocated size |
uci1 | 0:664899e0b988 | 54 | |
uci1 | 0:664899e0b988 | 55 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 0:664899e0b988 | 56 | |
uci1 | 0:664899e0b988 | 57 | const uint32_t rto = (timeOut < 3u) ? timeOut : 3u; |
uci1 | 0:664899e0b988 | 58 | bool readConf = false; |
uci1 | 0:664899e0b988 | 59 | while ( (readConf==false) && (timer.read() < timeOut) ) { |
uci1 | 0:664899e0b988 | 60 | // TODO: check if read works (for binary) |
uci1 | 0:664899e0b988 | 61 | readConf = GetWS().read(confBuf, &timer, rto); |
uci1 | 0:664899e0b988 | 62 | if (readConf) { |
uci1 | 0:664899e0b988 | 63 | const char* b = confBuf; |
uci1 | 0:664899e0b988 | 64 | conf.ReadFrom(b); |
uci1 | 0:664899e0b988 | 65 | res = SnCommWin::kOkWithMsg; |
uci1 | 0:664899e0b988 | 66 | } |
uci1 | 0:664899e0b988 | 67 | } |
uci1 | 0:664899e0b988 | 68 | if (readConf==false) { |
uci1 | 0:664899e0b988 | 69 | res = SnCommWin::kOkNoMsg; |
uci1 | 0:664899e0b988 | 70 | } |
uci1 | 0:664899e0b988 | 71 | |
uci1 | 0:664899e0b988 | 72 | return res; |
uci1 | 0:664899e0b988 | 73 | } |
uci1 | 0:664899e0b988 | 74 | |
uci1 | 0:664899e0b988 | 75 | SnCommWin::ECommWinResult SnCommAfar::SendStatus(const SnConfigFrame& conf, |
uci1 | 0:664899e0b988 | 76 | const SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 77 | char* const evtBuf, |
uci1 | 0:664899e0b988 | 78 | char* const statBuf) { |
uci1 | 0:664899e0b988 | 79 | // TODO: check if connected? |
uci1 | 0:664899e0b988 | 80 | SnStatusFrame::WriteTo(statBuf, SnConfigFrame::kAfar, conf, evt, evtBuf); |
uci1 | 0:664899e0b988 | 81 | GetWS().sendBinary(statBuf, SnStatusFrame::SizeOf(conf)); |
uci1 | 0:664899e0b988 | 82 | return SnCommWin::kOkMsgSent; |
uci1 | 0:664899e0b988 | 83 | } |
uci1 | 0:664899e0b988 | 84 | |
uci1 | 0:664899e0b988 | 85 | SnCommWin::ECommWinResult SnCommAfar::SendData(FILE* inf) { |
uci1 | 0:664899e0b988 | 86 | fseek(inf, 0, SEEK_END); |
uci1 | 0:664899e0b988 | 87 | const uint32_t fsize = ftell(inf); |
uci1 | 0:664899e0b988 | 88 | fseek(inf, 0, SEEK_SET); |
uci1 | 0:664899e0b988 | 89 | const bool ok = GetWS().sendBinary(inf, fsize); |
uci1 | 0:664899e0b988 | 90 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 0:664899e0b988 | 91 | } |
uci1 | 0:664899e0b988 | 92 | |
uci1 | 0:664899e0b988 | 93 | SnCommWin::ECommWinResult SnCommAfar::SendConfAndEvents(FILE* inf, |
uci1 | 0:664899e0b988 | 94 | const SnConfigFrame& curConf, |
uci1 | 0:664899e0b988 | 95 | SnEventFrame& evt, |
uci1 | 0:664899e0b988 | 96 | char* const evtBuf, |
uci1 | 0:664899e0b988 | 97 | char* const confBuf, |
uci1 | 0:664899e0b988 | 98 | const uint32_t nevts, |
uci1 | 0:664899e0b988 | 99 | const uint32_t firstEvt) { |
uci1 | 0:664899e0b988 | 100 | // firstEvt==0 ==> start at beginning |
uci1 | 0:664899e0b988 | 101 | // nevts==0 ==> NO events! (see SnCommWin::SendData |
uci1 | 0:664899e0b988 | 102 | // for the fcn to send the full file) |
uci1 | 0:664899e0b988 | 103 | |
uci1 | 0:664899e0b988 | 104 | // TODO: check memory for temporary config/event frames? |
uci1 | 0:664899e0b988 | 105 | SnConfigFrame conf; |
uci1 | 0:664899e0b988 | 106 | conf.ReadFrom(inf); |
uci1 | 0:664899e0b988 | 107 | char* b = confBuf; |
uci1 | 0:664899e0b988 | 108 | conf.WriteTo(b); |
uci1 | 0:664899e0b988 | 109 | bool ok = GetWS().sendBinary(confBuf, conf.SizeOf()); |
uci1 | 0:664899e0b988 | 110 | |
uci1 | 0:664899e0b988 | 111 | if (ok) { |
uci1 | 0:664899e0b988 | 112 | uint8_t sLoseLSB=0, sLoseMSB=0; |
uci1 | 0:664899e0b988 | 113 | uint16_t sWvBase=0; |
uci1 | 0:664899e0b988 | 114 | conf.GetPackParsFor(SnConfigFrame::kAfar, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 0:664899e0b988 | 115 | // do we have to unpack & repack events? |
uci1 | 0:664899e0b988 | 116 | const bool repack = (sLoseLSB != conf.GetWvLoseLSB()) |
uci1 | 0:664899e0b988 | 117 | && (sLoseMSB != conf.GetWvLoseMSB()) |
uci1 | 0:664899e0b988 | 118 | && (sWvBase != conf.GetWvBaseline()); |
uci1 | 0:664899e0b988 | 119 | |
uci1 | 0:664899e0b988 | 120 | // size of event in file |
uci1 | 0:664899e0b988 | 121 | const uint32_t esize = SnEventFrame::SizeOf(conf.GetWvLoseLSB(), |
uci1 | 0:664899e0b988 | 122 | conf.GetWvLoseMSB()); |
uci1 | 0:664899e0b988 | 123 | // move up to first event |
uci1 | 0:664899e0b988 | 124 | fseek(inf, conf.SizeOf() + (firstEvt*esize), SEEK_SET); |
uci1 | 0:664899e0b988 | 125 | if (repack) { |
uci1 | 0:664899e0b988 | 126 | // repack ==> send event by event |
uci1 | 0:664899e0b988 | 127 | // size of event sent over afar |
uci1 | 0:664899e0b988 | 128 | const uint32_t ssize = SnEventFrame::SizeOf(sLoseLSB, sLoseMSB); |
uci1 | 0:664899e0b988 | 129 | for (uint32_t i=0; (i<nevts) && ok; i++) { |
uci1 | 0:664899e0b988 | 130 | evt.ReadFrom(inf, evtBuf, |
uci1 | 0:664899e0b988 | 131 | conf.GetWvLoseLSB(), conf.GetWvLoseMSB(), |
uci1 | 0:664899e0b988 | 132 | conf.GetWvBaseline()); |
uci1 | 0:664899e0b988 | 133 | evt.WriteTo(evtBuf, sLoseLSB, sLoseMSB, sWvBase); |
uci1 | 0:664899e0b988 | 134 | ok = GetWS().sendBinary(evtBuf, ssize); |
uci1 | 0:664899e0b988 | 135 | } |
uci1 | 0:664899e0b988 | 136 | } else { |
uci1 | 0:664899e0b988 | 137 | // no repacking ==> just send the bytes from the file |
uci1 | 0:664899e0b988 | 138 | ok = GetWS().sendBinary(inf, nevts*esize); |
uci1 | 0:664899e0b988 | 139 | } |
uci1 | 0:664899e0b988 | 140 | } |
uci1 | 0:664899e0b988 | 141 | return ok ? SnCommWin::kOkMsgSent : SnCommWin::kFailPartSent; |
uci1 | 0:664899e0b988 | 142 | } |