📄 main.c
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : main.c
* Programmer(s): Kim Wooyoul
* Created : 2002/05/28
* Modified : Date - 2002/10/28
* Description - All file revision
*
* Description : Simple HTTP Server & SMTP Program
* This is a simple HTTP server program using well-known port #80 and service web page to user's web browser.
* This server program service only default.html.
* This default.html includes a form to control LCD and LED on EVB8051.
* Default.html provide a form that includes fields to write mail server, account, mail title and content.
* If you write the contents and summit on web brower, EVB8051 will extract and analyze the field values
* and try to connect to the mail server. And exchange messages with mail server by SMTP protocol.
* These functions are implemented by CGI. In this case, if there's CGI in request message from web browser,
* call the fucntion() and handle it. This way is different from normal CGI service that there's CGI file externally.
********************************************************************************
*/
/*
###############################################################################
Include Part
###############################################################################
*/
#include <reg51.h> // 8051 SFR Definition
//#include <stdio.h> // To use 'printf'
#include "serial.h" // serial related functions
#include "socket.h" // W3100A driver file
#include "httpd.h" // http related functions
#include "lcd.h" // LCD
#include "at24c02.h" // EEPROM
#include "netconf.h" // Network Configuration
#include "util.h" // useful functions
#include "smtp.h" // smtp handling related functions
#include "sockutil.h" // Useful function of W3100A
/*
###############################################################################
Define Part
###############################################################################
*/
#define MAX_BUF_SIZE 2048 // Maximum buffer size of Rx buffer
/*
###############################################################################
Local Variable Declaration Part
###############################################################################
*/
sfr CKCON = 0x8F; // CKCON Reg. Define
u_char xdata * rx_buf = 0x4000; // Position of Rx Buffer
u_char xdata * tx_buf = 0x6000; // Position of Tx Buffer
/*
###############################################################################
Function Prototype Declaration Part
###############################################################################
*/
void Init8051(); // Initialize 8051 MCU
void init_sock(u_char i); // Initialize designated channel
void InitNetConfig(void); // Initialize Network Information
/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
********************************************************************************
* Main function for Web server
*
* Description: Initialize 8051 and W3100A.
* Transfer the appropriate web page by web page request and wait for the connection establishment from browser
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void main()
{
SOCKET WebSock = 0; // Socket to work as web server
int len;
u_char state;
u_char type;
u_char xdata tmpTok[128]; // Variable to store name in the Form Request
u_char xdata tmpTok1[128]; // Variable to store value in the Form Request
u_char xdata* nextTok = 0;
u_char xdata* Token = 0;
Init8051();
PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
PutString (" Simple SMTP Program.(Web Server mode) - Created Date : ");PutLTOA(0x20021028); PutStringLn("");
PutStringLn(" - Created By : WIZnet, Inc.");
PutStringLn(" - W3100A Driver: V2.2");
PutStringLn(" - SMTP FILES : V1.1");
PutStringLn(" - Flatform : 8051 EVB V3.0");
PutStringLn("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\r\n");
InitLcd(); // Initialize LCD
initW3100A();
InitNetConfig();
for (WebSock = 0; WebSock < MAX_SOCK_NUM; WebSock++) //Find a unused socket and Create a TCP server channel for web server with it
{
if( select(WebSock,SEL_CONTROL) == SOCK_CLOSED)
{
init_sock(WebSock); // Initialize socket and Stay in wait mode for connection request
break;
}
}
while (1)
{
state = select(WebSock, SEL_CONTROL); // Re-confirm socket status in connected state
switch(state) // In this simple HTTP server of EVB8051, service only HTML document
{
case SOCK_ESTABLISHED :
if ((len = select(WebSock, SEL_RECV)) > 0) // Confirm if received data
{
inet_ntoa(GetDestAddr(WebSock,tmpTok),tmpTok1);
PutString(tmpTok1);
PutStringLn(" Request SMTP sample page.");
if (len > MAX_BUF_SIZE) len = MAX_BUF_SIZE; // When received data more than system buffer, handle as size as buffer.
len = recv(WebSock, rx_buf, len);
type = ParseReq(rx_buf); // Analyze request
len = PrintHeader(tx_buf, type); // Make HTTP Response Header
if (type == 'c')
{
/* Parse HTTP Request Parameter */
/* (URL?name=value&name=value...) */
/* SMTP Server IP, Account, Sender Mail Address, Recipient Mail Address, Title, Contents */
nextTok = FindFirst(rx_buf,'?')+1; // Analyze to URL
nextTok = StrTok(nextTok,'&',tmpTok1); // Mail server IP Parsing (SRVIP=xxx.xxx.xxx.xxx)
Token = StrTok(tmpTok1,'=',tmpTok); // Analyze SRVIP value
SetMailServerIP(Token);
nextTok = StrTok(nextTok,'&',tmpTok1); // User account
Token = StrTok(tmpTok1,'=',tmpTok);
SetAccount(Token);
nextTok = StrTok(nextTok,'&',tmpTok1); // User Mail address
Token = StrTok(tmpTok1,'=',tmpTok);
SetSMailAddr(Token);
nextTok = StrTok(nextTok,'&',tmpTok1); // Recipient Mail address
Token = StrTok(tmpTok1,'=',tmpTok);
SetRMailAddr(Token);
XEscape(nextTok); // Convert Escape character to ASCII
ReplaceAll(nextTok,'+',' '); // Convert '+' to ' '
nextTok = StrTok(nextTok,'&',tmpTok1); // Parse Mail title
Token = StrTok(tmpTok1,'=',tmpTok);
SetTitle(Token);
nextTok = FindFirst(nextTok,'=')+1; // Mail Contents Parsing
*(FindFirstStr(nextTok," HTTP/1.1")) = '\0';
PutString("Contents = ");PutStringLn(nextTok);
ClrScr();
GotoXY(0,0);
Puts("< SIMPLE SMTP >");
GotoXY(0,1);
Puts("Sending Mail...");
PutStringLn("Sending Mail...");
/* Start mail service */
if( -1 == SendMail(nextTok))
{
len += DoErrHTML(tx_buf+len); // If failed, creat 'failed reply HTML document'
ClrScr(); // Display error message on LCD
GotoXY(0,0);
Puts("Mail NOT sended.");
PutStringLn("Mail NOT sended.");
}
else
{
len += DoHTML(tx_buf+len); // If succeeded, create 'initial HTML document'
ClrScr(); // Display success message on LCD
GotoXY(0,0);
Puts("Mail sended.");
PutStringLn("Mail sended.");
}
// Send reply HTML document to client request
len = send(WebSock, tx_buf, len);
}
else
{
ClrScr();
GotoXY(0,0);
Puts("< SIMPLE SMTP >");
len += DoHTML(tx_buf + len); // If it's Default request, Create initial HTML document
// Send reply HTML document to client request
len = send(WebSock, tx_buf, len);
}
}
break;
case SOCK_CLOSE_WAIT : // close connection
//printf("CLOSE_WAIT : %bd\r\n", WebSock);
PutHTOA(WebSock); PutStringLn(" : CLOSE_WAIT");
close(WebSock); // SOCKET CLOSE
break;
case SOCK_CLOSED : // Connection closed
//printf("CLOSED : %bd\r\n", WebSock);
PutHTOA(WebSock);PutStringLn(" : CLOSED");
init_sock(WebSock);
break;
}
} // while
}
/*
********************************************************************************
* 8051 Initialization Function
*
* Description:
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void Init8051(void)
{
EA = 0; // Disable all interrupt
CKCON = 0x01; // X2 mode
IT0 = 0; // interrupt level trigger
EX0 = 1; // INT 0 enable
EX1 = 0; // INT 1 disable
EA = 1; // Enable to all interrupt
InitSerial(); // Initialize serial port (Refer to serial.c)
wait_10ms(1);
}
/*
********************************************************************************
* Re-initialization function for the disconnected channel.
*
* Description: Close and re-initialize for the disconnected channel.
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void init_sock(u_char i)
{
// printf("socket : ");
PutString("WebSock : ");
socket(i, SOCK_STREAM, 80, 0); // Create socket
// printf("%bd ok\r\n",i);
PutHTOA(i);PutStringLn(" ok");
// printf("listen : ");
PutString("listen : ");
NBlisten(i); // Server Mode
// printf("%bd ok..\r\n", i);
PutHTOA(i);PutStringLn(" ok");
}
/*
********************************************************************************
* Description: Setup network information ( Source IP, G/W, S/N, MAC Address)
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void InitNetConfig(void)
{
int i, j;
char c;
u_char xdata ip[6]; // Variable to setup network information
u_char xdata ipstr[16];
if(!Check_EEPROM())
{
ip[0] = 0x00; ip[1] = 0x08; ip[2] = 0xDC; ip[3] = 0x00; ip[4] = 0x00; ip[5] = 0x00; // ETC.
setMACAddr(ip); // Setup MAC
ip[0] = 192; ip[1] = 168; ip[2] = 0; ip[3] = 2; // VPN Env.
setIP(ip); // Setup source IP
ip[3] = 1; // VPN , Develope Env.
setgateway(ip); // Setup gateway address
ip[0] = 255; ip[1] = 255; ip[2] = 255; ip[3] = 0;
setsubmask(ip); // Setup subnet mask
}
else
{
ClrScr();
GotoXY(0,0);
Puts(" < NET CONFIG > ");
T0 = 0;
T1 = 1;
PutString("Press 'C' Key To Update Network Configuration");
GotoXY(0,1);
for(i = 0; i < 16; i++)
{
for( j = 0 ; j < 50 ; j++)
{
if(IsPressedKey() == 1)
{
c = GetByte();PutStringLn("");
if(c == 'C' || c == 'c')
{
Configure();
c = '*';
break;
}
else if(c== 0x1B) // Escape character
{
c = '*';
break;
}
}
wait_1ms(2);
}
if(c == '*') break;
T0 = !T0; // LED TOGGLE
T1 = !T1;
Putch(0xFF);
PutByte('.');
}
PutStringLn("");
Config_Load();
}
GetNetConfig(); // Display Network information
sysinit(0x55,0x55);
ClrScr();
GotoXY(0,0);
Puts("< SIMPLE SMTP >");
GotoXY(1,1);
GetIPAddress(ip);
inet_ntoa(ip,ipstr); // Display IP Address on LCD
Puts(ipstr);
T0 = 0; // LED0 ON
T1 = 0; // LED1 ON
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -