Fix bt-service crash on Fhub TCT
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / hardware / hardware.c
1 /*
2  * Copyright (C) 2008 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 <hardware/hardware.h>
18
19 #include <dlfcn.h>
20 #include <string.h>
21 #include <pthread.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25
26 #define LOG_TAG "HAL"
27
28 #define LOG_INFO " I"
29 #define LOG_WARN " W"
30 #define LOG_ERROR " E"
31 #define LOG_DEBUG " D"
32 #define ALOG(pri, tag, fmt, arg...) fprintf(stderr, tag pri": " fmt"\n", ##arg)
33
34 #define info(fmt, arg...) ALOG(LOG_INFO, LOG_TAG, fmt, ##arg)
35 #define warn(fmt, arg...) ALOG(LOG_WARN, LOG_TAG, fmt, ##arg)
36 #define error(fmt, arg...) ALOG(LOG_ERROR, LOG_TAG, fmt, ##arg)
37
38 #ifdef ARCH64
39 #define HAL_LIBRARY_PATH                "/usr/lib64"
40 #else
41 #define HAL_LIBRARY_PATH                "/usr/lib"
42 #endif
43
44 /**
45  * Load the file defined by the variant and if successful
46  * return the dlopen handle and the hmi.
47  * @return 0 = success, !0 = failure.
48  */
49 static int load(const char *id,
50                 const char *path,
51                 const struct hw_module_t **pHmi)
52 {
53         int status;
54         void *handle;
55         struct hw_module_t *hmi;
56         const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
57
58         /*
59          * load the symbols resolving undefined symbols before
60          * dlopen returns. Since RTLD_GLOBAL is not or'd in with
61          * RTLD_NOW the external symbols will not be global
62          */
63         handle = dlopen(path, RTLD_NOW);
64         if (handle == NULL) {
65                 char const *err_str = dlerror();
66                 error("load: module=%s\n%s", path, err_str ? err_str : "unknown");
67                 status = -EINVAL;
68                 goto done;
69         }
70
71         /* Get the address of the struct hal_module_info. */
72         hmi = (struct hw_module_t *)dlsym(handle, sym);
73         if (hmi == NULL) {
74                 error("load: couldn't find symbol %s", sym);
75                 status = -EINVAL;
76                 goto done;
77         }
78
79         /* Check that the id matches */
80         if (strcmp(id, hmi->id) != 0) {
81                 error("load: id=%s != hmi->id=%s", id, hmi->id);
82                 status = -EINVAL;
83                 goto done;
84         }
85
86         hmi->dso = handle;
87
88         *pHmi = hmi;
89
90         info("loaded HAL id=%s path=%s hmi=%p handle=%p",
91                         id, path, *pHmi, handle);
92
93         return 0;
94
95 done:
96         hmi = NULL;
97         if (handle != NULL) {
98                 dlclose(handle);
99                 handle = NULL;
100         }
101
102         return status;
103 }
104
105 int hw_get_module_by_class(const char *class_id, const char *inst,
106                 const struct hw_module_t **module)
107 {
108         char path[PATH_MAX];
109         char name[PATH_MAX];
110
111         if (inst)
112                 snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
113         else
114                 snprintf(name, PATH_MAX, "%s", class_id);
115
116         /*
117          * Here we rely on the fact that calling dlopen multiple times on
118          * the same .so will simply increment a refcount (and not load
119          * a new copy of the library).
120          * We also assume that dlopen() is thread-safe.
121          */
122         snprintf(path, sizeof(path), "%s/%s.default.so", HAL_LIBRARY_PATH, name);
123
124         return load(class_id, path, module);
125 }
126
127 int hw_get_module(const char *id, const struct hw_module_t **module)
128 {
129         return hw_get_module_by_class(id, NULL, module);
130 }