Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfarTCP.cpp@8:95a325df1f6b, 2012-08-08 (annotated)
- Committer:
- uci1
- Date:
- Wed Aug 08 23:27:37 2012 +0000
- Revision:
- 8:95a325df1f6b
- Parent:
- 7:079617408fec
- Child:
- 10:3c93db1cfb12
Check power for low pow running. Average power over 500 readings. Use RTOS timers instead of tickers. Allow data taking of events, power readings or both, even if cards are off. Bug in EthernetInterface prevents IPs being reset. Lots of debug output.
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 | 8:95a325df1f6b | 6 | /* |
uci1 | 6:6f002d202f59 | 7 | #include "SnSDUtils.h" |
uci1 | 6:6f002d202f59 | 8 | #include "SnHeaderFrame.h" |
uci1 | 6:6f002d202f59 | 9 | #include "SnConfigFrame.h" |
uci1 | 6:6f002d202f59 | 10 | #include "SnEventFrame.h" |
uci1 | 6:6f002d202f59 | 11 | #include "SnStatusFrame.h" |
uci1 | 8:95a325df1f6b | 12 | */ |
uci1 | 5:9cea89700c66 | 13 | #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) |
uci1 | 5:9cea89700c66 | 14 | |
uci1 | 8:95a325df1f6b | 15 | SnCommAfarTCP::SnCommAfarTCP(const SnConfigFrame& conf) : |
uci1 | 8:95a325df1f6b | 16 | fUseB64(false), fRserv(conf.GetRemoteServer()), |
uci1 | 8:95a325df1f6b | 17 | fRport(conf.GetRemotePort()), |
uci1 | 8:95a325df1f6b | 18 | fMyIp(conf.GetMbedIP()), fMyMask(conf.GetMbedMask()), |
uci1 | 8:95a325df1f6b | 19 | fMyGate(conf.GetMbedGate()), |
uci1 | 3:24c5f0f50bf1 | 20 | fEth(new EthernetInterface), fSock(new TCPSocketConnection) { |
uci1 | 3:24c5f0f50bf1 | 21 | |
uci1 | 8:95a325df1f6b | 22 | fEth->init(fMyIp.c_str(),fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 3:24c5f0f50bf1 | 23 | } |
uci1 | 3:24c5f0f50bf1 | 24 | |
uci1 | 3:24c5f0f50bf1 | 25 | SnCommAfarTCP::~SnCommAfarTCP() { |
uci1 | 3:24c5f0f50bf1 | 26 | delete fEth; |
uci1 | 3:24c5f0f50bf1 | 27 | delete fSock; |
uci1 | 3:24c5f0f50bf1 | 28 | } |
uci1 | 3:24c5f0f50bf1 | 29 | |
uci1 | 8:95a325df1f6b | 30 | void SnCommAfarTCP::Set(const char* remote, const uint16_t rport, |
uci1 | 8:95a325df1f6b | 31 | const char* myip, const char* mask, |
uci1 | 8:95a325df1f6b | 32 | const char* gate, const bool useb64) { |
uci1 | 8:95a325df1f6b | 33 | fUseB64 = useb64; |
uci1 | 8:95a325df1f6b | 34 | fRserv = remote; |
uci1 | 8:95a325df1f6b | 35 | fRport = rport; |
uci1 | 8:95a325df1f6b | 36 | fMyIp = myip; |
uci1 | 8:95a325df1f6b | 37 | fMyMask = mask; |
uci1 | 8:95a325df1f6b | 38 | fMyGate = gate; |
uci1 | 8:95a325df1f6b | 39 | printf("closing socket\r\n"); |
uci1 | 8:95a325df1f6b | 40 | fSock->close(); |
uci1 | 8:95a325df1f6b | 41 | //delete fSock; |
uci1 | 8:95a325df1f6b | 42 | //fSock = new TCPSocketConnection; |
uci1 | 8:95a325df1f6b | 43 | printf("disconnect eth\r\n"); |
uci1 | 8:95a325df1f6b | 44 | fEth->disconnect(); |
uci1 | 8:95a325df1f6b | 45 | //delete fEth; |
uci1 | 8:95a325df1f6b | 46 | //fEth = new EthernetInterface; |
uci1 | 8:95a325df1f6b | 47 | printf("init %s, %s, %s\r\n",fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 8:95a325df1f6b | 48 | fEth->init(fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str()); |
uci1 | 8:95a325df1f6b | 49 | printf("Set done\r\n"); |
uci1 | 8:95a325df1f6b | 50 | } |
uci1 | 8:95a325df1f6b | 51 | |
uci1 | 6:6f002d202f59 | 52 | int SnCommAfarTCP::DoIO(char* const data, |
uci1 | 5:9cea89700c66 | 53 | const uint32_t length, |
uci1 | 5:9cea89700c66 | 54 | const uint32_t timeout_clock, |
uci1 | 5:9cea89700c66 | 55 | TCPSendRecv fcn) { |
uci1 | 5:9cea89700c66 | 56 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 3:24c5f0f50bf1 | 57 | int res=0; |
uci1 | 3:24c5f0f50bf1 | 58 | uint32_t b=0; |
uci1 | 3:24c5f0f50bf1 | 59 | while ( (length>b) && (time(0)<timeout_clock) ) { |
uci1 | 6:6f002d202f59 | 60 | res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b); |
uci1 | 4:a91682e19d6b | 61 | switch (res) { |
uci1 | 4:a91682e19d6b | 62 | case -1: |
uci1 | 4:a91682e19d6b | 63 | // TODO: how to check the error? |
uci1 | 4:a91682e19d6b | 64 | continue; |
uci1 | 4:a91682e19d6b | 65 | case 0: |
uci1 | 4:a91682e19d6b | 66 | return res; |
uci1 | 4:a91682e19d6b | 67 | default: |
uci1 | 4:a91682e19d6b | 68 | b += res; |
uci1 | 4:a91682e19d6b | 69 | }; |
uci1 | 3:24c5f0f50bf1 | 70 | } |
uci1 | 4:a91682e19d6b | 71 | return res; // timeout |
uci1 | 3:24c5f0f50bf1 | 72 | } |
uci1 | 3:24c5f0f50bf1 | 73 | |
uci1 | 8:95a325df1f6b | 74 | int32_t SnCommAfarTCP::ReceiveAll(char* const buf, const uint32_t mlen, |
uci1 | 8:95a325df1f6b | 75 | const uint32_t timeout_clock) { |
uci1 | 5:9cea89700c66 | 76 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 6:6f002d202f59 | 77 | return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all); |
uci1 | 5:9cea89700c66 | 78 | } |
uci1 | 5:9cea89700c66 | 79 | |
uci1 | 8:95a325df1f6b | 80 | int32_t SnCommAfarTCP::SendAll(char* const data, const uint32_t length, |
uci1 | 8:95a325df1f6b | 81 | const uint32_t timeout_clock) { |
uci1 | 5:9cea89700c66 | 82 | // TODO: if B64, must return number of bytes of raw (non encoded) message |
uci1 | 6:6f002d202f59 | 83 | return DoIO(data, length, timeout_clock, &TCPSocketConnection::send_all); |
uci1 | 5:9cea89700c66 | 84 | } |
uci1 | 5:9cea89700c66 | 85 | |
uci1 | 3:24c5f0f50bf1 | 86 | |
uci1 | 3:24c5f0f50bf1 | 87 | bool SnCommAfarTCP::Connect(const uint32_t timeout) { |
uci1 | 8:95a325df1f6b | 88 | printf("SnCOmmAfarTCP::Connect\r\n"); |
uci1 | 3:24c5f0f50bf1 | 89 | bool isConn = false; |
uci1 | 3:24c5f0f50bf1 | 90 | |
uci1 | 3:24c5f0f50bf1 | 91 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 92 | wait_ms(250); |
uci1 | 3:24c5f0f50bf1 | 93 | isConn = (fEth->connect()==0); |
uci1 | 3:24c5f0f50bf1 | 94 | } |
uci1 | 8:95a325df1f6b | 95 | printf("eth isConn=%d\r\n",(int)isConn); |
uci1 | 3:24c5f0f50bf1 | 96 | |
uci1 | 8:95a325df1f6b | 97 | isConn=false; |
uci1 | 3:24c5f0f50bf1 | 98 | while ( (isConn==false) && ( time(0) < timeout) ) { |
uci1 | 3:24c5f0f50bf1 | 99 | wait_ms(250); |
uci1 | 6:6f002d202f59 | 100 | isConn = (fSock->connect(fRserv.c_str(), fRport)==0); |
uci1 | 3:24c5f0f50bf1 | 101 | } |
uci1 | 8:95a325df1f6b | 102 | printf("sock isConn=%d\r\n",(int)isConn); |
uci1 | 3:24c5f0f50bf1 | 103 | return isConn; |
uci1 | 3:24c5f0f50bf1 | 104 | } |
uci1 | 3:24c5f0f50bf1 | 105 | |
uci1 | 3:24c5f0f50bf1 | 106 | SnCommWin::ECommWinResult SnCommAfarTCP::OpenWindow(const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 107 | const bool sendStatus, |
uci1 | 3:24c5f0f50bf1 | 108 | const SnConfigFrame& conf, |
uci1 | 3:24c5f0f50bf1 | 109 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 110 | const SnPowerFrame& pow, |
uci1 | 3:24c5f0f50bf1 | 111 | char* const genBuf) { |
uci1 | 8:95a325df1f6b | 112 | printf("SnCommAfarTCP::OpenWindow\r\n"); |
uci1 | 3:24c5f0f50bf1 | 113 | const bool canCon = Connect(timeout); |
uci1 | 3:24c5f0f50bf1 | 114 | |
uci1 | 3:24c5f0f50bf1 | 115 | SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected |
uci1 | 3:24c5f0f50bf1 | 116 | : SnCommWin::kCanNotConnect; |
uci1 | 3:24c5f0f50bf1 | 117 | |
uci1 | 3:24c5f0f50bf1 | 118 | if (canCon && sendStatus) { |
uci1 | 8:95a325df1f6b | 119 | ret = SendStatus(conf, evt, pow, genBuf, timeout); |
uci1 | 3:24c5f0f50bf1 | 120 | } |
uci1 | 3:24c5f0f50bf1 | 121 | |
uci1 | 3:24c5f0f50bf1 | 122 | return ret; |
uci1 | 3:24c5f0f50bf1 | 123 | } |
uci1 | 4:a91682e19d6b | 124 |