Ejemplo n.º 1
0
// Data = AuthData#FTCServer(localIp/netMask/gateWay/dnsServer)
void EasyLinkNotify_EasyLinkGetExtraDataHandler(int datalen, char* data, mico_Context_t * const inContext)
{
  OSStatus err;
  int index ;
  char address[16];
  easylink_log_trace();
  uint32_t *ipInfo, ipInfoCount;
  require_action(inContext, exit, err = kParamErr);
  char *debugString;

  debugString = DataToHexStringWithSpaces( (const uint8_t *)data, datalen );
  easylink_log("Get user info: %s", debugString);
  free(debugString);

  for(index = datalen - 1; index>=0; index-- ){
    if(data[index] == '#' &&( (datalen - index) == 5 || (datalen - index) == 25 ) )
      break;
  }
  require_action(index >= 0, exit, err = kParamErr);

  data[index++] = 0x0;
  ipInfo = (uint32_t *)&data[index];
  ipInfoCount = (datalen - index)/sizeof(uint32_t);
  require_action(ipInfoCount >= 1, exit, err = kParamErr);
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  inContext->flashContentInRam.micoSystemConfig.easylinkServerIP = *(uint32_t *)(ipInfo);

  if(ipInfoCount == 1){
    inContext->flashContentInRam.micoSystemConfig.dhcpEnable = true;
    inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP);
    easylink_log("Get auth info: %s, EasyLink server ip address: %s", data, address);
  }else{
    inContext->flashContentInRam.micoSystemConfig.dhcpEnable = false;
    ipInfo = (uint32_t *)&data[index];
    inet_ntoa(inContext->flashContentInRam.micoSystemConfig.localIp, *(uint32_t *)(ipInfo+1));
    inet_ntoa(inContext->flashContentInRam.micoSystemConfig.netMask, *(uint32_t *)(ipInfo+2));
    inet_ntoa(inContext->flashContentInRam.micoSystemConfig.gateWay, *(uint32_t *)(ipInfo+3));
    inet_ntoa(inContext->flashContentInRam.micoSystemConfig.dnsServer, *(uint32_t *)(ipInfo+4));
    strcpy((char *)inContext->micoStatus.localIp, inContext->flashContentInRam.micoSystemConfig.localIp);
    strcpy((char *)inContext->micoStatus.netMask, inContext->flashContentInRam.micoSystemConfig.netMask);
    strcpy((char *)inContext->micoStatus.gateWay, inContext->flashContentInRam.micoSystemConfig.gateWay);
    strcpy((char *)inContext->micoStatus.dnsServer, inContext->flashContentInRam.micoSystemConfig.dnsServer);
    inet_ntoa( address, inContext->flashContentInRam.micoSystemConfig.easylinkServerIP);
    easylink_log("Get auth info: %s, EasyLink server ip address: %s, local IP info:%s %s %s %s ", data, address, inContext->flashContentInRam.micoSystemConfig.localIp,\
    inContext->flashContentInRam.micoSystemConfig.netMask, inContext->flashContentInRam.micoSystemConfig.gateWay,inContext->flashContentInRam.micoSystemConfig.dnsServer);
  }
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);

  require_noerr(ConfigELRecvAuthData(data, inContext), exit);
  
  EasylinkFailed = false;
  mico_rtos_set_semaphore(&easylink_sem);
  ConfigEasyLinkIsSuccess(inContext);
  return;

