Add uIP-split code to the Webserver project, so that each packet is split in half to avoid the delayed-ACK problem when communicating with other devices. Condense HTTP server code, so that the HTTP headers are all sent from the one state. Make default filename append to any directory URI, rather than just the root directory.

pull/1469/head
Dean Camera 15 years ago
parent c77b30e9e6
commit 3b6987bca3

File diff suppressed because one or more lines are too long

@ -110,6 +110,7 @@ ISR(TIMER1_COMPA_vect, ISR_BLOCK)
/* Reset log tick counter to prepare for next logging interval */
CurrentLoggingTicks = 0;
/* Only log when not connected to a USB host */
if (USB_DeviceState == DEVICE_STATE_Unattached)
{
uint8_t Day, Month, Year;
@ -139,15 +140,15 @@ int main(void)
/* Fetch logging interval from EEPROM */
LoggingInterval500MS_SRAM = eeprom_read_byte(&LoggingInterval500MS_EEPROM);
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
SetupHardware();
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
/* Mount and open the log file on the dataflash FAT partition */
OpenLogFile();
/* Discard the first sample from the temperature sensor, as it is generally incorrect */
uint8_t Dummy = Temperature_GetTemperature();
volatile uint8_t Dummy = Temperature_GetTemperature();
(void)Dummy;
for (;;)

@ -54,11 +54,14 @@ const char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n"
"MIME-version: 1.0\r\n"
"Content-Type: text/plain\r\n\r\n"
"Error 404: File Not Found";
"Error 404: File Not Found: /";
/** Default MIME type sent if no other MIME type can be determined. */
const char PROGMEM DefaultMIMEType[] = "text/plain";
/** Default filename to fetch when a directory is requested */
const char PROGMEM DefaultDirFileName[] = "index.htm";
/** List of MIME types for each supported file extension. */
const MIME_Type_t MIMETypes[] =
{
@ -125,6 +128,12 @@ void HTTPServerApp_Callback(void)
AppState->HTTPServer.CurrentState = AppState->HTTPServer.NextState;
}
if (uip_rexmit())
{
/* Return file pointer to the last ACKed position */
f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.ACKedFilePos);
}
if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll())
{
switch (AppState->HTTPServer.CurrentState)
@ -135,9 +144,6 @@ void HTTPServerApp_Callback(void)
case WEBSERVER_STATE_SendResponseHeader:
HTTPServerApp_SendResponseHeader();
break;
case WEBSERVER_STATE_SendMIMETypeHeader:
HTTPServerApp_SendMIMETypeHeader();
break;
case WEBSERVER_STATE_SendData:
HTTPServerApp_SendData();
break;
@ -163,6 +169,7 @@ static void HTTPServerApp_OpenRequestedFile(void)
return;
char* RequestToken = strtok(AppData, " ");
char* RequestedFileName = strtok(NULL, " ");
/* Must be a GET request, abort otherwise */
if (strcmp(RequestToken, "GET") != 0)
@ -171,19 +178,25 @@ static void HTTPServerApp_OpenRequestedFile(void)
return;
}
char* RequestedFileName = strtok(NULL, " ");
/* If the requested filename has more that just the leading '/' path in it, copy it over */
if (strlen(RequestedFileName) > 1)
/* Copy over the requested filename */
strncpy(AppState->HTTPServer.FileName, &RequestedFileName[1], (sizeof(AppState->HTTPServer.FileName) - 1));
else
strcpy(AppState->HTTPServer.FileName, "index.htm");
/* Ensure filename is null-terminated */
AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00;
/* If the URI is a directory, append the default filename */
if (AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName) - 1] == '/')
{
strncpy_P(&AppState->HTTPServer.FileName[strlen(AppState->HTTPServer.FileName)], DefaultDirFileName,
(sizeof(AppState->HTTPServer.FileName) - (strlen(AppState->HTTPServer.FileName) + 1)));
/* Ensure altered filename is still null-terminated */
AppState->HTTPServer.FileName[(sizeof(AppState->HTTPServer.FileName) - 1)] = 0x00;
}
/* Try to open the file from the Dataflash disk */
AppState->HTTPServer.FileOpen = (f_open(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.FileName, FA_OPEN_EXISTING | FA_READ) == FR_OK);
AppState->HTTPServer.FileOpen = (f_open(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.FileName,
(FA_OPEN_EXISTING | FA_READ)) == FR_OK);
/* Lock to the SendResponseHeader state until connection terminated */
AppState->HTTPServer.CurrentState = WEBSERVER_STATE_SendResponseHeader;
@ -198,37 +211,25 @@ static void HTTPServerApp_SendResponseHeader(void)
uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
char* const AppData = (char*)uip_appdata;
const char* HeaderToSend;
char* Extension = strpbrk(AppState->HTTPServer.FileName, ".");
bool FoundMIMEType = false;
/* Determine which HTTP header should be sent to the client */
if (AppState->HTTPServer.FileOpen)
{
HeaderToSend = HTTP200Header;
AppState->HTTPServer.NextState = WEBSERVER_STATE_SendMIMETypeHeader;
}
else
/* If the file isn't already open, it wasn't found - send back a 404 error response and abort */
if (!(AppState->HTTPServer.FileOpen))
{
HeaderToSend = HTTP404Header;
AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing;
}
/* Copy over the HTTP response header and send it to the receiving client */
strcpy_P(AppData, HeaderToSend);
/* Copy over the HTTP 404 response header and send it to the receiving client */
strcpy_P(AppData, HTTP404Header);
strcpy(&AppData[strlen(AppData)], AppState->HTTPServer.FileName);
uip_send(AppData, strlen(AppData));
}
/** HTTP Server State handler for the MIME Header Send state. This state manages the transmission of the file
* MIME type header for the requested file to the receiving HTTP client.
*/
static void HTTPServerApp_SendMIMETypeHeader(void)
{
uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
char* const AppData = (char*)uip_appdata;
AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing;
return;
}
char* Extension = strpbrk(AppState->HTTPServer.FileName, ".");
uint16_t MIMEHeaderLength = 0;
/* Copy over the HTTP 200 response header and send it to the receiving client */
strcpy_P(AppData, HTTP200Header);
/* Check to see if a file extension was found for the requested filename */
/* Check to see if a MIME type for the requested file's extension was found */
if (Extension != NULL)
{
/* Look through the MIME type list, copy over the required MIME type if found */
@ -236,27 +237,25 @@ static void HTTPServerApp_SendMIMETypeHeader(void)
{
if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0)
{
MIMEHeaderLength = strlen(MIMETypes[i].MIMEType);
strncpy(AppData, MIMETypes[i].MIMEType, MIMEHeaderLength);
strcpy(&AppData[strlen(AppData)], MIMETypes[i].MIMEType);
FoundMIMEType = true;
break;
}
}
}
/* Check if a MIME type was found and copied to the output buffer */
if (!(MIMEHeaderLength))
if (!(FoundMIMEType))
{
/* MIME type not found - copy over the default MIME type */
MIMEHeaderLength = strlen_P(DefaultMIMEType);
strncpy_P(AppData, DefaultMIMEType, MIMEHeaderLength);
strcpy_P(&AppData[strlen(AppData)], DefaultMIMEType);
}
/* Add the end-of line terminator and end-of-headers terminator after the MIME type */
strncpy(&AppData[MIMEHeaderLength], "\r\n\r\n", sizeof("\r\n\r\n"));
MIMEHeaderLength += (sizeof("\r\n\r\n") - 1);
strcpy(&AppData[strlen(AppData)], "\r\n\r\n");
/* Send the MIME header to the receiving client */
uip_send(AppData, MIMEHeaderLength);
uip_send(AppData, strlen(AppData));
/* When the MIME header is ACKed, progress to the data send stage */
AppState->HTTPServer.NextState = WEBSERVER_STATE_SendData;
@ -270,22 +269,16 @@ static void HTTPServerApp_SendData(void)
uip_tcp_appstate_t* const AppState = &uip_conn->appstate;
char* const AppData = (char*)uip_appdata;
/* Must determine the maximum segment size to determine maximum file chunk size - never send a completely
* full packet, as this will cause some hosts to start delaying ACKs until a non-full packet is received.
* since uIP only allows one packet to be in transit at a time, this would cause long delays between packets
* until the host times out and sends the ACK for the last received packet.
*/
uint16_t MaxSegmentSize = (uip_mss() >> 1);
/* Return file pointer to the last ACKed position */
f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.ACKedFilePos);
/* Get the maximum segment size for the current packet */
uint16_t MaxChunkSize = uip_mss();
/* Read the next chunk of data from the open file */
f_read(&AppState->HTTPServer.FileHandle, AppData, MaxSegmentSize, &AppState->HTTPServer.SentChunkSize);
f_read(&AppState->HTTPServer.FileHandle, AppData, MaxChunkSize, &AppState->HTTPServer.SentChunkSize);
/* Send the next file chunk to the receiving client */
uip_send(AppData, AppState->HTTPServer.SentChunkSize);
/* Check if we are at the last chunk of the file, if so next ACK should close the connection */
AppState->HTTPServer.NextState = (MaxSegmentSize != AppState->HTTPServer.SentChunkSize) ? WEBSERVER_STATE_Closing : WEBSERVER_STATE_SendData;
if (MaxChunkSize != AppState->HTTPServer.SentChunkSize)
AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing;
}

