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!") );
// 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(1);
}
}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;
}
|