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 * cc3000.c - CC3000 Functions 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
dflet 0:6ad60d78b315 36 #include "mbed.h"
dflet 0:6ad60d78b315 37 #include "cc3000.h"
dflet 0:6ad60d78b315 38 //#include <msp430.h>
dflet 0:6ad60d78b315 39 #include "wlan.h"
dflet 0:6ad60d78b315 40 #include "evnt_handler.h" // callback function declaration
dflet 0:6ad60d78b315 41 #include "nvmem.h"
dflet 0:6ad60d78b315 42 #include "socket.h"
dflet 0:6ad60d78b315 43 #include "Common.h"
dflet 0:6ad60d78b315 44 #include "netapp.h"
dflet 0:6ad60d78b315 45 //#include "common.h"
dflet 0:6ad60d78b315 46 #include "demo_config.h"
dflet 0:6ad60d78b315 47 #include "spi.h"
dflet 0:6ad60d78b315 48 #include "Board.h"
dflet 0:6ad60d78b315 49 #include "dispatcher.h"
dflet 0:6ad60d78b315 50 #include "spi_version.h"
dflet 0:6ad60d78b315 51 #include "application_version.h"
dflet 0:6ad60d78b315 52 #include "host_driver_version.h"
dflet 0:6ad60d78b315 53 #include "security.h"
dflet 0:6ad60d78b315 54
dflet 0:6ad60d78b315 55 #define PALTFORM_VERSION (6)
dflet 0:6ad60d78b315 56
dflet 0:6ad60d78b315 57
dflet 0:6ad60d78b315 58 // Variable to indicate whether the Smart Config Process has finished
dflet 0:6ad60d78b315 59 volatile unsigned long ulSmartConfigFinished = 0;
dflet 0:6ad60d78b315 60
dflet 0:6ad60d78b315 61 unsigned char pucIP_Addr[4];
dflet 0:6ad60d78b315 62 unsigned char pucIP_DefaultGWAddr[4];
dflet 0:6ad60d78b315 63 unsigned char pucSubnetMask[4];
dflet 0:6ad60d78b315 64 unsigned char pucDNS[4];
dflet 0:6ad60d78b315 65
dflet 0:6ad60d78b315 66 tNetappIpconfigRetArgs ipinfo;
dflet 0:6ad60d78b315 67
dflet 0:6ad60d78b315 68 // Smart Config Prefix - Texas Instruments
dflet 0:6ad60d78b315 69 char aucCC3000_prefix[3] = {'T', 'T', 'T'};
dflet 0:6ad60d78b315 70 extern char digits[];
dflet 0:6ad60d78b315 71
dflet 0:6ad60d78b315 72 const unsigned char smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
dflet 0:6ad60d78b315 73
dflet 0:6ad60d78b315 74 const unsigned char pucUARTExampleAppString[] = {'\f', '\r','E', 'x', 'a', 'm', 'p', 'l', 'e', ' ', 'A', 'p', 'p', ':', 'd', 'r', 'i', 'v', 'e', 'r', ' ', 'v', 'e', 'r', 's', 'i', 'o', 'n', ' ' };
dflet 0:6ad60d78b315 75
dflet 0:6ad60d78b315 76 char cc3000state = CC3000_UNINIT;
dflet 0:6ad60d78b315 77
dflet 0:6ad60d78b315 78 extern unsigned char ConnectUsingSmartConfig;
dflet 0:6ad60d78b315 79 extern volatile unsigned long ulCC3000Connected;
dflet 0:6ad60d78b315 80 char CheckSocket = 0;
dflet 0:6ad60d78b315 81 signed char sd[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
dflet 0:6ad60d78b315 82
dflet 0:6ad60d78b315 83 //*****************************************************************************
dflet 0:6ad60d78b315 84 //
dflet 0:6ad60d78b315 85 //! ConnectUsingSSID
dflet 0:6ad60d78b315 86 //!
dflet 0:6ad60d78b315 87 //! \param ssidName is a string of the AP's SSID
dflet 0:6ad60d78b315 88 //!
dflet 0:6ad60d78b315 89 //! \return none
dflet 0:6ad60d78b315 90 //!
dflet 0:6ad60d78b315 91 //! \brief Connect to an Access Point using the specified SSID
dflet 0:6ad60d78b315 92 //
dflet 0:6ad60d78b315 93 //*****************************************************************************
dflet 0:6ad60d78b315 94 int ConnectUsingSSID(char * ssidName)
dflet 0:6ad60d78b315 95 {
dflet 0:6ad60d78b315 96
dflet 0:6ad60d78b315 97 unsetCC3000MachineState(CC3000_ASSOC);
dflet 0:6ad60d78b315 98
dflet 0:6ad60d78b315 99 // Disable Profiles and Fast Connect
dflet 0:6ad60d78b315 100 wlan_ioctl_set_connection_policy(0, 0, 0);
dflet 0:6ad60d78b315 101
dflet 0:6ad60d78b315 102 wlan_disconnect();
dflet 0:6ad60d78b315 103 wait_us(500);
dflet 0:6ad60d78b315 104 //__delay_cycles(10000);
dflet 0:6ad60d78b315 105
dflet 0:6ad60d78b315 106 // This triggers the CC3000 to connect to specific AP with certain parameters
dflet 0:6ad60d78b315 107 //sends a request to connect (does not necessarily connect - callback checks that for me)
dflet 0:6ad60d78b315 108 #ifndef CC3000_TINY_DRIVER
dflet 0:6ad60d78b315 109 wlan_connect(0, ssidName, strlen(ssidName), NULL, NULL, 0);
dflet 0:6ad60d78b315 110 #else
dflet 0:6ad60d78b315 111 wlan_connect(ssidName, strlen(ssidName));
dflet 0:6ad60d78b315 112 #endif
dflet 0:6ad60d78b315 113 // We don't wait for connection. This is handled somewhere else (in the main
dflet 0:6ad60d78b315 114 // loop for example).
dflet 0:6ad60d78b315 115
dflet 0:6ad60d78b315 116 return 0;
dflet 0:6ad60d78b315 117 }
dflet 0:6ad60d78b315 118
dflet 0:6ad60d78b315 119
dflet 0:6ad60d78b315 120 //*****************************************************************************
dflet 0:6ad60d78b315 121 //
dflet 0:6ad60d78b315 122 //! itoa
dflet 0:6ad60d78b315 123 //!
dflet 0:6ad60d78b315 124 //! @param[in] integer number to convert
dflet 0:6ad60d78b315 125 //!
dflet 0:6ad60d78b315 126 //! @param[in/out] output string
dflet 0:6ad60d78b315 127 //!
dflet 0:6ad60d78b315 128 //! @return number of ASCII parameters
dflet 0:6ad60d78b315 129 //!
dflet 0:6ad60d78b315 130 //! @brief Convert integer to ASCII in decimal base
dflet 0:6ad60d78b315 131 //
dflet 0:6ad60d78b315 132 //*****************************************************************************
dflet 0:6ad60d78b315 133 unsigned short itoa(char cNum, char *cString)
dflet 0:6ad60d78b315 134 {
dflet 0:6ad60d78b315 135 char* ptr;
dflet 0:6ad60d78b315 136 char uTemp = cNum;
dflet 0:6ad60d78b315 137 unsigned short length;
dflet 0:6ad60d78b315 138
dflet 0:6ad60d78b315 139 // value 0 is a special case
dflet 0:6ad60d78b315 140 if (cNum == 0)
dflet 0:6ad60d78b315 141 {
dflet 0:6ad60d78b315 142 length = 1;
dflet 0:6ad60d78b315 143 *cString = '0';
dflet 0:6ad60d78b315 144
dflet 0:6ad60d78b315 145 return length;
dflet 0:6ad60d78b315 146 }
dflet 0:6ad60d78b315 147
dflet 0:6ad60d78b315 148 // Find out the length of the number, in decimal base
dflet 0:6ad60d78b315 149 length = 0;
dflet 0:6ad60d78b315 150 while (uTemp > 0)
dflet 0:6ad60d78b315 151 {
dflet 0:6ad60d78b315 152 uTemp /= 10;
dflet 0:6ad60d78b315 153 length++;
dflet 0:6ad60d78b315 154 }
dflet 0:6ad60d78b315 155
dflet 0:6ad60d78b315 156 // Do the actual formatting, right to left
dflet 0:6ad60d78b315 157 uTemp = cNum;
dflet 0:6ad60d78b315 158 ptr = cString + length;
dflet 0:6ad60d78b315 159 while (uTemp > 0)
dflet 0:6ad60d78b315 160 {
dflet 0:6ad60d78b315 161 --ptr;
dflet 0:6ad60d78b315 162 *ptr = digits[uTemp % 10];
dflet 0:6ad60d78b315 163 uTemp /= 10;
dflet 0:6ad60d78b315 164 }
dflet 0:6ad60d78b315 165
dflet 0:6ad60d78b315 166 return length;
dflet 0:6ad60d78b315 167 }
dflet 0:6ad60d78b315 168
dflet 0:6ad60d78b315 169
dflet 0:6ad60d78b315 170 //*****************************************************************************
dflet 0:6ad60d78b315 171 //
dflet 0:6ad60d78b315 172 //! sendDriverPatch
dflet 0:6ad60d78b315 173 //!
dflet 0:6ad60d78b315 174 //! \param pointer to the length
dflet 0:6ad60d78b315 175 //!
dflet 0:6ad60d78b315 176 //! \return none
dflet 0:6ad60d78b315 177 //!
dflet 0:6ad60d78b315 178 //! \brief The function returns a pointer to the driver patch: since there is no patch yet -
dflet 0:6ad60d78b315 179 //! it returns 0
dflet 0:6ad60d78b315 180 //
dflet 0:6ad60d78b315 181 //*****************************************************************************
dflet 0:6ad60d78b315 182 char *sendDriverPatch(unsigned long *Length)
dflet 0:6ad60d78b315 183 {
dflet 0:6ad60d78b315 184 *Length = 0;
dflet 0:6ad60d78b315 185 return NULL;
dflet 0:6ad60d78b315 186 }
dflet 0:6ad60d78b315 187
dflet 0:6ad60d78b315 188
dflet 0:6ad60d78b315 189 //*****************************************************************************
dflet 0:6ad60d78b315 190 //
dflet 0:6ad60d78b315 191 //! sendBootLoaderPatch
dflet 0:6ad60d78b315 192 //!
dflet 0:6ad60d78b315 193 //! \param pointer to the length
dflet 0:6ad60d78b315 194 //!
dflet 0:6ad60d78b315 195 //! \return none
dflet 0:6ad60d78b315 196 //!
dflet 0:6ad60d78b315 197 //! \brief The function returns a pointer to the boot loader patch: since there is no patch yet -
dflet 0:6ad60d78b315 198 //! it returns 0
dflet 0:6ad60d78b315 199 //
dflet 0:6ad60d78b315 200 //*****************************************************************************
dflet 0:6ad60d78b315 201 char *sendBootLoaderPatch(unsigned long *Length)
dflet 0:6ad60d78b315 202 {
dflet 0:6ad60d78b315 203 *Length = 0;
dflet 0:6ad60d78b315 204 return NULL;
dflet 0:6ad60d78b315 205 }
dflet 0:6ad60d78b315 206
dflet 0:6ad60d78b315 207 //*****************************************************************************
dflet 0:6ad60d78b315 208 //
dflet 0:6ad60d78b315 209 //! sendWLFWPatch
dflet 0:6ad60d78b315 210 //!
dflet 0:6ad60d78b315 211 //! \param pointer to the length
dflet 0:6ad60d78b315 212 //!
dflet 0:6ad60d78b315 213 //! \return none
dflet 0:6ad60d78b315 214 //!
dflet 0:6ad60d78b315 215 //! \brief The function returns a pointer to the FW patch: since there is no patch yet - it returns 0
dflet 0:6ad60d78b315 216 //
dflet 0:6ad60d78b315 217 //*****************************************************************************
dflet 0:6ad60d78b315 218
dflet 0:6ad60d78b315 219 char *sendWLFWPatch(unsigned long *Length)
dflet 0:6ad60d78b315 220 {
dflet 0:6ad60d78b315 221 *Length = 0;
dflet 0:6ad60d78b315 222 return NULL;
dflet 0:6ad60d78b315 223 }
dflet 0:6ad60d78b315 224
dflet 0:6ad60d78b315 225
dflet 0:6ad60d78b315 226 //*****************************************************************************
dflet 0:6ad60d78b315 227 //
dflet 0:6ad60d78b315 228 //! CC3000_UsynchCallback
dflet 0:6ad60d78b315 229 //!
dflet 0:6ad60d78b315 230 //! \param Event type
dflet 0:6ad60d78b315 231 //!
dflet 0:6ad60d78b315 232 //! \return none
dflet 0:6ad60d78b315 233 //!
dflet 0:6ad60d78b315 234 //! \brief The function handles asynchronous events that come from CC3000 device
dflet 0:6ad60d78b315 235 //! and operates a LED4 to have an on-board indication
dflet 0:6ad60d78b315 236 //
dflet 0:6ad60d78b315 237 //*****************************************************************************
dflet 0:6ad60d78b315 238
dflet 0:6ad60d78b315 239 void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length)
dflet 0:6ad60d78b315 240 {
dflet 0:6ad60d78b315 241 if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
dflet 0:6ad60d78b315 242 {
dflet 0:6ad60d78b315 243 ulSmartConfigFinished = 1;
dflet 0:6ad60d78b315 244 }
dflet 0:6ad60d78b315 245
dflet 0:6ad60d78b315 246 if (lEventType == HCI_EVNT_WLAN_UNSOL_INIT)
dflet 0:6ad60d78b315 247 {
dflet 0:6ad60d78b315 248 setCC3000MachineState(CC3000_INIT);
dflet 0:6ad60d78b315 249 }
dflet 0:6ad60d78b315 250 if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT)
dflet 0:6ad60d78b315 251 {
dflet 0:6ad60d78b315 252 ulCC3000Connected = 1;
dflet 0:6ad60d78b315 253 setCC3000MachineState(CC3000_ASSOC);
dflet 0:6ad60d78b315 254
dflet 0:6ad60d78b315 255 }
dflet 0:6ad60d78b315 256
dflet 0:6ad60d78b315 257 if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
dflet 0:6ad60d78b315 258 {
dflet 0:6ad60d78b315 259 ulCC3000Connected = 0;
dflet 0:6ad60d78b315 260 //restartMSP430();
dflet 0:6ad60d78b315 261 unsetCC3000MachineState(CC3000_ASSOC);
dflet 0:6ad60d78b315 262
dflet 0:6ad60d78b315 263 }
dflet 0:6ad60d78b315 264 if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP)
dflet 0:6ad60d78b315 265 {
dflet 0:6ad60d78b315 266 setCC3000MachineState(CC3000_IP_ALLOC);
dflet 0:6ad60d78b315 267 }
dflet 0:6ad60d78b315 268
dflet 0:6ad60d78b315 269 // This Event is gengerated when the TCP connection is Half closed
dflet 0:6ad60d78b315 270 if (lEventType == HCI_EVNT_BSD_TCP_CLOSE_WAIT)
dflet 0:6ad60d78b315 271 {
dflet 0:6ad60d78b315 272 sd[data[0]] = 1;
dflet 0:6ad60d78b315 273 CheckSocket = 1;
dflet 0:6ad60d78b315 274 }
dflet 0:6ad60d78b315 275
dflet 0:6ad60d78b315 276 //if (lEventType == HCI_EVENT_CC3000_CAN_SHUT_DOWN){
dflet 0:6ad60d78b315 277 //OkToDoShutDown = 1;
dflet 0:6ad60d78b315 278 // printf("CC3000 Usync event: OK to shut down\r\n");
dflet 0:6ad60d78b315 279 // }
dflet 0:6ad60d78b315 280 }
dflet 0:6ad60d78b315 281
dflet 0:6ad60d78b315 282 //*****************************************************************************
dflet 0:6ad60d78b315 283 //
dflet 0:6ad60d78b315 284 //! initDriver
dflet 0:6ad60d78b315 285 //!
dflet 0:6ad60d78b315 286 //! \param None
dflet 0:6ad60d78b315 287 //!
dflet 0:6ad60d78b315 288 //! \return none
dflet 0:6ad60d78b315 289 //!
dflet 0:6ad60d78b315 290 //! \brief The function initializes a CC3000 device and triggers it to start operation
dflet 0:6ad60d78b315 291 //
dflet 0:6ad60d78b315 292 //*****************************************************************************
dflet 0:6ad60d78b315 293 int
dflet 0:6ad60d78b315 294 initDriver(void)
dflet 0:6ad60d78b315 295 {
dflet 0:6ad60d78b315 296 // Init GPIO's
dflet 0:6ad60d78b315 297 //pio_init();
dflet 0:6ad60d78b315 298
dflet 0:6ad60d78b315 299 // Init SPI
dflet 0:6ad60d78b315 300 init_spi();
dflet 0:6ad60d78b315 301
dflet 0:6ad60d78b315 302 DispatcherUARTConfigure();
dflet 0:6ad60d78b315 303
dflet 0:6ad60d78b315 304 // Globally enable interrupts
dflet 0:6ad60d78b315 305 //__enable_interrupt();
dflet 0:6ad60d78b315 306
dflet 0:6ad60d78b315 307 //
dflet 0:6ad60d78b315 308 // WLAN On API Implementation
dflet 0:6ad60d78b315 309 //
dflet 0:6ad60d78b315 310 wlan_init( CC3000_UsynchCallback, sendWLFWPatch, sendDriverPatch, sendBootLoaderPatch, ReadWlanInterruptPin, WlanInterruptEnable, WlanInterruptDisable, WriteWlanPin);
dflet 0:6ad60d78b315 311
dflet 0:6ad60d78b315 312 //
dflet 0:6ad60d78b315 313 // Trigger a WLAN device
dflet 0:6ad60d78b315 314 //
dflet 0:6ad60d78b315 315 wlan_start(0);
dflet 0:6ad60d78b315 316
dflet 0:6ad60d78b315 317 //
dflet 0:6ad60d78b315 318 // Mask out all non-required events from CC3000
dflet 0:6ad60d78b315 319 //
dflet 0:6ad60d78b315 320
dflet 0:6ad60d78b315 321 wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_ASYNC_PING_REPORT|HCI_EVNT_WLAN_UNSOL_INIT);
dflet 0:6ad60d78b315 322
dflet 0:6ad60d78b315 323 // Generate event to CLI: send a version string
dflet 0:6ad60d78b315 324 char cc3000IP[50];
dflet 0:6ad60d78b315 325 char *ccPtr;
dflet 0:6ad60d78b315 326 unsigned short ccLen;
dflet 0:6ad60d78b315 327
dflet 0:6ad60d78b315 328 DispatcherUartSendPacket((const char*)pucUARTExampleAppString, sizeof(pucUARTExampleAppString));
dflet 0:6ad60d78b315 329
dflet 0:6ad60d78b315 330 ccPtr = &cc3000IP[0];
dflet 0:6ad60d78b315 331 ccLen = itoa(PALTFORM_VERSION, ccPtr);
dflet 0:6ad60d78b315 332 ccPtr += ccLen;
dflet 0:6ad60d78b315 333 *ccPtr++ = '.';
dflet 0:6ad60d78b315 334 ccLen = itoa(APPLICATION_VERSION, ccPtr);
dflet 0:6ad60d78b315 335 ccPtr += ccLen;
dflet 0:6ad60d78b315 336 *ccPtr++ = '.';
dflet 0:6ad60d78b315 337 ccLen = itoa(SPI_VERSION_NUMBER, ccPtr);
dflet 0:6ad60d78b315 338 ccPtr += ccLen;
dflet 0:6ad60d78b315 339 *ccPtr++ = '.';
dflet 0:6ad60d78b315 340 ccLen = itoa(DRIVER_VERSION_NUMBER, ccPtr);
dflet 0:6ad60d78b315 341 ccPtr += ccLen;
dflet 0:6ad60d78b315 342 *ccPtr++ = '\r';
dflet 0:6ad60d78b315 343 *ccPtr++ = '\n';
dflet 0:6ad60d78b315 344 *ccPtr++ = '\0';
dflet 0:6ad60d78b315 345 ccLen = strlen(cc3000IP);
dflet 0:6ad60d78b315 346
dflet 0:6ad60d78b315 347 DispatcherUartSendPacket((const char*)cc3000IP, strlen(cc3000IP));
dflet 0:6ad60d78b315 348
dflet 0:6ad60d78b315 349 // CC3000 has been initialized
dflet 0:6ad60d78b315 350 setCC3000MachineState(CC3000_INIT);
dflet 0:6ad60d78b315 351
dflet 0:6ad60d78b315 352 unsigned long aucDHCP, aucARP, aucKeepalive, aucInactivity;
dflet 0:6ad60d78b315 353
dflet 0:6ad60d78b315 354 aucDHCP = 14400;
dflet 0:6ad60d78b315 355 aucARP = 3600;
dflet 0:6ad60d78b315 356 aucKeepalive = 10;
dflet 0:6ad60d78b315 357 aucInactivity = 100;
dflet 0:6ad60d78b315 358
dflet 0:6ad60d78b315 359 if(netapp_timeout_values(&(aucDHCP), &(aucARP), &(aucKeepalive), &(aucInactivity)) != 0)
dflet 0:6ad60d78b315 360 {
dflet 0:6ad60d78b315 361 while(1);
dflet 0:6ad60d78b315 362 }
dflet 0:6ad60d78b315 363
dflet 0:6ad60d78b315 364 return(0);
dflet 0:6ad60d78b315 365 }
dflet 0:6ad60d78b315 366
dflet 0:6ad60d78b315 367
dflet 0:6ad60d78b315 368 //*****************************************************************************
dflet 0:6ad60d78b315 369 //
dflet 0:6ad60d78b315 370 //! \brief Return the highest state which we're in
dflet 0:6ad60d78b315 371 //!
dflet 0:6ad60d78b315 372 //! \param None
dflet 0:6ad60d78b315 373 //!
dflet 0:6ad60d78b315 374 //! \return none
dflet 0:6ad60d78b315 375 //!
dflet 0:6ad60d78b315 376 //
dflet 0:6ad60d78b315 377 //*****************************************************************************
dflet 0:6ad60d78b315 378 char highestCC3000State()
dflet 0:6ad60d78b315 379 {
dflet 0:6ad60d78b315 380 // We start at the highest state and go down, checking if the state
dflet 0:6ad60d78b315 381 // is set.
dflet 0:6ad60d78b315 382 char mask = 0x80;
dflet 0:6ad60d78b315 383 while(!(cc3000state & mask))
dflet 0:6ad60d78b315 384 {
dflet 0:6ad60d78b315 385 mask = mask >> 1;
dflet 0:6ad60d78b315 386 }
dflet 0:6ad60d78b315 387
dflet 0:6ad60d78b315 388 return mask;
dflet 0:6ad60d78b315 389 }
dflet 0:6ad60d78b315 390
dflet 0:6ad60d78b315 391 //*****************************************************************************
dflet 0:6ad60d78b315 392 //
dflet 0:6ad60d78b315 393 //! \brief Return the current state bits
dflet 0:6ad60d78b315 394 //!
dflet 0:6ad60d78b315 395 //! \param None
dflet 0:6ad60d78b315 396 //!
dflet 0:6ad60d78b315 397 //! \return none
dflet 0:6ad60d78b315 398 //!
dflet 0:6ad60d78b315 399 //
dflet 0:6ad60d78b315 400 //*****************************************************************************
dflet 0:6ad60d78b315 401 char currentCC3000State()
dflet 0:6ad60d78b315 402 {
dflet 0:6ad60d78b315 403 return cc3000state;
dflet 0:6ad60d78b315 404 }
dflet 0:6ad60d78b315 405
dflet 0:6ad60d78b315 406 void setCC3000MachineState(char stat)
dflet 0:6ad60d78b315 407 {
dflet 0:6ad60d78b315 408 char bitmask = stat;
dflet 0:6ad60d78b315 409 cc3000state |= bitmask;
dflet 0:6ad60d78b315 410
dflet 0:6ad60d78b315 411 int i = FIRST_STATE_LED_NUM;
dflet 0:6ad60d78b315 412
dflet 0:6ad60d78b315 413 // Find LED number which needs to be set
dflet 0:6ad60d78b315 414 while(bitmask < 0x80)
dflet 0:6ad60d78b315 415 {
dflet 0:6ad60d78b315 416 bitmask = bitmask << 1;
dflet 0:6ad60d78b315 417 i++;
dflet 0:6ad60d78b315 418 }
dflet 0:6ad60d78b315 419 turnLedOn(NUM_STATES-i+2);
dflet 0:6ad60d78b315 420
dflet 0:6ad60d78b315 421 }
dflet 0:6ad60d78b315 422
dflet 0:6ad60d78b315 423
dflet 0:6ad60d78b315 424 //*****************************************************************************
dflet 0:6ad60d78b315 425 //
dflet 0:6ad60d78b315 426 //! \brief Unsets a state from the state machine
dflet 0:6ad60d78b315 427 //! Also handles LEDs
dflet 0:6ad60d78b315 428 //!
dflet 0:6ad60d78b315 429 //! \param None
dflet 0:6ad60d78b315 430 //!
dflet 0:6ad60d78b315 431 //! \return none
dflet 0:6ad60d78b315 432 //!
dflet 0:6ad60d78b315 433 //
dflet 0:6ad60d78b315 434 //*****************************************************************************
dflet 0:6ad60d78b315 435 void unsetCC3000MachineState(char stat)
dflet 0:6ad60d78b315 436 {
dflet 0:6ad60d78b315 437 char bitmask = stat;
dflet 0:6ad60d78b315 438 cc3000state &= ~bitmask;
dflet 0:6ad60d78b315 439
dflet 0:6ad60d78b315 440 int i = FIRST_STATE_LED_NUM;
dflet 0:6ad60d78b315 441 int k = NUM_STATES; // Set to last element in state list
dflet 0:6ad60d78b315 442
dflet 0:6ad60d78b315 443 // Set all upper bits to 0 as well since state machine cannot have
dflet 0:6ad60d78b315 444 // those states.
dflet 0:6ad60d78b315 445 while(bitmask < 0x80)
dflet 0:6ad60d78b315 446 {
dflet 0:6ad60d78b315 447 cc3000state &= ~bitmask;
dflet 0:6ad60d78b315 448 bitmask = bitmask << 1;
dflet 0:6ad60d78b315 449 i++;
dflet 0:6ad60d78b315 450 }
dflet 0:6ad60d78b315 451
dflet 0:6ad60d78b315 452 // Turn off all upper state LEDs
dflet 0:6ad60d78b315 453 for(; i > FIRST_STATE_LED_NUM; i--)
dflet 0:6ad60d78b315 454 {
dflet 0:6ad60d78b315 455 turnLedOff(k);
dflet 0:6ad60d78b315 456 k--;
dflet 0:6ad60d78b315 457 }
dflet 0:6ad60d78b315 458 }
dflet 0:6ad60d78b315 459
dflet 0:6ad60d78b315 460 //*****************************************************************************
dflet 0:6ad60d78b315 461 //
dflet 0:6ad60d78b315 462 //! \brief Resets the State Machine
dflet 0:6ad60d78b315 463 //!
dflet 0:6ad60d78b315 464 //! \param None
dflet 0:6ad60d78b315 465 //!
dflet 0:6ad60d78b315 466 //! \return none
dflet 0:6ad60d78b315 467 //!
dflet 0:6ad60d78b315 468 //
dflet 0:6ad60d78b315 469 //*****************************************************************************
dflet 0:6ad60d78b315 470 void resetCC3000StateMachine()
dflet 0:6ad60d78b315 471 {
dflet 0:6ad60d78b315 472 cc3000state = CC3000_UNINIT;
dflet 0:6ad60d78b315 473
dflet 0:6ad60d78b315 474 // Turn off all Board LEDs
dflet 0:6ad60d78b315 475
dflet 0:6ad60d78b315 476 turnLedOff(CC3000_ON_IND);
dflet 0:6ad60d78b315 477 turnLedOff(CC3000_ASSOCIATED_IND);
dflet 0:6ad60d78b315 478 turnLedOff(CC3000_IP_ALLOC_IND);
dflet 0:6ad60d78b315 479 turnLedOff(CC3000_SERVER_INIT_IND);
dflet 0:6ad60d78b315 480 turnLedOff(CC3000_CLIENT_CONNECTED_IND);
dflet 0:6ad60d78b315 481 turnLedOff(CC3000_SENDING_DATA_IND);
dflet 0:6ad60d78b315 482 turnLedOff(CC3000_UNUSED1_IND);
dflet 0:6ad60d78b315 483 turnLedOff(CC3000_FTC_IND);
dflet 0:6ad60d78b315 484 }
dflet 0:6ad60d78b315 485
dflet 0:6ad60d78b315 486 //*****************************************************************************
dflet 0:6ad60d78b315 487 //
dflet 0:6ad60d78b315 488 //! \brief Obtains the CC3000 Connection Information from the CC3000
dflet 0:6ad60d78b315 489 //!
dflet 0:6ad60d78b315 490 //! \param None
dflet 0:6ad60d78b315 491 //!
dflet 0:6ad60d78b315 492 //! \return none
dflet 0:6ad60d78b315 493 //!
dflet 0:6ad60d78b315 494 //
dflet 0:6ad60d78b315 495 //*****************************************************************************
dflet 0:6ad60d78b315 496 #ifndef CC3000_TINY_DRIVER
dflet 0:6ad60d78b315 497 tNetappIpconfigRetArgs * getCC3000Info()
dflet 0:6ad60d78b315 498 {
dflet 0:6ad60d78b315 499 netapp_ipconfig(&ipinfo);
dflet 0:6ad60d78b315 500 return (&ipinfo);
dflet 0:6ad60d78b315 501 }
dflet 0:6ad60d78b315 502 #endif
dflet 0:6ad60d78b315 503
dflet 0:6ad60d78b315 504 //*****************************************************************************
dflet 0:6ad60d78b315 505 //
dflet 0:6ad60d78b315 506 //! StartSmartConfig
dflet 0:6ad60d78b315 507 //!
dflet 0:6ad60d78b315 508 //! \param None
dflet 0:6ad60d78b315 509 //!
dflet 0:6ad60d78b315 510 //! \return none
dflet 0:6ad60d78b315 511 //!
dflet 0:6ad60d78b315 512 //! \brief The function triggers a smart configuration process on CC3000.
dflet 0:6ad60d78b315 513 //! it exists upon completion of the process
dflet 0:6ad60d78b315 514 //
dflet 0:6ad60d78b315 515 //*****************************************************************************
dflet 0:6ad60d78b315 516
dflet 0:6ad60d78b315 517 void StartSmartConfig(void)
dflet 0:6ad60d78b315 518 {
dflet 0:6ad60d78b315 519
dflet 0:6ad60d78b315 520 // Reset all the previous configuration
dflet 0:6ad60d78b315 521 //
dflet 0:6ad60d78b315 522 wlan_ioctl_set_connection_policy(0, 0, 0);
dflet 0:6ad60d78b315 523 wlan_ioctl_del_profile(255);
dflet 0:6ad60d78b315 524
dflet 0:6ad60d78b315 525 //Wait until CC3000 is dissconected
dflet 0:6ad60d78b315 526 while (ulCC3000Connected == 1)
dflet 0:6ad60d78b315 527 {
dflet 0:6ad60d78b315 528 wait_us(5);
dflet 0:6ad60d78b315 529 //__delay_cycles(100);
dflet 0:6ad60d78b315 530 }
dflet 0:6ad60d78b315 531
dflet 0:6ad60d78b315 532 // Start blinking LED6 during Smart Configuration process
dflet 0:6ad60d78b315 533 turnLedOn(LED6);
dflet 0:6ad60d78b315 534 wlan_smart_config_set_prefix(aucCC3000_prefix);
dflet 0:6ad60d78b315 535 //wlan_first_time_config_set_prefix(aucCC3000_prefix);
dflet 0:6ad60d78b315 536 turnLedOff(ind4);
dflet 0:6ad60d78b315 537
dflet 0:6ad60d78b315 538 // Start the SmartConfig start process
dflet 0:6ad60d78b315 539 wlan_smart_config_start(1);
dflet 0:6ad60d78b315 540 turnLedOn(ind4);
dflet 0:6ad60d78b315 541
dflet 0:6ad60d78b315 542 //
dflet 0:6ad60d78b315 543 // Wait for Smart config finished
dflet 0:6ad60d78b315 544 //
dflet 0:6ad60d78b315 545 while (ulSmartConfigFinished == 0)
dflet 0:6ad60d78b315 546 {
dflet 0:6ad60d78b315 547 //__delay_cycles(6000000);
dflet 0:6ad60d78b315 548 wait_ms(250);
dflet 0:6ad60d78b315 549 turnLedOn(ind4);
dflet 0:6ad60d78b315 550
dflet 0:6ad60d78b315 551 //__delay_cycles(6000000);
dflet 0:6ad60d78b315 552 wait_ms(250);
dflet 0:6ad60d78b315 553 turnLedOff(ind4);
dflet 0:6ad60d78b315 554 }
dflet 0:6ad60d78b315 555
dflet 0:6ad60d78b315 556 turnLedOff(ind4);
dflet 0:6ad60d78b315 557
dflet 0:6ad60d78b315 558 // create new entry for AES encryption key
dflet 0:6ad60d78b315 559 nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16);
dflet 0:6ad60d78b315 560
dflet 0:6ad60d78b315 561 // write AES key to NVMEM
dflet 0:6ad60d78b315 562 aes_write_key((unsigned char *)(&smartconfigkey[0]));
dflet 0:6ad60d78b315 563
dflet 0:6ad60d78b315 564 // Decrypt configuration information and add profile
dflet 0:6ad60d78b315 565 wlan_smart_config_process();
dflet 0:6ad60d78b315 566
dflet 0:6ad60d78b315 567 //
dflet 0:6ad60d78b315 568 // Configure to connect automatically to the AP retrieved in the
dflet 0:6ad60d78b315 569 // Smart config process
dflet 0:6ad60d78b315 570 //
dflet 0:6ad60d78b315 571 wlan_ioctl_set_connection_policy(0, 0, 1);
dflet 0:6ad60d78b315 572
dflet 0:6ad60d78b315 573 //
dflet 0:6ad60d78b315 574 // reset the CC3000
dflet 0:6ad60d78b315 575 //
dflet 0:6ad60d78b315 576 wlan_stop();
dflet 0:6ad60d78b315 577 wait_ms(250);
dflet 0:6ad60d78b315 578 //__delay_cycles(600000);
dflet 0:6ad60d78b315 579 wlan_start(0);
dflet 0:6ad60d78b315 580
dflet 0:6ad60d78b315 581 ConnectUsingSmartConfig = 1;
dflet 0:6ad60d78b315 582 ulSmartConfigFinished = 0;
dflet 0:6ad60d78b315 583 //
dflet 0:6ad60d78b315 584 // Mask out all non-required events
dflet 0:6ad60d78b315 585 //
dflet 0:6ad60d78b315 586 wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT);
dflet 0:6ad60d78b315 587 }
dflet 0:6ad60d78b315 588