1 /*
2 * Copyright (C) 2023 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 #ifdef HDI_WPA_INTERFACE_SUPPORT
17
18 #include <dlfcn.h>
19 #include <dirent.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include "wifi_hdi_wpa_proxy.h"
27 #include "servmgr_hdi.h"
28 #include "devmgr_hdi.h"
29 #include "hdf_remote_service.h"
30 #include "osal_mem.h"
31 #include "wifi_native_define.h"
32 #include "wifi_hdi_wpa_sta_impl.h"
33 #include "wifi_hdi_wpa_p2p_impl.h"
34 #ifndef UT_TEST
35 #include "wifi_log.h"
36 #else
37 #define static
38 #define LOGI(...)
39 #define LOGE(...)
40 #endif
41
42 #undef LOG_TAG
43 #define LOG_TAG "WifiHdiWpaProxy"
44 #define PATH_NUM 2
45 #define BUFF_SIZE 256
46
47 #define MAX_READ_FILE_SIZE 1024
48 #define MAX_FILE_BLOCK_SIZE 1024
49 #define FILE_OPEN_PRIV 0666
50
51 #define CTRL_LEN 128
52 #define IFACENAME_LEN 16
53 #define CFGNAME_LEN 30
54 #define WIFI_MULTI_CMD_MAX_LEN 1024
55
56 #if (AP_NUM > 1)
57 #define WIFI_5G_CFG "hostapd_0.conf"
58 #define WIFI_2G_CFG "hostapd_1.conf"
59 #else
60 #define WPA_HOSTAPD_NAME "hostapd"
61 #define AP_IFNAME "wlan0"
62 #define AP_IFNAME_COEX "wlan1"
63 #define WIFI_DEFAULT_CFG "hostapd.conf"
64 #define WIFI_COEX_CFG "hostapd_coex.conf"
65 #define HOSTAPD_DEFAULT_CFG CONFIG_ROOR_DIR"/wap_supplicant/"WIFI_DEFAULT_CFG
66 #define HOSTAPD_DEFAULT_CFG_COEX CONFIG_ROOR_DIR"/wap_supplicant/"WIFI_COEX_CFG
67 #endif
68
69 const char *HDI_WPA_SERVICE_NAME = "wpa_interface_service";
70 static void ProxyOnRemoteDied(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service);
71 static pthread_mutex_t g_wpaObjMutex = PTHREAD_MUTEX_INITIALIZER;
72 static struct IWpaInterface *g_wpaObj = NULL;
73 static struct HDIDeviceManager *g_devMgr = NULL;
74 static struct HdfRemoteService* g_remote = NULL;
75 static struct HdfDeathRecipient g_recipient = { ProxyOnRemoteDied };
76 static pthread_mutex_t g_ifaceNameMutex = PTHREAD_MUTEX_INITIALIZER;
77 static char g_staIfaceName[STA_INSTANCE_MAX_NUM][IFACENAME_LEN] = {{0}, {0}};
78 static char g_p2pIfaceName[IFACENAME_LEN] = {0};
79
80 const char *HDI_AP_SERVICE_NAME = "hostapd_interface_service";
81 static pthread_mutex_t g_apObjMutex = PTHREAD_MUTEX_INITIALIZER;
82 static struct IHostapdInterface *g_apObj = NULL;
83 static struct HDIDeviceManager *g_apDevMgr = NULL;
84 static pthread_mutex_t g_apIfaceNameMutex = PTHREAD_MUTEX_INITIALIZER;
85 static char g_apIfaceName[IFACENAME_LEN] = {0};
86 static char g_hostapdCfg[CTRL_LEN] = {0};
87 static char g_apCfgName[CFGNAME_LEN] = {0};
88 static int g_id;
89 static int g_execDisable;
90 static bool g_apIsRunning = false;
91 struct IfaceNameInfo {
92 char ifName[BUFF_SIZE];
93 struct IfaceNameInfo* next;
94 };
95 struct IfaceNameInfo* g_IfaceNameInfoHead = NULL;
96
FindifaceName(const char * ifName)97 static bool FindifaceName(const char* ifName)
98 {
99 LOGI("%{public}s enter", __func__);
100 if (ifName == NULL || strlen(ifName) == 0) {
101 LOGI("%{public}s err1", __func__);
102 return true;
103 }
104 struct IfaceNameInfo* currernt = g_IfaceNameInfoHead;
105 while (currernt != NULL) {
106 if (strncmp(currernt->ifName, ifName, strlen(ifName)) == 0) {
107 LOGI("%{public}s out1", __func__);
108 return true;
109 }
110 currernt = currernt->next;
111 }
112 LOGI("%{public}s out", __func__);
113 return false;
114 }
115
AddIfaceName(const char * ifName)116 static void AddIfaceName(const char* ifName)
117 {
118 LOGI("%{public}s enter", __func__);
119 if (ifName == NULL || strlen(ifName) == 0) {
120 LOGI("%{public}s err", __func__);
121 return;
122 }
123 struct IfaceNameInfo* pre = NULL;
124 struct IfaceNameInfo* currernt = g_IfaceNameInfoHead;
125 while (currernt != NULL) {
126 pre = currernt;
127 currernt = currernt->next;
128 }
129 currernt =(struct IfaceNameInfo*) malloc(sizeof(struct IfaceNameInfo));
130 if (currernt == NULL) {
131 LOGI("%{public}s err2", __func__);
132 return;
133 }
134 if (memset_s(currernt->ifName, BUFF_SIZE, 0, strlen(ifName)) != EOK) {
135 free(currernt);
136 currernt = NULL;
137 LOGI("%{public}s err4", __func__);
138 return;
139 }
140 currernt->next = NULL;
141 if (strncpy_s(currernt->ifName, BUFF_SIZE, ifName, strlen(ifName)) != EOK) {
142 free(currernt);
143 currernt = NULL;
144 LOGI("%{public}s err3", __func__);
145 return;
146 }
147 if (pre != NULL) {
148 pre->next = currernt;
149 } else {
150 g_IfaceNameInfoHead = currernt;
151 }
152 LOGI("%{public}s out", __func__);
153 return;
154 }
155
RemoveIfaceName(const char * ifName)156 static void RemoveIfaceName(const char* ifName)
157 {
158 LOGI("%{public}s enter", __func__);
159 if (ifName == NULL || strlen(ifName) == 0) {
160 return;
161 }
162 struct IfaceNameInfo* pre = NULL;
163 struct IfaceNameInfo* currernt = g_IfaceNameInfoHead;
164 while (currernt != NULL) {
165 if (strncmp(currernt->ifName, ifName, BUFF_SIZE) != 0) {
166 pre = currernt;
167 currernt = currernt->next;
168 continue;
169 }
170 if (pre == NULL) {
171 g_IfaceNameInfoHead = currernt->next;
172 } else {
173 pre->next = currernt->next;
174 }
175 free(currernt);
176 currernt = NULL;
177 }
178 LOGI("%{public}s out", __func__);
179 return;
180 }
181
ClearIfaceName(void)182 static void ClearIfaceName(void)
183 {
184 while (g_IfaceNameInfoHead != NULL) {
185 struct IfaceNameInfo* currernt = g_IfaceNameInfoHead;
186 g_IfaceNameInfoHead = g_IfaceNameInfoHead->next;
187 LOGI("ClearIfaceName ifName:%{public}s", currernt->ifName);
188 free(currernt);
189 currernt = NULL;
190 }
191 }
192
193 static void (*mNativeProcessCallback)(int) = NULL;
SetNativeProcessCallback(void (* callback)(int))194 WifiErrorNo SetNativeProcessCallback(void (*callback)(int))
195 {
196 LOGI("%{public}s enter", __func__);
197 mNativeProcessCallback = callback;
198 return WIFI_HAL_OPT_OK;
199 }
200
HdiWpaResetGlobalObj()201 static void HdiWpaResetGlobalObj()
202 {
203 if (IsHdiWpaStopped() == WIFI_HAL_OPT_OK) {
204 LOGE("%{public}s HdiWpa already stopped", __func__);
205 return;
206 }
207 pthread_mutex_lock(&g_wpaObjMutex);
208 IWpaInterfaceReleaseInstance(HDI_WPA_SERVICE_NAME, g_wpaObj, false);
209 g_wpaObj = NULL;
210 if (g_devMgr != NULL) {
211 g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
212 HDIDeviceManagerRelease(g_devMgr);
213 g_devMgr = NULL;
214 }
215 ClearIfaceName();
216 pthread_mutex_unlock(&g_wpaObjMutex);
217 LOGE("%{public}s reset wpa g_wpaObj", __func__);
218 if (mNativeProcessCallback != NULL) {
219 mNativeProcessCallback(WPA_DEATH);
220 }
221 }
RecycleServiceAndRecipient(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)222 static void RecycleServiceAndRecipient(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service)
223 {
224 LOGI("%{public}s enter", __func__);
225 pthread_mutex_lock(&g_wpaObjMutex);
226 if (recipient == NULL || service == NULL || g_remote == NULL) {
227 LOGE("%{public}s input param is null", __func__);
228 pthread_mutex_unlock(&g_wpaObjMutex);
229 return;
230 }
231 HdfRemoteServiceRemoveDeathRecipient(service, recipient);
232 HdfRemoteServiceRecycle(service);
233 g_remote = NULL;
234 pthread_mutex_unlock(&g_wpaObjMutex);
235 }
236
ProxyOnRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)237 static void ProxyOnRemoteDied(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service)
238 {
239 LOGI("%{public}s enter", __func__);
240 RecycleServiceAndRecipient(recipient, service);
241 HdiWpaResetGlobalObj();
242 }
243
RegistHdfDeathCallBack()244 static WifiErrorNo RegistHdfDeathCallBack()
245 {
246 struct HDIServiceManager* serviceMgr = HDIServiceManagerGet();
247 if (serviceMgr == NULL) {
248 LOGE("%{public}s: failed to get HDIServiceManager", __func__);
249 return WIFI_HAL_OPT_FAILED;
250 }
251 struct HdfRemoteService* remote = serviceMgr->GetService(serviceMgr, HDI_WPA_SERVICE_NAME);
252 HDIServiceManagerRelease(serviceMgr);
253 if (remote == NULL) {
254 LOGE("%{public}s: failed to get HdfRemoteService", __func__);
255 return WIFI_HAL_OPT_FAILED;
256 }
257 g_remote = remote;
258 LOGI("%{public}s: success to get HdfRemoteService", __func__);
259 HdfRemoteServiceAddDeathRecipient(remote, &g_recipient);
260 return WIFI_HAL_OPT_OK;
261 }
262
UnRegistHdfDeathCallBack()263 static WifiErrorNo UnRegistHdfDeathCallBack()
264 {
265 if (g_remote == NULL) {
266 LOGE("%{public}s: Invalid remote or recipient", __func__);
267 return WIFI_HAL_OPT_FAILED;
268 }
269 HdfRemoteServiceRemoveDeathRecipient(g_remote, &g_recipient);
270 HdfRemoteServiceRecycle(g_remote);
271 g_remote = NULL;
272 LOGI("%{public}s: Death recipient unregistered", __func__);
273 return WIFI_HAL_OPT_OK;
274 }
275
UnloadDeviceInfo(void)276 static void UnloadDeviceInfo(void)
277 {
278 if (g_devMgr != NULL) {
279 g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
280 HDIDeviceManagerRelease(g_devMgr);
281 g_devMgr = NULL;
282 }
283 }
284
HdiWpaStart()285 WifiErrorNo HdiWpaStart()
286 {
287 LOGI("HdiWpaStart start...");
288 pthread_mutex_lock(&g_wpaObjMutex);
289 if (g_wpaObj != NULL && g_devMgr != NULL) {
290 pthread_mutex_unlock(&g_wpaObjMutex);
291 LOGI("%{public}s wpa hdi already started", __func__);
292 return WIFI_HAL_OPT_OK;
293 }
294 g_devMgr = HDIDeviceManagerGet();
295 if (g_devMgr == NULL) {
296 pthread_mutex_unlock(&g_wpaObjMutex);
297 LOGE("%{public}s HDIDeviceManagerGet failed", __func__);
298 return WIFI_HAL_OPT_FAILED;
299 }
300 HDF_STATUS retDevice = g_devMgr->LoadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
301 if (retDevice == HDF_ERR_DEVICE_BUSY) {
302 LOGE("%{public}s LoadDevice busy: %{public}d", __func__, retDevice);
303 } else if (retDevice != HDF_SUCCESS) {
304 HDIDeviceManagerRelease(g_devMgr);
305 g_devMgr = NULL;
306 pthread_mutex_unlock(&g_wpaObjMutex);
307 LOGE("%{public}s LoadDevice failed", __func__);
308 return WIFI_HAL_OPT_FAILED;
309 }
310 g_wpaObj = IWpaInterfaceGetInstance(HDI_WPA_SERVICE_NAME, false);
311 if (g_wpaObj == NULL) {
312 UnloadDeviceInfo();
313 pthread_mutex_unlock(&g_wpaObjMutex);
314 LOGE("%{public}s WpaInterfaceGetInstance failed", __func__);
315 return WIFI_HAL_OPT_FAILED;
316 }
317 int32_t ret = g_wpaObj->Start(g_wpaObj);
318 if (ret != HDF_SUCCESS) {
319 LOGE("%{public}s Start failed: %{public}d", __func__, ret);
320 IWpaInterfaceReleaseInstance(HDI_WPA_SERVICE_NAME, g_wpaObj, false);
321 g_wpaObj = NULL;
322 UnloadDeviceInfo();
323 pthread_mutex_unlock(&g_wpaObjMutex);
324 return WIFI_HAL_OPT_FAILED;
325 }
326
327 RegistHdfDeathCallBack();
328 pthread_mutex_unlock(&g_wpaObjMutex);
329 LOGI("HdiWpaStart start success!");
330 return WIFI_HAL_OPT_OK;
331 }
332
HdiWpaStop()333 WifiErrorNo HdiWpaStop()
334 {
335 LOGI("HdiWpaStop stop...");
336 pthread_mutex_lock(&g_wpaObjMutex);
337 if (g_wpaObj == NULL) {
338 pthread_mutex_unlock(&g_wpaObjMutex);
339 LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
340 return WIFI_HAL_OPT_OK;
341 }
342
343 int32_t ret = g_wpaObj->Stop(g_wpaObj);
344 if (ret != HDF_SUCCESS) {
345 LOGE("%{public}s Stop failed: %{public}d", __func__, ret);
346 }
347 IWpaInterfaceReleaseInstance(HDI_WPA_SERVICE_NAME, g_wpaObj, false);
348 g_wpaObj = NULL;
349 if (g_devMgr != NULL) {
350 if (UnRegistHdfDeathCallBack() != WIFI_HAL_OPT_OK) {
351 LOGE("%{public}s UnRegistHdfDeathCallBack failed", __func__);
352 }
353 g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
354 HDIDeviceManagerRelease(g_devMgr);
355 g_devMgr = NULL;
356 }
357 ClearIfaceName();
358 pthread_mutex_unlock(&g_wpaObjMutex);
359 LOGI("HdiWpaStart stop success!");
360 return WIFI_HAL_OPT_OK;
361 }
362
IsHdiWpaStopped()363 WifiErrorNo IsHdiWpaStopped()
364 {
365 pthread_mutex_lock(&g_wpaObjMutex);
366 if (g_wpaObj == NULL && g_devMgr == NULL) {
367 LOGI("HdiWpa already stopped");
368 pthread_mutex_unlock(&g_wpaObjMutex);
369 return WIFI_HAL_OPT_OK;
370 }
371
372 pthread_mutex_unlock(&g_wpaObjMutex);
373 return WIFI_HAL_OPT_FAILED;
374 }
375
HdiAddWpaIface(const char * ifName,const char * confName)376 WifiErrorNo HdiAddWpaIface(const char *ifName, const char *confName)
377 {
378 pthread_mutex_lock(&g_wpaObjMutex);
379 if (ifName == NULL || confName == NULL || strlen(ifName) == 0) {
380 pthread_mutex_unlock(&g_wpaObjMutex);
381 LOGE("HdiAddWpaIface: invalid parameter!");
382 return WIFI_HAL_OPT_INVALID_PARAM;
383 }
384
385 if (g_wpaObj == NULL) {
386 pthread_mutex_unlock(&g_wpaObjMutex);
387 LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
388 return WIFI_HAL_OPT_FAILED;
389 }
390 LOGI("HdiAddWpaIface ifName:%{public}s, confName:%{public}s", ifName, confName);
391 if (!FindifaceName(ifName)) {
392 int32_t ret = g_wpaObj->AddWpaIface(g_wpaObj, ifName, confName);
393 if (ret != HDF_SUCCESS) {
394 LOGE("%{public}s AddWpaIface failed: %{public}d", __func__, ret);
395 pthread_mutex_unlock(&g_wpaObjMutex);
396 return WIFI_HAL_OPT_FAILED;
397 }
398 AddIfaceName(ifName);
399 }
400 pthread_mutex_unlock(&g_wpaObjMutex);
401 LOGI("%{public}s AddWpaIface success!", __func__);
402 return WIFI_HAL_OPT_OK;
403 }
404
HdiRemoveWpaIface(const char * ifName)405 WifiErrorNo HdiRemoveWpaIface(const char *ifName)
406 {
407 pthread_mutex_lock(&g_wpaObjMutex);
408 if (ifName == NULL) {
409 pthread_mutex_unlock(&g_wpaObjMutex);
410 LOGE("HdiRemoveWpaIface: invalid parameter!");
411 return WIFI_HAL_OPT_INVALID_PARAM;
412 }
413
414 if (g_wpaObj == NULL) {
415 pthread_mutex_unlock(&g_wpaObjMutex);
416 LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
417 return WIFI_HAL_OPT_OK;
418 }
419
420 LOGI("HdiRemoveWpaIface ifName:%{public}s", ifName);
421 if (FindifaceName(ifName)) {
422 int32_t ret = g_wpaObj->RemoveWpaIface(g_wpaObj, ifName);
423 if (ret != HDF_SUCCESS) {
424 LOGE("%{public}s RemoveWpaIface failed: %{public}d", __func__, ret);
425 pthread_mutex_unlock(&g_wpaObjMutex);
426 return WIFI_HAL_OPT_FAILED;
427 }
428 RemoveIfaceName(ifName);
429 }
430 if (strncmp(ifName, "p2p", strlen("p2p")) == 0) {
431 ReleaseP2pCallback();
432 }
433 if (strncmp(ifName, "wlan", strlen("wlan")) == 0) {
434 ReleaseStaCallback(ifName);
435 }
436 pthread_mutex_unlock(&g_wpaObjMutex);
437 LOGI("%{public}s RemoveWpaIface success!", __func__);
438 return WIFI_HAL_OPT_OK;
439 }
440
GetWpaInterface()441 struct IWpaInterface* GetWpaInterface()
442 {
443 struct IWpaInterface *wpaObj = NULL;
444 wpaObj = g_wpaObj;
445 return wpaObj;
446 }
447
GetWpaObjMutex(void)448 pthread_mutex_t* GetWpaObjMutex(void)
449 {
450 return &g_wpaObjMutex;
451 }
452
SetHdiStaIfaceName(const char * ifaceName,int instId)453 WifiErrorNo SetHdiStaIfaceName(const char *ifaceName, int instId)
454 {
455 pthread_mutex_lock(&g_ifaceNameMutex);
456 LOGI("SetHdiStaIfaceName enter instId = %{public}d", instId);
457 if (ifaceName == NULL || instId >= STA_INSTANCE_MAX_NUM) {
458 pthread_mutex_unlock(&g_ifaceNameMutex);
459 return WIFI_HAL_OPT_INVALID_PARAM;
460 }
461
462 if (memset_s(g_staIfaceName[instId], IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
463 pthread_mutex_unlock(&g_ifaceNameMutex);
464 return WIFI_HAL_OPT_FAILED;
465 }
466
467 if (strcpy_s(g_staIfaceName[instId], IFACENAME_LEN, ifaceName) != EOK) {
468 pthread_mutex_unlock(&g_ifaceNameMutex);
469 return WIFI_HAL_OPT_FAILED;
470 }
471
472 LOGI("SetHdiStaIfaceName, g_staIfaceName:%{public}s, instId = %{public}d", g_staIfaceName[instId], instId);
473 pthread_mutex_unlock(&g_ifaceNameMutex);
474 return WIFI_HAL_OPT_OK;
475 }
476
GetHdiStaIfaceName(int instId)477 const char *GetHdiStaIfaceName(int instId)
478 {
479 LOGI("GetHdiStaIfaceName enter instId = %{public}d", instId);
480 const char *ifaceName = NULL;
481 if (instId >= STA_INSTANCE_MAX_NUM || instId < 0) {
482 LOGE("invalid param instId = %{public}d", instId);
483 return ifaceName;
484 }
485
486 pthread_mutex_lock(&g_ifaceNameMutex);
487 ifaceName = g_staIfaceName[instId];
488 pthread_mutex_unlock(&g_ifaceNameMutex);
489 LOGI("GetHdiStaIfaceName enter ifaceName = %{public}s", ifaceName);
490 return ifaceName;
491 }
492
ClearHdiStaIfaceName(int instId)493 void ClearHdiStaIfaceName(int instId)
494 {
495 pthread_mutex_lock(&g_ifaceNameMutex);
496 if (memset_s(g_staIfaceName[instId], IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
497 pthread_mutex_unlock(&g_ifaceNameMutex);
498 return;
499 }
500 pthread_mutex_unlock(&g_ifaceNameMutex);
501 }
502
SetHdiP2pIfaceName(const char * ifaceName)503 WifiErrorNo SetHdiP2pIfaceName(const char *ifaceName)
504 {
505 pthread_mutex_lock(&g_ifaceNameMutex);
506 if (ifaceName == NULL) {
507 pthread_mutex_unlock(&g_ifaceNameMutex);
508 return WIFI_HAL_OPT_INVALID_PARAM;
509 }
510
511 if (memset_s(g_p2pIfaceName, IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
512 pthread_mutex_unlock(&g_ifaceNameMutex);
513 return WIFI_HAL_OPT_FAILED;
514 }
515
516 if (strcpy_s(g_p2pIfaceName, IFACENAME_LEN, ifaceName) != EOK) {
517 pthread_mutex_unlock(&g_ifaceNameMutex);
518 return WIFI_HAL_OPT_FAILED;
519 }
520
521 LOGI("SetHdiP2pIfaceName, g_p2pIfaceName:%{public}s", g_p2pIfaceName);
522 pthread_mutex_unlock(&g_ifaceNameMutex);
523 return WIFI_HAL_OPT_OK;
524 }
525
GetHdiP2pIfaceName()526 const char *GetHdiP2pIfaceName()
527 {
528 const char *ifaceName = NULL;
529 pthread_mutex_lock(&g_ifaceNameMutex);
530 ifaceName = g_p2pIfaceName;
531 pthread_mutex_unlock(&g_ifaceNameMutex);
532 return ifaceName;
533 }
534
CopyUserFile(const char * srcFilePath,const char * destFilePath)535 WifiErrorNo CopyUserFile(const char *srcFilePath, const char* destFilePath)
536 {
537 LOGI("Execute CopyUserFile enter");
538 if (srcFilePath == NULL || destFilePath == NULL) {
539 LOGE("CopyUserFile() srcFilePath or destFilePath is nullptr!");
540 return WIFI_HAL_OPT_FAILED;
541 }
542 int srcFd = -1;
543 int destFd = -1;
544 do {
545 if ((srcFd = open(srcFilePath, O_RDONLY)) < 0) {
546 LOGE("CopyUserFile() failed, open srcFilePath:%{public}s error!", srcFilePath);
547 break;
548 }
549 if ((destFd = open(destFilePath, O_RDWR | O_CREAT | O_TRUNC, FILE_OPEN_PRIV))< 0) {
550 LOGE("CopyUserFile() failed, open destFilePath:%{public}s error!", destFilePath);
551 break;
552 }
553 ssize_t bytes;
554 lseek(srcFd, 0, SEEK_SET);
555 char buf[MAX_READ_FILE_SIZE] = {0};
556 for (int i = 0; i < MAX_FILE_BLOCK_SIZE; i++) {
557 if (memset_s(buf, MAX_READ_FILE_SIZE, 0, MAX_READ_FILE_SIZE) != WIFI_HAL_OPT_OK) {
558 break;
559 }
560 if ((bytes = read(srcFd, buf, MAX_READ_FILE_SIZE-1)) < 0) {
561 LOGE("CopyUserFile() failed, read srcFilePath:%{public}s error!", srcFilePath);
562 break;
563 }
564 if (write(destFd, buf, bytes) < 0) {
565 LOGE("CopyUserFile() failed, write destFilePath:%{public}s error!", destFilePath);
566 }
567 }
568 } while (0);
569 if (srcFd>=0) {
570 close(srcFd);
571 }
572
573 if (destFd>=0) {
574 close(destFd);
575 }
576 LOGI("CopyUserFile() copy file succeed.");
577 return WIFI_HAL_OPT_OK;
578 }
579
CopyConfigFile(const char * configName)580 WifiErrorNo CopyConfigFile(const char* configName)
581 {
582 if (configName == NULL || strlen(configName) == 0) {
583 LOGE("Copy config file failed:is null");
584 return WIFI_HAL_OPT_FAILED;
585 }
586 char path[PATH_NUM][BUFF_SIZE] = {"/system/etc/wifi/", "/vendor/etc/wifi/"};
587 for (int i = 0; i != PATH_NUM; ++i) {
588 if (strcat_s(path[i], sizeof(path[i]), configName) != EOK) {
589 LOGE("strcat_s failed.");
590 return WIFI_HAL_OPT_FAILED;
591 }
592 if (access(path[i], F_OK) != -1) {
593 char destFilePath[BUFF_SIZE] = {0};
594 if (snprintf_s(destFilePath, sizeof(destFilePath), sizeof(destFilePath) - 1,
595 "%s/wpa_supplicant/%s", CONFIG_ROOR_DIR, configName) < 0) {
596 LOGE("snprintf_s destFilePath failed.");
597 return WIFI_HAL_OPT_FAILED;
598 }
599 return CopyUserFile(path[i], destFilePath);
600 }
601 }
602 LOGE("Copy config file failed: %{public}s", configName);
603 return WIFI_HAL_OPT_FAILED;
604 }
605
HdiApResetGlobalObj()606 static void HdiApResetGlobalObj()
607 {
608 LOGI("%{public}s try reset ap", __func__);
609 if (IsHdiApStopped() == WIFI_HAL_OPT_OK) {
610 LOGI("%{public}s HdiAp already stopped", __func__);
611 return;
612 }
613 pthread_mutex_lock(&g_apObjMutex);
614 g_apIsRunning = false;
615 IHostapdInterfaceReleaseInstance(HDI_AP_SERVICE_NAME, g_apObj, false);
616 g_apObj = NULL;
617 if (g_apDevMgr != NULL) {
618 g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
619 HDIDeviceManagerRelease(g_apDevMgr);
620 g_apDevMgr = NULL;
621 }
622 pthread_mutex_unlock(&g_apObjMutex);
623 if (mNativeProcessCallback != NULL) {
624 mNativeProcessCallback(AP_DEATH);
625 }
626 }
627
ProxyOnApRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)628 static void ProxyOnApRemoteDied(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service)
629 {
630 LOGI("%{public}s enter", __func__);
631 if (recipient == NULL || service == NULL) {
632 LOGE("%{public}s input param is null", __func__);
633 HdiApResetGlobalObj();
634 return;
635 }
636 HdfRemoteServiceRemoveDeathRecipient(service, recipient);
637 HdfRemoteServiceRecycle(service);
638 if (recipient == NULL) {
639 LOGE("%{public}s param recipient is null", __func__);
640 HdiApResetGlobalObj();
641 return;
642 }
643 OsalMemFree(recipient);
644 recipient = NULL;
645 HdiApResetGlobalObj();
646 }
647
RegistHdfApDeathCallBack()648 static WifiErrorNo RegistHdfApDeathCallBack()
649 {
650 struct HDIServiceManager* serviceMgr = HDIServiceManagerGet();
651 if (serviceMgr == NULL) {
652 LOGE("%{public}s: failed to get HDIServiceManager", __func__);
653 return WIFI_HAL_OPT_FAILED;
654 }
655 struct HdfRemoteService* remote = serviceMgr->GetService(serviceMgr, HDI_AP_SERVICE_NAME);
656 HDIServiceManagerRelease(serviceMgr);
657 if (remote == NULL) {
658 LOGE("%{public}s: failed to get HdfRemoteService", __func__);
659 return WIFI_HAL_OPT_FAILED;
660 }
661 LOGI("%{public}s: success to get HdfRemoteService", __func__);
662 struct HdfDeathRecipient* recipient = (struct HdfDeathRecipient*)OsalMemCalloc(sizeof(struct HdfDeathRecipient));
663 if (recipient == NULL) {
664 LOGE("%{public}s: OsalMemCalloc is failed", __func__);
665 return WIFI_HAL_OPT_FAILED;
666 }
667 recipient->OnRemoteDied = ProxyOnApRemoteDied;
668 HdfRemoteServiceAddDeathRecipient(remote, recipient);
669 return WIFI_HAL_OPT_OK;
670 }
671
GetApInstance()672 static WifiErrorNo GetApInstance()
673 {
674 g_apDevMgr = HDIDeviceManagerGet();
675 if (g_apDevMgr == NULL) {
676 LOGE("%{public}s HDIDeviceManagerGet failed", __func__);
677 return WIFI_HAL_OPT_FAILED;
678 }
679 HDF_STATUS retDevice = g_apDevMgr->LoadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME) ;
680 if (retDevice == HDF_ERR_DEVICE_BUSY) {
681 LOGE("%{public}s LoadDevice busy: %{public}d", __func__, retDevice);
682 } else if (retDevice != HDF_SUCCESS) {
683 HDIDeviceManagerRelease(g_apDevMgr);
684 g_apDevMgr = NULL;
685 LOGE("%{public}s LoadDevice failed", __func__);
686 return WIFI_HAL_OPT_FAILED;
687 }
688 g_apObj = IHostapdInterfaceGetInstance(HDI_AP_SERVICE_NAME, false);
689 if (g_apObj == NULL && g_apDevMgr != NULL) {
690 g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
691 HDIDeviceManagerRelease(g_apDevMgr);
692 g_apDevMgr = NULL;
693 LOGE("%{public}s HostapdInterfaceGetInstance failed", __func__);
694 return WIFI_HAL_OPT_FAILED;
695 }
696 return WIFI_HAL_OPT_OK;
697 }
698
StartApHdi(int id,const char * ifaceName)699 static WifiErrorNo StartApHdi(int id, const char *ifaceName)
700 {
701 if (g_apObj == NULL) {
702 LOGE("%{public}s Pointer g_apObj is NULL", __func__);
703 return WIFI_HAL_OPT_FAILED;
704 }
705 int32_t ret = g_apObj->StartApWithCmd(g_apObj, ifaceName, id);
706 if (ret != HDF_SUCCESS) {
707 LOGE("%{public}s Start failed: %{public}d", __func__, ret);
708 IHostapdInterfaceGetInstance(HDI_AP_SERVICE_NAME, false);
709 g_apObj = NULL;
710 if (g_apDevMgr != NULL) {
711 g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
712 HDIDeviceManagerRelease(g_apDevMgr);
713 g_apDevMgr = NULL;
714 }
715 return WIFI_HAL_OPT_FAILED;
716 }
717 return WIFI_HAL_OPT_OK;
718 }
719
HdiApStart(int id,const char * ifaceName)720 WifiErrorNo HdiApStart(int id, const char *ifaceName)
721 {
722 LOGI("HdiApStart start...");
723 pthread_mutex_lock(&g_apObjMutex);
724
725 g_id = id;
726 WifiErrorNo result = WIFI_HAL_OPT_FAILED;
727 do {
728 #if (AP_NUM > 1)
729 result = CopyConfigFile(WIFI_5G_CFG);
730 if (result != WIFI_HAL_OPT_OK) {
731 break;
732 }
733 result = CopyConfigFile(WIFI_2G_CFG);
734 #else
735 result = CopyConfigFile(WIFI_DEFAULT_CFG);
736 #endif
737 if (result != WIFI_HAL_OPT_OK) {
738 break;
739 }
740 result = GetApInstance();
741 if (result != WIFI_HAL_OPT_OK) {
742 break;
743 }
744 result = StartApHdi(id, ifaceName);
745 if (result != WIFI_HAL_OPT_OK) {
746 break;
747 }
748 result = RegistHdfApDeathCallBack();
749 if (result != WIFI_HAL_OPT_OK) {
750 break;
751 }
752 g_apIsRunning = true;
753 LOGI("HdiApStart start success");
754 } while (0);
755 pthread_mutex_unlock(&g_apObjMutex);
756 return result;
757 }
758
HdiApStop(int id)759 WifiErrorNo HdiApStop(int id)
760 {
761 LOGI("HdiApStop stop...");
762 pthread_mutex_lock(&g_apObjMutex);
763
764 int32_t ret;
765 if (g_apObj == NULL) {
766 LOGE("%{public}s, g_apObj is NULL", __func__);
767 pthread_mutex_unlock(&g_apObjMutex);
768 return WIFI_HAL_OPT_OK;
769 }
770 ret = g_apObj->DisableAp(g_apObj, g_apIfaceName, id);
771 ret = g_apObj->StopAp(g_apObj);
772 if (ret != HDF_SUCCESS) {
773 LOGE("%{public}s Stop failed: %{public}d", __func__, ret);
774 }
775 IHostapdInterfaceReleaseInstance(HDI_AP_SERVICE_NAME, g_apObj, false);
776 g_apObj = NULL;
777 if (g_apDevMgr != NULL) {
778 g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
779 HDIDeviceManagerRelease(g_apDevMgr);
780 g_apDevMgr = NULL;
781 }
782 g_apIsRunning = false;
783 pthread_mutex_unlock(&g_apObjMutex);
784 LOGI("HdiApStop stop success");
785 return WIFI_HAL_OPT_OK;
786 }
787
IsHdiApStopped()788 WifiErrorNo IsHdiApStopped()
789 {
790 pthread_mutex_lock(&g_apObjMutex);
791 if (g_apIsRunning == false && g_apObj == NULL && g_apDevMgr == NULL) {
792 LOGI("IsHdiApStopped, HdiAp already stopped");
793 pthread_mutex_unlock(&g_apObjMutex);
794 return WIFI_HAL_OPT_OK;
795 }
796
797 pthread_mutex_unlock(&g_apObjMutex);
798 return WIFI_HAL_OPT_FAILED;
799 }
800
GetApInterface()801 struct IHostapdInterface* GetApInterface()
802 {
803 struct IHostapdInterface *apObj = NULL;
804 pthread_mutex_lock(&g_apObjMutex);
805 apObj = g_apObj;
806 pthread_mutex_unlock(&g_apObjMutex);
807 return apObj;
808 }
809
SetHdiApIfaceName(const char * ifaceName)810 WifiErrorNo SetHdiApIfaceName(const char *ifaceName)
811 {
812 pthread_mutex_lock(&g_apIfaceNameMutex);
813 if (ifaceName == NULL) {
814 pthread_mutex_unlock(&g_apIfaceNameMutex);
815 return WIFI_HAL_OPT_INVALID_PARAM;
816 }
817
818 if (memset_s(g_apCfgName, CFGNAME_LEN, 0, CFGNAME_LEN) != EOK
819 || memset_s(g_apIfaceName, IFACENAME_LEN, 0, IFACENAME_LEN) != EOK
820 || memset_s(g_hostapdCfg, CTRL_LEN, 0, CTRL_LEN) != EOK) {
821 pthread_mutex_unlock(&g_apIfaceNameMutex);
822 return WIFI_HAL_OPT_FAILED;
823 }
824
825 if (strncmp(ifaceName, AP_IFNAME_COEX, strlen(AP_IFNAME_COEX)) == 0) {
826 if (strcpy_s(g_apCfgName, CFGNAME_LEN, WIFI_COEX_CFG) != EOK
827 || strcpy_s(g_apIfaceName, IFACENAME_LEN, AP_IFNAME_COEX) != EOK
828 || strcpy_s(g_hostapdCfg, CTRL_LEN, HOSTAPD_DEFAULT_CFG_COEX) != EOK) {
829 pthread_mutex_unlock(&g_apIfaceNameMutex);
830 return WIFI_HAL_OPT_FAILED;
831 }
832 } else {
833 if (strcpy_s(g_apCfgName, CFGNAME_LEN, WIFI_DEFAULT_CFG) != EOK
834 || strcpy_s(g_apIfaceName, IFACENAME_LEN, AP_IFNAME) != EOK
835 || strcpy_s(g_hostapdCfg, CTRL_LEN, HOSTAPD_DEFAULT_CFG) != EOK) {
836 pthread_mutex_unlock(&g_apIfaceNameMutex);
837 return WIFI_HAL_OPT_FAILED;
838 }
839 }
840
841 LOGI("SetHdiApIfaceName, g_apIfaceName:%{public}s", g_apIfaceName);
842 pthread_mutex_unlock(&g_apIfaceNameMutex);
843 return WIFI_HAL_OPT_OK;
844 }
845
GetHdiApIfaceName()846 const char *GetHdiApIfaceName()
847 {
848 const char *ifaceName = NULL;
849 pthread_mutex_lock(&g_apIfaceNameMutex);
850 ifaceName = g_apIfaceName;
851 pthread_mutex_unlock(&g_apIfaceNameMutex);
852 return ifaceName;
853 }
854
SetExecDisable(int execDisable)855 void SetExecDisable(int execDisable)
856 {
857 g_execDisable = execDisable;
858 }
859
GetExecDisable()860 int GetExecDisable()
861 {
862 return g_execDisable;
863 }
864
865 #endif