exit:
  easylink_log("ERROR, err: %d", err);
  ConfigWillStop(inContext);
  MicoSystemReboot();
}
Ejemplo n.º 2
0
OSStatus _FTCRespondInComingMessage(int fd, HTTPHeader_t* inHeader, mico_Context_t * const inContext)
{
    OSStatus err = kUnknownErr;
    const char *        value;
    size_t              valueSize;

    easylink_log_trace();

    switch(inHeader->statusCode){
      case kStatusAccept:
        easylink_log("Easylink server accepted!");
        err = kNoErr;
        goto exit;
      break;
      case kStatusOK:
        easylink_log("Easylink server respond status OK!");
        err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL );
        require_noerr(err, exit);
        if( strnicmpx( value, valueSize, kMIMEType_JSON ) == 0 ){
          easylink_log("Receive JSON config data!");
          err = ConfigIncommingJsonMessage( inHeader->extraDataPtr, inContext);
          SocketClose(&fd);
          inContext->micoStatus.sys_state = eState_Software_Reset;
          require(inContext->micoStatus.sys_state_change_sem, exit);
          mico_rtos_set_semaphore(&inContext->micoStatus.sys_state_change_sem);
        }else if(strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){
          easylink_log("Receive OTA data!");
          mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
          memset(&inContext->flashContentInRam.bootTable, 0, sizeof(boot_table_t));
          inContext->flashContentInRam.bootTable.length = inHeader->contentLength;
          inContext->flashContentInRam.bootTable.start_address = UPDATE_START_ADDRESS;
          inContext->flashContentInRam.bootTable.type = 'A';
          inContext->flashContentInRam.bootTable.upgrade_type = 'U';
          inContext->flashContentInRam.micoSystemConfig.easyLinkEnable = false;
          MICOUpdateConfiguration(inContext);
          mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
          SocketClose(&fd);
          inContext->micoStatus.sys_state = eState_Software_Reset;
          require(inContext->micoStatus.sys_state_change_sem, exit);
          mico_rtos_set_semaphore(&inContext->micoStatus.sys_state_change_sem);
        }else{
          return kUnsupportedDataErr;
        }
        err = kNoErr;
        goto exit;
      break;
      default:
        goto exit;
    }

 exit:
    return err;

}
Ejemplo n.º 3
0
OSStatus startEasyLink( mico_Context_t * const inContext)
{
  easylink_log_trace();
  OSStatus err = kUnknownErr;
  easylinkClient_fd = -1;

  mico_mcu_powersave_config(true);
  err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)EasyLinkNotify_DHCPCompleteHandler );
  require_noerr( err, exit );    
  err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPoerOffHandler );
  require_noerr( err, exit ); 

  // Start the EasyLink thread
  ConfigWillStart(inContext);
  mico_rtos_init_semaphore(&easylink_sem, 1);
  err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x1000, (void*)inContext );
  require_noerr_action( err, exit, easylink_log("ERROR: Unable to start the EasyLink thread.") );

exit:
  return err;
}
Ejemplo n.º 4
0
void _easylinkStartSoftAp( mico_Context_t * const inContext)
{
  OSStatus err;
  easylink_log_trace();
  network_InitTypeDef_st wNetConfig;

  memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st));
  wNetConfig.wifi_mode = Soft_AP;
  snprintf(wNetConfig.wifi_ssid, 32, "MXCHIP_%c%c%c%c%c%c", inContext->micoStatus.mac[9],  inContext->micoStatus.mac[10], \
                                                            inContext->micoStatus.mac[12], inContext->micoStatus.mac[13],
                                                            inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] );
  strcpy((char*)wNetConfig.wifi_key, "");
  strcpy((char*)wNetConfig.local_ip_addr, "10.10.10.1");
  strcpy((char*)wNetConfig.net_mask, "255.255.255.0");
  strcpy((char*)wNetConfig.gateway_ip_addr, "10.10.10.1");
  wNetConfig.dhcpMode = DHCP_Server;
  micoWlanStart(&wNetConfig);
  easylink_log("Establish soft ap: %s.....", wNetConfig.wifi_ssid);

  if(inContext->flashContentInRam.micoSystemConfig.bonjourEnable == true){
    err = MICOStartBonjourService( Soft_AP , inContext );
    require_noerr(err, exit);
  }
  
  if(inContext->flashContentInRam.micoSystemConfig.configServerEnable == true){
    err = MICOStartConfigServer  ( inContext );
    require_noerr(err, exit);
  }

  ConfigSoftApWillStart( inContext );

