Fix SVACE issues in Tizen New BT Architecture
[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 #define HAL_LIBRARY_PATH "/usr/lib"
39
40 /**
41  * Load the file defined by the variant and if successful
42  * return the dlopen handle and the hmi.
43  * @return 0 = success, !0 = failure.
44  */
45 static int load(const char *id,
46                 const char *path,
47                 const struct hw_module_t **pHmi)
48 {
49         int status;
50         void *handle;
51         struct hw_module_t *hmi;
52         const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
53
54         /*
55          * load the symbols resolving undefined symbols before
56          * dlopen returns. Since RTLD_GLOBAL is not or'd in with
57          * RTLD_NOW the external symbols will not be global
58          */
59         handle = dlopen(path, RTLD_NOW);
60         if (handle == NULL) {
61                 char const *err_str = dlerror();
62                 error("load: module=%s\n%s", path, err_str?err_str:"unknown");
63                 status = -EINVAL;
64                 goto done;
65         }
66
67         /* Get the address of the struct hal_module_info. */
68         hmi = (struct hw_module_t *)dlsym(handle, sym);
69         if (hmi == NULL) {
70                 error("load: couldn't find symbol %s", sym);
71                 status = -EINVAL;
72                 goto done;
73         }
74
75         /* Check that the id matches */
76         if (strcmp(id, hmi->id) != 0) {
77                 error("load: id=%s != hmi->id=%s", id, hmi->id);
78                 status = -EINVAL;
79                 goto done;
80         }
81
82         hmi->dso = handle;
83
84         *pHmi = hmi;
85
86         info("loaded HAL id=%s path=%s hmi=%p handle=%p",
87                         id, path, *pHmi, handle);
88
89         return 0;
90
91 done:
92         hmi = NULL;
93         if (handle != NULL) {
94                 dlclose(handle);
95                 handle = NULL;
96         }
97
98         return status;
99 }
100
101 int hw_get_module_by_class(const char *class_id, const char *inst,
102                 const struct hw_module_t **module)
103 {
104         char path[PATH_MAX];
105         char name[PATH_MAX];
106
107         if (inst)
108                 snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
109         else
110                 snprintf(name, PATH_MAX, "%s", class_id);
111
112         /*
113          * Here we rely on the fact that calling dlopen multiple times on
114          * the same .so will simply increment a refcount (and not load
115          * a new copy of the library).
116          * We also assume that dlopen() is thread-safe.
117          */
118         snprintf(path, sizeof(path), "%s/%s.default.so", HAL_LIBRARY_PATH, name);
119
120         return load(class_id, path, module);
121 }
122
123 int hw_get_module(const char *id, const struct hw_module_t **module)
124 {
125         return hw_get_module_by_class(id, NULL, module);
126 }