/* ------------------------------------------------------------------------------------------------ */
/* FILE :		WebServer.h																			*/
/*																									*/
/* CONTENTS :																						*/
/*				Code for the lwIP webserver with netconn / BSD sockets.								*/
/*				This file contains all the webservices management.									*/
/*																									*/
/*																									*/
/* Copyright (c) 2013-2014, Code-Time Technologies Inc. All rights reserved.						*/
/*																									*/
/* Code-Time Technologies retains all right, title, and interest in and to this work				*/
/*																									*/
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS							*/
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF										*/
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL							*/
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR								*/
/* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,							*/
/* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR							*/
/* OTHER DEALINGS IN THE SOFTWARE.																	*/
/*																									*/
/*																									*/
/*	$Revision: 1.8 $																				*/
/*	$Date: 2013/10/03 15:55:16 $																	*/
/*																									*/
/* ------------------------------------------------------------------------------------------------ */

#ifndef __WEBSERVER_H__
#define __WEBSERVER_H__


#include <stdio.h>
#include <string.h>
#include "stm32f2xx.h"
#include "stm322xg_eval.h"
#include "stm32f2x7_eth.h"
#include "stm32f2x7_eth_bsp.h"
#include "stm322xg_eval_lcd.h"
#include "stm322xg_eval_ioe.h"

#include "NetAddr.h"
#ifndef OS_CMSIS_RTOS
  #include "Abassi.h"
#else
  #include "cmsis_os.h"
#endif
#if (((OS_DEMO) == 10) || ((OS_DEMO) == 11) || ((OS_DEMO) == 110) || ((OS_DEMO) == 111))
  #include "fs.h"									/* Only needed with in-memory file system		*/
#endif

#ifdef __cplusplus
 extern "C" {
#endif

#ifndef WEBS_PRIO
 #ifndef osCMSIS
  #define WEBS_PRIO					(OX_PRIO_MIN-1)	/* Priority of the HTTP webserver task			*/
 #else
  #define WEBS_PRIO					osPriorityLow
 #endif
#endif
#ifndef WEBS_STACKSIZE
  #define WEBS_STACKSIZE			2048			/* Stack size of the HTTP webserver task		*/
#endif
#ifndef WEBS_MAX_CGI
  #define WEBS_MAX_CGI				8				/* Maximum number of different CGIs handlers	*/
#endif
#ifndef WEBS_MAX_POST
  #define WEBS_MAX_POST				8				/* Maximum number of different POSTs handlers	*/
#endif
#ifndef WEBS_CGI_POST_PARAM
  #define WEBS_MAX_CGI_POST_PARAM	10				/* Maximum number of Param=Value in a request	*/
#endif
#ifndef WEBS_MAX_VARIABLE
  #define WEBS_MAX_VARIABLE			48				/* Maximum number of server variables for SSI	*/
#endif
#ifndef WEBS_MAX_SSI
  #define WEBS_MAX_SSI				16				/* Maximum number of SSI update functions		*/
#endif

/* ------------------------------------------------------------------------------------------------ */

typedef u16_t       (*SSIhandler_t)(int Index, char *Insert, int InsertLen);
typedef const char *(*CGIhandler_t)(int NumParams, char *Param[], char *Value[]);

typedef struct {
    const char  *Name;
    CGIhandler_t Handler;
} CGI_t;

enum VerbTypes {V_GET,   V_HEAD,    V_POST,    V_PUT,   V_DELETE,
                V_TRACE, V_OPTIONS, V_CONNECT, V_PATCH, V_UNKNOWN_};

struct Methods {									/* Association of text to verbs					*/
	char          *Text;
	enum VerbTypes Verb;
};

struct LocalVar {									/* Local variable information holding			*/
	char VarName[32];								/* Name of the variable							*/
	char Value[32];									/* Value of the variable: always text			*/
	char *(*Command)(char *);    					/* Command associated when is an exec variable	*/
};

#if (((OS_DEMO) == 10) || ((OS_DEMO) == 11) || ((OS_DEMO) == 110) || ((OS_DEMO) == 111))
  typedef struct fs_file	*FILE_DSC_t;
  #define F_INIT_FDSC(x)	((x)=NULL)
  #define F_MNT()                               0
  #define F_OPEN(F_Dsc, F_Name, F_Access)		(NULL != ((F_Dsc)=fs_open(F_Name)))
  #define F_CLOSE(F_Dsc)						fs_close(F_Dsc)
  #define F_READ(F_Dsc, Buf, BufSize, Nread)												\
						if (-1 == (*(Nread)=fs_read((F_Dsc), (char *)Buf, (BufSize)))) {	\
							*(Nread) = 0;													\
						}
  #define FA_READ								0
  #define F_PATCH_OPEN(F_Dsc)					do {(F_Dsc)->index = 0;} while(0)
#endif
#if (((OS_DEMO) == 12) || ((OS_DEMO) == 13) || ((OS_DEMO) == 112) || ((OS_DEMO) == 113))
  #include "ff.h"
  typedef FIL				FILE_DSC_t;
  #define F_INIT_FDSC(x)
  #define F_MNT()                             f_mount(&g_FileSys, "0", 1)
  #define F_OPEN(F_Dsc, F_Name, F_Access)	  (FR_OK == (f_open(&(F_Dsc),(F_Name), (F_Access))))
  #define F_CLOSE(F_Dsc)					  f_close(&(F_Dsc))
  #define F_READ(F_Dsc, Buf, BufSize, Nread)  f_read(&(F_Dsc),(void *)(Buf),(BufSize),(UINT *)(Nread))
  #define F_PATCH_OPEN(F_Dsc)                 do{int _=0;_=_;}while(0)
#endif

#if (USE_UART)
  #define DISPLAY(Line, Txt) 	do {																\
									LCD_DisplayStringLine(Line, (uint8_t*)(Txt));					\
									puts(Txt);														\
								} while (0);