exit:
  return;
}
Ejemplo n.º 5
0
void EasyLinkNotify_EasyLinkCompleteHandler(network_InitTypeDef_st *nwkpara, mico_Context_t * const inContext)
{
  OSStatus err;
  easylink_log_trace();
  easylink_log("EasyLink return @ %d", mico_get_time());
  require_action(inContext, exit, err = kParamErr);
  require_action(nwkpara, exit, err = kTimeoutErr);
  
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  memcpy(inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen);
  memcpy(inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen);
  inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key);
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  easylink_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key);
  return;

/*EasyLink is not start*/    
exit:
  easylink_log("ERROR, err: %d", err);
#if defined (CONFIG_MODE_EASYLINK_WITH_SOFTAP)
  EasylinkFailed = true;
  mico_rtos_set_semaphore(&inContext->micoStatus.easylink_sem);
#else
  ConfigWillStop(inContext);
  /*so roll back to previous settings  (if it has) and reboot*/
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  if(inContext->flashContentInRam.micoSystemConfig.configured != unConfigured){
    inContext->flashContentInRam.micoSystemConfig.configured = allConfigured;
    MICOUpdateConfiguration(inContext);
    PlatformSoftReboot();
  }
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  /*module should powd down in default setting*/ 
  wifi_power_down();
  mico_stop_timer(&_Led_EL_timer);
  Platform_LED_SYS_Set_Status(OFF);
#endif

  
  return;
}
Ejemplo n.º 6
0
OSStatus _connectFTCServer( mico_Context_t * const inContext, int *fd)
{
  OSStatus    err;
  struct      sockaddr_t addr;
  json_object *easylink_report = NULL;
  const char  *json_str;
  
  size_t      httpResponseLen = 0;

  *fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  addr.s_ip = inContext->flashContentInRam.micoSystemConfig.easylinkServerIP; 
  addr.s_port = FTC_PORT;  
  err = connect(*fd, &addr, sizeof(addr));
  require_noerr(err, exit);
  _FTCClientConnected = true;

  easylink_log("Connect to FTC server success, fd: %d", *fd);

  easylink_report = ConfigCreateReportJsonMessage( inContext );
  require( easylink_report, exit );

  json_str = json_object_to_json_string(easylink_report);
  require( json_str, exit );

  easylink_log("Send config object=%s", json_str);
  err =  CreateHTTPMessage( "POST", kEasyLinkURLAuth, kMIMEType_JSON, (uint8_t *)json_str, strlen(json_str), &httpResponse, &httpResponseLen );
  require_noerr( err, exit );
  require( httpResponse, exit );

  json_object_put(easylink_report);

  err = SocketSend( *fd, httpResponse, httpResponseLen );
  free(httpResponse);
  require_noerr( err, exit );
  easylink_log("Current configuration sent");

exit:
  return err;
}
Ejemplo n.º 7
0
void EasyLinkNotify_EasyLinkCompleteHandler(network_InitTypeDef_st *nwkpara, mico_Context_t * const inContext)
{
  OSStatus err;
  easylink_log_trace();
  easylink_log("EasyLink return");
  require_action(inContext, exit, err = kParamErr);
  require_action(nwkpara, exit, err = kTimeoutErr);
  
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  memcpy(inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen);
  memset(inContext->flashContentInRam.micoSystemConfig.bssid, 0x0, 6);
  memcpy(inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen);
  inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key);
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  easylink_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key);
  return;

/*EasyLink timeout or error*/    
exit:
  easylink_log("ERROR, err: %d", err);
#if ( MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP)
  EasylinkFailed = true;
  mico_rtos_set_semaphore(&easylink_sem);
#else
  ConfigWillStop(inContext);
  /*so roll back to previous settings  (if it has) and reboot*/
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  if(inContext->flashContentInRam.micoSystemConfig.configured != unConfigured){
    inContext->flashContentInRam.micoSystemConfig.configured = allConfigured;
    MICOUpdateConfiguration(inContext);
    MicoSystemReboot();
  }
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  /*module should powd down in default setting*/ 
  micoWlanPowerOff();
