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:
Tue Jul 23 14:56:17 2013 +0000
Revision:
81:6d216969b701
Parent:
44:ffd9a11d4f95
redirected prints

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
daniele 29:1a47b7151851 30 #include "EthernetInterface.h"
daniele 29:1a47b7151851 31 #include "Queue.h"
daniele 29:1a47b7151851 32 #include "wrapper.h"
daniele 29:1a47b7151851 33 #include "proxy_endpoint.h"
daniele 29:1a47b7151851 34 #include "pico_dev_mbed_emac.h"
daniele 29:1a47b7151851 35 #include "mbed.h"
daniele 29:1a47b7151851 36 #include "PicoCondition.h"
tass 81:6d216969b701 37 #include "PicoTerm.h"
daniele 29:1a47b7151851 38 extern "C"{
daniele 29:1a47b7151851 39 #include "pico_stack.h"
daniele 29:1a47b7151851 40 #include "pico_config.h"
daniele 29:1a47b7151851 41 #include "cmsis_os.h"
daniele 29:1a47b7151851 42 #include "pico_dhcp_client.h"
daniele 29:1a47b7151851 43 #include "pico_dns_client.h"
daniele 29:1a47b7151851 44
daniele 29:1a47b7151851 45 void (*linkCb)(uint32_t link) = NULL;
daniele 29:1a47b7151851 46 }
daniele 29:1a47b7151851 47
tass 81:6d216969b701 48 //#define eth_dbg mbed_dbg
tass 81:6d216969b701 49 #define eth_dbg ptm_dbg
tass 81:6d216969b701 50
daniele 29:1a47b7151851 51 //DigitalOutput led(LED3);
daniele 29:1a47b7151851 52 /* TCP/IP and Network Interface Initialisation */
daniele 29:1a47b7151851 53 static struct pico_device *lpc_eth;
daniele 29:1a47b7151851 54
daniele 29:1a47b7151851 55 static uint32_t dhcp_xid = 0;
daniele 29:1a47b7151851 56 static PicoCondition dhcp_mx;
daniele 29:1a47b7151851 57 static int dhcp_retval = -1;
daniele 29:1a47b7151851 58
daniele 29:1a47b7151851 59 static char mac_addr[19];
daniele 29:1a47b7151851 60 static char ip_addr[17] = "\0";
daniele 29:1a47b7151851 61 static char gw_addr[17] = "\0";
daniele 29:1a47b7151851 62 static bool use_dhcp = false;
daniele 29:1a47b7151851 63 static bool is_initialized = false;
daniele 29:1a47b7151851 64
daniele 29:1a47b7151851 65 static void dhcp_cb(void *cli, int code)
daniele 29:1a47b7151851 66 {
daniele 29:1a47b7151851 67 void *id = NULL;
daniele 29:1a47b7151851 68 struct pico_ip4 address, gateway, zero = {};
tass 41:4c18c62b31b9 69
tass 42:13e557b44f68 70 if (PICO_DHCP_ERROR == code)
daniele 29:1a47b7151851 71 goto fail;
daniele 29:1a47b7151851 72
daniele 29:1a47b7151851 73 id = pico_dhcp_get_identifier(dhcp_xid);
daniele 29:1a47b7151851 74 if (!id)
daniele 29:1a47b7151851 75 goto fail;
daniele 29:1a47b7151851 76 address = pico_dhcp_get_address(id);
daniele 29:1a47b7151851 77 gateway = pico_dhcp_get_gateway(id);
daniele 29:1a47b7151851 78 //if (address) ? // still needed
daniele 29:1a47b7151851 79 pico_ipv4_to_string(ip_addr, address.addr);
tass 81:6d216969b701 80 eth_dbg("IP assigned : %s\n",ip_addr);
daniele 29:1a47b7151851 81
daniele 29:1a47b7151851 82 if (gateway.addr != 0) {
daniele 29:1a47b7151851 83 pico_ipv4_to_string(gw_addr, gateway.addr);
tass 81:6d216969b701 84 eth_dbg("Default gateway assigned : %s\n",gw_addr);
daniele 29:1a47b7151851 85 pico_ipv4_route_add(zero, zero, gateway, 1, NULL);
daniele 29:1a47b7151851 86 }
tass 43:5f2146814b83 87
tass 43:5f2146814b83 88 dhcp_retval = 0;
daniele 29:1a47b7151851 89 dhcp_mx.unlock();
tass 41:4c18c62b31b9 90
daniele 29:1a47b7151851 91 return;
daniele 29:1a47b7151851 92
daniele 29:1a47b7151851 93 fail:
tass 42:13e557b44f68 94 eth_dbg("DHCP request failed!\n");
daniele 29:1a47b7151851 95 dhcp_retval = -1;
daniele 29:1a47b7151851 96 dhcp_mx.unlock();
daniele 29:1a47b7151851 97 }
daniele 29:1a47b7151851 98
daniele 29:1a47b7151851 99 static void init_eth(void)
daniele 29:1a47b7151851 100 {
daniele 29:1a47b7151851 101 if (!is_initialized) {
daniele 29:1a47b7151851 102 pico_stack_init();
daniele 29:1a47b7151851 103 picotcp_init();
daniele 29:1a47b7151851 104 lpc_eth = pico_emac_create("mbed0");
daniele 29:1a47b7151851 105 is_initialized = true;
daniele 29:1a47b7151851 106 pico_dns_client_init();
daniele 29:1a47b7151851 107 }
daniele 29:1a47b7151851 108 if (lpc_eth) {
daniele 29:1a47b7151851 109 snprintf(mac_addr, 19, "%02X:%02X:%02X:%02X:%02X:%02X", lpc_eth->eth->mac.addr[0], lpc_eth->eth->mac.addr[1],
daniele 29:1a47b7151851 110 lpc_eth->eth->mac.addr[2], lpc_eth->eth->mac.addr[3], lpc_eth->eth->mac.addr[4], lpc_eth->eth->mac.addr[5]);
daniele 29:1a47b7151851 111 }
tass 41:4c18c62b31b9 112
tass 41:4c18c62b31b9 113 if(lpc_eth)
tass 42:13e557b44f68 114 eth_dbg("Ethernet initialized...\n");
tass 41:4c18c62b31b9 115 else
tass 42:13e557b44f68 116 eth_dbg("Failed to start Ethernet...\n");
tass 41:4c18c62b31b9 117
daniele 29:1a47b7151851 118 }
daniele 29:1a47b7151851 119
daniele 29:1a47b7151851 120 int EthernetInterface::init()
daniele 29:1a47b7151851 121 {
daniele 29:1a47b7151851 122 init_eth();
daniele 29:1a47b7151851 123 /* use dhcp to retrieve address and gateway. */
daniele 29:1a47b7151851 124 use_dhcp = true;
daniele 29:1a47b7151851 125 return 0;
daniele 29:1a47b7151851 126 }
daniele 29:1a47b7151851 127
daniele 29:1a47b7151851 128 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
daniele 29:1a47b7151851 129 pico_ip4 pico_addr, pico_netmask, pico_gw = {0}, zero = {0};
daniele 29:1a47b7151851 130
daniele 29:1a47b7151851 131 init_eth();
daniele 29:1a47b7151851 132
daniele 29:1a47b7151851 133 use_dhcp = false;
daniele 29:1a47b7151851 134 strcpy(ip_addr, ip);
daniele 29:1a47b7151851 135
daniele 29:1a47b7151851 136 pico_string_to_ipv4(ip, &pico_addr.addr);
daniele 29:1a47b7151851 137 pico_string_to_ipv4(mask, &pico_netmask.addr);
daniele 29:1a47b7151851 138 if (gateway) {
daniele 29:1a47b7151851 139 pico_string_to_ipv4(ip, &pico_gw.addr);
daniele 29:1a47b7151851 140 }
daniele 29:1a47b7151851 141 pico_ipv4_link_add(lpc_eth, pico_addr, pico_netmask);
daniele 29:1a47b7151851 142
daniele 29:1a47b7151851 143 if (pico_gw.addr)
daniele 29:1a47b7151851 144 pico_ipv4_route_add(zero, zero, pico_gw, 1, NULL);
daniele 29:1a47b7151851 145
daniele 29:1a47b7151851 146 return 0;
daniele 29:1a47b7151851 147 }
daniele 29:1a47b7151851 148
daniele 29:1a47b7151851 149 int EthernetInterface::connect(unsigned int timeout_ms) {
daniele 29:1a47b7151851 150 //dhcp_mx.lock(); do we still need this ?
daniele 29:1a47b7151851 151 if (use_dhcp) {
daniele 29:1a47b7151851 152 if (pico_dhcp_initiate_negotiation(lpc_eth, &dhcp_cb, &dhcp_xid) < 0)
daniele 29:1a47b7151851 153 return -1;
daniele 29:1a47b7151851 154
daniele 29:1a47b7151851 155 dhcp_mx.lock(timeout_ms); // wait for a sign
tass 41:4c18c62b31b9 156
daniele 29:1a47b7151851 157 return dhcp_retval;
daniele 29:1a47b7151851 158
daniele 29:1a47b7151851 159 } else {
daniele 29:1a47b7151851 160 return 0;
daniele 29:1a47b7151851 161 }
daniele 29:1a47b7151851 162 }
daniele 29:1a47b7151851 163
daniele 29:1a47b7151851 164 int EthernetInterface::disconnect() {
daniele 29:1a47b7151851 165 if (use_dhcp) {
daniele 29:1a47b7151851 166 }
daniele 29:1a47b7151851 167 pico_device_destroy(lpc_eth);
daniele 29:1a47b7151851 168 lpc_eth = NULL;
daniele 29:1a47b7151851 169 return 0;
daniele 29:1a47b7151851 170 }
daniele 29:1a47b7151851 171
daniele 29:1a47b7151851 172 char* EthernetInterface::getMACAddress() {
daniele 29:1a47b7151851 173 return mac_addr;
daniele 29:1a47b7151851 174 }
daniele 29:1a47b7151851 175
daniele 29:1a47b7151851 176 char* EthernetInterface::getIPAddress() {
daniele 29:1a47b7151851 177 return ip_addr;
daniele 29:1a47b7151851 178 }
daniele 29:1a47b7151851 179
daniele 29:1a47b7151851 180 int EthernetInterface::registerLinkStatus(void (*cb)(uint32_t linkStatus))
daniele 29:1a47b7151851 181 {
daniele 29:1a47b7151851 182 ::linkCb = cb;
daniele 29:1a47b7151851 183 return 0;
daniele 29:1a47b7151851 184 }
daniele 29:1a47b7151851 185
daniele 29:1a47b7151851 186 int EthernetInterface::setDnsServer(const char * name)
daniele 29:1a47b7151851 187 {
daniele 29:1a47b7151851 188 struct pico_ip4 addr;
daniele 29:1a47b7151851 189 pico_string_to_ipv4(name,&addr.addr);
daniele 29:1a47b7151851 190 return pico_dns_client_nameserver(&addr,PICO_DNS_NS_ADD);
daniele 29:1a47b7151851 191 }