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