/****************************************************************** ***** ***** ***** 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 ***** ***** ***** ******************************************************************/ #include #include #include #define extern // Keil: Line added for modular project management #include "easyweb.h" #include "EMAC.h" // Ethernet packet driver #include "tcpip.h" // easyWEB TCP/IP stack #include // Keil: Register definition file for LPC2368 #include "webpage.h" // webside for our HTTP server (HTML) //void main(void) int main(void) { TCPLowLevelInit(); /* Start of Config ADC */ PCONP |= (1<<12); // Power-On Clock Control of ADC PINSEL1 &= 0xFFFF3FFF; // Reset Pin Config P0[23] PINSEL1 |= 0x00004000; // Config P0[23] = AD0 /* End of Config ADC */ 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 } } unsigned int GetAD0Val(void) { unsigned int val; AD0CR = 0x01000001 | 0x002E0400; // Setup A/D: 10-bit AIN0 @ 3MHz do { val = AD0GDR; // Read A/D Data Register } while ((val & 0x80000000) == 0); // Wait for end of A/D Conversion AD0CR &= ~0x01000001; // Stop A/D Conversion val = (val >> 6) & 0x03FF; // Extract AIN0 Value return(val / 10); // 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; 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 '0' : // "AD0%"? { sprintf(NewKey, "%3u", GetAD0Val()); // insert AD converter value memcpy(Key, NewKey, 3); // AD0(P0[23]) break; } } Key++; } }