Exemple #1
0
//==============================================
static int lwifi_station_getlink (lua_State* L )
{
  LinkStatusTypeDef link;
  memset(&link,0x00,sizeof(link));
  micoWlanGetLinkStatus(&link);
  
  if (link.is_connected==0) {
    lua_pushstring(L, "disconnected");
    lua_pushnil(L);
    lua_pushnil(L);
    lua_pushnil(L);
  }
  else {
    lua_pushstring(L, "connected");
    lua_pushinteger(L, link.wifi_strength);
    lua_pushstring(L, (char*)link.ssid);

    char temp[20]={0};
    char temp2[4];
    temp[0]=0x00;
    for (int j = 0; j < 6; j++) {
      if ( j == 6 - 1 ) { 
        sprintf(temp2,"%02X",link.bssid[j]);
        strcat(temp,temp2);
      }
      else {
        sprintf(temp2,"%02X:",link.bssid[j]);
        strcat(temp,temp2);
      }
    }    
    lua_pushstring(L,temp);
  }
  return 4;
}
/* MICO APP entrance */
OSStatus MICOStartApplication( mico_Context_t * const mico_context )
{
  app_log_trace();
  OSStatus err = kNoErr;
  LinkStatusTypeDef wifi_link_status;
    
  require_action(mico_context, exit, err = kParamErr);
    
  app_log("Application version: %s", mico_context->flashContentInRam.appConfig.fogcloudConfig.romVersion);
  
  // LED on when Wi-Fi connected.
  MicoSysLed(false);
  
  // init application wifi link status
  do{
    err = micoWlanGetLinkStatus(&wifi_link_status);
    if(kNoErr != err){
      mico_thread_sleep(3);
    }
  }while(kNoErr != err);
  
  if(1 ==  wifi_link_status.is_connected){
    mico_context->appStatus.isWifiConnected = true;
  }
  else{
    mico_context->appStatus.isWifiConnected = false;
  }
    
  /* Bonjour for service searching */
  if(mico_context->flashContentInRam.micoSystemConfig.bonjourEnable == true) {
    MICOStartBonjourService( Station, mico_context );
  }
  
  /* start cloud service */
#if (MICO_CLOUD_TYPE == CLOUD_FOGCLOUD)
  app_log("MICO CloudService: FogCloud.");
  err = MicoStartFogCloudService( mico_context );
  require_noerr_action( err, exit, app_log("ERROR: Unable to start FogCloud service.") );
#elif (MICO_CLOUD_TYPE == CLOUD_ALINK)
  app_log("MICO CloudService: Alink.");
#elif (MICO_CLOUD_TYPE == CLOUD_DISABLED)
  app_log("MICO CloudService: disabled.");
#else
  #error "MICO cloud service type is not defined"?
#endif
  
  /* start user thread */
  err = startUserMainThread( mico_context );
  require_noerr_action( err, exit, app_log("ERROR: start user_main thread failed!") );

exit:
  return err;
}
Exemple #3
0
//stat = ftp.start()
//===================================
static int lftp_start( lua_State* L )
{
  LinkStatusTypeDef wifi_link;
  int err = micoWlanGetLinkStatus( &wifi_link );

  if ( wifi_link.is_connected == false ) {
    ftp_log("[FTP usr] WiFi NOT CONNECTED!\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  
  if ( (gL == NULL) || (ftpCmdSocket == NULL) ) {
    ftp_log("[FTP usr] Execute ftp.new first!\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }
  if (ftp_thread_is_started) {
    ftp_log("[FTP usr] Already started!\r\n" );
    lua_pushinteger(L, -3);
    return 1;
  }
  
  mico_system_notify_register( mico_notify_TCP_CLIENT_CONNECTED, (void *)_micoNotify_FTPClientConnectedHandler, NULL );

  // all setup, start the ftp thread
  if (!ftp_thread_is_started) {
    if (mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY-1, "Ftp_Thread", _thread_ftp, 1024, NULL) != kNoErr) {
      _ftp_deinit(0);    
      ftp_log("[FTP usr] Create thread failed\r\n" );
      lua_pushinteger(L, -4);
      return 1;
    }
    else ftp_thread_is_started = true;
  } 

  if (ftpCmdSocket->logon_cb != LUA_NOREF) {
    lua_pushinteger(L, 0);
    return 1;
  }
  
  // wait max 10 sec for login
  uint32_t tmo = mico_get_time();
  while ( (ftp_thread_is_started) && !(status & FTP_LOGGED) ) {
    if ((mico_get_time() - tmo) > 10000) break;
    mico_thread_msleep(100);
    luaWdgReload();
  }
  if (!(status & FTP_LOGGED)) lua_pushinteger(L, -4);
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #4
0
//=======================================
static int lwifi_startsta( lua_State* L )
{
  LinkStatusTypeDef link;
  network_InitTypeDef_st wNetConfig;
  size_t len=0;
  lua_system_param_t lua_system_param;
  uint8_t has_default = 0;
  signed retry_interval = 0;
  int con_wait = 0;
  int tmo = mico_get_time();
  
  // check if wifi is already connected  
  memset(&link, 0x00, sizeof(link));
  micoWlanGetLinkStatus(&link);

  if (!lua_istable(L, 1)) {
    // ==== Call without parameters, return connection status ===
    if (link.is_connected != 0) lua_pushboolean(L, true);
    else lua_pushboolean(L, false);
    return 1;
  }

  // ==== parameters exists, configure and start ====
  if (wifi_sta_started == 1) _stopWifiSta();
  
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
  // check if default params exists
  if (getLua_systemParams(&lua_system_param) == 1) {
    if ((strlen(lua_system_param.wifi_ssid) > 0) && (strlen(lua_system_param.wifi_key) > 0)) {
      has_default = 1;
    }
  }
  
  //wait for connection
  lua_getfield(L, 1, "wait");
  if (!lua_isnil(L, -1)) {
      int wfc = luaL_checkinteger( L, -1 );
      if ((wfc > 0) && (wfc < 16)) con_wait = wfc * 1000;
      else return luaL_error( L, "wait must be 1 ~ 15");
  }

  //ssid  
  lua_getfield(L, 1, "ssid");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *ssid = luaL_checklstring( L, -1, &len );
      if (len >= 32) return luaL_error( L, "ssid: <32" );
      strncpy(wNetConfig.wifi_ssid,ssid,len);
    } 
    else return luaL_error( L, "wrong arg type:ssid" );
  }
  else if (has_default == 1) {
    strcpy(wNetConfig.wifi_ssid, lua_system_param.wifi_ssid);
  }
  else return luaL_error( L, "arg: ssid needed" );
  
  //pwd  
  lua_getfield(L, 1, "pwd");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *pwd = luaL_checklstring( L, -1, &len );
      if (len >= 64) return luaL_error( L, "pwd: <64" );
      if (len > 0) strncpy(wNetConfig.wifi_key,pwd,len);
      else strcpy(wNetConfig.wifi_key,"");  
    } 
    else return luaL_error( L, "wrong arg type: pwd" );
  }
  else if (has_default == 1) {
    strcpy(wNetConfig.wifi_key, lua_system_param.wifi_key);
  }
  else return luaL_error( L, "arg: pwd needed" );

  //dhcp
  wNetConfig.dhcpMode = DHCP_Client;
  lua_getfield(L, 1, "dhcp");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *pwd = luaL_checklstring( L, -1, &len );
      if (strcmp(pwd, "disable") == 0) wNetConfig.dhcpMode = DHCP_Disable;
    }   
    else return luaL_error( L, "wrong arg type: dhcp" );
  }

  //ip
  lua_getfield(L, 1, "ip");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *ip = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "ip: <16" );
      if (is_valid_ip(ip) == false) return luaL_error( L, "ip invalid" );
      strncpy(wNetConfig.local_ip_addr,ip,len);
    } 
    else return luaL_error( L, "wrong arg type:ip" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: ip needed" );

  //netmask  
  lua_getfield(L, 1, "netmask");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *netmask = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "netmask: <16" );
      if (is_valid_ip(netmask) == false) return luaL_error( L, "netmask invalid" );
      strncpy(wNetConfig.net_mask,netmask,len);
    } 
    else return luaL_error( L, "wrong arg type: netmask" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: netmask needed" );
  
  //gateway 
  lua_getfield(L, 1, "gateway");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *gateway = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "gateway: <16" );
      if (is_valid_ip(gateway) == false) return luaL_error( L, "gateway invalid" );
      strncpy(wNetConfig.gateway_ip_addr,gateway,len);
    } 
    else return luaL_error( L, "wrong arg type: gateway" );
  }
  else if(wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: gateway needed" );
  
  //dnsSrv
  lua_getfield(L, 1, "dnsSrv");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *dnsSrv = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "dnsSrv: <16" );
      if (is_valid_ip(dnsSrv) == false) return luaL_error( L, "dnsSrv invalid" );
      strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len);
    } 
    else return luaL_error( L, "wrong arg type: dnsSrv" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: dnsSrv needed" );
  
  //retry_interval
  lua_getfield(L, 1, "retry_interval");
  if (!lua_isnil(L, -1)) {
      retry_interval = (signed)luaL_checknumber( L, -1 );
      if (retry_interval < 0) return luaL_error( L, "retry_interval: >=0ms" );
      if (retry_interval == 0) retry_interval = 0x7FFFFFFF;
  }
  else retry_interval = 1000; 
  wNetConfig.wifi_retry_interval = retry_interval;
  
  gL = L;
  //notify, set CB function for wifi state change
  if (wifi_status_changed_STA != LUA_NOREF)
    luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_STA);
  wifi_status_changed_STA = LUA_NOREF;

  if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) {
    lua_pushvalue(L, 2);  // copy argument (func) to the top of stack
      
    wifi_status_changed_STA = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL );
  
  //start  
  wNetConfig.wifi_mode = Station;
  micoWlanStart(&wNetConfig);
  wifi_sta_started = 1;

  if (con_wait == 0) {
    lua_pushboolean(L, false);
    return 1;
  }

  tmo = mico_get_time();
  micoWlanGetLinkStatus(&link);
  while (link.is_connected == 0) {
    if ((mico_get_time() - tmo) > con_wait) break;
    mico_thread_msleep(50);
    luaWdgReload();
    micoWlanGetLinkStatus(&link);
  }

  if (link.is_connected == 0) lua_pushboolean(L, false);
  else lua_pushboolean(L, true);

  return 1;
}
int application_start(void)
{
  app_log_trace();
  OSStatus err = kNoErr;
  app_context_t* app_context;
  mico_Context_t* mico_context;
  LinkStatusTypeDef wifi_link_status;

  /* Create application context */
  app_context = ( app_context_t *)calloc(1, sizeof(app_context_t) );
  require_action( app_context, exit, err = kNoMemoryErr );

  /* Create mico system context and read application's config data from flash */
  mico_context = mico_system_context_init( sizeof( application_config_t) );
  require_action(mico_context, exit, err = kNoResourcesErr);
  app_context->appConfig = mico_system_context_get_user_data( mico_context );
  app_context->mico_context = mico_context;
  app_context_global = app_context;
  
  /* user params restore check */
  if(app_context->appConfig->configDataVer != CONFIGURATION_VERSION){
    err = mico_system_context_restore(mico_context);
    require_noerr( err, exit );
  }

  /* mico system initialize */
  err = mico_system_init( mico_context );
  require_noerr( err, exit );
  MicoSysLed(true);
  
  // fix for AP down problem
  err = mico_system_notify_register( mico_notify_WIFI_CONNECT_FAILED,
                                    (void *)appNotify_ConnectFailedHandler, app_context->mico_context );
  require_noerr_action(err, exit, 
                       app_log("ERROR: MICOAddNotification (mico_notify_WIFI_CONNECT_FAILED) failed!") );
  
//#ifdef USE_MiCOKit_EXT
//  /* user test mode to test MiCOKit-EXT board */
//  if(MicoExtShouldEnterTestMode()){
//    app_log("Enter ext-board test mode by key2.");
//    micokit_ext_mfg_test(mico_context);
//  }
//#endif
  
  // block here if no wifi configuration.
  while(1){
    if( mico_context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured ||
       mico_context->flashContentInRam.micoSystemConfig.configured == unConfigured){
         mico_thread_msleep(100);
       }
    else{
      break;
    }
  }

  /* Bonjour for service searching */
  MICOStartBonjourService( Station, app_context );
    
  /* check wifi link status */
  do{
    err = micoWlanGetLinkStatus(&wifi_link_status);
    if(kNoErr != err){
      mico_thread_sleep(3);
    }
  }while(kNoErr != err);
  
  if(1 ==  wifi_link_status.is_connected){
    app_context->appStatus.isWifiConnected = true;
    MicoRfLed(true);
  }
  else{
    app_context->appStatus.isWifiConnected = false;
    MicoRfLed(false);
  }
  
  /* start cloud service */
#if (MICO_CLOUD_TYPE == CLOUD_FOGCLOUD)
  app_log("MICO CloudService: FogCloud.");
  err = MiCOStartFogCloudService( app_context );
  require_noerr_action( err, exit, app_log("ERROR: Unable to start FogCloud service.") );
#elif (MICO_CLOUD_TYPE == CLOUD_ALINK)
  app_log("MICO CloudService: Alink.");
#elif (MICO_CLOUD_TYPE == CLOUD_DISABLED)
  app_log("MICO CloudService: disabled.");
#else
  #error "MICO cloud service type is not defined"?
#endif
  
  /* start user thread */
  err = startUserMainThread( app_context );
  require_noerr_action( err, exit, app_log("ERROR: start user_main thread failed!") );

exit:
  mico_rtos_delete_thread(NULL);
  return err;
}
Exemple #6
0
void NTPClient_thread(void *arg)
{
  ntp_log_trace();
  OSStatus err = kUnknownErr;
  UNUSED_PARAMETER( arg );
  
  int  Ntp_fd = -1;
  fd_set readfds;
  struct timeval_t t ;
  struct sockaddr_t addr;
  socklen_t addrLen;	
  char ipstr[16];
  unsigned int trans_sec, current;
  struct NtpPacket outpacket ,inpacket;
  struct tm *currentTime;
  mico_rtc_time_t time;
  LinkStatusTypeDef wifi_link;
  int contry = 0;
  
  /* Regisist notifications */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 
 
  memset(&outpacket,0x0,sizeof(outpacket));
  memset(&inpacket,0x0,sizeof(inpacket));

  outpacket.flags = NTP_Flags;
  outpacket.stratum = NTP_Stratum;
  outpacket.poll = NTP_Poll;
  outpacket.precision = NTP_Precision;
  outpacket.root_delay = NTP_Root_Delay;
  outpacket.root_dispersion = NTP_Root_Dispersion;
  
  err = micoWlanGetLinkStatus( &wifi_link );
  require_noerr( err, exit );

  if( wifi_link.is_connected == true )
    _wifiConnected = true;
  
  if(_wifiConnected == false)
    mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER);
  
  Ntp_fd = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
  require_action(IsValidSocket( Ntp_fd ), exit, err = kNoResourcesErr );
  addr.s_ip = INADDR_ANY; 
  addr.s_port = 45000;
  err = bind(Ntp_fd, &addr, sizeof(addr));
  err = kNoErr;
  require_noerr(err, exit);

   contry=0;
   while(1) {
     //err = gethostbyname((char *)NTP_Server, (uint8_t *)ipstr, 16);
     err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16);
     contry+=1;
     if (contry > 4) { require_noerr(err, exit); }
     else { require_noerr(err, ReConnWithDelay); }
     ntp_log("NTP server address: %s",ipstr);
     break;

   ReConnWithDelay:
     mico_thread_sleep(5);
   }

  addr.s_ip = inet_addr(ipstr);
  addr.s_port = NTP_Port;

  t.tv_sec = 5;
  t.tv_usec = 0;
  
  while(1) {
    require_action(sendto(Ntp_fd, &outpacket,sizeof(outpacket), 0, &addr, sizeof(addr)), exit, err = kNotWritableErr);

    FD_ZERO(&readfds);
    FD_SET(Ntp_fd, &readfds);

    select(1, &readfds, NULL, NULL, &t);
    
    if(FD_ISSET(Ntp_fd, &readfds))
    {
      require_action(recvfrom(Ntp_fd, &inpacket, sizeof(struct NtpPacket), 0, &addr, &addrLen)>=0, exit, err = kNotReadableErr);
      trans_sec = inpacket.trans_ts_sec;
      trans_sec = ntohl(trans_sec);
      current = trans_sec - UNIX_OFFSET + (ntp_time_zone*3600);
      ntp_log("Time Synchronised, %s, tz=%d, from %s\n\r",asctime(gmtime(&current)),ntp_time_zone, NTP_Server);

      //currentTime = localtime(&current);
      currentTime = gmtime(&current);
      time.sec = currentTime->tm_sec;
      time.min = currentTime->tm_min ;
      time.hr = currentTime->tm_hour;

      time.date = currentTime->tm_mday;
      time.weekday = currentTime->tm_wday;
      time.month = currentTime->tm_mon + 1;
      time.year = (currentTime->tm_year + 1900)%100;
      MicoRtcSetTime( &time );
      goto exit;
    }
  }
