/*************************************************** * * FUNCTION : Into EasyLink mode * ****************************************************/ void IntoEasyLink() { int ledflag=0; #if (GAGENT_FEATURE_WXCHIP_EASYLINK == 1) wlan_disconnect(); OpenEasylink2(60); while( g_Xpg_GlobalVar.AirLinkFlag==0) { msleep(100); DRV_Led_Red(ledflag); DRV_Led_Green(1); ledflag=!ledflag; } #endif return; }
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; } } }