• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int 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 
106     return 0;
107     /*********************END********************/
108 }
109 
WifiAPTask(void)110 static BOOL WifiAPTask(void)
111 {
112     // 延时2S便于查看日志
113     osDelay(TASK_DELAY_2S);
114 
115     // 注册wifi事件的回调函数
116     g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
117     g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
118     g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
119     error = RegisterWifiEvent(&g_wifiEventHandler);
120     if (error != WIFI_SUCCESS) {
121         printf("RegisterWifiEvent failed, error = %d.\r\n", error);
122         return -1;
123     }
124     printf("RegisterWifiEvent succeed!\r\n");
125     // 检查热点模式是否使能
126     if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) {
127         printf("Wifi station is  active.\r\n");
128         return -1;
129     }
130     // 设置指定的热点配置
131     HotspotConfig config = { 0 };
132 
133     strcpy_s(config.ssid, strlen(AP_SSID) + 1, AP_SSID);
134     strcpy_s(config.preSharedKey, strlen(AP_PSK) + 1, AP_PSK);
135     config.securityType = WIFI_SEC_TYPE_PSK;
136     config.band = HOTSPOT_BAND_TYPE_2G;
137     config.channelNum = CHANNEL_NUM;
138 
139     error = SetHotspotConfig(&config);
140     if (error != WIFI_SUCCESS) {
141         printf("SetHotspotConfig failed, error = %d.\r\n", error);
142         return -1;
143     }
144     printf("SetHotspotConfig succeed!\r\n");
145 
146     // 启动wifi热点模式
147     error = EnableHotspot();
148     if (error != WIFI_SUCCESS) {
149         printf("EnableHotspot failed, error = %d.\r\n", error);
150         return -1;
151     }
152     printf("EnableHotspot succeed!\r\n");
153 
154     StartUdpServer();
155     return 0;
156 }
157 
OnHotspotStaJoinHandler(StationInfo * info)158 static void OnHotspotStaJoinHandler(StationInfo *info)
159 {
160     if (info == NULL) {
161         printf("HotspotStaJoin:info is null.\r\n");
162     } else {
163         static char macAddress[MAC_ADDRESS_LEN] = { 0 };
164         unsigned char *mac = info->macAddress;
165         snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
166             mac[MAC_DATA0], mac[MAC_DATA1], mac[MAC_DATA2], mac[MAC_DATA3], mac[MAC_DATA4], mac[MAC_DATA5]);
167         printf("HotspotStaJoin: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
168         g_apEnableSuccess++;
169     }
170 }
171 
OnHotspotStaLeaveHandler(StationInfo * info)172 static void OnHotspotStaLeaveHandler(StationInfo *info)
173 {
174     if (info == NULL) {
175         printf("HotspotStaLeave:info is null.\r\n");
176     } else {
177         static char macAddress[MAC_ADDRESS_LEN] = { 0 };
178         unsigned char *mac = info->macAddress;
179         snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
180             mac[MAC_DATA0], mac[MAC_DATA1], mac[MAC_DATA2], mac[MAC_DATA3], mac[MAC_DATA4], mac[MAC_DATA5]);
181         printf("HotspotStaLeave: macAddress=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
182         g_apEnableSuccess--;
183     }
184 }
185 
OnHotspotStateChangedHandler(int state)186 static void OnHotspotStateChangedHandler(int state)
187 {
188     printf("HotspotStateChanged:state is %d.\r\n", state);
189     if (state == WIFI_HOTSPOT_ACTIVE) {
190         printf("wifi hotspot active.\r\n");
191     } else {
192         printf("wifi hotspot noactive.\r\n");
193     }
194 }
195 
Wifi_AP_Demo(void)196 static void Wifi_AP_Demo(void)
197 {
198     osThreadAttr_t attr;
199 
200     attr.name = "WifiAPTask";
201     attr.attr_bits = 0U;
202     attr.cb_mem = NULL;
203     attr.cb_size = 0U;
204     attr.stack_mem = NULL;
205     attr.stack_size = TASK_STACK_SIZE;
206     attr.priority = TASK_PRIO;
207 
208     if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL) {
209         printf("Failed to create WifiAPTask!\r\n");
210     }
211 }
212 
213 APP_FEATURE_INIT(Wifi_AP_Demo);
214