1 /*
2 * Copyright (c) 2024 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 <dirent.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <dlfcn.h>
20 #include "wifi_vendor_hal_list.h"
21 #include "wifi_vendor_hal_stubs.h"
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Wlan {
26 namespace Chip {
27 namespace V2_0 {
28
29 const std::string VENDOR_HARDWARE_PATH = "libwifi_hal_hw.z.so";
30 const std::string VENDOR_DEFAULT_PATH = "libwifi_hal_default.z.so";
31
WifiVendorHalList(const std::weak_ptr<IfaceTool> ifaceTool)32 WifiVendorHalList::WifiVendorHalList(
33 const std::weak_ptr<IfaceTool> ifaceTool)
34 : ifaceTool_(ifaceTool) {}
35
GetHals()36 std::vector<std::shared_ptr<WifiVendorHal>> WifiVendorHalList::GetHals()
37 {
38 if (vendorHals_.empty()) {
39 InitVendorHalsDescriptorList();
40 for (auto& desc : descs_) {
41 std::shared_ptr<WifiVendorHal> hal =
42 std::make_shared<WifiVendorHal>(ifaceTool_, desc.fn,
43 desc.primary);
44 vendorHals_.push_back(hal);
45 }
46 }
47 return vendorHals_;
48 }
49
InitVendorHalsDescriptorList()50 void WifiVendorHalList::InitVendorHalsDescriptorList()
51 {
52 WifiHalLibDesc desc;
53 std::string path = VENDOR_DEFAULT_PATH;
54 if (strncmp(HAL_SO_NAME, "hardware", strlen(HAL_SO_NAME)) == 0) {
55 path = VENDOR_HARDWARE_PATH;
56 }
57 desc.primary = true;
58 if (LoadVendorHalLib(path, desc)) {
59 if (desc.primary) {
60 descs_.insert(descs_.begin(), desc);
61 } else {
62 descs_.push_back(desc);
63 }
64 }
65 }
66
LoadVendorHalLib(const std::string & path,WifiHalLibDesc & desc)67 bool WifiVendorHalList::LoadVendorHalLib(const std::string& path, WifiHalLibDesc &desc)
68 {
69 void* fd = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
70 InitWifiVendorHalFuncTableT initfn;
71 WifiError res;
72 if (!fd) {
73 HDF_LOGE("failed to open vendor hal library: %{public}s", path.c_str());
74 return false;
75 }
76 initfn = reinterpret_cast<InitWifiVendorHalFuncTableT>(dlsym(
77 fd, "InitWifiVendorHalFuncTable"));
78 if (!initfn) {
79 HDF_LOGE("InitWifiVendorHalFuncTable not found in: %{public}s", path.c_str());
80 dlclose(fd);
81 return false;
82 }
83 if (!InitHalFuncTableWithStubs(&desc.fn)) {
84 HDF_LOGE("Can not initialize the basic function pointer table");
85 dlclose(fd);
86 return false;
87 }
88 res = initfn(&desc.fn);
89 if (res != HAL_SUCCESS) {
90 HDF_LOGE("failed to initialize the vendor func table in: %{public}s, error: %{public}d",
91 path.c_str(), res);
92 dlclose(fd);
93 return false;
94 }
95 res = desc.fn.vendorHalPreInit();
96 if (res != HAL_SUCCESS && res != HAL_NOT_SUPPORTED) {
97 HDF_LOGE("early initialization failed in: %{public}s, error: %{public}d", path.c_str(), res);
98 dlclose(fd);
99 return false;
100 }
101 desc.handle = fd;
102 return true;
103 }
104
105 } // namespace v2_0
106 } // namespace Chip
107 } // namespace Wlan
108 } // namespace HDI
109 } // namespace OHOS