Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apatel336 0:1496281373a5 1 /* Copyright (C) 2012 mbed.org, MIT License
apatel336 0:1496281373a5 2 *
apatel336 0:1496281373a5 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
apatel336 0:1496281373a5 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
apatel336 0:1496281373a5 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
apatel336 0:1496281373a5 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
apatel336 0:1496281373a5 7 * furnished to do so, subject to the following conditions:
apatel336 0:1496281373a5 8 *
apatel336 0:1496281373a5 9 * The above copyright notice and this permission notice shall be included in all copies or
apatel336 0:1496281373a5 10 * substantial portions of the Software.
apatel336 0:1496281373a5 11 *
apatel336 0:1496281373a5 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
apatel336 0:1496281373a5 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
apatel336 0:1496281373a5 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
apatel336 0:1496281373a5 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
apatel336 0:1496281373a5 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
apatel336 0:1496281373a5 17 */
apatel336 0:1496281373a5 18 #include "TCPSocketConnection.h"
apatel336 0:1496281373a5 19 #include <cstring>
apatel336 0:1496281373a5 20
apatel336 0:1496281373a5 21 using std::memset;
apatel336 0:1496281373a5 22 using std::memcpy;
apatel336 0:1496281373a5 23
apatel336 0:1496281373a5 24 TCPSocketConnection::TCPSocketConnection() :
apatel336 0:1496281373a5 25 _is_connected(false) {
apatel336 0:1496281373a5 26 }
apatel336 0:1496281373a5 27
apatel336 0:1496281373a5 28 int TCPSocketConnection::connect(const char* host, const int port) {
apatel336 0:1496281373a5 29 if (init_socket(SOCK_STREAM) < 0)
apatel336 0:1496281373a5 30 return -1;
apatel336 0:1496281373a5 31
apatel336 0:1496281373a5 32 if (set_address(host, port) != 0)
apatel336 0:1496281373a5 33 return -1;
apatel336 0:1496281373a5 34
apatel336 0:1496281373a5 35 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
apatel336 0:1496281373a5 36 close();
apatel336 0:1496281373a5 37 return -1;
apatel336 0:1496281373a5 38 }
apatel336 0:1496281373a5 39 _is_connected = true;
apatel336 0:1496281373a5 40
apatel336 0:1496281373a5 41 return 0;
apatel336 0:1496281373a5 42 }
apatel336 0:1496281373a5 43
apatel336 0:1496281373a5 44 bool TCPSocketConnection::is_connected(void) {
apatel336 0:1496281373a5 45 return _is_connected;
apatel336 0:1496281373a5 46 }
apatel336 0:1496281373a5 47
apatel336 0:1496281373a5 48 int TCPSocketConnection::send(char* data, int length) {
apatel336 0:1496281373a5 49 if ((_sock_fd < 0) || !_is_connected)
apatel336 0:1496281373a5 50 return -1;
apatel336 0:1496281373a5 51
apatel336 0:1496281373a5 52 if (!_blocking) {
apatel336 0:1496281373a5 53 TimeInterval timeout(_timeout);
apatel336 0:1496281373a5 54 if (wait_writable(timeout) != 0)
apatel336 0:1496281373a5 55 return -1;
apatel336 0:1496281373a5 56 }
apatel336 0:1496281373a5 57
apatel336 0:1496281373a5 58 int n = lwip_send(_sock_fd, data, length, 0);
apatel336 0:1496281373a5 59 _is_connected = (n != 0);
apatel336 0:1496281373a5 60
apatel336 0:1496281373a5 61 return n;
apatel336 0:1496281373a5 62 }
apatel336 0:1496281373a5 63
apatel336 0:1496281373a5 64 // -1 if unsuccessful, else number of bytes written
apatel336 0:1496281373a5 65 int TCPSocketConnection::send_all(char* data, int length) {
apatel336 0:1496281373a5 66 if ((_sock_fd < 0) || !_is_connected)
apatel336 0:1496281373a5 67 return -1;
apatel336 0:1496281373a5 68
apatel336 0:1496281373a5 69 int writtenLen = 0;
apatel336 0:1496281373a5 70 TimeInterval timeout(_timeout);
apatel336 0:1496281373a5 71 while (writtenLen < length) {
apatel336 0:1496281373a5 72 if (!_blocking) {
apatel336 0:1496281373a5 73 // Wait for socket to be writeable
apatel336 0:1496281373a5 74 if (wait_writable(timeout) != 0)
apatel336 0:1496281373a5 75 return writtenLen;
apatel336 0:1496281373a5 76 }
apatel336 0:1496281373a5 77
apatel336 0:1496281373a5 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
apatel336 0:1496281373a5 79 if (ret > 0) {
apatel336 0:1496281373a5 80 writtenLen += ret;
apatel336 0:1496281373a5 81 continue;
apatel336 0:1496281373a5 82 } else if (ret == 0) {
apatel336 0:1496281373a5 83 _is_connected = false;
apatel336 0:1496281373a5 84 return writtenLen;
apatel336 0:1496281373a5 85 } else {
apatel336 0:1496281373a5 86 return -1; //Connnection error
apatel336 0:1496281373a5 87 }
apatel336 0:1496281373a5 88 }
apatel336 0:1496281373a5 89 return writtenLen;
apatel336 0:1496281373a5 90 }
apatel336 0:1496281373a5 91
apatel336 0:1496281373a5 92 int TCPSocketConnection::receive(char* data, int length) {
apatel336 0:1496281373a5 93 if ((_sock_fd < 0) || !_is_connected)
apatel336 0:1496281373a5 94 return -1;
apatel336 0:1496281373a5 95
apatel336 0:1496281373a5 96 if (!_blocking) {
apatel336 0:1496281373a5 97 TimeInterval timeout(_timeout);
apatel336 0:1496281373a5 98 if (wait_readable(timeout) != 0)
apatel336 0:1496281373a5 99 return -1;
apatel336 0:1496281373a5 100 }
apatel336 0:1496281373a5 101
apatel336 0:1496281373a5 102 int n = lwip_recv(_sock_fd, data, length, 0);
apatel336 0:1496281373a5 103 _is_connected = (n != 0);
apatel336 0:1496281373a5 104
apatel336 0:1496281373a5 105 return n;
apatel336 0:1496281373a5 106 }
apatel336 0:1496281373a5 107
apatel336 0:1496281373a5 108 // -1 if unsuccessful, else number of bytes received
apatel336 0:1496281373a5 109 int TCPSocketConnection::receive_all(char* data, int length) {
apatel336 0:1496281373a5 110 if ((_sock_fd < 0) || !_is_connected)
apatel336 0:1496281373a5 111 return -1;
apatel336 0:1496281373a5 112
apatel336 0:1496281373a5 113 int readLen = 0;
apatel336 0:1496281373a5 114 TimeInterval timeout(_timeout);
apatel336 0:1496281373a5 115 while (readLen < length) {
apatel336 0:1496281373a5 116 if (!_blocking) {
apatel336 0:1496281373a5 117 //Wait for socket to be readable
apatel336 0:1496281373a5 118 if (wait_readable(timeout) != 0)
apatel336 0:1496281373a5 119 return readLen;
apatel336 0:1496281373a5 120 }
apatel336 0:1496281373a5 121
apatel336 0:1496281373a5 122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
apatel336 0:1496281373a5 123 if (ret > 0) {
apatel336 0:1496281373a5 124 readLen += ret;
apatel336 0:1496281373a5 125 } else if (ret == 0) {
apatel336 0:1496281373a5 126 _is_connected = false;
apatel336 0:1496281373a5 127 return readLen;
apatel336 0:1496281373a5 128 } else {
apatel336 0:1496281373a5 129 return -1; //Connnection error
apatel336 0:1496281373a5 130 }
apatel336 0:1496281373a5 131 }
apatel336 0:1496281373a5 132 return readLen;
apatel336 0:1496281373a5 133 }