Merge branch 'tizen' into tizen_5.5
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-manager.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <dlog.h>
22 #include <string.h>
23 #include <vconf.h>
24 #include <sys/prctl.h>
25 #include <unistd.h>
26 #include <dlfcn.h>
27
28 #include <bluetooth.h>
29
30 #include "oal-internal.h"
31 #include "oal-event.h"
32 #include <oal-hardware.h>
33
34 #define BT_HAL_LIB_NAME                 "libbluetooth.default.so"
35
36 #ifdef ARCH64
37 #define HAL_LIBRARY_PATH                "/usr/lib64"
38 #else
39 #define HAL_LIBRARY_PATH                "/usr/lib"
40 #endif
41
42 #define LIB_PATH_SIZE                   50
43 #define LIB_NAME_SIZE                   50
44
45 static const hw_module_t* module = NULL;
46 static const bt_interface_t *blued_api = NULL;
47
48 static gboolean unload_libs(gpointer data);
49 static bluetooth_device_t* load_hal_lib(void);
50 static const bt_interface_t * get_stack_interface(bluetooth_device_t* bt_device);
51 static int load(const char *libname, const struct hw_module_t **module);
52 static int unload(const struct hw_module_t *module);
53
54 oal_status_t oal_mgr_init_internal(void)
55 {
56         bluetooth_device_t* bt_device;
57
58         bt_device = load_hal_lib();
59
60         if (bt_device == NULL) {
61                 BT_ERR("HAL Library loading failed");
62                 return OAL_STATUS_INTERNAL_ERROR;
63         }
64
65         blued_api = get_stack_interface(bt_device);
66
67         if (blued_api == NULL) {
68                 BT_ERR("Stack Interface failed");
69                 return OAL_STATUS_INTERNAL_ERROR;
70         }
71
72         device_mgr_init(blued_api);
73
74         return adapter_mgr_init(blued_api);
75 }
76
77 oal_status_t oal_bt_init(oal_event_callback cb)
78 {
79         API_TRACE("Version: %s", OAL_VERSION_STR);
80         _bt_event_dispatcher_init(cb);
81         return OAL_STATUS_PENDING;
82 }
83
84 void oal_bt_deinit(void)
85 {
86         BT_INFO("+");
87         if (blued_api) {
88                 blued_api->cleanup();
89                 blued_api = NULL;
90         }
91         _bt_event_dispatcher_deinit();
92         sleep(1);
93         unload_libs(NULL);
94         BT_INFO("-");
95 }
96
97 void oal_mgr_cleanup(void)
98 {
99         /*TODO Unsupported */
100 }
101
102 void oal_mgr_stack_reload(void)
103 {
104         /*TODO Unsupported */
105 }
106
107 gboolean oal_lib_init(gpointer data)
108 {
109         oal_status_t ret;
110         BT_INFO("Going to check Chip Attachment...");
111
112         if (hw_is_module_ready() == OAL_STATUS_SUCCESS) {
113                 if (hw_get_chip_type() == BT_CHIP_TYPE_UNKNOWN) {
114                         BT_DBG("Chip Type Unknown, starting timer...");
115                 } else {
116                         ret = oal_mgr_init_internal();
117                         if (OAL_STATUS_SUCCESS == ret)
118                                 send_event(OAL_EVENT_OAL_INITIALISED_SUCCESS, NULL, 0);
119                         else
120                                 send_event(OAL_EVENT_OAL_INITIALISED_FAILED, NULL, 0);
121                 }
122         } else {
123                 BT_DBG("Chip Not Yet Ready, try again...");
124                 return FALSE;
125         }
126         return TRUE;
127 }
128
129 static gboolean unload_libs(gpointer data)
130 {
131         unload((hw_module_t const*)module);
132         module = NULL;
133         return FALSE;
134 }
135
136 static bluetooth_device_t* load_hal_lib(void)
137 {
138         int err = 0;
139         hw_device_t* device;
140         bluetooth_device_t* bt_device = NULL;
141
142         BT_DBG("Loading HAL lib");
143         if (module == NULL) {
144                 switch (hw_get_chip_type()) {
145                 case BT_CHIP_TYPE_PLATFORM:
146                         BT_INFO("Tizen Platform BT chip: Tizen Platform HAL library will be loaded");
147                         err = load(BT_HAL_LIB_NAME, (const hw_module_t **)&module);
148                         break;
149                 default:
150                         BT_WARN("Chip type Unknown, So no Library Load");
151                         err = -EINVAL;
152                         break;
153                 }
154         } else
155                 BT_WARN("Lib already loaded");
156
157         if (err == 0) {
158                 err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
159                 if (err == 0) {
160                         bt_device = (bluetooth_device_t *)device;
161                         BT_INFO("HAL Library loaded successfullly");
162                 }
163         }
164
165         if (err != 0)
166                 BT_INFO("%d", err);
167         return bt_device;
168 }
169
170 static const bt_interface_t * get_stack_interface(bluetooth_device_t* bt_device)
171 {
172         const bt_interface_t *blued_api = NULL;
173         /* Get the Bluetooth interface */
174         blued_api = bt_device->get_bluetooth_interface();
175
176         return blued_api;
177 }
178
179 static int load(const char *libname, const struct hw_module_t **module)
180 {
181         int status = -ENOENT;
182         char libpath[LIB_PATH_SIZE];
183         void *handle;
184         struct hw_module_t *hmi;
185
186         OAL_CHECK_PARAMETER(libname, return);
187
188         snprintf(libpath, sizeof(libpath), "%s/%s", HAL_LIBRARY_PATH, libname);
189         BT_INFO("Loading Library: %s", libpath);
190
191         /*
192          * load the symbols resolving undefined symbols before
193          * dlopen returns. Since RTLD_GLOBAL is not or'd in with
194          * RTLD_NOW the external symbols will not be global
195          */
196
197         prctl(666, "[bt-service] Load Lib S", strlen("[bt-service] Load Lib S"));
198
199         handle = dlopen(libpath, RTLD_NOW);
200         if (handle == NULL) {
201                 char const *err_str = dlerror();
202                 BT_ERR("load: module=%s\n%s", libpath, err_str ? err_str : "unknown");
203                 status = -EINVAL;
204                 goto done;
205         }
206
207         prctl(666, "[bt-service] Load Lib E", strlen("[bt-service] Load Lib E"));
208
209         /* Get the address of the struct hal_module_info. */
210         const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
211         hmi = (struct hw_module_t *)dlsym(handle, sym);
212         if (hmi == NULL) {
213                 BT_ERR("load: couldn't find symbol %s", sym);
214                 status = -EINVAL;
215                 goto done;
216         }
217
218         /* Check that the id matches */
219         if (strcmp(BT_HARDWARE_MODULE_ID, hmi->id) != 0) {
220                 BT_ERR("load: id=%s != hmi->id=%s", BT_HARDWARE_MODULE_ID, hmi->id);
221                 status = -EINVAL;
222                 goto done;
223         }
224
225         hmi->dso = handle;
226         status = 0;
227
228 done:
229         if (status != 0) {
230                 hmi = NULL;
231                 if (handle != NULL) {
232                         dlclose(handle);
233                         handle = NULL;
234                 }
235         } else {
236                 BT_DBG("loaded HAL id=%s libpath=%s hmi=%p handle=%p",
237                                 BT_HARDWARE_MODULE_ID, libpath, hmi, handle);
238         }
239         *module = hmi;
240         return status;
241 }
242
243 static int unload(const struct hw_module_t *module)
244 {
245         int ret = 1;
246
247         if (module)
248                 ret = dlclose(module->dso);
249
250         if (ret != 0)
251                 BT_ERR("dlclose failed:%d", ret);
252         BT_WARN("Issues with dl: %s\n", dlerror());
253         return ret;
254 }
255
256 void oal_set_debug_mode(gboolean mode)
257 {
258         /*TODO Unsupported */
259 }
260
261 gboolean oal_get_debug_mode(void)
262 {
263         /*TODO Unsupported */
264         return FALSE;
265 }