OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [lwip_tcpip/] [current/] [tests/] [httpd_sequential.c] - Blame information for rev 865

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      httpd_sequential.c
4
//
5
//      A simple HTTP server using the netconn API in 'Sequential' mode.
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 2009 Free Software Foundation
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
// -------------------------------------------
36
//####ECOSGPLCOPYRIGHTEND####
37
//==========================================================================
38
//#####DESCRIPTIONBEGIN####
39
//
40
// Author(s):    Simon Kallweit
41
// Contributors:
42
// Date:         2009-05-14
43
// Purpose:
44
// Description:  A simple HTTP server using the netconn API in 'Sequential'
45
//               mode.
46
//
47
//####DESCRIPTIONEND####
48
//
49
//==========================================================================
50
 
51
#include <cyg/hal/hal_arch.h>
52
#include <cyg/infra/diag.h>
53
#include <cyg/infra/testcase.h>
54
#include <cyg/kernel/kapi.h>
55
 
56
#include <lwip.h>
57
#include <lwip/init.h>
58
 
59
#ifdef CYGFUN_LWIP_MODE_SEQUENTIAL
60
#if LWIP_TCP
61
#if LWIP_NETCONN
62
 
63
struct http_state {
64
    char buf[256];
65
    char *file;
66
    u32_t left;
67
    u8_t retries;
68
};
69
 
70
static int request_counter = 1;
71
 
72
#define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
73
 
74
static char main_stack[STACK_SIZE];
75
static cyg_thread main_thread;
76
static cyg_handle_t main_handle;
77
 
78
 
79
// Sends a text.
80
static void
81
send_text(struct netconn *conn, const char *text)
82
{
83
    const char *src = text;
84
    u16_t len = 0;
85
 
86
    while (*src++ != '\0')
87
        len++;
88
 
89
    netconn_write(conn, text, len, NETCONN_COPY);
90
}
91
 
92
// Sends a decimal value.
93
static void
94
send_dec(struct netconn *conn, int value)
95
{
96
    char tmp1[16], tmp2[16];
97
    int i = 0, j = 0;
98
 
99
    do {
100
        tmp1[i++] = '0' + value % 10;
101
        value /= 10;
102
    } while (value > 0);
103
 
104
    while (i-- > 0)
105
        tmp2[j++] = tmp1[i];
106
 
107
    netconn_write(conn, tmp2, j, NETCONN_COPY);
108
}
109
 
110
// Sends the HTTP page.
111
static void
112
send_page(struct netconn *conn)
113
{
114
    send_text(conn, "HTTP/1.0 200 OK\r\n");
115
    send_text(conn, "Content-Type: text/html\r\n");
116
    send_text(conn, "\r\n");
117
    send_text(conn, "<html>\r\n");
118
    send_text(conn, "<h1>eCos - HTTP test server running on lwIP</h1>\r\n");
119
    send_text(conn, "<table><tr><td>Version:</td><td>");
120
    send_dec(conn, LWIP_VERSION_MAJOR);
121
    send_text(conn, ".");
122
    send_dec(conn, LWIP_VERSION_MINOR);
123
    send_text(conn, ".");
124
    send_dec(conn, LWIP_VERSION_REVISION);
125
    if (LWIP_VERSION_IS_RELEASE)
126
        send_text(conn, " (release)");
127
    if (LWIP_VERSION_IS_DEVELOPMENT)
128
        send_text(conn, " (development)");
129
    if (LWIP_VERSION_IS_RC)
130
        send_text(conn, " (rc)");
131
    send_text(conn, "</td></tr><tr><td>Requests:</td><td>");
132
    send_dec(conn, request_counter++);
133
    send_text(conn, "</td></tr></table>\r\n");
134
    send_text(conn, "</html>");
135
}
136
 
137
static void
138
http_server_serve(struct netconn *conn)
139
{
140
    struct netbuf *inbuf;
141
    char *buf;
142
    u16_t len;
143
 
144
    // Read the data from the port, blocking if nothing yet there.
145
    // We assume the request (the part we care about) is in one netbuf.
146
    inbuf = netconn_recv(conn);
147
    if (!inbuf)
148
        return;
149
 
150
    if (netconn_err(conn) == ERR_OK) {
151
 
152
        // Get data from the netbuf
153
        netbuf_data(inbuf, (void **) &buf, &len);
154
 
155
        // Is this an HTTP GET command? Only check the first 5 chars, since
156
        // there are other formats for GET, and we're keeping it very simple.
157
        if (len >= 5 && buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T' &&
158
            buf[3] == ' ' && buf[4] == '/' ) {
159
 
160
            // Send the response
161
            send_page(conn);
162
        }
163
    }
164
 
165
    // Delete the buffer (netconn_recv gives us ownership, so we have to make
166
    // sure to deallocate the buffer).
167
    netbuf_delete(inbuf);
168
}
169
 
170
 
171
// Main thread.
172
void
173
main_thread_entry(cyg_addrword_t p)
174
{
175
    struct netconn *server, *client;
176
 
177
    CYG_TEST_INFO("Initializing lwIP");
178
    cyg_lwip_sequential_init();
179
 
180
    // Create a new TCP connection handle
181
    CYG_TEST_INFO("Initializing server");
182
    server = netconn_new(NETCONN_TCP);
183
    if (server == NULL)
184
        CYG_TEST_FAIL_FINISH("Cannot allocate server netconn");
185
 
186
    // Bind to port 80 (HTTP) with default IP address
187
    netconn_bind(server, NULL, 80);
188
 
189
    // Put the connection into LISTEN state
190
    netconn_listen(server);
191
 
192
    // Accept client connections
193
    CYG_TEST_INFO("Running");
194
    while(1) {
195
        client = netconn_accept(server);
196
        http_server_serve(client);
197
        netconn_close(client);
198
        netconn_delete(client);
199
    }
200
}
201
 
202
void
203
httpd_main(void)
204
{
205
    CYG_TEST_INIT();
206
    CYG_TEST_INFO("Testing httpd");
207
 
208
    cyg_thread_create(
209
        10,                 // Priority
210
        main_thread_entry,  // Entry
211
        0,                  // Entry parameter
212
        "main",             // Name
213
        main_stack,         // Stack
214
        STACK_SIZE,         // Size
215
        &main_handle,       // Handle
216
        &main_thread        // Thread data structure
217
    );
218
    cyg_thread_resume(main_handle);
219
    cyg_scheduler_start();
220
 
221
    CYG_TEST_FAIL_FINISH("Not reached");
222
}
223
 
224
externC void
225
cyg_start(void)
226
{
227
    httpd_main();
228
}
229
 
230
#else // LWIP_NETCONN
231
#define N_A_MSG "Netconn support disabled"
232
#endif // LWIP_NETCONN
233
 
234
#else // LWIP_TCP
235
#define N_A_MSG "TCP support disabled"
236
#endif // LWIP_TCP
237
 
238
#else // CYGFUN_LWIP_MODE_SEQUENTIAL
239
#define N_A_MSG "Not configured in 'Sequential' mode"
240
#endif // CYGFUN_LWIP_MODE_SEQUENTIAL
241
 
242
#ifdef N_A_MSG
243
externC void
244
cyg_start(void)
245
{
246
    CYG_TEST_INIT();
247
    CYG_TEST_NA(N_A_MSG);
248
}
249
#endif // N_A_MSG

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.