1 /*
2 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "cmsis_os2.h"
21 #include "ohos_init.h"
22
23 #include "wifi_device.h"
24 #include "wifi_hotspot.h"
25 #include "wifi_error_code.h"
26 #include "lwip/netifapi.h"
27
28
29 #define AP_SSID "BearPi"
30 #define AP_PSK "0987654321"
31
32 #define TASK_STACK_SIZE (1024 * 10)
33 #define TASK_PRIO 25
34 #define TASK_DELAY_2S 200
35 #define ONE_SECOND 1
36 #define DEF_TIMEOUT 15
37 #define CHANNEL_NUM 7
38 #define MAC_ADDRESS_LEN 32
39 #define MAC_DATA0 0
40 #define MAC_DATA1 1
41 #define MAC_DATA2 2
42 #define MAC_DATA3 3
43 #define MAC_DATA4 4
44 #define MAC_DATA5 5
45 #define UDP_SERVERPORT 8888
46
47 static void OnHotspotStaJoinHandler(StationInfo *info);
48 static void OnHotspotStateChangedHandler(int state);
49 static void OnHotspotStaLeaveHandler(StationInfo *info);
50
51 static struct netif *g_lwip_netif = NULL;
52 static int g_apEnableSuccess = 0;
53 static WifiEvent g_wifiEventHandler = { 0 };
54 WifiErrorCode error;
55
56
StartUdpServer(void)57 static void StartUdpServer(void)
58 {
59 /****************以下为UDP服务器代码,默认IP:192.168.5.1***************/
60 // 在sock_fd 进行监听
61 int sock_fd;
62 // 服务端地址信息
63 struct sockaddr_in server_sock;
64
65 // 创建socket
66 if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
67 perror("socket is error.\r\n");
68 return -1;
69 }
70
71 bzero(&server_sock, sizeof(server_sock));
72 server_sock.sin_family = AF_INET;
73 server_sock.sin_addr.s_addr = htonl(INADDR_ANY);
74 server_sock.sin_port = htons(UDP_SERVERPORT);
75
76 // 调用bind函数绑定socket和地址
77 if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1) {
78 perror("bind is error.\r\n");
79 return -1;
80 }
81
82 int ret;
83 char recvBuf[512] = { 0 };
84 // 客户端地址信息
85 struct sockaddr_in client_addr;
86 int size_client_addr = sizeof(struct sockaddr_in);
87 while (1) {
88 printf("Waiting to receive data...\r\n");
89 memset_s(recvBuf, sizeof(recvBuf), 0, sizeof(recvBuf));
90 ret = recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&client_addr,
91 (socklen_t*)&size_client_addr);
92 if (ret < 0) {
93 printf("UDP server receive failed!\r\n");
94 return -1;
95 }
96 printf("receive %d bytes of data from ipaddr = %s, port = %d.\r\n", ret, inet_ntoa(client_addr.sin_addr),
97 ntohs(client_addr.sin_port));
98 printf("data is %s\r\n", recvBuf);
99 ret = sendto(sock_fd, recvBuf, strlen(recvBuf), 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
100 if (ret < 0) {
101 printf("UDP server send failed!\r\n");
102 return -1;
103 }
104 }
105 /*********************END********************/
106 }
107
WifiAPTask(void)108 static BOOL WifiAPTask(void)
109 {
110 // 延时2S便于查看日志
111 osDelay(TASK_DELAY_2S);
112
113 // 注册wifi事件的回调函数
114 g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
115 g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
116 g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
117 error = RegisterWifiEvent(&g_wifiEventHandler);
118 if (error != WIFI_SUCCESS) {
119 printf("RegisterWifiEvent failed, error = %d.\r\n", error);
120 return -1;
121 }
122 printf("RegisterWifiEvent succeed!\r\n");
123 // 检查热点模式是否使能
124 if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) {
125 printf("Wifi station is actived.\r\n");
126 return -1;
127 }
128 // 设置指定的热点配置
129 HotspotConfig config = { 0 };
130
131 strcpy_s(config.ssid, strlen(AP_SSID) + 1, AP_SSID);
132 strcpy_s(config.preSharedKey, strlen(AP_PSK) + 1, AP_PSK);
133 config.securityType = WIFI_SEC_TYPE_PSK;
134 config.band = HOTSPOT_BAND_TYPE_2G;
135 config.channelNum = CHANNEL_NUM;
136
137 error = SetHotspotConfig(&config);
138 if (error != WIFI_SUCCESS) {
139 printf("SetHotspotConfig failed, error = %d.\r\n", error);
140 return -1;
141 }
142 printf("SetHotspotConfig succeed!\r\n");
143
144 // 启动wifi热点模式
145 error = EnableHotspot();
146 if (error != WIFI_SUCCESS) {
147 printf("EnableHotspot failed, error = %d.\r\n", error);
148 return -1;
149 }
150 printf("EnableHotspot succeed!\r\n");
151
152 StartUdpServer();
153 }
154
OnHotspotStaJoinHandler(StationInfo * info)155 static void OnHotspotStaJoinHandler(StationInfo *info)
156 {
157 if (info == NULL) {
158 printf("HotspotStaJoin:info is null.\r\n");
159 } else {
160 static char macAddress[MAC_ADDRESS_LEN] = { 0 };
161 unsigned char *mac = info->macAddress;
162 snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
163 mac[MAC_DATA0], mac[MAC_DATA1], mac[MAC_DATA2], mac[MAC_DATA3], mac[MAC_DATA4], mac[MAC_DATA5]);
164 printf("HotspotStaJoin: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
165 g_apEnableSuccess++;
166 }
167 return;
168 }
169
OnHotspotStaLeaveHandler(StationInfo * info)170 static void OnHotspotStaLeaveHandler(StationInfo *info)
171 {
172 if (info == NULL) {
173 printf("HotspotStaLeave:info is null.\r\n");
174 } else {
175 static char macAddress[MAC_ADDRESS_LEN] = { 0 };
176 unsigned char *mac = info->macAddress;
177 snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
178 mac[MAC_DATA0], mac[MAC_DATA1], mac[MAC_DATA2], mac[MAC_DATA3], mac[MAC_DATA4], mac[MAC_DATA5]);
179 printf("HotspotStaLeave: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
180 g_apEnableSuccess--;
181 }
182 return;
183 }
184
OnHotspotStateChangedHandler(int state)185 static void OnHotspotStateChangedHandler(int state)
186 {
187 printf("HotspotStateChanged:state is %d.\r\n", state);
188 if (state == WIFI_HOTSPOT_ACTIVE) {
189 printf("wifi hotspot active.\r\n");
190 } else {
191 printf("wifi hotspot noactive.\r\n");
192 }
193 }
194
Wifi_AP_Demo(void)195 static void Wifi_AP_Demo(void)
196 {
197 osThreadAttr_t attr;
198
199 attr.name = "WifiAPTask";
200 attr.attr_bits = 0U;
201 attr.cb_mem = NULL;
202 attr.cb_size = 0U;
203 attr.stack_mem = NULL;
204 attr.stack_size = TASK_STACK_SIZE;
205 attr.priority = TASK_PRIO;
206
207 if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL) {
208 printf("Falied to create WifiAPTask!\r\n");
209 }
210 }
211
212 APP_FEATURE_INIT(Wifi_AP_Demo);
213