int application_start( void )
{
  OSStatus err = kNoErr;
  network_InitTypeDef_adv_st  wNetConfigAdv={0};
 
  MicoInit( );
  
  /* Register user function when wlan connection status is changed */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 
  
  /* Register user function when wlan connection is faile in one attempt */
  err = mico_system_notify_register( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler, NULL );
  require_noerr( err, exit );
  
  /* Initialize wlan parameters */
  strcpy((char*)wNetConfigAdv.ap_info.ssid, "Xiaomi.Router");   /* wlan ssid string */
  strcpy((char*)wNetConfigAdv.key, "stm32f215");                /* wlan key string or hex data in WEP mode */
  wNetConfigAdv.key_len = strlen(ap_key);                       /* wlan key length */
  wNetConfigAdv.ap_info.security = SECURITY_TYPE_AUTO;          /* wlan security mode */
  wNetConfigAdv.ap_info.channel = 0;                            /* Select channel automatically */
  wNetConfigAdv.dhcpMode = DHCP_Client;                         /* Fetch Ip address from DHCP server */
  wNetConfigAdv.wifi_retry_interval = 100;                      /* Retry interval after a failure connection */
  
  /* Connect Now! */
  wifi_station_log("connecting to %s...", wNetConfigAdv.ap_info.ssid);
  micoWlanStartAdv(&wNetConfigAdv);
  
exit:
  mico_rtos_delete_thread(NULL);
  return err;
}
Example #2
0
OSStatus system_notification_init( mico_Context_t * const inContext )
{
  OSStatus err = kNoErr;

  err = mico_system_notify_register( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler, inContext );
  require_noerr( err, exit );

  err = mico_system_notify_register( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler, inContext );
  require_noerr( err, exit ); 

  err = mico_system_notify_register( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler, inContext );
  require_noerr( err, exit );

  err = mico_system_notify_register( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler, inContext );
  require_noerr( err, exit ); 

  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, inContext );
  require_noerr( err, exit );

  err = mico_system_notify_register( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler, inContext );
  require_noerr( err, exit ); 

