2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/prctl.h>
28 #include <bluetooth.h>
30 #include "oal-internal.h"
31 #include "oal-event.h"
32 #include <oal-hardware.h>
34 #define BT_HAL_LIB_NAME "libbluetooth.default.so"
35 #define HAL_LIBRARY_PATH "/usr/lib"
36 #define LIB_PATH_SIZE 50
37 #define LIB_NAME_SIZE 50
39 static const hw_module_t* module = NULL;
40 static const bt_interface_t *blued_api = NULL;
42 static gboolean unload_libs(gpointer data);
43 static bluetooth_device_t* load_hal_lib(void);
44 static const bt_interface_t * get_stack_interface(bluetooth_device_t* bt_device);
45 static int load(const char *libname, const struct hw_module_t **module);
46 static int unload(const struct hw_module_t *module);
48 oal_status_t oal_mgr_init_internal(void)
50 bluetooth_device_t* bt_device;
52 bt_device = load_hal_lib();
54 if (bt_device == NULL) {
55 BT_ERR("HAL Library loading failed");
56 return OAL_STATUS_INTERNAL_ERROR;
59 blued_api = get_stack_interface(bt_device);
61 if (blued_api == NULL) {
62 BT_ERR("Stack Interface failed");
63 return OAL_STATUS_INTERNAL_ERROR;
66 device_mgr_init(blued_api);
68 return adapter_mgr_init(blued_api);
71 oal_status_t oal_bt_init(oal_event_callback cb)
73 API_TRACE("Version: %s", OAL_VERSION_STR);
74 _bt_event_dispatcher_init(cb);
75 return OAL_STATUS_PENDING;
78 void oal_bt_deinit(void)
87 void oal_mgr_cleanup(void)
92 void oal_mgr_stack_reload(void)
97 gboolean oal_lib_init(gpointer data)
100 BT_INFO("Going to check Chip Attachment...");
102 if (hw_is_module_ready() == OAL_STATUS_SUCCESS) {
103 if (hw_get_chip_type() == BT_CHIP_TYPE_UNKNOWN) {
104 BT_DBG("Chip Type Unknown, starting timer...");
106 ret = oal_mgr_init_internal();
107 if (OAL_STATUS_SUCCESS == ret)
108 send_event(OAL_EVENT_OAL_INITIALISED_SUCCESS, NULL, 0);
110 send_event(OAL_EVENT_OAL_INITIALISED_FAILED, NULL, 0);
113 BT_DBG("Chip Not Yet Ready, try again...");
119 static gboolean unload_libs(gpointer data)
121 unload((hw_module_t const*)module);
126 static bluetooth_device_t* load_hal_lib(void)
130 bluetooth_device_t* bt_device = NULL;
132 BT_DBG("Loading HAL lib");
133 if (module == NULL) {
134 switch (hw_get_chip_type()) {
135 case BT_CHIP_TYPE_PLATFORM:
136 BT_INFO("Tizen Platform BT chip: Tizen Platform HAL library will be loaded");
137 err = load(BT_HAL_LIB_NAME, (const hw_module_t **)&module);
140 BT_WARN("Chip type Unknown, So no Library Load");
145 BT_WARN("Lib already loaded");
148 err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
150 bt_device = (bluetooth_device_t *)device;
151 BT_INFO("HAL Library loaded successfullly");
160 static const bt_interface_t * get_stack_interface(bluetooth_device_t* bt_device)
162 const bt_interface_t *blued_api = NULL;
163 /* Get the Bluetooth interface */
164 blued_api = bt_device->get_bluetooth_interface();
169 static int load(const char *libname, const struct hw_module_t **module)
171 int status = -ENOENT;
172 char libpath[LIB_PATH_SIZE];
174 struct hw_module_t *hmi;
176 OAL_CHECK_PARAMETER(libname, return);
178 snprintf(libpath, sizeof(libpath), "%s/%s", HAL_LIBRARY_PATH, libname);
179 BT_INFO("Loading Library: %s", libpath);
182 * load the symbols resolving undefined symbols before
183 * dlopen returns. Since RTLD_GLOBAL is not or'd in with
184 * RTLD_NOW the external symbols will not be global
187 prctl(666, "[bt-service] Load Lib S", strlen("[bt-service] Load Lib S"));
189 handle = dlopen(libpath, RTLD_NOW);
190 if (handle == NULL) {
191 char const *err_str = dlerror();
192 BT_ERR("load: module=%s\n%s", libpath, err_str ? err_str : "unknown");
197 prctl(666, "[bt-service] Load Lib E", strlen("[bt-service] Load Lib E"));
199 /* Get the address of the struct hal_module_info. */
200 const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
201 hmi = (struct hw_module_t *)dlsym(handle, sym);
203 BT_ERR("load: couldn't find symbol %s", sym);
208 /* Check that the id matches */
209 if (strcmp(BT_HARDWARE_MODULE_ID, hmi->id) != 0) {
210 BT_ERR("load: id=%s != hmi->id=%s", BT_HARDWARE_MODULE_ID, hmi->id);
221 if (handle != NULL) {
226 BT_DBG("loaded HAL id=%s libpath=%s hmi=%p handle=%p",
227 BT_HARDWARE_MODULE_ID, libpath, hmi, handle);
233 static int unload(const struct hw_module_t *module)
238 ret = dlclose(module->dso);
241 BT_ERR("dlclose failed:%d", ret);
242 BT_WARN("Issues with dl: %s\n", dlerror());
246 void oal_set_debug_mode(gboolean mode)
248 /*TODO Unsupported */
251 gboolean oal_get_debug_mode(void)
253 /*TODO Unsupported */