Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfarTCP.cpp@12:d472f9811262, 2012-08-18 (annotated)
- Committer:
- uci1
- Date:
- Sat Aug 18 05:00:32 2012 +0000
- Revision:
- 12:d472f9811262
- Parent:
- 10:3c93db1cfb12
- Child:
- 13:7a1fb885a8e4
Fixed bug in throttling timers. Now evt stores dt of triggers but throttle works on dt of events. AfarTCP communication possibly done. Debug output put behind precompiler macros.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 7:079617408fec | 1 | |
uci1 | 3:24c5f0f50bf1 | 2 | #include "SnCommAfarTCP.h" |
uci1 | 3:24c5f0f50bf1 | 3 | |
uci1 | 6:6f002d202f59 | 4 | #include "EthernetInterface.h" |
uci1 | 6:6f002d202f59 | 5 | #include "TCPSocketConnection.h" |
uci1 | 12:d472f9811262 | 6 | |
uci1 | 12:d472f9811262 | 7 | #include "Watchdog.h" |
uci1 | 12:d472f9811262 | 8 | |
uci1 | 5:9cea89700c66 | 9 | #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) |
uci1 | 5:9cea89700c66 | 10 | |
uci1 | 8:95a325df1f6b | 11 | SnCommAfarTCP::SnCommAfarTCP(const SnConfigFrame& conf) : |
uci1 | 8:95a325df1f6b | 12 | fUseB64(false), fRserv(conf.GetRemoteServer()), |
uci1 | 8:95a325df1f6b | 13 | fRport(conf.GetRemotePort()), |
uci1 | 8:95a325df1f6b | 14 | fMyIp(conf.GetMbedIP()), fMyMask(conf.GetMbedMask()), |
uci1 | 8:95a325df1f6b | 15 | fMyGate(conf.GetMbedGate()), |
uci1 | 3:24c5f0f50bf1 | 16 | fEth(new EthernetInterface), fSock(new TCPSocketConnection) { |
uci1 | 3:24c5f0f50bf1 | 17 | |
uci1 | 8:95a325df1f6b | 18 | fEth->init(fMyIp.c_str(),fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 3:24c5f0f50bf1 | 19 | } |
uci1 | 3:24c5f0f50bf1 | 20 | |
uci1 | 3:24c5f0f50bf1 | 21 | SnCommAfarTCP::~SnCommAfarTCP() { |
uci1 | 3:24c5f0f50bf1 | 22 | delete fEth; |
uci1 | 3:24c5f0f50bf1 | 23 | delete fSock; |
uci1 | 3:24c5f0f50bf1 | 24 | } |
uci1 | 3:24c5f0f50bf1 | 25 | |
uci1 | 8:95a325df1f6b | 26 | void SnCommAfarTCP::Set(const char* remote, const uint16_t rport, |
uci1 | 8:95a325df1f6b | 27 | const char* myip, const char* mask, |
uci1 | 8:95a325df1f6b | 28 | const char* gate, const bool useb64) { |
uci1 | 8:95a325df1f6b | 29 | fUseB64 = useb64; |
uci1 | 8:95a325df1f6b | 30 | fRserv = remote; |
uci1 | 8:95a325df1f6b | 31 | fRport = rport; |
uci1 | 8:95a325df1f6b | 32 | fMyIp = myip; |
uci1 | 8:95a325df1f6b | 33 | fMyMask = mask; |
uci1 | 8:95a325df1f6b | 34 | fMyGate = gate; |
uci1 | 12:d472f9811262 | 35 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 36 | printf("closing socket\r\n"); |
uci1 | 12:d472f9811262 | 37 | #endif |
uci1 | 8:95a325df1f6b | 38 | fSock->close(); |
uci1 | 8:95a325df1f6b | 39 | //delete fSock; |
uci1 | 8:95a325df1f6b | 40 | //fSock = new TCPSocketConnection; |
uci1 | 12:d472f9811262 | 41 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 42 | printf("disconnect eth\r\n"); |
uci1 | 12:d472f9811262 | 43 | #endif |
uci1 | 8:95a325df1f6b | 44 | fEth->disconnect(); |
uci1 | 8:95a325df1f6b | 45 | //delete fEth; |
uci1 | 8:95a325df1f6b | 46 | //fEth = new EthernetInterface; |
uci1 | 12:d472f9811262 | 47 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 48 | printf("init %s, %s, %s\r\n",fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 12:d472f9811262 | 49 | #endif |
uci1 | 8:95a325df1f6b | 50 | fEth->init(fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 12:d472f9811262 | 51 | |
uci1 | 12:d472f9811262 | 52 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 53 | printf("Set done\r\n"); |
uci1 | 12:d472f9811262 | 54 | #endif |
uci1 | 8:95a325df1f6b | 55 | } |
uci1 | 8:95a325df1f6b | 56 | |
uci1 | 6:6f002d202f59 | 57 | int SnCommAfarTCP::DoIO(char* const data, |
uci1 | 5:9cea89700c66 | 58 | const uint32_t length, |
uci1 | 5:9cea89700c66 | 59 | const uint32_t timeout_clock, |
uci1 | 5:9cea89700c66 | 60 | TCPSendRecv fcn) { |
uci1 | 5:9cea89700c66 | 61 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 3:24c5f0f50bf1 | 62 | int res=0; |
uci1 | 3:24c5f0f50bf1 | 63 | uint32_t b=0; |
uci1 | 12:d472f9811262 | 64 | while ( (length>b) ) { |
uci1 | 12:d472f9811262 | 65 | if (IsTimedOut(timeout_clock)) { |
uci1 | 12:d472f9811262 | 66 | break; |
uci1 | 12:d472f9811262 | 67 | } |
uci1 | 6:6f002d202f59 | 68 | res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b); |
uci1 | 4:a91682e19d6b | 69 | switch (res) { |
uci1 | 4:a91682e19d6b | 70 | case -1: |
uci1 | 4:a91682e19d6b | 71 | // TODO: how to check the error? |
uci1 | 4:a91682e19d6b | 72 | continue; |
uci1 | 4:a91682e19d6b | 73 | case 0: |
uci1 | 4:a91682e19d6b | 74 | return res; |
uci1 | 4:a91682e19d6b | 75 | default: |
uci1 | 4:a91682e19d6b | 76 | b += res; |
uci1 | 4:a91682e19d6b | 77 | }; |
uci1 | 12:d472f9811262 | 78 | Watchdog::kick(); // don't reset; wait until timeout |
uci1 | 3:24c5f0f50bf1 | 79 | } |
uci1 | 4:a91682e19d6b | 80 | return res; // timeout |
uci1 | 3:24c5f0f50bf1 | 81 | } |
uci1 | 3:24c5f0f50bf1 | 82 | |
uci1 | 8:95a325df1f6b | 83 | int32_t SnCommAfarTCP::ReceiveAll(char* const buf, const uint32_t mlen, |
uci1 | 8:95a325df1f6b | 84 | const uint32_t timeout_clock) { |
uci1 | 5:9cea89700c66 | 85 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 12:d472f9811262 | 86 | //return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all); |
uci1 | 12:d472f9811262 | 87 | // use regular receive; DoIO will do a receive_all but use our timeout |
uci1 | 12:d472f9811262 | 88 | return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive); |
uci1 | 5:9cea89700c66 | 89 | } |
uci1 | 5:9cea89700c66 | 90 | |
uci1 | 8:95a325df1f6b | 91 | int32_t SnCommAfarTCP::SendAll(char* const data, const uint32_t length, |
uci1 | 8:95a325df1f6b | 92 | const uint32_t timeout_clock) { |
uci1 | 5:9cea89700c66 | 93 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 12:d472f9811262 | 94 | //return DoIO(data, length, timeout_clock, &TCPSocketConnection::send_all); |
uci1 | 12:d472f9811262 | 95 | // use regular sell; DoIO will do a send_all but use our timeout |
uci1 | 12:d472f9811262 | 96 | return DoIO(data, length, timeout_clock, &TCPSocketConnection::send); |
uci1 | 5:9cea89700c66 | 97 | } |
uci1 | 5:9cea89700c66 | 98 | |
uci1 | 3:24c5f0f50bf1 | 99 | |
uci1 | 3:24c5f0f50bf1 | 100 | bool SnCommAfarTCP::Connect(const uint32_t timeout) { |
uci1 | 12:d472f9811262 | 101 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 102 | printf("SnCOmmAfarTCP::Connect\r\n"); |
uci1 | 12:d472f9811262 | 103 | #endif |
uci1 | 3:24c5f0f50bf1 | 104 | bool isConn = false; |
uci1 | 3:24c5f0f50bf1 | 105 | |
uci1 | 3:24c5f0f50bf1 | 106 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 107 | wait_ms(250); |
uci1 | 3:24c5f0f50bf1 | 108 | isConn = (fEth->connect()==0); |
uci1 | 3:24c5f0f50bf1 | 109 | } |
uci1 | 12:d472f9811262 | 110 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 111 | printf("eth isConn=%d\r\n",(int)isConn); |
uci1 | 12:d472f9811262 | 112 | #endif |
uci1 | 3:24c5f0f50bf1 | 113 | |
uci1 | 8:95a325df1f6b | 114 | isConn=false; |
uci1 | 3:24c5f0f50bf1 | 115 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 116 | wait_ms(250); |
uci1 | 6:6f002d202f59 | 117 | isConn = (fSock->connect(fRserv.c_str(), fRport)==0); |
uci1 | 3:24c5f0f50bf1 | 118 | } |
uci1 | 12:d472f9811262 | 119 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 120 | printf("sock isConn=%d\r\n",(int)isConn); |
uci1 | 12:d472f9811262 | 121 | #endif |
uci1 | 3:24c5f0f50bf1 | 122 | return isConn; |
uci1 | 3:24c5f0f50bf1 | 123 | } |
uci1 | 3:24c5f0f50bf1 | 124 | |
uci1 | 3:24c5f0f50bf1 | 125 | SnCommWin::ECommWinResult SnCommAfarTCP::OpenWindow(const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 126 | const bool sendStatus, |
uci1 | 3:24c5f0f50bf1 | 127 | const SnConfigFrame& conf, |
uci1 | 3:24c5f0f50bf1 | 128 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 129 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 130 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 131 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 132 | const float evtrate, |
uci1 | 10:3c93db1cfb12 | 133 | char* const genBuf) { |
uci1 | 12:d472f9811262 | 134 | #ifdef DEBUG |
uci1 | 8:95a325df1f6b | 135 | printf("SnCommAfarTCP::OpenWindow\r\n"); |
uci1 | 12:d472f9811262 | 136 | #endif |
uci1 | 3:24c5f0f50bf1 | 137 | const bool canCon = Connect(timeout); |
uci1 | 3:24c5f0f50bf1 | 138 | |
uci1 | 3:24c5f0f50bf1 | 139 | SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected |
uci1 | 3:24c5f0f50bf1 | 140 | : SnCommWin::kCanNotConnect; |
uci1 | 3:24c5f0f50bf1 | 141 | |
uci1 | 3:24c5f0f50bf1 | 142 | if (canCon && sendStatus) { |
uci1 | 10:3c93db1cfb12 | 143 | ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout); |
uci1 | 3:24c5f0f50bf1 | 144 | } |
uci1 | 3:24c5f0f50bf1 | 145 | |
uci1 | 3:24c5f0f50bf1 | 146 | return ret; |
uci1 | 3:24c5f0f50bf1 | 147 | } |
uci1 | 4:a91682e19d6b | 148 | |
uci1 | 12:d472f9811262 | 149 | bool SnCommAfarTCP::CloseConn() { |
uci1 | 12:d472f9811262 | 150 | return (fSock->close())==0; |
uci1 | 12:d472f9811262 | 151 | } |