Open1081 softap+TCP UDP echo
时间:10-02
整理:3721RD
点击:
为下面的项目做准备,调通Open1081的softap和TCP,UDP收发。
发现一个问题,softap不能设置密码(wifi_key),设置之后StartNetwork返回-1。这个有谁知道的?
把wifi_key参数留空,电脑就能顺利的找到热点。下面是我设置的Open1081热点。
试了一下TCP连接,最多7个,看代码应该是8个才对,可能是因为监听用去了一个socket
测试log
感觉官方自带的代码有点乱,我作了一些调整,看起来舒服一点。
- #include "stdio.h"
- #include "ctype.h"
- #include "stm32f2xx.h"
- #include "platform.h"
- #include "mxchipWNET.h"
- #define AP_NAME "TP-604"
- #define AP_PASSWORD "robot604"
- #define APP_INFO "------ Open1081 ------"
- #define SOFTAP_NAME "Open1081"
- #define SOFTAP_PASSWD "123456"
- int wifi_up = 0;
- int webserverTest = 1;
- int cloud_ip_addr = -1;
- int cloud_connected = 0;
- int dns_pending = 0;
- extern u32 MS_TIMER;
- /* ========================================
- User provide callback functions
- ======================================== */
- void system_version(char *str, int len)
- {
- snprintf(str, len, "%s", APP_INFO);
- }
- void WifiStatusHandler(int event)
- {
- switch (event) {
- case MXCHIP_WIFI_UP:
- wifi_up = 1;
- printf("Station up \r\n");
- break;
- case MXCHIP_WIFI_DOWN:
- printf("Station down \r\n");
- break;
- case MXCHIP_UAP_UP:
- break;
- case MXCHIP_UAP_DOWN:
- break;
- default:
- break;
- }
- }
- void NetCallback(net_para_st *pnet)
- {
- printf("IP address: %s \r\n", pnet->ip);
- printf("NetMask address: %s \r\n", pnet->mask);
- printf("Gateway address: %s \r\n", pnet->gate);
- printf("DNS server address: %s \r\n", pnet->dns);
- printf("MAC address: %s \r\n", pnet->mac);
- }
- void Open1081stationModeStart(void)
- {
- network_InitTypeDef_st wNetConfig;
- int ret;
- 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;
- ret = StartNetwork(&wNetConfig);
- printf("connect to %s.....[%d]\r\n", wNetConfig.wifi_ssid, ret);
- }
- void Open1081softAPModeStart(void)
- {
- static network_InitTypeDef_st wNetConfig;
- int ret;
- memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
- wNetConfig.wifi_mode = Soft_AP;
- strcpy((char*)wNetConfig.wifi_ssid, SOFTAP_NAME);
- strcpy((char*)wNetConfig.wifi_key, "");
- strcpy((char*)wNetConfig.local_ip_addr, "192.168.1.1");
- strcpy((char*)wNetConfig.net_mask, "255.255.255.0");
- strcpy((char*)wNetConfig.address_pool_start,"192.168.1.10");
- strcpy((char*)wNetConfig.address_pool_end, "192.168.1.250");
- wNetConfig.dhcpMode = DHCP_Server;
- ret = StartNetwork(&wNetConfig);
- printf("Setup soft AP: %s[%d]\r\n", wNetConfig.wifi_ssid, ret);
- }
- void Open1081Init()
- {
- lib_config_t libConfig;
- 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", APP_INFO);
- printf("library version: %s\r\n", system_lib_version());
- ps_enable();
- set_tcp_keepalive(3, 60);
- Open1081softAPModeStart();
- }
- int Open1081TCPUDPInit(int* fd_listen, int* fd_udp)
- {
- int bufferSize;
- struct sockaddr_t addr;
- /* Establish a TCP server that accept the tcp clients connections */
- *fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- bufferSize = 5*1024;
- setsockopt(*fd_listen, 0, SO_RDBUFLEN, &bufferSize, 4);
- bufferSize = 5*1024;
- setsockopt(*fd_listen, 0, SO_WRBUFLEN, &bufferSize, 4);
- 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 */
- *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);
- return 0;
- }
- int main(void)
- {
- int tcp_client;
- int i, fd_listen = -1, fd_udp = -1, fd_client = -1;
- char *buf, ip_address[16];
- int len;
- int con = -1;
- int clientfd[8];
- fd_set readfds, exceptfds;
- struct timeval_t t;
- struct sockaddr_t addr;
- socklen_t addrLen;
- Open1081Init();
- for(i = 0; i < 8; i++)
- clientfd[i] = -1;
- t.tv_sec = 0;
- t.tv_usec = 100;
- // tcp and udo recv data buffer 3KB
- buf = NULL;
- buf = (char*)malloc(3*1024);
- if (!buf) {
- printf("malloc error\n");
- return 1;
- }
- Open1081TCPUDPInit(&fd_listen, &fd_udp);
- for (;;) {
- mxchipTick();
- /* Check status on every 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)) {
- tcp_client = accept(fd_listen, &addr, &len);
- if (tcp_client > 0) {
- inet_ntoa(ip_address, addr.s_ip);
- printf("Client %s:%d connected[%d]\r\n", ip_address, addr.s_port, tcp_client);
- for(i = 0; i < 8; i++) {
- if (clientfd[i] == -1) {
- clientfd[i] = tcp_client;
- break;
- }
- }
- }
- }
- /* 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 {
- printf("clientfd[%d]: %d closed\r\n", i, clientfd[i]);
- 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));
- }
- }
- }