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 Belgium NV
Date:
Tue Mar 11 15:34:51 2014 +0100
Revision:
148:969fdb223792
Parent:
130:0cee8bdac6d0
Fixed bug #92 in PicoTCP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniele 20:3fa3db9fd4a4 1 /*
daniele 20:3fa3db9fd4a4 2 *
daniele 20:3fa3db9fd4a4 3 * PicoTCP Socket interface for mbed.
daniele 20:3fa3db9fd4a4 4 * Copyright (C) 2013 TASS Belgium NV
daniele 20:3fa3db9fd4a4 5 *
daniele 20:3fa3db9fd4a4 6 * Released under GPL v2
daniele 20:3fa3db9fd4a4 7 *
daniele 20:3fa3db9fd4a4 8 * Other licensing models might apply at the sole discretion of the copyright holders.
daniele 20:3fa3db9fd4a4 9 *
daniele 20:3fa3db9fd4a4 10 *
daniele 20:3fa3db9fd4a4 11 * This software is based on the mbed.org EthernetInterface implementation:
daniele 20:3fa3db9fd4a4 12 * Copyright (C) 2012 mbed.org, MIT License
daniele 20:3fa3db9fd4a4 13 *
daniele 20:3fa3db9fd4a4 14 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
daniele 20:3fa3db9fd4a4 15 * and associated documentation files (the "Software"), to deal in the Software without restriction,
daniele 20:3fa3db9fd4a4 16 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
daniele 20:3fa3db9fd4a4 17 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
daniele 20:3fa3db9fd4a4 18 * furnished to do so, subject to the following conditions:
daniele 20:3fa3db9fd4a4 19 *
daniele 20:3fa3db9fd4a4 20 * The above copyright notice and this permission notice shall be included in all copies or
daniele 20:3fa3db9fd4a4 21 * substantial portions of the Software.
daniele 20:3fa3db9fd4a4 22 *
daniele 20:3fa3db9fd4a4 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
daniele 20:3fa3db9fd4a4 24 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
daniele 20:3fa3db9fd4a4 25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
daniele 20:3fa3db9fd4a4 26 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
daniele 20:3fa3db9fd4a4 27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
daniele 20:3fa3db9fd4a4 28 */
daniele 20:3fa3db9fd4a4 29
daniele 20:3fa3db9fd4a4 30 #include "Socket/UDPSocket.h"
daniele 20:3fa3db9fd4a4 31 #include "wrapper.h"
daniele 20:3fa3db9fd4a4 32 #include "proxy_endpoint.h"
daniele 20:3fa3db9fd4a4 33
daniele 20:3fa3db9fd4a4 34 #include <cstring>
daniele 20:3fa3db9fd4a4 35
daniele 20:3fa3db9fd4a4 36 using std::memset;
daniele 20:3fa3db9fd4a4 37
daniele 20:3fa3db9fd4a4 38 UDPSocket::UDPSocket() {
daniele 20:3fa3db9fd4a4 39 }
daniele 20:3fa3db9fd4a4 40
daniele 20:3fa3db9fd4a4 41 int UDPSocket::init(void) {
daniele 20:3fa3db9fd4a4 42 return init_socket(SOCK_DGRAM);
daniele 20:3fa3db9fd4a4 43 }
daniele 20:3fa3db9fd4a4 44
daniele 20:3fa3db9fd4a4 45 // Server initialization
daniele 20:3fa3db9fd4a4 46 int UDPSocket::bind(int port) {
daniele 20:3fa3db9fd4a4 47 if (init_socket(SOCK_DGRAM) < 0)
daniele 20:3fa3db9fd4a4 48 return -1;
tass 35:6078073547bb 49
daniele 20:3fa3db9fd4a4 50 struct sockaddr_in localHost;
tass 35:6078073547bb 51
daniele 20:3fa3db9fd4a4 52 std::memset(&localHost, 0, sizeof(localHost));
daniele 20:3fa3db9fd4a4 53
daniele 20:3fa3db9fd4a4 54 localHost.sin_family = AF_INET;
daniele 20:3fa3db9fd4a4 55 localHost.sin_port = short_be(port);
daniele 20:3fa3db9fd4a4 56 localHost.sin_addr.s_addr = INADDR_ANY;
daniele 20:3fa3db9fd4a4 57
daniele 20:3fa3db9fd4a4 58 if (picotcp_bind(_ep, (struct sockaddr *) &localHost, (socklen_t)sizeof(localHost)) < 0) {
daniele 20:3fa3db9fd4a4 59 close();
daniele 20:3fa3db9fd4a4 60 return -1;
daniele 20:3fa3db9fd4a4 61 }
daniele 20:3fa3db9fd4a4 62
daniele 20:3fa3db9fd4a4 63 return 0;
daniele 20:3fa3db9fd4a4 64 }
daniele 20:3fa3db9fd4a4 65
daniele 20:3fa3db9fd4a4 66 int UDPSocket::join_multicast_group(EthernetInterface& eth, const char* address) {
daniele 20:3fa3db9fd4a4 67
daniele 20:3fa3db9fd4a4 68 return picotcp_join_multicast(_ep,address,eth.getIPAddress());
daniele 20:3fa3db9fd4a4 69 }
daniele 20:3fa3db9fd4a4 70
daniele 20:3fa3db9fd4a4 71 int UDPSocket::set_broadcasting(void) {
daniele 20:3fa3db9fd4a4 72 int option = 1;
daniele 20:3fa3db9fd4a4 73 return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
daniele 20:3fa3db9fd4a4 74 }
daniele 20:3fa3db9fd4a4 75
daniele 20:3fa3db9fd4a4 76 // -1 if unsuccessful, else number of bytes written
daniele 20:3fa3db9fd4a4 77 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
tass 35:6078073547bb 78 if (!_ep)
daniele 20:3fa3db9fd4a4 79 {
tass 44:ffd9a11d4f95 80 mbed_dbg("Error on socket descriptor \n");
daniele 20:3fa3db9fd4a4 81 return -1;
daniele 20:3fa3db9fd4a4 82 }
daniele 20:3fa3db9fd4a4 83
tass 35:6078073547bb 84 return picotcp_sendto(_ep, packet, length, (struct sockaddr *) &remote._remoteHost, sizeof(struct sockaddr_in));
daniele 20:3fa3db9fd4a4 85 }
daniele 20:3fa3db9fd4a4 86
daniele 20:3fa3db9fd4a4 87 // -1 if unsuccessful, else number of bytes received
daniele 20:3fa3db9fd4a4 88 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
tass 39:8d4d653d94bd 89 socklen_t remoteHostLen = sizeof(remote._remoteHost);
tass 39:8d4d653d94bd 90 int ret;
tass 35:6078073547bb 91
tass 35:6078073547bb 92 if (!_ep)
daniele 20:3fa3db9fd4a4 93 return -1;
tass 130:0cee8bdac6d0 94 _ep->revents &= ~PICO_SOCK_EV_ERR;
TASS Belgium NV 148:969fdb223792 95 ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
TASS Belgium NV 148:969fdb223792 96 if(ret < length)
TASS Belgium NV 148:969fdb223792 97 _ep->revents &= (~PICO_SOCK_EV_RD);
TASS Belgium NV 148:969fdb223792 98 if(ret) // data was read or error was reported
TASS Belgium NV 148:969fdb223792 99 return ret;
tass 39:8d4d653d94bd 100
tass 35:6078073547bb 101 TimeInterval timeout(!_blocking ? _timeout : osWaitForever);
tass 35:6078073547bb 102 if (wait_readable(timeout) != 0)
tass 35:6078073547bb 103 return 0;
tass 39:8d4d653d94bd 104
tass 35:6078073547bb 105
tass 39:8d4d653d94bd 106 ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
tass 39:8d4d653d94bd 107 if(ret < length)
tass 39:8d4d653d94bd 108 _ep->revents &= (~PICO_SOCK_EV_RD);
tass 39:8d4d653d94bd 109
tass 39:8d4d653d94bd 110 return ret;
daniele 20:3fa3db9fd4a4 111 }