4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
26 #include <dbus/dbus-glib-lowlevel.h>
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus.h>
34 #include <net_connection.h>
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
40 static DBusGConnection *system_conn;
41 static DBusGConnection *session_conn;
42 static DBusGProxy *manager_proxy;
43 static DBusGProxy *adapter_proxy;
44 static void *net_conn;
46 static DBusGProxy *adapter_properties_proxy;
48 static DBusGProxy *__bt_init_manager_proxy(void)
54 if (system_conn == NULL) {
55 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
56 retv_if(system_conn == NULL, NULL);
59 proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
60 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
62 retv_if(proxy == NULL, NULL);
64 manager_proxy = proxy;
69 static DBusGProxy *__bt_init_adapter_proxy(void)
71 DBusGProxy *manager_proxy;
74 if (system_conn == NULL) {
75 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
76 retv_if(system_conn == NULL, NULL);
79 manager_proxy = _bt_get_manager_proxy();
80 retv_if(manager_proxy == NULL, NULL);
82 proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
83 BT_BLUEZ_HCI_PATH, BT_ADAPTER_INTERFACE);
85 retv_if(proxy == NULL, NULL);
87 adapter_proxy = proxy;
92 static DBusGProxy *__bt_init_adapter_properties_proxy(void)
94 DBusGProxy *manager_proxy;
99 if (system_conn == NULL) {
100 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
101 retv_if(system_conn == NULL, NULL);
104 manager_proxy = _bt_get_manager_proxy();
105 retv_if(manager_proxy == NULL, NULL);
107 proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
108 BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
110 retv_if(proxy == NULL, NULL);
112 adapter_properties_proxy = proxy;
117 DBusGConnection *__bt_init_system_gconn(void)
121 if (system_conn == NULL)
122 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
127 DBusGConnection *__bt_init_session_conn(void)
129 if (session_conn == NULL)
130 session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
135 DBusGConnection *_bt_get_session_gconn(void)
137 return (session_conn) ? session_conn : __bt_init_session_conn();
140 DBusGConnection *_bt_get_system_gconn(void)
142 return (system_conn) ? system_conn : __bt_init_system_gconn();
145 DBusConnection *_bt_get_system_conn(void)
147 DBusGConnection *g_conn;
149 if (system_conn == NULL) {
150 g_conn = __bt_init_system_gconn();
152 g_conn = system_conn;
155 retv_if(g_conn == NULL, NULL);
157 return dbus_g_connection_get_connection(g_conn);
160 DBusGProxy *_bt_get_manager_proxy(void)
163 const char *path = dbus_g_proxy_get_path(manager_proxy);
165 BT_ERR("Already proxy released hence creating new proxy");
166 return __bt_init_manager_proxy();
168 return manager_proxy;
170 return __bt_init_manager_proxy();
173 static void *__bt_init_net_conn(void)
176 connection_h connection = NULL;
178 if (net_conn == NULL) {
179 result = connection_create(&connection);
181 if (result != CONNECTION_ERROR_NONE ||
182 connection == NULL) {
183 BT_DBG("connection_create() failed: %d", result);
187 net_conn = connection;
192 void *_bt_get_net_conn(void)
194 return (net_conn) ? net_conn : __bt_init_net_conn();
197 gboolean _bt_get_adapter_power(void)
199 DBusGProxy *proxy = NULL;
201 GValue powered_v = { 0 };
204 proxy = _bt_get_adapter_properties_proxy();
205 retv_if(proxy == NULL, FALSE);
207 if (!dbus_g_proxy_call(proxy, "Get", &err,
208 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
209 G_TYPE_STRING, "Powered",
211 G_TYPE_VALUE, &powered_v,
214 BT_ERR("Getting property failed: [%s]\n", err->message);
220 powered = (gboolean)g_value_get_boolean(&powered_v);
222 BT_DBG("powered = %d", powered);
227 DBusGProxy *_bt_get_adapter_proxy(void)
230 const char *path = dbus_g_proxy_get_path(adapter_proxy);
232 BT_ERR("Already proxy released hence creating new proxy");
233 return __bt_init_adapter_proxy();
236 return adapter_proxy;
238 return __bt_init_adapter_proxy();
242 DBusGProxy *_bt_get_adapter_properties_proxy(void)
244 return (adapter_properties_proxy) ? adapter_properties_proxy :
245 __bt_init_adapter_properties_proxy();
248 static char *__bt_extract_adapter_path(DBusMessageIter *msg_iter)
250 char *object_path = NULL;
251 DBusMessageIter value_iter;
253 /* Parse the signature: oa{sa{sv}}} */
254 retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
255 DBUS_TYPE_OBJECT_PATH, NULL);
257 dbus_message_iter_get_basic(msg_iter, &object_path);
258 retv_if(object_path == NULL, NULL);
260 /* object array (oa) */
261 retv_if(dbus_message_iter_next(msg_iter) == FALSE, NULL);
262 retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
263 DBUS_TYPE_ARRAY, NULL);
265 dbus_message_iter_recurse(msg_iter, &value_iter);
267 /* string array (sa) */
268 while (dbus_message_iter_get_arg_type(&value_iter) ==
269 DBUS_TYPE_DICT_ENTRY) {
270 char *interface_name = NULL;
271 DBusMessageIter interface_iter;
273 dbus_message_iter_recurse(&value_iter, &interface_iter);
275 retv_if(dbus_message_iter_get_arg_type(&interface_iter) !=
276 DBUS_TYPE_STRING, NULL);
278 dbus_message_iter_get_basic(&interface_iter, &interface_name);
280 if (g_strcmp0(interface_name, "org.bluez.Adapter1") == 0) {
281 /* Tizen don't allow the multi-adapter */
282 BT_DBG("Found an adapter: %s", object_path);
283 return g_strdup(object_path);
286 dbus_message_iter_next(&value_iter);
289 BT_DBG("There is no adapter");
294 char *_bt_get_adapter_path(void)
298 DBusMessageIter reply_iter;
299 DBusMessageIter value_iter;
301 DBusConnection *conn;
302 char *adapter_path = NULL;
304 conn = _bt_get_system_conn();
305 retv_if(conn == NULL, NULL);
307 msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
308 BT_MANAGER_INTERFACE,
309 "GetManagedObjects");
311 retv_if(msg == NULL, NULL);
313 /* Synchronous call */
314 dbus_error_init(&err);
315 reply = dbus_connection_send_with_reply_and_block(
318 dbus_message_unref(msg);
321 BT_ERR("Can't get managed objects");
323 if (dbus_error_is_set(&err)) {
324 BT_ERR("%s", err.message);
325 dbus_error_free(&err);
330 if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
331 BT_ERR("Fail to iterate the reply");
332 dbus_message_unref(reply);
336 dbus_message_iter_recurse(&reply_iter, &value_iter);
338 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
339 while (dbus_message_iter_get_arg_type(&value_iter) ==
340 DBUS_TYPE_DICT_ENTRY) {
341 DBusMessageIter msg_iter;
343 dbus_message_iter_recurse(&value_iter, &msg_iter);
345 adapter_path = __bt_extract_adapter_path(&msg_iter);
346 if (adapter_path != NULL) {
347 BT_DBG("Found the adapter path");
351 dbus_message_iter_next(&value_iter);
353 dbus_message_unref(reply);
358 void _bt_deinit_bluez_proxy(void)
361 g_object_unref(manager_proxy);
362 manager_proxy = NULL;
366 g_object_unref(adapter_proxy);
367 adapter_proxy = NULL;
369 if (adapter_properties_proxy) {
370 g_object_unref(adapter_properties_proxy);
371 adapter_properties_proxy = NULL;
375 void _bt_deinit_proxys(void)
378 _bt_deinit_bluez_proxy();
381 dbus_g_connection_unref(system_conn);
386 dbus_g_connection_unref(session_conn);
391 ret = connection_destroy(net_conn);
394 BT_ERR("connection_destroy failed : %d", ret);
398 void _bt_convert_device_path_to_address(const char *device_path,
399 char *device_address)
401 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
404 ret_if(device_path == NULL);
405 ret_if(device_address == NULL);
407 dev_addr = strstr(device_path, "dev_");
408 if (dev_addr != NULL) {
411 g_strlcpy(address, dev_addr, sizeof(address));
413 while ((pos = strchr(address, '_')) != NULL) {
417 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
422 void _bt_convert_addr_string_to_type(unsigned char *addr,
428 ret_if(address == NULL);
429 ret_if(addr == NULL);
431 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
432 addr[i] = strtol(address, &ptr, 16);
433 if (ptr[0] != '\0') {
442 void _bt_convert_addr_type_to_string(char *address,
445 ret_if(address == NULL);
446 ret_if(addr == NULL);
448 snprintf(address, BT_ADDRESS_STRING_SIZE,
449 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
450 addr[0], addr[1], addr[2],
451 addr[3], addr[4], addr[5]);
454 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
456 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
457 addr->addr[3], addr->addr[4], addr->addr[5]);
460 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
463 ret_if(device_class == NULL);
465 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
466 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
467 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
469 if (cod & 0x002000) {
470 device_class->service_class |=
471 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
475 void _bt_free_device_info(bt_remote_dev_info_t *dev_info)
479 ret_if(dev_info == NULL);
481 g_free(dev_info->address);
482 g_free(dev_info->name);
483 g_free(dev_info->manufacturer_data);
485 if (dev_info->uuids) {
486 for (i = 0; dev_info->uuids[i] != NULL; i++)
487 g_free(dev_info->uuids[i]);
489 g_free(dev_info->uuids);
495 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
497 ret_if(le_dev_info == NULL);
499 g_free(le_dev_info->adv_data);
503 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
510 if (dest == NULL || src == NULL)
511 return BLUETOOTH_ERROR_INVALID_PARAM;
513 BT_DBG("+src : %s", src);
514 BT_DBG("+dest : %s", dest);
517 while (*p != '\0' && i < length) {
518 next = g_utf8_next_char(p);
521 while (count > 0 && ((i + count) < length)) {
528 return BLUETOOTH_ERROR_NONE;
531 gboolean _bt_utf8_validate(char *name)
535 glong items_written = 0;
537 if (FALSE == g_utf8_validate(name, -1, NULL))
540 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
546 if (items_written != g_utf8_strlen(name, -1))
553 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
555 if (!_bt_agent_register_osp_server( type, uuid, path, fd))
556 return BLUETOOTH_ERROR_INTERNAL;
558 return BLUETOOTH_ERROR_NONE;
561 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
563 if (!_bt_agent_unregister_osp_server( type, uuid))
564 return BLUETOOTH_ERROR_INTERNAL;
566 return BLUETOOTH_ERROR_NONE;
569 int _bt_set_socket_non_blocking(int socket_fd)
571 /* Set Nonblocking */
574 arg = fcntl(socket_fd, F_GETFL);
579 if (arg & O_NONBLOCK) {
580 BT_ERR("Already Non-blocking \n");
585 if (fcntl(socket_fd, F_SETFL, arg) < 0)
588 return BLUETOOTH_ERROR_NONE;
591 int _bt_set_non_blocking_tty(int sk)
593 struct termios ti = {0,};
596 err = _bt_set_socket_non_blocking(sk);
599 BT_ERR("Error in set non blocking!\n");
603 tcflush(sk, TCIOFLUSH);
605 /* Switch tty to RAW mode */
607 tcsetattr(sk, TCSANOW, &ti);
609 return BLUETOOTH_ERROR_NONE;
612 static char *__bt_extract_device_path(DBusMessageIter *msg_iter, char *address)
614 char *object_path = NULL;
615 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
617 /* Parse the signature: oa{sa{sv}}} */
618 retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
619 DBUS_TYPE_OBJECT_PATH, NULL);
621 dbus_message_iter_get_basic(msg_iter, &object_path);
622 retv_if(object_path == NULL, NULL);
624 _bt_convert_device_path_to_address(object_path, device_address);
626 if (g_strcmp0(address, device_address) == 0) {
627 return g_strdup(object_path);
633 char *_bt_get_device_object_path(char *address)
637 DBusMessageIter reply_iter;
638 DBusMessageIter value_iter;
640 DBusConnection *conn;
641 char *object_path = NULL;
643 conn = _bt_get_system_conn();
644 retv_if(conn == NULL, NULL);
646 msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
647 BT_MANAGER_INTERFACE,
648 "GetManagedObjects");
650 retv_if(msg == NULL, NULL);
652 /* Synchronous call */
653 dbus_error_init(&err);
654 reply = dbus_connection_send_with_reply_and_block(
657 dbus_message_unref(msg);
660 BT_ERR("Can't get managed objects");
662 if (dbus_error_is_set(&err)) {
663 BT_ERR("%s", err.message);
664 dbus_error_free(&err);
669 if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
670 BT_ERR("Fail to iterate the reply");
671 dbus_message_unref(reply);
675 dbus_message_iter_recurse(&reply_iter, &value_iter);
677 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
678 while (dbus_message_iter_get_arg_type(&value_iter) ==
679 DBUS_TYPE_DICT_ENTRY) {
680 DBusMessageIter msg_iter;
682 dbus_message_iter_recurse(&value_iter, &msg_iter);
684 object_path = __bt_extract_device_path(&msg_iter, address);
685 if (object_path != NULL) {
686 BT_DBG("Found the device path");
690 dbus_message_iter_next(&value_iter);
692 dbus_message_unref(reply);
697 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
699 switch(profile_type) {
700 case BT_PROFILE_CONN_RFCOMM:
701 return strdup(RFCOMM_UUID_STR);
702 case BT_PROFILE_CONN_A2DP:
703 return strdup(A2DP_SINK_UUID);
704 case BT_PROFILE_CONN_HSP:
705 return strdup(HFP_HS_UUID);
706 case BT_PROFILE_CONN_HID:
707 return strdup(HID_UUID);
708 case BT_PROFILE_CONN_NAP:
709 return strdup(NAP_UUID);
710 case BT_PROFILE_CONN_HFG:
711 return strdup(HFP_AG_UUID);
712 case BT_PROFILE_CONN_GATT:
713 case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
719 char *_bt_convert_error_to_string(int error)
722 case BLUETOOTH_ERROR_CANCEL:
724 case BLUETOOTH_ERROR_INVALID_PARAM:
725 return "INVALID_PARAMETER";
726 case BLUETOOTH_ERROR_INVALID_DATA:
727 return "INVALID DATA";
728 case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
729 case BLUETOOTH_ERROR_OUT_OF_MEMORY:
730 return "OUT_OF_MEMORY";
731 case BLUETOOTH_ERROR_TIMEOUT:
733 case BLUETOOTH_ERROR_NO_RESOURCES:
734 return "NO_RESOURCES";
735 case BLUETOOTH_ERROR_INTERNAL:
737 case BLUETOOTH_ERROR_NOT_SUPPORT:
738 return "NOT_SUPPORT";
739 case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
740 return "NOT_ENABLED";
741 case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
742 return "ALREADY_ENABLED";
743 case BLUETOOTH_ERROR_DEVICE_BUSY:
744 return "DEVICE_BUSY";
745 case BLUETOOTH_ERROR_ACCESS_DENIED:
746 return "ACCESS_DENIED";
747 case BLUETOOTH_ERROR_MAX_CLIENT:
749 case BLUETOOTH_ERROR_NOT_FOUND:
751 case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
752 return "SERVICE_SEARCH_ERROR";
753 case BLUETOOTH_ERROR_PARING_FAILED:
754 return "PARING_FAILED";
755 case BLUETOOTH_ERROR_NOT_PAIRED:
757 case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
758 return "SERVICE_NOT_FOUND";
759 case BLUETOOTH_ERROR_NOT_CONNECTED:
760 return "NOT_CONNECTED";
761 case BLUETOOTH_ERROR_ALREADY_CONNECT:
762 return "ALREADY_CONNECT";
763 case BLUETOOTH_ERROR_CONNECTION_BUSY:
764 return "CONNECTION_BUSY";
765 case BLUETOOTH_ERROR_CONNECTION_ERROR:
766 return "CONNECTION_ERROR";
767 case BLUETOOTH_ERROR_MAX_CONNECTION:
768 return "MAX_CONNECTION";
769 case BLUETOOTH_ERROR_NOT_IN_OPERATION:
770 return "NOT_IN_OPERATION";
771 case BLUETOOTH_ERROR_CANCEL_BY_USER:
772 return "CANCEL_BY_USER";
773 case BLUETOOTH_ERROR_REGISTRATION_FAILED:
774 return "REGISTRATION_FAILED";
775 case BLUETOOTH_ERROR_IN_PROGRESS:
776 return "IN_PROGRESS";
777 case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
778 return "AUTHENTICATION_FAILED";
779 case BLUETOOTH_ERROR_HOST_DOWN:
781 case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
782 return "END_OF_DEVICE_LIST";
783 case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
784 return "AGENT_ALREADY_EXIST";
785 case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
786 return "AGENT_DOES_NOT_EXIST";
787 case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
788 return "ALREADY_INITIALIZED";
789 case BLUETOOTH_ERROR_PERMISSION_DEINED:
790 return "PERMISSION_DEINED";
791 case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
792 return "ALREADY_DEACTIVATED";
793 case BLUETOOTH_ERROR_NOT_INITIALIZED:
794 return "NOT_INITIALIZED";
800 char * _bt_convert_disc_reason_to_string(int reason)
806 return "Connection terminated by local host";
808 return "Remote user terminated connection";
815 void _bt_logging_connection(gboolean connect, int addr_type)
817 static int le_conn = 0;
818 static int le_disc = 0;
819 static int edr_conn = 0;
820 static int edr_disc = 0;
834 BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
835 le_conn, le_disc, edr_conn, edr_disc);