Microchip® Advanced Software Framework

BasicWEB.c File Reference
#include <stdio.h>
#include <string.h>
#include "conf_eth.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "serial.h"
#include "portmacro.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/init.h"
#include "ctrl_access.h"
#include "fsaccess.h"
#include "config_file.h"
#include "ethernet.h"
#include "supervisor.h"
#include "shell.h"
#include "datalog.h"
#include "defaultpage.htm.h"
#include "tracedump.h"
#include "BasicWEB.h"

Macros

#define ERR_BUFFER_SIZE   512
 
#define ERR_DIR   "errors"
 
#define HTTP_CONFIG_FILE   "A:/CFG/http.txt"
 
#define webCONN_TIMEOUT   5000
 
#define webHTTP_CONNECTION_PRIORITY   (CTRLPANEL_WEB_SERVER_PRIORITY+1)
 
#define webHTTP_CONNECTION_STACK_SIZE   384
 
#define webHTTP_NB_CONN   1
 
#define webHTTP_PORT   ( 80 )
 
#define webMAX_DATA_TO_SEND   webNB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR
 
#define webNB_SECTOR_TO_SEND   4
 
#define webSERVER_FOLDER   "A:/WEB/"
 
#define webSERVER_PROTOCOL   "HTTP/1.1"
 
#define webSERVER_SOFTWARE   "AVR32 UC3 WEB SERVER"
 
#define webSHORT_DELAY   ( 10 )
 

Typedefs

typedef ssize_t(* pf_read_src )(int fd, void *buf, size_t count)
 

Functions

eExecStatus e_webserver_cmd_get_config (eModId xModId, signed short FsNavId, int ac, signed portCHAR *av[], signed portCHAR **ppcStringReply)
 The get webserver config command: get the config fields value of http port Takes no parameter. More...
 
eExecStatus e_webserver_cmd_set_config (eModId xModId, signed short FsNavId, int ac, signed portCHAR *av[], signed portCHAR **ppcStringReply)
 The set webserver config command: set the http port. Takes one parameter : port=ppppp. More...
 
 portTASK_FUNCTION (vBasicWEBServer, pvParameters)
 WEB server main task check for incoming connection and process it. More...
 
 portTASK_FUNCTION (prvweb_ProcessSingleConnection, pvParameters)
 process an incoming connection parse the request and close/delete the connection More...
 
 portTASK_FUNCTION_PROTO (prvweb_ProcessSingleConnection, pvParameters)
 
static unsigned portLONG prulweb_BuildErrorBody (portCHAR *response, int s, char *title, const char *text)
 build the static error page body More...
 
static unsigned portLONG prulweb_BuildErrorTail (portCHAR *response)
 build the static error page tail More...
 
static unsigned portLONG prulweb_BuildHeaders (portCHAR *response, int s, char *title, char *extra_header, char *me, char *mt)
 build the HTTP header More...
 
static ssize_t prv_read_default_page (int fd, void *buf, size_t count)
 Copy part of the Default html page to another buffer. More...
 
static void prvweb_ParseCGIRequest (struct netconn *pxNetCon, char *request)
 parse the CGI request and send it to the shell More...
 
static void prvweb_ParseHTMLRequest (struct netconn *pxNetCon)
 parse the incoming request parse the HTML request and send file or execute CGI request More...
 
static void prvweb_SendErrorPage (struct netconn *pxNetCon, int s, char *extra_header, const char *text)
 send the static error page More...
 
static void prvweb_SendFile (struct netconn *pxNetCon, char *filename)
 parse the file request and send the requested file to the host More...
 

Variables

static portSHORT sCurrentNbHTTPConn = 0
 
xTaskHandle tTaskHandle [webHTTP_NB_CONN] = { NULL }
 
static unsigned int webHttpPort = 0
 
static size_t x_default_page_len
 
xSemaphoreHandle xCFGMutex
 
static xSemaphoreHandle xMutexNbHTTPConn
 
xSemaphoreHandle xWEBMutex
 

#define ERR_BUFFER_SIZE   512

The error buffer size for error page

Referenced by prvweb_SendErrorPage().

#define ERR_DIR   "errors"
#define HTTP_CONFIG_FILE   "A:/CFG/http.txt"

The config file location

Referenced by e_webserver_cmd_set_config(), and portTASK_FUNCTION().

#define webCONN_TIMEOUT   5000

The max timeout before killing all pending tasks (ms)

#define webHTTP_CONNECTION_PRIORITY   (CTRLPANEL_WEB_SERVER_PRIORITY+1)

The priority of a http connection.

Referenced by portTASK_FUNCTION().

