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.
modules/pico_simple_http.c@131:4758606c9316, 2013-12-16 (annotated)
- Committer:
- TASS Belgium NV
- Date:
- Mon Dec 16 11:25:54 2013 +0100
- Revision:
- 131:4758606c9316
- Parent:
- 68:0847e35d08a6
Syncronized with master branch
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tass | 68:0847e35d08a6 | 1 | /********************************************************************* |
TASS Belgium NV |
131:4758606c9316 | 2 | PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. |
TASS Belgium NV |
131:4758606c9316 | 3 | See LICENSE and COPYING for usage. |
tass | 68:0847e35d08a6 | 4 | |
TASS Belgium NV |
131:4758606c9316 | 5 | Author: Andrei Carp <andrei.carp@tass.be> |
TASS Belgium NV |
131:4758606c9316 | 6 | *********************************************************************/ |
tass | 68:0847e35d08a6 | 7 | |
tass | 68:0847e35d08a6 | 8 | #include "pico_config.h" |
tass | 68:0847e35d08a6 | 9 | #include "pico_socket.h" |
tass | 68:0847e35d08a6 | 10 | #include "pico_tcp.h" |
tass | 68:0847e35d08a6 | 11 | #include "pico_ipv4.h" |
tass | 68:0847e35d08a6 | 12 | #include "pico_simple_http.h" |
tass | 68:0847e35d08a6 | 13 | |
tass | 68:0847e35d08a6 | 14 | /* The HTTP Server cannot be available without TCP support */ |
tass | 68:0847e35d08a6 | 15 | #if (defined PICO_SUPPORT_HTTP) && (defined PICO_SUPPORT_IPV4) && (defined PICO_SUPPORT_TCP) |
tass | 68:0847e35d08a6 | 16 | |
TASS Belgium NV |
131:4758606c9316 | 17 | #define HTTP_LISTEN_PORT 80u |
TASS Belgium NV |
131:4758606c9316 | 18 | #define HTTP_BACKLOG 5u |
tass | 68:0847e35d08a6 | 19 | #define HTTP_HEADER_SIZE 256u |
tass | 68:0847e35d08a6 | 20 | |
TASS Belgium NV |
131:4758606c9316 | 21 | #define HTTP_SUCCESS 0 |
TASS Belgium NV |
131:4758606c9316 | 22 | #define HTTP_ERROR -1 |
tass | 68:0847e35d08a6 | 23 | |
TASS Belgium NV |
131:4758606c9316 | 24 | static struct pico_socket *httpServer = NULL; |
TASS Belgium NV |
131:4758606c9316 | 25 | static char httpResponse[] = |
TASS Belgium NV |
131:4758606c9316 | 26 | "HTTP/1.0 200 OK\r\n\ |
tass | 68:0847e35d08a6 | 27 | Content-Type: text/html\r\n\ |
tass | 68:0847e35d08a6 | 28 | \r\n\ |
tass | 68:0847e35d08a6 | 29 | <html><head>\r\n\ |
tass | 68:0847e35d08a6 | 30 | <title>picoTCP Simple Http server</title>\r\n\ |
tass | 68:0847e35d08a6 | 31 | </head>\r\n\ |
tass | 68:0847e35d08a6 | 32 | <body>\r\n\ |
tass | 68:0847e35d08a6 | 33 | <h1>Hello world from picoTCP !!</h1>\r\n\ |
tass | 68:0847e35d08a6 | 34 | </body>\r\n"; |
tass | 68:0847e35d08a6 | 35 | |
tass | 68:0847e35d08a6 | 36 | static void httpEventCbk(uint16_t ev, struct pico_socket *self) |
tass | 68:0847e35d08a6 | 37 | { |
TASS Belgium NV |
131:4758606c9316 | 38 | static struct pico_socket *client = NULL; |
TASS Belgium NV |
131:4758606c9316 | 39 | uint32_t peer; |
TASS Belgium NV |
131:4758606c9316 | 40 | uint16_t port; |
TASS Belgium NV |
131:4758606c9316 | 41 | int r; |
TASS Belgium NV |
131:4758606c9316 | 42 | char buffer[HTTP_HEADER_SIZE]; |
tass | 68:0847e35d08a6 | 43 | |
TASS Belgium NV |
131:4758606c9316 | 44 | switch(ev) |
TASS Belgium NV |
131:4758606c9316 | 45 | { |
TASS Belgium NV |
131:4758606c9316 | 46 | case PICO_SOCK_EV_CONN: |
TASS Belgium NV |
131:4758606c9316 | 47 | if(!client) |
TASS Belgium NV |
131:4758606c9316 | 48 | client = pico_socket_accept(self, &peer, &port); |
TASS Belgium NV |
131:4758606c9316 | 49 | |
TASS Belgium NV |
131:4758606c9316 | 50 | break; |
tass | 68:0847e35d08a6 | 51 | |
TASS Belgium NV |
131:4758606c9316 | 52 | case PICO_SOCK_EV_RD: |
TASS Belgium NV |
131:4758606c9316 | 53 | /* do not check http integrity, just mark that the http header has arrived */ |
TASS Belgium NV |
131:4758606c9316 | 54 | /* prepare to send the response */ |
TASS Belgium NV |
131:4758606c9316 | 55 | r = pico_socket_recvfrom(self, buffer, HTTP_HEADER_SIZE, &peer, &port); |
TASS Belgium NV |
131:4758606c9316 | 56 | if(r > 0 && memcmp(buffer, "GET", 3u) == 0u) |
TASS Belgium NV |
131:4758606c9316 | 57 | { /* it is an http header asking for data, return data and close */ |
TASS Belgium NV |
131:4758606c9316 | 58 | pico_socket_write(self, httpResponse, sizeof(httpResponse)); |
TASS Belgium NV |
131:4758606c9316 | 59 | pico_socket_close(self); |
TASS Belgium NV |
131:4758606c9316 | 60 | } |
TASS Belgium NV |
131:4758606c9316 | 61 | else |
TASS Belgium NV |
131:4758606c9316 | 62 | { |
TASS Belgium NV |
131:4758606c9316 | 63 | /* kill the connection, invalid header */ |
TASS Belgium NV |
131:4758606c9316 | 64 | pico_socket_close(self); |
TASS Belgium NV |
131:4758606c9316 | 65 | } |
tass | 68:0847e35d08a6 | 66 | |
TASS Belgium NV |
131:4758606c9316 | 67 | break; |
tass | 68:0847e35d08a6 | 68 | |
TASS Belgium NV |
131:4758606c9316 | 69 | case PICO_SOCK_EV_ERR: |
TASS Belgium NV |
131:4758606c9316 | 70 | case PICO_SOCK_EV_CLOSE: |
TASS Belgium NV |
131:4758606c9316 | 71 | /* free the used socket */ |
TASS Belgium NV |
131:4758606c9316 | 72 | client = NULL; |
TASS Belgium NV |
131:4758606c9316 | 73 | break; |
TASS Belgium NV |
131:4758606c9316 | 74 | |
TASS Belgium NV |
131:4758606c9316 | 75 | default: |
TASS Belgium NV |
131:4758606c9316 | 76 | break; |
TASS Belgium NV |
131:4758606c9316 | 77 | } |
tass | 68:0847e35d08a6 | 78 | } |
tass | 68:0847e35d08a6 | 79 | |
TASS Belgium NV |
131:4758606c9316 | 80 | int pico_startHttpServer(struct pico_ip4 *address) |
tass | 68:0847e35d08a6 | 81 | { |
tass | 68:0847e35d08a6 | 82 | |
TASS Belgium NV |
131:4758606c9316 | 83 | uint16_t localHttpPort = short_be(HTTP_LISTEN_PORT); |
tass | 68:0847e35d08a6 | 84 | |
TASS Belgium NV |
131:4758606c9316 | 85 | if(!pico_is_port_free(localHttpPort, PICO_PROTO_TCP, address, &pico_proto_ipv4)) |
TASS Belgium NV |
131:4758606c9316 | 86 | { |
TASS Belgium NV |
131:4758606c9316 | 87 | pico_err = PICO_ERR_EADDRINUSE; |
TASS Belgium NV |
131:4758606c9316 | 88 | return HTTP_ERROR; |
TASS Belgium NV |
131:4758606c9316 | 89 | } |
tass | 68:0847e35d08a6 | 90 | |
TASS Belgium NV |
131:4758606c9316 | 91 | httpServer = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, httpEventCbk); |
tass | 68:0847e35d08a6 | 92 | |
TASS Belgium NV |
131:4758606c9316 | 93 | if(!httpServer) |
TASS Belgium NV |
131:4758606c9316 | 94 | { |
TASS Belgium NV |
131:4758606c9316 | 95 | pico_err = PICO_ERR_ENOMEM; |
TASS Belgium NV |
131:4758606c9316 | 96 | return HTTP_ERROR; |
TASS Belgium NV |
131:4758606c9316 | 97 | } |
tass | 68:0847e35d08a6 | 98 | |
TASS Belgium NV |
131:4758606c9316 | 99 | /* both functions set the pico_err themselves. */ |
TASS Belgium NV |
131:4758606c9316 | 100 | if(pico_socket_bind(httpServer, address, &localHttpPort)) |
TASS Belgium NV |
131:4758606c9316 | 101 | return HTTP_ERROR; |
tass | 68:0847e35d08a6 | 102 | |
TASS Belgium NV |
131:4758606c9316 | 103 | if(pico_socket_listen(httpServer, HTTP_BACKLOG)) |
TASS Belgium NV |
131:4758606c9316 | 104 | return HTTP_ERROR; |
tass | 68:0847e35d08a6 | 105 | |
TASS Belgium NV |
131:4758606c9316 | 106 | return HTTP_SUCCESS; |
tass | 68:0847e35d08a6 | 107 | } |
tass | 68:0847e35d08a6 | 108 | |
tass | 68:0847e35d08a6 | 109 | int pico_stopHttpServer(void) |
tass | 68:0847e35d08a6 | 110 | { |
TASS Belgium NV |
131:4758606c9316 | 111 | if(!httpServer) |
TASS Belgium NV |
131:4758606c9316 | 112 | { |
TASS Belgium NV |
131:4758606c9316 | 113 | pico_err = PICO_ERR_EINVAL; |
TASS Belgium NV |
131:4758606c9316 | 114 | return HTTP_ERROR; |
TASS Belgium NV |
131:4758606c9316 | 115 | } |
tass | 68:0847e35d08a6 | 116 | |
TASS Belgium NV |
131:4758606c9316 | 117 | if(pico_socket_close(httpServer)) |
TASS Belgium NV |
131:4758606c9316 | 118 | { |
TASS Belgium NV |
131:4758606c9316 | 119 | /* no need to set the error here, function already set it */ |
TASS Belgium NV |
131:4758606c9316 | 120 | httpServer = NULL; |
TASS Belgium NV |
131:4758606c9316 | 121 | return HTTP_ERROR; |
TASS Belgium NV |
131:4758606c9316 | 122 | } |
tass | 68:0847e35d08a6 | 123 | |
TASS Belgium NV |
131:4758606c9316 | 124 | httpServer = NULL; |
TASS Belgium NV |
131:4758606c9316 | 125 | return HTTP_SUCCESS; |
tass | 68:0847e35d08a6 | 126 | } |
tass | 68:0847e35d08a6 | 127 | |
tass | 68:0847e35d08a6 | 128 | #endif |
tass | 68:0847e35d08a6 | 129 | |
tass | 68:0847e35d08a6 | 130 |