Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Sep 12 04:47:22 2012 +0000
Revision:
16:744ce85aede2
Parent:
15:f2569d8e4176
Child:
18:55f1581f2ee4
SBD comm seems to be working. USB comm seems to be working (at 115200 baud). AFAR comm seems to be working. This version is set for USB communication and has zero text output.

Who changed what in which revision?

UserRevisionLine numberNew 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 16:744ce85aede2 9 //#define DEBUG
uci1 16:744ce85aede2 10
uci1 5:9cea89700c66 11 #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
uci1 5:9cea89700c66 12
uci1 8:95a325df1f6b 13 SnCommAfarTCP::SnCommAfarTCP(const SnConfigFrame& conf) :
uci1 8:95a325df1f6b 14 fUseB64(false), fRserv(conf.GetRemoteServer()),
uci1 8:95a325df1f6b 15 fRport(conf.GetRemotePort()),
uci1 8:95a325df1f6b 16 fMyIp(conf.GetMbedIP()), fMyMask(conf.GetMbedMask()),
uci1 8:95a325df1f6b 17 fMyGate(conf.GetMbedGate()),
uci1 3:24c5f0f50bf1 18 fEth(new EthernetInterface), fSock(new TCPSocketConnection) {
uci1 3:24c5f0f50bf1 19
uci1 16:744ce85aede2 20 #ifdef DEBUG
uci1 16:744ce85aede2 21 printf("init. ip=%s, mas=%s, gate=%s\r\n",
uci1 16:744ce85aede2 22 fMyIp.c_str(),fMyMask.c_str(), fMyGate.c_str());
uci1 16:744ce85aede2 23 #endif
uci1 8:95a325df1f6b 24 fEth->init(fMyIp.c_str(),fMyMask.c_str(), fMyGate.c_str());
uci1 3:24c5f0f50bf1 25 }
uci1 3:24c5f0f50bf1 26
uci1 3:24c5f0f50bf1 27 SnCommAfarTCP::~SnCommAfarTCP() {
uci1 3:24c5f0f50bf1 28 delete fEth;
uci1 3:24c5f0f50bf1 29 delete fSock;
uci1 3:24c5f0f50bf1 30 }
uci1 3:24c5f0f50bf1 31
uci1 8:95a325df1f6b 32 void SnCommAfarTCP::Set(const char* remote, const uint16_t rport,
uci1 8:95a325df1f6b 33 const char* myip, const char* mask,
uci1 8:95a325df1f6b 34 const char* gate, const bool useb64) {
uci1 8:95a325df1f6b 35 fUseB64 = useb64;
uci1 8:95a325df1f6b 36 fRserv = remote;
uci1 8:95a325df1f6b 37 fRport = rport;
uci1 8:95a325df1f6b 38 fMyIp = myip;
uci1 8:95a325df1f6b 39 fMyMask = mask;
uci1 8:95a325df1f6b 40 fMyGate = gate;
uci1 12:d472f9811262 41 #ifdef DEBUG
uci1 8:95a325df1f6b 42 printf("closing socket\r\n");
uci1 12:d472f9811262 43 #endif
uci1 8:95a325df1f6b 44 fSock->close();
uci1 8:95a325df1f6b 45 //delete fSock;
uci1 8:95a325df1f6b 46 //fSock = new TCPSocketConnection;
uci1 12:d472f9811262 47 #ifdef DEBUG
uci1 8:95a325df1f6b 48 printf("disconnect eth\r\n");
uci1 12:d472f9811262 49 #endif
uci1 8:95a325df1f6b 50 fEth->disconnect();
uci1 8:95a325df1f6b 51 //delete fEth;
uci1 8:95a325df1f6b 52 //fEth = new EthernetInterface;
uci1 12:d472f9811262 53 #ifdef DEBUG
uci1 8:95a325df1f6b 54 printf("init %s, %s, %s\r\n",fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
uci1 12:d472f9811262 55 #endif
uci1 8:95a325df1f6b 56 fEth->init(fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
uci1 12:d472f9811262 57
uci1 12:d472f9811262 58 #ifdef DEBUG
uci1 8:95a325df1f6b 59 printf("Set done\r\n");
uci1 12:d472f9811262 60 #endif
uci1 8:95a325df1f6b 61 }
uci1 8:95a325df1f6b 62
uci1 6:6f002d202f59 63 int SnCommAfarTCP::DoIO(char* const data,
uci1 5:9cea89700c66 64 const uint32_t length,
uci1 5:9cea89700c66 65 const uint32_t timeout_clock,
uci1 5:9cea89700c66 66 TCPSendRecv fcn) {
uci1 5:9cea89700c66 67 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 3:24c5f0f50bf1 68 int res=0;
uci1 3:24c5f0f50bf1 69 uint32_t b=0;
uci1 12:d472f9811262 70 while ( (length>b) ) {
uci1 12:d472f9811262 71 if (IsTimedOut(timeout_clock)) {
uci1 12:d472f9811262 72 break;
uci1 12:d472f9811262 73 }
uci1 6:6f002d202f59 74 res = CALL_MEMBER_FN(*fSock, fcn)(data+b, length-b);
uci1 4:a91682e19d6b 75 switch (res) {
uci1 4:a91682e19d6b 76 case -1:
uci1 4:a91682e19d6b 77 // TODO: how to check the error?
uci1 4:a91682e19d6b 78 continue;
uci1 4:a91682e19d6b 79 case 0:
uci1 4:a91682e19d6b 80 return res;
uci1 4:a91682e19d6b 81 default:
uci1 4:a91682e19d6b 82 b += res;
uci1 4:a91682e19d6b 83 };
uci1 12:d472f9811262 84 Watchdog::kick(); // don't reset; wait until timeout
uci1 3:24c5f0f50bf1 85 }
uci1 16:744ce85aede2 86 return b; // timeout
uci1 3:24c5f0f50bf1 87 }
uci1 3:24c5f0f50bf1 88
uci1 8:95a325df1f6b 89 int32_t SnCommAfarTCP::ReceiveAll(char* const buf, const uint32_t mlen,
uci1 8:95a325df1f6b 90 const uint32_t timeout_clock) {
uci1 5:9cea89700c66 91 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 12:d472f9811262 92 //return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all);
uci1 12:d472f9811262 93 // use regular receive; DoIO will do a receive_all but use our timeout
uci1 12:d472f9811262 94 return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive);
uci1 5:9cea89700c66 95 }
uci1 5:9cea89700c66 96
uci1 8:95a325df1f6b 97 int32_t SnCommAfarTCP::SendAll(char* const data, const uint32_t length,
uci1 8:95a325df1f6b 98 const uint32_t timeout_clock) {
uci1 5:9cea89700c66 99 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 12:d472f9811262 100 //return DoIO(data, length, timeout_clock, &TCPSocketConnection::send_all);
uci1 12:d472f9811262 101 // use regular sell; DoIO will do a send_all but use our timeout
uci1 12:d472f9811262 102 return DoIO(data, length, timeout_clock, &TCPSocketConnection::send);
uci1 5:9cea89700c66 103 }
uci1 5:9cea89700c66 104
uci1 3:24c5f0f50bf1 105
uci1 3:24c5f0f50bf1 106 bool SnCommAfarTCP::Connect(const uint32_t timeout) {
uci1 12:d472f9811262 107 #ifdef DEBUG
uci1 13:7a1fb885a8e4 108 printf("SnCommAfarTCP::Connect\r\n");
uci1 12:d472f9811262 109 #endif
uci1 3:24c5f0f50bf1 110 bool isConn = false;
uci1 3:24c5f0f50bf1 111
uci1 3:24c5f0f50bf1 112 while ( (isConn==false) && ( time(0) < timeout) ) {
uci1 3:24c5f0f50bf1 113 wait_ms(250);
uci1 3:24c5f0f50bf1 114 isConn = (fEth->connect()==0);
uci1 3:24c5f0f50bf1 115 }
uci1 12:d472f9811262 116 #ifdef DEBUG
uci1 8:95a325df1f6b 117 printf("eth isConn=%d\r\n",(int)isConn);
uci1 12:d472f9811262 118 #endif
uci1 3:24c5f0f50bf1 119
uci1 8:95a325df1f6b 120 isConn=false;
uci1 3:24c5f0f50bf1 121 while ( (isConn==false) && ( time(0) < timeout) ) {
uci1 3:24c5f0f50bf1 122 wait_ms(250);
uci1 6:6f002d202f59 123 isConn = (fSock->connect(fRserv.c_str(), fRport)==0);
uci1 3:24c5f0f50bf1 124 }
uci1 12:d472f9811262 125 #ifdef DEBUG
uci1 8:95a325df1f6b 126 printf("sock isConn=%d\r\n",(int)isConn);
uci1 12:d472f9811262 127 #endif
uci1 3:24c5f0f50bf1 128 return isConn;
uci1 3:24c5f0f50bf1 129 }
uci1 3:24c5f0f50bf1 130
uci1 3:24c5f0f50bf1 131 SnCommWin::ECommWinResult SnCommAfarTCP::OpenWindow(const uint32_t timeout,
uci1 3:24c5f0f50bf1 132 const bool sendStatus,
uci1 3:24c5f0f50bf1 133 const SnConfigFrame& conf,
uci1 3:24c5f0f50bf1 134 const SnEventFrame& evt,
uci1 8:95a325df1f6b 135 const SnPowerFrame& pow,
uci1 10:3c93db1cfb12 136 const uint16_t seq,
uci1 10:3c93db1cfb12 137 const float thmrate,
uci1 10:3c93db1cfb12 138 const float evtrate,
uci1 10:3c93db1cfb12 139 char* const genBuf) {
uci1 12:d472f9811262 140 #ifdef DEBUG
uci1 8:95a325df1f6b 141 printf("SnCommAfarTCP::OpenWindow\r\n");
uci1 12:d472f9811262 142 #endif
uci1 3:24c5f0f50bf1 143 const bool canCon = Connect(timeout);
uci1 3:24c5f0f50bf1 144
uci1 3:24c5f0f50bf1 145 SnCommWin::ECommWinResult ret = canCon ? SnCommWin::kConnected
uci1 3:24c5f0f50bf1 146 : SnCommWin::kCanNotConnect;
uci1 3:24c5f0f50bf1 147
uci1 3:24c5f0f50bf1 148 if (canCon && sendStatus) {
uci1 10:3c93db1cfb12 149 ret = SendStatus(conf, evt, pow, seq, thmrate, evtrate, genBuf, timeout);
uci1 3:24c5f0f50bf1 150 }
uci1 3:24c5f0f50bf1 151
uci1 3:24c5f0f50bf1 152 return ret;
uci1 3:24c5f0f50bf1 153 }
uci1 4:a91682e19d6b 154
uci1 15:f2569d8e4176 155 bool SnCommAfarTCP::CloseConn(const uint32_t) {
uci1 12:d472f9811262 156 return (fSock->close())==0;
uci1 12:d472f9811262 157 }