#define webHTTP_CONNECTION_STACK_SIZE   384

The stack size of a http connection task

Referenced by portTASK_FUNCTION().

#define webHTTP_NB_CONN   1

The max number of simultaneous http connection.

Referenced by portTASK_FUNCTION().

#define webHTTP_PORT   ( 80 )

The port on which we listen.

Referenced by portTASK_FUNCTION().

#define webMAX_DATA_TO_SEND   webNB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR

The max amount of fat data to try to send

Referenced by prv_read_default_page(), and prvweb_SendFile().

#define webNB_SECTOR_TO_SEND   4

The max number of sector to try to send

Referenced by prvweb_SendFile().

#define webSERVER_FOLDER   "A:/WEB/"

The folder were server files are

Referenced by prvweb_ParseHTMLRequest().

#define webSERVER_PROTOCOL   "HTTP/1.1"

The server protocol.

Referenced by prulweb_BuildHeaders().

#define webSERVER_SOFTWARE   "AVR32 UC3 WEB SERVER"

The server software name.

Referenced by prulweb_BuildErrorTail(), and prulweb_BuildHeaders().

#define webSHORT_DELAY   ( 10 )

Delay on close error.

Referenced by portTASK_FUNCTION().

typedef ssize_t(* pf_read_src)(int fd, void *buf, size_t count)

eExecStatus e_webserver_cmd_get_config ( eModId  xModId,
signed short  FsNavId,
int  ac,
signed portCHAR *  av[],
signed portCHAR **  ppcStringReply 
)

The get webserver config command: get the config fields value of http port Takes no parameter.

Note
This function must be of the type pfShellCmd defined by the shell module.
Parameters
xModIdInput. The module that is calling this function.
FsNavIdIgnored.
acInput. The argument counter. For this command, should be 1.
avInput. The argument vector.
ppcStringReplyInput/Output. The response string. If Input is NULL, no response string will be output. Else a malloc for the response string is performed here; the caller must free this string.
Returns
the status of the command execution.

References NULL, pvPortMalloc(), SHELL_ERRMSG_MEMALLOC, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, sprintf(), and webHttpPort.

Referenced by e_syscmds_cmd_get_config().

eExecStatus e_webserver_cmd_set_config ( eModId  xModId,
signed short  FsNavId,
int  ac,
signed portCHAR *  av[],
signed portCHAR **  ppcStringReply 
)

The set webserver config command: set the http port. Takes one parameter : port=ppppp.

Note
This function must be of the type pfShellCmd defined by the shell module.
Parameters
xModIdInput. The module that is calling this function.
FsNavIdIgnored.
acInput. The argument counter. For this command, should be 1.
avInput. The argument vector.
ppcStringReplyInput/Output. The response string. If Input is NULL, no response string will be output. Else a malloc for the response string is performed here; the caller must free this string.
Returns
the status of the command execution.

References config_file_set_value(), HTTP_CONFIG_FILE, NULL, pvPortMalloc(), SHELL_ERRMSG_CONFIGERROR, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, and SHELL_MSG_REBOOT.

Referenced by e_syscmds_cmd_set_config().

portTASK_FUNCTION ( prvweb_ProcessSingleConnection  ,
pvParameters   
)

process an incoming connection parse the request and close/delete the connection

Note
Input. The netconn to use to send and receive data.

References ERR_OK, netconn_close(), netconn_delete(), NULL, pdTRUE, prvweb_ParseHTMLRequest(), sCurrentNbHTTPConn, tTaskHandle, vTaskDelay(), vTaskDelete(), webHTTP_NB_CONN, webSHORT_DELAY, x_supervisor_SemaphoreGive(), xSemaphoreGive, xSemaphoreTake, and xTaskGetCurrentTaskHandle().

portTASK_FUNCTION_PROTO ( prvweb_ProcessSingleConnection  ,
pvParameters   
)
static unsigned portLONG prulweb_BuildErrorBody ( portCHAR *  response,
int  s,
char *  title,
const char *  text 
)
static

build the static error page body

Parameters
responseInput/Output. the buffer to store data.
sInput. The error code.
titleInput. title of the error page.
textInput. text to display in the error page.

References sprintf().

Referenced by prvweb_SendErrorPage().

static unsigned portLONG prulweb_BuildErrorTail ( portCHAR *  response)
static

build the static error page tail

Parameters
responseInput/Output. the buffer to store data.

References sprintf(), vEthernetGetIPAddr(), and webSERVER_SOFTWARE.

Referenced by prvweb_SendErrorPage().