#endif
  return;
}
Ejemplo n.º 8
0
void EasyLinkNotify_WifiStatusHandler(WiFiEvent event, mico_Context_t * const inContext)
{
  easylink_log_trace();
  require(inContext, exit);
  switch (event) {
  case NOTIFY_STATION_UP:
    easylink_log("Access point connected");
    mico_rtos_set_semaphore(&easylink_sem);
    break;
  case NOTIFY_STATION_DOWN:
    break;
  default:
    break;
  }
exit:
  return;
}
Ejemplo n.º 9
0
void _easylinkConnectWiFi( mico_Context_t * const inContext)
{
  easylink_log_trace();
  network_InitTypeDef_adv_st wNetConfig;
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_adv_st));
  
  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  strncpy((char*)wNetConfig.ap_info.ssid, inContext->flashContentInRam.micoSystemConfig.ssid, maxSsidLen);
  wNetConfig.ap_info.security = SECURITY_TYPE_AUTO;
  memcpy(wNetConfig.key, inContext->flashContentInRam.micoSystemConfig.user_key, maxKeyLen);
  wNetConfig.key_len = inContext->flashContentInRam.micoSystemConfig.user_keyLength;
  wNetConfig.dhcpMode = inContext->flashContentInRam.micoSystemConfig.dhcpEnable;
  strncpy((char*)wNetConfig.local_ip_addr, inContext->flashContentInRam.micoSystemConfig.localIp, maxIpLen);
  strncpy((char*)wNetConfig.net_mask, inContext->flashContentInRam.micoSystemConfig.netMask, maxIpLen);
  strncpy((char*)wNetConfig.gateway_ip_addr, inContext->flashContentInRam.micoSystemConfig.gateWay, maxIpLen);
  strncpy((char*)wNetConfig.dnsServer_ip_addr, inContext->flashContentInRam.micoSystemConfig.dnsServer, maxIpLen);

  wNetConfig.wifi_retry_interval = 100;
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  StartAdvNetwork(&wNetConfig);
  easylink_log("connect to %s.....", wNetConfig.ap_info.ssid);
}
Ejemplo n.º 10
0
OSStatus startEasyLink( mico_Context_t * const inContext)
{
  easylink_log_trace();

  OSStatus err = kUnknownErr;
  require_action(inContext->micoStatus.easylink_thread_handler==NULL, exit, err = kParamErr);
  require_action(inContext->micoStatus.easylink_sem==NULL, exit, err = kParamErr);
  inContext->micoStatus.easylinkClient_fd = -1;
  //ps_enable();
  mico_mcu_powersave_config(true);
  mico_rtos_init_mutex(&_mutex);

  err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler );
  require_noerr(err, exit);
  err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)EasyLinkNotify_DHCPCompleteHandler );
  require_noerr( err, exit );    
  err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPoerOffHandler );
  require_noerr( err, exit ); 

  /*Led trigger*/
  mico_init_timer(&_Led_EL_timer, LED_EL_TRIGGER_INTERVAL, _led_EL_Timeout_handler, NULL);
  mico_start_timer(&_Led_EL_timer);

  // Start the EasyLink thread
  ConfigWillStart(inContext);
  mico_rtos_init_semaphore(&inContext->micoStatus.easylink_sem, 1);
  err = mico_rtos_create_thread(&inContext->micoStatus.easylink_thread_handler, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x1000, (void*)inContext );
  require_noerr_action( err, exit, easylink_log("ERROR: Unable to start the EasyLink thread.") );

