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