Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

PicoTCP. Copyright (c) 2013 TASS Belgium NV.

Released under the GNU General Public License, version 2.

Different licensing models may exist, at the sole discretion of the Copyright holders.

Official homepage: http://www.picotcp.com

Bug tracker: https://github.com/tass-belgium/picotcp/issues

Development steps:

  • initial integration with mbed RTOS
  • generic mbed Ethernet driver
  • high performance NXP LPC1768 specific Ethernet driver
  • Multi-threading support for mbed RTOS
  • Berkeley sockets and integration with the New Socket API
  • Fork of the apps running on top of the New Socket API
  • Scheduling optimizations
  • Debugging/benchmarking/testing

Demo application (measuring TCP sender performance):

Import programlpc1768-picotcp-demo

A PicoTCP demo app testing the ethernet throughput on the lpc1768 mbed board.

Committer:
tass
Date:
Wed Sep 04 08:36:00 2013 +0000
Revision:
53:f3ea2e39a7b2
Parent:
47:ed8f44fa8db9
Child:
58:5f6eedbbbd5b
removed _is_connected variable in TCPSocketConnection class because it was redundant. Also fixed is_connected method

Who changed what in which revision?

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