Example #1
0
BOOL cc3000_deleteAllProfiles() {
  printf("deleting profiles\n");
  return wlan_ioctl_del_profile(255) == 0;
}
/**
 * @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;
}
Example #3
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;
}
Example #4
0
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 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);
}
/*------------------------------------------------------------------------
  
  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;
}
Example #7
0
void SPARK_WLAN_Setup(void (*presence_announcement_callback)(void))
{
  announce_presence = presence_announcement_callback;

	/* Initialize CC3000's CS, EN and INT pins to their default states */
	CC3000_WIFI_Init();

	/* Configure & initialize CC3000 SPI_DMA Interface */
	CC3000_SPI_DMA_Init();

	/* WLAN On API Implementation */
	wlan_init(WLAN_Async_Callback, WLAN_Firmware_Patch, WLAN_Driver_Patch, WLAN_BootLoader_Patch,
				CC3000_Read_Interrupt_Pin, CC3000_Interrupt_Enable, CC3000_Interrupt_Disable, CC3000_Write_Enable_Pin);

	Delay(100);

	/* Trigger a WLAN device */
	wlan_start(0);

	SPARK_LED_FADE = 0;

	SPARK_WLAN_STARTED = 1;

	/* Mask out all non-required events from CC3000 */
	wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_ASYNC_PING_REPORT);

	if(NVMEM_SPARK_Reset_SysFlag == 0x0001 || nvmem_read(NVMEM_SPARK_FILE_ID, NVMEM_SPARK_FILE_SIZE, 0, NVMEM_Spark_File_Data) != 0)
	{
		/* Delete all previously stored wlan profiles */
		wlan_ioctl_del_profile(255);

		/* Create new entry for Spark File in CC3000 EEPROM */
		nvmem_create_entry(NVMEM_SPARK_FILE_ID, NVMEM_SPARK_FILE_SIZE);

		int i = 0;
		for(i = 0; i < NVMEM_SPARK_FILE_SIZE; i++)
			NVMEM_Spark_File_Data[i] = 0;

		nvmem_write(NVMEM_SPARK_FILE_ID, NVMEM_SPARK_FILE_SIZE, 0, NVMEM_Spark_File_Data);

		NVMEM_SPARK_Reset_SysFlag = 0x0000;
		Save_SystemFlags();
	}

	if(!WLAN_MANUAL_CONNECT)
	{
		if(NVMEM_Spark_File_Data[WLAN_PROFILE_FILE_OFFSET] == 0)
		{
			WLAN_SMART_CONFIG_START = 1;
		}
		else if(NVMEM_Spark_File_Data[WLAN_POLICY_FILE_OFFSET] == 0)
		{
			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]);
		}
	}

#if defined (USE_SPARK_CORE_V02)
	if(WLAN_MANUAL_CONNECT || !WLAN_SMART_CONFIG_START)
	{
		LED_SetRGBColor(RGB_COLOR_GREEN);
		LED_On(LED_RGB);
	}
#endif

	nvmem_read_sp_version(patchVer);
	if (patchVer[1] == 24)//19 for old patch
	{
		/* Latest Patch Available after flashing "cc3000-patch-programmer.bin" */
	}

	Clear_NetApp_Dhcp();

	Set_NetApp_Timeout();
}
Example #8
0
/*******************************************************************************
 * 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;

	TimingSparkCommTimeout = 0;

#if defined (USE_SPARK_CORE_V02)
	LED_SetRGBColor(RGB_COLOR_BLUE);
	LED_On(LED_RGB);
#endif

	/* 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--)
			{
#if defined (USE_SPARK_CORE_V01)
				LED_Toggle(LED2);
#elif defined (USE_SPARK_CORE_V02)
				LED_Toggle(LED_RGB);
#endif
				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
		{
#if defined (USE_SPARK_CORE_V01)
			LED_Toggle(LED2);
#elif defined (USE_SPARK_CORE_V02)
			LED_Toggle(LED_RGB);
#endif
			Delay(250);

			wifi_creds_reader.read();
		}
	}

#if defined (USE_SPARK_CORE_V01)
	LED_Off(LED2);
#elif defined (USE_SPARK_CORE_V02)
	LED_On(LED_RGB);
#endif

	/* 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);

#if defined (USE_SPARK_CORE_V02)
    LED_SetRGBColor(RGB_COLOR_GREEN);
	LED_On(LED_RGB);
#endif

	Set_NetApp_Timeout();

	WLAN_SMART_CONFIG_START = 0;
}
Example #9
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);
}
Example #10
0
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");
  }
}