int application_start(void)
{
    app_log_trace();
    OSStatus err = kNoErr;
    mico_uart_config_t uart_config;
    app_context_t* app_context;
    mico_Context_t* mico_context;

    /* 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) );
    app_context->appConfig = mico_system_context_get_user_data( mico_context );

    /* mico system initialize */
    err = mico_system_init( mico_context );
    require_noerr( err, exit );

    /* Bonjour for service searching */
    MICOStartBonjourService( Station, app_context );

    /* Protocol initialize */
    sppProtocolInit( app_context );

    /*UART receive thread*/
    uart_config.baud_rate    = app_context->appConfig->USART_BaudRate;
    uart_config.data_width   = DATA_WIDTH_8BIT;
    uart_config.parity       = NO_PARITY;
    uart_config.stop_bits    = STOP_BITS_1;
    uart_config.flow_control = FLOW_CONTROL_DISABLED;
    if(mico_context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true)
        uart_config.flags = UART_WAKEUP_ENABLE;
    else
        uart_config.flags = UART_WAKEUP_DISABLE;
    
    ring_buffer_init  ( (ring_buffer_t *)&rx_buffer, (uint8_t *)rx_data, UART_BUFFER_LENGTH );
    MicoUartInitialize( UART_FOR_APP, &uart_config, (ring_buffer_t *)&rx_buffer );
    err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "UART Recv", uartRecv_thread, STACK_SIZE_UART_RECV_THREAD, (void*)app_context );
    require_noerr_action( err, exit, app_log("ERROR: Unable to start the uart recv thread.") );

    /* Local TCP server thread */
    if(app_context->appConfig->localServerEnable == true)
    {
        err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "Local Server", localTcpServer_thread, STACK_SIZE_LOCAL_TCP_SERVER_THREAD, (void*)app_context );
        require_noerr_action( err, exit, app_log("ERROR: Unable to start the local server thread.") );
    }

    /* Remote TCP client thread */
    if(app_context->appConfig->remoteServerEnable == true)
    {
        err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "Remote Client", remoteTcpClient_thread, STACK_SIZE_REMOTE_TCP_CLIENT_THREAD, (void*)app_context );
        require_noerr_action( err, exit, app_log("ERROR: Unable to start the remote client thread.") );
    }

exit:
    mico_rtos_delete_thread(NULL);
    return err;
}
Esempio n. 2
0
USED void config_server_delegate_report( json_object *app_menu, mico_Context_t *in_context )
{
  config_delegate_log_trace();
  OSStatus err = kNoErr;
  application_config_t *appConfig = mico_system_context_get_user_data(in_context);
  
  // confifg data version number
  err = config_server_create_number_cell(app_menu, "ConfigDataVer", appConfig->configDataVer, "RW", NULL);
  require_noerr(err, exit);
  // bonjourt service port
  err = config_server_create_number_cell(app_menu, "BonjourPort", appConfig->bonjourServicePort, "RW", NULL);
  require_noerr(err, exit);
  // activate status
  err = config_server_create_bool_cell(app_menu, "Activated", appConfig->fogcloudConfig.isActivated, "RO");
  require_noerr(err, exit);
  // owner binding status
  err = config_server_create_bool_cell(app_menu, "OwnerBinding", appConfig->fogcloudConfig.owner_binding, "RO");
  require_noerr(err, exit);
  // loginID
  err = config_server_create_string_cell(app_menu, "loginID", appConfig->fogcloudConfig.loginId, "RW", NULL);
  require_noerr(err, exit);
  // login passwd
  err = config_server_create_string_cell(app_menu, "loginPasswd", appConfig->fogcloudConfig.devPasswd, "RW", NULL);
  require_noerr(err, exit);
  // user_token
  err = config_server_create_string_cell(app_menu, "userToken", appConfig->fogcloudConfig.userToken, "RW", NULL);
  require_noerr(err, exit);
  // rom version
  err = config_server_create_string_cell(app_menu, "RomVersion", appConfig->fogcloudConfig.romVersion, "RW", NULL);
  require_noerr(err, exit);
  // device id
  err = config_server_create_string_cell(app_menu, "deviceID", appConfig->fogcloudConfig.deviceId, "RW", NULL);
  require_noerr(err, exit);
  // device secret
  err = config_server_create_string_cell(app_menu, "deviceSec", appConfig->fogcloudConfig.masterDeviceKey, "RW", NULL);
  require_noerr(err, exit);
  
exit:
  return;
}
Esempio n. 3
0
USED void config_server_delegate_recv( const char *key, json_object *value, bool *need_reboot, mico_Context_t *in_context )
{
  config_delegate_log_trace();
  application_config_t *appConfig = mico_system_context_get_user_data(in_context);

  if(!strcmp(key, "ConfigDataVer")){
    appConfig->configDataVer = json_object_get_int(value);
    *need_reboot = true;
  }else if(!strcmp(key, "BonjourPort")){
    appConfig->bonjourServicePort = json_object_get_int(value);
    *need_reboot = true;
  }else if(!strcmp(key, "loginId")){
    strncpy(appConfig->fogcloudConfig.loginId, json_object_get_string(value), MAX_SIZE_LOGIN_ID);
    *need_reboot = true;
  } else if(!strcmp(key, "loginPasswd")){
    strncpy(appConfig->fogcloudConfig.devPasswd, json_object_get_string(value), MAX_SIZE_DEV_PASSWD);
    *need_reboot = true;
  } else if(!strcmp(key, "userToken")){
    strncpy(appConfig->fogcloudConfig.userToken, json_object_get_string(value), MAX_SIZE_USER_TOKEN);
    *need_reboot = true;
  }else{
  }
}
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;
}
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;
}