Arianna station communication peripherals.

Dependents:   AutonomousDAQ AutonomousDAQ

Committer:
uci1
Date:
Wed Aug 08 21:00:41 2018 +0000
Revision:
10:29301aaa8c33
Parent:
4:8328c2972290
Fixed EOL termination for SBD comms, replacing \r\n with \r (or something like that)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:26c9189e5924 1 #include "SnCommAfarNetIf.h"
uci1 0:26c9189e5924 2
uci1 0:26c9189e5924 3 #ifdef ENABLE_AFAR
uci1 0:26c9189e5924 4
uci1 0:26c9189e5924 5
uci1 0:26c9189e5924 6 #include "lwip/inet.h"
uci1 0:26c9189e5924 7
uci1 0:26c9189e5924 8 //#define DEBUG
uci1 0:26c9189e5924 9
uci1 0:26c9189e5924 10 SnCommAfarNetIf::SnCommAfarNetIf(const char* rserv,
uci1 0:26c9189e5924 11 const uint16_t rport, const char* ip,
uci1 0:26c9189e5924 12 const char* mask, const char* gate) :
uci1 0:26c9189e5924 13 fUseB64(false),
uci1 0:26c9189e5924 14 fRserv(rserv), fRport(rport),
uci1 0:26c9189e5924 15 fMyIp(ip), fMyMask(mask), fMyGate(gate),
uci1 0:26c9189e5924 16 fEth(0), fSock(0),
uci1 0:26c9189e5924 17 fEthConnected(false), fSockReading(false),
uci1 0:26c9189e5924 18 fEthWriteable(false), fConnAborted(false), fEthSetup(false),
uci1 0:26c9189e5924 19 fRxRd(fRxBuf), fRxWt(fRxBuf), fRxEnd(fRxBuf+RX_BUF_LEN),
uci1 0:26c9189e5924 20 fRxBufWriteable(true)
uci1 0:26c9189e5924 21 {
uci1 0:26c9189e5924 22
uci1 0:26c9189e5924 23 #ifdef DEBUG
uci1 0:26c9189e5924 24 printf("SnCommAfarNetIf ctor ip=%s, mas=%s, gate=%s\r\n",
uci1 0:26c9189e5924 25 fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
uci1 0:26c9189e5924 26 printf("rserv=%s, rport=%hu\r\n", fRserv.c_str(), fRport);
uci1 0:26c9189e5924 27 printf("calling NewEthAndSocket\r\n");
uci1 0:26c9189e5924 28 #endif
uci1 0:26c9189e5924 29
uci1 0:26c9189e5924 30 NewEthAndSocket();
uci1 0:26c9189e5924 31
uci1 0:26c9189e5924 32 #ifdef DEBUG
uci1 0:26c9189e5924 33 printf("ctor done\r\n");
uci1 0:26c9189e5924 34 #endif
uci1 0:26c9189e5924 35 }
uci1 0:26c9189e5924 36
uci1 0:26c9189e5924 37 SnCommAfarNetIf::~SnCommAfarNetIf() {
uci1 0:26c9189e5924 38 delete fEth;
uci1 0:26c9189e5924 39 delete fSock;
uci1 0:26c9189e5924 40 }
uci1 0:26c9189e5924 41
uci1 0:26c9189e5924 42 void SnCommAfarNetIf::Set(const char* remote, const uint16_t rport) {
uci1 0:26c9189e5924 43 fRserv = remote;
uci1 0:26c9189e5924 44 fRport = rport;
uci1 4:8328c2972290 45 /*
uci1 0:26c9189e5924 46 #ifdef DEBUG
uci1 0:26c9189e5924 47 printf("closing socket\r\n");
uci1 0:26c9189e5924 48 #endif
uci1 0:26c9189e5924 49 fSock->close();
uci1 4:8328c2972290 50 */
uci1 0:26c9189e5924 51 #ifdef DEBUG
uci1 0:26c9189e5924 52 printf("disconnect eth\r\n");
uci1 0:26c9189e5924 53 #endif
uci1 0:26c9189e5924 54 #ifdef DEBUG
uci1 0:26c9189e5924 55 printf("init %s, %s, %s\r\n",fMyIp.c_str(), fMyMask.c_str(), fMyGate.c_str());
uci1 0:26c9189e5924 56 #endif
uci1 0:26c9189e5924 57 NewEthAndSocket();
uci1 0:26c9189e5924 58 #ifdef DEBUG
uci1 0:26c9189e5924 59 printf("Set done\r\n");
uci1 0:26c9189e5924 60 #endif
uci1 0:26c9189e5924 61 }
uci1 0:26c9189e5924 62
uci1 0:26c9189e5924 63 void SnCommAfarNetIf::Set(const char* remote, const uint16_t rport,
uci1 0:26c9189e5924 64 const char* myip, const char* mask,
uci1 0:26c9189e5924 65 const char* gate, const bool useb64) {
uci1 0:26c9189e5924 66 fUseB64 = useb64;
uci1 0:26c9189e5924 67 fMyIp = myip;
uci1 0:26c9189e5924 68 fMyMask = mask;
uci1 0:26c9189e5924 69 fMyGate = gate;
uci1 0:26c9189e5924 70 Set(remote, rport);
uci1 0:26c9189e5924 71 }
uci1 0:26c9189e5924 72
uci1 0:26c9189e5924 73 void SnCommAfarNetIf::NewEthAndSocket() {
uci1 0:26c9189e5924 74 #ifdef DEBUG
uci1 0:26c9189e5924 75 printf("deleting fEth(%p)\r\n",(void*)fEth);
uci1 0:26c9189e5924 76 #endif
uci1 0:26c9189e5924 77 delete fEth;
uci1 0:26c9189e5924 78 fEthSetup = false;
uci1 0:26c9189e5924 79 #ifdef DEBUG
uci1 0:26c9189e5924 80 printf("making ip_addr_t's\r\n");
uci1 0:26c9189e5924 81 #endif
uci1 0:26c9189e5924 82 ip_addr_t ip_n, mask_n, gateway_n, dns_n;
uci1 0:26c9189e5924 83 inet_aton(fMyIp.c_str(), &ip_n);
uci1 0:26c9189e5924 84 inet_aton(fMyMask.c_str(), &mask_n);
uci1 0:26c9189e5924 85 inet_aton(fMyGate.c_str(), &gateway_n);
uci1 0:26c9189e5924 86 //inet_aton("0.0.0.0", &dns_n);
uci1 0:26c9189e5924 87 //inet_aton("128.200.1.201", &dns_n);
uci1 0:26c9189e5924 88 inet_aton("157.132.107.58", &dns_n);
uci1 0:26c9189e5924 89 #ifdef DEBUG
uci1 0:26c9189e5924 90 printf("new ethernet\r\n");
uci1 0:26c9189e5924 91 #endif
uci1 0:26c9189e5924 92 fEth = new EthernetNetIf(
uci1 0:26c9189e5924 93 IpAddr(&ip_n),
uci1 0:26c9189e5924 94 IpAddr(&mask_n),
uci1 0:26c9189e5924 95 IpAddr(&gateway_n),
uci1 0:26c9189e5924 96 IpAddr(&dns_n)
uci1 0:26c9189e5924 97 );
uci1 0:26c9189e5924 98
uci1 0:26c9189e5924 99 NewSocket();
uci1 0:26c9189e5924 100 }
uci1 0:26c9189e5924 101
uci1 0:26c9189e5924 102 void SnCommAfarNetIf::NewSocket() {
uci1 0:26c9189e5924 103 #ifdef DEBUG
uci1 0:26c9189e5924 104 printf("deleting fSock(%p)\r\n",(void*)fSock);
uci1 0:26c9189e5924 105 #endif
uci1 0:26c9189e5924 106 delete fSock;
uci1 0:26c9189e5924 107
uci1 0:26c9189e5924 108 #ifdef DEBUG
uci1 0:26c9189e5924 109 printf("new socket\r\n");
uci1 0:26c9189e5924 110 #endif
uci1 0:26c9189e5924 111 fSock = new TCPSocket;
uci1 0:26c9189e5924 112
uci1 0:26c9189e5924 113 #ifdef DEBUG
uci1 0:26c9189e5924 114 printf("clearing rx buffer\r\n");
uci1 0:26c9189e5924 115 #endif
uci1 0:26c9189e5924 116 memset(fRxBuf, 0, RX_BUF_LEN*sizeof(char));
uci1 0:26c9189e5924 117
uci1 4:8328c2972290 118
uci1 4:8328c2972290 119 #ifdef DEBUG
uci1 4:8328c2972290 120 printf("setOnEvent\r\n");
uci1 4:8328c2972290 121 #endif
uci1 4:8328c2972290 122 // catch events
uci1 4:8328c2972290 123 fSock->setOnEvent(this, &SnCommAfarNetIf::onTCPSocketEvent);
uci1 4:8328c2972290 124
uci1 0:26c9189e5924 125 fEthConnected = false;
uci1 0:26c9189e5924 126 fEthWriteable = false;
uci1 0:26c9189e5924 127 }
uci1 0:26c9189e5924 128
uci1 0:26c9189e5924 129 int32_t SnCommAfarNetIf::PullFromBuffTo(char* const buf, const uint32_t len) {
uci1 0:26c9189e5924 130 // pull 'len' bytes out of the Rx buffer and copy them to buf
uci1 0:26c9189e5924 131 // no check on the length of 'buf' is performed!
uci1 0:26c9189e5924 132 // no check on 'len' is performed!
uci1 0:26c9189e5924 133
uci1 0:26c9189e5924 134 memcpy(buf, fRxRd, len);
uci1 0:26c9189e5924 135 fRxRd += len;
uci1 0:26c9189e5924 136 if (fRxWt > fRxRd) {
uci1 0:26c9189e5924 137 // there's still data in the buffer
uci1 0:26c9189e5924 138 fRxBufWriteable = false;
uci1 0:26c9189e5924 139 const int32_t nb = fRxWt - fRxRd;
uci1 0:26c9189e5924 140 memmove(fRxBuf, fRxRd, nb); // move it to the beginning (overlap ok in memmove)
uci1 0:26c9189e5924 141 fRxRd = fRxBuf;
uci1 0:26c9189e5924 142 fRxWt -= nb;
uci1 0:26c9189e5924 143 fRxBufWriteable = true;
uci1 0:26c9189e5924 144 } else {
uci1 0:26c9189e5924 145 // nothing in buffer
uci1 0:26c9189e5924 146 fRxBufWriteable = false;
uci1 0:26c9189e5924 147 fRxRd = fRxWt = fRxBuf;
uci1 0:26c9189e5924 148 fRxBufWriteable = true;
uci1 0:26c9189e5924 149 }
uci1 0:26c9189e5924 150 return len;
uci1 0:26c9189e5924 151 }
uci1 0:26c9189e5924 152
uci1 0:26c9189e5924 153 int32_t SnCommAfarNetIf::ReceiveAll(char* const buf, const uint32_t mlen,
uci1 0:26c9189e5924 154 const uint32_t timeout_clock) {
uci1 0:26c9189e5924 155 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 0:26c9189e5924 156 //return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive_all);
uci1 0:26c9189e5924 157 // use regular recv; DoIO will do a receive_all but use our timeout
uci1 0:26c9189e5924 158
uci1 0:26c9189e5924 159 Net::poll();
uci1 0:26c9189e5924 160 // first get out of buffer
uci1 0:26c9189e5924 161 int32_t br = 0;
uci1 0:26c9189e5924 162 while ( (br<mlen) && (fRxRd<fRxWt) ) {
uci1 0:26c9189e5924 163 int32_t bl = fRxWt - fRxRd;
uci1 0:26c9189e5924 164 if (mlen<bl) {
uci1 0:26c9189e5924 165 bl = mlen;
uci1 0:26c9189e5924 166 }
uci1 0:26c9189e5924 167 br += PullFromBuffTo(buf+br, bl);
uci1 0:26c9189e5924 168 }
uci1 0:26c9189e5924 169 // get the rest from the socket
uci1 0:26c9189e5924 170 if (br<mlen) {
uci1 0:26c9189e5924 171 fSockReading = true;
uci1 0:26c9189e5924 172 br += DoIO(buf, mlen, timeout_clock, &TCPSocket::recv, fRxBufWriteable);
uci1 0:26c9189e5924 173 fSockReading = false;
uci1 0:26c9189e5924 174 }
uci1 0:26c9189e5924 175 return br;
uci1 0:26c9189e5924 176 }
uci1 0:26c9189e5924 177
uci1 0:26c9189e5924 178 int32_t SnCommAfarNetIf::SendAll(const char* const data, const uint32_t length,
uci1 0:26c9189e5924 179 const uint32_t timeout_clock) {
uci1 0:26c9189e5924 180 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 0:26c9189e5924 181 //return DoIO(data, length, timeout_clock, &TCPSocketConnection::send_all);
uci1 0:26c9189e5924 182 // use regular send; DoIO will do a send_all but use our timeout
uci1 0:26c9189e5924 183
uci1 0:26c9189e5924 184 Net::poll();
uci1 0:26c9189e5924 185 const char* const buf = data;
uci1 0:26c9189e5924 186 #ifdef DEBUG
uci1 0:26c9189e5924 187 const int32_t br =
uci1 0:26c9189e5924 188 #else
uci1 0:26c9189e5924 189 return
uci1 0:26c9189e5924 190 #endif
uci1 0:26c9189e5924 191 DoIO(buf, length, timeout_clock, &TCPSocket::send, fEthWriteable);
uci1 0:26c9189e5924 192 #ifdef DEBUG
uci1 0:26c9189e5924 193 printf("SendAll: DoIO returned %d bytes\r\n",br);
uci1 0:26c9189e5924 194 return br;
uci1 0:26c9189e5924 195 #endif
uci1 0:26c9189e5924 196 }
uci1 0:26c9189e5924 197
uci1 0:26c9189e5924 198
uci1 0:26c9189e5924 199 bool SnCommAfarNetIf::Connect(const uint32_t timeout) {
uci1 0:26c9189e5924 200 #ifdef DEBUG
uci1 0:26c9189e5924 201 printf("SnCommAfarNetIf::Connect : setup\r\n");
uci1 4:8328c2972290 202 printf("fEthConnected=%s\r\n",fEthConnected?"true":"false");
uci1 0:26c9189e5924 203 #endif
uci1 0:26c9189e5924 204 if (fEthConnected==false) {
uci1 0:26c9189e5924 205
uci1 4:8328c2972290 206 #ifdef DEBUG
uci1 4:8328c2972290 207 printf("fEthSetup=%s\r\n",fEthSetup?"true":"false");
uci1 4:8328c2972290 208 #endif
uci1 0:26c9189e5924 209 if (fEthSetup==false) {
uci1 0:26c9189e5924 210 EthernetErr ethErr = fEth->setup();
uci1 0:26c9189e5924 211
uci1 0:26c9189e5924 212 while ( (ethErr!=ETH_OK) && (IsTimedOut(timeout)==false) ) {
uci1 0:26c9189e5924 213 wait_ms(250);
uci1 0:26c9189e5924 214 ethErr = fEth->setup();
uci1 0:26c9189e5924 215 }
uci1 0:26c9189e5924 216
uci1 0:26c9189e5924 217 if (ethErr==ETH_OK) {
uci1 0:26c9189e5924 218 fEthSetup = true;
uci1 0:26c9189e5924 219 }
uci1 0:26c9189e5924 220
uci1 4:8328c2972290 221 #ifdef DEBUG
uci1 4:8328c2972290 222 printf("fEthSetup=%s\r\n",fEthSetup?"true":"false");
uci1 4:8328c2972290 223 #endif
uci1 4:8328c2972290 224
uci1 4:8328c2972290 225 // why were these waits here?? -- test in comms lab at McMurdo!
uci1 4:8328c2972290 226 //wait(10);
uci1 0:26c9189e5924 227 //wait(40); // for use with switch in comms @ McMurdo
uci1 0:26c9189e5924 228 }
uci1 0:26c9189e5924 229
uci1 0:26c9189e5924 230 if (fEthSetup) {
uci1 0:26c9189e5924 231
uci1 0:26c9189e5924 232 #ifdef DEBUG
uci1 0:26c9189e5924 233 printf("SnCommAfarNetIf::Connect : bind\r\n");
uci1 0:26c9189e5924 234 #endif
uci1 0:26c9189e5924 235
uci1 0:26c9189e5924 236 ip_addr_t rserv_n;
uci1 0:26c9189e5924 237 inet_aton(fRserv.c_str(), &rserv_n);
uci1 0:26c9189e5924 238 Host server( IpAddr(&rserv_n), fRport );
uci1 0:26c9189e5924 239 //Host server( IpAddr(128,195,204,151), 6655 );
uci1 0:26c9189e5924 240
uci1 4:8328c2972290 241 #ifdef DEBUG
uci1 4:8328c2972290 242 printf("SnCommAfarNetIf::Connect : fRserv=%s, fRport=%hu\r\n",
uci1 4:8328c2972290 243 fRserv.c_str(), fRport);
uci1 4:8328c2972290 244 #endif
uci1 0:26c9189e5924 245 SockConnect(server, timeout);
uci1 4:8328c2972290 246 /*
uci1 0:26c9189e5924 247 // catch events
uci1 0:26c9189e5924 248 fSock->setOnEvent(this, &SnCommAfarNetIf::onTCPSocketEvent);
uci1 4:8328c2972290 249 */
uci1 0:26c9189e5924 250 #ifdef DEBUG
uci1 0:26c9189e5924 251 printf("SnCommAfarNetIf::Connect : tcp connect\r\n");
uci1 0:26c9189e5924 252 #endif
uci1 0:26c9189e5924 253 //fEthConnected = false;
uci1 0:26c9189e5924 254 while ( (fEthConnected==false) && (IsTimedOut(timeout)==false) ) {
uci1 0:26c9189e5924 255 Net::poll();
uci1 0:26c9189e5924 256 if (fConnAborted) {
uci1 4:8328c2972290 257 #ifdef DEBUG
uci1 4:8328c2972290 258 printf("SnCommAfarNetIf::Connect : conn aborted. new socket, sockconnect...\r\n");
uci1 4:8328c2972290 259 #endif
uci1 0:26c9189e5924 260 fConnAborted = false;
uci1 0:26c9189e5924 261 NewSocket();
uci1 0:26c9189e5924 262 SockConnect(server, timeout);
uci1 0:26c9189e5924 263 }
uci1 0:26c9189e5924 264 }
uci1 0:26c9189e5924 265
uci1 0:26c9189e5924 266 fEthWriteable = fEthConnected;
uci1 0:26c9189e5924 267 }
uci1 0:26c9189e5924 268 }
uci1 0:26c9189e5924 269
uci1 0:26c9189e5924 270 return fEthConnected;
uci1 0:26c9189e5924 271
uci1 0:26c9189e5924 272 }
uci1 0:26c9189e5924 273
uci1 0:26c9189e5924 274 bool SnCommAfarNetIf::SockConnect(const Host& server, const uint32_t timeout) {
uci1 0:26c9189e5924 275 #ifdef DEBUG
uci1 0:26c9189e5924 276 printf("SnCommAfarNetIf::SockConnect\r\n");
uci1 0:26c9189e5924 277 #endif
uci1 0:26c9189e5924 278 TCPSocketErr bindErr = fSock->connect(server);
uci1 0:26c9189e5924 279 while ( (bindErr!=TCPSOCKET_OK) && (IsTimedOut(timeout)==false) ) {
uci1 0:26c9189e5924 280 wait_ms(250);
uci1 0:26c9189e5924 281 bindErr = fSock->connect(server);
uci1 0:26c9189e5924 282 }
uci1 0:26c9189e5924 283 #ifdef DEBUG
uci1 0:26c9189e5924 284 printf("SnCommAfarNetIf::SockConnect: %s\r\n",
uci1 0:26c9189e5924 285 (bindErr==TCPSOCKET_OK) ? "true" : "false");
uci1 0:26c9189e5924 286 #endif
uci1 0:26c9189e5924 287 return (bindErr==TCPSOCKET_OK);
uci1 0:26c9189e5924 288 }
uci1 0:26c9189e5924 289
uci1 0:26c9189e5924 290 bool SnCommAfarNetIf::CloseConn(const uint32_t) {
uci1 0:26c9189e5924 291 fEthConnected = false;
uci1 4:8328c2972290 292 fEthSetup = false; // new addition -- why was this not here before?
uci1 0:26c9189e5924 293 return (fSock->close() == TCPSOCKET_OK);
uci1 0:26c9189e5924 294 }
uci1 0:26c9189e5924 295
uci1 0:26c9189e5924 296 void SnCommAfarNetIf::onTCPSocketEvent(TCPSocketEvent e) {
uci1 0:26c9189e5924 297 #ifdef DEBUG
uci1 4:8328c2972290 298 printf("TCPSocketEvent is (%d) ",static_cast<int>(e));
uci1 0:26c9189e5924 299 #endif
uci1 0:26c9189e5924 300 if (e == TCPSOCKET_CONNECTED) {
uci1 0:26c9189e5924 301 fEthConnected = true;
uci1 0:26c9189e5924 302 #ifdef DEBUG
uci1 0:26c9189e5924 303 printf("TCPSOCKET_CONNECTED\r\n");
uci1 0:26c9189e5924 304 #endif
uci1 0:26c9189e5924 305 } else if (e == TCPSOCKET_WRITEABLE) {
uci1 0:26c9189e5924 306 #ifdef DEBUG
uci1 0:26c9189e5924 307 printf("TCPSOCKET_WRITEABLE\r\n");
uci1 0:26c9189e5924 308 fEthWriteable=true;
uci1 0:26c9189e5924 309 #endif
uci1 0:26c9189e5924 310 //TODO: does this event ever occur?
uci1 0:26c9189e5924 311 } else if (e == TCPSOCKET_READABLE) {
uci1 0:26c9189e5924 312 #ifdef DEBUG
uci1 0:26c9189e5924 313 printf("TCPSOCKET_READABLE\r\n");
uci1 0:26c9189e5924 314 #endif
uci1 0:26c9189e5924 315 fEthConnected = true;
uci1 0:26c9189e5924 316 // read & buffer the data immediately (if there's room)
uci1 0:26c9189e5924 317 if (fSockReading==false) {
uci1 0:26c9189e5924 318 fSockReading = true;
uci1 0:26c9189e5924 319 fRxWt += DoIO(fRxWt, fRxEnd-fRxWt, time(0)+RX_TIMEOUT, &TCPSocket::recv, fRxBufWriteable);
uci1 0:26c9189e5924 320 fSockReading = false;
uci1 0:26c9189e5924 321 #ifdef DEBUG
uci1 0:26c9189e5924 322 printf("fRxBuf now contains:\r\n");
uci1 0:26c9189e5924 323 dispStrBytes(fRxBuf, fRxWt-fRxBuf);
uci1 0:26c9189e5924 324 #endif
uci1 0:26c9189e5924 325 }
uci1 0:26c9189e5924 326 } else if (e == TCPSOCKET_CONABRT) {
uci1 0:26c9189e5924 327 #ifdef DEBUG
uci1 0:26c9189e5924 328 printf("TCPSOCKET_CONABRT\r\n");
uci1 0:26c9189e5924 329 #endif
uci1 0:26c9189e5924 330 fConnAborted = true;
uci1 0:26c9189e5924 331 }
uci1 0:26c9189e5924 332 #ifdef DEBUG
uci1 0:26c9189e5924 333 else {
uci1 0:26c9189e5924 334 printf(" %d\r\n",(int)e);
uci1 0:26c9189e5924 335 }
uci1 0:26c9189e5924 336 #endif
uci1 0:26c9189e5924 337 }
uci1 0:26c9189e5924 338
uci1 0:26c9189e5924 339
uci1 0:26c9189e5924 340 #endif // ENABLE_AFAR