这次使用Hi3861来完成Wifi热点的连接,并启动TCP SocketServer,接收消息并将消息反馈TcpCLient。
一、连接Wifi热点。主要做法是启动开发板Wifi,然后设置热点和密码等配置信息,再连接热点。
1、先定义两个Wifi监听器,一个连接改变、一个状态改变,并注册监听器。其中重要的是OnWifiConnectionChanged连接状态事件处理函数。该函数会在连接成功后设置全局变量g_connected=1,代表已经连接成功。
2、启动Wifi
3、设置Wifi热点信息,并返回NetworkId
4、连接热点,注意此时的g_connected变量,true代表已连接,false代表未连接。这个状态在事件处理函数中设置。未连接成功时,系统会循环等待,知道事件设置该值。
二、进行联网,找到wlan0的network interface,然后启动DHCP客户端,获取IP地址。
三、启动TcpSocketServer,并收发消息
1、创建SocketServer,设置服务端口,并启动监听
2、客户端连接。接收客户端消息并发送回去。注意连接后,会创建一个新的Socket File Description。
3、关闭TcpSocketServer
四、联网结束,关闭DHCP客户端,断开Wifi,移除热点的配置信息,禁用Wifi。
五、测试情况如下:
1、启动开发板,连接Wifi热点,启动TcpServer
2、TcpClient(网络调试助手)连接开发板的TcpServer(HiBurn)。
![2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区 2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202101/0191c9a68652d868ce04738d5f8def3d885d27.PNG)
3、TcpClient输入数据并发送,TcpServer接收后再发送回TcpClient。
![2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区 2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202101/e7cb04397b04c4e6333949afc6835d023bdcc6.PNG)
![2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区 2020征文#使用Hi3861完成连接wifi热点并启动TCPSocketServer-鸿蒙开发者社区](https://dl-harmonyos.51cto.com/images/202101/c86de3e59a969fe822524225e64fccd34d41eb.PNG)
六、全部源代码,我都注释了,希望大家能够有所参考。
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_device.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
static char request[128] = "";
static int g_connected = 0;
void PrintLinkedInfo(WifiLinkedInfo* info) {
if (!info) return;
static char macAddress[32] = {0};
unsigned char* mac = info->bssid;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %srn", macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}
void OnWifiConnectionChanged(int state, WifiLinkedInfo* info) {
if (!info) return;
printf("%s %d, state = %d, info = rn", __FUNCTION__, __LINE__, state);
PrintLinkedInfo(info);
if (state == WIFI_STATE_AVALIABLE) {
g_connected = 1;
} else {
g_connected = 0;
}
}
void OnWifiScanStateChanged(int state, int size) {
printf("%s %d, state = %X, size = %drn", __FUNCTION__, __LINE__, state, size);
}
void DisconnectTcpSocket(int connfd) {
sleep(1);
printf("do_disconnect...rn");
lwip_close(connfd);
sleep(1);
}
void CloseTcpSocket(int socketfd) {
printf("do_cleanup...rn");
lwip_close(socketfd);
}
static void TcpServerHandler(void) {
ssize_t retval = 0;
unsigned short port = 9118;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in clientAddr = {0};
socklen_t clientAddrLen = sizeof(clientAddr);
struct sockaddr_in serverAddr = {0};
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
retval = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (retval < 0) {
printf("bind failed, %ld!rn", retval);
CloseTcpSocket(sockfd);
return;
}
printf("bind to port %d success!rn", port);
int backlog = 1;
retval = listen(sockfd, backlog);
if (retval < 0) {
printf("listen failed!rn");
CloseTcpSocket(sockfd);
return;
}
printf("listen with %d backlog success!rn", backlog);
int outerFlag = 1;
while (outerFlag) {
int connfd = -1;
connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (connfd < 0) {
printf("accept failed, %d, %drn", connfd, errno);
CloseTcpSocket(sockfd);
outerFlag = 0;
}
printf("accept success, connfd = %d !rn", connfd);
printf("client addr info: host = %s, port = %drn", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
int innerFlag = 1;
while (innerFlag) {
retval = recv(connfd, request, sizeof(request), 0);
if (retval < 0) {
printf("recv request failed, %ld!rn", retval);
innerFlag = 0;
} else if (retval == 0) {
printf("client disconnected!rn");
innerFlag = 0;
} else {
printf("recv request{%s} from client done!rn", request);
retval = send(connfd, request, strlen(request), 0);
if (retval <= 0) {
printf("send response failed, %ld!rn", retval);
innerFlag = 0;
}
printf("send response{%s} to client done!rn", request);
memset(&request, 0, sizeof(request));
}
DisconnectTcpSocket(connfd);
outerFlag = 0;
}
CloseTcpSocket(sockfd);
}
static void TcpServerTask(void *arg) {
(void)arg;
WifiEvent eventListener = {
.OnWifiConnectionChanged = OnWifiConnectionChanged,
.OnWifiScanStateChanged = OnWifiScanStateChanged
};
osDelay(10);
WifiErrorCode errCode = RegisterWifiEvent(&eventListener);
printf("RegisterWifiEvent: %drn", errCode);
WifiDeviceConfig apConfig = {};
strcpy(apConfig.ssid, "MyMobile");
strcpy(apConfig.preSharedKey, "12345678");
apConfig.securityType = WIFI_SEC_TYPE_PSK;
int netId = -1;
errCode = EnableWifi();
printf("EnableWifi: %drn", errCode);
osDelay(10);
errCode = AddDeviceConfig(&apConfig, &netId);
printf("AddDeviceConfig: %drn", errCode);
g_connected = 0;
errCode = ConnectTo(netId);
printf("ConnectTo(%d): %drn", netId, errCode);
while (!g_connected) {
osDelay(10);
}
printf("g_connected: %drn", g_connected);
osDelay(50);
struct netif* iface = netifapi_netif_find("wlan0");
if (iface) {
err_t ret = netifapi_dhcp_start(iface);
printf("netifapi_dhcp_start: %drn", ret);
osDelay(300);
ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
printf("netifapi_netif_common: %drn", ret);
}
TcpServerHandler();
err_t ret = netifapi_dhcp_stop(iface);
printf("netifapi_dhcp_stop: %drn", ret);
Disconnect();
RemoveDevice(netId);
errCode = DisableWifi();
printf("DisableWifi: %drn", errCode);
osDelay(200);
}
static void TcpServerEntry(void) {
osThreadAttr_t attr;
attr.name = "TcpServerTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)TcpServerTask, NULL, &attr) == NULL) {
printf("SunLaoTest-Fail Create");
}
}
APP_FEATURE_INIT(TcpServerEntry);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
\r\n因为发布的时候,总是报错,我都改为rn了。
赞一波
真快啊
厉害牛牪犇