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         HDIDeviceManagerRelease(g_devMgr);
208         g_devMgr = NULL;
209     }
210     ClearIfaceName();
211     pthread_mutex_unlock(&g_wpaObjMutex);
212     LOGE("%{public}s reset wpa g_wpaObj", __func__);
213     if (mNativeProcessCallback != NULL) {
214         mNativeProcessCallback(WPA_DEATH);
215     }
216 }
217 
ProxyOnRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)218 static void ProxyOnRemoteDied(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service)
219 {
220     LOGI("%{public}s enter", __func__);
221     if (recipient == NULL || service == NULL) {
222         LOGE("%{public}s input param is null", __func__);
223         HdiWpaResetGlobalObj();
224         return;
225     }
226     HdfRemoteServiceRemoveDeathRecipient(service, recipient);
227     HdfRemoteServiceRecycle(service);
228     if (recipient == NULL) {
229         LOGE("%{public}s param recipient is null", __func__);
230         HdiWpaResetGlobalObj();
231         return;
232     }
233     OsalMemFree(recipient);
234     recipient = NULL;
235     HdiWpaResetGlobalObj();
236 }
237 
RegistHdfDeathCallBack()238 static WifiErrorNo RegistHdfDeathCallBack()
239 {
240     struct HDIServiceManager* serviceMgr = HDIServiceManagerGet();
241     if (serviceMgr == NULL) {
242         LOGE("%{public}s: failed to get HDIServiceManager", __func__);
243         return WIFI_HAL_OPT_FAILED;
244     }
245     struct HdfRemoteService* remote = serviceMgr->GetService(serviceMgr, HDI_WPA_SERVICE_NAME);
246     HDIServiceManagerRelease(serviceMgr);
247     if (remote == NULL) {
248         LOGE("%{public}s: failed to get HdfRemoteService", __func__);
249         return WIFI_HAL_OPT_FAILED;
250     }
251     LOGI("%{public}s: success to get HdfRemoteService", __func__);
252     struct HdfDeathRecipient* recipient = (struct HdfDeathRecipient*)OsalMemCalloc(sizeof(struct HdfDeathRecipient));
253     if (recipient == NULL) {
254         LOGE("%{public}s: OsalMemCalloc is failed", __func__);
255         return WIFI_HAL_OPT_FAILED;
256     }
257     recipient->OnRemoteDied = ProxyOnRemoteDied;
258     HdfRemoteServiceAddDeathRecipient(remote, recipient);
259     return WIFI_HAL_OPT_OK;
260 }
261 
HdiWpaStart()262 WifiErrorNo HdiWpaStart()
263 {
264     LOGI("HdiWpaStart start...");
265     pthread_mutex_lock(&g_wpaObjMutex);
266     if (g_wpaObj != NULL && g_devMgr != NULL) {
267         pthread_mutex_unlock(&g_wpaObjMutex);
268         LOGI("%{public}s wpa hdi already started", __func__);
269         return WIFI_HAL_OPT_OK;
270     }
271     g_devMgr = HDIDeviceManagerGet();
272     if (g_devMgr == NULL) {
273         pthread_mutex_unlock(&g_wpaObjMutex);
274         LOGE("%{public}s HDIDeviceManagerGet failed", __func__);
275         return WIFI_HAL_OPT_FAILED;
276     }
277     HDF_STATUS retDevice = g_devMgr->LoadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
278     if (retDevice == HDF_ERR_DEVICE_BUSY) {
279         LOGE("%{public}s LoadDevice busy: %{public}d", __func__, retDevice);
280     } else if (retDevice != HDF_SUCCESS) {
281         HDIDeviceManagerRelease(g_devMgr);
282         g_devMgr = NULL;
283         pthread_mutex_unlock(&g_wpaObjMutex);
284         LOGE("%{public}s LoadDevice failed", __func__);
285         return WIFI_HAL_OPT_FAILED;
286     }
287     g_wpaObj = IWpaInterfaceGetInstance(HDI_WPA_SERVICE_NAME, false);
288     if (g_wpaObj == NULL) {
289         if (g_devMgr != NULL) {
290             g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
291             HDIDeviceManagerRelease(g_devMgr);
292             g_devMgr = NULL;
293         }
294         pthread_mutex_unlock(&g_wpaObjMutex);
295         LOGE("%{public}s WpaInterfaceGetInstance failed", __func__);
296         return WIFI_HAL_OPT_FAILED;
297     }
298 
299     int32_t ret = g_wpaObj->Start(g_wpaObj);
300     if (ret != HDF_SUCCESS) {
301         LOGE("%{public}s Start failed: %{public}d", __func__, ret);
302         IWpaInterfaceReleaseInstance(HDI_WPA_SERVICE_NAME, g_wpaObj, false);
303         g_wpaObj = NULL;
304         if (g_devMgr != NULL) {
305             g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
306             HDIDeviceManagerRelease(g_devMgr);
307             g_devMgr = NULL;
308         }
309         pthread_mutex_unlock(&g_wpaObjMutex);
310         return WIFI_HAL_OPT_FAILED;
311     }
312     RegistHdfDeathCallBack();
313     pthread_mutex_unlock(&g_wpaObjMutex);
314     LOGI("HdiWpaStart start success!");
315     return WIFI_HAL_OPT_OK;
316 }
317 
HdiWpaStop()318 WifiErrorNo HdiWpaStop()
319 {
320     LOGI("HdiWpaStop stop...");
321     pthread_mutex_lock(&g_wpaObjMutex);
322     if (g_wpaObj == NULL) {
323         pthread_mutex_unlock(&g_wpaObjMutex);
324         LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
325         return WIFI_HAL_OPT_FAILED;
326     }
327 
328     int32_t ret = g_wpaObj->Stop(g_wpaObj);
329     if (ret != HDF_SUCCESS) {
330         LOGE("%{public}s Stop failed: %{public}d", __func__, ret);
331     }
332     IWpaInterfaceReleaseInstance(HDI_WPA_SERVICE_NAME, g_wpaObj, false);
333     g_wpaObj = NULL;
334     if (g_devMgr != NULL) {
335         g_devMgr->UnloadDevice(g_devMgr, HDI_WPA_SERVICE_NAME);
336         HDIDeviceManagerRelease(g_devMgr);
337         g_devMgr = NULL;
338     }
339     ClearIfaceName();
340     pthread_mutex_unlock(&g_wpaObjMutex);
341     LOGI("HdiWpaStart stop success!");
342     return WIFI_HAL_OPT_OK;
343 }
344 
IsHdiWpaStopped()345 WifiErrorNo IsHdiWpaStopped()
346 {
347     pthread_mutex_lock(&g_wpaObjMutex);
348     if (g_wpaObj == NULL && g_devMgr == NULL) {
349         LOGI("HdiWpa already stopped");
350         pthread_mutex_unlock(&g_wpaObjMutex);
351         return WIFI_HAL_OPT_OK;
352     }
353 
354     pthread_mutex_unlock(&g_wpaObjMutex);
355     return WIFI_HAL_OPT_FAILED;
356 }
357 
HdiAddWpaIface(const char * ifName,const char * confName)358 WifiErrorNo HdiAddWpaIface(const char *ifName, const char *confName)
359 {
360     pthread_mutex_lock(&g_wpaObjMutex);
361     if (ifName == NULL || confName == NULL || strlen(ifName) == 0) {
362         pthread_mutex_unlock(&g_wpaObjMutex);
363         LOGE("HdiAddWpaIface: invalid parameter!");
364         return WIFI_HAL_OPT_INVALID_PARAM;
365     }
366 
367     if (g_wpaObj == NULL) {
368         pthread_mutex_unlock(&g_wpaObjMutex);
369         LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
370         return WIFI_HAL_OPT_FAILED;
371     }
372     LOGI("HdiAddWpaIface ifName:%{public}s, confName:%{public}s", ifName, confName);
373     if (!FindifaceName(ifName)) {
374         int32_t ret = g_wpaObj->AddWpaIface(g_wpaObj, ifName, confName);
375         if (ret != HDF_SUCCESS) {
376             LOGE("%{public}s AddWpaIface failed: %{public}d", __func__, ret);
377             pthread_mutex_unlock(&g_wpaObjMutex);
378             return WIFI_HAL_OPT_FAILED;
379         }
380         AddIfaceName(ifName);
381     }
382     pthread_mutex_unlock(&g_wpaObjMutex);
383     LOGI("%{public}s AddWpaIface success!", __func__);
384     return WIFI_HAL_OPT_OK;
385 }
386 
HdiRemoveWpaIface(const char * ifName)387 WifiErrorNo HdiRemoveWpaIface(const char *ifName)
388 {
389     pthread_mutex_lock(&g_wpaObjMutex);
390     if (ifName == NULL) {
391         pthread_mutex_unlock(&g_wpaObjMutex);
392         LOGE("HdiRemoveWpaIface: invalid parameter!");
393         return WIFI_HAL_OPT_INVALID_PARAM;
394     }
395 
396     if (g_wpaObj == NULL) {
397         pthread_mutex_unlock(&g_wpaObjMutex);
398         LOGE("%{public}s g_wpaObj is NULL or wpa hdi already stopped", __func__);
399         return WIFI_HAL_OPT_OK;
400     }
401 
402     LOGI("HdiRemoveWpaIface ifName:%{public}s", ifName);
403     if (FindifaceName(ifName)) {
404         int32_t ret = g_wpaObj->RemoveWpaIface(g_wpaObj, ifName);
405         if (ret != HDF_SUCCESS) {
406             LOGE("%{public}s RemoveWpaIface failed: %{public}d", __func__, ret);
407             pthread_mutex_unlock(&g_wpaObjMutex);
408             return WIFI_HAL_OPT_FAILED;
409         }
410         RemoveIfaceName(ifName);
411     }
412     pthread_mutex_unlock(&g_wpaObjMutex);
413     LOGI("%{public}s RemoveWpaIface success!", __func__);
414     return WIFI_HAL_OPT_OK;
415 }
416 
GetWpaInterface()417 struct IWpaInterface* GetWpaInterface()
418 {
419     struct IWpaInterface *wpaObj = NULL;
420     wpaObj = g_wpaObj;
421     return wpaObj;
422 }
423 
GetWpaObjMutex(void)424 pthread_mutex_t* GetWpaObjMutex(void)
425 {
426     return &g_wpaObjMutex;
427 }
428 
SetHdiStaIfaceName(const char * ifaceName,int instId)429 WifiErrorNo SetHdiStaIfaceName(const char *ifaceName, int instId)
430 {
431     pthread_mutex_lock(&g_ifaceNameMutex);
432     LOGI("SetHdiStaIfaceName enter instId = %{public}d", instId);
433     if (ifaceName == NULL || instId >= STA_INSTANCE_MAX_NUM) {
434         pthread_mutex_unlock(&g_ifaceNameMutex);
435         return WIFI_HAL_OPT_INVALID_PARAM;
436     }
437 
438     if (memset_s(g_staIfaceName[instId], IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
439         pthread_mutex_unlock(&g_ifaceNameMutex);
440         return WIFI_HAL_OPT_FAILED;
441     }
442 
443     if (strcpy_s(g_staIfaceName[instId], IFACENAME_LEN, ifaceName) != EOK) {
444         pthread_mutex_unlock(&g_ifaceNameMutex);
445         return WIFI_HAL_OPT_FAILED;
446     }
447 
448     LOGI("SetHdiStaIfaceName, g_staIfaceName:%{public}s,  instId = %{public}d", g_staIfaceName[instId], instId);
449     pthread_mutex_unlock(&g_ifaceNameMutex);
450     return WIFI_HAL_OPT_OK;
451 }
452 
GetHdiStaIfaceName(int instId)453 const char *GetHdiStaIfaceName(int instId)
454 {
455     LOGI("GetHdiStaIfaceName enter instId = %{public}d", instId);
456     const char *ifaceName = NULL;
457     if (instId >= STA_INSTANCE_MAX_NUM || instId < 0) {
458         LOGE("invalid param instId = %{public}d", instId);
459         return ifaceName;
460     }
461 
462     pthread_mutex_lock(&g_ifaceNameMutex);
463     ifaceName = g_staIfaceName[instId];
464     pthread_mutex_unlock(&g_ifaceNameMutex);
465     LOGI("GetHdiStaIfaceName enter ifaceName = %{public}s", ifaceName);
466     return ifaceName;
467 }
468 
ClearHdiStaIfaceName(int instId)469 void ClearHdiStaIfaceName(int instId)
470 {
471     pthread_mutex_lock(&g_ifaceNameMutex);
472     if (memset_s(g_staIfaceName[instId], IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
473         pthread_mutex_unlock(&g_ifaceNameMutex);
474         return;
475     }
476     pthread_mutex_unlock(&g_ifaceNameMutex);
477 }
478 
SetHdiP2pIfaceName(const char * ifaceName)479 WifiErrorNo SetHdiP2pIfaceName(const char *ifaceName)
480 {
481     pthread_mutex_lock(&g_ifaceNameMutex);
482     if (ifaceName == NULL) {
483         pthread_mutex_unlock(&g_ifaceNameMutex);
484         return WIFI_HAL_OPT_INVALID_PARAM;
485     }
486 
487     if (memset_s(g_p2pIfaceName, IFACENAME_LEN, 0, IFACENAME_LEN) != EOK) {
488         pthread_mutex_unlock(&g_ifaceNameMutex);
489         return WIFI_HAL_OPT_FAILED;
490     }
491 
492     if (strcpy_s(g_p2pIfaceName, IFACENAME_LEN, ifaceName) != EOK) {
493         pthread_mutex_unlock(&g_ifaceNameMutex);
494         return WIFI_HAL_OPT_FAILED;
495     }
496 
497     LOGI("SetHdiP2pIfaceName, g_p2pIfaceName:%{public}s", g_p2pIfaceName);
498     pthread_mutex_unlock(&g_ifaceNameMutex);
499     return WIFI_HAL_OPT_OK;
500 }
501 
GetHdiP2pIfaceName()502 const char *GetHdiP2pIfaceName()
503 {
504     const char *ifaceName = NULL;
505     pthread_mutex_lock(&g_ifaceNameMutex);
506     ifaceName = g_p2pIfaceName;
507     pthread_mutex_unlock(&g_ifaceNameMutex);
508     return ifaceName;
509 }
510 
CopyUserFile(const char * srcFilePath,const char * destFilePath)511 WifiErrorNo CopyUserFile(const char *srcFilePath, const char* destFilePath)
512 {
513     LOGI("Execute CopyUserFile enter");
514     if (srcFilePath == NULL || destFilePath == NULL) {
515         LOGE("CopyUserFile() srcFilePath or destFilePath is nullptr!");
516         return WIFI_HAL_OPT_FAILED;
517     }
518     int srcFd = -1;
519     int destFd = -1;
520     do {
521         if ((srcFd = open(srcFilePath, O_RDONLY)) < 0)  {
522             LOGE("CopyUserFile() failed, open srcFilePath:%{public}s error!", srcFilePath);
523             break;
524         }
525         if ((destFd = open(destFilePath, O_RDWR | O_CREAT | O_TRUNC, FILE_OPEN_PRIV))< 0)  {
526             LOGE("CopyUserFile() failed, open destFilePath:%{public}s error!", destFilePath);
527             break;
528         }
529         ssize_t bytes;
530         lseek(srcFd, 0, SEEK_SET);
531         char buf[MAX_READ_FILE_SIZE] = {0};
532         for (int i = 0; i < MAX_FILE_BLOCK_SIZE; i++) {
533             if (memset_s(buf, MAX_READ_FILE_SIZE, 0, MAX_READ_FILE_SIZE) != WIFI_HAL_OPT_OK) {
534                 break;
535             }
536             if ((bytes = read(srcFd, buf, MAX_READ_FILE_SIZE-1)) < 0) {
537                 LOGE("CopyUserFile() failed, read srcFilePath:%{public}s error!", srcFilePath);
538                 break;
539             }
540             if (write(destFd, buf, bytes) < 0) {
541                 LOGE("CopyUserFile() failed, write destFilePath:%{public}s error!", destFilePath);
542             }
543         }
544     } while (0);
545     if (srcFd>=0) {
546         close(srcFd);
547     }
548 
549     if (destFd>=0) {
550         close(destFd);
551     }
552     LOGI("CopyUserFile() copy file succeed.");
553     return WIFI_HAL_OPT_OK;
554 }
555 
CopyConfigFile(const char * configName)556 WifiErrorNo CopyConfigFile(const char* configName)
557 {
558     if (configName == NULL || strlen(configName) == 0) {
559         LOGE("Copy config file failed:is null");
560         return WIFI_HAL_OPT_FAILED;
561     }
562     char path[PATH_NUM][BUFF_SIZE] = {"/system/etc/wifi/", "/vendor/etc/wifi/"};
563     for (int i = 0; i != PATH_NUM; ++i) {
564         if (strcat_s(path[i], sizeof(path[i]), configName) != EOK) {
565             LOGE("strcat_s failed.");
566             return WIFI_HAL_OPT_FAILED;
567         }
568         if (access(path[i], F_OK) != -1) {
569             char destFilePath[BUFF_SIZE] = {0};
570             if (snprintf_s(destFilePath, sizeof(destFilePath), sizeof(destFilePath) - 1,
571                 "%s/wpa_supplicant/%s", CONFIG_ROOR_DIR, configName) < 0) {
572                 LOGE("snprintf_s destFilePath failed.");
573                 return WIFI_HAL_OPT_FAILED;
574             }
575             return CopyUserFile(path[i], destFilePath);
576         }
577     }
578     LOGE("Copy config file failed: %{public}s", configName);
579     return WIFI_HAL_OPT_FAILED;
580 }
581 
HdiApResetGlobalObj()582 static void HdiApResetGlobalObj()
583 {
584     LOGI("%{public}s try reset ap", __func__);
585     if (IsHdiApStopped() == WIFI_HAL_OPT_OK) {
586         LOGI("%{public}s HdiAp already stopped", __func__);
587         return;
588     }
589     pthread_mutex_lock(&g_apObjMutex);
590     g_apIsRunning = false;
591     IHostapdInterfaceReleaseInstance(HDI_AP_SERVICE_NAME, g_apObj, false);
592     g_apObj = NULL;
593     if (g_apDevMgr != NULL) {
594         g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
595         HDIDeviceManagerRelease(g_apDevMgr);
596         g_apDevMgr = NULL;
597     }
598     pthread_mutex_unlock(&g_apObjMutex);
599     if (mNativeProcessCallback != NULL) {
600         mNativeProcessCallback(AP_DEATH);
601     }
602 }
603 
ProxyOnApRemoteDied(struct HdfDeathRecipient * recipient,struct HdfRemoteService * service)604 static void ProxyOnApRemoteDied(struct HdfDeathRecipient* recipient, struct HdfRemoteService* service)
605 {
606     LOGI("%{public}s enter", __func__);
607     if (recipient == NULL || service == NULL) {
608         LOGE("%{public}s input param is null", __func__);
609         HdiApResetGlobalObj();
610         return;
611     }
612     HdfRemoteServiceRemoveDeathRecipient(service, recipient);
613     HdfRemoteServiceRecycle(service);
614     if (recipient == NULL) {
615         LOGE("%{public}s param recipient is null", __func__);
616         HdiApResetGlobalObj();
617         return;
618     }
619     OsalMemFree(recipient);
620     recipient = NULL;
621     HdiApResetGlobalObj();
622 }
623 
RegistHdfApDeathCallBack()624 static WifiErrorNo RegistHdfApDeathCallBack()
625 {
626     struct HDIServiceManager* serviceMgr = HDIServiceManagerGet();
627     if (serviceMgr == NULL) {
628         LOGE("%{public}s: failed to get HDIServiceManager", __func__);
629         return WIFI_HAL_OPT_FAILED;
630     }
631     struct HdfRemoteService* remote = serviceMgr->GetService(serviceMgr, HDI_AP_SERVICE_NAME);
632     HDIServiceManagerRelease(serviceMgr);
633     if (remote == NULL) {
634         LOGE("%{public}s: failed to get HdfRemoteService", __func__);
635         return WIFI_HAL_OPT_FAILED;
636     }
637     LOGI("%{public}s: success to get HdfRemoteService", __func__);
638     struct HdfDeathRecipient* recipient = (struct HdfDeathRecipient*)OsalMemCalloc(sizeof(struct HdfDeathRecipient));
639     if (recipient == NULL) {
640         LOGE("%{public}s: OsalMemCalloc is failed", __func__);
641         return WIFI_HAL_OPT_FAILED;
642     }
643     recipient->OnRemoteDied = ProxyOnApRemoteDied;
644     HdfRemoteServiceAddDeathRecipient(remote, recipient);
645     return WIFI_HAL_OPT_OK;
646 }
647 
GetApInstance()648 static WifiErrorNo GetApInstance()
649 {
650     g_apDevMgr = HDIDeviceManagerGet();
651     if (g_apDevMgr == NULL) {
652         LOGE("%{public}s HDIDeviceManagerGet failed", __func__);
653         return WIFI_HAL_OPT_FAILED;
654     }
655     HDF_STATUS retDevice = g_apDevMgr->LoadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME) ;
656     if (retDevice == HDF_ERR_DEVICE_BUSY) {
657         LOGE("%{public}s LoadDevice busy: %{public}d", __func__, retDevice);
658     } else if (retDevice != HDF_SUCCESS) {
659         HDIDeviceManagerRelease(g_apDevMgr);
660         g_apDevMgr = NULL;
661         LOGE("%{public}s LoadDevice failed", __func__);
662         return WIFI_HAL_OPT_FAILED;
663     }
664     g_apObj = IHostapdInterfaceGetInstance(HDI_AP_SERVICE_NAME, false);
665     if (g_apObj == NULL && g_apDevMgr != NULL) {
666         g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
667         HDIDeviceManagerRelease(g_apDevMgr);
668         g_apDevMgr = NULL;
669         LOGE("%{public}s HostapdInterfaceGetInstance failed", __func__);
670         return WIFI_HAL_OPT_FAILED;
671     }
672     return WIFI_HAL_OPT_OK;
673 }
674 
StartApHdi(int id,const char * ifaceName)675 static WifiErrorNo StartApHdi(int id, const char *ifaceName)
676 {
677     if (g_apObj == NULL) {
678         LOGE("%{public}s Pointer g_apObj is NULL", __func__);
679         return WIFI_HAL_OPT_FAILED;
680     }
681     int32_t ret = g_apObj->StartApWithCmd(g_apObj, ifaceName, id);
682     if (ret != HDF_SUCCESS) {
683         LOGE("%{public}s Start failed: %{public}d", __func__, ret);
684         IHostapdInterfaceReleaseInstance(HDI_AP_SERVICE_NAME, g_apObj, false);
685         g_apObj = NULL;
686         if (g_apDevMgr != NULL) {
687             g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
688             HDIDeviceManagerRelease(g_apDevMgr);
689             g_apDevMgr = NULL;
690         }
691         return WIFI_HAL_OPT_FAILED;
692     }
693     return WIFI_HAL_OPT_OK;
694 }
695 
HdiApStart(int id,const char * ifaceName)696 WifiErrorNo HdiApStart(int id, const char *ifaceName)
697 {
698     LOGI("HdiApStart start...");
699     pthread_mutex_lock(&g_apObjMutex);
700 
701     g_id = id;
702     WifiErrorNo result = WIFI_HAL_OPT_FAILED;
703     do {
704 #if (AP_NUM > 1)
705         result = CopyConfigFile(WIFI_5G_CFG);
706         if (result != WIFI_HAL_OPT_OK) {
707             break;
708         }
709         result = CopyConfigFile(WIFI_2G_CFG);
710 #else
711         result = CopyConfigFile(WIFI_DEFAULT_CFG);
712 #endif
713         if (result != WIFI_HAL_OPT_OK) {
714             break;
715         }
716         result = GetApInstance();
717         if (result != WIFI_HAL_OPT_OK) {
718             break;
719         }
720         result = StartApHdi(id, ifaceName);
721         if (result != WIFI_HAL_OPT_OK) {
722             break;
723         }
724         result = RegistHdfApDeathCallBack();
725         if (result != WIFI_HAL_OPT_OK) {
726             break;
727         }
728         g_apIsRunning = true;
729         LOGI("HdiApStart start success");
730     } while (0);
731     pthread_mutex_unlock(&g_apObjMutex);
732     return result;
733 }
734 
HdiApStop(int id)735 WifiErrorNo HdiApStop(int id)
736 {
737     LOGI("HdiApStop stop...");
738     pthread_mutex_lock(&g_apObjMutex);
739 
740     int32_t ret;
741     if (g_apObj == NULL) {
742         LOGE("%{public}s, g_apObj is NULL", __func__);
743         pthread_mutex_unlock(&g_apObjMutex);
744         return WIFI_HAL_OPT_FAILED;
745     }
746     ret = g_apObj->DisableAp(g_apObj, g_apIfaceName, id);
747     ret = g_apObj->StopAp(g_apObj);
748     if (ret != HDF_SUCCESS) {
749         LOGE("%{public}s Stop failed: %{public}d", __func__, ret);
750     }
751     IHostapdInterfaceReleaseInstance(HDI_AP_SERVICE_NAME, g_apObj, false);
752     g_apObj = NULL;
753     if (g_apDevMgr != NULL) {
754         g_apDevMgr->UnloadDevice(g_apDevMgr, HDI_AP_SERVICE_NAME);
755         HDIDeviceManagerRelease(g_apDevMgr);
756         g_apDevMgr = NULL;
757     }
758     g_apIsRunning = false;
759     pthread_mutex_unlock(&g_apObjMutex);
760     LOGI("HdiApStop stop success");
761     return WIFI_HAL_OPT_OK;
762 }
763 
IsHdiApStopped()764 WifiErrorNo IsHdiApStopped()
765 {
766     pthread_mutex_lock(&g_apObjMutex);
767     if (g_apIsRunning == false && g_apObj == NULL && g_apDevMgr == NULL) {
768         LOGI("IsHdiApStopped, HdiAp already stopped");
769         pthread_mutex_unlock(&g_apObjMutex);
770         return WIFI_HAL_OPT_OK;
771     }
772 
773     pthread_mutex_unlock(&g_apObjMutex);
774     return WIFI_HAL_OPT_FAILED;
775 }
776 
GetApInterface()777 struct IHostapdInterface* GetApInterface()
778 {
779     struct IHostapdInterface *apObj = NULL;
780     pthread_mutex_lock(&g_apObjMutex);
781     apObj = g_apObj;
782     pthread_mutex_unlock(&g_apObjMutex);
783     return apObj;
784 }
785 
SetHdiApIfaceName(const char * ifaceName)786 WifiErrorNo SetHdiApIfaceName(const char *ifaceName)
787 {
788     pthread_mutex_lock(&g_apIfaceNameMutex);
789     if (ifaceName == NULL) {
790         pthread_mutex_unlock(&g_apIfaceNameMutex);
791         return WIFI_HAL_OPT_INVALID_PARAM;
792     }
793 
794     if (memset_s(g_apCfgName, CFGNAME_LEN, 0, CFGNAME_LEN) != EOK
795         || memset_s(g_apIfaceName, IFACENAME_LEN, 0, IFACENAME_LEN) != EOK
796         || memset_s(g_hostapdCfg, CTRL_LEN, 0, CTRL_LEN) != EOK) {
797         pthread_mutex_unlock(&g_apIfaceNameMutex);
798         return WIFI_HAL_OPT_FAILED;
799     }
800 
801     if (strncmp(ifaceName, AP_IFNAME_COEX, IFACENAME_LEN -1) == 0) {
802         if (strcpy_s(g_apCfgName, CFGNAME_LEN, WIFI_COEX_CFG) != EOK
803             || strcpy_s(g_apIfaceName, IFACENAME_LEN, AP_IFNAME_COEX) != EOK
804             || strcpy_s(g_hostapdCfg, CTRL_LEN, HOSTAPD_DEFAULT_CFG_COEX) != EOK) {
805             pthread_mutex_unlock(&g_apIfaceNameMutex);
806             return WIFI_HAL_OPT_FAILED;
807         }
808     } else {
809         if (strcpy_s(g_apCfgName, CFGNAME_LEN, WIFI_DEFAULT_CFG) != EOK
810             || strcpy_s(g_apIfaceName, IFACENAME_LEN, AP_IFNAME) != EOK
811             || strcpy_s(g_hostapdCfg, CTRL_LEN, HOSTAPD_DEFAULT_CFG) != EOK) {
812             pthread_mutex_unlock(&g_apIfaceNameMutex);
813             return WIFI_HAL_OPT_FAILED;
814         }
815     }
816 
817     LOGI("SetHdiApIfaceName, g_apIfaceName:%{public}s", g_apIfaceName);
818     pthread_mutex_unlock(&g_apIfaceNameMutex);
819     return WIFI_HAL_OPT_OK;
820 }
821 
GetHdiApIfaceName()822 const char *GetHdiApIfaceName()
823 {
824     const char *ifaceName = NULL;
825     pthread_mutex_lock(&g_apIfaceNameMutex);
826     ifaceName = g_apIfaceName;
827     pthread_mutex_unlock(&g_apIfaceNameMutex);
828     return ifaceName;
829 }
830 
SetExecDisable(int execDisable)831 void SetExecDisable(int execDisable)
832 {
833     g_execDisable = execDisable;
834 }
835 
GetExecDisable()836 int GetExecDisable()
837 {
838     return g_execDisable;
839 }
840 
841 #endif