1 |
/****************************************************************** |
---|
2 |
***** ***** |
---|
3 |
***** Name: easyweb.c ***** |
---|
4 |
***** Ver.: 1.0 ***** |
---|
5 |
***** Date: 07/05/2001 ***** |
---|
6 |
***** Auth: Andreas Dannenberg ***** |
---|
7 |
***** HTWK Leipzig ***** |
---|
8 |
***** university of applied sciences ***** |
---|
9 |
***** Germany ***** |
---|
10 |
***** Func: implements a dynamic HTTP-server by using ***** |
---|
11 |
***** the easyWEB-API ***** |
---|
12 |
***** ***** |
---|
13 |
******************************************************************/ |
---|
14 |
|
---|
15 |
#include <stdlib.h> |
---|
16 |
#include <stdio.h> |
---|
17 |
#include <string.h> |
---|
18 |
|
---|
19 |
#define extern // Keil: Line added for modular project management |
---|
20 |
|
---|
21 |
#include "easyweb.h" |
---|
22 |
#include "EMAC.h" // Ethernet packet driver |
---|
23 |
#include "tcpip.h" // easyWEB TCP/IP stack |
---|
24 |
#include <LPC23xx.h> // Keil: Register definition file for LPC2368 |
---|
25 |
#include "webpage.h" // webside for our HTTP server (HTML) |
---|
26 |
|
---|
27 |
|
---|
28 |
//void main(void) |
---|
29 |
int main(void) |
---|
30 |
{ |
---|
31 |
TCPLowLevelInit(); |
---|
32 |
|
---|
33 |
/* Start of Config ADC */ |
---|
34 |
PCONP |= (1<<12); // Power-On Clock Control of ADC |
---|
35 |
PINSEL1 &= 0xFFFF3FFF; // Reset Pin Config P0[23] |
---|
36 |
PINSEL1 |= 0x00004000; // Config P0[23] = AD0 |
---|
37 |
/* End of Config ADC */ |
---|
38 |
|
---|
39 |
HTTPStatus = 0; // clear HTTP-server's flag register |
---|
40 |
TCPLocalPort = TCP_PORT_HTTP; // set port we want to listen to |
---|
41 |
|
---|
42 |
while (1) // repeat forever |
---|
43 |
{ |
---|
44 |
if (!(SocketStatus & SOCK_ACTIVE)) TCPPassiveOpen(); // listen for incoming TCP-connection |
---|
45 |
DoNetworkStuff(); // handle network and easyWEB-stack |
---|
46 |
// events |
---|
47 |
HTTPServer(); |
---|
48 |
} |
---|
49 |
} |
---|
50 |
|
---|
51 |
// This function implements a very simple dynamic HTTP-server. |
---|
52 |
// It waits until connected, then sends a HTTP-header and the |
---|
53 |
// HTML-code stored in memory. Before sending, it replaces |
---|
54 |
// some special strings with dynamic values. |
---|
55 |
// NOTE: For strings crossing page boundaries, replacing will |
---|
56 |
// not work. In this case, simply add some extra lines |
---|
57 |
// (e.g. CR and LFs) to the HTML-code. |
---|
58 |
|
---|
59 |
void HTTPServer(void) |
---|
60 |
{ |
---|
61 |
if (SocketStatus & SOCK_CONNECTED) // check if somebody has connected to our TCP |
---|
62 |
{ |
---|
63 |
if (SocketStatus & SOCK_DATA_AVAILABLE) // check if remote TCP sent data |
---|
64 |
TCPReleaseRxBuffer(); // and throw it away |
---|
65 |
|
---|
66 |
if (SocketStatus & SOCK_TX_BUF_RELEASED) // check if buffer is free for TX |
---|
67 |
{ |
---|
68 |
if (!(HTTPStatus & HTTP_SEND_PAGE)) // init byte-counter and pointer to webside |
---|
69 |
{ // if called the 1st time |
---|
70 |
HTTPBytesToSend = sizeof(WebSide) - 1; // get HTML length, ignore trailing zero |
---|
71 |
PWebSide = (unsigned char *)WebSide; // pointer to HTML-code |
---|
72 |
} |
---|
73 |
|
---|
74 |
if (HTTPBytesToSend > MAX_TCP_TX_DATA_SIZE) // transmit a segment of MAX_SIZE |
---|
75 |
{ |
---|
76 |
if (!(HTTPStatus & HTTP_SEND_PAGE)) // 1st time, include HTTP-header |
---|
77 |
{ |
---|
78 |
memcpy(TCP_TX_BUF, GetResponse, sizeof(GetResponse) - 1); |
---|
79 |
memcpy(TCP_TX_BUF + sizeof(GetResponse) - 1, PWebSide, MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1); |
---|
80 |
HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1; |
---|
81 |
PWebSide += MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1; |
---|
82 |
} |
---|
83 |
else |
---|
84 |
{ |
---|
85 |
memcpy(TCP_TX_BUF, PWebSide, MAX_TCP_TX_DATA_SIZE); |
---|
86 |
HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE; |
---|
87 |
PWebSide += MAX_TCP_TX_DATA_SIZE; |
---|
88 |
} |
---|
89 |
|
---|
90 |
TCPTxDataCount = MAX_TCP_TX_DATA_SIZE; // bytes to xfer |
---|
91 |
InsertDynamicValues(); // exchange some strings... |
---|
92 |
TCPTransmitTxBuffer(); // xfer buffer |
---|
93 |
} |
---|
94 |
else if (HTTPBytesToSend) // transmit leftover bytes |
---|
95 |
{ |
---|
96 |
memcpy(TCP_TX_BUF, PWebSide, HTTPBytesToSend); |
---|
97 |
TCPTxDataCount = HTTPBytesToSend; // bytes to xfer |
---|
98 |
InsertDynamicValues(); // exchange some strings... |
---|
99 |
TCPTransmitTxBuffer(); // send last segment |
---|
100 |
TCPClose(); // and close connection |
---|
101 |
HTTPBytesToSend = 0; // all data sent |
---|
102 |
} |
---|
103 |
|
---|
104 |
HTTPStatus |= HTTP_SEND_PAGE; // ok, 1st loop executed |
---|
105 |
} |
---|
106 |
} |
---|
107 |
else |
---|
108 |
{ |
---|
109 |
HTTPStatus &= ~HTTP_SEND_PAGE; // reset help-flag if not connected |
---|
110 |
} |
---|
111 |
} |
---|
112 |
|
---|
113 |
unsigned int GetAD0Val(void) |
---|
114 |
{ |
---|
115 |
unsigned int val; |
---|
116 |
|
---|
117 |
AD0CR = 0x01000001 | 0x002E0400; // Setup A/D: 10-bit AIN0 @ 3MHz |
---|
118 |
do |
---|
119 |
{ |
---|
120 |
val = AD0GDR; // Read A/D Data Register |
---|
121 |
} while ((val & 0x80000000) == 0); // Wait for end of A/D Conversion |
---|
122 |
AD0CR &= ~0x01000001; // Stop A/D Conversion |
---|
123 |
val = (val >> 6) & 0x03FF; // Extract AIN0 Value |
---|
124 |
return(val / 10); // result of A/D process |
---|
125 |
} |
---|
126 |
|
---|
127 |
// searches the TX-buffer for special strings and replaces them |
---|
128 |
// with dynamic values (AD-converter results) |
---|
129 |
|
---|
130 |
void InsertDynamicValues(void) |
---|
131 |
{ |
---|
132 |
unsigned char *Key; |
---|
133 |
char NewKey[5]; |
---|
134 |
unsigned int i; |
---|
135 |
|
---|
136 |
if (TCPTxDataCount < 4) return; // there can't be any special string |
---|
137 |
|
---|
138 |
Key = TCP_TX_BUF; |
---|
139 |
|
---|
140 |
for (i = 0; i < (TCPTxDataCount - 3); i++) |
---|
141 |
{ |
---|
142 |
if (*Key == 'A') |
---|
143 |
if (*(Key + 1) == 'D') |
---|
144 |
if (*(Key + 3) == '%') |
---|
145 |
switch (*(Key + 2)) |
---|
146 |
{ |
---|
147 |
case '0' : // "AD0%"? |
---|
148 |
{ |
---|
149 |
sprintf(NewKey, "%3u", GetAD0Val()); // insert AD converter value |
---|
150 |
memcpy(Key, NewKey, 3); // AD0(P0[23]) |
---|
151 |
break; |
---|
152 |
} |
---|
153 |
} |
---|
154 |
Key++; |
---|
155 |
} |
---|
156 |
} |
---|