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