exit:
  return err;
}
Ejemplo n.º 11
0
void easylink_thread(void *inContext)
{
  OSStatus err = kNoErr;
  mico_Context_t *Context = inContext;
  fd_set readfds;
  struct timeval_t t;
  int reConnCount = 0;

  easylink_log_trace();
  require_action(easylink_sem, threadexit, err = kNotPreparedErr);
  
  if(Context->flashContentInRam.micoSystemConfig.easyLinkEnable != false){
    OpenEasylink2_withdata(EasyLink_TimeOut); 
    easylink_log("Start easylink");
    mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER);
    if(EasylinkFailed == false)
      _easylinkConnectWiFi(Context);
    else{
      _easylinkStartSoftAp(Context);
      mico_rtos_delete_thread(NULL);
      return;
    }
      
  }else{
    mico_rtos_lock_mutex(&Context->flashContentInRam_mutex);
    Context->flashContentInRam.micoSystemConfig.easyLinkEnable = true;
    MICOUpdateConfiguration(Context);
    mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex);
    _easylinkConnectWiFi_fast(Context);
  }

  err = mico_rtos_get_semaphore(&easylink_sem, ConnectFTC_Timeout);
  require_noerr(err, reboot);

  httpHeader = HTTPHeaderCreate();
  require_action( httpHeader, threadexit, err = kNoMemoryErr );
  HTTPHeaderClear( httpHeader );
  
  t.tv_sec = 100;
  t.tv_usec = 0;
    
  while(1){
    if(easylinkClient_fd == -1){
      err = _connectFTCServer(inContext, &easylinkClient_fd);
      require_noerr(err, Reconn);
    }else{
      FD_ZERO(&readfds);  
      FD_SET(easylinkClient_fd, &readfds);

      err = select(1, &readfds, NULL, NULL, &t);
    
      if(FD_ISSET(easylinkClient_fd, &readfds)){
        err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader );

        switch ( err )
        {
          case kNoErr:
            // Read the rest of the HTTP body if necessary
            err = SocketReadHTTPBody( easylinkClient_fd, httpHeader );
            require_noerr(err, Reconn);

            PrintHTTPHeader(httpHeader);
            // Call the HTTPServer owner back with the acquired HTTP header
            err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context );
            require_noerr( err, Reconn );
            // Reuse HTTPHeader
            HTTPHeaderClear( httpHeader );
          break;

          case EWOULDBLOCK:
              // NO-OP, keep reading
          break;

          case kNoSpaceErr:
            easylink_log("ERROR: Cannot fit HTTPHeader.");
            goto Reconn;
          break;

          case kConnectionErr:
            // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed
            easylink_log("ERROR: Connection closed.");
            goto threadexit;
             //goto Reconn;
          break;
          default:
            easylink_log("ERROR: HTTP Header parse internal error: %d", err);
            goto Reconn;
        }
      }
    }
    continue;
Reconn:
    HTTPHeaderClear( httpHeader );
    SocketClose(&easylinkClient_fd);
    easylinkClient_fd = -1;
    require(reConnCount < 6, threadexit);
    reConnCount++;
    sleep(5);
  }  

/*Module is ignored by FTC server, */    
threadexit:
  ConfigWillStop( Context );
  _cleanEasyLinkResource( Context );

  /*Roll back to previous settings (if it has) and reboot*/
  mico_rtos_lock_mutex(&Context->flashContentInRam_mutex);
  if(Context->flashContentInRam.micoSystemConfig.configured != unConfigured){
    Context->flashContentInRam.micoSystemConfig.configured = allConfigured;
    MICOUpdateConfiguration( Context );
    PlatformSoftReboot();
  }
  mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex);


  wifi_power_down();
  mico_rtos_delete_thread( NULL );
  return;

