David Fletcher / Mbed 2 deprecated CC3000WebServer

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WebServerApp.cpp Source File

WebServerApp.cpp

00001 /*****************************************************************************
00002 *
00003 *  WebServerApp.c - CC3000 Slim Driver Implementation.
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 #include "mbed.h"
00036 #include "wlan.h" 
00037 #include "evnt_handler.h"    // callback function declaration
00038 #include "nvmem.h"
00039 #include "socket.h"
00040 #include "netapp.h"
00041 #include "host_driver_version.h"
00042 #include "cc3000.h"
00043 #include "Common.h"
00044 #include "demo_config.h"
00045 #include "HttpString.h"
00046 
00047 //#include <msp430.h>
00048 #include "Board.h"
00049 #include "HttpCore.h"
00050 #include "Wheel.h"
00051 #include "dispatcher.h"
00052 
00053 #define FALSE 0
00054 #define SERVERAPPVERSION "v1.10.11.6.8.1"
00055 
00056 //ADC pin is pin20
00057 
00058 volatile unsigned long ulCC3000Connected,ulCC3000DHCP,OkToDoShutDown;
00059 unsigned char obtainIpInfoFlag = FALSE;
00060 unsigned char ConnectUsingSmartConfig;
00061 tNetappIpconfigRetArgs *ipconfig;
00062 char DevServname[] = {'C','C','3','0','0','0'};
00063 volatile char runSmartConfig = 0;
00064 unsigned char mDNSValid, skipCount, mDNSSend;
00065 int8_t isInitialized = FALSE;
00066 
00067 void ManualConnect(void);
00068 
00069 int String_utoa(int uNum, char *pString)
00070 {
00071     char digits[] = "0123456789";
00072     char* ptr;
00073     int uLength;
00074     int uTemp = uNum;
00075 
00076     // value 0 is a special format
00077     if (uNum == 0)
00078     {
00079         uLength = 1;
00080         *pString = '0';
00081         return uLength;
00082     }
00083 
00084     // Find out the length of the number, in decimal base
00085     uLength = 0;
00086     while (uTemp > 0)
00087     {
00088         uTemp /= 10;
00089         uLength++;
00090     }
00091 
00092     // Do the actual formatting, right to left
00093     uTemp = uNum;
00094     ptr = (char*)pString + uLength;
00095     while (uTemp > 0)
00096     {
00097         --ptr;
00098         *ptr = digits[uTemp % 10];
00099         uTemp /= 10;
00100     }
00101     return uLength;
00102 }
00103 
00104 int iptostring(unsigned char *ip, char *ipstring)
00105 {
00106   int temp,i, length, uLength;
00107   char *ptr;
00108   ip =ip +3;
00109   ptr = ipstring;
00110   uLength = 0;
00111   for (i=0; i<4; i++)
00112   {
00113     temp = *ip;
00114     length = String_utoa((unsigned long) temp, ptr);
00115     ptr = ptr + length;
00116     uLength += length;
00117     *ptr = '.';
00118     ptr++;
00119     uLength++;
00120     ip--;
00121   }
00122   return (uLength-1);  
00123 }
00124 
00125 //*****************************************************************************
00126 //
00127 //! main
00128 //!
00129 //!  \param  None
00130 //!
00131 //!  \return none
00132 //!
00133 //!  \brief  The main loop is executed here
00134 //
00135 //*****************************************************************************
00136 
00137 int main()
00138 {
00139   char ipvalue[15];
00140   int length;
00141   runSmartConfig = 0;
00142   obtainIpInfoFlag = 0;
00143   skipCount = 0;
00144   mDNSValid = 0;
00145   mDNSSend =0;
00146   unsigned char buff[5];
00147   int rval;
00148   //Stop watch dog timer, In IAR this is done in low_level_init.c file
00149 //#ifdef __CCS__
00150 //  WDTCTL = WDTPW + WDTHOLD;
00151 //#endif
00152 
00153     // Start CC3000 State Machine
00154     resetCC3000StateMachine();
00155 
00156     //
00157     //  Board Initialization start
00158     //
00159         
00160     initDriver();
00161     isInitialized = TRUE; // For manual connect
00162     Wheel_init();
00163     Wheel_enable();
00164     //__enable_interrupt();
00165     DispatcherUartSendPacket("\r\n\r\n--Initializing Application--",32);
00166     DispatcherUartSendPacket("\r\nWeb Server App Version=",25);
00167     DispatcherUartSendPacket(SERVERAPPVERSION,14);
00168     DispatcherUartSendPacket("\r\n", 2);
00169     
00170     if (nvmem_read_sp_version(buff)!=0){
00171        printf("nvmem_read_sp_version failed...\r\n"); 
00172        }
00173        else
00174        {
00175        printf("Firmware version: %d.%d\r\n",buff[0], buff[1]);
00176        } 
00177        
00178     
00179     while(1)
00180     {
00181         // Perform Smart Config if button pressed
00182                 if(runSmartConfig == 1)
00183                 {
00184                     mDNSValid = 0;
00185                     // Turn Off PAD LED's
00186                     turnLedOff(ind1);
00187                     turnLedOff(ind2);
00188                     turnLedOff(ind3);
00189                     turnLedOff(ind4);
00190                     turnLedOff(LED8);
00191 
00192                     // Start the Smart Config Process
00193                     StartSmartConfig();
00194                     runSmartConfig = 0;
00195                 }
00196 
00197                 if(!(currentCC3000State() & CC3000_ASSOC))
00198                 {
00199                    //Connect via profile (unsol connect) or :-
00200                    // ManualConnect();
00201                     
00202                     // Wait until connection is finished
00203                     while (!(currentCC3000State() & CC3000_ASSOC))
00204                     {
00205                         
00206                         wait_us(5);
00207                         //__delay_cycles(100);
00208                         // Check if user pressed button to do Smart Config
00209                         if(runSmartConfig == 1)
00210                             break;
00211                     }
00212                 }
00213 
00214                 // Print out connection status
00215                 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == FALSE)
00216                 {
00217                     // Set flag so we don't constantly obtain the IP info
00218                     obtainIpInfoFlag = TRUE;
00219                     turnLedOn(CC3000_IP_ALLOC_IND);
00220                  }
00221 
00222                 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == TRUE)
00223                 {
00224                     //If smart config was done was broadcast mdns signal
00225                     if (ConnectUsingSmartConfig==1)
00226                     {
00227                         mdnsAdvertiser(1,DevServname, sizeof(DevServname));
00228                         ConnectUsingSmartConfig = 0;
00229                     }
00230                         mDNSValid =1;
00231                         skipCount =0;
00232                         ipconfig = getCC3000Info();
00233                         length = iptostring(ipconfig->aucIP, ipvalue);
00234                         DispatcherUartSendPacket("\r\nIP = ", 7);
00235                         DispatcherUartSendPacket(ipvalue, length);
00236                         DispatcherUartSendPacket("\r\n", 2);
00237                         //Initialize and start the HTTP server
00238                         HttpServerInitAndRun();
00239                 }
00240     }
00241     return 1;
00242 }
00243 
00244 /************************************************************************************
00245 //    This is an example of how you'd connect the CC3000 to an AP without using
00246 //    Smart Config or a stored profile.
00247 //    
00248 //    All the code above wlan_connect() is just for this demo program; if you're
00249 //    always going to connect to your network this way you wouldn't need it.
00250 ************************************************************************************/
00251 
00252 void ManualConnect() {
00253 
00254     char ssidName[] = "*********";
00255     //char AP_KEY[] = "********";//Refer to demo_config.h
00256     int8_t rval;
00257 
00258     if (!isInitialized) {
00259         printf("CC3000 not initialized; can't run manual connect.\r\n");
00260         return;
00261         }
00262 
00263     printf("Starting manual connect...\r\n");
00264     
00265     printf(("  Disabling auto-connect policy...\r\n"));
00266     rval = wlan_ioctl_set_connection_policy(0, 0, 0);
00267 
00268     printf("  Deleting all existing profiles...\r\n");
00269     rval = wlan_ioctl_del_profile(255);
00270 
00271     printf("  Waiting until disconnected...\r\n");
00272     while (ulCC3000Connected == 1) {
00273         wait_ms(100);
00274         }
00275     
00276     printf("  Manually connecting...\r\n");
00277 
00278     // Parameter 1 is the security type: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
00279     //              WLAN_SEC_WPA or WLAN_SEC_WPA2
00280     // Parameter 3 is the MAC adddress of the AP. All the TI examples
00281     //              use NULL. I suppose you would want to specify this
00282     //              if you were security paranoid.
00283     rval = wlan_connect(WLAN_SEC_WPA2,
00284                 ssidName,
00285                 strlen(ssidName),
00286                 NULL,
00287                 (unsigned char *)AP_KEY,
00288                 strlen(AP_KEY));
00289 
00290     if (rval==0) {
00291         printf("  Manual connect success.\r\n");
00292         }
00293     else {
00294         printf("  Unusual return value: ");
00295         printf("%d\r\n",rval);
00296         }
00297         return;
00298     }