void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT resetCC3000StateMachine(); // Start CC3000 State Machine initDriver(); // Initialize Board and CC3000 unsolicicted_events_timer_init(); // Initialize CC3000 Unsolicited Events Timer __enable_interrupt(); // Enable interrupts for UART DefaultWifiConnection(); // Do a default connection while (1) { hci_unsolicited_event_handler(); // Handle any un-solicited event if required - the function shall be triggered few times in a second unsolicicted_events_timer_init(); if(currentCC3000State() & CC3000_IP_ALLOC) { turnLedOn(CC3000_IP_ALLOC_IND); unsolicicted_events_timer_disable(); // Attempt to start data server initServer(); if(currentCC3000State() & CC3000_SERVER_INIT) { waitForConnection(); } else//Wait for a bit, and try again. { __delay_cycles(100000); } unsolicicted_events_timer_init(); } } }
//***************************************************************************************************************************** //***************************************************************************************************************************** //***************************************************************************************************************************** void waitForConnection(void) { setup_data_packet_header(); //RESET ADC and DMA: refresh_ADC(); ADC_STATE = HALTED; //RESET DMA STATE. refresh_DMA(); DMA_STATE = HALTED; if(currentCC3000State() & CC3000_SERVER_INIT) // Check whether the server functionality is successfully initialized { while(1) // Begin waiting for client and handle the connection { hci_unsolicited_event_handler(); addrlen = sizeof(clientaddr); // accept blocks until we receive a connection while ( (clientDescriptor == -1) || (clientDescriptor == -2) ) { clientDescriptor = accept(serverSocket, (sockaddr *) &clientaddr, &addrlen); } hci_unsolicited_event_handler(); if(clientDescriptor >= 0) // Connection Accepted, Wait for data exchange { setCC3000MachineState(CC3000_CLIENT_CONNECTED); unsolicicted_events_timer_disable(); //********************** // Important Function. Manages incoming Message aswell as initiating outgoing message. incomingPacketManager(); //********************** } else if(clientDescriptor == SOCKET_INACTIVE_ERR) { clientDescriptor = -1; // Reinitialize the server shutdownServer(); initServer(); } if(bytesRecvd < 0){check_socket_connection();} //If the recieve function goes inactive, shuts down. This checks for that case hci_unsolicited_event_handler(); } } }
//***************************************************************************** // //! 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; }