static unsigned portLONG prulweb_BuildHeaders ( portCHAR *  response,
int  s,
char *  title,
char *  extra_header,
char *  me,
char *  mt 
)
static

build the HTTP header

Parameters
responseInput/Output. the buffer to store data.
sInput. The page code (200, 404, etc).
titleInput. title of the page.
extra_headerInput. text to display in the error page.
meInput. MIME encoding, if used.
mtInput. MIME type if used.

References sprintf(), webSERVER_PROTOCOL, and webSERVER_SOFTWARE.

Referenced by prvweb_ParseCGIRequest(), prvweb_SendErrorPage(), and prvweb_SendFile().

static ssize_t prv_read_default_page ( int  fd,
void buf,
size_t  count 
)
static

Copy part of the Default html page to another buffer.

Parameters
fdInput. file descriptor. Ignored.
bufInput/ouput. Destination memory ptr.
countInput. Amount of bytes to read.
Returns
amount of data read (or -1 if error)

References FS_SIZE_OF_SECTOR, pcDefaultPage, webMAX_DATA_TO_SEND, and x_default_page_len.

Referenced by prvweb_SendFile().

static void prvweb_ParseCGIRequest ( struct netconn pxNetCon,
char *  request 
)
static

parse the CGI request and send it to the shell

Parameters
pxNetConInput. The netconn to use to send and receive data.
requestInput. The incoming CGI request.

References NAKED_TRACE_COM2, NETCONN_COPY, netconn_write(), NULL, prulweb_BuildHeaders(), pvPortMalloc(), Shell_exec(), SHELL_EXECSTATUS_OK, SYS_MODID_HTTP, and vPortFree().

Referenced by prvweb_ParseHTMLRequest().

static void prvweb_ParseHTMLRequest ( struct netconn pxNetCon)
static

parse the incoming request parse the HTML request and send file or execute CGI request

Parameters
pxNetConInput. The netconn to use to send and receive data.

References ERR_OK, MAX_FILE_PATH_LENGTH, NAKED_TRACE_COM2, netbuf_data(), netbuf_delete(), netconn_recv(), NULL, prvweb_ParseCGIRequest(), prvweb_SendErrorPage(), prvweb_SendFile(), pvPortMalloc(), sprintf(), vPortFree(), and webSERVER_FOLDER.

Referenced by portTASK_FUNCTION().

static void prvweb_SendErrorPage ( struct netconn pxNetCon,
int  s,
char *  extra_header,
const char *  text 
)
static

send the static error page

Parameters
pxNetConInput. The netconn to use to send and receive data.
sInput. The error code.
extra_headerInput. data to push in the error answer.
textInput. text to display in the error page.

References ERR_BUFFER_SIZE, NAKED_TRACE_COM2, NETCONN_COPY, netconn_write(), NULL, prulweb_BuildErrorBody(), prulweb_BuildErrorTail(), prulweb_BuildHeaders(), pvPortMalloc(), and vPortFree().

Referenced by portTASK_FUNCTION(), prvweb_ParseHTMLRequest(), and prvweb_SendFile().

static void prvweb_SendFile ( struct netconn pxNetCon,
char *  filename 
)
static

parse the file request and send the requested file to the host

Parameters
pxNetConInput. The netconn to use to send and receive data.
filenameInput. The incoming filename.

References close, FS_SIZE_OF_SECTOR, fsaccess_file_get_size(), NAKED_TRACE_COM2, NETCONN_COPY, netconn_write(), NULL, O_RDONLY, open(), prulweb_BuildHeaders(), prv_read_default_page(), prvweb_SendErrorPage(), pvPortMalloc(), read, vPortFree(), webMAX_DATA_TO_SEND, webNB_SECTOR_TO_SEND, and x_default_page_len.

Referenced by prvweb_ParseHTMLRequest().

portSHORT sCurrentNbHTTPConn = 0
static

Current nb of HTTP connections.

Referenced by portTASK_FUNCTION().

xTaskHandle tTaskHandle[webHTTP_NB_CONN] = { NULL }

The handlers for connection tasks.

Referenced by portTASK_FUNCTION().

unsigned int webHttpPort = 0
static

HTTP port for server.

Referenced by e_webserver_cmd_get_config(), and portTASK_FUNCTION().

size_t x_default_page_len
static

The size of the default contant html page.

Referenced by portTASK_FUNCTION(), prv_read_default_page(), and prvweb_SendFile().

xSemaphoreHandle xCFGMutex

The CFG system mutex.

xSemaphoreHandle xMutexNbHTTPConn
static

Mutex to access the current_nb_http_conn variable.

xSemaphoreHandle xWEBMutex

The Web server system mutex.