-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyweb.c
194 lines (162 loc) · 8.58 KB
/
easyweb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
A /******************************************************************
***** *****
***** Name: easyweb.c *****
***** Ver.: 1.0 *****
***** Date: 07/05/2001 *****
***** Auth: Andreas Dannenberg *****
***** HTWK Leipzig *****
***** university of applied sciences *****
***** Germany *****
***** Func: implements a dynamic HTTP-server by using *****
***** the easyWEB-API *****
***** *****
******************************************************************/
#define extern // Keil: Line added for modular project management
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "easyweb.h"
#include "lan91c111.h" // Keil: *.c -> *.h // ethernet packet driver
#include "tcpip.h" // Keil: *.c -> *.h // easyWEB TCP/IP stack
// Keil: Register definition file for LPC2294
#include "webpage.h" // webside for our HTTP server (HTML)
void main(void)
{
//InitOsc(); // Keil: No oscillator initialization necessary at this time.
//InitPorts(); // Keil: No port initialization necessary at this time.
TCPLowLevelInit();
/*
*(unsigned char *)RemoteIP = 24; // uncomment those lines to get the
*((unsigned char *)RemoteIP + 1) = 8; // quote of the day from a real
*((unsigned char *)RemoteIP + 2) = 69; // internet server! (gateway must be
*((unsigned char *)RemoteIP + 3) = 7; // set to your LAN-router)
TCPLocalPort = 2025;
TCPRemotePort = TCP_PORT_QOTD;
TCPActiveOpen();
while (SocketStatus & SOCK_ACTIVE) // read the quote from memory
{ // by using the hardware-debugger
DoNetworkStuff();
}
*/
HTTPStatus = 0; // clear HTTP-server's flag register
TCPLocalPort = TCP_PORT_HTTP; // set port we want to listen to
while (1) // repeat forever
{
if (!(SocketStatus & SOCK_ACTIVE)) TCPPassiveOpen(); // listen for incoming TCP-connection
DoNetworkStuff(); // handle network and easyWEB-stack
// events
HTTPServer();
}
}
// This function implements a very simple dynamic HTTP-server.
// It waits until connected, then sends a HTTP-header and the
// HTML-code stored in memory. Before sending, it replaces
// some special strings with dynamic values.
// NOTE: For strings crossing page boundaries, replacing will
// not work. In this case, simply add some extra lines
// (e.g. CR and LFs) to the HTML-code.
void HTTPServer(void)
{
if (SocketStatus & SOCK_CONNECTED) // check if somebody has connected to our TCP
{
if (SocketStatus & SOCK_DATA_AVAILABLE) // check if remote TCP sent data
TCPReleaseRxBuffer(); // and throw it away
if (SocketStatus & SOCK_TX_BUF_RELEASED) // check if buffer is free for TX
{
if (!(HTTPStatus & HTTP_SEND_PAGE)) // init byte-counter and pointer to webside
{ // if called the 1st time
HTTPBytesToSend = sizeof(WebSide) - 1; // get HTML length, ignore trailing zero
PWebSide = (unsigned char *)WebSide; // pointer to HTML-code
}
if (HTTPBytesToSend > MAX_TCP_TX_DATA_SIZE) // transmit a segment of MAX_SIZE
{
if (!(HTTPStatus & HTTP_SEND_PAGE)) // 1st time, include HTTP-header
{
memcpy(TCP_TX_BUF, GetResponse, sizeof(GetResponse) - 1);
memcpy(TCP_TX_BUF + sizeof(GetResponse) - 1, PWebSide, MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1);
HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1;
PWebSide += MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1;
}
else
{
memcpy(TCP_TX_BUF, PWebSide, MAX_TCP_TX_DATA_SIZE);
HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE;
PWebSide += MAX_TCP_TX_DATA_SIZE;
}
TCPTxDataCount = MAX_TCP_TX_DATA_SIZE; // bytes to xfer
InsertDynamicValues(); // exchange some strings...
TCPTransmitTxBuffer(); // xfer buffer
}
else if (HTTPBytesToSend) // transmit leftover bytes
{
memcpy(TCP_TX_BUF, PWebSide, HTTPBytesToSend);
TCPTxDataCount = HTTPBytesToSend; // bytes to xfer
InsertDynamicValues(); // exchange some strings...
TCPTransmitTxBuffer(); // send last segment
TCPClose(); // and close connection
HTTPBytesToSend = 0; // all data sent
}
HTTPStatus |= HTTP_SEND_PAGE; // ok, 1st loop executed
}
}
else
HTTPStatus &= ~HTTP_SEND_PAGE; // reset help-flag if not connected
}
// samples and returns the AD-converter value of channel 0
unsigned int GetAD7Val(void)
{
// Keil: function replaced to handle LPC2294 A/D converter.
unsigned int val;
ADCR = 0x01000001 | 0x002E0400; // Setup A/D: 10-bit AIN0 @ 3MHz
do {
val = ADDR; // Read A/D Data Register
} while ((val & 0x80000000) == 0); // Wait for end of A/D Conversion
ADCR &= ~0x01000001; // Stop A/D Conversion
val = (val >> 6) & 0x03FF; // Extract AIN0 Value
return(val / 11); // result of A/D process
}
// samples and returns AD-converter value of channel 1
unsigned int GetTempVal(void)
{
// Keil: function replaced to handle LPC2294 A/D converter.
unsigned int val;
ADCR = 0x01000002 | 0x002E0400; // Setup A/D: 10-bit AIN1 @ 3MHz
do {
val = ADDR; // Read A/D Data Register
} while ((val & 0x80000000) == 0); // Wait for end of A/D Conversion
ADCR &= ~0x01000002; // Stop A/D Conversion
val = (val >> 6) & 0x03FF; // Extract AIN1 Value
return(val / 11); // result of A/D process
}
// searches the TX-buffer for special strings and replaces them
// with dynamic values (AD-converter results)
void InsertDynamicValues(void)
{
unsigned char *Key;
unsigned char NewKey[5];
unsigned int i;
if (TCPTxDataCount < 4) return; // there can't be any special string
Key = TCP_TX_BUF;
for (i = 0; i < (TCPTxDataCount - 3); i++)
{
if (*Key == 'A')
if (*(Key + 1) == 'D')
if (*(Key + 3) == '%')
switch (*(Key + 2))
{
case '7' : // "AD7%"?
{
sprintf(NewKey, "%3u", GetAD7Val()); // insert AD converter value
memcpy(Key, NewKey, 3); // channel 7 (P6.7)
break;
}
case 'A' : // "ADA%"?
{
sprintf(NewKey, "%3u", GetTempVal()); // insert AD converter value
memcpy(Key, NewKey, 3); // channel 10 (temp.-diode)
break;
}
}
Key++;
}
}