static void cmd_setup_cc3000(BaseSequentialStream *chp, int argc, char *argv[]) { char ssid_name[32]; char ssid_key[40]; char ssid_sectype[3]; unsigned long sectype; unsigned char rval; (void)argv; if(argc > 0) { chprintf(chp, "Usage: smartcfg\r\n"); return; } chprintf(chp, "Enter Security profile: \r\n"); chprintf(chp, " [0] Unsecured\r\n"); chprintf(chp, " [1] WEP\r\n"); chprintf(chp, " [2] WPA\r\n"); chprintf(chp, " [3] WPA2\r\n"); shellGetLine(chp, ssid_sectype, 3); sectype = ssid_sectype[0] - '0'; chprintf(chp, "Enter the AP SSID\r\n"); shellGetLine(chp, ssid_name, 32); chprintf(chp, "Enter SSID Key\r\n"); shellGetLine(chp, ssid_key, 40); chprintf(chp, "Type[%d]\r\n", sectype); chprintf(chp, "SSID[%s]\r\n", ssid_name); chprintf(chp, "Passkey[%s]\r\n", ssid_key); chprintf(chp, "Disabling connection policy ...\r\n"); wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); chprintf(chp, "Adding profile ...\r\n"); rval = wlan_add_profile(sectype, (unsigned char *) ssid_name, strlen(ssid_name), NULL, 0, 0x18, 0x1e, 0x2, (unsigned char *) ssid_key, strlen(ssid_key) ); if (rval != 255) { chprintf(chp, "Add profile returned: %d\r\n", rval); chprintf(chp, "Enabling auto connect ...\r\n"); wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE); chprintf(chp, "Restarting wlan ...\r\n"); wlan_stop(); chThdSleep(MS2ST(500)); wlan_start(0); return; }else { // TODO: Delete profiles and add again chprintf(chp, "Error adding profile (list full) ...\r\n"); return; } chprintf(chp, "Lan profile added!\r\n"); }
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)); }
/* mgmt mutex MUST be locked by caller */ static clarityError clarityMgmtAttemptActivate_mtxext(void) { clarityError rtn = CLARITY_SUCCESS; #if 0 uint32_t ip = 0; uint32_t subnet = 0; uint32_t gateway = 0; uint32_t dns = 0; #endif if (mgmtData.active == true) { return CLARITY_SUCCESS; } #if 0 if (mgmtData.ap->deviceIp.isStatic == true) { ip = htonl(mgmtData.ap->deviceIp.ip); subnet = htonl(mgmtData.ap->deviceIp.subnet); gateway = htonl(mgmtData.ap->deviceIp.gateway); dns = htonl(mgmtData.ap->deviceIp.dns); } #endif clarityCC3000ApiLock(); wlan_start(0); mgmtData.active = true; #if 0 if (netapp_dhcp(&ip, &subnet, &gateway, &dns) != 0) { CLAR_PRINT_ERROR(); rtn = CLARITY_ERROR_CC3000_NETAPP; } wlan_disconnect(); wlan_stop(); chThdSleep(MS2ST(200)); wlan_start(0); #endif clarityCC3000ApiUnlock(); if ((rtn = connectToWifi_mtxext()) != CLARITY_SUCCESS) { CLAR_PRINT_ERROR(); } if (rtn != CLARITY_SUCCESS) { clarityCC3000ApiLock(); wlan_disconnect(); wlan_stop(); clarityCC3000ApiUnlock(); mgmtData.active = false; } return rtn; }
/******************************************************************************* * Function Name : Start_Smart_Config. * Description : The function triggers a smart configuration process on CC3000. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Start_Smart_Config(void) { WLAN_SMART_CONFIG_FINISHED = 0; WLAN_SMART_CONFIG_STOP = 0; WLAN_SERIAL_CONFIG_DONE = 0; WLAN_CONNECTED = 0; WLAN_DHCP = 0; WLAN_CAN_SHUTDOWN = 0; SPARK_SOCKET_CONNECTED = 0; SPARK_HANDSHAKE_COMPLETED = 0; SPARK_FLASH_UPDATE = 0; SPARK_LED_FADE = 0; LED_SetRGBColor(RGB_COLOR_BLUE); LED_On(LED_RGB); /* Reset all the previous configuration */ wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); NVMEM_Spark_File_Data[WLAN_POLICY_FILE_OFFSET] = 0; nvmem_write(NVMEM_SPARK_FILE_ID, 1, WLAN_POLICY_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_POLICY_FILE_OFFSET]); /* Wait until CC3000 is disconnected */ while (WLAN_CONNECTED == 1) { //Delay 100ms Delay(100); hci_unsolicited_event_handler(); } /* Create new entry for AES encryption key */ nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16); /* Write AES key to NVMEM */ aes_write_key((unsigned char *)(&smartconfigkey[0])); wlan_smart_config_set_prefix((char*)aucCC3000_prefix); /* Start the SmartConfig start process */ wlan_smart_config_start(1); WiFiCredentialsReader wifi_creds_reader(wifi_add_profile_callback); /* Wait for SmartConfig/SerialConfig to finish */ while (!(WLAN_SMART_CONFIG_FINISHED | WLAN_SERIAL_CONFIG_DONE)) { if(WLAN_DELETE_PROFILES && wlan_ioctl_del_profile(255) == 0) { int toggle = 25; while(toggle--) { LED_Toggle(LED_RGB); Delay(50); } NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET] = 0; nvmem_write(NVMEM_SPARK_FILE_ID, 1, WLAN_PROFILE_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET]); WLAN_DELETE_PROFILES = 0; } else { LED_Toggle(LED_RGB); Delay(250); wifi_creds_reader.read(); } } LED_On(LED_RGB); /* read count of wlan profiles stored */ nvmem_read(NVMEM_SPARK_FILE_ID, 1, WLAN_PROFILE_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET]); // if(NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET] >= 7) // { // if(wlan_ioctl_del_profile(255) == 0) // NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET] = 0; // } if(WLAN_SMART_CONFIG_FINISHED) { /* Decrypt configuration information and add profile */ wlan_profile_index = wlan_smart_config_process(); } if(wlan_profile_index != -1) { NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET] = wlan_profile_index + 1; } /* write count of wlan profiles stored */ nvmem_write(NVMEM_SPARK_FILE_ID, 1, WLAN_PROFILE_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET]); /* Configure to connect automatically to the AP retrieved in the Smart config process */ wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE); NVMEM_Spark_File_Data[WLAN_POLICY_FILE_OFFSET] = 1; nvmem_write(NVMEM_SPARK_FILE_ID, 1, WLAN_POLICY_FILE_OFFSET, &NVMEM_Spark_File_Data[WLAN_POLICY_FILE_OFFSET]); /* Reset the CC3000 */ wlan_stop(); Delay(100); wlan_start(0); SPARK_WLAN_STARTED = 1; /* Mask out all non-required events */ wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_ASYNC_PING_REPORT); LED_SetRGBColor(RGB_COLOR_GREEN); LED_On(LED_RGB); Set_NetApp_Timeout(); WLAN_SMART_CONFIG_START = 0; }
void StartSmartConfig(void) { ulSmartConfigFinished = 0; ulCC3000Connected = 0; ulCC3000DHCP = 0; OkToDoShutDown=0; // Reset all the previous configuration wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE); wlan_ioctl_del_profile(255); //Wait until CC3000 is disconnected while (ulCC3000Connected == 1) { __delay_cycles(1000); } // Trigger the Smart Config process // Start blinking LED6 during Smart Configuration process turnLedOn(6); wlan_smart_config_set_prefix((char*)aucCC3000_prefix); turnLedOff(6); // Start the SmartConfig start process wlan_smart_config_start(0 /* unencrypted SmartConfig */); turnLedOn(6); // Wait for Smartconfig process complete while (ulSmartConfigFinished == 0) { __delay_cycles(60000); turnLedOff(6); __delay_cycles(60000); turnLedOn(6); } turnLedOn(6); #ifndef CC3000_UNENCRYPTED_SMART_CONFIG // create new entry for AES encryption key nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16); // write AES key to NVMEM aes_write_key((unsigned char *)(&smartconfigkey[0])); // Decrypt configuration information and add profile wlan_smart_config_process(); #endif // Configure to connect automatically to the AP retrieved in the // Smart config process wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE); // reset the CC3000 wlan_stop(); __delay_cycles(60000); DispatcherUartSendPacket((unsigned char*)pucUARTCommandSmartConfigDoneString, sizeof(pucUARTCommandSmartConfigDoneString)); wlan_start(0); // Mask out all non-required events wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT); }
/** * @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; }
void StartSmartConfig(void) { long rval; if (!isInitialized) { printf("CC3000 not initialized; can't run Smart Config.\n"); return; } printf("Starting Smart Config\n"); printf(" Disabling auto-connect policy..."); if ((rval = wlan_ioctl_set_connection_policy(DISABLE, DISABLE, DISABLE)) !=0) { printf(" Failed!\n Setting auto connection policy failed, error: %lx\n", (unsigned long)rval); return; } printf(" Succeed\n"); printf(" Deleting all existing profiles..."); fflush(stdout); if ((rval = wlan_ioctl_del_profile(255)) !=0) { printf(" Failed!\n Deleting all profiles failed, error: %lx\n", (unsigned long)rval); return; } printf(" Succeed\n"); wait_on(20*MS_PER_SEC, &ulCC3000Connected, 0, " Waiting until disconnected"); printf(" Setting smart config prefix..."); fflush(stdout); if ((rval = wlan_smart_config_set_prefix(simpleConfigPrefix)) !=0) { printf(" Failed!\n Setting smart config prefix failed, error: %lx", (unsigned long)rval); return; } printf(" Succeed\n"); printf(" Starting smart config..."); fflush(stdout); if ((rval = wlan_smart_config_start(0)) !=0) { printf(" Failed!\n Starting smart config failed, error: %lx\n", (unsigned long)rval); return; } printf(" Succeed\n"); if (!wait_on(30*MS_PER_SEC, &ulSmartConfigFinished, 1, " Waiting on Starting smart config done")) { printf(" Timed out waiting for Smart Config to finish. Hopefully it did anyway\n"); } printf(" Smart Config packet %s!\n",ulSmartConfigFinished ? "seen" : "NOT seen"); printf(" Enabling auto-connect policy..."); fflush(stdout); if ((rval = wlan_ioctl_set_connection_policy(DISABLE, DISABLE, ENABLE)) !=0) { printf(" Failed!\n Setting auto connection policy failed, error: %lx\n", (unsigned long)rval); return; } printf(" Succeed\n"); printf(" Stopping CC3000...\n"); fflush(stdout); wlan_stop(); /* No error returned here, so nothing to check */ printf(" Pausing for 2 seconds...\n"); sleep(2); printf(" Restarting CC3000... \n"); wlan_start(0); /* No error returned here, so nothing to check */ if (!wait_on(20*MS_PER_SEC, &ulCC3000Connected, 1, " Waiting for connection to AP")) { printf(" Timed out waiting for connection to AP\n"); return; } if (!wait_on(15*MS_PER_SEC, &ulCC3000DHCP, 1, " Waiting for IP address from DHCP")) { printf(" Timed out waiting for IP address from DHCP\n"); return; } printf(" Sending mDNS broadcast to signal we're done with Smart Config...\n"); fflush(stdout); /* The API documentation says mdnsAdvertiser() is supposed to return 0 on * success and SOC_ERROR on failure, but it looks like what it actually * returns is the socket number it used. So we ignore it. */ mdnsadvertiser(1, device_name, strlen(device_name)); printf(" Smart Config finished Successfully!\n"); ShowInformation(); fflush(stdout); }
void patch_prog_start() { unsigned short index; unsigned char *pRMParams; printf("Initializing module...\n"); // Init module and request to load with no patches. // This is in order to overwrite restrictions to // write to specific places in EEPROM. initDriver(1); // Read MAC address. mac_status = nvmem_get_mac_address(cMacFromEeprom); return_status = 1; printf("Reading RM parameters...\n"); while ((return_status) && (counter < 3)) { // Read RM parameters. // Read in 16 parts to work with tiny driver. return_status = 0; pRMParams = cRMParamsFromEeprom; for (index = 0; index < 16; index++) { return_status |= nvmem_read(NVMEM_RM_FILEID, 8, 8*index, pRMParams); pRMParams += 8; } counter++; } // If RM file is not valid, load the default one. if (counter == 3) { printf("RM is not valid, loading default one...\n"); pRMParams = (unsigned char *)cRMdefaultParams; } else { printf("RM is valid.\n"); pRMParams = cRMParamsFromEeprom; } return_status = 1; printf("Writing new FAT\n"); while (return_status) { // Write new FAT. return_status = fat_write_content(aFATEntries[0], aFATEntries[1]); } return_status = 1; printf("Writing RM parameters...\n"); while (return_status) { // Write RM parameters. // Write in 4 parts to work with tiny driver. return_status = 0; for (index = 0; index < 4; index++) { return_status |= nvmem_write(NVMEM_RM_FILEID, 32, 32*index, (pRMParams + 32*index)); } } return_status = 1; // Write back the MAC address, only if exists. if (mac_status == 0) { // Zero out MCAST bit if set. cMacFromEeprom[0] &= 0xfe; printf("Writing back MAC address..\n"); while (return_status) { return_status = nvmem_set_mac_address(cMacFromEeprom); } } // Update driver ucStatus_Dr = 1; printf("Updating driver patch...\n"); while (ucStatus_Dr) { // Writing driver patch to EEPRROM - PROTABLE CODE // Note that the array itself is changing between the // different Service Packs. ucStatus_Dr = nvmem_write_patch(NVMEM_WLAN_DRIVER_SP_FILEID, drv_length, wlan_drv_patch); } // Update firmware ucStatus_FW = 1; printf("Updating firmware patch...\n"); while (ucStatus_FW) { // Writing FW patch to EEPRROM - PROTABLE CODE // Note that the array itself is changing between the // different Service Packs. ucStatus_FW = nvmem_write_patch(NVMEM_WLAN_FW_SP_FILEID, fw_length, fw_patch); } printf("Update complete, resetting module\n"\ "If this doesn't work, reset manually...\n"); wlan_stop(); systick_sleep(500); // Re-Init module and request to load with patches. initDriver(0); // If MAC does not exist, it is recommended // that the user will write a valid mac address. if (mac_status != 0) { printf("MAC address is not valid, please write a new one\n"); } // Patch update done printf("All done, call wlan.patch_version()\n"); }
/*------------------------------------------------------------------------ Spider_SmartConfig Use Smart config function set Spider connect to a exist AP. return 0, Connect success. -----------------------------------------------------------------------*/ int Spider_SmartConfig(void){ SmartConfigFinished = 0; SpiderConnected = 0; SpiderDHCP = 0; SpiderCanShutDown = 0; // Reset all the previous configuration wlan_ioctl_set_connection_policy(0, 0, 0); wlan_ioctl_del_profile(255); //Wait until Spider is disconnected while (SpiderConnected == 1){ delay(1); hci_unsolicited_event_handler(); } // create new entry for AES encryption key nvmem_create_entry(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE); // write AES key to NVMEM aes_write_key((unsigned char *)(&SmartConfig_key[0])); // Start blinking LED1 during Smart Configuration process digitalWrite(13, HIGH); wlan_smart_config_set_prefix((char*)SmartConfig_prefix); digitalWrite(13, LOW); // Start the SmartConfig start process if(wlan_smart_config_start(0) != 0){ return -1; } // Wait for Smart config to finish while (SmartConfigFinished == 0){ digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); } digitalWrite(13, LOW); // Decrypt configuration information and add profile //wlan_smart_config_process(); #if 1 // Configure to connect automatically to the AP retrieved in the // Smart config process wlan_ioctl_set_connection_policy(0, 0, 1); // reset the CC3000 wlan_stop(); delay(100); wlan_start(0); // Set CC3000 event masking, right now without ping report. wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT); #endif return 0; }
void Initialize(void) { #ifdef CONFIG_EXAMPLES_CC3000_MEM_CHECK mmstart = mallinfo(); memcpy(&mmprevious, &mmstart, sizeof(struct mallinfo)); show_memory_usage(&mmstart,&mmprevious); #endif uint8_t fancyBuffer[MAC_ADDR_LEN]; if (isInitialized) { printf("CC3000 already initialized. Shutting down and restarting...\n"); wlan_stop(); usleep(1000000); /* Delay 1s */ } printf("Initializing CC3000...\n"); CC3000_Init(); #ifdef CONFIG_EXAMPLES_CC3000_STACK_CHECK stkmon_disp(); #endif printf(" CC3000 init complete.\n"); if (nvmem_read_sp_version(fancyBuffer) == 0) { printf(" Firmware version is: "); printf("%d", fancyBuffer[0]); printf("."); printf("%d\n", fancyBuffer[1]); } else { printf("Unable to get firmware version. Can't continue.\n"); return; } #if 0 if (nvmem_get_mac_address(fancyBuffer) == 0) { printf(" MAC address: "); for (i = 0; i < MAC_ADDR_LEN; i++) { if (i != 0) { printf(":"); } printf("%x", fancyBuffer[i]); } printf("\n"); isInitialized = true; } else { printf("Unable to get MAC address. Can't continue.\n"); } #else isInitialized = true; #endif #ifdef CONFIG_EXAMPLES_CC3000_MEM_CHECK mmprevious = mallinfo(); show_memory_usage(&mmstart,&mmprevious); #endif }
wlan_result_t wlan_deactivate() { wlan_stop(); return 0; }
void StartSmartConfig(void) { // Reset all the previous configuration // wlan_ioctl_set_connection_policy(0, 0, 0); wlan_ioctl_del_profile(255); //Wait until CC3000 is dissconected while (ulCC3000Connected == 1) { __delay_cycles(100); } // Start blinking LED6 during Smart Configuration process turnLedOn(LED6); wlan_smart_config_set_prefix(aucCC3000_prefix); //wlan_first_time_config_set_prefix(aucCC3000_prefix); turnLedOff(LED6); // Start the SmartConfig start process wlan_smart_config_start(1); turnLedOn(LED6); // // Wait for Smart config finished // while (ulSmartConfigFinished == 0) { __delay_cycles(6000000); turnLedOn(LED6); __delay_cycles(6000000); turnLedOff(LED6); } turnLedOff(LED6); // create new entry for AES encryption key nvmem_create_entry(NVMEM_AES128_KEY_FILEID,16); // write AES key to NVMEM aes_write_key((unsigned char *)(&smartconfigkey[0])); // Decrypt configuration information and add profile wlan_smart_config_process(); // // Configure to connect automatically to the AP retrieved in the // Smart config process // wlan_ioctl_set_connection_policy(0, 0, 1); // // reset the CC3000 // wlan_stop(); __delay_cycles(600000); wlan_start(0); ConnectUsingSmartConfig = 1; ulSmartConfigFinished = 0; // // Mask out all non-required events // wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT); }
/*JSON{ "type":"method", "class" : "WLAN", "name" : "disconnect", "generate" : "jswrap_wlan_disconnect", "description" : "Completely uninitialise and power down the CC3000. After this you'll have to use ```require(\"CC3000\").connect()``` again." }*/ void jswrap_wlan_disconnect(JsVar *wlanObj) { jsvUnLock(jsvObjectSetChild(wlanObj,JS_HIDDEN_CHAR_STR"DISC", jsvNewFromBool(true))); networkState = NETWORKSTATE_OFFLINE; // force offline //wlan_disconnect(); wlan_stop(); }
void StartSmartConfig(void) { ulSmartConfigFinished = 0; ulCC3000Connected = 0; ulCC3000DHCP = 0; OkToDoShutDown=0; // Reset all the previous configuration if (wlan_ioctl_set_connection_policy(0, 0, 0) != 0) { digitalWrite(ErrorLED, HIGH); return; } if (wlan_ioctl_del_profile(255) != 0) { digitalWrite(ErrorLED, HIGH); return; } //Wait until CC3000 is disconnected while (ulCC3000Connected == 1) { delayMicroseconds(100); } // Serial.println("waiting for disconnect"); // Trigger the Smart Config process // Start blinking LED6 during Smart Configuration process digitalWrite(ConnLED, HIGH); if (wlan_smart_config_set_prefix((char*)aucCC3000_prefix) != 0){ digitalWrite(ErrorLED, HIGH); return; } digitalWrite(ConnLED, LOW); // Start the SmartConfig start process if (wlan_smart_config_start(0) != 0){ digitalWrite(ErrorLED, HIGH); return; } if (DEBUG_MODE) { Serial.println("smart config start"); } digitalWrite(ConnLED, HIGH); // Wait for Smartconfig process complete while (ulSmartConfigFinished == 0) { delay(500); digitalWrite(ConnLED, LOW); delay(500); digitalWrite(ConnLED, HIGH); } if (DEBUG_MODE) { Serial.println("smart config finished"); } digitalWrite(ConnLED, LOW); // Configure to connect automatically to the AP retrieved in the // Smart config process. Enabled fast connect. if (wlan_ioctl_set_connection_policy(0, 0, 1) != 0){ digitalWrite(ErrorLED, HIGH); return; } // reset the CC3000 wlan_stop(); delayMicroseconds(500); wlan_start(0); // Mask out all non-required events wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT); if (DEBUG_MODE) { Serial.print("Config done"); } }
void main (void) { unsigned long wake_utt; int rc; long lrc; char as_text[BSP430_UPTIME_AS_TEXT_LENGTH]; uint32_u ntp_addr; uint32_u self_addr; vBSP430platformInitialize_ni(); (void)iBSP430consoleInitialize(); cprintf("\nntp " __DATE__ " " __TIME__ "\n"); /* Initialization can be done with interrupts disabled, since the * function does nothing but store callbacks. We use the same * callback for all three update capabilities. */ rc = iBSP430cc3000spiInitialize(wlan_cb, NULL, NULL, NULL); if (0 > rc) { cprintf("ERR: Initialization failed: %d\n", rc); return; } BSP430_CORE_ENABLE_INTERRUPT(); /* Local addresses use all zeros for inet addr. bind does not * support dynamic assignment of unused port through sin_port=0. */ memset(&local_addr, 0, sizeof(local_addr)); local_addr.sai.sin_family = AF_INET; local_addr.sai.sin_port = htons(60123); /* Remote server will be determined by DNS from the NTP pool once we * start. */ remote_addr = local_addr; remote_addr.sai.sin_port = htons(123); ntp_addr.u32 = 0; self_addr.u32 = 0; cprintf("Remote: %s:%u\n", net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port)); rc = sizeof(sBSP430uptimeNTPPacketHeader); if (48 != rc) { cprintf("ERR: NTP header size %d\n", rc); return; } wake_utt = ulBSP430uptime(); (void)rc; while (1) { unsigned long timeout_utt; do { tNetappIpconfigRetArgs ipc; unsigned long start_utt; unsigned long finished_utt; int sfd; int nfds; fd_set rfds; int servers_left; int retries_left; /* Clear everything as we're starting a cycle */ BSP430_CORE_DISABLE_INTERRUPT(); do { event_flags_v = 0; start_utt = ulBSP430uptime_ni(); } while (0); BSP430_CORE_ENABLE_INTERRUPT(); /* Start the WAN process. This is asynchronous; wait up to 2 * seconds for it to complete. */ cprintf("%s: ", xBSP430uptimeAsText(start_utt, as_text)); cputchar('W'); wlan_start(0); vBSP430ledSet(BSP430_LED_RED, 1); (void)wlan_set_event_mask(0UL); lrc = BSP430_UPTIME_MS_TO_UTT(2000); timeout_utt = ulBSP430uptime() + lrc; while ((! (EVENT_FLAG_WLANCONN & event_flags_v)) && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) { } if (! (EVENT_FLAG_WLANCONN & event_flags_v)) { cprintf("WLAN start failed\n"); break; } /* Wait for IP connectivity (signalled by a DHCP event). * Continue using the previous timeout. */ cputchar('D'); while ((! (EVENT_FLAG_IPCONN & event_flags_v)) && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) { } if (! (EVENT_FLAG_IPCONN & event_flags_v)) { cprintf("IP conn failed\n"); break; } /* Inspect the IP configuration. Sometimes we get the event, * but there's no IP assigned. */ netapp_ipconfig(&ipc); memcpy(self_addr.u8, ipc.aucIP, sizeof(self_addr)); if (! self_addr.u32) { cprintf("IP assignment failed\n"); break; } vBSP430ledSet(BSP430_LED_GREEN, 1); /* Obtain a UDP socket and bind it for local operations. */ cputchar('I'); sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (0 > sfd) { cprintf("socket() failed: %d\n", sfd); break; } cputchar('S'); lrc = bind(sfd, &local_addr.sa, sizeof(local_addr.sa)); if (0 > lrc) { cprintf("bind() failed: %ld\n", lrc); break; } cputchar('B'); servers_left = NTP_SERVERS_PER_ATTEMPT; retries_left = NTP_REQUESTS_PER_SERVER; do { sBSP430uptimeNTPPacketHeader ntp0; sBSP430uptimeNTPPacketHeader ntp1; int have_invalid_epoch; struct timeval tv; sockaddr_u src; socklen_t slen = sizeof(src); unsigned long recv_utt; uint64_t recv_ntp; int64_t adjustment_ntp; long adjustment_ms; unsigned long rtt_us; have_invalid_epoch = 0 != iBSP430uptimeCheckEpochValidity(); if (! remote_addr.sai.sin_addr.s_addr) { const char ntp_fqdn[] = "0.pool.ntp.org"; ntp_addr.u32 = 0; rc = gethostbyname((char *)ntp_fqdn, sizeof(ntp_fqdn)-1, &ntp_addr.u32); cputchar('d'); if (-95 == rc) { /* ARP request failed; retry usually works */ rc = gethostbyname((char *)ntp_fqdn, sizeof(ntp_fqdn)-1, &ntp_addr.u32); cputchar('d'); } if (0 == ntp_addr.u32) { cprintf("gethostbyname(%s) failed: %d\n", ntp_fqdn, rc); rc = -1; break; } remote_addr.sai.sin_addr.s_addr = htonl(ntp_addr.u32); cprintf("{%s}", net_ipv4AsText(&remote_addr.sai.sin_addr)); retries_left = NTP_REQUESTS_PER_SERVER; } /* Configure the NTP request and send it */ iBSP430uptimeInitializeNTPRequest(&ntp0); iBSP430uptimeSetNTPXmtField(&ntp0, NULL); BSP430_CORE_DISABLE_INTERRUPT(); do { /* Clear the shutdown bit, so we know when it's ok to shut * down after this send */ event_flags_v &= ~EVENT_FLAG_SHUTDOWN; } while (0); BSP430_CORE_ENABLE_INTERRUPT(); rc = sendto(sfd, &ntp0, sizeof(ntp0), 0, &remote_addr.sa, sizeof(remote_addr.sai)); if (sizeof(ntp0) != rc) { cprintf("sendto %s:%u failed: %d\n", net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port), rc); rc = -1; break; } cputchar('s'); /* If we get an answer it should be here in less than 100 * ms, but give it 400 ms just to be kind. */ tv.tv_sec = 0; tv.tv_usec = 400000UL; FD_ZERO(&rfds); FD_SET(sfd, &rfds); nfds = sfd+1; rc = select(nfds, &rfds, NULL, NULL, &tv); if (! FD_ISSET(sfd, &rfds)) { /* We didn't get an answer. If there are any retries left, use them. */ if (0 < retries_left--) { rc = 1; continue; } /* No retries left on this server: forget about it. If * there are any servers left, try another. */ cputchar('!'); remote_addr.sai.sin_addr.s_addr = 0; if (0 < servers_left--) { rc = 1; continue; } /* No retries from all available servers. Fail this attempt */ cprintf("no responsive NTP server found\n"); rc = -1; break; } /* Got a response. Record the time it came in and then read * it (no high-resolution packet RX time available, but we * believe it's here already so set the RX time first). The * message is unacceptable if it isn't an NTP packet. */ recv_utt = ulBSP430uptime(); rc = recvfrom(sfd, &ntp1, sizeof(ntp1), 0, &src.sa, &slen); if (sizeof(ntp1) != rc) { cprintf("recv failed: %d\n", rc); rc = -1; break; } cputchar('r'); /* Convert the RX time to NTP, then process the message to * determine the offset. */ rc = iBSP430uptimeAsNTP(recv_utt, &recv_ntp, have_invalid_epoch); if (0 != rc) { cprintf("NTP decode failed: %d\n", rc); continue; } rc = iBSP430uptimeProcessNTPResponse(&ntp0, &ntp1, recv_ntp, &adjustment_ntp, &adjustment_ms, &rtt_us); if (0 != rc) { cprintf("Process failed: %d\n", rc); continue; } if (have_invalid_epoch) { rc = iBSP430uptimeSetEpochFromNTP(BSP430_UPTIME_BYPASS_EPOCH_NTP + adjustment_ntp); cputchar('E'); if (0 != rc) { cprintf("\nERR: SetEpoch failed: %d\n", rc); } #if (NTP_ADJUST_EACH_ITER - 0) } else { rc = iBSP430uptimeAdjustEpochFromNTP(adjustment_ntp); cputchar('A'); if (0 != rc) { cprintf("\nERR: AdjustEpoch failed: %d\n", rc); } #endif } cprintf("[%s:%u adj %lld ntp = %ld ms, rtt %lu us]", net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port), adjustment_ntp, adjustment_ms, rtt_us); } while (0 != rc); if (0 != rc) { cprintf("NTP query failed\n"); break; } #if 0 /* The shutdown OK seems to arrive about 1000 ms after the last * transmit, which is unnecessarily long. As we're not doing * TCP, there's no reason to wait for it. */ lrc = BSP430_UPTIME_MS_TO_UTT(4000); timeout_utt = ulBSP430uptime() + lrc; while ((! (EVENT_FLAG_SHUTDOWN & event_flags_v)) && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) { } if (! (EVENT_FLAG_SHUTDOWN & event_flags_v)) { cprintf("SHUTDOWN ok never received\n"); break; } #endif finished_utt = ulBSP430uptime(); cprintf("[%s]\n", xBSP430uptimeAsText(finished_utt - start_utt, as_text)); } while (0); BSP430_CORE_DISABLE_INTERRUPT(); do { event_flags_v = 0; } while (0); BSP430_CORE_ENABLE_INTERRUPT(); wlan_stop(); vBSP430ledSet(BSP430_LED_GREEN, 0); vBSP430ledSet(BSP430_LED_RED, 0); wake_utt += 60 * ulBSP430uptimeConversionFrequency_Hz(); while (0 < lBSP430uptimeSleepUntil(wake_utt, LPM2_bits)) { } } cprintf("Fell off end\n"); }