@ -51,7 +51,6 @@
{
WEBSERVER_STATE_OpenRequestedFile, /**< Currently opening requested file */
WEBSERVER_STATE_SendResponseHeader, /**< Currently sending HTTP response headers to the client */
WEBSERVER_STATE_SendMIMETypeHeader, /**< Currently sending HTTP MIME type header to the client */
WEBSERVER_STATE_SendData, /**< Currently sending HTTP page data to the client */
WEBSERVER_STATE_Closing, /**< Ready to close the connection to the client */
WEBSERVER_STATE_Closed, /**< Connection closed after all data sent */
@ -76,7 +75,6 @@
#if defined(INCLUDE_FROM_HTTPSERVERAPP_C)
static void HTTPServerApp_OpenRequestedFile(void);
static void HTTPServerApp_SendResponseHeader(void);
static void HTTPServerApp_SendMIMETypeHeader(void);
static void HTTPServerApp_SendData(void);
#endif

@ -52,7 +52,7 @@ void uIPManagement_Init(void)
{
/* uIP Timing Initialization */
clock_init();
timer_set(&ConnectionTimer, CLOCK_SECOND / 10);
timer_set(&ConnectionTimer, CLOCK_SECOND / 5);
timer_set(&ARPTimer, CLOCK_SECOND * 10);
/* uIP Stack Initialization */
@ -153,7 +153,7 @@ static void uIPManagement_ProcessIncomingPacket(void)
/* Add destination MAC to outgoing packet */
uip_arp_out();
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
uip_split_output();
}
break;
@ -163,7 +163,7 @@ static void uIPManagement_ProcessIncomingPacket(void)
/* If a response was generated, send it */
if (uip_len > 0)
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
uip_split_output();
break;
}
@ -186,7 +186,8 @@ static void uIPManagement_ManageConnections(void)
/* Add destination MAC to outgoing packet */
uip_arp_out();
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
/* Split and send the outgoing packet */
uip_split_output();
}
}
@ -208,7 +209,8 @@ static void uIPManagement_ManageConnections(void)
/* Add destination MAC to outgoing packet */
uip_arp_out();
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
/* Split and send the outgoing packet */
uip_split_output();
}
}
@ -224,7 +226,8 @@ static void uIPManagement_ManageConnections(void)
/* Add destination MAC to outgoing packet */
uip_arp_out();
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
/* Split and send the outgoing packet */
uip_split_output();
}
}
#endif

