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 <netlink-types.h>
21 #include <dlfcn.h>
22 
23 #include "wifi_hal.h"
24 #include "common.h"
25 #include <netlink-types.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 
wifi_register_cmd(wifi_handle handle,int id,WifiCommand * cmd)190 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd)
191 {
192     hal_info *info = (hal_info *)handle;
193 
194     if (info->num_cmd < info->alloc_cmd) {
195         info->cmd[info->num_cmd].id   = id;
196         info->cmd[info->num_cmd].cmd  = cmd;
197         info->num_cmd++;
198         ALOGV("Successfully added command %d: %p", id, cmd);
199         return WIFI_SUCCESS;
200     } else {
201         return WIFI_ERROR_OUT_OF_MEMORY;
202     }
203 }
204 
wifi_unregister_cmd(wifi_handle handle,int id)205 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id)
206 {
207     hal_info *info = (hal_info *)handle;
208 
209     for (int i = 0; i < info->num_cmd; i++) {
210         if (info->cmd[i].id == id) {
211             WifiCommand *cmd = info->cmd[i].cmd;
212             memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));
213             info->num_cmd--;
214             ALOGV("Successfully removed command %d: %p", id, cmd);
215             return cmd;
216         }
217     }
218 
219     return NULL;
220 }
221 
wifi_unregister_cmd(wifi_handle handle,WifiCommand * cmd)222 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd)
223 {
224     hal_info *info = (hal_info *)handle;
225 
226     for (int i = 0; i < info->num_cmd; i++) {
227         if (info->cmd[i].cmd == cmd) {
228             int id = info->cmd[i].id;
229             memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));
230             info->num_cmd--;
231             ALOGV("Successfully removed command %d: %p", id, cmd);
232             return;
233         }
234     }
235 }
236 
237 #ifdef __cplusplus
238 extern "C"
239 {
240 #endif /* __cplusplus */
241 
hexdump(void * buf,u16 len)242 void hexdump(void *buf, u16 len)
243 {
244     int i=0;
245     char *bytes = (char *)buf;
246     ALOGV("******HexDump len:%d*********", len);
247     for (i = 0; ((i + 7) < len); i+=8) {
248         ALOGV("%02x %02x %02x %02x   %02x %02x %02x %02x",
249               bytes[i], bytes[i+1],
250               bytes[i+2], bytes[i+3],
251               bytes[i+4], bytes[i+5],
252               bytes[i+6], bytes[i+7]);
253     }
254     if ((len - i) >= 4) {
255         ALOGV("%02x %02x %02x %02x",
256               bytes[i], bytes[i+1],
257               bytes[i+2], bytes[i+3]);
258         i+=4;
259     }
260     for (;i < len;i++) {
261         ALOGV("%02x", bytes[i]);
262     }
263     ALOGV("******HexDump End***********");
264 }
265 
266 /* Firmware sends RSSI value without noise floor.
267  * Add noise floor to the same and return absolute values.
268  */
get_rssi(u8 rssi_wo_noise_floor)269 u8 get_rssi(u8 rssi_wo_noise_floor)
270 {
271     return abs((int)rssi_wo_noise_floor - 96);
272 }
273 
274 #ifdef __cplusplus
275 }
276 #endif /* __cplusplus */
277 
278 /* Pointer to the table of LOWI callback funcs */
279 lowi_cb_table_t *LowiWifiHalApi = NULL;
280 /* LowiSupportedCapabilities read */
281 u32 lowiSupportedCapabilities = 0;
282 
compareLowiVersion(u16 major,u16 minor,u16 micro)283 int compareLowiVersion(u16 major, u16 minor, u16 micro)
284 {
285     u32 currVersion = 0x10000*(WIFIHAL_LOWI_MAJOR_VERSION) + \
286                       0x100*(WIFIHAL_LOWI_MINOR_VERSION) + \
287                       WIFIHAL_LOWI_MICRO_VERSION;
288 
289     u32 lowiVersion = 0x10000*(major) + \
290                       0x100*(minor) + \
291                       micro;
292 
293     return (memcmp(&currVersion, &lowiVersion, sizeof(u32)));
294 }
295 
296 /*
297  * This function will open the lowi shared library and obtain the
298  * Lowi Callback table and the capabilities supported.
299  * A version check is also performed in this function and if the version
300  * check fails then the callback table returned will be NULL.
301  */
fetchLowiCbTableAndCapabilities(lowi_cb_table_t ** lowi_wifihal_api,bool * lowi_get_capa_supported)302 wifi_error fetchLowiCbTableAndCapabilities(lowi_cb_table_t **lowi_wifihal_api,
303                                            bool *lowi_get_capa_supported)
304 {
305     getCbTable_t* lowiCbTable = NULL;
306     int ret = 0;
307     wifi_error retVal = WIFI_SUCCESS;
308 
309     *lowi_wifihal_api = NULL;
310     *lowi_get_capa_supported = false;
311 
312 #if __WORDSIZE == 64
313     void* lowi_handle = dlopen("/vendor/lib64/liblowi_wifihal.so", RTLD_NOW);
314 #else
315     void* lowi_handle = dlopen("/vendor/lib/liblowi_wifihal.so", RTLD_NOW);
316 #endif
317     if (!lowi_handle) {
318         ALOGE("%s: NULL lowi_handle, err: %s", __FUNCTION__, dlerror());
319         return WIFI_ERROR_UNKNOWN;
320     }
321 
322     lowiCbTable = (getCbTable_t*)dlsym(lowi_handle,
323                                        "lowi_wifihal_get_cb_table");
324     if (!lowiCbTable) {
325         ALOGE("%s: NULL lowi callback table", __FUNCTION__);
326         return WIFI_ERROR_UNKNOWN;
327     }
328 
329     *lowi_wifihal_api = lowiCbTable();
330 
331     /* First check whether lowi module implements the get_lowi_version
332      * function. All the functions in lowi module starts with
333      * "lowi_wifihal_" prefix thus the below function name.
334      */
335     if ((dlsym(lowi_handle, "lowi_wifihal_get_lowi_version") != NULL) &&
336         ((*lowi_wifihal_api)->get_lowi_version != NULL)) {
337         u16 lowiMajorVersion = WIFIHAL_LOWI_MAJOR_VERSION;
338         u16 lowiMinorVersion = WIFIHAL_LOWI_MINOR_VERSION;
339         u16 lowiMicroVersion = WIFIHAL_LOWI_MICRO_VERSION;
340         int versionCheck = -1;
341 
342         ret = (*lowi_wifihal_api)->get_lowi_version(&lowiMajorVersion,
343                                                     &lowiMinorVersion,
344                                                     &lowiMicroVersion);
345         if (ret) {
346             ALOGE("%s: get_lowi_version returned error:%d",
347                   __FUNCTION__, ret);
348             retVal = WIFI_ERROR_NOT_SUPPORTED;
349             goto cleanup;
350         }
351         ALOGV("%s: Lowi version:%d.%d.%d", __FUNCTION__,
352               lowiMajorVersion, lowiMinorVersion,
353               lowiMicroVersion);
354 
355         /* Compare the version with version in wifihal_internal.h */
356         versionCheck = compareLowiVersion(lowiMajorVersion,
357                                           lowiMinorVersion,
358                                           lowiMicroVersion);
359         if (versionCheck < 0) {
360             ALOGE("%s: Version Check failed:%d", __FUNCTION__,
361                   versionCheck);
362             retVal = WIFI_ERROR_NOT_SUPPORTED;
363             goto cleanup;
364         }
365     }
366     else {
367         ALOGV("%s: lowi_wifihal_get_lowi_version not present",
368               __FUNCTION__);
369     }
370 
371 
372     /* Check if get_lowi_capabilities func pointer exists in
373      * the lowi lib and populate lowi_get_capa_supported
374      * All the functions in lowi modules starts with
375      * "lowi_wifihal_ prefix" thus the below function name.
376      */
377     if (dlsym(lowi_handle, "lowi_wifihal_get_lowi_capabilities") != NULL) {
378         *lowi_get_capa_supported = true;
379     }
380     else {
381         ALOGV("lowi_wifihal_get_lowi_capabilities() is not supported.");
382         *lowi_get_capa_supported = false;
383     }
384 cleanup:
385     if (retVal) {
386         *lowi_wifihal_api = NULL;
387     }
388     return retVal;
389 }
390 
getLowiCallbackTable(u32 requested_lowi_capabilities)391 lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities)
392 {
393     int ret = WIFI_SUCCESS;
394     bool lowi_get_capabilities_support = false;
395 
396     if (LowiWifiHalApi == NULL) {
397         ALOGV("%s: LowiWifiHalApi Null, Initialize Lowi",
398               __FUNCTION__);
399         ret = fetchLowiCbTableAndCapabilities(&LowiWifiHalApi,
400                                               &lowi_get_capabilities_support);
401         if (ret != WIFI_SUCCESS || LowiWifiHalApi == NULL ||
402             LowiWifiHalApi->init == NULL) {
403             ALOGE("%s: LOWI is not supported.", __FUNCTION__);
404             goto cleanup;
405         }
406         /* Initialize LOWI if it isn't up already. */
407         ret = LowiWifiHalApi->init();
408         if (ret) {
409             ALOGE("%s: failed lowi initialization. "
410                 "Returned error:%d. Exit.", __FUNCTION__, ret);
411             goto cleanup;
412         }
413         if (!lowi_get_capabilities_support ||
414             LowiWifiHalApi->get_lowi_capabilities == NULL) {
415                 ALOGV("%s: Allow rtt APIs thru LOWI to proceed even though "
416                       "get_lowi_capabilities() is not supported. Returning",
417                       __FUNCTION__);
418                 lowiSupportedCapabilities |=
419                     (ONE_SIDED_RANGING_SUPPORTED|DUAL_SIDED_RANGING_SUPPORED);
420                 return LowiWifiHalApi;
421         }
422         ret =
423             LowiWifiHalApi->get_lowi_capabilities(&lowiSupportedCapabilities);
424         if (ret) {
425             ALOGV("%s: failed to get lowi supported capabilities."
426                 "Returned error:%d. Exit.", __FUNCTION__, ret);
427             goto cleanup;
428         }
429     }
430 
431     if ((lowiSupportedCapabilities & requested_lowi_capabilities) == 0) {
432         return NULL;
433     }
434     return LowiWifiHalApi;
435 
436 cleanup:
437     if (LowiWifiHalApi && LowiWifiHalApi->destroy) {
438         ret = LowiWifiHalApi->destroy();
439     }
440     LowiWifiHalApi = NULL;
441     lowiSupportedCapabilities = 0;
442     return LowiWifiHalApi;
443 }
444 
445