A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Committer:
dflet
Date:
Mon Sep 16 18:37:14 2013 +0000
Revision:
2:e6a185df9e4c
Parent:
0:6ad60d78b315
ADC and Leds now work on board and config.html page.

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 2:e6a185df9e4c 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 2:e6a185df9e4c 56 //ADC pin is pin20
dflet 2:e6a185df9e4c 57
dflet 0:6ad60d78b315 58 volatile unsigned long ulCC3000Connected,ulCC3000DHCP,OkToDoShutDown;
dflet 0:6ad60d78b315 59 unsigned char obtainIpInfoFlag = FALSE;
dflet 0:6ad60d78b315 60 unsigned char ConnectUsingSmartConfig;
dflet 0:6ad60d78b315 61 tNetappIpconfigRetArgs *ipconfig;
dflet 0:6ad60d78b315 62 char DevServname[] = {'C','C','3','0','0','0'};
dflet 0:6ad60d78b315 63 volatile char runSmartConfig = 0;
dflet 0:6ad60d78b315 64 unsigned char mDNSValid, skipCount, mDNSSend;
dflet 0:6ad60d78b315 65 int8_t isInitialized = FALSE;
dflet 0:6ad60d78b315 66
dflet 0:6ad60d78b315 67 void ManualConnect(void);
dflet 0:6ad60d78b315 68
dflet 0:6ad60d78b315 69 int String_utoa(int uNum, char *pString)
dflet 0:6ad60d78b315 70 {
dflet 0:6ad60d78b315 71 char digits[] = "0123456789";
dflet 0:6ad60d78b315 72 char* ptr;
dflet 0:6ad60d78b315 73 int uLength;
dflet 0:6ad60d78b315 74 int uTemp = uNum;
dflet 0:6ad60d78b315 75
dflet 0:6ad60d78b315 76 // value 0 is a special format
dflet 0:6ad60d78b315 77 if (uNum == 0)
dflet 0:6ad60d78b315 78 {
dflet 0:6ad60d78b315 79 uLength = 1;
dflet 0:6ad60d78b315 80 *pString = '0';
dflet 0:6ad60d78b315 81 return uLength;
dflet 0:6ad60d78b315 82 }
dflet 0:6ad60d78b315 83
dflet 0:6ad60d78b315 84 // Find out the length of the number, in decimal base
dflet 0:6ad60d78b315 85 uLength = 0;
dflet 0:6ad60d78b315 86 while (uTemp > 0)
dflet 0:6ad60d78b315 87 {
dflet 0:6ad60d78b315 88 uTemp /= 10;
dflet 0:6ad60d78b315 89 uLength++;
dflet 0:6ad60d78b315 90 }
dflet 0:6ad60d78b315 91
dflet 0:6ad60d78b315 92 // Do the actual formatting, right to left
dflet 0:6ad60d78b315 93 uTemp = uNum;
dflet 0:6ad60d78b315 94 ptr = (char*)pString + uLength;
dflet 0:6ad60d78b315 95 while (uTemp > 0)
dflet 0:6ad60d78b315 96 {
dflet 0:6ad60d78b315 97 --ptr;
dflet 0:6ad60d78b315 98 *ptr = digits[uTemp % 10];
dflet 0:6ad60d78b315 99 uTemp /= 10;
dflet 0:6ad60d78b315 100 }
dflet 0:6ad60d78b315 101 return uLength;
dflet 0:6ad60d78b315 102 }
dflet 0:6ad60d78b315 103
dflet 0:6ad60d78b315 104 int iptostring(unsigned char *ip, char *ipstring)
dflet 0:6ad60d78b315 105 {
dflet 0:6ad60d78b315 106 int temp,i, length, uLength;
dflet 0:6ad60d78b315 107 char *ptr;
dflet 0:6ad60d78b315 108 ip =ip +3;
dflet 0:6ad60d78b315 109 ptr = ipstring;
dflet 0:6ad60d78b315 110 uLength = 0;
dflet 0:6ad60d78b315 111 for (i=0; i<4; i++)
dflet 0:6ad60d78b315 112 {
dflet 0:6ad60d78b315 113 temp = *ip;
dflet 0:6ad60d78b315 114 length = String_utoa((unsigned long) temp, ptr);
dflet 0:6ad60d78b315 115 ptr = ptr + length;
dflet 0:6ad60d78b315 116 uLength += length;
dflet 0:6ad60d78b315 117 *ptr = '.';
dflet 0:6ad60d78b315 118 ptr++;
dflet 0:6ad60d78b315 119 uLength++;
dflet 0:6ad60d78b315 120 ip--;
dflet 0:6ad60d78b315 121 }
dflet 0:6ad60d78b315 122 return (uLength-1);
dflet 0:6ad60d78b315 123 }
dflet 0:6ad60d78b315 124
dflet 0:6ad60d78b315 125 //*****************************************************************************
dflet 0:6ad60d78b315 126 //
dflet 0:6ad60d78b315 127 //! main
dflet 0:6ad60d78b315 128 //!
dflet 0:6ad60d78b315 129 //! \param None
dflet 0:6ad60d78b315 130 //!
dflet 0:6ad60d78b315 131 //! \return none
dflet 0:6ad60d78b315 132 //!
dflet 0:6ad60d78b315 133 //! \brief The main loop is executed here
dflet 0:6ad60d78b315 134 //
dflet 0:6ad60d78b315 135 //*****************************************************************************
dflet 0:6ad60d78b315 136
dflet 0:6ad60d78b315 137 int main()
dflet 0:6ad60d78b315 138 {
dflet 0:6ad60d78b315 139 char ipvalue[15];
dflet 0:6ad60d78b315 140 int length;
dflet 0:6ad60d78b315 141 runSmartConfig = 0;
dflet 0:6ad60d78b315 142 obtainIpInfoFlag = 0;
dflet 0:6ad60d78b315 143 skipCount = 0;
dflet 0:6ad60d78b315 144 mDNSValid = 0;
dflet 0:6ad60d78b315 145 mDNSSend =0;
dflet 0:6ad60d78b315 146 unsigned char buff[5];
dflet 0:6ad60d78b315 147 int rval;
dflet 0:6ad60d78b315 148 //Stop watch dog timer, In IAR this is done in low_level_init.c file
dflet 0:6ad60d78b315 149 //#ifdef __CCS__
dflet 0:6ad60d78b315 150 // WDTCTL = WDTPW + WDTHOLD;
dflet 0:6ad60d78b315 151 //#endif
dflet 0:6ad60d78b315 152
dflet 0:6ad60d78b315 153 // Start CC3000 State Machine
dflet 0:6ad60d78b315 154 resetCC3000StateMachine();
dflet 0:6ad60d78b315 155
dflet 0:6ad60d78b315 156 //
dflet 0:6ad60d78b315 157 // Board Initialization start
dflet 0:6ad60d78b315 158 //
dflet 0:6ad60d78b315 159
dflet 0:6ad60d78b315 160 initDriver();
dflet 0:6ad60d78b315 161 isInitialized = TRUE; // For manual connect
dflet 2:e6a185df9e4c 162 Wheel_init();
dflet 2:e6a185df9e4c 163 Wheel_enable();
dflet 0:6ad60d78b315 164 //__enable_interrupt();
dflet 0:6ad60d78b315 165 DispatcherUartSendPacket("\r\n\r\n--Initializing Application--",32);
dflet 0:6ad60d78b315 166 DispatcherUartSendPacket("\r\nWeb Server App Version=",25);
dflet 0:6ad60d78b315 167 DispatcherUartSendPacket(SERVERAPPVERSION,14);
dflet 0:6ad60d78b315 168 DispatcherUartSendPacket("\r\n", 2);
dflet 0:6ad60d78b315 169
dflet 0:6ad60d78b315 170 if (nvmem_read_sp_version(buff)!=0){
dflet 0:6ad60d78b315 171 printf("nvmem_read_sp_version failed...\r\n");
dflet 0:6ad60d78b315 172 }
dflet 0:6ad60d78b315 173 else
dflet 0:6ad60d78b315 174 {
dflet 0:6ad60d78b315 175 printf("Firmware version: %d.%d\r\n",buff[0], buff[1]);
dflet 0:6ad60d78b315 176 }
dflet 0:6ad60d78b315 177
dflet 0:6ad60d78b315 178
dflet 0:6ad60d78b315 179 while(1)
dflet 0:6ad60d78b315 180 {
dflet 0:6ad60d78b315 181 // Perform Smart Config if button pressed
dflet 0:6ad60d78b315 182 if(runSmartConfig == 1)
dflet 0:6ad60d78b315 183 {
dflet 0:6ad60d78b315 184 mDNSValid = 0;
dflet 0:6ad60d78b315 185 // Turn Off PAD LED's
dflet 0:6ad60d78b315 186 turnLedOff(ind1);
dflet 0:6ad60d78b315 187 turnLedOff(ind2);
dflet 0:6ad60d78b315 188 turnLedOff(ind3);
dflet 0:6ad60d78b315 189 turnLedOff(ind4);
dflet 0:6ad60d78b315 190 turnLedOff(LED8);
dflet 0:6ad60d78b315 191
dflet 0:6ad60d78b315 192 // Start the Smart Config Process
dflet 0:6ad60d78b315 193 StartSmartConfig();
dflet 0:6ad60d78b315 194 runSmartConfig = 0;
dflet 0:6ad60d78b315 195 }
dflet 0:6ad60d78b315 196
dflet 0:6ad60d78b315 197 if(!(currentCC3000State() & CC3000_ASSOC))
dflet 0:6ad60d78b315 198 {
dflet 0:6ad60d78b315 199 //Connect via profile (unsol connect) or :-
dflet 0:6ad60d78b315 200 // ManualConnect();
dflet 0:6ad60d78b315 201
dflet 0:6ad60d78b315 202 // Wait until connection is finished
dflet 0:6ad60d78b315 203 while (!(currentCC3000State() & CC3000_ASSOC))
dflet 0:6ad60d78b315 204 {
dflet 0:6ad60d78b315 205
dflet 0:6ad60d78b315 206 wait_us(5);
dflet 0:6ad60d78b315 207 //__delay_cycles(100);
dflet 0:6ad60d78b315 208 // Check if user pressed button to do Smart Config
dflet 0:6ad60d78b315 209 if(runSmartConfig == 1)
dflet 0:6ad60d78b315 210 break;
dflet 0:6ad60d78b315 211 }
dflet 0:6ad60d78b315 212 }
dflet 0:6ad60d78b315 213
dflet 0:6ad60d78b315 214 // Print out connection status
dflet 0:6ad60d78b315 215 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == FALSE)
dflet 0:6ad60d78b315 216 {
dflet 0:6ad60d78b315 217 // Set flag so we don't constantly obtain the IP info
dflet 0:6ad60d78b315 218 obtainIpInfoFlag = TRUE;
dflet 0:6ad60d78b315 219 turnLedOn(CC3000_IP_ALLOC_IND);
dflet 0:6ad60d78b315 220 }
dflet 0:6ad60d78b315 221
dflet 0:6ad60d78b315 222 if(currentCC3000State() & CC3000_IP_ALLOC && obtainIpInfoFlag == TRUE)
dflet 0:6ad60d78b315 223 {
dflet 0:6ad60d78b315 224 //If smart config was done was broadcast mdns signal
dflet 0:6ad60d78b315 225 if (ConnectUsingSmartConfig==1)
dflet 0:6ad60d78b315 226 {
dflet 0:6ad60d78b315 227 mdnsAdvertiser(1,DevServname, sizeof(DevServname));
dflet 0:6ad60d78b315 228 ConnectUsingSmartConfig = 0;
dflet 0:6ad60d78b315 229 }
dflet 0:6ad60d78b315 230 mDNSValid =1;
dflet 0:6ad60d78b315 231 skipCount =0;
dflet 0:6ad60d78b315 232 ipconfig = getCC3000Info();
dflet 0:6ad60d78b315 233 length = iptostring(ipconfig->aucIP, ipvalue);
dflet 0:6ad60d78b315 234 DispatcherUartSendPacket("\r\nIP = ", 7);
dflet 0:6ad60d78b315 235 DispatcherUartSendPacket(ipvalue, length);
dflet 0:6ad60d78b315 236 DispatcherUartSendPacket("\r\n", 2);
dflet 0:6ad60d78b315 237 //Initialize and start the HTTP server
dflet 0:6ad60d78b315 238 HttpServerInitAndRun();
dflet 0:6ad60d78b315 239 }
dflet 0:6ad60d78b315 240 }
dflet 0:6ad60d78b315 241 return 1;
dflet 0:6ad60d78b315 242 }
dflet 0:6ad60d78b315 243
dflet 0:6ad60d78b315 244 /************************************************************************************
dflet 0:6ad60d78b315 245 // This is an example of how you'd connect the CC3000 to an AP without using
dflet 0:6ad60d78b315 246 // Smart Config or a stored profile.
dflet 0:6ad60d78b315 247 //
dflet 0:6ad60d78b315 248 // All the code above wlan_connect() is just for this demo program; if you're
dflet 0:6ad60d78b315 249 // always going to connect to your network this way you wouldn't need it.
dflet 0:6ad60d78b315 250 ************************************************************************************/
dflet 0:6ad60d78b315 251
dflet 0:6ad60d78b315 252 void ManualConnect() {
dflet 0:6ad60d78b315 253
dflet 0:6ad60d78b315 254 char ssidName[] = "*********";
dflet 0:6ad60d78b315 255 //char AP_KEY[] = "********";//Refer to demo_config.h
dflet 0:6ad60d78b315 256 int8_t rval;
dflet 0:6ad60d78b315 257
dflet 0:6ad60d78b315 258 if (!isInitialized) {
dflet 0:6ad60d78b315 259 printf("CC3000 not initialized; can't run manual connect.\r\n");
dflet 0:6ad60d78b315 260 return;
dflet 0:6ad60d78b315 261 }
dflet 0:6ad60d78b315 262
dflet 0:6ad60d78b315 263 printf("Starting manual connect...\r\n");
dflet 0:6ad60d78b315 264
dflet 0:6ad60d78b315 265 printf((" Disabling auto-connect policy...\r\n"));
dflet 0:6ad60d78b315 266 rval = wlan_ioctl_set_connection_policy(0, 0, 0);
dflet 0:6ad60d78b315 267
dflet 0:6ad60d78b315 268 printf(" Deleting all existing profiles...\r\n");
dflet 0:6ad60d78b315 269 rval = wlan_ioctl_del_profile(255);
dflet 0:6ad60d78b315 270
dflet 0:6ad60d78b315 271 printf(" Waiting until disconnected...\r\n");
dflet 0:6ad60d78b315 272 while (ulCC3000Connected == 1) {
dflet 0:6ad60d78b315 273 wait_ms(100);
dflet 0:6ad60d78b315 274 }
dflet 0:6ad60d78b315 275
dflet 0:6ad60d78b315 276 printf(" Manually connecting...\r\n");
dflet 0:6ad60d78b315 277
dflet 0:6ad60d78b315 278 // Parameter 1 is the security type: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
dflet 0:6ad60d78b315 279 // WLAN_SEC_WPA or WLAN_SEC_WPA2
dflet 0:6ad60d78b315 280 // Parameter 3 is the MAC adddress of the AP. All the TI examples
dflet 0:6ad60d78b315 281 // use NULL. I suppose you would want to specify this
dflet 0:6ad60d78b315 282 // if you were security paranoid.
dflet 0:6ad60d78b315 283 rval = wlan_connect(WLAN_SEC_WPA2,
dflet 0:6ad60d78b315 284 ssidName,
dflet 0:6ad60d78b315 285 strlen(ssidName),
dflet 0:6ad60d78b315 286 NULL,
dflet 0:6ad60d78b315 287 (unsigned char *)AP_KEY,
dflet 0:6ad60d78b315 288 strlen(AP_KEY));
dflet 0:6ad60d78b315 289
dflet 0:6ad60d78b315 290 if (rval==0) {
dflet 0:6ad60d78b315 291 printf(" Manual connect success.\r\n");
dflet 0:6ad60d78b315 292 }
dflet 0:6ad60d78b315 293 else {
dflet 0:6ad60d78b315 294 printf(" Unusual return value: ");
dflet 0:6ad60d78b315 295 printf("%d\r\n",rval);
dflet 0:6ad60d78b315 296 }
dflet 0:6ad60d78b315 297 return;
dflet 0:6ad60d78b315 298 }