int application_start(void) { mxchipInit(); Platform_Init(); UART_Init(); #ifdef RFLowPowerMode ps_enable(); #endif #ifdef MCULowPowerMode mico_mcu_powersave_config(mxEnable); #endif debug_out("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); debug_out (menu); debug_out ("\nMXCHIP> "); while(1) { Main_Menu(); if(configSuccess){ wNetConfig.wifi_mode = Station; wNetConfig.dhcpMode = DHCP_Client; wNetConfig.wifi_retry_interval = 100; StartNetwork(&wNetConfig); debug_out("connect to %s.....\r\n", wNetConfig.wifi_ssid); configSuccess = 0; debug_out ("\nMXCHIP> "); } } }
/* Sysytem initilize */ void mxchipWNet_HA_init(void) { network_InitTypeDef_st wNetConfig; network_InitTypeDef_adv_st wNetConfigAdv; int err = MXCHIP_FAILED; net_para_st para; device_info = (mxchipWNet_HA_st *)malloc(sizeof(mxchipWNet_HA_st)); memset(device_info, 0, sizeof(mxchipWNet_HA_st)); SystemCoreClockUpdate(); mxchipInit(); hal_uart_init(); getNetPara(¶, Station); formatMACAddr((void *)device_info->status.mac, ¶.mac); strcpy((char *)device_info->status.ip, (char *)¶.ip); strcpy((char *)device_info->status.mask, (char *)¶.mask); strcpy((char *)device_info->status.gw, (char *)¶.gate); strcpy((char *)device_info->status.dns, (char *)¶.dns); readConfiguration(device_info); device_info->tcpServer_fd = -1; device_info->tcpClient_fd = (int *)malloc(4 * MAX_CLIENT); device_info->cloud_fd = -1; device_info->udpSearch_fd = -1; memset(device_info->tcpClient_fd, -1, sizeof(4 * MAX_CLIENT)); if(device_info->conf.fastLinkConf.availableRecord){ //Try fast link memcpy(&wNetConfigAdv.ap_info, &device_info->conf.fastLinkConf.ap_info, sizeof(ApList_adv_t)); memcpy(&wNetConfigAdv.key, &device_info->conf.fastLinkConf.key, device_info->conf.fastLinkConf.key_len); wNetConfigAdv.key_len = device_info->conf.fastLinkConf.key_len; wNetConfigAdv.dhcpMode = DHCP_Client; strcpy(wNetConfigAdv.local_ip_addr, (char*)device_info->conf.ip); strcpy(wNetConfigAdv.net_mask, (char*)device_info->conf.mask); strcpy(wNetConfigAdv.gateway_ip_addr, (char*)device_info->conf.gw); strcpy(wNetConfigAdv.dnsServer_ip_addr, (char*)device_info->conf.dns); wNetConfigAdv.wifi_retry_interval = 100; err = StartAdvNetwork(&wNetConfigAdv); } if(err == MXCHIP_FAILED){ wNetConfig.wifi_mode = Station; strcpy(wNetConfig.wifi_ssid, device_info->conf.sta_ssid); strcpy(wNetConfig.wifi_key, device_info->conf.sta_key); wNetConfig.dhcpMode = DHCP_Client; strcpy(wNetConfig.local_ip_addr, (char*)device_info->conf.ip); strcpy(wNetConfig.net_mask, (char*)device_info->conf.mask); strcpy(wNetConfig.gateway_ip_addr, (char*)device_info->conf.gw); strcpy(wNetConfig.dnsServer_ip_addr, (char*)device_info->conf.dns); wNetConfig.wifi_retry_interval = 500; StartNetwork(&wNetConfig); } ps_enable(); }
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; }
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 main(void) { int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1; char *buf, ip_address[16],ipstr[32]; int len; int con = -1; int opt = 0; int clientfd[8]; fd_set readfds, exceptfds; struct timeval_t t; struct sockaddr_t addr; socklen_t addrLen; struct timeval_t timeout; #ifdef DynamicMemAlloc int bufferSize; libConfig.tcp_buf_dynamic = mxEnable; libConfig.tcp_max_connection_num = 12; libConfig.tcp_rx_size = 2048; libConfig.tcp_tx_size = 2048; libConfig.hw_watchdog = 0; libConfig.wifi_channel = WIFI_CHANNEL_1_13; lib_config(&libConfig); #endif for(i=0;i<8;i++) clientfd[i] = -1; buf = (char*)malloc(3*1024); mxchipInit(); UART_Init(); printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); #ifdef LowPowerMode ps_enable(); #endif memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); wNetConfig.wifi_mode = Station; strcpy((char*)wNetConfig.wifi_ssid, AP_NAME); strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD); wNetConfig.dhcpMode = DHCP_Client; StartNetwork(&wNetConfig); printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid); t.tv_sec = 0; t.tv_usec = 100; set_tcp_keepalive(3, 60); while(1) { mxchipTick(); /*If wifi is established, connect to www.baidu.com, and send a http request*/ #ifdef BlockMode if(wifi_up&&webserverTest){ webserverTest = 0; if(gethostbyname(WEB_SERVER, (u8 *)ipstr, 32)!=0){ printf("DNS test: %s failed \r\n", WEB_SERVER); webserverTest = 1; continue; } printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr); fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.s_ip = inet_addr(ipstr); addr.s_port = 80; timeout.tv_sec = 3; timeout.tv_usec = 0; setsockopt(fd_client,0,SO_CONTIMEO,&timeout,sizeof(struct timeval_t)); if (connect(fd_client, &addr, sizeof(addr))!=0) { close(fd_client); fd_client = -1; printf("Connect to web server failed! \r\n"); webserverTest = 1; } else{ printf("Connect to web server success! Reading web pages...\r\n"); send(fd_client, httpRequest, sizeof(httpRequest), 0); } } #else if(cloud_ip_addr == -1 && dns_pending == 0 && wifi_up){ //DNS function cloud_ip_addr = dns_request(WEB_SERVER); if(cloud_ip_addr == -1) printf("DNS test: %s failed. \r\n", WEB_SERVER); else if (cloud_ip_addr == 0) //DNS pending, waiting for callback dns_pending = 1; } if( fd_client == -1 && cloud_ip_addr!=-1 && !dns_pending){ fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4); addr.s_ip = cloud_ip_addr; addr.s_port = 80; printf("Connecting to %s..., at port %d\r\n", inet_ntoa(ipstr, cloud_ip_addr), addr.s_port); if (connect(fd_client, &addr, sizeof(addr))!=0) { printf("Connect to %s failed.\r\n", WEB_SERVER); printf("Free: %d, max length: %d.\r\n", total_free, max_len); } } if(cloud_connected == 1){ send(fd_client, httpRequest, sizeof(httpRequest), 0); cloud_connected = 0; } #endif /*Establish a TCP server that accept the tcp clients connections*/ if (fd_listen==-1) { fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); #ifdef DynamicMemAlloc bufferSize = 5*1024; setsockopt(fd_listen,0,SO_RDBUFLEN,&bufferSize,4); bufferSize = 5*1024; setsockopt(fd_listen,0,SO_WRBUFLEN,&bufferSize,4); #endif addr.s_port = 8080; bind(fd_listen, &addr, sizeof(addr)); listen(fd_listen, 0); printf("TCP server established at port: %d \r\n", addr.s_port); } /*Establish a UDP port to receive any data sent to this port*/ if (fd_udp==-1) { fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP); addr.s_port = 8090; bind(fd_udp, &addr, sizeof(addr)); printf("Open UDP port %d\r\n", addr.s_port); } /*Check status on erery sockets */ FD_ZERO(&readfds); FD_SET(fd_listen, &readfds); FD_SET(fd_udp, &readfds); if(fd_client!=-1) FD_SET(fd_client, &readfds); for(i=0;i<8;i++) { if (clientfd[i] != -1) FD_SET(clientfd[i], &readfds); } select(1, &readfds, NULL, &exceptfds, &t); /*Check tcp connection requests */ if(FD_ISSET(fd_listen, &readfds)) { j = accept(fd_listen, &addr, &len); if (j > 0) { inet_ntoa(ip_address, addr.s_ip ); printf("Client %s:%d connected\r\n", ip_address, addr.s_port); for(i=0;i<8;i++) { if (clientfd[i] == -1) { clientfd[i] = j; break; } } } } /*Read html data from www.baidu.com */ if(fd_client != -1){ if(FD_ISSET(fd_client, &readfds)) { con = recv(fd_client, buf, 2*1024, 0); if(con > 0) printf("Get %s data successful! data length: %d bytes\r\n", WEB_SERVER, con); else{ close(fd_client); fd_client = -1; cloud_ip_addr = -1; webserverTest = 1; printf("Web connection closed.\r\n"); } } } /*Read data from tcp clients and send data back */ for(i=0;i<8;i++) { if (clientfd[i] != -1) { if (FD_ISSET(clientfd[i], &readfds)) { con = recv(clientfd[i], buf, 1*1024, 0); if (con > 0) send(clientfd[i], buf, con, 0); else { close(clientfd[i]); clientfd[i] = -1; } } else if (FD_ISSET(clientfd[i], &exceptfds)) clientfd[i] = -1; } } /*Read data from udp and send data back */ if (FD_ISSET(fd_udp, &readfds)) { con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen); sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t)); } } }