Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App

Dependencies:   NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers demo.cpp Source File

demo.cpp

00001 
00002 /*****************************************************************************
00003 * Dynamic HTML string handlers: 
00004 * Nine dynamic HTML fields are updated on browser refresh or button press: 
00005 *     Acceleration
00006 *     Magnetometer                  
00007 *     eCompass      
00008 *     Altitude
00009 *     Battery Voltage
00010 *     Ambient Light
00011 *     Temperature                   
00012 *     Slider Position       
00013 *     Page Views
00014 *
00015 * CGI HTML forms: 
00016 * Three CGI form inputs are used to submit data from browser to the Server: 
00017 * On screen buttons: -Red-, Green, Blue (for control of RGB LED output color) 
00018 *
00019 * myindex[] contains the HTML string that defines the webpage that is served
00020 * Use an online HTML validator to verify HTML code before running it on the MCU
00021 * eg. 
00022 * www.w3schools.com/tags/tryit.asp?filename=tryhtml_div_test
00023 * www.onlinewebcheck.com/check.php?adv=1
00024 * Note: Before checking the HTML in one of these validators, 
00025 * strip-out all “\” backslash characters (using search & replace)
00026 *
00027 * Webserver code is based on TI's CC3000 Simple HTTP Webserver:  
00028 * http://processors.wiki.ti.com/index.php/CC3000_Wi-Fi_for_MCU
00029 * 
00030 * More detail on implementation of this Webserver App is available here:
00031 * http://processors.wiki.ti.com/index.php/CC3000_HTTP_Server_Demo_Session
00032 *
00033 * A more advanced Webserver and Client App is also available from TI for the CC3000:
00034 * http://processors.wiki.ti.com/index.php/CC3000_Web_Server_Client_Application
00035 * (at this time not yet ported to Kinetis-L as the host processor)
00036 * 
00037 ******************************************************************************
00038 *
00039 *  demo.c - CC3000 Main Demo Application
00040 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00041 *
00042 *  Redistribution and use in source and binary forms, with or without
00043 *  modification, are permitted provided that the following conditions
00044 *  are met:
00045 *
00046 *    Redistributions of source code must retain the above copyright
00047 *    notice, this list of conditions and the following disclaimer.
00048 *
00049 *    Redistributions in binary form must reproduce the above copyright
00050 *    notice, this list of conditions and the following disclaimer in the
00051 *    documentation and/or other materials provided with the
00052 *    distribution.
00053 *
00054 *    Neither the name of Texas Instruments Incorporated nor the names of
00055 *    its contributors may be used to endorse or promote products derived
00056 *    from this software without specific prior written permission.
00057 *
00058 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00059 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00060 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00061 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00062 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00063 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00064 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00065 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00066 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00067 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00068 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00069 *
00070 *****************************************************************************/
00071 
00072 #include "mbed.h"
00073 #include "defLED.h"
00074 #include "demo.h"
00075 #include "AvnetHTML.h"
00076 #include "TSISensor.h"
00077 #include "Wi-Go_eCompass_Lib_V3.h"
00078 
00079 extern DigitalOut ledr;
00080 extern DigitalOut ledg;
00081 extern DigitalOut ledb;
00082 extern DigitalOut led1;
00083 extern DigitalOut led2;
00084 extern DigitalOut led3;
00085 extern TSISensor tsi;
00086 
00087 // Setup the functions to handle our CGI parameters
00088 cgi_handler pHandlers;
00089 dyn_html_handler htmlHandlers;
00090 
00091 extern tNetappIpconfigRetArgs ipinfo2;
00092 
00093 extern axis6_t axis6;
00094 extern int server_running;
00095 extern unsigned char newData;
00096 extern unsigned short adc_sample3;
00097 
00098 // Variable declared in main and checked in the systick handler
00099 // Code in the systick handler is only processed when Systick_Allowed = 1
00100 extern bool Systick_Allowed;
00101 
00102 /** \brief Pointer to the index HTML page */
00103 char * indexPage;
00104 
00105 /** \brief Pointer to CGI handler structure */
00106 cgi_handler * chList;
00107 
00108 /** \brief Pointer to Dynamic HTML handler structure */
00109 dyn_html_handler * htmlList;
00110 
00111 /** \brief Page view counter */
00112 int viewCounter = 1;
00113 #define REQ_BUFFER_SIZE     400
00114 #define HTTP_TX_BLOCK_SIZE  256
00115 //#define HTTP_TX_BLOCK_SIZE  1024
00116 //#define HTTP_TX_BLOCK_SIZE  512
00117 
00118 
00119 // Setup the functions to handle our CGI parameters
00120 char requestBuffer[REQ_BUFFER_SIZE];
00121 
00122 
00123 
00124 
00125 
00126 /*
00127 // ---------- HTML Webpage Content is defined here ----------
00128 // Caution! Field labels and spaces in the HTML should not be edited without making corresponding changes in the C-code!
00129 char index[] = {
00130 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html lang=\"en-US\">"
00131 "<head>"
00132 "<META content=\"text/html;charset=ISO-8859-1\" http-equiv=\"content-type\">"
00133 "<title>Wi-Go WebServer</title>"
00134 //"<META HTTP-EQUIV=\"refresh\" content=\"2\">"   // Uncomment for auto-refresh every 2 seconds
00135 "</head>"
00136 
00137 "<body><div style=\"text-align: left\"><font size=\"6\" color=\"Red\" face=\"Tahoma\">"
00138 "<b>Avnet Wi-Go Webserver</b></font>"
00139 "<hr size=3 width=600 align=left>"
00140 "<font size=\"5\" color=\"Red\" face=\"Tahoma\"><b>LED RGB color select...</b></font>"
00141 
00142 //"<font size=\"5\" color=\"Black\" face=\"monospace\"></font>" 
00143 "<form method=\"get\" action=\"index.html\" name=\"server\">"
00144 "<input name=\"ledCon\" type=\"submit\" value=\"-Red-\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
00145 "<input name=\"ledCon\" type=\"submit\" value=\"Green\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
00146 "<input name=\"ledCon\" type=\"submit\" value=\"Blue-\">"
00147 "</form></div>" 
00148 
00149 "<hr size=3 width=600 align=left>"
00150 "<div style=\"text-align: left\"><font size=\"5\" color=\"Red\" face=\"Tahoma\">"
00151 "<b>Navigation Sensors</b></font></div>"
00152 
00153 "<div style=\"text-align:left\"><font size=\"4\" color=\"Black\" face=\"monospace\"><b>"
00154 "Acceleration(G)..                                       <br>"
00155 "Magnetometer(uT).                                       <br>"
00156 "eCompass.........                                         <br>"
00157 "Altitude.........                <br></b></font></div>"
00158 
00159 "<hr size=3 width=600 align=left>"
00160 "<div style=\"text-align: left\"><font size=\"5\" color=\"Red\" face=\"Tahoma\">"
00161 "<b>Status and Control</b></font></div>"
00162 
00163 "<div style=\"text-align:left\"><font size=\"4\" color=\"Black\" face=\"monospace\"><b>"
00164 "Battery Voltage..       <br>"
00165 "Ambient Light....       <br>"
00166 "Temperature......          <br>"
00167 "Slider Position..          <br>"
00168 "Page Views.......        </b></font>"
00169 "<hr size=3 width=600 align=left>"
00170 "</body></html>"};  // delete this line if adding the SVG code below...
00171 */
00172 // Optional section 1: Full SVG graphic example: Uncomment this section to display the standard Wi-Fi logo in lower area of webpage
00173 // Browsers supporting SVG: Chrome and FireFox (all versions), Android (3.0 onwards), Safari (5.0 onwards), IE (9.0 onwards?)
00174 /*
00175 "<!--[if !IE]><!-->"  // This prevents SVG content being sent to Internet Explorer
00176 "<svg>" 
00177 "xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"175.49\" width=\"400\""
00178 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
00179 "xmlns:cc=\"http://creativecommons.org/ns#\""
00180 "xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
00181 "<defs></defs>"
00182 "<metadata>"
00183 "<rdf:RDF>"
00184 "<cc:Work rdf:about=\"\">"
00185 "<dc:format>image/svg+xml</dc:format>"
00186 "<dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>"
00187 "<dc:title/>"
00188 "</cc:Work>"
00189 "</rdf:RDF>"
00190 "</metadata>"
00191 "<g transform=\"translate(-309.85919,-343.20843)\">"
00192 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,576.05074,474.82936)\">"
00193 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0c4.977,0,9.049,4.077,9.049,9.049v5.389c0,4.973-4.072,9.048-9.049,9.048h-35.433c-4.973,0-9.049-4.075-9.049-9.048v-5.389c0-4.972,4.076-9.049,9.049-9.049\"/>"
00194 "</g>"
00195 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,514.3818,441.01715)\">"
00196 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0,0,5.389c0,4.072,3.314,7.32,7.32,7.32h9.187c4.007,0,7.253-3.248,7.253-7.32v-5.389c0-4.005-3.246-7.32-7.253-7.32h-20.239c2.281,1.656,3.732,4.284,3.732,7.32\"/>"
00197 "</g>"
00198 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,469.99257,451.60916)\">"
00199 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0-3.043,0-0.55,2.56c-0.345,1.794-0.692,4.005-0.761,4.833-0.069-0.828-0.416-3.039-0.825-4.833l-0.555-2.56h-2.968l-2.767,11.748h3.317l0.343-2.004c0.276-1.66,0.556-3.659,0.695-5.044,0.136,1.385,0.481,3.384,0.896,5.044l0.412,2.004h2.972l0.413-2.004c0.348-1.66,0.693-3.659,0.833-5.044,0.136,1.385,0.482,3.384,0.757,5.044l0.278,2.004h3.313\"/>"
00200 "</g>"
00201 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,491.40819,416.23898)\">"
00202 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0c-0.968,0-1.727,0.553-1.727,1.451,0,0.899,0.759,1.45,1.727,1.45,1.036,0,1.796-0.551,1.796-1.45,0-0.898-0.76-1.451-1.796-1.451m-1.521-0.968,3.0401,0,0-8.4984-3.0401,0,0,8.4984z\"/>"
00203 "</g>"
00204 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,541.9845,418.05849)\">"
00205 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0,0-2.618,6.22,0,0-2.767-6.22,0,0-3.593-3.247,0,0,11.748,10.156,0,0-2.77\"/>"
00206 "</g>"
00207 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,580.44446,416.23898)\">"
00208 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0c-0.97,0-1.727,0.553-1.727,1.451,0,0.899,0.757,1.45,1.727,1.45,1.035,0,1.797-0.551,1.797-1.45,0-0.898-0.762-1.451-1.797-1.451m-1.521-0.968,3.0371,0,0-8.4984-3.0371,0,0,8.4984z\"/>"
00209 "</g>"
00210 "</g>"
00211 "</svg>"
00212 "<!--<![endif]-->"  
00213 */                // end of conditional inclusion of SVG graphic (excludes Internet Explorer browser)
00214 // ---- End of Wi-Fi logo SVG image definition ----
00215 
00216 // Optional section 2: Simple SVG graphic example: Uncomment this section to generate a filled circle  in lower area of webpage
00217 // eg. Could use to indicate RGB LED color (with addition of applicable C-code to dynamically update specified "fill=" color)
00218 /*
00219 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">"
00220 "<circle cx=\"155\" cy=\"22\" r=\"20\" stroke=\"DimGrey\" stroke-width=\"2\" fill=\"Red\"/>"
00221 "</svg>"
00222 "</body></html>"};
00223 */
00224 
00225 // ----------------------------------------------------------
00226 
00227 //char ssid_name[] = SSID;
00228 char testString[20];
00229 //int vcc = 0;
00230 //*****************************************************************************
00231 //
00232 //!  demo_wi-fi_main
00233 //!
00234 //!  \param  None
00235 //!
00236 //!  \return none
00237 //!
00238 //!  \brief   The main loop is executed here
00239 //
00240 //*****************************************************************************
00241 
00242 int demo_wifi_main(void)
00243 {
00244     server_running = 1;
00245     while(1)
00246     {
00247       /* Configure dynamic HTML string handlers
00248          0 : Acceleration
00249          1 : Magnetometer                   
00250          2 : eCompass       
00251          3 : Altitude
00252          ------------------- 
00253          4 : Battery Voltage
00254          5 : Ambient Light
00255          6 : Temperature                    
00256          7 : Slider Position        
00257          8 : Page Views
00258       */                    
00259         (htmlHandlers.dynHtmlFunc[0]) = getAccelXYZ_Str; 
00260         memcpy(htmlHandlers.dynHtmlParamName[0],"Acceleration(G).. ",strlen("Acceleration(G).. "));
00261         htmlHandlers.dynHtmlParamName[0][strlen("Acceleration(G).. ")] ='\0';
00262 
00263         (htmlHandlers.dynHtmlFunc[1]) = getM3110Str; 
00264         memcpy(htmlHandlers.dynHtmlParamName[1],"Magnetometer(uT). ",strlen("Magnetometer(uT). "));
00265         htmlHandlers.dynHtmlParamName[1][strlen("Magnetometer(uT). ")] ='\0';
00266 
00267         (htmlHandlers.dynHtmlFunc[2]) = getCompassStr;
00268         memcpy(htmlHandlers.dynHtmlParamName[2],"eCompass......... ",strlen("eCompass......... "));
00269         htmlHandlers.dynHtmlParamName[2][strlen("eCompass......... ")] ='\0';
00270 
00271         (htmlHandlers.dynHtmlFunc[3]) = getAltitudeStr; 
00272         memcpy(htmlHandlers.dynHtmlParamName[3],"Altitude......... ",strlen("Altitude......... "));
00273         htmlHandlers.dynHtmlParamName[3][strlen("Altitude......... ")] ='\0';
00274 
00275         (htmlHandlers.dynHtmlFunc[4]) = getBatteryVoltageStr;
00276         memcpy(htmlHandlers.dynHtmlParamName[4],"Battery Voltage.. ",strlen("Battery Voltage.. "));
00277         htmlHandlers.dynHtmlParamName[4][strlen("Battery Voltage.. ")] ='\0';
00278 
00279         (htmlHandlers.dynHtmlFunc[5]) = getLightVoltageStr;
00280         memcpy(htmlHandlers.dynHtmlParamName[5],"Ambient Light.... ",strlen("Ambient Light.... "));
00281         htmlHandlers.dynHtmlParamName[5][strlen("Ambient Light.... ")] ='\0';
00282             
00283         (htmlHandlers.dynHtmlFunc[6]) = getTemperatureStr; 
00284         memcpy(htmlHandlers.dynHtmlParamName[6],"Temperature...... ",strlen("Temperature...... "));
00285         htmlHandlers.dynHtmlParamName[6][strlen("Temperature...... ")] ='\0';
00286 
00287         (htmlHandlers.dynHtmlFunc[7]) = getTSI_sliderStr; 
00288         memcpy(htmlHandlers.dynHtmlParamName[7],"Slider Position.. ",strlen("Slider Position.. "));
00289         htmlHandlers.dynHtmlParamName[7][strlen("Slider Position.. ")] ='\0';
00290 
00291         (htmlHandlers.dynHtmlFunc[8]) = getViewsNum;
00292         memcpy(htmlHandlers.dynHtmlParamName[8],"Page Views....... ",strlen("Page Views....... "));
00293         htmlHandlers.dynHtmlParamName[8][strlen("Page Views....... ")] ='\0';
00294 
00295         // Configure CGI Handler
00296         (pHandlers.cgiHandlerFunc[0]) = testFunc;
00297 
00298         serverMain(HTTP_PORT,(char *)index, &pHandlers, &htmlHandlers);
00299     }
00300 }
00301 
00302 
00303 void testFunc(char * str)
00304 {
00305     memcpy(testString,str,strlen(str));
00306     if(strcmp(str, "-Red-") == 0)
00307     {
00308         RED_ON; GREEN_OFF; BLUE_OFF; 
00309     }
00310     else if(strcmp(str, "Green") == 0) 
00311     {
00312         RED_OFF; GREEN_ON; BLUE_OFF;
00313     }
00314     else if (strcmp(str, "Blue-") == 0) 
00315     {
00316         RED_OFF; GREEN_OFF; BLUE_ON;
00317     }      
00318 }
00319 
00320 void getBatteryVoltageStr(char * str)
00321 {
00322     sprintf(str,"    "); //clears field (needed if previous string had more characters)
00323     sprintf(str, "%d %%", adc_sample3);
00324 }
00325  
00326 void getLightVoltageStr(char * str)
00327 {
00328     int LightPercent = 0;
00329     LightPercent = (axis6.light * 100) / 4096; 
00330     sprintf(str,"    "); //clears field (needed if previous string had more characters)
00331     sprintf(str, "%d %%", LightPercent);
00332 }
00333 
00334 void getAccelXYZ_Str(char * str) // MMA8451Q accelerometer - report axis with highest value 
00335 {
00336     sprintf(str,"                                    "); //clears field (needed if previous string had more characters)   
00337     sprintf(str, "X= %1.2f, Y= %1.2f, Z= %1.2f", axis6.fGax, axis6.fGay, axis6.fGaz);;  
00338 }  
00339 
00340 void getTemperatureStr(char * str) // 
00341 {
00342     sprintf(str, "%+d C", axis6.temp); 
00343 }
00344 
00345 void getTSI_sliderStr(char * str) // TSI Slider position 
00346 {
00347     uint8_t slider_position; 
00348     
00349     slider_position = tsi.readPercentage() * 100; // Slider position as percentage
00350     sprintf(str,"    "); //clears field (needed if previous string had more characters)
00351     sprintf(str, "%d %%", slider_position);
00352 }   
00353 
00354 void getCompassStr(char * str)   // Mag3110 generated Compass bearing 
00355 {
00356     char *compass_points[9] = {"North", "N-East", "East", "S-East", "South", "S-West", "West", "N-West", "North"};
00357     signed short compass_bearing = (axis6.compass + 23) / 45;
00358     sprintf(str,"                                        "); //clears field (needed if previous string had more characters)
00359     sprintf(str, "Roll=%-d  Pitch=%-d  Yaw=%-d [%s]", axis6.roll, axis6.pitch, axis6.yaw, compass_points[compass_bearing]);   // 
00360 }   
00361 
00362 void getM3110Str(char * str)   // Mag3110 displayed in units of UT 
00363 {
00364     sprintf(str,"                                    "); //clears field (needed if previous string had more characters)
00365     sprintf(str, "X= %3.1f, Y= %3.1f, Z= %3.1f", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
00366 }   
00367 
00368 void getAltitudeStr(char * str)    // Get Altitude 
00369 {
00370     sprintf(str, "%+d meters", axis6.alt);   // str = integer portion of result 
00371 }   
00372 
00373 
00374 
00375 //*****************************************************************************
00376 //
00377 //! \brief  Main HTTP Server
00378 //!
00379 //! \param none
00380 //!
00381 //! \return none
00382 //!
00383 //
00384 //*****************************************************************************
00385 void serverMain(int port,
00386                 char * ipage,
00387                 cgi_handler * handleList,
00388                 dyn_html_handler * dhList)
00389 {
00390     static TCPSocketServer server;
00391     static TCPSocketConnection client;
00392 
00393     indexPage = ipage;
00394     chList = handleList;
00395     htmlList = dhList;
00396 
00397     server.bind(port);
00398 
00399     printf("Main HTTP server\r\n");
00400 
00401     // Start Listening
00402     if(server.listen() != 0);       // !!?? if statement is of no use - replace with server.listen();
00403 
00404     // Handle Clients and Data
00405     while(1)
00406     {
00407         int32_t status = server.accept(client);
00408         if (status >= 0)
00409         {
00410             LED_D2_ON;
00411             // Connection Accepted, Send Data
00412             // Wait for a data update
00413             client.set_blocking(true);
00414             printf("Connection\r\n");
00415 //            printf("Connection from: %s \r\n", client.get_address());
00416             if(newData)
00417             {
00418                 Systick_Allowed = 0;
00419 // Alternative to Systick_Allowed - more intrusive as it will entirely disable systicks
00420 //                SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;  // *** Disable SysTick Timer
00421                 handleHTTPRequest(&client);
00422             }
00423             newData = 0;
00424             client.close();
00425             LED_D2_OFF;
00426         }
00427         else if(status == -57)
00428         {
00429             // BUG: Socket inactive so reopen socket
00430             // Inactive Socket, close and reopen it
00431             printf("Oops!!!\r\n");
00432             server.close();
00433             indexPage = ipage;
00434             chList = handleList;
00435             htmlList = dhList;
00436             server.bind(port);
00437 
00438             // Start Listening
00439             if (server.listen() != 0);       // !!?? if statement is of no use - replace with server.listen();
00440         }
00441         Systick_Allowed = 1;
00442 //        SysTick->CTRL |=  SysTick_CTRL_TICKINT_Msk;  // *** Re-Enable SysTick Timer
00443     }
00444 }
00445 
00446 //*****************************************************************************
00447 //
00448 //! \brief  Handles HTTP Requests
00449 //!
00450 //! \param cnum is the client socket handle to be used
00451 //!
00452 //! \return none
00453 //!
00454 //
00455 //*****************************************************************************
00456 void handleHTTPRequest(TCPSocketConnection *client)
00457 {
00458     char * reqline[3];
00459     char * cgiTok;
00460 
00461     int i = 0;
00462     char paramBuf[20];
00463     int bytesRecvd;
00464     char tempStr[40]; //PF was 26
00465 
00466     memset(requestBuffer,0,sizeof (requestBuffer));
00467     bytesRecvd = client->receive(requestBuffer, sizeof(requestBuffer));
00468 
00469     printf("handleHTTPRequest\r\n");
00470 
00471     if(bytesRecvd > 0)
00472     {
00473         // Received some data, check it and send data back
00474         reqline[0] = strstr(requestBuffer, "GET");
00475         if ( reqline[0] != NULL )
00476         {
00477             if (strstr (requestBuffer, "HTTP/1.0") != NULL && strstr (requestBuffer, "HTTP/1.1") != NULL )
00478             {
00479                 client->send_all("HTTP/1.0 400 Bad Request\n", 25);
00480             }
00481             else
00482             {
00483 
00484 #ifdef HTTP_CGI_ENABLED
00485                 // Do we have CGI parameters we need to parse?
00486                 if(strchr(requestBuffer, '?') != NULL)
00487                 {
00488                     // Decode URL and handle each parameter sequentially
00489                     // according to table previously setup.
00490                     cgiTok = strstr(requestBuffer,"=");
00491                     if(cgiTok != NULL)
00492                     {
00493                         memset(paramBuf,0,sizeof(paramBuf));
00494                         memcpy(paramBuf,cgiTok+1,5);     // hard-coded for demo: 5 character parameter (-Red-/Green/Blue-)
00495                         chList->cgiHandlerFunc[0](paramBuf);
00496 
00497                     }
00498                 }
00499 #endif
00500 
00501 #ifdef HTTP_DYN_HTML_ENABLED
00502                 // The code below replaces data in the HTML page
00503                 // with that generated by the specified functions.
00504                 for(i = 0; i < 9; i++)  // change the range here for more dynamic fields on webpage
00505                 {
00506                     memset(tempStr,0,sizeof(tempStr));
00507                     htmlList->dynHtmlFunc[i](tempStr);
00508                     tempStr[strlen(tempStr)]= ' ';
00509                     pageReplace((char *)indexPage,
00510                                 (char *)htmlList->dynHtmlParamName[i],
00511                                 (char *)tempStr);
00512                 }
00513 #endif
00514                 viewCounter++;
00515                 sendHTTPData(HTTP_RESP, strlen(HTTP_RESP), client);
00516                                 
00517                 for(i = 0; i < strlen(indexPage); i += HTTP_TX_BLOCK_SIZE)
00518                 {
00519                     if(strlen(indexPage) - i < HTTP_TX_BLOCK_SIZE)
00520                     {
00521                         sendHTTPData(&indexPage[i], strlen(indexPage) - i, client);
00522                     }
00523                     else
00524                     {
00525                         sendHTTPData(&indexPage[i], HTTP_TX_BLOCK_SIZE, client);
00526                     }
00527                 }
00528             }
00529         }
00530     }
00531 }
00532 
00533 //*****************************************************************************
00534 //
00535 //! \brief  Inserts characters in page that appear after an indicator ind
00536 //! with the value from val
00537 //!
00538 //! \param  page is a pointer to the array holding the page's HTML code
00539 //! \param ind is a pointer to a string that has the name of the parameter on the page to modify
00540 //! \param val is the pointer to a string holding the string to insert in the XXX
00541 //!
00542 //! \return none
00543 //!
00544 //
00545 //*****************************************************************************
00546 void pageReplace(char * page, char * ind, char * val)
00547 {
00548     char * indicLoc;
00549     indicLoc = strstr (page,ind);
00550     memcpy(indicLoc+strlen(ind), val, strlen(val));
00551 }
00552 
00553 //*****************************************************************************
00554 //
00555 //! \brief  Returns a string with the number of views of the page
00556 //!
00557 //! \param  str is a pointer to the array where the number of views will be put
00558 //!
00559 //! \return none
00560 //!
00561 //
00562 //*****************************************************************************
00563 void getViewsNum(char * str)
00564 {
00565     sprintf(str, "%d", viewCounter);
00566 }
00567 
00568 //*****************************************************************************
00569 //
00570 //! \brief  Sends HTTP Data
00571 //!
00572 //! \param sdesc is the socket descriptor of the socket used for sending data
00573 //! \param buf is a pointer to the buffer with the data to be sent
00574 //! \param len is the number of bytes to send
00575 //!
00576 //! \return none
00577 //!
00578 //
00579 //*****************************************************************************
00580 void sendHTTPData(char * buf, long len, TCPSocketConnection *client)
00581 {
00582     int bytesSent = -2;
00583     while(bytesSent == -2) bytesSent = client->send_all(buf, len);
00584 }
00585 
00586 
00587