Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfarTCP.cpp@4:a91682e19d6b, 2012-08-02 (annotated)
- Committer:
- uci1
- Date:
- Thu Aug 02 05:42:47 2012 +0000
- Revision:
- 4:a91682e19d6b
- Parent:
- 3:24c5f0f50bf1
- Child:
- 5:9cea89700c66
Add power reading, once per file. Prevent seq number from wrapping around. Still many debugging messages. Communications not finished.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 3:24c5f0f50bf1 | 1 | /* |
uci1 | 3:24c5f0f50bf1 | 2 | #include "SnCommAfarTCP.h" |
uci1 | 3:24c5f0f50bf1 | 3 | |
uci1 | 4:a91682e19d6b | 4 | SnCommAfarTCP::SnCommAfarTCP(const bool useb64, |
uci1 | 4:a91682e19d6b | 5 | char* const b64buf, const uint32_t bblen) : |
uci1 | 4:a91682e19d6b | 6 | fUseB64(useb64), fB64buf(b64buf), fbblen(bblen), fRmtServ(remote), |
uci1 | 3:24c5f0f50bf1 | 7 | fEth(new EthernetInterface), fSock(new TCPSocketConnection) { |
uci1 | 3:24c5f0f50bf1 | 8 | |
uci1 | 3:24c5f0f50bf1 | 9 | fEth->init("128.195.204.148", // my IP |
uci1 | 3:24c5f0f50bf1 | 10 | "255.255.255.0", // mask |
uci1 | 3:24c5f0f50bf1 | 11 | "128.195.204.1"); // gateway |
uci1 | 3:24c5f0f50bf1 | 12 | |
uci1 | 3:24c5f0f50bf1 | 13 | fRserv = "128.195.204.151"; |
uci1 | 3:24c5f0f50bf1 | 14 | fRport = 6655; |
uci1 | 3:24c5f0f50bf1 | 15 | |
uci1 | 3:24c5f0f50bf1 | 16 | } |
uci1 | 3:24c5f0f50bf1 | 17 | |
uci1 | 3:24c5f0f50bf1 | 18 | SnCommAfarTCP::~SnCommAfarTCP() { |
uci1 | 3:24c5f0f50bf1 | 19 | delete fEth; |
uci1 | 3:24c5f0f50bf1 | 20 | delete fSock; |
uci1 | 3:24c5f0f50bf1 | 21 | } |
uci1 | 3:24c5f0f50bf1 | 22 | |
uci1 | 4:a91682e19d6b | 23 | int SnCommAfarTCP::Receive(char* const buf, const uin32_t mlen, |
uci1 | 4:a91682e19d6b | 24 | const uint32_t bsize, |
uci1 | 4:a91682e19d6b | 25 | const uint32_t timeout_clock) { |
uci1 | 4:a91682e19d6b | 26 | |
uci1 | 4:a91682e19d6b | 27 | } |
uci1 | 4:a91682e19d6b | 28 | |
uci1 | 3:24c5f0f50bf1 | 29 | int SnCommAfarTCP::SendAll(const char* const data, const uint32_t length, |
uci1 | 3:24c5f0f50bf1 | 30 | const uint32_t timeout_clock) { |
uci1 | 3:24c5f0f50bf1 | 31 | int res=0; |
uci1 | 3:24c5f0f50bf1 | 32 | uint32_t b=0; |
uci1 | 3:24c5f0f50bf1 | 33 | while ( (length>b) && (time(0)<timeout_clock) ) { |
uci1 | 4:a91682e19d6b | 34 | res = fSock->send_all(data+b, length-b); |
uci1 | 4:a91682e19d6b | 35 | switch (res) { |
uci1 | 4:a91682e19d6b | 36 | case -1: |
uci1 | 4:a91682e19d6b | 37 | // TODO: how to check the error? |
uci1 | 4:a91682e19d6b | 38 | continue; |
uci1 | 4:a91682e19d6b | 39 | case 0: |
uci1 | 4:a91682e19d6b | 40 | return res; |
uci1 | 4:a91682e19d6b | 41 | default: |
uci1 | 4:a91682e19d6b | 42 | b += res; |
uci1 | 4:a91682e19d6b | 43 | }; |
uci1 | 3:24c5f0f50bf1 | 44 | } |
uci1 | 4:a91682e19d6b | 45 | return res; // timeout |
uci1 | 3:24c5f0f50bf1 | 46 | } |
uci1 | 3:24c5f0f50bf1 | 47 | |
uci1 | 3:24c5f0f50bf1 | 48 | |
uci1 | 3:24c5f0f50bf1 | 49 | bool SnCommAfarTCP::Connect(const uint32_t timeout) { |
uci1 | 3:24c5f0f50bf1 | 50 | bool isConn = false; |
uci1 | 3:24c5f0f50bf1 | 51 | |
uci1 | 3:24c5f0f50bf1 | 52 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 53 | wait_ms(250); |
uci1 | 3:24c5f0f50bf1 | 54 | isConn = (fEth->connect()==0); |
uci1 | 3:24c5f0f50bf1 | 55 | } |
uci1 | 3:24c5f0f50bf1 | 56 | |
uci1 | 3:24c5f0f50bf1 | 57 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 58 | wait_ms(250); |
uci1 | 3:24c5f0f50bf1 | 59 | isConn = (fSock->connect(fRserv.c_str(), fRport)==0);s |
uci1 | 3:24c5f0f50bf1 | 60 | } |
uci1 | 3:24c5f0f50bf1 | 61 | |
uci1 | 3:24c5f0f50bf1 | 62 | return isConn; |
uci1 | 3:24c5f0f50bf1 | 63 | } |
uci1 | 3:24c5f0f50bf1 | 64 | |
uci1 | 3:24c5f0f50bf1 | 65 | SnCommWin::ECommWinResult SnCommAfarTCP::OpenWindow(const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 66 | const bool sendStatus, |
uci1 | 3:24c5f0f50bf1 | 67 | const SnConfigFrame& conf, |
uci1 | 3:24c5f0f50bf1 | 68 | const SnEventFrame& evt, |
uci1 | 3:24c5f0f50bf1 | 69 | char* const genBuf) { |
uci1 | 3:24c5f0f50bf1 | 70 | const bool canCon = Connect(timeout); |
uci1 | 3:24c5f0f50bf1 | 71 | |
uci1 | 3:24c5f0f50bf1 | 72 | SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected |
uci1 | 3:24c5f0f50bf1 | 73 | : SnCommWin::kCanNotConnect; |
uci1 | 3:24c5f0f50bf1 | 74 | |
uci1 | 3:24c5f0f50bf1 | 75 | if (canCon && sendStatus) { |
uci1 | 3:24c5f0f50bf1 | 76 | ret = SendStatus(conf, evt, genBuf); |
uci1 | 3:24c5f0f50bf1 | 77 | } |
uci1 | 3:24c5f0f50bf1 | 78 | |
uci1 | 3:24c5f0f50bf1 | 79 | return ret; |
uci1 | 3:24c5f0f50bf1 | 80 | } |
uci1 | 4:a91682e19d6b | 81 | |
uci1 | 4:a91682e19d6b | 82 | SnCommWin::ECommWinResult SnCommAfarTCP::WaitHandshake(const uint32_t timeout, |
uci1 | 4:a91682e19d6b | 83 | char* const buf, |
uci1 | 4:a91682e19d6b | 84 | const uint32_t bsize) { |
uci1 | 4:a91682e19d6b | 85 | printf("WaitHandshake, to=%u\r\n",timeout); |
uci1 | 4:a91682e19d6b | 86 | |
uci1 | 4:a91682e19d6b | 87 | uint32_t mlen=0; // this message length |
uci1 | 4:a91682e19d6b | 88 | const bool rd = Receive(buf, mlen, bsize, timeout, fB64buf, fbblen); |
uci1 | 4:a91682e19d6b | 89 | if (rd) { |
uci1 | 4:a91682e19d6b | 90 | uint32_t msgLen=0; |
uci1 | 4:a91682e19d6b | 91 | uint8_t msgCode=0; |
uci1 | 4:a91682e19d6b | 92 | const char* b = buf; |
uci1 | 4:a91682e19d6b | 93 | SnHeaderFrame::ReadFrom(b, msgCode, msgLen); |
uci1 | 4:a91682e19d6b | 94 | if (msgCode==SnHeaderFrame::kHandshakeCode) { |
uci1 | 4:a91682e19d6b | 95 | return SnCommWin::kOkWithMsg; |
uci1 | 4:a91682e19d6b | 96 | } else { |
uci1 | 4:a91682e19d6b | 97 | // TODO: somehow handle unexpected message? |
uci1 | 4:a91682e19d6b | 98 | return SnCommWin::kUnexpectedRec; |
uci1 | 4:a91682e19d6b | 99 | } |
uci1 | 4:a91682e19d6b | 100 | } |
uci1 | 4:a91682e19d6b | 101 | return SnCommWin::kOkNoMsg; |
uci1 | 4:a91682e19d6b | 102 | } |
uci1 | 3:24c5f0f50bf1 | 103 | */ |