@ -41,6 +41,7 @@
#include <uip.h>
#include <uip_arp.h>
#include <uip-split.h>
#include <timer.h>
#include "Lib/DHCPClientApp.h"

@ -0,0 +1,145 @@
/*
* Copyright (c) 2004, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: uip-split.c,v 1.2 2008/10/14 13:39:12 julienabeille Exp $
*/
#include "uip-split.h"
#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
/*-----------------------------------------------------------------------------*/
void
uip_split_output(void)
{
#if UIP_TCP
u16_t tcplen, len1, len2;
/* We only try to split maximum sized TCP segments. */
if(BUF->proto == UIP_PROTO_TCP && uip_len == UIP_BUFSIZE) {
tcplen = uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN;
/* Split the segment in two. If the original packet length was
odd, we make the second packet one byte larger. */
len1 = len2 = tcplen / 2;
if(len1 + len2 < tcplen) {
++len2;
}
/* Create the first packet. This is done by altering the length
field of the IP header and updating the checksums. */
uip_len = len1 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
#if UIP_CONF_IPV6
/* For IPv6, the IP length field does not include the IPv6 IP header
length. */
BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
BUF->len[0] = (uip_len - UIP_LLH_LEN) >> 8;
BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
#endif /* UIP_CONF_IPV6 */
/* Recalculate the TCP checksum. */
BUF->tcpchksum = 0;
BUF->tcpchksum = ~(uip_tcpchksum());
#if !UIP_CONF_IPV6
/* Recalculate the IP checksum. */
BUF->ipchksum = 0;
BUF->ipchksum = ~(uip_ipchksum());
#endif /* UIP_CONF_IPV6 */
/* Transmit the first packet. */
/* uip_fw_output();*/
#if UIP_CONF_IPV6
tcpip_ipv6_output();
#else
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
#endif /* UIP_CONF_IPV6 */
/* Now, create the second packet. To do this, it is not enough to
just alter the length field, but we must also update the TCP
sequence number and point the uip_appdata to a new place in
memory. This place is detemined by the length of the first
packet (len1). */
uip_len = len2 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
#if UIP_CONF_IPV6
/* For IPv6, the IP length field does not include the IPv6 IP header
length. */
BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
BUF->len[0] = (uip_len - UIP_LLH_LEN) >> 8;
BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
#endif /* UIP_CONF_IPV6 */
/* uip_appdata += len1;*/
memcpy(uip_appdata, (u8_t *)uip_appdata + len1, len2);
uip_add32(BUF->seqno, len1);
BUF->seqno[0] = uip_acc32[0];
BUF->seqno[1] = uip_acc32[1];
BUF->seqno[2] = uip_acc32[2];
BUF->seqno[3] = uip_acc32[3];
/* Recalculate the TCP checksum. */
BUF->tcpchksum = 0;
BUF->tcpchksum = ~(uip_tcpchksum());
#if !UIP_CONF_IPV6
/* Recalculate the IP checksum. */
BUF->ipchksum = 0;
BUF->ipchksum = ~(uip_ipchksum());
#endif /* UIP_CONF_IPV6 */
/* Transmit the second packet. */
/* uip_fw_output();*/
#if UIP_CONF_IPV6
tcpip_ipv6_output();
#else
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
//tcpip_output();
#endif /* UIP_CONF_IPV6 */
return;
}
#endif /* UIP_TCP */
/* uip_fw_output();*/
#if UIP_CONF_IPV6
tcpip_ipv6_output();
#else
RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
#endif /* UIP_CONF_IPV6 */
}
/*-----------------------------------------------------------------------------*/

