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:
daniele
Date:
Sun Jun 16 20:19:44 2013 +0000
Revision:
29:1a47b7151851
Child:
41:4c18c62b31b9
Updated from masterbranch;

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