exit:
    if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err);
    mico_system_notify_remove( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler );
    if(_wifiConnected_sem) mico_rtos_deinit_semaphore(&_wifiConnected_sem);
    SocketClose(&Ntp_fd);
    mico_rtos_delete_thread(NULL);
    return;
}
void remoteTcpClient_thread(void *inContext)
{
  client_log_trace();
  OSStatus err = kUnknownErr;
  int len;
  mico_Context_t *Context = inContext;
  struct sockaddr_t addr;
  fd_set readfds;
  fd_set writeSet;
  char ipstr[16];
  struct timeval_t t;
  int remoteTcpClient_fd = -1;
  uint8_t *inDataBuffer = NULL;
  int eventFd = -1;
  mico_queue_t queue;
  socket_msg_t *msg;
  LinkStatusTypeDef wifi_link;
  int sent_len, errno;
  
  mico_rtos_init_semaphore(&_wifiConnected_sem, 1);
  
  /* Regisist notifications */
  err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)clientNotify_WifiStatusHandler );
  require_noerr( err, exit ); 
  
  inDataBuffer = malloc(wlanBufferLen);
  require_action(inDataBuffer, exit, err = kNoMemoryErr);
  
  err = micoWlanGetLinkStatus( &wifi_link );
  require_noerr( err, exit );
  
  if( wifi_link.is_connected == true )
    _wifiConnected = true;
  
  
  while(1) {
    if(remoteTcpClient_fd == -1 ) {
      if(_wifiConnected == false){
        require_action_quiet(mico_rtos_get_semaphore(&_wifiConnected_sem, 200000) == kNoErr, Continue, err = kTimeoutErr);
      }
      err = gethostbyname((char *)Context->flashContentInRam.appConfig.remoteServerDomain, (uint8_t *)ipstr, 16);
      require_noerr(err, ReConnWithDelay);
      
      remoteTcpClient_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      addr.s_ip = inet_addr(ipstr); 
      addr.s_port = Context->flashContentInRam.appConfig.remoteServerPort;
      
      err = connect(remoteTcpClient_fd, &addr, sizeof(addr));
      require_noerr_quiet(err, ReConnWithDelay);
      client_log("Remote server connected at port: %d, fd: %d",  Context->flashContentInRam.appConfig.remoteServerPort,
                 remoteTcpClient_fd);
      
      err = socket_queue_create(Context, &queue);
      require_noerr( err, exit );
      eventFd = mico_create_event_fd(queue);
      if (eventFd < 0) {
        client_log("create event fd error");
        socket_queue_delete(Context, &queue);
        goto ReConnWithDelay;
      }
    }else{
      FD_ZERO(&readfds);
      FD_SET(remoteTcpClient_fd, &readfds);
      FD_SET(eventFd, &readfds); 
      FD_ZERO(&writeSet );
      FD_SET(remoteTcpClient_fd, &writeSet );
      t.tv_sec = 4;
      t.tv_usec = 0;
      select(1, &readfds, &writeSet, NULL, &t);
      /* send UART data */
      if ((FD_ISSET( eventFd, &readfds )) && (FD_ISSET(remoteTcpClient_fd, &writeSet ))) {// have data and can write
        if (kNoErr == mico_rtos_pop_from_queue( &queue, &msg, 0)) {
           sent_len = write(remoteTcpClient_fd, msg->data, msg->len);
           if (sent_len <= 0) {
            len = sizeof(errno);
            getsockopt(remoteTcpClient_fd, SOL_SOCKET, SO_ERROR, &errno, &len);
      
            socket_msg_free(msg);
            if (errno != ENOMEM) {
                client_log("write error, fd: %d, errno %d", remoteTcpClient_fd,errno );
                goto ReConnWithDelay;
            }
           } else {
                    socket_msg_free(msg);
                }
            }
      }
      /*recv wlan data using remote client fd*/
      if (FD_ISSET(remoteTcpClient_fd, &readfds)) {
        len = recv(remoteTcpClient_fd, inDataBuffer, wlanBufferLen, 0);
        if(len <= 0) {
          client_log("Remote client closed, fd: %d", remoteTcpClient_fd);
          goto ReConnWithDelay;
        }
        sppWlanCommandProcess(inDataBuffer, &len, remoteTcpClient_fd, Context);
      }

    Continue:    
      continue;
      
    ReConnWithDelay:
        if (eventFd >= 0) {
          mico_delete_event_fd(eventFd);
          eventFd = -1;
          socket_queue_delete(Context, &queue);
        }
        if(remoteTcpClient_fd != -1){
          SocketClose(&remoteTcpClient_fd);
        }
        sleep(CLOUD_RETRY);
    }
  }
    
