1 /*
2 * Copyright (C) 2021-2022 Huawei Device 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 "wifi_hal_ap_interface.h"
17 #include <errno.h>
18 #include <securec.h>
19 #include "wifi_hal_adapter.h"
20 #include "wifi_hal_module_manage.h"
21 #include "wifi_hal_common_func.h"
22 #include "wifi_log.h"
23 #include "wifi_wpa_hal.h"
24 #include "wifi_hostapd_hal.h"
25 #include "wifi_hal_define.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "WifiHalApInterface"
29
30 #define DISABLE_AP_WAIT_MS 50000
31 #define ABLE_AP_WAIT_MS 50000
32 #define WIFI_MULTI_CMD_MAX_LEN 1024
33 #define IFCAE_NAME_LEN 256
34
StartSoftAp(int id,char * ifaceName)35 WifiErrorNo StartSoftAp(int id, char *ifaceName)
36 {
37 LOGI("Ready to start hostapd: %{public}d, %{public}s", id, ifaceName);
38 InitCfg(ifaceName);
39 if (StartHostapd() != WIFI_HAL_SUCCESS) {
40 LOGE("hostapd start failed!");
41 return WIFI_HAL_OPEN_HOSTAPD_FAILED;
42 }
43 if (StartHostapdHal(id) != WIFI_HAL_SUCCESS) {
44 LOGE("hostapd init failed!");
45 return WIFI_HAL_HOSTAPD_NOT_INIT;
46 }
47 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
48 if (hostapdHalDevice == NULL) {
49 LOGE("hostapdHalDevice is NULL!");
50 return WIFI_HAL_HOSTAPD_NOT_INIT;
51 }
52 if (GetIfaceState(ifaceName) == 0 || id > 0) {
53 int ret = hostapdHalDevice->enableAp(id);
54 if (ret != 0) {
55 LOGE("enableAp failed! ret=%{public}d", ret);
56 return WIFI_HAL_FAILED;
57 }
58 }
59 LOGI("AP start successfully, id:%{public}d!", id);
60 return WIFI_HAL_SUCCESS;
61 }
62
StartHostapd(void)63 WifiErrorNo StartHostapd(void)
64 {
65 char startCmd[WIFI_MULTI_CMD_MAX_LEN] = {0};
66 char *p = startCmd;
67 int onceMove = 0;
68 int sumMove = 0;
69 onceMove = snprintf_s(p, WIFI_MULTI_CMD_MAX_LEN - sumMove,
70 WIFI_MULTI_CMD_MAX_LEN - sumMove - 1, "%s", WPA_HOSTAPD_NAME);
71 if (onceMove < 0) {
72 return WIFI_HAL_FAILED;
73 }
74 p = p + onceMove;
75 sumMove = sumMove + onceMove;
76 int num;
77 WifiHostapdHalDeviceInfo *cfg = GetWifiCfg(&num);
78 if (cfg == NULL) {
79 return WIFI_HAL_FAILED;
80 }
81 for (int i = 0; i < num; i++) {
82 if (CopyConfigFile(cfg[i].cfgName) != 0) {
83 return WIFI_HAL_FAILED;
84 }
85 onceMove = snprintf_s(p, WIFI_MULTI_CMD_MAX_LEN - sumMove,
86 WIFI_MULTI_CMD_MAX_LEN - sumMove -1, " %s", cfg[i].config);
87 if (onceMove < 0) {
88 return WIFI_HAL_FAILED;
89 }
90 p = p + onceMove;
91 sumMove = sumMove + onceMove;
92 }
93 ModuleManageRetCode ret = StartModule(WPA_HOSTAPD_NAME, startCmd);
94 if (ret == MM_SUCCESS) {
95 return WIFI_HAL_SUCCESS;
96 }
97
98 LOGE("start hostapd failed!");
99 return WIFI_HAL_FAILED;
100 }
101
StartHostapdHal(int id)102 WifiErrorNo StartHostapdHal(int id)
103 {
104 LOGD("Ready to init hostapd");
105 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
106 if (hostapdHalDevice == NULL) {
107 return WIFI_HAL_FAILED;
108 }
109 return WIFI_HAL_SUCCESS;
110 }
111
StopSoftAp(int id)112 WifiErrorNo StopSoftAp(int id)
113 {
114 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
115 if (hostapdHalDevice != NULL) {
116 int ret = hostapdHalDevice->disableAp(id);
117 if (ret != 0) {
118 LOGE("disableAp failed! ret=%{public}d", ret);
119 }
120 } else {
121 LOGE("cant not get hostapd dev");
122 }
123 if (StopHostapd() != WIFI_HAL_SUCCESS) {
124 LOGE("hostapd stop failed!");
125 return WIFI_HAL_FAILED;
126 }
127 if (StopHostapdHal(id) != WIFI_HAL_SUCCESS) {
128 LOGE("hostapd_hal stop failed!");
129 return WIFI_HAL_FAILED;
130 }
131 LOGI("AP stop successfully!");
132 return WIFI_HAL_SUCCESS;
133 }
134
StopHostapd(void)135 WifiErrorNo StopHostapd(void)
136 {
137 ModuleManageRetCode ret;
138 ret = StopModule(WPA_HOSTAPD_NAME, true);
139 if (ret == MM_FAILED) {
140 LOGE("stop hostapd failed!");
141 return WIFI_HAL_FAILED;
142 }
143 return WIFI_HAL_SUCCESS;
144 }
145
StopHostapdHal(int id)146 WifiErrorNo StopHostapdHal(int id)
147 {
148 ReleaseHostapdDev(id);
149 return WIFI_HAL_SUCCESS;
150 }
151
GetStaInfos(char * infos,int32_t * size,int id)152 WifiErrorNo GetStaInfos(char *infos, int32_t *size, int id)
153 {
154 if (infos == NULL || size == NULL) {
155 LOGE("GetStaInfos infos or size is NULL");
156 return WIFI_HAL_FAILED;
157 }
158 LOGD("GetStaInfos:Start");
159 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
160 if (hostapdHalDevice == NULL) {
161 return WIFI_HAL_HOSTAPD_NOT_INIT;
162 }
163 if (hostapdHalDevice->showConnectedDevList(infos, *size, id) != 0) {
164 LOGE("ShowConnectedDevList failed!");
165 return WIFI_HAL_FAILED;
166 }
167 return WIFI_HAL_SUCCESS;
168 }
169
SetCountryCode(const char * code,int id)170 WifiErrorNo SetCountryCode(const char *code, int id)
171 {
172 if (code == NULL || strlen(code) != WIFI_COUNTRY_CODE_MAXLEN) {
173 LOGE("SetCountryCode code is invalid");
174 return WIFI_HAL_INVALID_PARAM;
175 }
176 LOGD("SetCountryCode() code: %{public}s", code);
177 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
178 if (hostapdHalDevice == NULL) {
179 return WIFI_HAL_HOSTAPD_NOT_INIT;
180 }
181 if (hostapdHalDevice->setCountryCode(code, id) != 0) {
182 LOGE("SetCountryCode failed!");
183 return WIFI_HAL_FAILED;
184 }
185 return WIFI_HAL_SUCCESS;
186 }
187
SetHostapdConfig(HostapdConfig * config,int id)188 WifiErrorNo SetHostapdConfig(HostapdConfig *config, int id)
189 {
190 if (config == NULL) {
191 LOGE("SetHostapdConfig config is NULL");
192 return WIFI_HAL_FAILED;
193 }
194 LOGD("SetHostapdConfig()");
195 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
196 if (hostapdHalDevice == NULL) {
197 return WIFI_HAL_HOSTAPD_NOT_INIT;
198 }
199 int ret = hostapdHalDevice->setApInfo(config, id);
200 if (ret != 0) {
201 LOGE("SetApInfo failed!");
202 return WIFI_HAL_FAILED;
203 }
204 ret = hostapdHalDevice->reloadApConfigInfo(id);
205 if (ret != 0) {
206 LOGE("ReloadApConfigInfo failed!");
207 return WIFI_HAL_FAILED;
208 }
209 ret = hostapdHalDevice->disableAp(id);
210 if (ret != 0) {
211 LOGE("DisableAp failed!");
212 return WIFI_HAL_FAILED;
213 }
214 ret = hostapdHalDevice->enableAp(id);
215 if (ret != 0) {
216 LOGE("EnableAp failed!");
217 return WIFI_HAL_FAILED;
218 }
219 LOGD("SetHostapdConfig successfully!");
220 return WIFI_HAL_SUCCESS;
221 }
222
SetMacFilter(const unsigned char * mac,int lenMac,int id)223 WifiErrorNo SetMacFilter(const unsigned char *mac, int lenMac, int id)
224 {
225 if (mac == NULL) {
226 LOGE("SetMacFilter is NULL");
227 return WIFI_HAL_FAILED;
228 }
229 LOGD("SetMacFilter:mac: %{private}s, len_mac: %{public}d", (const char *)mac, lenMac);
230 if (strlen((const char *)mac) != WIFI_MAC_LENGTH || lenMac != WIFI_MAC_LENGTH) {
231 LOGE("Mac size not correct! mac len %{public}zu, request lenMac %{public}d", strlen((const char *)mac), lenMac);
232 return WIFI_HAL_FAILED;
233 }
234 if (CheckMacIsValid((const char *)mac) != 0) {
235 return WIFI_HAL_INPUT_MAC_INVALID;
236 }
237 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
238 if (hostapdHalDevice == NULL) {
239 return WIFI_HAL_HOSTAPD_NOT_INIT;
240 }
241 if (hostapdHalDevice->addBlocklist((const char *)mac, id) != 0) {
242 LOGE("AddBlocklist failed!");
243 return WIFI_HAL_FAILED;
244 }
245 return WIFI_HAL_SUCCESS;
246 }
247
DelMacFilter(const unsigned char * mac,int lenMac,int id)248 WifiErrorNo DelMacFilter(const unsigned char *mac, int lenMac, int id)
249 {
250 if (mac == NULL) {
251 LOGE("DelMacFilter is NULL");
252 return WIFI_HAL_FAILED;
253 }
254 LOGD("DelMacFilter:mac: %{private}s, len_mac: %{public}d", (const char *)mac, lenMac);
255 if (strlen((const char *)mac) != WIFI_MAC_LENGTH || lenMac != WIFI_MAC_LENGTH) {
256 LOGE("Mac size not correct! mac len %{public}zu, request lenMac %{public}d", strlen((const char *)mac), lenMac);
257 return WIFI_HAL_FAILED;
258 }
259 if (CheckMacIsValid((const char *)mac) != 0) {
260 return WIFI_HAL_INPUT_MAC_INVALID;
261 }
262 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
263 if (hostapdHalDevice == NULL) {
264 return WIFI_HAL_HOSTAPD_NOT_INIT;
265 }
266 if (hostapdHalDevice->delBlocklist((const char *)mac, id) != 0) {
267 LOGE("DelBlocklist failed!");
268 return WIFI_HAL_FAILED;
269 }
270 return WIFI_HAL_SUCCESS;
271 }
272
DisassociateSta(const unsigned char * mac,int lenMac,int id)273 WifiErrorNo DisassociateSta(const unsigned char *mac, int lenMac, int id)
274 {
275 if (mac == NULL) {
276 LOGE("DisassociateSta is NULL");
277 return WIFI_HAL_FAILED;
278 }
279 LOGD("DisassociateSta:mac: %{private}s, len_mac: %{public}d", (const char *)mac, lenMac);
280 if (strlen((const char *)mac) != WIFI_MAC_LENGTH || lenMac != WIFI_MAC_LENGTH) {
281 LOGE("Mac size not correct! mac len %{public}zu, request lenMac %{public}d", strlen((const char *)mac), lenMac);
282 return WIFI_HAL_FAILED;
283 }
284 if (CheckMacIsValid((const char *)mac) != 0) {
285 return WIFI_HAL_INPUT_MAC_INVALID;
286 }
287 WifiHostapdHalDevice *hostapdHalDevice = GetWifiHostapdDev(id);
288 if (hostapdHalDevice == NULL) {
289 return WIFI_HAL_HOSTAPD_NOT_INIT;
290 }
291 if (hostapdHalDevice->disConnectedDev((const char *)mac, id) != 0) {
292 LOGE("DisConnectedDev failed!");
293 return WIFI_HAL_FAILED;
294 }
295 return WIFI_HAL_SUCCESS;
296 }
297
GetValidFrequenciesForBand(int32_t band,int * frequencies,int32_t * size,int id)298 WifiErrorNo GetValidFrequenciesForBand(int32_t band, int *frequencies, int32_t *size, int id)
299 {
300 if (frequencies == NULL || size == NULL) {
301 LOGE("%{public}s frequencies or size is null.", __func__);
302 return WIFI_HAL_FAILED;
303 }
304 LOGE("%{public}s func is not support!", __func__);
305 return WIFI_HAL_FAILED;
306 }
307
WifiSetPowerModel(const int mode,int id)308 WifiErrorNo WifiSetPowerModel(const int mode, int id)
309 {
310 LOGE("%{public}s func is not support!", __func__);
311 return WIFI_HAL_FAILED;
312 }
313
WifiGetPowerModel(int * mode,int id)314 WifiErrorNo WifiGetPowerModel(int* mode, int id)
315 {
316 LOGE("%{public}s func is not support!", __func__);
317 return WIFI_HAL_FAILED;
318 }
319