David Fletcher / Mbed 2 deprecated CC3000WebServer

Dependencies:   mbed

Committer:
dflet
Date:
Sat Sep 14 17:38:41 2013 +0000
Revision:
0:6ad60d78b315
Child:
2:e6a185df9e4c
Mostly working will serve the default pages index.html and config.html, but config.html is not woring at present.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:6ad60d78b315 1 /*****************************************************************************
dflet 0:6ad60d78b315 2 *
dflet 0:6ad60d78b315 3 * WebServerApp.c - CC3000 Slim Driver Implementation.
dflet 0:6ad60d78b315 4 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:6ad60d78b315 5 *
dflet 0:6ad60d78b315 6 * Redistribution and use in source and binary forms, with or without
dflet 0:6ad60d78b315 7 * modification, are permitted provided that the following conditions
dflet 0:6ad60d78b315 8 * are met:
dflet 0:6ad60d78b315 9 *
dflet 0:6ad60d78b315 10 * Redistributions of source code must retain the above copyright
dflet 0:6ad60d78b315 11 * notice, this list of conditions and the following disclaimer.
dflet 0:6ad60d78b315 12 *
dflet 0:6ad60d78b315 13 * Redistributions in binary form must reproduce the above copyright
dflet 0:6ad60d78b315 14 * notice, this list of conditions and the following disclaimer in the
dflet 0:6ad60d78b315 15 * documentation and/or other materials provided with the
dflet 0:6ad60d78b315 16 * distribution.
dflet 0:6ad60d78b315 17 *
dflet 0:6ad60d78b315 18 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:6ad60d78b315 19 * its contributors may be used to endorse or promote products derived
dflet 0:6ad60d78b315 20 * from this software without specific prior written permission.
dflet 0:6ad60d78b315 21 *
dflet 0:6ad60d78b315 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:6ad60d78b315 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:6ad60d78b315 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:6ad60d78b315 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:6ad60d78b315 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:6ad60d78b315 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:6ad60d78b315 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:6ad60d78b315 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:6ad60d78b315 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:6ad60d78b315 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:6ad60d78b315 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:6ad60d78b315 33 *
dflet 0:6ad60d78b315 34 *****************************************************************************/
dflet 0:6ad60d78b315 35 #include "mbed.h"
dflet 0:6ad60d78b315 36 #include "wlan.h"
dflet 0:6ad60d78b315 37 #include "evnt_handler.h" // callback function declaration
dflet 0:6ad60d78b315 38 #include "nvmem.h"
dflet 0:6ad60d78b315 39 #include "socket.h"
dflet 0:6ad60d78b315 40 #include "netapp.h"
dflet 0:6ad60d78b315 41 #include "host_driver_version.h"
dflet 0:6ad60d78b315 42 #include "cc3000.h"
dflet 0:6ad60d78b315 43 #include "Common.h"
dflet 0:6ad60d78b315 44 #include "demo_config.h"
dflet 0:6ad60d78b315 45 #include "HttpString.h"
dflet 0:6ad60d78b315 46
dflet 0:6ad60d78b315 47 //#include <msp430.h>
dflet 0:6ad60d78b315 48 #include "Board.h"
dflet 0:6ad60d78b315 49 #include "HttpCore.h"
dflet 0:6ad60d78b315 50 //#include "Wheel.h"
dflet 0:6ad60d78b315 51 #include "dispatcher.h"
dflet 0:6ad60d78b315 52
dflet 0:6ad60d78b315 53 #define FALSE 0
dflet 0:6ad60d78b315 54 #define SERVERAPPVERSION "v1.10.11.6.8.1"
dflet 0:6ad60d78b315 55
dflet 0:6ad60d78b315 56 volatile unsigned long ulCC3000Connected,ulCC3000DHCP,OkToDoShutDown;
dflet 0:6ad60d78b315 57 unsigned char obtainIpInfoFlag = FALSE;
dflet 0:6ad60d78b315 58 unsigned char ConnectUsingSmartConfig;
dflet 0:6ad60d78b315 59 tNetappIpconfigRetArgs *ipconfig;
dflet 0:6ad60d78b315 60 char DevServname[] = {'C','C','3','0','0','0'};
dflet 0:6ad60d78b315 61 volatile char runSmartConfig = 0;
dflet 0:6ad60d78b315 62 unsigned char mDNSValid, skipCount, mDNSSend;
dflet 0:6ad60d78b315 63 int8_t isInitialized = FALSE;
dflet 0:6ad60d78b315 64
dflet 0:6ad60d78b315 65 void ManualConnect(void);
dflet 0:6ad60d78b315 66
dflet 0:6ad60d78b315 67 int String_utoa(int uNum, char *pString)
dflet 0:6ad60d78b315 68 {
dflet 0:6ad60d78b315 69 char digits[] = "0123456789";
dflet 0:6ad60d78b315 70 char* ptr;
dflet 0:6ad60d78b315 71 int uLength;
dflet 0:6ad60d78b315 72 int uTemp = uNum;
dflet 0:6ad60d78b315 73
dflet 0:6ad60d78b315 74 // value 0 is a special format
dflet 0:6ad60d78b315 75 if (uNum == 0)
dflet 0:6ad60d78b315 76 {
dflet 0:6ad60d78b315 77 uLength = 1;
dflet 0:6ad60d78b315 78 *pString = '0';
dflet 0:6ad60d78b315 79 return uLength;
dflet 0:6ad60d78b315 80 }
dflet 0:6ad60d78b315 81
dflet 0:6ad60d78b315 82 // Find out the length of the number, in decimal base
dflet 0:6ad60d78b315 83 uLength = 0;
dflet 0:6ad60d78b315 84 while (uTemp > 0)
dflet 0:6ad60d78b315 85 {
dflet 0:6ad60d78b315 86 uTemp /= 10;
dflet 0:6ad60d78b315 87 uLength++;
dflet 0:6ad60d78b315 88 }
dflet 0:6ad60d78b315 89
dflet 0:6ad60d78b315 90 // Do the actual formatting, right to left
dflet 0:6ad60d78b315 91 uTemp = uNum;
dflet 0:6ad60d78b315 92 ptr = (char*)pString + uLength;
dflet 0:6ad60d78b315 93 while (uTemp > 0)
dflet 0:6ad60d78b315 94 {
dflet 0:6ad60d78b315 95 --ptr;
dflet 0:6ad60d78b315 96 *ptr = digits[uTemp % 10];
dflet 0:6ad60d78b315 97 uTemp /= 10;
dflet 0:6ad60d78b315 98 }
dflet 0:6ad60d78b315 99 return uLength;
dflet 0:6ad60d78b315 100 }
dflet 0:6ad60d78b315 101
dflet 0:6ad60d78b315 102 int iptostring(unsigned char *ip, char *ipstring)
dflet 0:6ad60d78b315 103 {
dflet 0:6ad60d78b315 104 int temp,i, length, uLength;
dflet 0:6ad60d78b315 105 char *ptr;
dflet 0:6ad60d78b315 106 ip =ip +3;
dflet 0:6ad60d78b315 107 ptr = ipstring;
dflet 0:6ad60d78b315 108 uLength = 0;
dflet 0:6ad60d78b315 109 for (i=0; i<4; i++)
dflet 0:6ad60d78b315 110 {
dflet 0:6ad60d78b315 111 temp = *ip;
dflet 0:6ad60d78b315 112 length = String_utoa((unsigned long) temp, ptr);
dflet 0:6ad60d78b315 113 ptr = ptr + length;
dflet 0:6ad60d78b315 114 uLength += length;
dflet 0:6ad60d78b315 115 *ptr = '.';
dflet 0:6ad60d78b315 116 ptr++;
dflet 0:6ad60d78b315 117 uLength++;
dflet 0:6ad60d78b315 118 ip--;
dflet 0:6ad60d78b315 119 }
dflet 0:6ad60d78b315 120 return (uLength-1);
dflet 0:6ad60d78b315 121 }
dflet 0:6ad60d78b315 122
dflet 0:6ad60d78b315 123 //*****************************************************************************
dflet 0:6ad60d78b315 124 //
dflet 0:6ad60d78b315 125 //! main
dflet 0:6ad60d78b315 126 //!
dflet 0:6ad60d78b315 127 //! \param None
dflet 0:6ad60d78b315 128 //!
dflet 0:6ad60d78b315 129 //! \return none
dflet 0:6ad60d78b315 130 //!
dflet 0:6ad60d78b315 131 //! \brief The main loop is executed here
dflet 0:6ad60d78b315 132 //
dflet 0:6ad60d78b315 133 //*****************************************************************************
dflet 0:6ad60d78b315 134
dflet 0:6ad60d78b315 135 int main()
dflet 0:6ad60d78b315 136 {
dflet 0:6ad60d78b315 137 char ipvalue[15];
dflet 0:6ad60d78b315 138 int length;
dflet 0:6ad60d78b315 139 runSmartConfig = 0;
dflet 0:6ad60d78b315 140 obtainIpInfoFlag = 0;
dflet 0:6ad60d78b315 141 skipCount = 0;
dflet 0:6ad60d78b315 142 mDNSValid = 0;
dflet 0:6ad60d78b315 143 mDNSSend =0;
dflet 0:6ad60d78b315 144 unsigned char buff[5];
dflet 0:6ad60d78b315 145 int rval;
dflet 0:6ad60d78b315 146 //Stop watch dog timer, In IAR this is done in low_level_init.c file
dflet 0:6ad60d78b315 147 //#ifdef __CCS__
dflet 0:6ad60d78b315 148 // WDTCTL = WDTPW + WDTHOLD;
dflet 0:6ad60d78b315 149 //#endif
dflet 0:6ad60d78b315 150
dflet 0:6ad60d78b315 151 // Start CC3000 State Machine
dflet 0:6ad60d78b315 152 resetCC3000StateMachine();
dflet 0:6ad60d78b315 153
dflet 0:6ad60d78b315 154 //
dflet 0:6ad60d78b315 155 // Board Initialization start
dflet 0:6ad60d78b315 156 //
dflet 0:6ad60d78b315 157
dflet 0:6ad60d78b315 158 initDriver();
dflet 0:6ad60d78b315 159 isInitialized = TRUE; // For manual connect
dflet 0:6ad60d78b315 160 //Wheel_init();
dflet 0:6ad60d78b315 161 //Wheel_enable();
dflet 0:6ad60d78b315 162 //__enable_interrupt();
dflet 0:6ad60d78b315 163 DispatcherUartSendPacket("\r\n\r\n--Initializing Application--",32);
dflet 0:6ad60d78b315 164 DispatcherUartSendPacket("\r\nWeb Server App Version=",25);
dflet 0:6ad60d78b315 165 DispatcherUartSendPacket(SERVERAPPVERSION,14);
dflet 0:6ad60d78b315 166 DispatcherUartSendPacket("\r\n", 2);
dflet 0:6ad60d78b315 167
dflet 0:6ad60d78b315 168 if (nvmem_read_sp_version(buff)!=0){
dflet 0:6ad60d78b315 169 printf("nvmem_read_sp_version failed...\r\n");
dflet 0:6ad60d78b315 170 }
dflet 0:6ad60d78b315 171 else
dflet 0:6ad60d78b315 172 {
dflet 0:6ad60d78b315 173 printf("Firmware version: %d.%d\r\n",buff[0], buff[1]);
dflet 0:6ad60d78b315 174 }
dflet 0:6ad60d78b315 175
dflet 0:6ad60d78b315 176
dflet 0:6ad60d78b315 177 while(1)
dflet 0:6ad60d78b315 178 {
dflet 0:6ad60d78b315 179 // Perform Smart Config if button pressed
dflet 0:6ad60d78b315 180 if(runSmartConfig == 1)
dflet 0:6ad60d78b315 181 {
dflet 0:6ad60d78b315 182 mDNSValid = 0;
dflet 0:6ad60d78b315 183 // Turn Off PAD LED's
dflet 0:6ad60d78b315 184 turnLedOff(ind1);
dflet 0:6ad60d78b315 185 turnLedOff(ind2);
dflet 0:6ad60d78b315 186 turnLedOff(ind3);
dflet 0:6ad60d78b315 187 turnLedOff(ind4);
dflet 0:6ad60d78b315 188 turnLedOff(LED8);
dflet 0:6ad60d78b315 189
dflet 0:6ad60d78b315 190 // Start the Smart Config Process
dflet 0:6ad60d78b315 191 StartSmartConfig();
dflet 0:6ad60d78b315 192 runSmartConfig = 0;
dflet 0:6ad60d78b315 193 }
dflet 0:6ad60d78b315 194
dflet 0:6ad60d78b315 195 if(!(currentCC3000State() & CC3000_ASSOC))
dflet 0:6ad60d78b315 196 {
dflet 0:6ad60d78b315 197 //Connect via profile (unsol connect) or :-
dflet 0:6ad60d78b315 198 // ManualConnect();
dflet 0:6ad60d78b315 199
dflet 0:6ad60d78b315 200 // Wait until connection is finished
dflet 0:6ad60d78b315 201 while (!(currentCC3000State() & CC3000_ASSOC))
dflet 0:6ad60d78b315 202 {
dflet 0:6ad60d78b315 203
dflet 0:6ad60d78b315 204 wait_us(5);
dflet 0:6ad60d78b315 205 //__delay_cycles(100);
dflet 0:6ad60d78b315 206 // Check if user pressed button to do Smart Config
dflet 0:6ad60d78b315 207 if(runSmartConfig == 1)
dflet 0:6ad60d78b315 208 break;
dflet 0:6ad60d78b315 209 }
dflet 0:6ad60d78b315 210 }
dflet 0:6ad60d78b315 211
dflet 0:6ad60d78b315 212 // Print out connection status
dflet 0:6ad60d78b315 213 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == FALSE)
dflet 0:6ad60d78b315 214 {
dflet 0:6ad60d78b315 215 // Set flag so we don't constantly obtain the IP info
dflet 0:6ad60d78b315 216 obtainIpInfoFlag = TRUE;
dflet 0:6ad60d78b315 217 turnLedOn(CC3000_IP_ALLOC_IND);
dflet 0:6ad60d78b315 218 }
dflet 0:6ad60d78b315 219
dflet 0:6ad60d78b315 220 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == TRUE)
dflet 0:6ad60d78b315 221 {
dflet 0:6ad60d78b315 222 //If smart config was done was broadcast mdns signal
dflet 0:6ad60d78b315 223 if (ConnectUsingSmartConfig==1)
dflet 0:6ad60d78b315 224 {
dflet 0:6ad60d78b315 225 mdnsAdvertiser(1,DevServname, sizeof(DevServname));
dflet 0:6ad60d78b315 226 ConnectUsingSmartConfig = 0;
dflet 0:6ad60d78b315 227 }
dflet 0:6ad60d78b315 228 mDNSValid =1;
dflet 0:6ad60d78b315 229 skipCount =0;
dflet 0:6ad60d78b315 230 ipconfig = getCC3000Info();
dflet 0:6ad60d78b315 231 length = iptostring(ipconfig->aucIP, ipvalue);
dflet 0:6ad60d78b315 232 DispatcherUartSendPacket("\r\nIP = ", 7);
dflet 0:6ad60d78b315 233 DispatcherUartSendPacket(ipvalue, length);
dflet 0:6ad60d78b315 234 DispatcherUartSendPacket("\r\n", 2);
dflet 0:6ad60d78b315 235 //Initialize and start the HTTP server
dflet 0:6ad60d78b315 236 HttpServerInitAndRun();
dflet 0:6ad60d78b315 237 }
dflet 0:6ad60d78b315 238 }
dflet 0:6ad60d78b315 239 return 1;
dflet 0:6ad60d78b315 240 }
dflet 0:6ad60d78b315 241
dflet 0:6ad60d78b315 242 /************************************************************************************
dflet 0:6ad60d78b315 243 // This is an example of how you'd connect the CC3000 to an AP without using
dflet 0:6ad60d78b315 244 // Smart Config or a stored profile.
dflet 0:6ad60d78b315 245 //
dflet 0:6ad60d78b315 246 // All the code above wlan_connect() is just for this demo program; if you're
dflet 0:6ad60d78b315 247 // always going to connect to your network this way you wouldn't need it.
dflet 0:6ad60d78b315 248 ************************************************************************************/
dflet 0:6ad60d78b315 249
dflet 0:6ad60d78b315 250 void ManualConnect() {
dflet 0:6ad60d78b315 251
dflet 0:6ad60d78b315 252 char ssidName[] = "*********";
dflet 0:6ad60d78b315 253 //char AP_KEY[] = "********";//Refer to demo_config.h
dflet 0:6ad60d78b315 254 int8_t rval;
dflet 0:6ad60d78b315 255
dflet 0:6ad60d78b315 256 if (!isInitialized) {
dflet 0:6ad60d78b315 257 printf("CC3000 not initialized; can't run manual connect.\r\n");
dflet 0:6ad60d78b315 258 return;
dflet 0:6ad60d78b315 259 }
dflet 0:6ad60d78b315 260
dflet 0:6ad60d78b315 261 printf("Starting manual connect...\r\n");
dflet 0:6ad60d78b315 262
dflet 0:6ad60d78b315 263 printf((" Disabling auto-connect policy...\r\n"));
dflet 0:6ad60d78b315 264 rval = wlan_ioctl_set_connection_policy(0, 0, 0);
dflet 0:6ad60d78b315 265
dflet 0:6ad60d78b315 266 printf(" Deleting all existing profiles...\r\n");
dflet 0:6ad60d78b315 267 rval = wlan_ioctl_del_profile(255);
dflet 0:6ad60d78b315 268
dflet 0:6ad60d78b315 269 printf(" Waiting until disconnected...\r\n");
dflet 0:6ad60d78b315 270 while (ulCC3000Connected == 1) {
dflet 0:6ad60d78b315 271 wait_ms(100);
dflet 0:6ad60d78b315 272 }
dflet 0:6ad60d78b315 273
dflet 0:6ad60d78b315 274 printf(" Manually connecting...\r\n");
dflet 0:6ad60d78b315 275
dflet 0:6ad60d78b315 276 // Parameter 1 is the security type: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
dflet 0:6ad60d78b315 277 // WLAN_SEC_WPA or WLAN_SEC_WPA2
dflet 0:6ad60d78b315 278 // Parameter 3 is the MAC adddress of the AP. All the TI examples
dflet 0:6ad60d78b315 279 // use NULL. I suppose you would want to specify this
dflet 0:6ad60d78b315 280 // if you were security paranoid.
dflet 0:6ad60d78b315 281 rval = wlan_connect(WLAN_SEC_WPA2,
dflet 0:6ad60d78b315 282 ssidName,
dflet 0:6ad60d78b315 283 strlen(ssidName),
dflet 0:6ad60d78b315 284 NULL,
dflet 0:6ad60d78b315 285 (unsigned char *)AP_KEY,
dflet 0:6ad60d78b315 286 strlen(AP_KEY));
dflet 0:6ad60d78b315 287
dflet 0:6ad60d78b315 288 if (rval==0) {
dflet 0:6ad60d78b315 289 printf(" Manual connect success.\r\n");
dflet 0:6ad60d78b315 290 }
dflet 0:6ad60d78b315 291 else {
dflet 0:6ad60d78b315 292 printf(" Unusual return value: ");
dflet 0:6ad60d78b315 293 printf("%d\r\n",rval);
dflet 0:6ad60d78b315 294 }
dflet 0:6ad60d78b315 295 return;
dflet 0:6ad60d78b315 296 }