exit:
  if(inDataBuffer) free(inDataBuffer);
  client_log("Exit: Remote TCP client exit with err = %d", err);
  mico_rtos_delete_thread(NULL);
  return;
}
int application_start(void)
{
  app_log_trace();
  OSStatus err = kNoErr;
  app_context_t* app_context;
  mico_Context_t* mico_context;
  LinkStatusTypeDef wifi_link_status;

  /* Create application context */
  app_context = ( app_context_t *)calloc(1, sizeof(app_context_t) );
  require_action( app_context, exit, err = kNoMemoryErr );

  /* Create mico system context and read application's config data from flash */
  mico_context = mico_system_context_init( sizeof( application_config_t) );
  require_action(mico_context, exit, err = kNoResourcesErr);
  app_context->appConfig = mico_system_context_get_user_data( mico_context );
  app_context->mico_context = mico_context;
  
  /* user params restore check */
  if(app_context->appConfig->configDataVer != CONFIGURATION_VERSION){
    err = mico_system_context_restore(mico_context);
    require_noerr( err, exit );
  }

  /* mico system initialize */
  err = mico_system_init( mico_context );
  require_noerr( err, exit );
  MicoSysLed(true);
	
  /* Bonjour for service searching */
  MICOStartBonjourService( Station, app_context );
  
  /* user test mode: MiCOKit-EXT */
  
  /* check wifi link status */
  do{
    err = micoWlanGetLinkStatus(&wifi_link_status);
    if(kNoErr != err){
      mico_thread_sleep(3);
    }
  }while(kNoErr != err);
  
  if(1 ==  wifi_link_status.is_connected){
    app_context->appStatus.isWifiConnected = true;
    MicoRfLed(true);
  }
  else{
    app_context->appStatus.isWifiConnected = false;
    MicoRfLed(false);
  }
  
  /* start cloud service */
#if (MICO_CLOUD_TYPE == CLOUD_FOGCLOUD)
  app_log("MICO CloudService: FogCloud.");
  err = MiCOStartFogCloudService( app_context );
  require_noerr_action( err, exit, app_log("ERROR: Unable to start FogCloud service.") );
#elif (MICO_CLOUD_TYPE == CLOUD_ALINK)
  app_log("MICO CloudService: Alink.");
#elif (MICO_CLOUD_TYPE == CLOUD_SITEWHERE)
  app_log("MICO CloudService: SiteWhere.");
  err = MiCOStartSiteWhereService( app_context );
  require_noerr_action( err, exit, app_log("ERROR: Unable to start SiteWhere service.") );
#elif (MICO_CLOUD_TYPE == CLOUD_DISABLED)
  app_log("MICO CloudService: disabled.");
#else
  #error "MICO cloud service type is not defined"?
#endif
  
  /* start user thread */
  err = startUserMainThread( app_context );
  require_noerr_action( err, exit, app_log("ERROR: start user_main thread failed!") );

exit:
  mico_rtos_delete_thread(NULL);
  return err;
}