Switched demo to lwip HTTP server for testing.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10798 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
Giovanni Di Sirio 2017-10-08 13:18:59 +00:00
parent 13691d8e0c
commit a549cb2cd8
5 changed files with 9 additions and 269 deletions

View File

@ -127,7 +127,7 @@ CSRC = $(STARTUPSRC) \
$(STREAMSSRC) \ $(STREAMSSRC) \
$(SHELLSRC) \ $(SHELLSRC) \
$(CHIBIOS)/os/various/evtimer.c \ $(CHIBIOS)/os/various/evtimer.c \
web/web.c usbcfg.c main.c usbcfg.c main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global # C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting. # setting.

View File

@ -379,7 +379,7 @@
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
#define CH_DBG_STATISTICS TRUE #define CH_DBG_STATISTICS FALSE
/** /**
* @brief Debug option, system state check. * @brief Debug option, system state check.
@ -388,7 +388,7 @@
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
#define CH_DBG_SYSTEM_STATE_CHECK TRUE #define CH_DBG_SYSTEM_STATE_CHECK FALSE
/** /**
* @brief Debug option, parameters checks. * @brief Debug option, parameters checks.
@ -397,7 +397,7 @@
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
#define CH_DBG_ENABLE_CHECKS TRUE #define CH_DBG_ENABLE_CHECKS FALSE
/** /**
* @brief Debug option, consistency checks. * @brief Debug option, consistency checks.
@ -407,7 +407,7 @@
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
#define CH_DBG_ENABLE_ASSERTS TRUE #define CH_DBG_ENABLE_ASSERTS FALSE
/** /**
* @brief Debug option, trace buffer. * @brief Debug option, trace buffer.
@ -415,7 +415,7 @@
* *
* @note The default is @p CH_DBG_TRACE_MASK_DISABLED. * @note The default is @p CH_DBG_TRACE_MASK_DISABLED.
*/ */
#define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_ALL #define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_DISABLED
/** /**
* @brief Trace buffer entries. * @brief Trace buffer entries.
@ -434,7 +434,7 @@
* @note The default failure mode is to halt the system with the global * @note The default failure mode is to halt the system with the global
* @p panic_msg variable set to @p NULL. * @p panic_msg variable set to @p NULL.
*/ */
#define CH_DBG_ENABLE_STACK_CHECK TRUE #define CH_DBG_ENABLE_STACK_CHECK FALSE
/** /**
* @brief Debug option, stacks initialization. * @brief Debug option, stacks initialization.
@ -444,7 +444,7 @@
* *
* @note The default is @p FALSE. * @note The default is @p FALSE.
*/ */
#define CH_DBG_FILL_THREADS TRUE #define CH_DBG_FILL_THREADS FALSE
/** /**
* @brief Debug option, threads profiling. * @brief Debug option, threads profiling.

View File

@ -322,10 +322,8 @@ int main(void) {
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
/* /*
* Creates the HTTP thread (it changes priority internally). * Starts the HTTP server.
*/ */
// chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1,
// http_server, NULL);
httpd_init(); httpd_init();
/* /*

View File

@ -1,203 +0,0 @@
/*
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* This file is a modified version of the lwIP web server demo. The original
* author is unknown because the file didn't contain any license information.
*/
/**
* @file web.c
* @brief HTTP server wrapper thread code.
* @addtogroup WEB_THREAD
* @{
*/
#include <ctype.h>
#include "ch.h"
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "web.h"
#if LWIP_NETCONN
static char url_buffer[WEB_MAX_PATH_SIZE];
#define HEXTOI(x) (isdigit(x) ? (x) - '0' : (x) - 'a' + 10)
/**
* @brief Decodes an URL sting.
* @note The string is terminated by a zero or a separator.
*
* @param[in] url encoded URL string
* @param[out] buf buffer for the processed string
* @param[in] max max number of chars to copy into the buffer
* @return The conversion status.
* @retval false string converted.
* @retval true the string was not valid or the buffer overflowed
*
* @notapi
*/
static bool decode_url(const char *url, char *buf, size_t max) {
while (true) {
int h, l;
unsigned c = *url++;
switch (c) {
case 0:
case '\r':
case '\n':
case '\t':
case ' ':
case '?':
*buf = 0;
return false;
case '.':
if (max <= 1)
return true;
h = *(url + 1);
if (h == '.')
return true;
break;
case '%':
if (max <= 1)
return true;
h = tolower((int)*url++);
if (h == 0)
return true;
if (!isxdigit(h))
return true;
l = tolower((int)*url++);
if (l == 0)
return true;
if (!isxdigit(l))
return true;
c = (char)((HEXTOI(h) << 4) | HEXTOI(l));
break;
default:
if (max <= 1)
return true;
if (!isalnum(c) && (c != '_') && (c != '-') && (c != '+') &&
(c != '/'))
return true;
break;
}
*buf++ = c;
max--;
}
}
static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
static const char http_index_html[] = "<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page.</body></html>";
static void http_server_serve(struct netconn *conn) {
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf.*/
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK) {
netbuf_data(inbuf, (void **)&buf, &buflen);
/* Is this an HTTP GET command? (only check the first 5 chars, since
there are other formats for GET, and we're keeping it very simple ).*/
if (buflen >= 5 &&
buf[0] == 'G' &&
buf[1] == 'E' &&
buf[2] == 'T' &&
buf[3] == ' ' &&
buf[4] == '/') {
if (decode_url(buf + 4, url_buffer, WEB_MAX_PATH_SIZE)) {
/* Invalid URL handling.*/
return;
}
/* Send the HTML header
* subtract 1 from the size, since we dont send the \0 in the string
* NETCONN_NOCOPY: our data is const static, so no need to copy it
*/
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
/* Send our HTML page */
netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY);
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
/**
* @brief Stack area for the http thread.
*/
THD_WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE);
/**
* @brief HTTP server thread.
*/
THD_FUNCTION(http_server, p) {
struct netconn *conn, *newconn;
err_t err;
(void)p;
chRegSetThreadName("http");
/* Create a new TCP connection handle */
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("http_server: invalid conn", (conn != NULL), chThdExit(MSG_RESET););
/* Bind to port 80 (HTTP) with default IP address */
netconn_bind(conn, NULL, WEB_THREAD_PORT);
/* Put the connection into LISTEN state */
netconn_listen(conn);
/* Goes to the final priority after initialization.*/
chThdSetPriority(WEB_THREAD_PRIORITY);
while (true) {
err = netconn_accept(conn, &newconn);
if (err != ERR_OK)
continue;
http_server_serve(newconn);
netconn_delete(newconn);
}
}
#endif /* LWIP_NETCONN */
/** @} */

View File

@ -1,55 +0,0 @@
/*
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file web.h
* @brief HTTP server wrapper thread macros and structures.
* @addtogroup WEB_THREAD
* @{
*/
#ifndef WEB_H
#define WEB_H
#if !defined(WEB_THREAD_STACK_SIZE)
#define WEB_THREAD_STACK_SIZE 1024
#endif
#if !defined(WEB_THREAD_PORT)
#define WEB_THREAD_PORT 80
#endif
#if !defined(WEB_THREAD_PRIORITY)
#define WEB_THREAD_PRIORITY (LOWPRIO + 2)
#endif
#if !defined(WEB_MAX_PATH_SIZE)
#define WEB_MAX_PATH_SIZE 128
#endif
extern THD_WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE);
#ifdef __cplusplus
extern "C" {
#endif
THD_FUNCTION(http_server, p);
#ifdef __cplusplus
}
#endif
#endif /* WEB_H */
/** @} */