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 V1_0 {
28
29 const std::string VENDOR_HARDWARE_PATH = "libwifi_hal_hw.z.so";
30 const std::string VENDOR_DAFAULT_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_DAFAULT_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* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
70 InitWifiVendorHalFuncTableT initfn;
71 WifiError res;
72 if (!h) {
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 h, "InitWifiVendorHalFuncTable"));
78 if (!initfn) {
79 HDF_LOGE("InitWifiVendorHalFuncTable not found in: %{public}s", path.c_str());
80 goto out_err;
81 }
82 if (!InitHalFuncTableWithStubs(&desc.fn)) {
83 HDF_LOGE("Can not initialize the basic function pointer table");
84 goto out_err;
85 }
86 res = initfn(&desc.fn);
87 if (res != HAL_SUCCESS) {
88 HDF_LOGE("failed to initialize the vendor func table in: %{public}s, error: %{public}d",
89 path.c_str(), res);
90 goto out_err;
91 }
92 res = desc.fn.vendorHalPreInit();
93 if (res != HAL_SUCCESS && res != HAL_NOT_SUPPORTED) {
94 HDF_LOGE("early initialization failed in: %{public}s, error: %{public}d", path.c_str(), res);
95 goto out_err;
96 }
97 desc.handle = h;
98 return true;
99 out_err:
100 dlclose(h);
101 return false;
102 }
103
104 } // namespace v1_0
105 } // namespace Chip
106 } // namespace Wlan
107 } // namespace HDI
108 } // namespace OHOS