main(void) { ulCC3000DHCP = 0; ulCC3000Connected = 0; ulSocket = 0; ulSmartConfigFinished=0; // Board Initialization start initDriver(); // Initialize the UART RX Buffer memset(g_ucUARTBuffer, 0xFF, UART_IF_BUFFER); uart_have_cmd =0; // Loop forever waiting for commands from PC... while(1) { if (uart_have_cmd && !(UARTBusy(UART0_BASE)) ) { while(UARTBusy(UART0_BASE)); //Process the cmd in RX buffer DemoHandleUartCommand(g_ucUARTBuffer); //Clear cmd and reset buffer pointer uart_have_cmd = 0; memset(g_ucUARTBuffer, 0xFF, UART_IF_BUFFER); } // complete smart config process: // 1. if smart config is done // 2. CC3000 established AP connection // 3. DHCP IP is configured // then send mDNS packet to stop external SmartConfig application if ((ucStopSmartConfig == 1) && (ulCC3000DHCP == 1) && (ulCC3000Connected == 1)) { unsigned char loop_index = 0; while (loop_index < 3) { mdnsAdvertiser(1,device_name,strlen(device_name)); loop_index++; } ucStopSmartConfig = 0; } if( (ulCC3000DHCP == 1) && (ulCC3000Connected == 1) && (printOnce == 1) ) { printOnce = 0; DispatcherUartSendPacket((unsigned char*)pucCC3000_Rx_Buffer, strlen((char const*)pucCC3000_Rx_Buffer)); } } }
/*------------------------------------------------------------------------ Spider_SmartConfig_AckFinish Acknowledge smart config finished message to smart config APP. return 0, Ack success. return -1, mdns functions failed, ack failed. -----------------------------------------------------------------------*/ int Spider_SmartConfig_AckFinish(char* ack_msg){ int i; for(i = 0; i < 3; i++){ digitalWrite(13, HIGH); delay(10); digitalWrite(13, LOW); //Ack mDNS package to SmartConfig phone to finished connection process. if(mdnsAdvertiser(1, (char*)ack_msg, strlen(ack_msg)) != 0){ return -1; } } return 0; }
//***************************************************************************** // //! DemoHandleUartCommand //! //! @param buffer //! //! @return none //! //! @brief The function handles commands arrived from CLI // //***************************************************************************** void DemoHandleUartCommand(unsigned char *usBuffer) { char *pcSsid, *pcData, *pcSockAddrAscii; unsigned long ulSsidLen, ulDataLength; volatile signed long iReturnValue; sockaddr tSocketAddr; socklen_t tRxPacketLength; unsigned char pucIP_Addr[4]; unsigned char pucIP_DefaultGWAddr[4]; unsigned char pucSubnetMask[4]; unsigned char pucDNS[4]; // usBuffer[0] contains always 0 // usBuffer[1] maps the command // usBuffer[2..end] optional parameters switch(usBuffer[1]) { // Start a smart configuration process case UART_COMMAND_CC3000_SIMPLE_CONFIG_START: StartSmartConfig(); break; // Start a WLAN Connect process case UART_COMMAND_CC3000_CONNECT: { ulSsidLen = atoc(usBuffer[2]); pcSsid = (char *)&usBuffer[3]; #ifndef CC3000_TINY_DRIVER wlan_connect(WLAN_SEC_UNSEC, pcSsid, ulSsidLen,NULL, NULL, 0); #else wlan_connect(pcSsid,ulSsidLen); #endif } break; // Handle open socket command case UART_COMMAND_SOCKET_OPEN: // wait for DHCP process to finish. if you are using a static IP address // please delete the wait for DHCP event - ulCC3000DHCP while ((ulCC3000DHCP == 0) || (ulCC3000Connected == 0)) { hci_unsolicited_event_handler(); SysCtlDelay(1000); } ulSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); break; // Handle close socket command case UART_COMMAND_SOCKET_CLOSE: closesocket(ulSocket); ulSocket = 0xFFFFFFFF; break; // Handle receive data command case UART_COMMAND_RCV_DATA: iReturnValue = recvfrom(ulSocket, pucCC3000_Rx_Buffer, CC3000_APP_BUFFER_SIZE, 0, &tSocketAddr, &tRxPacketLength); if (iReturnValue <= 0) { // No data received by device DispatcherUartSendPacket((unsigned char*)pucUARTNoDataString, sizeof(pucUARTNoDataString)); } else { // Send data to UART... DispatcherUartSendPacket(pucCC3000_Rx_Buffer, CC3000_APP_BUFFER_SIZE); } break; // Handle send data command case UART_COMMAND_SEND_DATA: // data pointer pcData = (char *)&usBuffer[4]; // data length to send ulDataLength = atoshort(usBuffer[2], usBuffer[3]); pcSockAddrAscii = (pcData + ulDataLength); // the family is always AF_INET tSocketAddr.sa_family = atoshort(pcSockAddrAscii[0], pcSockAddrAscii[1]); // the destination port tSocketAddr.sa_data[0] = ascii_to_char(pcSockAddrAscii[2], pcSockAddrAscii[3]); tSocketAddr.sa_data[1] = ascii_to_char(pcSockAddrAscii[4], pcSockAddrAscii[5]); // the destination IP address tSocketAddr.sa_data[2] = ascii_to_char(pcSockAddrAscii[6], pcSockAddrAscii[7]); tSocketAddr.sa_data[3] = ascii_to_char(pcSockAddrAscii[8], pcSockAddrAscii[9]); tSocketAddr.sa_data[4] = ascii_to_char(pcSockAddrAscii[10], pcSockAddrAscii[11]); tSocketAddr.sa_data[5] = ascii_to_char(pcSockAddrAscii[12], pcSockAddrAscii[13]); sendto(ulSocket, pcData, ulDataLength, 0, &tSocketAddr, sizeof(sockaddr)); break; // Handle bind command case UART_COMMAND_BSD_BIND: tSocketAddr.sa_family = AF_INET; // the source port tSocketAddr.sa_data[0] = ascii_to_char(usBuffer[2], usBuffer[3]); tSocketAddr.sa_data[1] = ascii_to_char(usBuffer[4], usBuffer[5]); // all 0 IP address memset (&tSocketAddr.sa_data[2], 0, 4); bind(ulSocket, &tSocketAddr, sizeof(sockaddr)); break; // Handle IP configuration command case UART_COMMAND_IP_CONFIG: // Network mask is assumed to be 255.255.255.0 pucSubnetMask[0] = 0xFF; pucSubnetMask[1] = 0xFF; pucSubnetMask[2] = 0xFF; pucSubnetMask[3] = 0x0; pucIP_Addr[0] = ascii_to_char(usBuffer[2], usBuffer[3]); pucIP_Addr[1] = ascii_to_char(usBuffer[4], usBuffer[5]); pucIP_Addr[2] = ascii_to_char(usBuffer[6], usBuffer[7]); pucIP_Addr[3] = ascii_to_char(usBuffer[8], usBuffer[9]); pucIP_DefaultGWAddr[0] = ascii_to_char(usBuffer[10], usBuffer[11]); pucIP_DefaultGWAddr[1] = ascii_to_char(usBuffer[12], usBuffer[13]); pucIP_DefaultGWAddr[2] = ascii_to_char(usBuffer[14], usBuffer[15]); pucIP_DefaultGWAddr[3] = ascii_to_char(usBuffer[16], usBuffer[17]); pucDNS[0] = 0; pucDNS[1] = 0; pucDNS[2] = 0; pucDNS[3] = 0; netapp_dhcp((unsigned long *)pucIP_Addr, (unsigned long *)pucSubnetMask, (unsigned long *)pucIP_DefaultGWAddr, (unsigned long *)pucDNS); break; // Handle WLAN disconnect command case UART_COMMAND_CC3000_DISCONNECT: wlan_disconnect(); break; // Handle erase policy command case UART_COMMAND_CC3000_DEL_POLICY: wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); break; // Handle send DNS Discovery command case UART_COMMAND_SEND_DNS_ADVERTIZE: if(ulCC3000DHCP) { mdnsAdvertiser(1,device_name,strlen(device_name)); } break; default: DispatcherUartSendPacket((unsigned char*)pucUARTIllegalCommandString, sizeof(pucUARTIllegalCommandString)); break; } // Send a response - the command handling has finished DispatcherUartSendPacket((unsigned char *)(pucUARTCommandDoneString), sizeof(pucUARTCommandDoneString)); }
static void cmd_smart_cc3000(BaseSequentialStream *chp, int argc, char *argv[]) { long rval; (void)argv; if(argc > 0) { chprintf(chp, "Usage: setup\r\n"); return; } chprintf(chp, "Disconnecting "); wlan_disconnect(); while(cc3000AsyncData.connected == 1) { chprintf(chp, "."); } chprintf(chp, "done!\r\n"); chprintf(chp, "Disabling auto connect policy ...\r\n"); /* Set connection policy to disable */ if ((rval = wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE)) != 0) { chprintf(chp, "Error disabling auto connect policy ...\r\n"); return; } /* Delete all profiles */ chprintf(chp, "Deleting all profiles ...\r\n"); if ((rval = wlan_ioctl_del_profile(255)) != 0) { chprintf(chp, "Error deleting profiles ...\r\n"); return; } chprintf(chp, "Creating AES keys ...\r\n"); nvmem_create_entry(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE); aes_write_key((UINT8 *) AES_KEY); // Set the smart config prefix chprintf(chp, "Setting smart config prefix ...\r\n"); if((rval = wlan_smart_config_set_prefix(smartConfigPrefix)) != 0) { chprintf(chp, "Error setting smart config prefix ... \r\n"); return; } chprintf(chp, "Starting CC3000 SmartConfig ...\r\n"); if((rval = wlan_smart_config_start(0)) != 0) { chprintf(chp, "Error starting smart config ...\r\n"); return; } chprintf(chp, "Waiting for SmartConfig to finish ...\r\n"); /* Wait until smart config is finished */ while(cc3000AsyncData.smartConfigFinished == 0) { // We blink the led here .. the thread process // will set this to PAL_LOW (since we are disconnected) palWritePad(CON_LED_PORT, CON_LED_PIN, PAL_HIGH); chThdSleep(MS2ST(200)); } chprintf(chp, "Smart config packet received ...\r\n"); /* Re enable auto connect policy */ if ((rval = wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE)) != 0) { chprintf(chp, "Error setting auto connect policy ...\r\n"); return; } /* Restart wlan */ wlan_stop(); chprintf(chp, "Reconnecting ...\r\n"); chThdSleep(MS2ST(2000)); wlan_start(0); /* No need to call connect, hopefully auto connect policy * can connect to our AP now **/ chprintf(chp, "Waiting for connection to AP ...\r\n"); while (cc3000AsyncData.dhcp.present != 1) { chThdSleep(MS2ST(5)); } show_cc3_dhcp_info(chp); mdnsAdvertiser(1, deviceName, strlen(deviceName)); }
void SPARK_WLAN_Loop(void) { static int cofd_count = 0; if(SPARK_WLAN_RESET || SPARK_WLAN_SLEEP) { if(SPARK_WLAN_STARTED) { if (LED_RGB_OVERRIDE) { LED_Signaling_Stop(); } WLAN_CONNECTED = 0; WLAN_DHCP = 0; SPARK_WLAN_RESET = 0; SPARK_WLAN_STARTED = 0; SPARK_SOCKET_CONNECTED = 0; SPARK_HANDSHAKE_COMPLETED = 0; SPARK_FLASH_UPDATE = 0; SPARK_LED_FADE = 0; Spark_Error_Count = 0; cofd_count = 0; CC3000_Write_Enable_Pin(WLAN_DISABLE); //wlan_stop(); Delay(100); if(WLAN_SMART_CONFIG_START) { //Workaround to enter smart config when socket connect had blocked wlan_start(0); SPARK_WLAN_STARTED = 1; /* Start CC3000 Smart Config Process */ Start_Smart_Config(); } LED_SetRGBColor(RGB_COLOR_GREEN); LED_On(LED_RGB); } } else { if(!SPARK_WLAN_STARTED) { wlan_start(0); SPARK_WLAN_STARTED = 1; } } if(WLAN_SMART_CONFIG_START) { /* Start CC3000 Smart Config Process */ Start_Smart_Config(); } else if (WLAN_MANUAL_CONNECT > 0 && !WLAN_DHCP) { wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); /* Edit the below line before use*/ wlan_connect(WLAN_SEC_WPA2, _ssid, strlen(_ssid), NULL, (unsigned char*)_password, strlen(_password)); WLAN_MANUAL_CONNECT = -1; } // Complete Smart Config Process: // 1. if smart config is done // 2. CC3000 established AP connection // 3. DHCP IP is configured // then send mDNS packet to stop external SmartConfig application if ((WLAN_SMART_CONFIG_STOP == 1) && (WLAN_DHCP == 1) && (WLAN_CONNECTED == 1)) { unsigned char loop_index = 0; while (loop_index < 3) { mdnsAdvertiser(1,device_name,strlen(device_name)); loop_index++; } WLAN_SMART_CONFIG_STOP = 0; } if(SPARK_SOCKET_HANDSHAKE == 0) { if(SPARK_SOCKET_CONNECTED || SPARK_HANDSHAKE_COMPLETED) { Spark_Disconnect(); SPARK_FLASH_UPDATE = 0; SPARK_LED_FADE = 0; SPARK_HANDSHAKE_COMPLETED = 0; SPARK_SOCKET_CONNECTED = 0; LED_SetRGBColor(RGB_COLOR_GREEN); LED_On(LED_RGB); } return; } if(TimingSparkConnectDelay != 0) { return; } if(WLAN_DHCP && !SPARK_WLAN_SLEEP && !SPARK_SOCKET_CONNECTED) { Delay(100); netapp_ipconfig(&ip_config); if(Spark_Error_Count) { LED_SetRGBColor(RGB_COLOR_RED); while(Spark_Error_Count != 0) { LED_On(LED_RGB); Delay(500); LED_Off(LED_RGB); Delay(500); Spark_Error_Count--; } //Send the Error Count to Cloud: NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] //To Do //Reset Error Count NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] = 0; nvmem_write(NVMEM_SPARK_FILE_ID, 1, ERROR_COUNT_FILE_OFFSET, &NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET]); } LED_SetRGBColor(RGB_COLOR_CYAN); LED_On(LED_RGB); if(Spark_Connect() >= 0) { cofd_count = 0; SPARK_SOCKET_CONNECTED = 1; TimingCloudHandshakeTimeout = 0; } else { if(SPARK_WLAN_RESET) return; if ((cofd_count += RESET_ON_CFOD) == MAX_FAILED_CONNECTS) { SPARK_WLAN_RESET = RESET_ON_CFOD; ERROR("Resetting CC3000 due to %d failed connect attempts", MAX_FAILED_CONNECTS); } if(Internet_Test() < 0) { //No Internet Connection if ((cofd_count += RESET_ON_CFOD) == MAX_FAILED_CONNECTS) { SPARK_WLAN_RESET = RESET_ON_CFOD; ERROR("Resetting CC3000 due to %d failed connect attempts", MAX_FAILED_CONNECTS); } Spark_Error_Count = 2; } else { //Cloud not Reachable Spark_Error_Count = 3; } NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] = Spark_Error_Count; nvmem_write(NVMEM_SPARK_FILE_ID, 1, ERROR_COUNT_FILE_OFFSET, &NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET]); SPARK_SOCKET_CONNECTED = 0; } } if (SPARK_SOCKET_CONNECTED) { if (!SPARK_HANDSHAKE_COMPLETED) { int err = Spark_Handshake(); if (err) { if (0 > err) { // Wrong key error, red LED_SetRGBColor(0xff0000); } else if (1 == err) { // RSA decryption error, orange LED_SetRGBColor(0xff6000); } else if (2 == err) { // RSA signature verification error, magenta LED_SetRGBColor(0xff00ff); } LED_On(LED_RGB); Cloud_Handshake_Error_Count++; TimingSparkConnectDelay = Cloud_Handshake_Error_Count * TIMING_CLOUD_HANDSHAKE_TIMEOUT; } else { SPARK_HANDSHAKE_COMPLETED = 1; Cloud_Handshake_Error_Count = 0; TimingCloudActivityTimeout = 0; } } if (!Spark_Communication_Loop()) { if (LED_RGB_OVERRIDE) { LED_Signaling_Stop(); } SPARK_FLASH_UPDATE = 0; SPARK_LED_FADE = 0; SPARK_HANDSHAKE_COMPLETED = 0; SPARK_SOCKET_CONNECTED = 0; } } }
BOOL cc3000_setup(BOOL patchReq, uint8_t connectPolicy, const char* deviceName) { printf("BEGIN CC3000 Setup (patchReq: %d, connectPolicy: %d, deviceName: %s)\n", patchReq, connectPolicy, deviceName); _cc3000_pingReportNum = 0; _cc3000_irqEnabled = FALSE; memset(&_cc3000_status, 0, sizeof(_cc3000_status)); memset(&_cc3000_pingReport, 0, sizeof(_cc3000_pingReport)); for (int i = 0; i < MAX_SOCKETS; i++) { _cc3000_sockets[i].closed = FALSE; } _cc3000_inIrq = FALSE; wlan_init( _cc3000_asyncCallback, _cc3000_sendFWPatches, _cc3000_sendDriverPatches, _cc3000_sendBootLoaderPatches, _cc3000_readWlanInterruptPin, _cc3000_wlanInterruptEnable, _cc3000_wlanInterruptDisable, _cc3000_writeWlanPin ); wlan_start(patchReq); uint8_t firmwareMajor, firmwareMinor; if (_cc3000_getFirmwareVersion(&firmwareMajor, &firmwareMinor)) { printf("CC3000 firmware: %d.%d\n", firmwareMajor, firmwareMinor); } else { printf("failed to get firmware\n"); return FALSE; } // Check if we should erase previous stored connection details // (most likely written with data from the SmartConfig app) int connectToOpenAPs = (connectPolicy & CC3000_CONNECT_POLICY_OPEN_AP) ? 1 : 0; int useFastConnect = (connectPolicy & CC3000_CONNECT_POLICY_FAST) ? 1 : 0; int useProfiles = (connectPolicy & CC3000_CONNECT_POLICY_PROFILES) ? 1 : 0; wlan_ioctl_set_connection_policy(connectToOpenAPs, useFastConnect, useProfiles); if (connectToOpenAPs == 0 && useFastConnect == 0 && useProfiles == 0) { // Delete previous profiles from memory wlan_ioctl_del_profile(255); } if (wlan_set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | //HCI_EVNT_WLAN_ASYNC_PING_REPORT |// we want ping reports //HCI_EVNT_BSD_TCP_CLOSE_WAIT | //HCI_EVNT_WLAN_TX_COMPLETE | HCI_EVNT_WLAN_KEEPALIVE) != CC3000_SUCCESS) { printf("WLAN Set Event Mask FAIL\n"); return FALSE; } // Wait for re-connection if we're using SmartConfig data if (connectPolicy & CC3000_CONNECT_POLICY_SMART_CONFIG) { // Wait for a connection uint32_t timeout = 0; while (!_cc3000_status.isConnected) { _cc3000_irqPoll(); if (timeout > WLAN_CONNECT_TIMEOUT) { return FALSE; } timeout += 10; delay_ms(10); } delay_ms(1000); if (_cc3000_status.hasDHCP) { mdnsAdvertiser(1, (char*) deviceName, strlen(deviceName)); } } printf("END CC3000 Setup\n"); return TRUE; }
/** * @brief Begins SmartConfig. The user needs to run the SmartConfig phone app. * * @param[in] timeout optional time (ms) to wait before stopping. 0 = no timeout * @return True if connected to wireless network. False otherwise. */ bool SFE_CC3000::startSmartConfig(unsigned int timeout) { char cc3000_prefix[] = {'T', 'T', 'T'}; unsigned long time; /* Reset all global connection variables */ ulSmartConfigFinished = 0; ulCC3000Connected = 0; ulCC3000DHCP = 0; OkToDoShutDown=0; /* If CC3000 is not initialized, return false. */ if (!getInitStatus()) { return false; } /* Set connection profile to manual (no fast or auto connect) */ if (wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE) != CC3000_SUCCESS) { return false; } /* Delete old connection profiles */ if (wlan_ioctl_del_profile(255) != CC3000_SUCCESS) { return false; } /* Wait until CC3000 is disconnected */ while (getConnectionStatus()) { delay(1); } /* Sets the prefix for SmartConfig. Should always be "TTT" */ if (wlan_smart_config_set_prefix((char*)cc3000_prefix) != CC3000_SUCCESS) { return false; } /* Start the SmartConfig process */ if (wlan_smart_config_start(0) != CC3000_SUCCESS) { return false; } /* Wait for SmartConfig to complete */ time = millis(); while (ulSmartConfigFinished == 0) { if (timeout != 0) { if ( (millis() - time) > timeout ) { return false; } } } /* Configure to connect automatically to AP from SmartConfig process */ if (wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE) != CC3000_SUCCESS) { return false; } /* Reset CC3000 */ wlan_stop(); delay(400); wlan_start(0); /* Wait for connection and DHCP-assigned IP address */ while (getDHCPStatus() == false) { if (timeout != 0) { if ( (millis() - time) > timeout ) { return false; } } } /* If we make it this far, we need to tell the SmartConfig app to stop */ mdnsAdvertiser(1, DEVICE_NAME, strlen(DEVICE_NAME)); /* Get connection information */ netapp_ipconfig(&connection_info_); return true; }
main(void) { ulCC3000DHCP = 0; ulCC3000Connected = 0; ulSocket = 0; ulSmartConfigFinished=0; WDTCTL = WDTPW + WDTHOLD; // Board Initialization start initDriver(); // Initialize the UART RX Buffer memset(g_ucUARTBuffer, 0xFF, UART_IF_BUFFER); uart_have_cmd =0; // Loop forever waiting for commands from PC... while (1) { __bis_SR_register(LPM2_bits + GIE); __no_operation(); if (uart_have_cmd) { wakeup_timer_disable(); //Process the cmd in RX buffer DemoHandleUartCommand(g_ucUARTBuffer); uart_have_cmd = 0; memset(g_ucUARTBuffer, 0xFF, UART_IF_BUFFER); wakeup_timer_init(); } // complete smart config process: // 1. if smart config is done // 2. CC3000 established AP connection // 3. DHCP IP is configured // then send mDNS packet to stop external SmartConfig application if ((ucStopSmartConfig == 1) && (ulCC3000DHCP == 1) && (ulCC3000Connected == 1)) { unsigned char loop_index = 0; while (loop_index < 3) { mdnsAdvertiser(1,device_name,strlen(device_name)); loop_index++; } ucStopSmartConfig = 0; } if( (ulCC3000DHCP == 1) && (ulCC3000Connected == 1) && (printOnce == 1) ) { printOnce = 0; DispatcherUartSendPacket((unsigned char*)pucCC3000_Rx_Buffer, strlen((char const*)pucCC3000_Rx_Buffer)); } } }
//***************************************************************************** // //! checkWiFiConnected //! //! \param None //! //! \return TRUE if connected, FALSE if not //! //! \brief Checks to see that WiFi is still connected. If not associated //! with an AP for 5 consecutive retries, it will reset the board. // //***************************************************************************** unsigned char checkWiFiConnected(void) { unsigned char ipInfoFlagSet = 0; if(!(currentCC3000State() & CC3000_ASSOC)) //try to associate with an Access Point { // // Check whether Smart Config was run previously. If it was, we // use it to connect to an access point. Otherwise, we connect to the // default. // if((isFTCSet() == 0)&&(ConnectUsingSmartConfig==0)&&(*SmartConfigProfilestored != SMART_CONFIG_SET)) { // Smart Config not set, check whether we have an SSID // from the assoc terminal command. If not, use fixed SSID. //sendString("== ConnectUsingSSID==\r\n"); ConnectUsingSSID(SSID); } unsolicicted_events_timer_init(); // Wait until connection is finished //sendString("== Wait until connection is finished==\r\n"); while (!(currentCC3000State() & CC3000_ASSOC)) { WDTCTL = WDT_ARST_1000; __delay_cycles(100); // Handle any un-solicited event if required - the function will get triggered // few times in a second hci_unsolicited_event_handler(); // Check if user pressed button to do Smart Config if(runSmartConfig == 1) break; } } // Handle un-solicited events - will be triggered few times per second hci_unsolicited_event_handler(); WDTCTL = WDTPW + WDTHOLD; // Check if we are in a connected state. If so, set flags and LED if(currentCC3000State() & CC3000_IP_ALLOC) { unsolicicted_events_timer_disable(); // Turn our timer off since isr-driven routines set LEDs too... if (obtainIpInfoFlag == FALSE) { //sendString("== CC3000_IP_ALLOC_IND==\r\n"); obtainIpInfoFlag = TRUE; // Set flag so we don't constantly turn the LED on turnLedOn(CC3000_IP_ALLOC_IND); ipInfoFlagSet = 1; unsolicicted_events_timer_init(); } if (obtainIpInfoFlag == TRUE) { WDTCTL = WDT_ARST_1000; //If Smart Config was performed, we need to send complete notification to the configure (Smart Phone App) if (ConnectUsingSmartConfig==1) { mdnsAdvertiser(1,DevServname, sizeof(DevServname)); ConnectUsingSmartConfig = 0; *SmartConfigProfilestored = SMART_CONFIG_SET; } //Start mDNS timer in order to send mDNS Advertisement every 30 seconds mDNS_packet_trigger_timer_enable(); unsolicicted_events_timer_init(); } WDTCTL = WDTPW + WDTHOLD; if( ipInfoFlagSet == 1) { // Initialize an Exosite connection //sendString("== Exosite Activate ==\r\n"); cloud_status = Exosite_Activate(); ipInfoFlagSet = 0; } return TRUE; } return FALSE; }
void SPARK_WLAN_Loop(void) { if(SPARK_WLAN_RESET || SPARK_WLAN_SLEEP) { if(SPARK_WLAN_STARTED) { if (LED_RGB_OVERRIDE) { LED_Signaling_Stop(); } WLAN_CONNECTED = 0; WLAN_DHCP = 0; SPARK_WLAN_RESET = 0; SPARK_WLAN_STARTED = 0; SPARK_SOCKET_CONNECTED = 0; SPARK_HANDSHAKE_COMPLETED = 0; SPARK_FLASH_UPDATE = 0; SPARK_LED_FADE = 0; Spark_Error_Count = 0; TimingSparkCommTimeout = 0; CC3000_Write_Enable_Pin(WLAN_DISABLE); Delay(100); if(WLAN_SMART_CONFIG_START) { //Workaround to enter smart config when socket connect had blocked wlan_start(0); SPARK_WLAN_STARTED = 1; /* Start CC3000 Smart Config Process */ Start_Smart_Config(); } LED_SetRGBColor(RGB_COLOR_GREEN); LED_On(LED_RGB); } } else { if(!SPARK_WLAN_STARTED) { wlan_start(0); SPARK_WLAN_STARTED = 1; } } if(WLAN_SMART_CONFIG_START) { /* Start CC3000 Smart Config Process */ Start_Smart_Config(); } else if (WLAN_MANUAL_CONNECT && !WLAN_DHCP) { wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); /* Edit the below line before use*/ wlan_connect(WLAN_SEC_WPA2, _ssid, strlen(_ssid), NULL, (unsigned char*)_password, strlen(_password)); WLAN_MANUAL_CONNECT = 0; } // Complete Smart Config Process: // 1. if smart config is done // 2. CC3000 established AP connection // 3. DHCP IP is configured // then send mDNS packet to stop external SmartConfig application if ((WLAN_SMART_CONFIG_STOP == 1) && (WLAN_DHCP == 1) && (WLAN_CONNECTED == 1)) { unsigned char loop_index = 0; while (loop_index < 3) { mdnsAdvertiser(1,device_name,strlen(device_name)); loop_index++; } WLAN_SMART_CONFIG_STOP = 0; } if(WLAN_DHCP && !SPARK_WLAN_SLEEP && !SPARK_SOCKET_CONNECTED) { Delay(100); netapp_ipconfig(&ip_config); #if defined (USE_SPARK_CORE_V02) if(Spark_Error_Count) { LED_SetRGBColor(RGB_COLOR_RED); while(Spark_Error_Count != 0) { LED_On(LED_RGB); Delay(500); LED_Off(LED_RGB); Delay(500); Spark_Error_Count--; } //Send the Error Count to Cloud: NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] //To Do //Reset Error Count NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] = 0; nvmem_write(NVMEM_SPARK_FILE_ID, 1, ERROR_COUNT_FILE_OFFSET, &NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET]); } LED_SetRGBColor(RGB_COLOR_CYAN); LED_On(LED_RGB); #endif if(Spark_Connect() < 0) { if(SPARK_WLAN_RESET) return; if(Internet_Test() < 0) { //No Internet Connection Spark_Error_Count = 2; } else { //Cloud not Reachable Spark_Error_Count = 3; } NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET] = Spark_Error_Count; nvmem_write(NVMEM_SPARK_FILE_ID, 1, ERROR_COUNT_FILE_OFFSET, &NVMEM_Spark_File_Data[ERROR_COUNT_FILE_OFFSET]); SPARK_SOCKET_CONNECTED = 0; } else { SPARK_SOCKET_CONNECTED = 1; } } }