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"
37 #define HAL_LIBRARY_PATH "/usr/lib64"
39 #define HAL_LIBRARY_PATH "/usr/lib"
42 #define LIB_PATH_SIZE 50
43 #define LIB_NAME_SIZE 50
45 static const hw_module_t* module = NULL;
46 static const bt_interface_t *blued_api = NULL;
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);
54 oal_status_t oal_mgr_init_internal(void)
56 bluetooth_device_t* bt_device;
58 bt_device = load_hal_lib();
60 if (bt_device == NULL) {
61 BT_ERR("HAL Library loading failed");
62 return OAL_STATUS_INTERNAL_ERROR;
65 blued_api = get_stack_interface(bt_device);
67 if (blued_api == NULL) {
68 BT_ERR("Stack Interface failed");
69 return OAL_STATUS_INTERNAL_ERROR;
72 device_mgr_init(blued_api);
74 return adapter_mgr_init(blued_api);
77 oal_status_t oal_bt_init(oal_event_callback cb)
79 API_TRACE("Version: %s", OAL_VERSION_STR);
80 _bt_event_dispatcher_init(cb);
81 return OAL_STATUS_PENDING;
84 void oal_bt_deinit(void)
89 _bt_event_dispatcher_deinit();
95 void oal_mgr_cleanup(void)
100 void oal_mgr_stack_reload(void)
102 /*TODO Unsupported */
105 gboolean oal_lib_init(gpointer data)
108 BT_INFO("Going to check Chip Attachment...");
110 if (hw_is_module_ready() == OAL_STATUS_SUCCESS) {
111 if (hw_get_chip_type() == BT_CHIP_TYPE_UNKNOWN) {
112 BT_DBG("Chip Type Unknown, starting timer...");
114 ret = oal_mgr_init_internal();
115 if (OAL_STATUS_SUCCESS == ret)
116 send_event(OAL_EVENT_OAL_INITIALISED_SUCCESS, NULL, 0);
118 send_event(OAL_EVENT_OAL_INITIALISED_FAILED, NULL, 0);
121 BT_DBG("Chip Not Yet Ready, try again...");
127 static gboolean unload_libs(gpointer data)
129 unload((hw_module_t const*)module);
134 static bluetooth_device_t* load_hal_lib(void)
138 bluetooth_device_t* bt_device = NULL;
140 BT_DBG("Loading HAL lib");
141 if (module == NULL) {
142 switch (hw_get_chip_type()) {
143 case BT_CHIP_TYPE_PLATFORM:
144 BT_INFO("Tizen Platform BT chip: Tizen Platform HAL library will be loaded");
145 err = load(BT_HAL_LIB_NAME, (const hw_module_t **)&module);
148 BT_WARN("Chip type Unknown, So no Library Load");
153 BT_WARN("Lib already loaded");
156 err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device);
158 bt_device = (bluetooth_device_t *)device;
159 BT_INFO("HAL Library loaded successfullly");
168 static const bt_interface_t * get_stack_interface(bluetooth_device_t* bt_device)
170 const bt_interface_t *blued_api = NULL;
171 /* Get the Bluetooth interface */
172 blued_api = bt_device->get_bluetooth_interface();
177 static int load(const char *libname, const struct hw_module_t **module)
179 int status = -ENOENT;
180 char libpath[LIB_PATH_SIZE];
182 struct hw_module_t *hmi;
184 OAL_CHECK_PARAMETER(libname, return);
186 snprintf(libpath, sizeof(libpath), "%s/%s", HAL_LIBRARY_PATH, libname);
187 BT_INFO("Loading Library: %s", libpath);
190 * load the symbols resolving undefined symbols before
191 * dlopen returns. Since RTLD_GLOBAL is not or'd in with
192 * RTLD_NOW the external symbols will not be global
195 prctl(666, "[bt-service] Load Lib S", strlen("[bt-service] Load Lib S"));
197 handle = dlopen(libpath, RTLD_NOW);
198 if (handle == NULL) {
199 char const *err_str = dlerror();
200 BT_ERR("load: module=%s\n%s", libpath, err_str ? err_str : "unknown");
205 prctl(666, "[bt-service] Load Lib E", strlen("[bt-service] Load Lib E"));
207 /* Get the address of the struct hal_module_info. */
208 const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
209 hmi = (struct hw_module_t *)dlsym(handle, sym);
211 BT_ERR("load: couldn't find symbol %s", sym);
216 /* Check that the id matches */
217 if (strcmp(BT_HARDWARE_MODULE_ID, hmi->id) != 0) {
218 BT_ERR("load: id=%s != hmi->id=%s", BT_HARDWARE_MODULE_ID, hmi->id);
229 if (handle != NULL) {
234 BT_DBG("loaded HAL id=%s libpath=%s hmi=%p handle=%p",
235 BT_HARDWARE_MODULE_ID, libpath, hmi, handle);
241 static int unload(const struct hw_module_t *module)
246 ret = dlclose(module->dso);
249 BT_ERR("dlclose failed:%d", ret);
250 BT_WARN("Issues with dl: %s\n", dlerror());
254 void oal_set_debug_mode(gboolean mode)
256 /*TODO Unsupported */
259 gboolean oal_get_debug_mode(void)
261 /*TODO Unsupported */