1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 "cmsis_os2.h"
17 #include "hal_trace.h"
18 #include "ohos_init.h"
19 #include "wifi_device.h"
20 #include "wifi_error_code.h"
21 #include "wifi_hotspot.h"
22 #include <string.h>
23 #include <unistd.h>
24
25 #define AP_SSID "test_wifi"
26 #define AP_PSK "12345678"
27 #define AP_TASK_STACK_SIZE 10240
28 #define AP_TASK_PRIORITY 25
29 #define CHANNEL_NUM 7
30 #define BIT0 0
31 #define BIT1 1
32 #define BIT2 2
33 #define BIT3 3
34 #define BIT4 4
35 #define BIT5 5
36
37 static void OnHotspotStaJoinHandler(StationInfo *info);
38 static void OnHotspotStateChangedHandler(int state);
39 static void OnHotspotStaLeaveHandler(StationInfo *info);
40
41 static int g_apEnableSuccess = 0;
42 WifiEvent g_wifiEventHandler = {0};
43 WifiErrorCode error;
44
WifiAPTask(void)45 static void WifiAPTask(void)
46 {
47 osDelay(2000);
48
49 //注册wifi事件的回调函数
50 g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
51 g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
52 g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
53 error = RegisterWifiEvent(&g_wifiEventHandler);
54 if (error != WIFI_SUCCESS) {
55 printf("RegisterWifiEvent failed, error = %d.\r\n", error);
56 return;
57 }
58 printf("RegisterWifiEvent succeed!\r\n");
59
60 HotspotConfig config = {0};
61 strcpy(config.ssid, AP_SSID);
62 strcpy(config.preSharedKey, AP_PSK);
63 config.securityType = WIFI_SEC_TYPE_PSK;
64 config.band = HOTSPOT_BAND_TYPE_2G;
65 config.channelNum = CHANNEL_NUM;
66
67 error = SetHotspotConfig(&config);
68 if (error != WIFI_SUCCESS) {
69 printf("SetHotspotConfig failed, error = %d.\r\n", error);
70 return;
71 }
72 printf("SetHotspotConfig succeed!\r\n");
73
74 error = EnableHotspot();
75 if (error != WIFI_SUCCESS) {
76 printf("EnableHotspot failed, error = %d.\r\n", error);
77 return -1;
78 }
79 printf("EnableHotspot succeed!\r\n");
80
81 if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) {
82 printf("Wifi Hotspot is not actived.\r\n");
83 return -1;
84 }
85 printf("Wifi Hotspot is actived!\r\n");
86 }
87
OnHotspotStaJoinHandler(StationInfo * info)88 static void OnHotspotStaJoinHandler(StationInfo *info)
89 {
90 if (info == NULL) {
91 printf("HotspotStaJoin:info is null.\r\n");
92 return;
93 }
94 StationInfo stainfo[WIFI_MAX_STA_NUM] = {0};
95 StationInfo *sta_list_node = NULL;
96 unsigned int size = WIFI_MAX_STA_NUM;
97
98 error = GetStationList(stainfo, &size);
99 if (error != WIFI_SUCCESS) {
100 printf("HotspotStaJoin:get list fail, error is %d.\r\n", error);
101 return;
102 }
103 sta_list_node = stainfo;
104 for (uint32_t i = 0; i < size; i++, sta_list_node++) {
105 unsigned char *mac = sta_list_node->macAddress;
106 printf("HotspotSta[%u]: macAddress=%02X:%02X:%02X:%02X:%02X:%02X\r\n", i,
107 mac[BIT0], mac[BIT1], mac[BIT2], mac[BIT3], mac[BIT4], mac[BIT5]);
108 }
109 g_apEnableSuccess++;
110 }
111
OnHotspotStaLeaveHandler(StationInfo * info)112 static void OnHotspotStaLeaveHandler(StationInfo *info)
113 {
114 if (info == NULL) {
115 printf("HotspotStaLeave:info is null.\r\n");
116 return;
117 }
118 unsigned char *mac = info->macAddress;
119 printf("HotspotStaLeave: macAddress=%02X:%02X:%02X:%02X:%02X:%02X, reason=%d.\r\n",
120 mac[BIT0], mac[BIT1], mac[BIT2], mac[BIT3], mac[BIT4], mac[BIT5],
121 info->disconnectedReason);
122 g_apEnableSuccess--;
123 }
124
OnHotspotStateChangedHandler(int state)125 static void OnHotspotStateChangedHandler(int state)
126 {
127 printf("HotspotStateChanged:state is %d.\r\n", state);
128 if (state == WIFI_HOTSPOT_ACTIVE) {
129 printf("wifi hotspot active.\r\n");
130 } else {
131 printf("wifi hotspot noactive.\r\n");
132 }
133 }
134
Wifi_AP_Demo(void)135 static void Wifi_AP_Demo(void)
136 {
137 printf("[%s:%d]: %s\n", __FILE__, __LINE__, __func__);
138
139 osThreadAttr_t attr;
140 attr.name = "WifiAPTask";
141 attr.attr_bits = 0U;
142 attr.cb_mem = NULL;
143 attr.cb_size = 0U;
144 attr.stack_mem = NULL;
145 attr.stack_size = AP_TASK_STACK_SIZE;
146 attr.priority = AP_TASK_PRIORITY;
147
148 if (osThreadNew((osThreadFunc_t)WifiAPTask, NULL, &attr) == NULL) {
149 printf("Falied to create WifiAPTask!\r\n");
150 }
151 }
152
153 APP_FEATURE_INIT(Wifi_AP_Demo);
154