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 May 31 11:34:03 2013 +0000
Revision:
5:445d2fc04784
Child:
8:406097e529eb
Integrated mbed friendly sockets

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