@ -0,0 +1,103 @@
/*
* Copyright (c) 2004, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: uip-split.h,v 1.1 2006/06/17 22:41:19 adamdunkels Exp $
*/
/**
* \addtogroup uip
* @{
*/
/**
* \defgroup uipsplit uIP TCP throughput booster hack
* @{
*
* The basic uIP TCP implementation only allows each TCP connection to
* have a single TCP segment in flight at any given time. Because of
* the delayed ACK algorithm employed by most TCP receivers, uIP's
* limit on the amount of in-flight TCP segments seriously reduces the
* maximum achievable throughput for sending data from uIP.
*
* The uip-split module is a hack which tries to remedy this
* situation. By splitting maximum sized outgoing TCP segments into
* two, the delayed ACK algorithm is not invoked at TCP
* receivers. This improves the throughput when sending data from uIP
* by orders of magnitude.
*
* The uip-split module uses the uip-fw module (uIP IP packet
* forwarding) for sending packets. Therefore, the uip-fw module must
* be set up with the appropriate network interfaces for this module
* to work.
*/
/**
* \file
* Module for splitting outbound TCP segments in two to avoid the
* delayed ACK throughput degradation.
* \author
* Adam Dunkels <adam@sics.se>
*
*/
#ifndef __UIP_SPLIT_H__
#define __UIP_SPLIT_H__
#include <string.h>
#include <uip.h>
#include "../../USBHostMode.h"
#include <LUFA/Drivers/USB/Class/RNDIS.h>
/**
* Handle outgoing packets.
*
* This function inspects an outgoing packet in the uip_buf buffer and
* sends it out using the uip_fw_output() function. If the packet is a
* full-sized TCP segment it will be split into two segments and
* transmitted separately. This function should be called instead of
* the actual device driver output function, or the uip_fw_output()
* function.
*
* The headers of the outgoing packet is assumed to be in the uip_buf
* buffer and the payload is assumed to be wherever uip_appdata
* points. The length of the outgoing packet is assumed to be in the
* uip_len variable.
*
*/
void uip_split_output(void);
void uip_add32(u8_t *op32, u16_t op16);
#endif /* __UIP_SPLIT_H__ */
/** @} */
/** @} */

@ -138,6 +138,7 @@ SRC = $(TARGET).c \
Lib/uip/uip_arp.c \
Lib/uip/timer.c \
Lib/uip/clock.c \
Lib/uip/uip-split.c \
Lib/FATFs/diskio.c \
Lib/FATFs/ff.c \
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \

Loading…
Cancel
Save