#else
  #define DISPLAY(Line, Txt) 	do {																\
									LCD_DisplayStringLine(Line, (uint8_t*)(Txt));					\
								} while (0);
#endif
#define LCD_LINE(Line, Txt) 	do {																\
									LCD_DisplayStringLine(Line, (uint8_t*)(Txt));					\
								} while (0);

#define MY_LED_0		LED1
#define MY_LED_1		LED2
#define MY_LED_2		LED3
#define MY_LED_3		LED4

#define MY_BUTTON_0	BUTTON_WAKEUP
#define MY_BUTTON_1	BUTTON_TAMPER
#define MY_BUTTON_2	BUTTON_KEY

#ifndef osCMSIS										/* stdio mutex operations						*/
  #define MTXLOCK_STDIO()		MTXlock(G_OSmutex, -1)
  #define MTXUNLOCK_STDIO()		MTXunlock(G_OSmutex)
#else
  #define MTXLOCK_STDIO()		osMutexWait(G_OSmutex, 0)
  #define MTXUNLOCK_STDIO()		osMutexRelease(G_OSmutex)
#endif


/* ------------------------------------------------------------------------------------------------ */

void  CGIinit(void);
char *CGIprocess(char *buf);
int   CGInewHandler(const char *Fname, const CGIhandler_t Handler);
void  http_server_init(void);
void  LCDputs(char *Line_1, char *Line_2);
void  POSTinit(void);
int   POSTnewHandler(const char *Fname, const CGIhandler_t Handler);
char *POSTprocess(char *uri, char *Data);
int   SSIexpand(char *buf, const char *uri);
char *SSIgetVar(const char *Name);
void  SSIinit(void);
int   SSInewUpdate(void (*FctPtr)(void));
int   SSInewVar(const char *Name, char *Value, char *(*Command)(char *));
int   SSIvarUpdate(void);
void  WebServerInit(void);

#ifdef __cplusplus
}
#endif

#endif

/* EOF */

