int application_start( void ) { OSStatus err = kNoErr; IPStatusTypedef para; MicoInit( ); /*The notification message for the registered WiFi status change*/ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = mico_rtos_init_semaphore(&tcp_sem, 1); require_noerr( err, exit ); connect_ap( ); mico_rtos_get_semaphore(&tcp_sem, MICO_WAIT_FOREVER); micoWlanGetIPStatus(¶, Station); tcp_server_log("tcp server ip: %s", para.ip); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "TCP_server", tcp_server_thread, 0x800, NULL ); require_noerr_action( err, exit, tcp_server_log("ERROR: Unable to start the tcp server thread.") ); return err; exit: tcp_server_log("ERROR, err: %d", err); return err; }
int application_start( void ) { OSStatus err = kNoErr; is_easylink_success = 0; MicoInit( ); /*The notification message for the registered WiFi status change*/ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_EASYLINK_WPS_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); require_noerr(err, exit); // Start the EasyLink thread mico_rtos_init_semaphore(&easylink_sem, 1); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x800, NULL ); require_noerr_action( err, exit, wifi_easylink_log("ERROR: Unable to start the EasyLink thread.") ); return err; exit: wifi_easylink_log("ERROR, err: %d", err); return err; }
OSStatus startEasyLinkSoftAP( mico_Context_t * const inContext) { easylink_uap_log_trace(); OSStatus err = kUnknownErr; network_InitTypeDef_st wNetConfig; err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)_EasyLinkNotify_WifiStatusHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)_EasyLinkNotify_WiFIParaChangedHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)_EasyLinkNotify_DHCPCompleteHandler ); require_noerr( err, exit ); // Start the EasyLink thread ConfigWillStart(inContext); if(inContext->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_BYPASS_NO){ memset(&wNetConfig, 0, sizeof(network_InitTypeDef_st)); wNetConfig.wifi_mode = Soft_AP; snprintf(wNetConfig.wifi_ssid, 32, "EasyLink_%c%c%c%c%c%c", inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); strcpy((char*)wNetConfig.wifi_key, ""); strcpy((char*)wNetConfig.local_ip_addr, "10.10.10.1"); strcpy((char*)wNetConfig.net_mask, "255.255.255.0"); strcpy((char*)wNetConfig.gateway_ip_addr, "10.10.10.1"); wNetConfig.dhcpMode = DHCP_Server; micoWlanStart(&wNetConfig); easylink_uap_log("Establish soft ap: %s.....", wNetConfig.wifi_ssid); err = _initBonjourForEasyLink( Soft_AP , inContext ); require_noerr(err, exit); }else{ err = _initBonjourForEasyLink( Station , inContext ); _easylinkConnectWiFi_fast(inContext); require_noerr(err, exit); } start_bonjour_service(); _bonjourStarted = true; err = MICOStartConfigServer ( inContext ); require_noerr(err, exit); ConfigSoftApWillStart( inContext ); exit: return err; }
OSStatus MVDInit(mico_Context_t* const inContext) { OSStatus err = kUnknownErr; //init MVD status inContext->appStatus.virtualDevStatus.isCloudConnected = false; inContext->appStatus.virtualDevStatus.RecvRomFileSize = 0; //init MCU connect interface //err = MVDDevInterfaceInit(inContext); //require_noerr_action(err, exit, // mvd_log("ERROR: virtual device mcu interface init failed!") ); //init cloud service interface err = MVDCloudInterfaceInit(inContext); require_noerr_action(err, exit, mvd_log("ERROR: virtual device cloud interface init failed!") ); // wifi notify err = mico_rtos_init_semaphore(&_wifi_station_on_sem, 1); require_noerr_action(err, exit, mvd_log("ERROR: mico_rtos_init_semaphore (_wifi_station_on_sem) failed!") ); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)mvdNotify_WifiStatusHandler ); require_noerr_action(err, exit, mvd_log("ERROR: MICOAddNotification (mico_notify_WIFI_STATUS_CHANGED) failed!") ); // start MVD main thread err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "MVD main", MVDMainThread, STACK_SIZE_MVD_MAIN_THREAD, inContext ); exit: return err; }
int application_start( void ) { MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); network_InitTypeDef_adv_st wNetConfig; #if MCU_POWERSAVE_ENABLED MicoMcuPowerSaveConfig(true); #endif power_log( "Power measure program: RTOS and wlan initialized and connect wlan, wait station up to measure" ); MicoInit( ); #if IEEE_POWERSAVE_ENABLED micoWlanEnablePowerSave(); #endif memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_adv_st)); strncpy((char*)wNetConfig.ap_info.ssid, "William Xu", 32); wNetConfig.ap_info.security = SECURITY_TYPE_AUTO; strncpy((char*)wNetConfig.key, "mx099555", 64); wNetConfig.key_len = 8; wNetConfig.dhcpMode = true; wNetConfig.wifi_retry_interval = 100; micoWlanStartAdv(&wNetConfig); power_log("connect to %s.....", wNetConfig.ap_info.ssid); mico_rtos_delete_thread( NULL ); return 0; }
int application_start( void ) { OSStatus err = kNoErr; MicoInit( ); /*The notification message for the registered WiFi status change*/ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); strcpy((char*)wNetConfig.wifi_ssid, ap_ssid); strcpy((char*)wNetConfig.wifi_key, ap_key); wNetConfig.wifi_mode = Soft_AP; wNetConfig.dhcpMode = DHCP_Server; wNetConfig.wifi_retry_interval = 100; strcpy((char*)wNetConfig.local_ip_addr, "192.168.0.1"); strcpy((char*)wNetConfig.net_mask, "255.255.255.0"); strcpy((char*)wNetConfig.dnsServer_ip_addr, "192.168.0.1"); micoWlanStart(&wNetConfig); wifi_softap_log("ssid:%s key:%s", wNetConfig.wifi_ssid, wNetConfig.wifi_key); exit: return err; }
OSStatus MicoStartFogCloudService(mico_Context_t* const inContext) { OSStatus err = kUnknownErr; //init MicoFogCloud status inContext->appStatus.fogcloudStatus.isCloudConnected = false; inContext->appStatus.fogcloudStatus.RecvRomFileSize = 0; inContext->appStatus.fogcloudStatus.isActivated = inContext->flashContentInRam.appConfig.fogcloudConfig.isActivated; inContext->appStatus.fogcloudStatus.isOTAInProgress = false; //init fogcloud service interface err = fogCloudInit(inContext); require_noerr_action(err, exit, fogcloud_log("ERROR: FogCloud interface init failed!") ); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)fogNotify_WifiStatusHandler ); require_noerr_action(err, exit, fogcloud_log("ERROR: MICOAddNotification (mico_notify_WIFI_STATUS_CHANGED) failed!") ); // start MicoFogCloud main thread (dev reset && ota check, then start fogcloud service) err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "fog_main", fogcloud_main_thread, STACK_SIZE_FOGCLOUD_MAIN_THREAD, inContext ); exit: return err; }
int application_start( void ) { MicoInit( ); MICOAddNotification( mico_notify_WIFI_SCAN_COMPLETED, (void *)micoNotify_ApListCallback ); wifi_sacn_log("start scan mode, please wait..."); micoWlanStartScan( ); return kNoErr; }
OSStatus startEasyLink( mico_Context_t * const inContext) { easylink_log_trace(); OSStatus err = kUnknownErr; easylinkClient_fd = -1; mico_mcu_powersave_config(true); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)EasyLinkNotify_DHCPCompleteHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); // Start the EasyLink thread ConfigWillStart(inContext); mico_rtos_init_semaphore(&easylink_sem, 1); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x1000, (void*)inContext ); require_noerr_action( err, exit, easylink_log("ERROR: Unable to start the EasyLink thread.") ); exit: return err; }
//function listap(t) if t then for k,v in pairs(t) do print(k.."\t"..v);end else print('no ap') end end wifi.scan(listap) static int lwifi_scan( lua_State* L ) { 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); OSStatus err = MICOAddNotification( mico_notify_WIFI_SCAN_ADV_COMPLETED, (void *)_micoNotify_WiFi_Scan_OK ); require_noerr( err, exit ); micoWlanStartScanAdv(); gL = L; } else { if(wifi_scan_succeed != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed); wifi_scan_succeed = LUA_NOREF; } exit: return 0; }
OSStatus startEasyLink( mico_Context_t * const inContext) { easylink_log_trace(); OSStatus err = kUnknownErr; require_action(inContext->micoStatus.easylink_thread_handler==NULL, exit, err = kParamErr); require_action(inContext->micoStatus.easylink_sem==NULL, exit, err = kParamErr); inContext->micoStatus.easylinkClient_fd = -1; //ps_enable(); mico_mcu_powersave_config(true); mico_rtos_init_mutex(&_mutex); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)EasyLinkNotify_WifiStatusHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)EasyLinkNotify_WiFIParaChangedHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_COMPLETED, (void *)EasyLinkNotify_EasyLinkCompleteHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_EASYLINK_GET_EXTRA_DATA, (void *)EasyLinkNotify_EasyLinkGetExtraDataHandler ); require_noerr(err, exit); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)EasyLinkNotify_DHCPCompleteHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)EasyLinkNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); /*Led trigger*/ mico_init_timer(&_Led_EL_timer, LED_EL_TRIGGER_INTERVAL, _led_EL_Timeout_handler, NULL); mico_start_timer(&_Led_EL_timer); // Start the EasyLink thread ConfigWillStart(inContext); mico_rtos_init_semaphore(&inContext->micoStatus.easylink_sem, 1); err = mico_rtos_create_thread(&inContext->micoStatus.easylink_thread_handler, MICO_APPLICATION_PRIORITY, "EASYLINK", easylink_thread, 0x1000, (void*)inContext ); require_noerr_action( err, exit, easylink_log("ERROR: Unable to start the EasyLink thread.") ); exit: return err; }
static OSStatus _initBonjourForEasyLink( WiFi_Interface interface, mico_Context_t * const inContext ) { char *temp_txt= NULL; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; temp_txt = malloc(500); require_action(temp_txt, exit, err = kNoMemoryErr); memset(&init, 0x0, sizeof(bonjour_init_t)); micoWlanGetIPStatus(¶, Station); init.service_name = "_easylink_config._tcp.local."; /* name#xxxxxx.local. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.host_name = (char*)__strdup(temp_txt); /* name#xxxxxx. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.instance_name = (char*)__strdup(temp_txt); init.service_port = FTC_PORT; init.interface = interface; temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "MAC=%s.", temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(FIRMWARE_REVISION); sprintf(temp_txt, "%sFirmware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HARDWARE_REVISION); sprintf(temp_txt, "%sHardware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MicoGetVer()); sprintf(temp_txt, "%sMICO OS Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%sModel=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(PROTOCOL); sprintf(temp_txt, "%sProtocol=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MANUFACTURER); sprintf(temp_txt, "%sManufacturer=%s.", temp_txt, temp_txt2); free(temp_txt2); if(interface == Soft_AP) sprintf(temp_txt, "%swlan unconfigured=T.", temp_txt); else sprintf(temp_txt, "%swlan unconfigured=F.", temp_txt); sprintf(temp_txt, "%sSeed=%u.", temp_txt, inContext->flashContentInRam.micoSystemConfig.seed); init.txt_record = (char*)__strdup(temp_txt); bonjour_service_init(init); free(init.host_name); free(init.instance_name); free(init.txt_record); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)_EasyLinkNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); exit: if(temp_txt) free(temp_txt); 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; 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; }
OSStatus MICOStartBonjourService( WiFi_Interface interface, mico_Context_t * const inContext ) { char *temp_txt= NULL; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; temp_txt = malloc(500); require_action(temp_txt, exit, err = kNoMemoryErr); memset(&init, 0x0, sizeof(bonjour_init_t)); micoWlanGetIPStatus(¶, Station); init.service_name = BONJOUR_SERVICE; /* name#xxxxxx.local. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.host_name = (char*)__strdup(temp_txt); /* name#xxxxxx. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.instance_name = (char*)__strdup(temp_txt); init.service_port = inContext->flashContentInRam.appConfig.localServerPort; init.interface = interface; temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "MAC=%s.", temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(FIRMWARE_REVISION); sprintf(temp_txt, "%sFirmware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HARDWARE_REVISION); sprintf(temp_txt, "%sHardware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MicoGetVer()); sprintf(temp_txt, "%sMICO OS Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%sModel=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(PROTOCOL); sprintf(temp_txt, "%sProtocol=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MANUFACTURER); sprintf(temp_txt, "%sManufacturer=%s.", temp_txt, temp_txt2); free(temp_txt2); sprintf(temp_txt, "%sSeed=%u.", temp_txt, inContext->flashContentInRam.micoSystemConfig.seed); init.txt_record = (char*)__strdup(temp_txt); bonjour_service_init(init); free(init.host_name); free(init.instance_name); free(init.txt_record); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)BonjourNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)BonjourNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); start_bonjour_service(); _bonjourStarted = true; exit: if(temp_txt) free(temp_txt); return err; }
OSStatus MICOStartBonjourService( WiFi_Interface interface, mico_Context_t * const inContext ) { char temp_txt[500]; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; memset(&init, 0x0, sizeof(bonjour_init_t)); getNetPara(¶, Station); init.service_name = BONJOUR_SERVICE; /* name#xxxxxx.local. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.host_name = (char*)__strdup(temp_txt); /* name#xxxxxx. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.instance_name = (char*)__strdup(temp_txt); init.service_port = HA_SERVER_PORT; init.interface = interface; sprintf(temp_txt, "C#=%d.", 0x01); sprintf(temp_txt, "%sff=%d.", temp_txt, 0x10); temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "%sid=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%smd=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HA_PV); sprintf(temp_txt, "%spv=%s.", temp_txt, temp_txt2); free(temp_txt2); sprintf(temp_txt, "%ss#=%d.", temp_txt, 0x01); sprintf(temp_txt, "%ssf=%d.", temp_txt, 0x05); temp_txt2 = __strdup_trans_dot(HA_SV); sprintf(temp_txt, "%ssv=%s.", temp_txt, temp_txt2); free(temp_txt2); sprintf(temp_txt, "%sSeed=%u.", temp_txt, inContext->flashContentInRam.micoSystemConfig.seed); init.txt_record = (char*)__strdup(temp_txt); bonjour_service_init(init); free(init.host_name); free(init.instance_name); free(init.txt_record); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)BonjourNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)BonjourNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); start_bonjour_service(); _bonjourStarted = true; exit: return err; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC err = startMfiWac( context ); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ err = MICOStartBonjourService( Station, context ); require_noerr( err, exit ); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(100); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
void NTPClient_thread(void *inContext) { ntp_log_trace(); OSStatus err = kUnknownErr; mico_Context_t *Context = inContext; 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; socklen_t sockLen; char timeString[40]; /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)ntpNotify_WifiStatusHandler ); 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; if(_wifiConnected == false){ mico_rtos_get_semaphore(&_wifiConnected_sem, MICO_WAIT_FOREVER); mico_thread_msleep(50); } 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); while(1) { err = gethostbyname(NTP_Server, (uint8_t *)ipstr, 16); 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_log("Time Synchronoused, %s",asctime(localtime(¤t))); PlatformRTCWrite( localtime(¤t) ); goto exit; } } exit: if( err!=kNoErr )ntp_log("Exit: NTP client exit with err = %d", err); MICORemoveNotification( 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; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols;; err = startMFiWAC( context, WAC_Params, 1200); free(WAC_Params); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ // if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ // err = MICOStartBonjourService( Station, context ); // require_noerr( err, exit ); // } err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mDNSResponder", mDNSResponder_thread, 0x1000, (void*)context ); require_noerr_action( err, exit, mico_log("ERROR: Unable to start mDNSResponder thread.") ); /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start(void) { OSStatus err = kNoErr; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64]; mico_log_trace(); #if 1 /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); #endif /*wlan driver and tcpip init*/ MicoInit(); MicoSysLed(true); /**********************add ethernet**********************/ add_ethernet(); //sleep(5000); /*********************************************************/ mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /* Enter test mode, call a build-in test function amd output on STDIO */ if(MicoShouldEnterMFGMode()==true) mico_mfg_test(); /*Read current time from RTC.*/ MicoRtcGetTime(&time); currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000 - 100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); /*************add ethernet********Why add twice?***********/ //add_ethernet(); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == CONFIG_MODE_WAC) WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols; err = startMFiWAC( context, WAC_Params, 1200); free(WAC_Params); require_noerr( err, exit ); #else #error "Wi-Fi configuration mode is not defined"? #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); MicoSystemReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); micoWlanPowerOff(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); micoWlanPowerOff(); MicoSystemStandBy(MICO_WAIT_FOREVER); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start(void) { OSStatus err = kNoErr; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64] = {0}; mico_log_trace(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex);//ram互斥初始化 mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1);//系统状态信号量 mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "sys", _sys_state_thread, 800, NULL ); MICOReadConfiguration( context );//读flash数据 err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mico_log( "MiCO starting..." ); MicoInit(); #ifdef MICO_CLI_ENABLE MicoCliInit(); #endif MicoSysLed(true); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)para.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("ip = %s,mac=%s",para.ip,para.mac); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*Start system monotor thread*/ //err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000/2, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); /* Enter test mode, call a build-in test function amd output on MFG UART */ if(MicoShouldEnterMFGMode()==true){ mico_log( "Enter MFG mode by MFG button" ); mico_mfg_test(context); } /*Read current time from RTC.*/ if( MicoRtcGetTime(&time) == kNoErr ){ currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); }else mico_log("RTC function unsupported"); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); if( context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured || context->flashContentInRam.micoSystemConfig.configured == unConfigured){ mico_log("Empty configuration. Starting configuration mode..."); //HERE TO config network #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == CONFIG_MODE_WAC) WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols; err = startMFiWAC( context, WAC_Params, MICO_I2C_CP, 1200 ); free(WAC_Params); require_noerr( err, exit ); #else #error "Wi-Fi configuration mode is not defined" #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); _ConnectToAP( context ); } #ifdef MFG_MODE_AUTO if( context->flashContentInRam.micoSystemConfig.configured == mfgConfigured ){ mico_log( "Enter MFG mode automatically" ); mico_mfg_test(context); mico_thread_sleep(MICO_NEVER_TIMEOUT); } #endif err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
/*cfg={} cfg.ssid="" cfg.pwd="" cfg.ip (optional,default:11.11.11.1) cfg.netmask(optional,default:255.255.255.0) cfg.gateway(optional,default:11.11.11.1) cfg.dnsSrv(optional,default:11.11.11.1) cfg.retry_interval(optional,default:1000ms) wifi.startap(cfg,function(optional))*/ 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)) return luaL_error( L, "table arg needed" ); //ssid lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the string { 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)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the string { 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)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { 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"); //return luaL_error( L, "arg: ip needed" ); } //netmask lua_getfield(L, 1, "netmask"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { 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"); //return luaL_error( L, "arg: netmask needed" ); } //gateway lua_getfield(L, 1, "gateway"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { 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"); // return luaL_error( L, "arg: gateway needed" ); } //dnsSrv lua_getfield(L, 1, "dnsSrv"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { 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"); //return luaL_error( L, "arg: dnsSrv needed" ); } //retry_interval signed retry_interval=0; lua_getfield(L, 1, "retry_interval"); if (!lua_isnil(L, -1)){ /* found? */ retry_interval= luaL_checknumber( L, -1 ); if(retry_interval<=0) return luaL_error( L, "retry_interval:>0ms" ); } else retry_interval = 1000; wNetConfig.wifi_retry_interval = retry_interval; /*MCU_DBG("wifi_ssid:%s\r\n",wNetConfig.wifi_ssid); MCU_DBG("wifi_key:%s\r\n",wNetConfig.wifi_key); MCU_DBG("local_ip_addr:%s\r\n",wNetConfig.local_ip_addr); MCU_DBG("net_mask:%s\r\n",wNetConfig.net_mask); MCU_DBG("gateway_ip_addr:%s\r\n",wNetConfig.gateway_ip_addr); MCU_DBG("dnsServer_ip_addr:%s\r\n",wNetConfig.dnsServer_ip_addr); MCU_DBG("wifi_retry_interval:%d\r\n",wNetConfig.wifi_retry_interval);*/ //notify gL = L; 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); MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler ); } else { if(wifi_status_changed_AP != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP); wifi_status_changed_AP = LUA_NOREF; } //start wNetConfig.dhcpMode = DHCP_Server; wNetConfig.wifi_mode = Soft_AP; micoWlanStart(&wNetConfig); return 0; }
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 = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)clientNotify_WifiStatusHandler ); 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; }
void micokit_ext_mfg_test(mico_Context_t *inContext) { OSStatus err = kUnknownErr; char str[64] = {'\0'}; char mac[6]; int rgb_led_hue = 0; uint8_t dht11_ret = 0; uint8_t dht11_temp_data = 0; uint8_t dht11_hum_data = 0; int light_ret = 0; uint16_t light_sensor_data = 0; int infrared_ret = 0; uint16_t infrared_reflective_data = 0; int32_t bme280_temp = 0; uint32_t bme280_hum = 0; uint32_t bme280_press = 0; UNUSED_PARAMETER(inContext); mico_rtos_init_semaphore(&mfg_test_state_change_sem, 1); err = MICOAddNotification( mico_notify_WIFI_SCAN_COMPLETED, (void *)mico_notify_WifiScanCompleteHandler ); require_noerr( err, exit ); while(1){ switch(mfg_test_module_number){ case 0: // mfg mode start { sprintf(str, "%s\r\nStart:\r\n%s\r\n%s", "TEST MODE", " next: Key2", " prev: Key1"); mf_printf(str); while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, MICO_WAIT_FOREVER)); break; } case 1: // OLED { while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { sprintf(str, "%s OLED\r\n", OLED_MFG_TEST_PREFIX); mf_printf(str); mico_thread_msleep(300); mf_printf(mfg_test_oled_test_string); mico_thread_msleep(300); } OLED_Clear(); break; } case 2: // RGB_LED { sprintf(str, "%s RGB LED\r\nBlink: \r\n R=>G=>B", OLED_MFG_TEST_PREFIX); mf_printf(str); while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { hsb2rgb_led_open(rgb_led_hue, 100, 50); rgb_led_hue += 120; if(rgb_led_hue >= 360){ rgb_led_hue = 0; } mico_thread_msleep(300); } hsb2rgb_led_open(0, 0, 0); break; } case 3: // infrared sensor { while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { infrared_ret = infrared_reflective_read(&infrared_reflective_data); if(0 == infrared_ret){ sprintf(str, "%s Infrared\r\nInfrared: %d", OLED_MFG_TEST_PREFIX, infrared_reflective_data); mf_printf(str); } mico_thread_msleep(300); } break; } case 4: // DC Motor { sprintf(str, "%s DC Motor\r\nRun:\r\n on : 500ms\r\n off: 500ms", OLED_MFG_TEST_PREFIX); mf_printf(str); while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { dc_motor_set(1); mico_thread_msleep(500); dc_motor_set(0); mico_thread_msleep(500); } dc_motor_set(0); break; } case 5: // BME280 { while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { err = bme280_sensor_init(); if(kNoErr != err){ sprintf(str, "%s BME280\r\nMoule not found!", OLED_MFG_TEST_PREFIX); mf_printf(str); // goto next mdoule mico_thread_msleep(500); mfg_test_module_number = (mfg_test_module_number+1)%(MFG_TEST_MAX_MODULE_NUM+1); break; } else{ err = bme280_data_readout(&bme280_temp, &bme280_press, &bme280_hum); if(kNoErr == err){ sprintf(str, "%s BME280\r\nT: %3.1fC\r\nH: %3.1f%%\r\nP: %5.2fkPa", OLED_MFG_TEST_PREFIX, (float)bme280_temp/100, (float)bme280_hum/1024, (float)bme280_press/1000); mf_printf(str); } else{ sprintf(str, "%s BME280\r\nRead error!", OLED_MFG_TEST_PREFIX); mf_printf(str); } } mico_thread_msleep(500); } break; } case 6: // DHT11 { while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { dht11_ret = DHT11_Read_Data(&dht11_temp_data, &dht11_hum_data); if(0 == dht11_ret){ sprintf(str, "%s DHT11\r\nT: %3.1fC\r\nH: %3.1f%%", OLED_MFG_TEST_PREFIX, (float)dht11_temp_data, (float)dht11_hum_data); mf_printf(str); } mico_thread_sleep(1); // DHT11 must >= 1s } break; } case 7: // Light sensor { while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0)) { light_ret = light_sensor_read(&light_sensor_data); if(0 == light_ret){ sprintf(str, "%s Light\r\nLight: %d", OLED_MFG_TEST_PREFIX, light_sensor_data); mf_printf(str); } mico_thread_msleep(300); } break; } case 8: // wifi { wlan_get_mac_address(mac); sprintf(str, "%s Wi-Fi\r\nMAC:\r\n %02X%02X%02X%02X%02X%02X", OLED_MFG_TEST_PREFIX, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); mf_printf(str); //mico_thread_msleep(500); scanap_done = false; micoWlanStartScan(); while((!scanap_done) || (kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, MICO_WAIT_FOREVER))); break; } default: goto exit; // error break; } } exit: mico_thread_sleep(MICO_NEVER_TIMEOUT); }