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:
Fri Jun 07 13:44:59 2013 +0000
Revision:
18:c6f67fcfc62a
Parent:
8:406097e529eb
Started integrating the new socket interface.;

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