Daniele Lacamera / PicoTCP-Experimental_CDC_ECM_Branch

Fork of PicoTCP by Daniele Lacamera

Committer:
tass
Date:
Thu Jun 06 11:40:20 2013 +0000
Revision:
15:129f20ca4d7d
Parent:
13:c6662adea07d
updated picotcp_select

Who changed what in which revision?

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