//***************************************************************************** // //! \brief Starts Smart Configuration //! //! \param none //! //! \return void //! \note //! \warning //***************************************************************************** void SmartConfigTask(void* pValue) { long lRetVal = -1; DispatcherUartSendPacket((char*)pucUARTSmartConfigString, sizeof(pucUARTSmartConfigString)); //Turn off the Network Status LED GPIO_IF_LedOff(MCU_IP_ALLOC_IND); LedTimerConfigNStart(); //Reset the Network Status before Entering Smart Config Network_IF_UnsetMCUMachineState(STATUS_BIT_CONNECTION); Network_IF_UnsetMCUMachineState(STATUS_BIT_IP_AQUIRED); lRetVal = SmartConfigConnect(); if(lRetVal < 0) { ERR_PRINT(lRetVal); LOOP_FOREVER(); } // // Wait until IP is acquired // while (!(IS_CONNECTED(Network_IF_CurrentMCUState())) || !(IS_IP_ACQUIRED(Network_IF_CurrentMCUState()))); LedTimerDeinitStop(); // Red LED on GPIO_IF_LedOn(MCU_IP_ALLOC_IND); //Enable GPIO Interrupt Button_IF_EnableInterrupt(SW2); }
long ConnectToNetwork() { long lRetVal = -1; unsigned int uiConnectTimeoutCnt =0; //Start Simplelink Device lRetVal = sl_Start(NULL,NULL,NULL); ASSERT_ON_ERROR(lRetVal); if(lRetVal != ROLE_STA) { if (ROLE_AP == lRetVal) { // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } // // Configure to STA Mode // lRetVal = ConfigureMode(ROLE_STA); if(lRetVal !=ROLE_STA) { UART_PRINT("Unable to set STA mode...\n\r"); lRetVal = sl_Stop(SL_STOP_TIMEOUT); CLR_STATUS_BIT_ALL(g_ulStatus); return DEVICE_NOT_IN_STATION_MODE; } } //waiting for the device to Auto Connect while(uiConnectTimeoutCnt<AUTO_CONNECTION_TIMEOUT_COUNT && ((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))) { //Turn Green LED On GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); osi_Sleep(50); //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); osi_Sleep(50); uiConnectTimeoutCnt++; } //Couldn't connect Using Auto Profile if(uiConnectTimeoutCnt==AUTO_CONNECTION_TIMEOUT_COUNT) { CLR_STATUS_BIT_ALL(g_ulStatus); //Turn Green LED On GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); //Connect Using Smart Config lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); //Waiting for the device to Auto Connect while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { MAP_UtilsDelay(500); } //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); } return SUCCESS; }
int main(void) { long lRetVal = -1; // // Initialize Board configurations // BoardInit(); // // Configure the pinmux settings for the peripherals exercised // PinMuxConfig(); #ifndef NOTERM InitTerm(); #endif // configure RED LED GPIO_IF_LedConfigure(LED1); GPIO_IF_LedOff(MCU_RED_LED_GPIO); InitializeAppVariables(); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // lRetVal = ConfigureSimpleLinkToDefaultState(); if(lRetVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == lRetVal) UART_PRINT("Failed to configure the device in its " "default state \n\r"); LOOP_FOREVER(); } UART_PRINT("Device is configured in default state \n\r"); CLR_STATUS_BIT_ALL(g_ulStatus); //Start simplelink lRetVal = sl_Start(0,0,0); if (lRetVal < 0 || ROLE_STA != lRetVal) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(); } UART_PRINT("Device started as STATION \n\r"); /* Connect to our AP using SmartConfig method */ lRetVal = SmartConfigConnect(); if(lRetVal < 0) { ERR_PRINT(lRetVal); } LOOP_FOREVER(); }
//***************************************************************************** // //! UARTCommandHandler //! //! @brief The function handles commands arrived from CLI //! //! @param usBuffer is the receive buffer from the UART interface to PC //! //! @return 0 on success or error code on failure //! // //***************************************************************************** long UARTCommandHandler(char *usBuffer) { int iIndex = 0; int iParamcount = 0; long lRetVal = -1; signed char cStatus1 = 0; signed char cStatus2 = 0; if(usBuffer == NULL) { UART_PRINT("Null pointer\r\n"); return -1; } switch(usBuffer[1]) { //********************************************** // Case 01: Connect to default AP //********************************************** case UART_COMMAND_SIMPLE_CONFIG_START: if(!IS_CONNECTED(Network_IF_CurrentMCUState())) { LedTimerConfigNStart(); // Setting Acess Point's security parameters SecurityParams.Key = (signed char *)SECURITY_KEY; SecurityParams.KeyLen = strlen(SECURITY_KEY); SecurityParams.Type = SECURITY_TYPE; lRetVal = Network_IF_ConnectAP(SSID_NAME,SecurityParams); if(lRetVal < 0) { UART_PRINT("Error: %d Connecting to an AP.", lRetVal); return lRetVal; } } // // Disable the LED blinking Timer as Device is connected to AP // LedTimerDeinitStop(); // // Switch ON RED LED to indicate that Device acquired an IP // GPIO_IF_LedOn(MCU_IP_ALLOC_IND); DispatcherUartSendPacket((char*)pucUARTOKString, sizeof(pucUARTOKString)); break; //********************************************** // Case 02: Configure sender (source) email //********************************************** // TODO Phase 2: Include /* case UART_COMMAND_EMAIL_SOURCE: memset(serveruser,0,sizeof(serveruser)); memset(pcServerpass,0,sizeof(pcServerpass)); pcOfserveruser = &serveruser[0]; ofpcServerpass = &pcServerpass[0]; // '<' To maintain RFC 2821 format *pcOfserveruser++= '<'; iIndex = 2; while ((int)usBuffer[iIndex] != 0x0D) { //look for comma ',' for separation of params if((int)usBuffer[iIndex] == 44) { iParamcount++; } else { if(iParamcount==1) { //Enter smtp server username (email address) *pcOfserveruser++ = usBuffer[iIndex]; } if(iParamcount==2) { //Enter username's password *ofpcServerpass++ = usBuffer[iIndex]; } } iIndex++; } // '>' To maintain RFC 2821 format *pcOfserveruser++= '>'; *pcOfserveruser++= '\0'; *ofpcServerpass++= '\0'; //Set variables in smtp.c cStatus1 = smtpSetVariable(serveruser, USERNAME_VAR); cStatus2 = smtpSetVariable(pcServerpass, PASSWORD_VAR); //If error in return if(cStatus1 == 0 && cStatus2 == 0) { DispatcherUartSendPacket((char*)pucUARTOKString, sizeof(pucUARTOKString)); } else { DispatcherUartSendPacket((char*)putUARTErrorInputString, \ sizeof(putUARTErrorInputString)); } break; */ //********************************************** // Case 03: Configure sender (source) email //********************************************** case UART_COMMAND_EMAIL_HEADER: memset(pcEmailto,0,sizeof(pcEmailto)); pcOfemailto = &pcEmailto[0]; pcOfemailsubject = &pcEmailsubject[0]; // '<' To maintain RFC 2821 format *pcOfemailto++= '<'; iIndex = 2; while ((int)usBuffer[iIndex] != 0x0D && usBuffer[iIndex] != '\0') { //look for comma ',' for separation of params if((int)usBuffer[iIndex] == 44) { iParamcount++; } else { if(iParamcount==1) { //Enter destination email address *pcOfemailto++ = usBuffer[iIndex]; } if(iParamcount==2) { //Enter email subject *pcOfemailsubject++ = usBuffer[iIndex]; } } iIndex++; } // '>' To maintain RFC 2821 format *pcOfemailto++= '>'; *pcOfemailto++= '\0'; *pcOfemailsubject= '\0'; SlNetAppDestination_t destEmailAdd; memcpy(destEmailAdd.Email,pcEmailto,strlen(pcEmailto)+1); cStatus1 = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_DEST_EMAIL, \ strlen(pcEmailto)+1, \ (unsigned char *)&destEmailAdd); SlNetAppEmailSubject_t emailSubject; memcpy(emailSubject.Value,pcEmailsubject,strlen(pcEmailsubject)+1); cStatus2 = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_SUBJECT, \ strlen(pcEmailsubject)+1, \ (unsigned char *)&emailSubject); // Check for Error in setting the variables if(cStatus1 == 0 && cStatus2 == 0) { DispatcherUartSendPacket((char*)pucUARTOKString, \ sizeof(pucUARTOKString)); } else { DispatcherUartSendPacket((char*)putUARTErrorInputString, \ sizeof(putUARTErrorInputString)); } break; //********************************************** // Case 04: Record email message //********************************************** case UART_COMMAND_EMAIL_MESSAGE: pcOfemailmessage = &pcEmailmessage[0]; //Enter "Message" iIndex =3; while ((int)usBuffer[iIndex] != 0x0D && usBuffer[iIndex] != '\0') { if((int)usBuffer[iIndex] == 62) { iParamcount++; } else { if(iParamcount==0) { *pcOfemailmessage++ = usBuffer[iIndex]; } } iIndex++; } *pcOfemailmessage= '\0'; /* TODO here unsigned char is converting to char */ DispatcherUartSendPacket((char*)pucUARTOKString, \ sizeof(pucUARTOKString)); break; //********************************************** // Case 05: Send email message using configurations //********************************************** case UART_COMMAND_EMAIL_SEND: { // reset Orange LED state GPIO_IF_LedOff(MCU_SENDING_DATA_IND); // TODO: If no destination email given, default to hardcoded value SlNetAppEmailOpt_t eMailServerSetting; lRetVal = Network_IF_GetHostIP(GMAIL_HOST_NAME, &eMailServerSetting.Ip); if(lRetVal >= 0) { eMailServerSetting.Family = AF_INET; eMailServerSetting.Port = GMAIL_HOST_PORT; eMailServerSetting.SecurityMethod = SL_SO_SEC_METHOD_SSLV3; eMailServerSetting.SecurityCypher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_MD5; lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID, \ NETAPP_ADVANCED_OPT, \ sizeof(SlNetAppEmailOpt_t), \ (unsigned char*)&eMailServerSetting); ASSERT_ON_ERROR(lRetVal); } else { UART_PRINT("Error:%d GetHostIP.", lRetVal); return -1; } g_cConnectStatus = sl_NetAppEmailConnect(); // If return -1, throw connect error if(g_cConnectStatus == -1) { DispatcherUartSendPacket((char*)pucUARTErrorSocketCreateString, \ sizeof(pucUARTErrorSocketCreateString)); } // If return -2, throw socket option error if(g_cConnectStatus == -2) { DispatcherUartSendPacket((char*)pucUARTErrorSocketOptionString, \ sizeof(pucUARTErrorSocketOptionString)); } if(g_cConnectStatus == 0) { SlNetAppServerError_t sEmailErrorInfo; long lRetCode = SL_EMAIL_ERROR_FAILED; if((lRetCode = sl_NetAppEmailSend(pcEmailto,pcEmailsubject, \ pcEmailmessage, \ &sEmailErrorInfo)) == SL_EMAIL_ERROR_NONE) { // Blink LED7 to indicate email has been sent for(iIndex=0 ;iIndex<5 ;iIndex++) { MAP_UtilsDelay(6000000); GPIO_IF_LedOff(MCU_SENDING_DATA_IND); MAP_UtilsDelay(6000000); GPIO_IF_LedOn(MCU_SENDING_DATA_IND); } DispatcherUartSendPacket((char*)putUARTFinishString, \ sizeof(putUARTFinishString)); } else { lRetVal = EmailHandleERROR(lRetCode,(char*)sEmailErrorInfo.Value); ASSERT_ON_ERROR(lRetVal); } } } break; case UART_COMMAND_SMART_CONFIG: GPIO_IF_LedOff(MCU_IP_ALLOC_IND); DispatcherUartSendPacket((char*)pucUARTSmartConfigString, \ sizeof(pucUARTSmartConfigString)); // Start LED blinking Timer LedTimerConfigNStart(); //Reset the Network Status before Entering Smart Config Network_IF_UnsetMCUMachineState(STATUS_BIT_CONNECTION); Network_IF_UnsetMCUMachineState(STATUS_BIT_IP_AQUIRED); // start smart config process lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); while (!(IS_CONNECTED(Network_IF_CurrentMCUState())) || \ !(IS_IP_ACQUIRED(Network_IF_CurrentMCUState()))) { MAP_UtilsDelay(100); } LedTimerDeinitStop(); GPIO_IF_LedOn(MCU_IP_ALLOC_IND); break; default: DispatcherUartSendPacket((char*)pucUARTErrorString, \ sizeof(pucUARTErrorString)); break; } return SUCCESS; }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return None // //**************************************************************************** void ConnectToNetwork() { char ucAPSSID[32]; unsigned short len, config_opt; // staring simplelink g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); unsigned char macAddressVal[SL_MAC_ADDR_LEN]; unsigned char macAddressLen = SL_MAC_ADDR_LEN; sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(unsigned char *)macAddressVal); Report("CC3200 LaunchPad MAC Address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n\r", macAddressVal[0],macAddressVal[1],macAddressVal[2],macAddressVal[3],macAddressVal[4],macAddressVal[5]); // Device is in AP Mode and Force AP Jumper is not Connected if(((g_uiSimplelinkRole == ROLE_AP) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_STA ) { //Switch to STA Mode sl_WlanSetMode(ROLE_STA); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //Device is in STA Mode and Force AP Jumper is Connected if(((g_uiSimplelinkRole == ROLE_STA) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode sl_WlanSetMode(ROLE_AP); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //No Mode Change Required if(g_uiSimplelinkRole == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server while (!(g_usMCNetworkUstate & MCU_IP_ALLOC)) { } char iCount=0; //Read the AP SSID memset(ucAPSSID,'\0',AP_SSID_LEN_MAX); len = AP_SSID_LEN_MAX; config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) ucAPSSID); //Blink LED 3 times to Indicate AP Mode for(iCount=0;iCount<3;iCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } } else { //waiting for the device to Auto Connect while (((!(g_usMCNetworkUstate & MCU_AP_ASSOC)) || !(g_usMCNetworkUstate & MCU_IP_ALLOC))&& g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); g_ucConnectTimeout++; } //Couldn't connect Using Auto Profile if(g_ucConnectTimeout == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); g_ucConnectTimeout &= ~MCU_AP_ASSOC; g_ucConnectTimeout &= ~MCU_IP_ALLOC; //Connect Using Smart Config SmartConfigConnect(); //Waiting for the device to Auto Connect while (!(g_usMCNetworkUstate & MCU_AP_ASSOC) || !(g_usMCNetworkUstate & MCU_IP_ALLOC)) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); if (g_iInternetAccess == 0) { Report("Successful connection to the Internet\r\n"); osi_SyncObjSignal(&semaphore_Connected); //signal "Connected" semaphore so mqtt task can continue } else { Report("Could not obtain connection to the Internet\r\n"); } } }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return 0 - Success //! -1 - Failure // //**************************************************************************** long ConnectToNetwork() { long lRetVal = -1; unsigned int uiConnectTimeoutCnt =0; // staring simplelink lRetVal = sl_Start(NULL,NULL,NULL); ASSERT_ON_ERROR( lRetVal); // Device is in AP Mode and Force AP Jumper is not Connected if(ROLE_STA != lRetVal && g_uiDeviceModeConfig == ROLE_STA ) { if (ROLE_AP == lRetVal) { // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //Switch to STA Mode lRetVal = ConfigureMode(ROLE_STA); ASSERT_ON_ERROR( lRetVal); } //Device is in STA Mode and Force AP Jumper is Connected if(ROLE_AP != lRetVal && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode lRetVal = ConfigureMode(ROLE_AP); ASSERT_ON_ERROR( lRetVal); } //No Mode Change Required if(lRetVal == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); char cCount=0; //Blink LED 3 times to Indicate AP Mode for(cCount=0;cCount<3;cCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } char ssid[32]; unsigned short len = 32; unsigned short config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char* )ssid); UART_PRINT("\n\r Connect to : \'%s\'\n\r\n\r",ssid); } else { //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //waiting for the device to Auto Connect while(uiConnectTimeoutCnt<AUTO_CONNECTION_TIMEOUT_COUNT && ((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); uiConnectTimeoutCnt++; } //Couldn't connect Using Auto Profile if(uiConnectTimeoutCnt == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); CLR_STATUS_BIT_ALL(g_ulStatus); //Connect Using Smart Config lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); //Waiting for the device to Auto Connect while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); } return SUCCESS; }
/* * Application's entry point */ int main(int argc, char** argv) { _i32 retVal = -1; retVal = initializeAppVariables(); ASSERT_ON_ERROR(retVal); /* Stop WDT and initialize the system-clock of the MCU These functions needs to be implemented in PAL */ stopWDT(); initClk(); /* Configure command line interface */ CLI_Configure(); displayBanner(); /* * Following function configures the device to default state by cleaning * the persistent settings stored in NVMEM (viz. connection profiles & * policies, power policy etc) * * Applications may choose to skip this step if the developer is sure * that the device is in its default state at start of application * * Note that all profiles and persistent settings that were done on the * device will be lost */ retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) { CLI_Write(" Failed to configure the device in its default state \n\r"); } LOOP_FOREVER(); } /* * Assumption is that the device is configured in station mode already * and it is in its default state */ /* Initializing the CC3100 device */ retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } CLI_Write(" Device is configured in default state \n\r"); CLI_Write(" Starting smartconfig process \n\r"); /* Connect to our AP using SmartConfig method */ retVal = SmartConfigConnect(); if(retVal < 0) { CLI_Write(" Failed to establish connection w/ an AP \n\r"); LOOP_FOREVER(); } CLI_Write(" Connection established w/ AP and IP is acquired \n\r"); /* Delete all profiles */ retVal = sl_WlanProfileDel(WLAN_DEL_ALL_PROFILES); if(retVal < 0) LOOP_FOREVER(); return 0; }