These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers easyweb.c Source File

easyweb.c

00001 /******************************************************************
00002  *****                                                        *****
00003  *****  Name: easyweb.c                                       *****
00004  *****  Ver.: 1.0                                             *****
00005  *****  Date: 07/05/2001                                      *****
00006  *****  Auth: Andreas Dannenberg                              *****
00007  *****        HTWK Leipzig                                    *****
00008  *****        university of applied sciences                  *****
00009  *****        Germany                                         *****
00010  *****  Func: implements a dynamic HTTP-server by using       *****
00011  *****        the easyWEB-API                                 *****
00012  *****                                                        *****
00013  ******************************************************************/
00014 
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include "adc.h"
00019 #include "lpc17xx_libcfg.h"
00020 #include "EMAC.h"         // Keil: *.c -> *.h    // ethernet packet driver
00021 #define extern            // Keil: Line added for modular project management
00022 
00023 #include "easyweb.h"
00024 #include "tcpip.h"        // Keil: *.c -> *.h    // easyWEB TCP/IP stack
00025 #include "webpage.h"                             // webside for our HTTP server (HTML)
00026 
00027 /* Example group ----------------------------------------------------------- */
00028 /** @defgroup EMAC_Easy_Web Easy_Web
00029  * @ingroup EMAC_Examples
00030  * @{
00031  */
00032 
00033 // NXP: Include some header files that differs from the origin
00034 int main(void)
00035 {
00036   TCPLowLevelInit();
00037   HTTPStatus = 0;                                // clear HTTP-server's flag register
00038 
00039   TCPLocalPort = TCP_PORT_HTTP;                  // set port we want to listen to
00040 
00041   while (1)                                      // repeat forever
00042   {
00043     if (!(SocketStatus & SOCK_ACTIVE)) TCPPassiveOpen();   // listen for incoming TCP-connection
00044     DoNetworkStuff();                                      // handle network and easyWEB-stack
00045                                                            // events
00046     HTTPServer();
00047   }
00048 }
00049 
00050 // This function implements a very simple dynamic HTTP-server.
00051 // It waits until connected, then sends a HTTP-header and the
00052 // HTML-code stored in memory. Before sending, it replaces
00053 // some special strings with dynamic values.
00054 // NOTE: For strings crossing page boundaries, replacing will
00055 // not work. In this case, simply add some extra lines
00056 // (e.g. CR and LFs) to the HTML-code.
00057 
00058 void HTTPServer(void)
00059 {
00060   if (SocketStatus & SOCK_CONNECTED)             // check if somebody has connected to our TCP
00061   {
00062     if (SocketStatus & SOCK_DATA_AVAILABLE)      // check if remote TCP sent data
00063       TCPReleaseRxBuffer();                      // and throw it away
00064 
00065     if (SocketStatus & SOCK_TX_BUF_RELEASED)     // check if buffer is free for TX
00066     {
00067       if (!(HTTPStatus & HTTP_SEND_PAGE))        // init byte-counter and pointer to webside
00068       {                                          // if called the 1st time
00069         HTTPBytesToSend = sizeof(WebSide) - 1;   // get HTML length, ignore trailing zero
00070         PWebSide = (unsigned char *)WebSide;     // pointer to HTML-code
00071       }
00072 
00073       if (HTTPBytesToSend > MAX_TCP_TX_DATA_SIZE)     // transmit a segment of MAX_SIZE
00074       {
00075         if (!(HTTPStatus & HTTP_SEND_PAGE))           // 1st time, include HTTP-header
00076         {
00077           memcpy(TCP_TX_BUF, GetResponse, sizeof(GetResponse) - 1);
00078           memcpy(TCP_TX_BUF + sizeof(GetResponse) - 1, PWebSide, MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1);
00079           HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1;
00080           PWebSide += MAX_TCP_TX_DATA_SIZE - sizeof(GetResponse) + 1;
00081         }
00082         else
00083         {
00084           memcpy(TCP_TX_BUF, PWebSide, MAX_TCP_TX_DATA_SIZE);
00085           HTTPBytesToSend -= MAX_TCP_TX_DATA_SIZE;
00086           PWebSide += MAX_TCP_TX_DATA_SIZE;
00087         }
00088 
00089         TCPTxDataCount = MAX_TCP_TX_DATA_SIZE;   // bytes to xfer
00090         InsertDynamicValues();                   // exchange some strings...
00091         TCPTransmitTxBuffer();                   // xfer buffer
00092       }
00093       else if (HTTPBytesToSend)                  // transmit leftover bytes
00094       {
00095         memcpy(TCP_TX_BUF, PWebSide, HTTPBytesToSend);
00096         TCPTxDataCount = HTTPBytesToSend;        // bytes to xfer
00097         InsertDynamicValues();                   // exchange some strings...
00098         TCPTransmitTxBuffer();                   // send last segment
00099         TCPClose();                              // and close connection
00100         HTTPBytesToSend = 0;                     // all data sent
00101       }
00102 
00103       HTTPStatus |= HTTP_SEND_PAGE;              // ok, 1st loop executed
00104     }
00105   }
00106   else
00107     HTTPStatus &= ~HTTP_SEND_PAGE;               // reset help-flag if not connected
00108 }
00109 
00110 // samples and returns the AD-converter value of channel 2 (MCB1700 board) or channel 5 (IAR board)
00111 
00112 unsigned int GetAD7Val(void)
00113 {
00114     unsigned int val;
00115     ADC_startCnv();
00116     val = ADC_getCnv();
00117     ADC_stopCnv();
00118     return (val/40);
00119 }
00120 
00121 // samples and returns AD-converter value of channel 1
00122 
00123 unsigned int GetTempVal(void)
00124 {
00125     // Always return (0)
00126     return (0);
00127 }
00128 
00129 
00130 // searches the TX-buffer for special strings and replaces them
00131 // with dynamic values (AD-converter results)
00132 
00133 void InsertDynamicValues(void)
00134 {
00135   unsigned char *Key;
00136            char NewKey[5];
00137   unsigned int i;
00138 
00139   if (TCPTxDataCount < 4) return;                     // there can't be any special string
00140 
00141   Key = TCP_TX_BUF;
00142 
00143   for (i = 0; i < (TCPTxDataCount - 3); i++)
00144   {
00145     if (*Key == 'A')
00146      if (*(Key + 1) == 'D')
00147        if (*(Key + 3) == '%')
00148          switch (*(Key + 2))
00149          {
00150            case '7' :                                 // "AD7%"?
00151            {
00152              sprintf(NewKey, "%3u", GetAD7Val());     // insert AD converter value
00153              memcpy(Key, NewKey, 3);                  // channel 7 (P6.7)
00154              break;
00155            }
00156            case 'A' :                                 // "ADA%"?
00157            {
00158              sprintf(NewKey, "%3u", GetTempVal());    // insert AD converter value
00159              memcpy(Key, NewKey, 3);                  // channel 10 (temp.-diode)
00160              break;
00161            }
00162          }
00163     Key++;
00164   }
00165 }
00166 
00167 #ifdef  DEBUG
00168 /*******************************************************************************
00169 * @brief        Reports the name of the source file and the source line number
00170 *               where the CHECK_PARAM error has occurred.
00171 * @param[in]    file Pointer to the source file name
00172 * @param[in]    line assert_param error line source number
00173 * @return       None
00174 *******************************************************************************/
00175 void check_failed(uint8_t *file, uint32_t line)
00176 {
00177     /* User can add his own implementation to report the file name and line number,
00178      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00179 
00180     /* Infinite loop */
00181     while(1);
00182 }
00183 #endif
00184 
00185 /*
00186  * @}
00187  */