exit:
  return err;
}
Example #3
0
File: ftp.c Project: maplefish/MICO
//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;
}
Example #4
0
//===================================
static int lwifi_scan( lua_State* L )
{
  OSStatus err = 0;
  int tmo = mico_get_time();
  
  wifi_scanned_print = 0;
  
  if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) {
    lua_pushvalue(L, 1);  // copy argument (func) to the top of stack
    if (wifi_scan_succeed != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
    
    wifi_scan_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  else {
    if (wifi_scan_succeed != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
    wifi_scan_succeed = LUA_NOREF;
    if (lua_type(L, 1) == LUA_TNUMBER) {
      int prn = luaL_checkinteger( L, 1 );
      if (prn == 1) wifi_scanned_print = 1;
    }
  }
  err = mico_system_notify_register( mico_notify_WIFI_SCAN_ADV_COMPLETED, (void *)_micoNotify_WiFi_Scan_OK, NULL );
  require_noerr( err, exit );
  gL = L;
  wifi_scanned = 0;
  micoWlanStartScanAdv();

  tmo = mico_get_time();
  if (wifi_scan_succeed == LUA_NOREF) {
    while (wifi_scanned == 0) {
      if ((mico_get_time() - tmo) > 8000) break;
      mico_thread_msleep(100);
      luaWdgReload();
    }
    if ((wifi_scanned == 1) && (wifi_scanned_print == 0)) {
      return 2;
    }
  }
exit:
  return 0;
}
Example #5
0
int application_start( void )
{
  OSStatus err = kNoErr;
  
  /*Register user function for MiCO nitification: WiFi status changed */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 
  
  /* Start MiCO system functions according to mico_config.h */
  err = mico_system_init( mico_system_context_init( 0 ) );
  require_noerr( err, exit ); 
  
  /* Start TCP server listener thread*/
  err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "TCP_server", tcp_server_thread, 0x800, NULL );
  require_noerr_string( err, exit, "ERROR: Unable to start the tcp server thread." );
  
exit:
  mico_rtos_delete_thread( NULL );
  return err;
}
Example #6
0
int application_start( void )
{
  OSStatus err = kNoErr;

  /*Register user function for MiCO nitification: WiFi status changed */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 

  /* Start MiCO system functions according to mico_config.h */
  err = mico_system_init( mico_system_context_init( 0 ) );
  require_noerr( err, exit ); 

  err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "udp_unicast", udp_unicast_thread, 0x800, NULL );
  require_noerr_string( err, exit, "ERROR: Unable to start the UDP thread." );
  
exit:
  if( err != kNoErr )
    udp_unicast_log("Thread exit with err: %d", err);
  mico_rtos_delete_thread( NULL );
  return err;
}
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;
  char ipstr[16];
  struct timeval_t t;
  int remoteTcpClient_loopBack_fd = -1;
  int remoteTcpClient_fd = -1;
  uint8_t *inDataBuffer = NULL;
  uint8_t *outDataBuffer = NULL;  
  
  mico_rtos_init_semaphore(&_wifiConnected_sem, 1);
  
  /* Regisist notifications */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)clientNotify_WifiStatusHandler, NULL );
  require_noerr( err, exit ); 
  
  inDataBuffer = malloc(wlanBufferLen);
  require_action(inDataBuffer, exit, err = kNoMemoryErr);
  outDataBuffer = malloc(wlanBufferLen);
  require_action(inDataBuffer, exit, err = kNoMemoryErr);
  
  /*Loopback fd, recv data from other thread */
  remoteTcpClient_loopBack_fd = socket( AF_INET, SOCK_DGRM, IPPROTO_UDP );
  require_action(IsValidSocket( remoteTcpClient_loopBack_fd ), exit, err = kNoResourcesErr );
  addr.s_ip = IPADDR_LOOPBACK;
  addr.s_port = REMOTE_TCP_CLIENT_LOOPBACK_PORT;
  err = bind( remoteTcpClient_loopBack_fd, &addr, sizeof(addr) );
  require_noerr( err, exit );
  
  t.tv_sec = 4;
  t.tv_usec = 0;
  
  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);
      
      Context->appStatus.isRemoteConnected = true;
      client_log("Remote server connected at port: %d, fd: %d",  Context->flashContentInRam.appConfig.remoteServerPort,
                 remoteTcpClient_fd);
    }else{
      FD_ZERO(&readfds);
      FD_SET(remoteTcpClient_fd, &readfds);
      FD_SET(remoteTcpClient_loopBack_fd, &readfds);
      
      select(1, &readfds, NULL, NULL, &t);
      
      /*recv UART data using loopback fd*/
      if (FD_ISSET( remoteTcpClient_loopBack_fd, &readfds) ) {
        len = recv( remoteTcpClient_loopBack_fd, outDataBuffer, wlanBufferLen, 0 );
        SocketSend( remoteTcpClient_fd, outDataBuffer, len );
      }
      
      /*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);
          Context->appStatus.isRemoteConnected = false;
          goto ReConnWithDelay;
        }
        sppWlanCommandProcess(inDataBuffer, &len, remoteTcpClient_fd, Context);

      }

    Continue:    
      continue;
      
    ReConnWithDelay:
      if(remoteTcpClient_fd != -1){
        SocketClose(&remoteTcpClient_fd);
      }
      sleep(CLOUD_RETRY);
    }
  }
exit:
  if(inDataBuffer) free(inDataBuffer);
  if(outDataBuffer) free(outDataBuffer);
  if(remoteTcpClient_loopBack_fd != -1)
    SocketClose(&remoteTcpClient_loopBack_fd);
  client_log("Exit: Remote TCP client exit with err = %d", err);
  mico_rtos_delete_thread(NULL);
  return;
}
Example #8
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;
}
Example #9
0
//======================================
static int lwifi_startap( lua_State* L )
{
  //4 stations Max
  network_InitTypeDef_st wNetConfig;
  size_t len=0;
  
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
  
  if (!lua_istable(L, 1)) {
    // ==== Call without parameters, return status ===
    if (wifi_ap_started == 1) lua_pushboolean(L, true);
    else lua_pushboolean(L, false);
    return 1;
  }
  
  //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 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 return luaL_error( L, "arg: pwd needed" );
  
  //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 {
    strcpy(wNetConfig.local_ip_addr, "11.11.11.1");
  }

  //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 {
    strcpy(wNetConfig.net_mask, "255.255.255.0");
  }

  //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 {
    strcpy(wNetConfig.gateway_ip_addr,"11.11.11.1");
  }

  //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 {
    strcpy(wNetConfig.dnsServer_ip_addr, "11.11.11.1");
  }

  //retry_interval
  signed retry_interval = 0;
  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;
  
  //notify
  gL = L;

  if (wifi_status_changed_AP != LUA_NOREF)
    luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP);
  wifi_status_changed_AP = 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
    if (wifi_status_changed_AP != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP);    
      
    wifi_status_changed_AP = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL );

  //start
  wifi_ap_started = 0;
  wNetConfig.dhcpMode = DHCP_Server;  
  wNetConfig.wifi_mode = Soft_AP;
  micoWlanStart(&wNetConfig);  

  int tmo = mico_get_time();
  while (wifi_ap_started == 0) {
    if ((mico_get_time() - tmo) > 1000) break;
    mico_thread_msleep(10);
    luaWdgReload();
  }
  if (wifi_ap_started == 1) lua_pushboolean(L, true);
  else lua_pushboolean(L, false);
  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;
}
Example #11
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 acaUartInterface_thread(void *inContext)
{
  client_log_trace();
  OSStatus err = kUnknownErr;
  arrayent_net_status_t arg;
  arrayent_return_e ret;
  //int8_t retry = 0;
  uint16_t len = 0;
  static char property[150];
  
  mico_rtos_init_semaphore(&_wifiConnected_sem, 1);//初始化信号量
  /* Regisist notifications */
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)notify_WifiStatusHandler, (void *)inContext);//???
  require_noerr( err, exit );
  while(_wifiConnected == false){//after few time connected wifi success.???
    mico_rtos_get_semaphore(&_wifiConnected_sem, 200000);
    client_log("Waiting for Wi-Fi network...");
    mico_thread_sleep(1);
  }
  user_modules_init();
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, "Arrayent        ");
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, "  Connecting... ");
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, "                ");
  configureArrayent(inContext);
  
  //Initialize Arrayent stack
  client_log("Initializing Arrayent daemon...");
  ret = ArrayentInit();
  if(ret != ARRAYENT_SUCCESS) {
    client_log("Failed to initialize Arrayent daemon.%d", ret);
    return;
  }
  client_log("Arrayent daemon successfully initialized!");
  //Wait for Arrayent to finish connecting to the server
  do {
    ret = ArrayentNetStatus(&arg);//get connecting status to server
    if(ret != ARRAYENT_SUCCESS) {
      client_log("Failed to connect to Arrayent network.");
    }
    client_log("Waiting for good network status.");
    mico_thread_sleep(1);
  } while(!(arg.connected_to_server));//after few time connected server success.
  
  client_log("Connected to Arrayent network!\r\n");
  _connection_lost = false;
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, "Connected       ");
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, "    Arrayent!   ");
  OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, "                ");
  
  ArrayentSetProperty("DC","0");
  mico_thread_msleep(200);
  ArrayentSetProperty("RGB","off");
  
  err = mico_rtos_create_thread(NULL, MICO_DEFAULT_LIBRARY_PRIORITY, "update", update_property, STACK_SIZE_UART_RECV_THREAD, (void*)inContext);
  require_noerr_action( err, exit, client_log("ERROR: Unable to start the update_property thread.") );
  err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "except", except_handle, STACK_SIZE_UART_RECV_THREAD, (void*)inContext);
  require_noerr_action( err, exit, client_log("ERROR: Unable to start the except_handle thread.") );
  while(1) {
    //sure wifi connected.
    if(_wifiConnected == false){
      mico_rtos_get_semaphore(&_wifiConnected_sem, 200000);
      continue;
    }
    //sure server connected.
    if(_connection_lost && _wifiConnected){
      do {
        ret = ArrayentNetStatus(&arg);//get connecting status to server
        if(ret != ARRAYENT_SUCCESS) {
          client_log("Failed to connect to Arrayent network.");
        }
        client_log("Waiting for good network status.");
        mico_thread_sleep(1);
      } while(!(arg.connected_to_server));
      _connection_lost = false;
    }
    
    len = sizeof(property);
    if(ARRAYENT_SUCCESS==ArrayentRecvProperty(property,&len,10000)){//receive property data from server
      client_log("property = %s\n\r",property);
      if(0!=checkCmd(property, inContext)){//parse cmd success
        //_response = false;
        //retry = 1;
        client_log("%s", property);
      }
    }
  }
  
