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_HAL_PATH = "libwifi_hal_hw.z.so";
30 
WifiVendorHalList(const std::weak_ptr<IfaceTool> ifaceTool)31 WifiVendorHalList::WifiVendorHalList(
32     const std::weak_ptr<IfaceTool> ifaceTool)
33     : ifaceTool_(ifaceTool) {}
34 
GetHals()35 std::vector<std::shared_ptr<WifiVendorHal>> WifiVendorHalList::GetHals()
36 {
37     if (vendorHals_.empty()) {
38         InitVendorHalsDescriptorList();
39         for (auto& desc : descs_) {
40             std::shared_ptr<WifiVendorHal> hal =
41                 std::make_shared<WifiVendorHal>(ifaceTool_, desc.fn,
42                                                 desc.primary);
43             vendorHals_.push_back(hal);
44         }
45     }
46     return vendorHals_;
47 }
48 
InitVendorHalsDescriptorList()49 void WifiVendorHalList::InitVendorHalsDescriptorList()
50 {
51     WifiHalLibDesc desc;
52     std::string path = VENDOR_HAL_PATH;
53     desc.primary = true;
54     if (LoadVendorHalLib(path, desc)) {
55         if (desc.primary) {
56             descs_.insert(descs_.begin(), desc);
57         } else {
58             descs_.push_back(desc);
59         }
60     }
61 }
62 
LoadVendorHalLib(const std::string & path,WifiHalLibDesc & desc)63 bool WifiVendorHalList::LoadVendorHalLib(const std::string& path, WifiHalLibDesc &desc)
64 {
65     void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
66     InitWifiVendorHalFuncTableT initfn;
67     WifiError res;
68     if (!h) {
69         HDF_LOGE("failed to open vendor hal library: %{public}s", path.c_str());
70         return false;
71     }
72     initfn = reinterpret_cast<InitWifiVendorHalFuncTableT>(dlsym(
73         h, "InitWifiVendorHalFuncTable"));
74     if (!initfn) {
75         HDF_LOGE("InitWifiVendorHalFuncTable not found in: %{public}s", path.c_str());
76         goto out_err;
77     }
78     if (!InitHalFuncTableWithStubs(&desc.fn)) {
79         HDF_LOGE("Can not initialize the basic function pointer table");
80         goto out_err;
81     }
82     res = initfn(&desc.fn);
83     if (res != HAL_SUCCESS) {
84         HDF_LOGE("failed to initialize the vendor func table in: %{public}s, error: %{public}d",
85             path.c_str(), res);
86         goto out_err;
87     }
88     res = desc.fn.vendorHalPreInit();
89     if (res != HAL_SUCCESS && res != HAL_NOT_SUPPORTED) {
90         HDF_LOGE("early initialization failed in: %{public}s, error: %{public}d", path.c_str(), res);
91         goto out_err;
92     }
93     desc.handle = h;
94     return true;
95 out_err:
96     dlclose(h);
97     return false;
98 }
99 
100 } // namespace v1_0
101 } // namespace Chip
102 } // namespace Wlan
103 } // namespace HDI
104 } // namespace OHOS