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