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 RTOSgeneric mbed Ethernet driverhigh performance NXP LPC1768 specific Ethernet driverMulti-threading support for mbed RTOSBerkeley sockets and integration with the New Socket APIFork of the apps running on top of the New Socket APIScheduling 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.
Diff: Socket/bsd/stack_endpoint.cpp
- Revision:
- 35:6078073547bb
- Parent:
- 34:d868bb69114e
- Child:
- 37:bdf736327c71
--- a/Socket/bsd/stack_endpoint.cpp Thu Jun 20 13:48:41 2013 +0000 +++ b/Socket/bsd/stack_endpoint.cpp Fri Jun 21 08:47:35 2013 +0000 @@ -81,9 +81,11 @@ } if ((ev & PICO_SOCK_EV_CLOSE) || (ev & PICO_SOCK_EV_FIN)) { ep->connected = 0; + ep->state = SOCK_CLOSED; } if ((ev & PICO_SOCK_EV_CONN) || (ev & PICO_SOCK_EV_RD)) { ep->connected = 1; + ep->state = SOCK_CONNECTED; } ep->queue->put((void *)0); } @@ -104,6 +106,7 @@ } else { ep->s->priv = ep; printf("Added socket (open)\n"); + ep->state = SOCK_OPEN; ep->queue = new Queue<void,1>(); } PicoTcpLock->unlock(); @@ -113,13 +116,6 @@ int picotcp_state(struct stack_endpoint *ep) { -/* TODO: return one of: - SOCK_OPEN, - SOCK_BOUND, - SOCK_LISTEN, - SOCK_CONNECTED, - SOCK_CLOSED -*/ return ep->state; } @@ -131,6 +127,8 @@ PicoTcpLock->lock(); ret = pico_socket_bind(ep->s, (struct pico_ip4 *)(&local_addr->sin_addr.s_addr), &local_addr->sin_port); + if(ret == 0) + ep->state = SOCK_BOUND; PicoTcpLock->unlock(); return ret; } @@ -144,6 +142,8 @@ ep_accepting->queue = new Queue<void,1>(); if (!ep_accepting) ret = -1; + if(ret == 0) + ep->state = SOCK_LISTEN; PicoTcpLock->unlock(); return ret; } @@ -161,6 +161,7 @@ ep->revents &= (~PICO_SOCK_EV_CONN); ep->revents |= PICO_SOCK_EV_WR; ptsock_dbg("Established. sock state: %x\n", ep->s->state); + ep->state = SOCK_CONNECTED; retval = 0; } else { retval = -1; @@ -193,6 +194,7 @@ ep->revents &= (~PICO_SOCK_EV_CONN); aep->revents = 0; // set this to 0 to allow seq connections aep->revents |= PICO_SOCK_EV_WR; + aep->state = SOCK_CONNECTED; printf("Added socket (accept)\n"); *len = sizeof(struct sockaddr_in); @@ -222,28 +224,109 @@ return ret; } -int picotcp_send(struct stack_endpoint *ep,void * buff, int len, int flags) +int picotcp_send(struct stack_endpoint *ep,void * buf, int len, int flags) { - /* TODO */ - return -1; + int retval = 0; + int tot_len = 0; + if (!buf || (len <= 0)) + return 0; + PicoTcpLock->lock(); + while (tot_len < len) { + retval = pico_socket_send(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len); + if (retval == 0) { + if (tot_len < len) + ep->revents &= ~PICO_SOCK_EV_WR; + break; + } + if (retval < 0) { + tot_len = -1; + break; + } + tot_len += retval; + } + PicoTcpLock->unlock(); + picotcp_async_interrupt(NULL); + return tot_len; +} + +int picotcp_recv(struct stack_endpoint *ep,void * buf, int len, int flags) +{ + int retval = 0; + int tot_len = 0; + if (!buf || (len <= 0)) + return 0; + PicoTcpLock->lock(); + while (tot_len < len) { + retval = pico_socket_recv(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len); + if (retval == 0) { + if (tot_len < len) + ep->revents &= ~PICO_SOCK_EV_RD; + break; + } + if (retval < 0) { + tot_len = -1; + break; + } + tot_len += retval; + } + PicoTcpLock->unlock(); + picotcp_async_interrupt(NULL); + return tot_len; } -int picotcp_recv(struct stack_endpoint *ep,void * buff, int len, int flags) +int picotcp_sendto(struct stack_endpoint * ep,void * buf, int len, struct sockaddr* a,socklen_t size) { - /* TODO */ - return -1; + int retval = 0; + int tot_len = 0; + struct sockaddr_in * in = (struct sockaddr_in *)a; + if (!buf || (len <= 0)) + return 0; + if(!ep->broadcast && pico_ipv4_is_broadcast(in->sin_addr.s_addr)) + return -1; + + PicoTcpLock->lock(); + while (tot_len < len) { + retval = pico_socket_sendto(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len,(struct pico_ip4 *)&in->sin_addr.s_addr, in->sin_port); + + if (retval == 0) + break; + + if (retval < 0) { + tot_len = -1; + break; + } + tot_len += retval; + } + PicoTcpLock->unlock(); + picotcp_async_interrupt(NULL); + return tot_len; } -int picotcp_sendto(struct stack_endpoint *ep,void * buff, int len, struct sockaddr *a, socklen_t l) +int picotcp_recvfrom(struct stack_endpoint *ep,void * buf, int len, struct sockaddr *a, socklen_t * size) { - /* TODO */ - return -1; -} - -int picotcp_recvfrom(struct stack_endpoint *ep,void * buff, int len, struct sockaddr *a, socklen_t *l) -{ - /* TODO */ - return -1; + (void)len; + int retval = 0; + int tot_len = 0; + struct sockaddr_in * in = (struct sockaddr_in *)a; + if (!buf || (len <= 0)) + return 0; + PicoTcpLock->lock(); + while (tot_len < len) { + retval = pico_socket_recvfrom(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len,(struct pico_ip4 *)&(in->sin_addr.s_addr),&in->sin_port); + if (retval == 0) { + if (tot_len < len) + ep->revents &= ~PICO_SOCK_EV_RD; + break; + } + if (retval < 0) { + tot_len = -1; + break; + } + tot_len += retval; + } + PicoTcpLock->unlock(); + picotcp_async_interrupt(NULL); + return tot_len; } int picotcp_read(struct stack_endpoint *ep,void *buf, int len) @@ -280,10 +363,10 @@ PicoTcpLock->lock(); while (tot_len < len) { retval = pico_socket_write(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len); - retval = pico_socket_read(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len); + //retval = pico_socket_read(ep->s, ((uint8_t *)buf) + tot_len , len - tot_len); if (retval == 0) { if (tot_len < len) - ep->revents &= ~PICO_SOCK_EV_RD; + ep->revents &= ~PICO_SOCK_EV_WR; break; } if (retval < 0) { @@ -291,7 +374,6 @@ break; } tot_len += retval; - } PicoTcpLock->unlock(); picotcp_async_interrupt(NULL); @@ -301,14 +383,30 @@ int picotcp_setsockopt(struct stack_endpoint *ep, int option, void *value) { - /* TODO */ - return -1; + int ret; + + PicoTcpLock->lock(); + if(option == SO_BROADCAST) + { + ep->broadcast = *(int *)value; + ret = 0; + } + else + ret = pico_socket_setoption(ep->s,option,value); + PicoTcpLock->unlock(); + + return ret; } int picotcp_getsockopt(struct stack_endpoint *ep, int option, void *value) { - /* TODO */ - return -1; + int ret; + + PicoTcpLock->lock(); + ret = pico_socket_getoption(ep->s,option,value); + PicoTcpLock->unlock(); + + return ret; } int picotcp_close(struct stack_endpoint *ep) @@ -327,8 +425,16 @@ int picotcp_join_multicast(struct stack_endpoint *ep,const char* address,const char* local) { - /* TODO */ - return -1; + int ret; + struct pico_ip_mreq mreq={}; + + PicoTcpLock->lock(); + pico_string_to_ipv4(address,&mreq.mcast_group_addr.addr); + pico_string_to_ipv4(local,&mreq.mcast_link_addr.addr); + ret = pico_socket_setoption(ep->s, PICO_IP_ADD_MEMBERSHIP, &mreq); + PicoTcpLock->unlock(); + + return ret; }