• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wifi_legacy_hal_factory.h"
18 
19 #include <android-base/logging.h>
20 #include <dirent.h>
21 #include <dlfcn.h>
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <libxml/xmlmemory.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 
28 #include "wifi_legacy_hal_stubs.h"
29 
30 namespace {
31 static constexpr char kVendorHalsDescPath[] = "/vendor/etc/wifi/vendor_hals";
32 static constexpr char kVendorHalsDescExt[] = ".xml";
33 static constexpr uint32_t kVendorHalsDescVersion = 1;
34 
isDirectory(struct dirent * entryPtr)35 bool isDirectory(struct dirent* entryPtr) {
36     bool isDir = false;
37     if (entryPtr->d_type != DT_UNKNOWN && entryPtr->d_type != DT_LNK) {
38         isDir = (entryPtr->d_type == DT_DIR);
39     } else {
40         struct stat entryStat;
41         stat(entryPtr->d_name, &entryStat);
42         isDir = S_ISDIR(entryStat.st_mode);
43     }
44     return isDir;
45 }
46 
isFileExtension(const char * name,const char * ext)47 bool isFileExtension(const char* name, const char* ext) {
48     if (name == NULL) return false;
49     if (ext == NULL) return false;
50 
51     size_t extLen = strlen(ext);
52     size_t nameLen = strlen(name);
53 
54     if (extLen > nameLen) return false;
55 
56     if (strncmp(name + nameLen - extLen, ext, extLen) != 0) return false;
57 
58     return true;
59 }
60 };  // namespace
61 
62 namespace aidl {
63 namespace android {
64 namespace hardware {
65 namespace wifi {
66 namespace legacy_hal {
67 
WifiLegacyHalFactory(const std::weak_ptr<::android::wifi_system::InterfaceTool> iface_tool)68 WifiLegacyHalFactory::WifiLegacyHalFactory(
69         const std::weak_ptr<::android::wifi_system::InterfaceTool> iface_tool)
70     : iface_tool_(iface_tool) {}
71 
getHals()72 std::vector<std::shared_ptr<WifiLegacyHal>> WifiLegacyHalFactory::getHals() {
73     if (legacy_hals_.empty()) {
74         if (!initVendorHalDescriptorFromLinked()) initVendorHalsDescriptorList();
75         for (auto& desc : descs_) {
76             std::shared_ptr<WifiLegacyHal> hal =
77                     std::make_shared<WifiLegacyHal>(iface_tool_, desc.fn, desc.primary);
78             legacy_hals_.push_back(hal);
79         }
80     }
81 
82     return legacy_hals_;
83 }
84 
initVendorHalDescriptorFromLinked()85 bool WifiLegacyHalFactory::initVendorHalDescriptorFromLinked() {
86     wifi_hal_lib_desc desc;
87 
88     if (!initLinkedHalFunctionTable(&desc.fn)) return false;
89 
90     desc.primary = true;
91     desc.handle = NULL;
92     descs_.push_back(desc);
93     return true;
94 }
95 
initLinkedHalFunctionTable(wifi_hal_fn * hal_fn)96 bool WifiLegacyHalFactory::initLinkedHalFunctionTable(wifi_hal_fn* hal_fn) {
97     init_wifi_vendor_hal_func_table_t initfn;
98 
99     initfn = (init_wifi_vendor_hal_func_table_t)dlsym(RTLD_DEFAULT,
100                                                       "init_wifi_vendor_hal_func_table");
101     if (!initfn) {
102         LOG(INFO) << "no vendor HAL library linked, will try dynamic load";
103         return false;
104     }
105 
106     if (!initHalFuncTableWithStubs(hal_fn)) {
107         LOG(ERROR) << "Can not initialize the basic function pointer table";
108         return false;
109     }
110 
111     if (initfn(hal_fn) != WIFI_SUCCESS) {
112         LOG(ERROR) << "Can not initialize the vendor function pointer table";
113         return false;
114     }
115 
116     return true;
117 }
118 
119 /*
120  * Overall structure of the HAL descriptor XML schema
121  *
122  * <?xml version="1.0" encoding="UTF-8"?>
123  * <WifiVendorHal version="1">
124  * <path>/vendor/lib64/libwifi-hal-qcom.so</path>
125  * <primary>1</primary>
126  * </WifiVendorHal>
127  */
initVendorHalsDescriptorList()128 void WifiLegacyHalFactory::initVendorHalsDescriptorList() {
129     xmlDocPtr xml;
130     xmlNodePtr node, cnode;
131     char* version = NULL;
132     std::string path;
133     xmlChar* value;
134     wifi_hal_lib_desc desc;
135 
136     LOG(INFO) << "processing vendor HALs descriptions in " << kVendorHalsDescPath;
137     DIR* dirPtr = ::opendir(kVendorHalsDescPath);
138     if (dirPtr == NULL) {
139         LOG(ERROR) << "failed to open " << kVendorHalsDescPath;
140         return;
141     }
142     for (struct dirent* entryPtr = ::readdir(dirPtr); entryPtr != NULL;
143          entryPtr = ::readdir(dirPtr)) {
144         if (isDirectory(entryPtr)) continue;
145 
146         if (!isFileExtension(entryPtr->d_name, kVendorHalsDescExt))
147             continue;  // only process .xml files
148 
149         LOG(INFO) << "processing config file: " << entryPtr->d_name;
150 
151         std::string fullPath(kVendorHalsDescPath);
152         fullPath.append("/");
153         fullPath.append(entryPtr->d_name);
154         xml = xmlReadFile(fullPath.c_str(), "UTF-8", XML_PARSE_RECOVER);
155         if (!xml) {
156             LOG(ERROR) << "failed to parse: " << entryPtr->d_name << " skipping...";
157             continue;
158         }
159         node = xmlDocGetRootElement(xml);
160         if (!node) {
161             LOG(ERROR) << "empty config file: " << entryPtr->d_name << " skipping...";
162             goto skip;
163         }
164         if (xmlStrcmp(node->name, BAD_CAST "WifiVendorHal")) {
165             LOG(ERROR) << "bad config, root element not WifiVendorHal: " << entryPtr->d_name
166                        << " skipping...";
167             goto skip;
168         }
169         version = (char*)xmlGetProp(node, BAD_CAST "version");
170         if (!version || strtoul(version, NULL, 0) != kVendorHalsDescVersion) {
171             LOG(ERROR) << "conf file: " << entryPtr->d_name
172                        << "must have version: " << kVendorHalsDescVersion << ", skipping...";
173             goto skip;
174         }
175         cnode = node->children;
176         path.clear();
177         desc.primary = false;
178         while (cnode) {
179             if (!xmlStrcmp(cnode->name, BAD_CAST "path")) {
180                 value = xmlNodeListGetString(xml, cnode->children, 1);
181                 if (value) path = (char*)value;
182                 xmlFree(value);
183             } else if (!xmlStrcmp(cnode->name, BAD_CAST "primary")) {
184                 value = xmlNodeListGetString(xml, cnode->children, 1);
185                 desc.primary = !xmlStrcmp(value, BAD_CAST "1");
186                 xmlFree(value);
187             }
188             cnode = cnode->next;
189         }
190         if (path.empty()) {
191             LOG(ERROR) << "hal library path not provided in: " << entryPtr->d_name
192                        << ", skipping...";
193             goto skip;
194         }
195         if (loadVendorHalLib(path, desc)) {
196             if (desc.primary)
197                 descs_.insert(descs_.begin(), desc);
198             else
199                 descs_.push_back(desc);
200         }
201     skip:
202         xmlFreeDoc(xml);
203         if (version) {
204             xmlFree(version);
205             version = NULL;
206         }
207     }
208     ::closedir(dirPtr);
209 }
210 
loadVendorHalLib(const std::string & path,wifi_hal_lib_desc & desc)211 bool WifiLegacyHalFactory::loadVendorHalLib(const std::string& path, wifi_hal_lib_desc& desc) {
212     void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
213     init_wifi_vendor_hal_func_table_t initfn;
214     wifi_error res;
215 
216     if (!h) {
217         LOG(ERROR) << "failed to open vendor hal library: " << path;
218         return false;
219     }
220     initfn = (init_wifi_vendor_hal_func_table_t)dlsym(h, "init_wifi_vendor_hal_func_table");
221     if (!initfn) {
222         LOG(ERROR) << "init_wifi_vendor_hal_func_table not found in: " << path;
223         goto out_err;
224     }
225 
226     if (!initHalFuncTableWithStubs(&desc.fn)) {
227         LOG(ERROR) << "Can not initialize the basic function pointer table";
228         goto out_err;
229     }
230     res = initfn(&desc.fn);
231     if (res != WIFI_SUCCESS) {
232         LOG(ERROR) << "failed to initialize the vendor func table in: " << path
233                    << " error: " << res;
234         goto out_err;
235     }
236 
237     res = desc.fn.wifi_early_initialize();
238     // vendor HALs which do not implement early_initialize will return
239     // WIFI_ERROR_NOT_SUPPORTED, treat this as success.
240     if (res != WIFI_SUCCESS && res != WIFI_ERROR_NOT_SUPPORTED) {
241         LOG(ERROR) << "early initialization failed in: " << path << " error: " << res;
242         goto out_err;
243     }
244 
245     desc.handle = h;
246     return true;
247 out_err:
248     dlclose(h);
249     return false;
250 }
251 
252 }  // namespace legacy_hal
253 }  // namespace wifi
254 }  // namespace hardware
255 }  // namespace android
256 }  // namespace aidl
257