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.
23 #include <sys/types.h>
26 #include "bluetooth-api.h"
27 #include "bluetooth-audio-api.h"
28 #include "bluetooth-hid-api.h"
29 #include "bluetooth-media-control.h"
30 #include "bt-internal-types.h"
32 #include "bt-common.h"
33 #include "bt-request-sender.h"
34 #include "bt-event-handler.h"
36 static bt_user_info_t user_info[BT_MAX_USER_INFO];
37 static DBusGConnection *system_conn = NULL;
39 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
41 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
42 addr->addr[3], addr->addr[4], addr->addr[5]);
45 void _bt_set_user_data(int type, void *callback, void *user_data)
47 user_info[type].cb = callback;
48 user_info[type].user_data = user_data;
51 bt_user_info_t *_bt_get_user_data(int type)
53 return &user_info[type];
56 void _bt_common_event_cb(int event, int result, void *param,
57 void *callback, void *user_data)
59 bluetooth_event_param_t bt_event = { 0, };
60 bt_event.event = event;
61 bt_event.result = result;
62 bt_event.param_data = param;
65 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
69 void _bt_input_event_cb(int event, int result, void *param,
70 void *callback, void *user_data)
72 hid_event_param_t bt_event = { 0, };
73 bt_event.event = event;
74 bt_event.result = result;
75 bt_event.param_data = param;
78 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
82 void _bt_headset_event_cb(int event, int result, void *param,
83 void *callback, void *user_data)
85 bt_audio_event_param_t bt_event = { 0, };
86 bt_event.event = event;
87 bt_event.result = result;
88 bt_event.param_data = param;
91 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
95 void _bt_avrcp_event_cb(int event, int result, void *param,
96 void *callback, void *user_data)
98 media_event_param_t bt_event = { 0, };
99 bt_event.event = event;
100 bt_event.result = result;
101 bt_event.param_data = param;
104 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
108 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
111 ret_if(device_class == NULL);
113 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
114 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
115 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
117 if (cod & 0x002000) {
118 device_class->service_class |=
119 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
123 void _bt_convert_addr_string_to_type(unsigned char *addr,
129 ret_if(address == NULL);
130 ret_if(addr == NULL);
132 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
133 addr[i] = strtol(address, &ptr, 16);
143 void _bt_convert_addr_type_to_string(char *address,
146 ret_if(address == NULL);
147 ret_if(addr == NULL);
149 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
150 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
151 addr[0], addr[1], addr[2],
152 addr[3], addr[4], addr[5]);
155 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
162 if (dest == NULL || src == NULL)
163 return BLUETOOTH_ERROR_INVALID_PARAM;
166 while (*p != '\0' && i < length) {
167 next = g_utf8_next_char(p);
170 while (count > 0 && ((i + count) < length)) {
177 return BLUETOOTH_ERROR_NONE;
180 int _bt_get_adapter_path(DBusGConnection *conn, char *path)
183 DBusGProxy *manager_proxy = NULL;
184 char *adapter_path = NULL;
185 int ret = BLUETOOTH_ERROR_NONE;
187 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
189 manager_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
190 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
192 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
194 if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &err,
195 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
199 BT_ERR("Getting DefaultAdapter failed: [%s]\n", err->message);
202 g_object_unref(manager_proxy);
203 return BLUETOOTH_ERROR_INTERNAL;
206 if (adapter_path == NULL || strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
207 BT_ERR("Adapter path is inproper\n");
208 ret = BLUETOOTH_ERROR_INTERNAL;
213 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
215 g_free(adapter_path);
216 g_object_unref(manager_proxy);
221 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
224 DBusGProxy *manager_proxy = NULL;
225 DBusGProxy *adapter_proxy = NULL;
226 char *adapter_path = NULL;
228 retv_if(conn == NULL, NULL);
230 manager_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
231 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
233 retv_if(manager_proxy == NULL, NULL);
235 if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &err,
236 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
240 BT_ERR("Getting DefaultAdapter failed: [%s]\n", err->message);
243 g_object_unref(manager_proxy);
247 if (adapter_path == NULL || strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
248 BT_ERR("Adapter path is inproper\n");
249 g_free(adapter_path);
250 g_object_unref(manager_proxy);
254 adapter_proxy = dbus_g_proxy_new_for_name(conn,
257 BT_ADAPTER_INTERFACE);
258 g_free(adapter_path);
259 g_object_unref(manager_proxy);
261 return adapter_proxy;
264 void _bt_device_path_to_address(const char *device_path, char *device_address)
266 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
267 char *dev_addr = NULL;
269 if (!device_path || !device_address)
272 dev_addr = strstr(device_path, "dev_");
273 if (dev_addr != NULL) {
276 g_strlcpy(address, dev_addr, sizeof(address));
278 while ((pos = strchr(address, '_')) != NULL) {
282 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
286 DBusGConnection *__bt_init_system_gconn(void)
290 if (system_conn == NULL)
291 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
296 DBusGConnection *_bt_get_system_gconn(void)
298 return (system_conn) ? system_conn : __bt_init_system_gconn();
301 DBusConnection *_bt_get_system_conn(void)
303 DBusGConnection *g_conn;
305 if (system_conn == NULL) {
306 g_conn = __bt_init_system_gconn();
308 g_conn = system_conn;
311 retv_if(g_conn == NULL, NULL);
313 return dbus_g_connection_get_connection(g_conn);
316 BT_EXPORT_API int bluetooth_is_supported(void)
318 int is_supported = 0;
323 fd = open(RFKILL_NODE, O_RDONLY);
325 BT_DBG("Fail to open RFKILL node");
326 return BLUETOOTH_ERROR_INTERNAL;
329 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
330 BT_DBG("Fail to set RFKILL node to non-blocking");
332 return BLUETOOTH_ERROR_INTERNAL;
336 len = read(fd, &event, sizeof(event));
338 BT_DBG("Fail to read events");
342 if (len != RFKILL_EVENT_SIZE) {
343 BT_DBG("The size is wrong\n");
347 if (event.type == RFKILL_TYPE_BLUETOOTH) {
355 BT_DBG("supported: %d", is_supported);
360 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
364 __bt_init_system_gconn();
366 ret = _bt_init_event_handler();
368 if (ret != BLUETOOTH_ERROR_NONE &&
369 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
370 BT_ERR("Fail to init the event handler");
374 _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
376 /* Register All events */
377 _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
378 _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
379 _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
380 _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
381 _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
383 return BLUETOOTH_ERROR_NONE;
386 BT_EXPORT_API int bluetooth_unregister_callback(void)
388 _bt_unregister_event(BT_ADAPTER_EVENT);
389 _bt_unregister_event(BT_DEVICE_EVENT);
390 _bt_unregister_event(BT_NETWORK_EVENT);
391 _bt_unregister_event(BT_RFCOMM_CLIENT_EVENT);
392 _bt_unregister_event(BT_RFCOMM_SERVER_EVENT);
394 _bt_set_user_data(BT_COMMON, NULL, NULL);
397 dbus_g_connection_unref(system_conn);
401 return BLUETOOTH_ERROR_NONE;