⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssl_mboxdemo.c

📁 Zword公司的Rabbit2000系列相关文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************
   mboxdemo.c
   Z-World, 2003

	This program is used with RCM3700 series controllers
	with prototyping boards.

   This program has been modified to use SSL via HTTPS.
   See the SSL Walkthrough for information on creating
   certificates for SSL programs.

	An optional LCD/Keypad Module is required to run this
	program.  #define PORTA_AUX_IO at top of program to
	enable external I/O bus for LCD/Keypad operations.

	Description
	===========
   This program implements a WEB server that allows email messages to
   be entered that are then shown on the LCD display.  The keypad
   allows the user to scroll within messages, flip to other emails,
   mark messages as read, and delete emails.  When a new email
   arrives an LED (on the proto-board and LCD/Keypad Module) turns
   on and turns back off once the message has been marked as read.
   A log of all email actions is kept, which can be displayed in the
   WEB browser.  All current emails can also be read with the web
   browser.

 	(Adapted from Intellicom's mboxdemo.c)

	Instructions
	============
	1. Make changes in the configuration section below to match
		your requirements.
	2. Compile and run this program.
	3. Through the created web page you can read, send or view
		messages.
	4. The controller display will indicate if you have mail and
		you can do the following by using the keypad to:

			View Next Message, press Left Arrow key
			Scroll Up, press Up Arrow key
			Scroll Down, press Down Arrow key
			View Previous Message, press Right Arrow key
			Read Message, press Minus key
			Delete Message, press Plus key
			Ignore Message, press Enter key

********************************************************************/
#class auto
#define PORTA_AUX_IO		//required to run LCD/Keypad for this demo

// So brdInit() can distinguish from RCM3720 proto-board
#define RCM3700_PROTOBOARD

fontInfo fi6x8;
windowFrame wholewindow;

/***********************************
 * Configuration Section           *
 * ---------------------           *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/
/*
 * Pick the predefined TCP/IP configuration for this sample.  See
 * LIB\TCPIP\TCP_CONFIG.LIB for instructions on how to set the
 * configuration.
 */
#define TCPCONFIG 1

/*
 * TCP/IP modification - reduce TCP socket buffer
 * size, to allow more connections. This can be increased,
 * with increased performance, if the number of sockets
 * are reduced.  Note that this buffer size is split in
 * two for TCP sockets--1024 bytes for send and 1024 bytes
 * for receive.
 */
#define TCP_BUF_SIZE 2048

/*
 * Web server configuration
 */

/*
 * Define the number of HTTP servers and socket buffers.
 * With tcp_reserveport(), fewer HTTP servers are needed.
 */
#define HTTP_MAXSERVERS 1
#define MAX_TCP_SOCKET_BUFFERS 1

// This macro determines the number of HTTP servers that will use
// SSL (HTTPS servers). In this case, we have 1 server, and
// this defines it to be HTTPS
#define HTTP_SSL_SOCKETS 1

// SSL Stuff
// This macro tells the HTTP library to use SSL
#define USE_HTTP_SSL

// Import the certificate
#ximport "cert/mycert.dcc" SSL_CERTIFICATE

/*
 * Our web server as seen from the clients.
 * This should be the address that the clients (netscape/IE)
 * use to access your server. Usually, this is your IP address.
 * If you are behind a firewall, though, it might be a port on
 * the proxy, that will be forwarded to the Rabbit board. The
 * commented out line is an example of such a situation.
 */
#define REDIRECTHOST		_PRIMARY_STATIC_IP
// #define REDIRECTHOST	"my.host.com:8080"


/*
 * The following values can customize the maximum number of emails,
 * the maximum sizes of buffers, etc.
 */
#define MAX_EMAILS 25				// Maximum number of emails
#define MAX_FROM_LEN 41				// Maximum length of From line
#define MAX_SUBJECT_LEN 41			// Maximum length of Subject line
#define MAX_BODY_LEN 1000			// Maximum length of Body
#define DISP_BUFFER_LINES 100		// Maximum number of lines in display buffer
#define DISP_ROWS 4					// Number of rows on the LCD
#define DISP_COLS 20					// Number of cols on the LCD
#define LOG_BUFFER_SIZE 1000		// Size of the log buffer


/********************************
 * End of configuration section *
 ********************************/

// We need to make sure we are redirecting to HTTPS, not HTTP
#define REDIRECTTO      "https://" REDIRECTHOST "/index.html"

#memmap xmem
#use "dcrtcp.lib"
#use "http.lib"

#ximport "pages/main.html"		   index_html
#ximport "pages/enter.html"		enter_html

/* the default for / must be first */
SSPEC_MIMETABLE_START
	SSPEC_MIME_FUNC(".shtml", "text/html", shtml_handler),
	SSPEC_MIME(".html", "text/html"),
	SSPEC_MIME(".gif", "image/gif"),
	SSPEC_MIME(".cgi", "")
SSPEC_MIMETABLE_END

/*
 * The following structure is used to parse data returned from a FORM
 */
typedef struct {
	char *name;
	char *value;
	int len;
} FORMType;
FORMType FORMSpec[3];

/*
 * This structure is a node in a doubly linked list of emails
 */
struct EmailIndex {
	int prev;
	int next;
	int used;
	int read;
};
struct EmailIndex emailIndex[MAX_EMAILS];

/*
 * This structure keeps track of the head, tail, and current email in the
 * email list
 */
struct EmailList {
	int head;
	int tail;
	int ptr;
};
struct EmailList emailList;

int emailDispLine;	// Current line of email being displayed
int emailLastLine;	// Last line of email being displayed
int flag;

/*
 * This structure holds an email
 */
typedef struct {
	char from[MAX_FROM_LEN];
	char subject[MAX_SUBJECT_LEN];
	char body[MAX_BODY_LEN];
} Email;
Email email;
Email emailTemp;

// Points to the email buffer in xmem
long emailBuffer;

// Points to the display buffer in xmem
long dispBuffer;

// Points to the log buffer in xmem
long logBuffer;
// Points to the current location in the log buffer
int logPtr;

#define FLAG_DOWN 0
#define FLAG_UP   1

/*
 * Initialize the mailbox flag to down position
 */
void FlagInit(void)
{
	flag = FLAG_DOWN;
	dispLedOut(0,0);   //red led off
}

/*
 * Put the mailbox flag up, if it isn't already
 */
void FlagChange(int flagState)
{
	if (flagState == FLAG_UP)
	{
		if (flag == FLAG_DOWN)
		{
			flag = FLAG_UP;
			dispLedOut(0,1);   //red led oon
		}
	}
	else
	{
		if (flag == FLAG_UP)
		{
			flag = FLAG_DOWN;
			dispLedOut(0,0);   //red led off
		}
	}
}

/*
 * Go through the email list and set the read flag appropriately
 */
void FlagCheck(void)
{
	int ptr;
	int status;

	status = FLAG_DOWN;

	if (emailList.head == -1) {
		FlagChange(FLAG_DOWN);
	}
	for (ptr = emailList.head; (ptr != -1) && (status == FLAG_DOWN); ptr = emailIndex[ptr].next) {
		if (emailIndex[ptr].read == 0) {
			status = FLAG_UP;
		}
	}
	FlagChange(status);
}

/*
 * Create an empty email list
 */
void CreateEmailList(void)
{
	int i;
	for (i = 0; i < MAX_EMAILS; i++) {
		emailIndex[i].used = 0;
		emailIndex[i].read = 0;
		emailIndex[i].prev = -1;
		emailIndex[i].next = -1;
	}

	emailList.head = -1;
	emailList.tail = -1;
	emailList.ptr = -1;

	emailBuffer = xalloc(sizeof(Email) * MAX_EMAILS);
}

/*
 * Initialize the display buffer
 */
void DispBufferInit(void)
{
	dispBuffer = xalloc((DISP_COLS + 1) * DISP_BUFFER_LINES);
}

/*
 * Display the current window within the display buffer
 */
void DispUpdateWindow(void)
{
	int i;
	char line[DISP_COLS + 1];

	glBlankScreen();
	for (i = 0; i < DISP_ROWS; i++) {
		if ((emailDispLine + i) < emailLastLine) {
			TextGotoXY(&wholewindow, 0, i);
			xmem2root(line, dispBuffer + ((DISP_COLS+1) * (emailDispLine+i)), DISP_COLS+1);
			TextPrintf(&wholewindow, "%s", line);
		} else {
			TextGotoXY(&wholewindow, 0, i);
			TextPrintf(&wholewindow, "");
		}
	}
}

/*
 * Write out part of an email message in the display buffer (e.g., the
 * sender, the subject, the body...)
 */
void DispPart(char *src, int *lineNum, char *string) {
	char line[DISP_COLS + 1];
	int i;
	int j;

	strcpy(line, string);
	i = strlen(line);
	j = 0;
	while (src[j] != '\0') {
		while ((i < DISP_COLS) && (src[j] != '\n') && (src[j] != '\0')) {
			if (src[j] == '\r') {
				j++;
			} else {
				line[i] = src[j];
				i++;
				j++;
			}
		}
		line[i] = '\0';
		root2xmem(dispBuffer + ((DISP_COLS+1) * *lineNum), line, DISP_COLS+1);
		(*lineNum)++;

		if (src[j] == '\n') {
			j++;
			i = 0;
		} else if ((src[j] == '\r') && (src[j+1] == '\n')) {
			j += 2;
			i = 0;
		} else if (src[j] != '\0') {
			i = 0;
		}
	}
}

/*
 * Write out a "No email" message to the display buffer
 */
void DispNoEmail(void)
{
	int i;
	char line[DISP_COLS+1];

	strcpy(line, "No email");
	root2xmem(dispBuffer, line, strlen(line)+1);
	emailDispLine = 0;
	emailLastLine = 1;

	DispUpdateWindow();
}

/*
 * Write out an email message to the display buffer
 */
void DispEmail(void)
{
	int i;
	int j;
	char line[DISP_COLS + 1];
	int row;
	int col;
	int lineNum;

	lineNum = 0;
	emailDispLine = 0;

	if (emailList.ptr == -1)
	{
		DispNoEmail();
	}
	else
	{
		xmem2root(&email, emailBuffer + sizeof(Email)*emailList.ptr, sizeof(Email));
		DispPart(email.from, &lineNum, "FROM: ");
		DispPart(email.subject, &lineNum, "SUBJECT: ");
		strcpy(line, "BODY");
		root2xmem(dispBuffer + ((DISP_COLS+1) * lineNum), line, DISP_COLS+1);
		lineNum++;
		DispPart(email.body, &lineNum, "");

		line[0] = '\0';
		emailLastLine = lineNum;

		DispUpdateWindow();
	}
}

/*
 * Initialize the log buffer
 */
void LogInit(void)
{
	logBuffer = xalloc(LOG_BUFFER_SIZE);
	root2xmem(logBuffer, "\0", 1);
	logPtr = 0;
}

/*
 * Add an entry to the log
 */
void LogAddEntry(char *entry)
{
	int entrySize;
	entrySize = strlen(entry);
	if ((logPtr + entrySize) < (LOG_BUFFER_SIZE - 1)) {
		root2xmem(logBuffer + logPtr, entry, entrySize);
		root2xmem(logBuffer + logPtr + entrySize, "\n\0", 2);
		logPtr = logPtr + entrySize + 1;
	}
}

/*
 * Add an entry about email activity to the log
 */
void LogAddEmailEntry(char *message, char *from, char *subject)
{
	char line[100];
	strcpy(line, "Email ");
	strncat(line, message, 100 - strlen(line));
	strncat(line, " -- FROM: ", 100 - strlen(line));
	strncat(line, from, 100 - strlen(line));
	strncat(line, " -- SUBJECT: ", 100 - strlen(line));
	strncat(line, subject, 100 - strlen(line));
	LogAddEntry(line);
}

/*
 * Strip the encoding from the POST info
 */
void StripEncoding(char *dest, char *src, int len)
{
	int i;
	int j;
	char upper;
	char lower;
	char value;

	i = 0;
	j = 0;
	while( (i < len) && (src[i] != '\0') ) {
		if (src[i] == '+') {
			dest[j] = ' ';
		} else if (src[i] == '%') {
			if ((i + 2) >= len) {
				break;
			}
			upper = toupper(src[++i]);
			lower = toupper(src[++i]);

			if ((upper >= '0') && (upper <= '9')) {
				upper = upper - '0';
			} else if ((upper >= 'A') && (upper <= 'F')) {
				upper = upper - 'A' + 10;
			}
			if ((lower >= '0') && (lower <= '9')) {
				lower = lower - '0';
			} else if ((lower >= 'A') && (lower <= 'F')) {
				lower = lower - 'A' + 10;
			}

			value = (upper * 16) + lower;
			dest[j] = value;
		} else {
			dest[j] = src[i];
		}
		i++;
		j++;
	}
	dest[j] = '\0';
}

/*
 * Add an email to the email list
 */
int AddEmail(char *from, char *subject, char *body)
{
	int i;
	char line[100];

	if (emailList.head == -1) {
		emailList.head = 0;
		emailList.tail = 0;
		emailList.ptr = 0;
		emailIndex[emailList.head].prev = -1;
	} else {
		// Find an available slot
		for (i = 0; i < MAX_EMAILS; i++) {
			if (emailIndex[i].used == 0) {
				emailIndex[emailList.tail].next = i;
				emailIndex[i].prev = emailList.tail;
				emailList.tail = i;
				break;
			}
		}
		if (i == MAX_EMAILS) {
			// Failure--too full
			return -1;
		}
	}
	StripEncoding(email.from, from, MAX_FROM_LEN);
	StripEncoding(email.subject, subject, MAX_SUBJECT_LEN);
	StripEncoding(email.body, body, MAX_BODY_LEN);

	root2xmem(emailBuffer + sizeof(Email)*emailList.tail, &email, sizeof(Email));
	emailIndex[emailList.tail].next = -1;
	emailIndex[emailList.tail].used = 1;
	emailIndex[emailList.tail].read = 0;
	FlagChange(FLAG_UP);
	emailList.ptr = emailList.tail;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -