Esempio n. 1
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;
}
Esempio n. 2
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;
}