1 /*
2 * Copyright (C) 2014 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 <stdlib.h>
18 #include <linux/pkt_sched.h>
19 #include <netlink/object-api.h>
20 #include <dlfcn.h>
21 #include <pthread.h>
22
23 #include <hardware_legacy/wifi_hal.h>
24 #include "common.h"
25 #include <errno.h>
26
getIfaceInfo(wifi_interface_handle handle)27 interface_info *getIfaceInfo(wifi_interface_handle handle)
28 {
29 return (interface_info *)handle;
30 }
31
getWifiHandle(wifi_interface_handle handle)32 wifi_handle getWifiHandle(wifi_interface_handle handle)
33 {
34 return getIfaceInfo(handle)->handle;
35 }
36
getHalInfo(wifi_handle handle)37 hal_info *getHalInfo(wifi_handle handle)
38 {
39 return (hal_info *)handle;
40 }
41
getHalInfo(wifi_interface_handle handle)42 hal_info *getHalInfo(wifi_interface_handle handle)
43 {
44 return getHalInfo(getWifiHandle(handle));
45 }
46
getWifiHandle(hal_info * info)47 wifi_handle getWifiHandle(hal_info *info)
48 {
49 return (wifi_handle)info;
50 }
51
getIfaceHandle(interface_info * info)52 wifi_interface_handle getIfaceHandle(interface_info *info)
53 {
54 return (wifi_interface_handle)info;
55 }
56
wifi_register_handler(wifi_handle handle,int cmd,nl_recvmsg_msg_cb_t func,void * arg)57 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg)
58 {
59 hal_info *info = (hal_info *)handle;
60
61 pthread_mutex_lock(&info->cb_lock);
62
63 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
64
65 for (int i = 0; i < info->num_event_cb; i++) {
66 if(info->event_cb[i].nl_cmd == cmd &&
67 info->event_cb[i].cb_arg == arg) {
68 info->event_cb[i].cb_func = func;
69 ALOGV("Updated event handler %p for nl_cmd 0x%0x"
70 " and arg %p", func, cmd, arg);
71 pthread_mutex_unlock(&info->cb_lock);
72 return WIFI_SUCCESS;
73 }
74 }
75
76 if (info->num_event_cb < info->alloc_event_cb) {
77 info->event_cb[info->num_event_cb].nl_cmd = cmd;
78 info->event_cb[info->num_event_cb].vendor_id = 0;
79 info->event_cb[info->num_event_cb].vendor_subcmd = 0;
80 info->event_cb[info->num_event_cb].cb_func = func;
81 info->event_cb[info->num_event_cb].cb_arg = arg;
82 info->num_event_cb++;
83 ALOGV("Successfully added event handler %p for command %d", func, cmd);
84 result = WIFI_SUCCESS;
85 } else {
86 result = WIFI_ERROR_OUT_OF_MEMORY;
87 }
88
89 pthread_mutex_unlock(&info->cb_lock);
90 return result;
91 }
92
wifi_register_vendor_handler(wifi_handle handle,uint32_t id,int subcmd,nl_recvmsg_msg_cb_t func,void * arg)93 wifi_error wifi_register_vendor_handler(wifi_handle handle,
94 uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg)
95 {
96 hal_info *info = (hal_info *)handle;
97
98 pthread_mutex_lock(&info->cb_lock);
99
100 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
101
102 for (int i = 0; i < info->num_event_cb; i++) {
103 if(info->event_cb[i].vendor_id == id &&
104 info->event_cb[i].vendor_subcmd == subcmd)
105 {
106 info->event_cb[i].cb_func = func;
107 info->event_cb[i].cb_arg = arg;
108 ALOGV("Updated event handler %p for vendor 0x%0x, subcmd 0x%0x"
109 " and arg %p", func, id, subcmd, arg);
110 pthread_mutex_unlock(&info->cb_lock);
111 return WIFI_SUCCESS;
112 }
113 }
114
115 if (info->num_event_cb < info->alloc_event_cb) {
116 info->event_cb[info->num_event_cb].nl_cmd = NL80211_CMD_VENDOR;
117 info->event_cb[info->num_event_cb].vendor_id = id;
118 info->event_cb[info->num_event_cb].vendor_subcmd = subcmd;
119 info->event_cb[info->num_event_cb].cb_func = func;
120 info->event_cb[info->num_event_cb].cb_arg = arg;
121 info->num_event_cb++;
122 ALOGV("Added event handler %p for vendor 0x%0x, subcmd 0x%0x and arg"
123 " %p", func, id, subcmd, arg);
124 result = WIFI_SUCCESS;
125 } else {
126 result = WIFI_ERROR_OUT_OF_MEMORY;
127 }
128
129 pthread_mutex_unlock(&info->cb_lock);
130 return result;
131 }
132
wifi_unregister_handler(wifi_handle handle,int cmd)133 void wifi_unregister_handler(wifi_handle handle, int cmd)
134 {
135 hal_info *info = (hal_info *)handle;
136
137 if (cmd == NL80211_CMD_VENDOR) {
138 ALOGE("Must use wifi_unregister_vendor_handler to remove vendor handlers");
139 return;
140 }
141
142 pthread_mutex_lock(&info->cb_lock);
143
144 for (int i = 0; i < info->num_event_cb; i++) {
145 if (info->event_cb[i].nl_cmd == cmd) {
146 if(i < info->num_event_cb-1) {
147 /* No need to memmove if only one entry exist and deleting
148 * the same, as the num_event_cb will become 0 in this case.
149 */
150 memmove(&info->event_cb[i], &info->event_cb[i+1],
151 (info->num_event_cb - i) * sizeof(cb_info));
152 }
153 info->num_event_cb--;
154 ALOGV("Successfully removed event handler for command %d", cmd);
155 break;
156 }
157 }
158
159 pthread_mutex_unlock(&info->cb_lock);
160 }
161
wifi_unregister_vendor_handler(wifi_handle handle,uint32_t id,int subcmd)162 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd)
163 {
164 hal_info *info = (hal_info *)handle;
165
166 pthread_mutex_lock(&info->cb_lock);
167
168 for (int i = 0; i < info->num_event_cb; i++) {
169
170 if (info->event_cb[i].nl_cmd == NL80211_CMD_VENDOR
171 && info->event_cb[i].vendor_id == id
172 && info->event_cb[i].vendor_subcmd == subcmd) {
173 if(i < info->num_event_cb-1) {
174 /* No need to memmove if only one entry exist and deleting
175 * the same, as the num_event_cb will become 0 in this case.
176 */
177 memmove(&info->event_cb[i], &info->event_cb[i+1],
178 (info->num_event_cb - i) * sizeof(cb_info));
179 }
180 info->num_event_cb--;
181 ALOGV("Successfully removed event handler for vendor 0x%0x", id);
182 break;
183 }
184 }
185
186 pthread_mutex_unlock(&info->cb_lock);
187 }
188
189
190 #ifdef __cplusplus
191 extern "C"
192 {
193 #endif /* __cplusplus */
194
hexdump(void * buf,u16 len)195 void hexdump(void *buf, u16 len)
196 {
197 int i=0;
198 char *bytes = (char *)buf;
199
200 if (len) {
201 ALOGV("******HexDump len:%d*********", len);
202 for (i = 0; ((i + 7) < len); i+=8) {
203 ALOGV("%02x %02x %02x %02x %02x %02x %02x %02x",
204 bytes[i], bytes[i+1],
205 bytes[i+2], bytes[i+3],
206 bytes[i+4], bytes[i+5],
207 bytes[i+6], bytes[i+7]);
208 }
209 if ((len - i) >= 4) {
210 ALOGV("%02x %02x %02x %02x",
211 bytes[i], bytes[i+1],
212 bytes[i+2], bytes[i+3]);
213 i+=4;
214 }
215 for (;i < len;i++) {
216 ALOGV("%02x", bytes[i]);
217 }
218 ALOGV("******HexDump End***********");
219 } else {
220 return;
221 }
222 }
223
224 /* Firmware sends RSSI value without noise floor.
225 * Add noise floor to the same and return absolute values.
226 */
get_rssi(u8 rssi_wo_noise_floor)227 u8 get_rssi(u8 rssi_wo_noise_floor)
228 {
229 return abs((int)rssi_wo_noise_floor - 96);
230 }
231
232 #ifdef __cplusplus
233 }
234 #endif /* __cplusplus */
235
236 /* Pointer to the table of LOWI callback funcs */
237 lowi_cb_table_t *LowiWifiHalApi = NULL;
238 /* LowiSupportedCapabilities read */
239 u32 lowiSupportedCapabilities = 0;
240
compareLowiVersion(u16 major,u16 minor,u16 micro)241 int compareLowiVersion(u16 major, u16 minor, u16 micro)
242 {
243 u32 currVersion = 0x10000*(WIFIHAL_LOWI_MAJOR_VERSION) + \
244 0x100*(WIFIHAL_LOWI_MINOR_VERSION) + \
245 WIFIHAL_LOWI_MICRO_VERSION;
246
247 u32 lowiVersion = 0x10000*(major) + \
248 0x100*(minor) + \
249 micro;
250
251 return (memcmp(&currVersion, &lowiVersion, sizeof(u32)));
252 }
253
254 /*
255 * This function will open the lowi shared library and obtain the
256 * Lowi Callback table and the capabilities supported.
257 * A version check is also performed in this function and if the version
258 * check fails then the callback table returned will be NULL.
259 */
fetchLowiCbTableAndCapabilities(lowi_cb_table_t ** lowi_wifihal_api,bool * lowi_get_capa_supported)260 wifi_error fetchLowiCbTableAndCapabilities(lowi_cb_table_t **lowi_wifihal_api,
261 bool *lowi_get_capa_supported)
262 {
263 getCbTable_t* lowiCbTable = NULL;
264 int ret = 0;
265 wifi_error retVal = WIFI_SUCCESS;
266
267 *lowi_wifihal_api = NULL;
268 *lowi_get_capa_supported = false;
269
270 #if __WORDSIZE == 64
271 void* lowi_handle = dlopen("/vendor/lib64/liblowi_wifihal.so", RTLD_NOW);
272 #else
273 void* lowi_handle = dlopen("/vendor/lib/liblowi_wifihal.so", RTLD_NOW);
274 #endif
275 if (!lowi_handle) {
276 ALOGE("%s: NULL lowi_handle, err: %s", __FUNCTION__, dlerror());
277 return WIFI_ERROR_UNKNOWN;
278 }
279
280 lowiCbTable = (getCbTable_t*)dlsym(lowi_handle,
281 "lowi_wifihal_get_cb_table");
282 if (!lowiCbTable) {
283 ALOGE("%s: NULL lowi callback table", __FUNCTION__);
284 return WIFI_ERROR_UNKNOWN;
285 }
286
287 *lowi_wifihal_api = lowiCbTable();
288
289 /* First check whether lowi module implements the get_lowi_version
290 * function. All the functions in lowi module starts with
291 * "lowi_wifihal_" prefix thus the below function name.
292 */
293 if ((dlsym(lowi_handle, "lowi_wifihal_get_lowi_version") != NULL) &&
294 ((*lowi_wifihal_api)->get_lowi_version != NULL)) {
295 u16 lowiMajorVersion = WIFIHAL_LOWI_MAJOR_VERSION;
296 u16 lowiMinorVersion = WIFIHAL_LOWI_MINOR_VERSION;
297 u16 lowiMicroVersion = WIFIHAL_LOWI_MICRO_VERSION;
298 int versionCheck = -1;
299
300 ret = (*lowi_wifihal_api)->get_lowi_version(&lowiMajorVersion,
301 &lowiMinorVersion,
302 &lowiMicroVersion);
303 if (ret) {
304 ALOGE("%s: get_lowi_version returned error:%d",
305 __FUNCTION__, ret);
306 retVal = WIFI_ERROR_NOT_SUPPORTED;
307 goto cleanup;
308 }
309 ALOGV("%s: Lowi version:%d.%d.%d", __FUNCTION__,
310 lowiMajorVersion, lowiMinorVersion,
311 lowiMicroVersion);
312
313 /* Compare the version with version in wifihal_internal.h */
314 versionCheck = compareLowiVersion(lowiMajorVersion,
315 lowiMinorVersion,
316 lowiMicroVersion);
317 if (versionCheck < 0) {
318 ALOGE("%s: Version Check failed:%d", __FUNCTION__,
319 versionCheck);
320 retVal = WIFI_ERROR_NOT_SUPPORTED;
321 goto cleanup;
322 }
323 }
324 else {
325 ALOGV("%s: lowi_wifihal_get_lowi_version not present",
326 __FUNCTION__);
327 }
328
329
330 /* Check if get_lowi_capabilities func pointer exists in
331 * the lowi lib and populate lowi_get_capa_supported
332 * All the functions in lowi modules starts with
333 * "lowi_wifihal_ prefix" thus the below function name.
334 */
335 if (dlsym(lowi_handle, "lowi_wifihal_get_lowi_capabilities") != NULL) {
336 *lowi_get_capa_supported = true;
337 }
338 else {
339 ALOGV("lowi_wifihal_get_lowi_capabilities() is not supported.");
340 *lowi_get_capa_supported = false;
341 }
342 cleanup:
343 if (retVal) {
344 *lowi_wifihal_api = NULL;
345 }
346 return retVal;
347 }
348
getLowiCallbackTable(u32 requested_lowi_capabilities)349 lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities)
350 {
351 int ret = WIFI_SUCCESS;
352 bool lowi_get_capabilities_support = false;
353
354 if (LowiWifiHalApi == NULL) {
355 ALOGV("%s: LowiWifiHalApi Null, Initialize Lowi",
356 __FUNCTION__);
357 ret = fetchLowiCbTableAndCapabilities(&LowiWifiHalApi,
358 &lowi_get_capabilities_support);
359 if (ret != WIFI_SUCCESS || LowiWifiHalApi == NULL ||
360 LowiWifiHalApi->init == NULL) {
361 ALOGE("%s: LOWI is not supported.", __FUNCTION__);
362 goto cleanup;
363 }
364 /* Initialize LOWI if it isn't up already. */
365 ret = LowiWifiHalApi->init();
366 if (ret) {
367 ALOGE("%s: failed lowi initialization. "
368 "Returned error:%d. Exit.", __FUNCTION__, ret);
369 goto cleanup;
370 }
371 if (!lowi_get_capabilities_support ||
372 LowiWifiHalApi->get_lowi_capabilities == NULL) {
373 ALOGV("%s: Allow rtt APIs thru LOWI to proceed even though "
374 "get_lowi_capabilities() is not supported. Returning",
375 __FUNCTION__);
376 lowiSupportedCapabilities |=
377 (ONE_SIDED_RANGING_SUPPORTED|DUAL_SIDED_RANGING_SUPPORED);
378 return LowiWifiHalApi;
379 }
380 ret =
381 LowiWifiHalApi->get_lowi_capabilities(&lowiSupportedCapabilities);
382 if (ret) {
383 ALOGV("%s: failed to get lowi supported capabilities."
384 "Returned error:%d. Exit.", __FUNCTION__, ret);
385 goto cleanup;
386 }
387 }
388
389 if ((lowiSupportedCapabilities & requested_lowi_capabilities) == 0) {
390 return NULL;
391 }
392 return LowiWifiHalApi;
393
394 cleanup:
395 if (LowiWifiHalApi && LowiWifiHalApi->destroy) {
396 ret = LowiWifiHalApi->destroy();
397 }
398 LowiWifiHalApi = NULL;
399 lowiSupportedCapabilities = 0;
400 return LowiWifiHalApi;
401 }
402
mapKernelErrortoWifiHalError(int kern_err)403 wifi_error mapKernelErrortoWifiHalError(int kern_err)
404 {
405 if (kern_err >= 0)
406 return WIFI_SUCCESS;
407
408 switch (kern_err) {
409 case -EOPNOTSUPP:
410 return WIFI_ERROR_NOT_SUPPORTED;
411 case -EAGAIN:
412 return WIFI_ERROR_NOT_AVAILABLE;
413 case -EINVAL:
414 return WIFI_ERROR_INVALID_ARGS;
415 case -ETIMEDOUT:
416 return WIFI_ERROR_TIMED_OUT;
417 case -ENOMEM:
418 return WIFI_ERROR_OUT_OF_MEMORY;
419 case -EBUSY:
420 return WIFI_ERROR_BUSY;
421 case -ENOBUFS:
422 return WIFI_ERROR_TOO_MANY_REQUESTS;
423 }
424 return WIFI_ERROR_UNKNOWN;
425 }
426