int main(void) { mxchipInit(); UART_Init(); printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); readConfig(&configParas); #ifdef LowPowerMode enable_ps_mode(SLEEP_UNIT_MS, 100, 100); #endif memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); wNetConfig.wifi_mode = Soft_AP; strcpy(wNetConfig.wifi_ssid, configParas.wifi_ssid); strcpy(wNetConfig.wifi_key, configParas.wifi_key); sprintf(wNetConfig.local_ip_addr, "192.168.1.1"); sprintf(wNetConfig.net_mask, "255.255.255.0"); sprintf(wNetConfig.gateway_ip_addr, "192.168.1.1"); sprintf(wNetConfig.dnsServer_ip_addr, "192.168.1.1"); sprintf(wNetConfig.address_pool_start, "192.168.1.100"); sprintf(wNetConfig.address_pool_end, "192.168.1.200"); wNetConfig.dhcpMode = DHCP_Server; StartNetwork(&wNetConfig); printf("Establish soft AP: %s.....\r\n", wNetConfig.wifi_ssid); set_tcp_keepalive(3, 60); http_init(); while(1) { mxchipTick(); http_tick(); } }
/* main tick */ void mxchipWNet_HA_tick(void) { mxchipTick(); if (!is_wifi_disalbed()) { if (is_network_state(STA_CONNECT) == 0 && is_network_state(UAP_START) == 0) { if (MS_TIMER > uap_start_time) { uap_start(); } } if(is_network_state(STA_CONNECT) && cloud_enable && MS_TIMER > cloud_retry) connect_to_cloud(); tcp_recv_tick(); } if(need_report){ report_state(); need_report = 0; } if (need_reload == 1) { msleep(500); NVIC_SystemReset(); } }
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)); } } }
int main(void) { u32 endtime; system_state = SYS_STATE_INIT; wifi_state = 0; reset_uart_init(); default_gpio_init(); check_gotostandby(); int2host_init(); int2host(1); NFC_TAG_INIT(); mxchipInit(); userResetConfig(); reset_uart_deinit(); get_config(); tcpip_init(); emsp_init(); uart_init(); http_init(); #ifndef USE_0302 APP_Verify_Program_Code(); #endif dual_uap_start(); SetTimer(1, main_function_tick, 1); wifi_option_config(); int2host(0); system_is_bootup(); //EMSP_CMD_SYSTEM_BOOTUP while(1) { if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) != Bit_RESET) IWDG_ReloadCounter(); // Reload IWDG counter else reload(); if(NFC_STARTED){ SetTimer(3000,saveNFCConfig,0); NFC_STARTED = 0; //NFC task started NFC_STARTED2 = 1; //NFC task pending } http_tick(); // HTTP Server mxchipTick(); // wifi, network function switch (system_state) { case SYS_STATE_RESET: wait_uart_dma_clean(); NVIC_SystemReset(); break; case SYS_STATE_GOTO_DEFAULT: default_config(); save_config(); NVIC_SystemReset(); break; case SYS_STATE_DELAY_RELOAD: endtime = MS_TIMER+500; while(endtime>MS_TIMER) mxchipTick(); NVIC_SystemReset(); break; case SYS_STATE_WPS: // User pushed WPS PBC, try wps. user_wifi_stop(); wps_pbc_start(); set_sys_state(last_work_state); break; case SYS_STATE_WPS_FAIL: wps_ing = 0; user_wifi_start(); set_sys_state(last_work_state); set_conncetion_status(0); break; case SYS_STATE_WPS_SUCCESS: wps_ing = 0; set_sys_state(last_work_state); save_config(); set_conncetion_status(0); break; default: break; } } }
int main(void) { int fd_client = -1; char *buf; int con = -1; int opt = 0; fd_set readfds, exceptfds; struct timeval_t t; struct sockaddr_t addr; lib_config_t libConfig; volatile int setval; volatile int readval; buf = (char*)malloc(3*1024); 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); mxchipInit(); UART_Init(); printf("\r\n%s\r\n mxchipWNet library version: %s\r\n", APP_INFO, system_lib_version()); printf(menu); printf ("\nMXCHIP> "); //init http set_tcp_keepalive(3, 60); setSslMaxlen(6*1024); t.tv_sec = 0; t.tv_usec = 100; // void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); while(1) { mxchipTick(); if(menu_enable) Main_Menu(); if(configSuccess) { wNetConfig.wifi_mode = Station; wNetConfig.dhcpMode = DHCP_Client; StartNetwork(&wNetConfig); printf("connect to %s.....\r\n", wNetConfig.wifi_ssid); configSuccess = 0; menu_enable = 1; sysTick_configuration(); config_gpio_pc6(); config_gpio_pb8(); while(1) { readval= GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6); mxchipTick(); //If wifi is established, connect to WEBSERVER, and send a http request if(https_server_addr == 0 && dns_pending == 0) { //DNS function https_server_addr = dns_request(WEB_SERVER); if(https_server_addr == -1) printf("DNS test: %s failed. \r\n", WEB_SERVER); else if (https_server_addr == 0) //DNS pending, waiting for callback { dns_pending = 1; printf("2DNS test: %s success. \r\n", WEB_SERVER); printf("21http server addr=%x\n",https_server_addr); } } if( fd_client == -1 && (u32)https_server_addr>0) { fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4); addr.s_ip = https_server_addr; addr.s_port = 443; if (connect(fd_client, &addr, sizeof(addr))!=0) printf("Connect to %s failed.\r\n", WEB_SERVER); else printf("Connect to %s success.\r\n", WEB_SERVER); } if(serverConnectted&&sslConnectted==0) { printf("Connect to web server success! Setup SSL ecryption...\r\n"); if (setSSLmode(1, fd_client)!= MXCHIP_SUCCESS) { printf("SSL connect fail\r\n"); close(fd_client); fd_client = -1; serverConnectted = 0; } else { printf("SSL connect\r\n"); sslConnectted = 1; } } if(readval==1&&sslConnectted) { printf("read value is 1\n"); send(fd_client, sendhttpRequest, strlen(sendhttpRequest), 0); // return 0; } if(check_responce&&sslConnectted) { printf("readval=%d\n",readval); check_responce=0; send(fd_client, responcehttpRequest, strlen(responcehttpRequest), 0); //Check status on erery sockets FD_ZERO(&readfds); if(sslConnectted) FD_SET(fd_client, &readfds); select(1, &readfds, NULL, &exceptfds, &t); //Read html data from WEBSERVER if(sslConnectted) { 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 data\r\n", WEB_SERVER, con); setval=r_http(buf); printf("read from http is %d\n",setval); if(setval==1) GPIO_SetBits(GPIOB,GPIO_Pin_8); else GPIO_ResetBits(GPIOB,GPIO_Pin_8); } else { close(fd_client); serverConnectted = 0; sslConnectted = 0; fd_client = -1; printf("Web connection closed.\r\n"); } } } } } } if(easylink) { OpenEasylink2(60); easylink = 0; } } }