1 /*
2 * Copyright (C) 2020 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 <dirent.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20
21 #include <android-base/logging.h>
22 #include <dlfcn.h>
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 #include <libxml/xmlmemory.h>
26
27 #include "wifi_legacy_hal_factory.h"
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 android {
63 namespace hardware {
64 namespace wifi {
65 namespace V1_6 {
66 namespace implementation {
67 namespace legacy_hal {
68
WifiLegacyHalFactory(const std::weak_ptr<wifi_system::InterfaceTool> iface_tool)69 WifiLegacyHalFactory::WifiLegacyHalFactory(
70 const std::weak_ptr<wifi_system::InterfaceTool> iface_tool)
71 : iface_tool_(iface_tool) {}
72
getHals()73 std::vector<std::shared_ptr<WifiLegacyHal>> WifiLegacyHalFactory::getHals() {
74 if (legacy_hals_.empty()) {
75 if (!initVendorHalDescriptorFromLinked()) initVendorHalsDescriptorList();
76 for (auto& desc : descs_) {
77 std::shared_ptr<WifiLegacyHal> hal =
78 std::make_shared<WifiLegacyHal>(iface_tool_, desc.fn, desc.primary);
79 legacy_hals_.push_back(hal);
80 }
81 }
82
83 return legacy_hals_;
84 }
85
initVendorHalDescriptorFromLinked()86 bool WifiLegacyHalFactory::initVendorHalDescriptorFromLinked() {
87 wifi_hal_lib_desc desc;
88
89 if (!initLinkedHalFunctionTable(&desc.fn)) return false;
90
91 desc.primary = true;
92 desc.handle = NULL;
93 descs_.push_back(desc);
94 return true;
95 }
96
initLinkedHalFunctionTable(wifi_hal_fn * hal_fn)97 bool WifiLegacyHalFactory::initLinkedHalFunctionTable(wifi_hal_fn* hal_fn) {
98 init_wifi_vendor_hal_func_table_t initfn;
99
100 initfn = (init_wifi_vendor_hal_func_table_t)dlsym(RTLD_DEFAULT,
101 "init_wifi_vendor_hal_func_table");
102 if (!initfn) {
103 LOG(INFO) << "no vendor HAL library linked, will try dynamic load";
104 return false;
105 }
106
107 if (!initHalFuncTableWithStubs(hal_fn)) {
108 LOG(ERROR) << "Can not initialize the basic function pointer table";
109 return false;
110 }
111
112 if (initfn(hal_fn) != WIFI_SUCCESS) {
113 LOG(ERROR) << "Can not initialize the vendor function pointer table";
114 return false;
115 }
116
117 return true;
118 }
119
120 /*
121 * Overall structure of the HAL descriptor XML schema
122 *
123 * <?xml version="1.0" encoding="UTF-8"?>
124 * <WifiVendorHal version="1">
125 * <path>/vendor/lib64/libwifi-hal-qcom.so</path>
126 * <primary>1</primary>
127 * </WifiVendorHal>
128 */
initVendorHalsDescriptorList()129 void WifiLegacyHalFactory::initVendorHalsDescriptorList() {
130 xmlDocPtr xml;
131 xmlNodePtr node, cnode;
132 char* version;
133 std::string path;
134 xmlChar* value;
135 wifi_hal_lib_desc desc;
136
137 LOG(INFO) << "processing vendor HALs descriptions in " << kVendorHalsDescPath;
138 DIR* dirPtr = ::opendir(kVendorHalsDescPath);
139 if (dirPtr == NULL) {
140 LOG(ERROR) << "failed to open " << kVendorHalsDescPath;
141 return;
142 }
143 for (struct dirent* entryPtr = ::readdir(dirPtr); entryPtr != NULL;
144 entryPtr = ::readdir(dirPtr)) {
145 if (isDirectory(entryPtr)) continue;
146
147 if (!isFileExtension(entryPtr->d_name, kVendorHalsDescExt))
148 continue; // only process .xml files
149
150 LOG(INFO) << "processing config file: " << entryPtr->d_name;
151
152 std::string fullPath(kVendorHalsDescPath);
153 fullPath.append("/");
154 fullPath.append(entryPtr->d_name);
155 xml = xmlReadFile(fullPath.c_str(), "UTF-8", XML_PARSE_RECOVER);
156 if (!xml) {
157 LOG(ERROR) << "failed to parse: " << entryPtr->d_name << " skipping...";
158 continue;
159 }
160 node = xmlDocGetRootElement(xml);
161 if (!node) {
162 LOG(ERROR) << "empty config file: " << entryPtr->d_name << " skipping...";
163 goto skip;
164 }
165 if (xmlStrcmp(node->name, BAD_CAST "WifiVendorHal")) {
166 LOG(ERROR) << "bad config, root element not WifiVendorHal: " << entryPtr->d_name
167 << " skipping...";
168 goto skip;
169 }
170 version = (char*)xmlGetProp(node, BAD_CAST "version");
171 if (!version || strtoul(version, NULL, 0) != kVendorHalsDescVersion) {
172 LOG(ERROR) << "conf file: " << entryPtr->d_name
173 << "must have version: " << kVendorHalsDescVersion << ", skipping...";
174 goto skip;
175 }
176 cnode = node->children;
177 path.clear();
178 desc.primary = false;
179 while (cnode) {
180 if (!xmlStrcmp(cnode->name, BAD_CAST "path")) {
181 value = xmlNodeListGetString(xml, cnode->children, 1);
182 if (value) path = (char*)value;
183 xmlFree(value);
184 } else if (!xmlStrcmp(cnode->name, BAD_CAST "primary")) {
185 value = xmlNodeListGetString(xml, cnode->children, 1);
186 desc.primary = !xmlStrcmp(value, BAD_CAST "1");
187 xmlFree(value);
188 }
189 cnode = cnode->next;
190 }
191 if (path.empty()) {
192 LOG(ERROR) << "hal library path not provided in: " << entryPtr->d_name
193 << ", skipping...";
194 goto skip;
195 }
196 if (loadVendorHalLib(path, desc)) {
197 if (desc.primary)
198 descs_.insert(descs_.begin(), desc);
199 else
200 descs_.push_back(desc);
201 }
202 skip:
203 xmlFreeDoc(xml);
204 }
205 ::closedir(dirPtr);
206 }
207
loadVendorHalLib(const std::string & path,wifi_hal_lib_desc & desc)208 bool WifiLegacyHalFactory::loadVendorHalLib(const std::string& path, wifi_hal_lib_desc& desc) {
209 void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
210 init_wifi_vendor_hal_func_table_t initfn;
211 wifi_error res;
212
213 if (!h) {
214 LOG(ERROR) << "failed to open vendor hal library: " << path;
215 return false;
216 }
217 initfn = (init_wifi_vendor_hal_func_table_t)dlsym(h, "init_wifi_vendor_hal_func_table");
218 if (!initfn) {
219 LOG(ERROR) << "init_wifi_vendor_hal_func_table not found in: " << path;
220 goto out_err;
221 }
222
223 if (!initHalFuncTableWithStubs(&desc.fn)) {
224 LOG(ERROR) << "Can not initialize the basic function pointer table";
225 goto out_err;
226 }
227 res = initfn(&desc.fn);
228 if (res != WIFI_SUCCESS) {
229 LOG(ERROR) << "failed to initialize the vendor func table in: " << path
230 << " error: " << res;
231 goto out_err;
232 }
233
234 res = desc.fn.wifi_early_initialize();
235 // vendor HALs which do not implement early_initialize will return
236 // WIFI_ERROR_NOT_SUPPORTED, treat this as success.
237 if (res != WIFI_SUCCESS && res != WIFI_ERROR_NOT_SUPPORTED) {
238 LOG(ERROR) << "early initialization failed in: " << path << " error: " << res;
239 goto out_err;
240 }
241
242 desc.handle = h;
243 return true;
244 out_err:
245 dlclose(h);
246 return false;
247 }
248
249 } // namespace legacy_hal
250 } // namespace implementation
251 } // namespace V1_6
252 } // namespace wifi
253 } // namespace hardware
254 } // namespace android
255