exit:
  client_log("Exit: ACA App exit with err = %d", err);
  mico_rtos_delete_thread(NULL);
  return;
}
Example #13
0
OSStatus MiCOStartSiteWhereService(app_context_t* const inContext)
{
  sitewhere_log_trace();
  OSStatus err = kUnknownErr;
  char *bonjour_txt_record = NULL;
  char *bonjour_txt_field = NULL;
  
  // update wifi status
  err = mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)swNotify_WifiStatusHandler, inContext);
  require_noerr_action(err, exit, 
                       sitewhere_log("ERROR: mico_system_notify_register (mico_notify_WIFI_STATUS_CHANGED) failed!") );
  
  // create uinque hardeareID like: MODEL-HARDWARE_REVISION-MAC
  memset(hardwareId, '\0', sizeof(hardwareId));
  sprintf(hardwareId, "Enjoy_%s-%s-%c%c%c%c%c%c",MODEL, HARDWARE_REVISION, 
          mico_system_context_get()->micoStatus.mac[9],
          mico_system_context_get()->micoStatus.mac[10],
          mico_system_context_get()->micoStatus.mac[12],
          mico_system_context_get()->micoStatus.mac[13],
          mico_system_context_get()->micoStatus.mac[15],
          mico_system_context_get()->micoStatus.mac[16]
            );
  sitewhere_log("hardwareId=[%s]", hardwareId);
  // create system topic like: SiteWhere/commands/<hardwareid>
  memset(System1, '\0', sizeof(System1));
  sprintf(System1, "%s/%s", "SiteWhere/system", hardwareId);
  sitewhere_log("System1=[%s]", System1);
  // create command topic like: SiteWhere/<system/hardwareid>
  memset(Command1, '\0', sizeof(Command1));
  sprintf(Command1, "%s/%s", "SiteWhere/commands", hardwareId);
  sitewhere_log("Command1=[%s]", Command1);
  // specificationToken
  sitewhere_log("specificationToken=[%s]", specificationToken);
  
  //------------------------------------------------------------------------
  // update hardwareid in mdns text record
  sitewhere_log("update bonjour txt record.");
  bonjour_txt_record = malloc(550);
  require_action(bonjour_txt_record, exit, err = kNoMemoryErr);
  
  bonjour_txt_field = __strdup_trans_dot(inContext->mico_context->micoStatus.mac);
  sprintf(bonjour_txt_record, "MAC=%s.", bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(hardwareId);
  sprintf(bonjour_txt_record, "%sHardware ID=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(FIRMWARE_REVISION);
  sprintf(bonjour_txt_record, "%sFirmware Rev=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(HARDWARE_REVISION);
  sprintf(bonjour_txt_record, "%sHardware Rev=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(MicoGetVer());
  sprintf(bonjour_txt_record, "%sMICO OS Rev=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(MODEL);
  sprintf(bonjour_txt_record, "%sModel=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(PROTOCOL);
  sprintf(bonjour_txt_record, "%sProtocol=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  bonjour_txt_field = __strdup_trans_dot(MANUFACTURER);
  sprintf(bonjour_txt_record, "%sManufacturer=%s.", bonjour_txt_record, bonjour_txt_field);
  free(bonjour_txt_field);
  
  sprintf(bonjour_txt_record, "%sSeed=%u.", bonjour_txt_record, inContext->mico_context->flashContentInRam.micoSystemConfig.seed);
  
  mdns_update_txt_record(BONJOUR_SERVICE, Station, bonjour_txt_record);
  if(NULL != bonjour_txt_record) free(bonjour_txt_record);
  //------------------------------------------------------------------------
  
  err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "sitewhere", 
                                sitewhere_main_thread, STACK_SIZE_SITEWHERE_MAIN_THREAD, 
                                inContext );
  require_noerr_action( err, exit, sitewhere_log("ERROR: Unable to start sitewhere thread.") );
  
  //err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "MQTT Client", 
  //                              mqtt_loop_thread, 0xC00, (void*)inContext->mico_context );
  //require_noerr_action( err, exit, sitewhere_log("ERROR: Unable to start the MQTT client thread.") );
  
exit:
  return err;
}