David Fletcher / Mbed 2 deprecated CC3000Test

Dependencies:   CC3000HostDriver mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3000.cpp Source File

cc3000.cpp

00001 /*****************************************************************************
00002 *
00003 *  cc3000.c - CC3000 Functions 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 
00036 #include "mbed.h"
00037 #include "cc3000.h"
00038 //#include <msp430.h>
00039 #include "wlan.h" 
00040 #include "evnt_handler.h"    // callback function declaration
00041 #include "nvmem.h"
00042 #include "socket.h"
00043 //#include "common.h"
00044 #include "netapp.h"
00045 //#include "common.h"
00046 
00047 //#include "demo_config.h"
00048 #include "spi.h"
00049 //#include "board.h"
00050 //#include "dispatcher.h"
00051 #include "spi_version.h"
00052 //#include "application_version.h"
00053 //#include "host_driver_version.h"
00054 #include "security.h"
00055 #include "CC3000TestApp.h"
00056 #include "CC3000Core.h"
00057 #include "DigitalClass.h"
00058 
00059 
00060 char CheckSocket = 0;
00061 // Smart Config Prefix - Texas Instruments
00062 char aucCC3000_prefix[3] = {'T', 'T', 'T'};
00063 
00064 signed char sd[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
00065 
00066 const unsigned char smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
00067 
00068 //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', ' ' };
00069 unsigned char pucIP_Addr[4];
00070 unsigned char pucIP_DefaultGWAddr[4];
00071 unsigned char pucSubnetMask[4];
00072 unsigned char pucDNS[4];
00073 char digits[100];
00074 char cc3000state = CC3000_UNINIT;
00075 
00076 tNetappIpconfigRetArgs ipinfo;
00077 
00078 // Variable to indicate whether the Smart Config Process has finished
00079 volatile unsigned long ulSmartConfigFinished = 0;
00080 
00081 //*****************************************************************************
00082 //
00083 //! ConnectUsingSSID
00084 //!
00085 //! \param  ssidName is a string of the AP's SSID
00086 //!
00087 //! \return none
00088 //!
00089 //! \brief  Connect to an Access Point using the specified SSID
00090 //
00091 //*****************************************************************************
00092 int ConnectUsingSSID(char * ssidName)
00093 {
00094                     
00095     unsetCC3000MachineState(CC3000_ASSOC);
00096     
00097     // Disable Profiles and Fast Connect
00098     wlan_ioctl_set_connection_policy(0, 0, 0);
00099     
00100     wlan_disconnect();
00101     
00102     //__delay_cycles(10000);
00103     wait_us(500);
00104     
00105     // This triggers the CC3000 to connect to specific AP with certain parameters
00106     //sends a request to connect (does not necessarily connect - callback checks that for me)
00107 #ifndef CC3000_TINY_DRIVER
00108     wlan_connect(0, ssidName, strlen(ssidName), NULL, NULL, 0);   
00109 #else
00110     wlan_connect(ssidName, strlen(ssidName));
00111 #endif
00112     // We don't wait for connection. This is handled somewhere else (in the main
00113     // loop for example).
00114     
00115     return 0;      
00116 }
00117 
00118 
00119 //*****************************************************************************
00120 //
00121 //! itoa
00122 //!
00123 //! @param[in]  integer number to convert
00124 //!
00125 //! @param[in/out]  output string
00126 //!
00127 //! @return number of ASCII parameters
00128 //!
00129 //! @brief  Convert integer to ASCII in decimal base
00130 //
00131 //*****************************************************************************
00132 unsigned short itoa(char cNum, char *cString)
00133 {
00134     char* ptr;
00135     char uTemp = cNum;
00136     unsigned short length;
00137     
00138     
00139     // value 0 is a special case
00140     if (cNum == 0)
00141     {
00142         length = 1;
00143         *cString = '0';
00144         
00145         return length;
00146     }
00147     
00148     // Find out the length of the number, in decimal base
00149     length = 0;
00150     while (uTemp > 0)
00151     {
00152         uTemp /= 10;
00153         length++;
00154     }
00155     
00156     // Do the actual formatting, right to left
00157     uTemp = cNum;
00158     ptr = cString + length;
00159     while (uTemp > 0)
00160     {
00161         --ptr;
00162         *ptr = digits[uTemp % 10];
00163         uTemp /= 10;
00164     }
00165     
00166     return length;
00167 }
00168 
00169 
00170 //*****************************************************************************
00171 //
00172 //! sendDriverPatch
00173 //!
00174 //! \param  pointer to the length
00175 //!
00176 //! \return none
00177 //!
00178 //! \brief  The function returns a pointer to the driver patch: since there is no patch yet - 
00179 //!                it returns 0
00180 //
00181 //*****************************************************************************
00182 char *sendDriverPatch(unsigned long *Length)
00183 {
00184     *Length = 0;
00185     return NULL;
00186 }
00187 
00188 
00189 //*****************************************************************************
00190 //
00191 //! sendBootLoaderPatch
00192 //!
00193 //! \param  pointer to the length
00194 //!
00195 //! \return none
00196 //!
00197 //! \brief  The function returns a pointer to the boot loader patch: since there is no patch yet -
00198 //!                it returns 0
00199 //
00200 //*****************************************************************************
00201 char *sendBootLoaderPatch(unsigned long *Length)
00202 {
00203     *Length = 0;
00204     return NULL;
00205 }
00206 
00207 //*****************************************************************************
00208 //
00209 //! sendWLFWPatch
00210 //!
00211 //! \param  pointer to the length
00212 //!
00213 //! \return none
00214 //!
00215 //! \brief  The function returns a pointer to the FW patch: since there is no patch yet - it returns 0
00216 //
00217 //*****************************************************************************
00218 
00219 char *sendWLFWPatch(unsigned long *Length)
00220 {
00221     *Length = 0;
00222     return NULL;
00223 }
00224 
00225 
00226 //*****************************************************************************
00227 //
00228 //! CC3000_UsynchCallback
00229 //!
00230 //! \param  Event type
00231 //!
00232 //! \return none
00233 //!
00234 //! \brief  The function handles asynchronous events that come from CC3000 device 
00235 //!          and operates a LED4 to have an on-board indication
00236 //
00237 //*****************************************************************************
00238 
00239 void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length)
00240 {
00241     if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
00242     {
00243         ulSmartConfigFinished = 1;      
00244     }
00245     
00246     if (lEventType == HCI_EVNT_WLAN_UNSOL_INIT)
00247     {
00248         setCC3000MachineState(CC3000_INIT);
00249     }
00250     if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT)
00251     {
00252               ulCC3000Connected = 1;
00253         setCC3000MachineState(CC3000_ASSOC);
00254         
00255     }
00256     
00257     if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
00258     {
00259         ulCC3000Connected = 0;
00260         //restartMSP430();
00261         unsetCC3000MachineState(CC3000_ASSOC);
00262         
00263     }
00264     if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP)
00265     {
00266         setCC3000MachineState(CC3000_IP_ALLOC);        
00267     }
00268     
00269     // This Event is gengerated when the TCP connection is Half closed
00270     if (lEventType == HCI_EVNT_BSD_TCP_CLOSE_WAIT)
00271     {
00272       sd[data[0]] = 1;
00273       CheckSocket = 1;
00274     }
00275 }
00276 
00277 //*****************************************************************************
00278 //
00279 //! initDriver
00280 //!
00281 //!  \param  None
00282 //!
00283 //!  \return none
00284 //!
00285 //!  \brief  The function initializes a CC3000 device and triggers it to start operation
00286 //
00287 //*****************************************************************************
00288 int
00289   initDriver(void)
00290 {
00291         // Init GPIO's
00292     //pio_init();
00293     DigitalClass pio(p9, p10);
00294     // Init SPI
00295     init_spi();
00296     
00297     //DispatcherUARTConfigure();
00298     
00299     // Globally enable interrupts
00300     //__enable_interrupt();
00301     //  __enable_irq();  
00302     //
00303     // WLAN On API Implementation
00304     //
00305     wlan_init( CC3000_AsynchCallback, sendWLFWPatch, sendDriverPatch, sendBootLoaderPatch, ReadWlanInterruptPin, WlanInterruptEnable, WlanInterruptDisable, WriteWlanPin);
00306     
00307     //
00308     // Trigger a WLAN device
00309     //
00310     wlan_start(0);
00311     
00312     //
00313     // Mask out all non-required events from CC3000
00314     //
00315 
00316     wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_ASYNC_PING_REPORT|HCI_EVNT_WLAN_UNSOL_INIT);
00317 
00318     // Generate event to CLI: send a version string
00319     char cc3000IP[50];
00320     char *ccPtr;
00321     unsigned short ccLen;
00322         
00323     //DispatcherUartSendPacket((const char*)pucUARTExampleAppString, sizeof(pucUARTExampleAppString));
00324         
00325     ccPtr = &cc3000IP[0];
00326     ccLen = itoa(PALTFORM_VERSION, ccPtr);
00327     ccPtr += ccLen;
00328     *ccPtr++ = '.';
00329     //ccLen = itoa(APPLICATION_VERSION, ccPtr);
00330     ccPtr += ccLen;
00331     *ccPtr++ = '.';
00332     ccLen = itoa(SPI_VERSION_NUMBER, ccPtr);
00333     ccPtr += ccLen;
00334     *ccPtr++ = '.';
00335     //ccLen = itoa(DRIVER_VERSION_NUMBER, ccPtr);
00336     ccPtr += ccLen;
00337     *ccPtr++ = '\r';
00338         *ccPtr++ = '\n';
00339     *ccPtr++ = '\0';
00340         ccLen = strlen(cc3000IP);
00341         
00342     //DispatcherUartSendPacket((const char*)cc3000IP, strlen(cc3000IP));
00343     
00344     // CC3000 has been initialized
00345     setCC3000MachineState(CC3000_INIT);
00346     
00347     unsigned long aucDHCP, aucARP, aucKeepalive, aucInactivity;
00348     
00349     aucDHCP = 14400;
00350     aucARP = 3600;      
00351     aucKeepalive = 10;
00352     aucInactivity = 50;
00353     
00354     if(netapp_timeout_values(&(aucDHCP), &(aucARP), &(aucKeepalive), &(aucInactivity)) != 0)
00355     {
00356       while(1);
00357     }
00358     
00359     return(0);
00360 }
00361 
00362 
00363 //*****************************************************************************
00364 //
00365 //!  \brief  Return the highest state which we're in
00366 //!
00367 //!  \param  None
00368 //!
00369 //!  \return none
00370 //!
00371 //
00372 //*****************************************************************************
00373 char highestCC3000State()
00374 {
00375     // We start at the highest state and go down, checking if the state
00376     // is set.
00377     char mask = 0x80;
00378     while(!(cc3000state & mask))
00379     {
00380         mask = mask >> 1;
00381     }
00382         
00383     return mask;
00384 }
00385 
00386 //*****************************************************************************
00387 //
00388 //!  \brief  Return the current state bits
00389 //!
00390 //!  \param  None
00391 //!
00392 //!  \return none
00393 //!
00394 //
00395 //*****************************************************************************
00396 char currentCC3000State()
00397 {
00398     return cc3000state;
00399 }
00400 
00401 void setCC3000MachineState(char stat)
00402 {        
00403     char bitmask = stat;
00404     cc3000state |= bitmask;
00405     
00406     int i = FIRST_STATE_LED_NUM;
00407     
00408     // Find LED number which needs to be set
00409     while(bitmask < 0x80)
00410     {      
00411         bitmask  = bitmask << 1;
00412         i++;
00413     }    
00414     turnLedOn(NUM_STATES-i+2);
00415 
00416 }
00417 
00418 
00419 //*****************************************************************************
00420 //
00421 //!  \brief  Unsets a state from the state machine
00422 //!  Also handles LEDs
00423 //!  
00424 //!  \param  None
00425 //!
00426 //!  \return none
00427 //!  
00428 //
00429 //*****************************************************************************
00430 void unsetCC3000MachineState(char stat)
00431 {
00432     char bitmask = stat;
00433     cc3000state &= ~bitmask;
00434     
00435     int i = FIRST_STATE_LED_NUM;
00436     int k = NUM_STATES; // Set to last element in state list
00437     
00438     // Set all upper bits to 0 as well since state machine cannot have
00439     // those states.
00440     while(bitmask < 0x80)
00441     {
00442         cc3000state &= ~bitmask;
00443         bitmask = bitmask << 1;
00444         i++;
00445     }
00446     
00447     // Turn off all upper state LEDs
00448     for(; i > FIRST_STATE_LED_NUM; i--)
00449     {
00450         turnLedOff(k);
00451         k--;
00452     }    
00453 }
00454 
00455 //*****************************************************************************
00456 //
00457 //!  \brief  Resets the State Machine
00458 //!  
00459 //!  \param  None
00460 //!
00461 //!  \return none
00462 //!  
00463 //
00464 //*****************************************************************************
00465 void resetCC3000StateMachine()
00466 {
00467     cc3000state = CC3000_UNINIT;
00468 
00469     // Turn off all Board LEDs
00470 
00471     turnLedOff(CC3000_ON_IND);
00472     turnLedOff(CC3000_ASSOCIATED_IND);
00473     turnLedOff(CC3000_IP_ALLOC_IND);
00474     turnLedOff(CC3000_SERVER_INIT_IND);
00475     turnLedOff(CC3000_CLIENT_CONNECTED_IND);
00476     turnLedOff(CC3000_SENDING_DATA_IND);
00477     turnLedOff(CC3000_UNUSED1_IND);
00478     turnLedOff(CC3000_FTC_IND);
00479 }
00480 
00481 //*****************************************************************************
00482 //
00483 //!  \brief  Obtains the CC3000 Connection Information from the CC3000
00484 //!  
00485 //!  \param  None
00486 //!
00487 //!  \return none
00488 //!  
00489 //
00490 //*****************************************************************************
00491 #ifndef CC3000_TINY_DRIVER
00492 tNetappIpconfigRetArgs * getCC3000Info()
00493 {
00494     netapp_ipconfig(&ipinfo);
00495     return (&ipinfo);
00496 }
00497 #endif
00498 
00499 //*****************************************************************************
00500 //
00501 //! StartSmartConfig
00502 //!
00503 //!  \param  None
00504 //!
00505 //!  \return none
00506 //!
00507 //!  \brief  The function triggers a smart configuration process on CC3000.
00508 //!            it exists upon completion of the process
00509 //
00510 //*****************************************************************************
00511 
00512 void StartSmartConfig(void)
00513 {
00514 
00515     // Reset all the previous configuration
00516     //
00517     wlan_ioctl_set_connection_policy(0, 0, 0);    
00518         wlan_ioctl_del_profile(255);
00519     
00520     //Wait until CC3000 is dissconected
00521     while (ulCC3000Connected == 1)
00522     {
00523       //__delay_cycles(100);
00524       wait_us(5);
00525     }
00526 
00527     // Start blinking LED6 during Smart Configuration process
00528     turnLedOn(6);
00529     wlan_smart_config_set_prefix(aucCC3000_prefix);
00530     //wlan_first_time_config_set_prefix(aucCC3000_prefix);
00531     turnLedOff(6);
00532     
00533     // Start the SmartConfig start process
00534     wlan_smart_config_start(1);
00535     turnLedOn(6);
00536     
00537     //
00538     // Wait for Smart config finished
00539     // 
00540     while (ulSmartConfigFinished == 0)
00541     {
00542         //__delay_cycles(6000000);
00543         wait_ms(250);                                                                     
00544         turnLedOn(6);
00545 
00546         //__delay_cycles(6000000);
00547         wait_ms(250);        
00548         turnLedOff(6);
00549     }
00550 
00551     turnLedOff(6);
00552 
00553     // create new entry for AES encryption key
00554     nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16);
00555     
00556     // write AES key to NVMEM
00557     aes_write_key((unsigned char *)(&smartconfigkey[0]));
00558     
00559     // Decrypt configuration information and add profile
00560     wlan_smart_config_process();
00561         
00562     //
00563     // Configure to connect automatically to the AP retrieved in the 
00564     // Smart config process
00565     //
00566     wlan_ioctl_set_connection_policy(0, 0, 1);
00567     
00568     //
00569     // reset the CC3000
00570     // 
00571     wlan_stop();
00572     //__delay_cycles(600000);
00573     wait_ms(25);
00574     wlan_start(0);
00575 
00576     unsigned char ConnectUsingSmartConfig  = 1; 
00577         ulSmartConfigFinished = 0;
00578     //
00579     // Mask out all non-required events
00580     //
00581     wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT);
00582 }
00583