/*SSID or Password is not correct, module cannot connect to wlan, so reboot and enter EasyLink again*/
reboot:
  ConfigWillStop( Context );
  PlatformSoftReboot();
  return;
}
Ejemplo n.º 12
0
void easylink_thread(void *inContext)
{
  OSStatus err = kNoErr;
  mico_Context_t *Context = inContext;
  fd_set readfds;
  int reConnCount = 0;
  int clientFdIsSet;

  easylink_log_trace();
  require_action(easylink_sem, threadexit, err = kNotPreparedErr);
      
  if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_BYPASS){
    Context->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO;
    MICOUpdateConfiguration(Context);
    _easylinkConnectWiFi_fast(Context);
  }else if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_SOFT_AP_BYPASS){
    ConfigWillStop( Context );
    _easylinkStartSoftAp(Context);
    mico_rtos_delete_thread(NULL);
    return;
  }else{
#ifdef EasyLink_Plus
    easylink_log("Start easylink plus mode");
    micoWlanStartEasyLinkPlus(EasyLink_TimeOut/1000);
#else
    easylink_log("Start easylink V2");
    micoWlanStartEasyLink(EasyLink_TimeOut/1000);
#endif 
    mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER);
    if(EasylinkFailed == false)
      _easylinkConnectWiFi(Context);
    else{
      msleep(20);
      _cleanEasyLinkResource( Context );
      ConfigWillStop( Context );
      _easylinkStartSoftAp(Context);
      mico_rtos_delete_thread(NULL);
      return;
    }
  }

  err = mico_rtos_get_semaphore(&easylink_sem, EasyLink_ConnectWlan_Timeout);
  require_noerr(err, reboot);

  httpHeader = HTTPHeaderCreate();
  require_action( httpHeader, threadexit, err = kNoMemoryErr );
  HTTPHeaderClear( httpHeader );
    
  while(1){
    if(easylinkClient_fd == -1){
      err = _connectFTCServer(inContext, &easylinkClient_fd);
      require_noerr(err, Reconn);
    }else{
      FD_ZERO(&readfds);  
      FD_SET(easylinkClient_fd, &readfds);

      if(httpHeader->len == 0){
        err = select(1, &readfds, NULL, NULL, NULL);
        require(err>=1, threadexit);
        clientFdIsSet = FD_ISSET(easylinkClient_fd, &readfds);
      }
  
      if(clientFdIsSet||httpHeader->len){
        err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader );

        switch ( err )
        {
          case kNoErr:
            // Read the rest of the HTTP body if necessary
            do{
              err = SocketReadHTTPBody( easylinkClient_fd, httpHeader );
              require_noerr(err, Reconn);

              PrintHTTPHeader(httpHeader);
              // Call the HTTPServer owner back with the acquired HTTP header
              err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context );
              require_noerr( err, Reconn );
              if(httpHeader->contentLength == 0)
                break;
            } while( httpHeader->chunkedData == true || httpHeader->dataEndedbyClose == true);
              // Reuse HTTPHeader
              HTTPHeaderClear( httpHeader );
          break;

          case EWOULDBLOCK:
              // NO-OP, keep reading
          break;

          case kNoSpaceErr:
            easylink_log("ERROR: Cannot fit HTTPHeader.");
            goto Reconn;

          case kConnectionErr:
            // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed
            easylink_log("ERROR: Connection closed.");
              /*Roll back to previous settings (if it has) and reboot*/
            if(Context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured ){
              Context->flashContentInRam.micoSystemConfig.configured = allConfigured;
              MICOUpdateConfiguration( Context );
              MicoSystemReboot();
            }else{
              micoWlanPowerOff();
              msleep(20);
            }
            goto threadexit;

          default:
            easylink_log("ERROR: HTTP Header parse internal error: %d", err);
            goto Reconn;
        }
      }
    }
    continue;
Reconn:
    HTTPHeaderClear( httpHeader );
    SocketClose(&easylinkClient_fd);
    easylinkClient_fd = -1;
    require(reConnCount < 6, reboot);
    reConnCount++;
    sleep(5);
  }  

/*Module is ignored by FTC server, */    
threadexit:
  _cleanEasyLinkResource( Context );
  ConfigWillStop( Context );
  mico_rtos_delete_thread( NULL );
  return;
  
/*SSID or Password is not correct, module cannot connect to wlan, so reboot and enter EasyLink again*/
reboot:
  MicoSystemReboot();
  return;
}