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.
27 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <gio/gunixfdlist.h>
35 #include "bluetooth-api.h"
36 #include "bluetooth-audio-api.h"
37 #include "bluetooth-hid-api.h"
38 #include "bluetooth-media-control.h"
39 #include "bt-internal-types.h"
41 #include "bt-common.h"
42 #include "bt-request-sender.h"
43 #include "bt-event-handler.h"
45 static bt_user_info_t user_info[BT_MAX_USER_INFO];
46 static DBusGConnection *system_conn = NULL;
47 static GDBusConnection *system_gdbus_conn = NULL;
50 static size_t cookie_size;
54 static GDBusConnection *system_gconn = NULL;
56 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
58 GDBusConnection *_bt_gdbus_init_system_gconn(void)
62 if (!g_thread_supported()) {
70 if (system_gconn != NULL)
73 system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
76 BT_ERR("Unable to connect to dbus: %s", error->message);
77 g_clear_error(&error);
83 GDBusConnection *_bt_gdbus_get_system_gconn(void)
85 GDBusConnection *local_system_gconn = NULL;
88 if (system_gconn == NULL) {
89 system_gconn = _bt_gdbus_init_system_gconn();
90 } else if (g_dbus_connection_is_closed(system_gconn)){
92 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
94 if (!local_system_gconn) {
95 BT_ERR("Unable to connect to dbus: %s", error->message);
96 g_clear_error(&error);
99 system_gconn = local_system_gconn;
105 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
107 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
108 addr->addr[3], addr->addr[4], addr->addr[5]);
111 void _bt_set_user_data(int type, void *callback, void *user_data)
113 user_info[type].cb = callback;
114 user_info[type].user_data = user_data;
117 bt_user_info_t *_bt_get_user_data(int type)
119 return &user_info[type];
122 void _bt_common_event_cb(int event, int result, void *param,
123 void *callback, void *user_data)
125 bluetooth_event_param_t bt_event = { 0, };
126 bt_event.event = event;
127 bt_event.result = result;
128 bt_event.param_data = param;
131 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
135 void _bt_input_event_cb(int event, int result, void *param,
136 void *callback, void *user_data)
138 hid_event_param_t bt_event = { 0, };
139 bt_event.event = event;
140 bt_event.result = result;
141 bt_event.param_data = param;
144 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
148 void _bt_headset_event_cb(int event, int result, void *param,
149 void *callback, void *user_data)
151 bt_audio_event_param_t bt_event = { 0, };
152 bt_event.event = event;
153 bt_event.result = result;
154 bt_event.param_data = param;
157 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
161 void _bt_a2dp_source_event_cb(int event, int result, void *param,
162 void *callback, void *user_data)
164 bt_audio_event_param_t bt_event = { 0, };
165 bt_event.event = event;
166 bt_event.result = result;
167 bt_event.param_data = param;
169 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
173 void _bt_hf_event_cb(int event, int result, void *param,
174 void *callback, void *user_data)
176 bt_hf_event_param_t bt_event = { 0, };
177 bt_event.event = event;
178 bt_event.result = result;
179 bt_event.param_data = param;
182 ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
187 void _bt_avrcp_event_cb(int event, int result, void *param,
188 void *callback, void *user_data)
190 media_event_param_t bt_event = { 0, };
191 bt_event.event = event;
192 bt_event.result = result;
193 bt_event.param_data = param;
196 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
200 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
203 ret_if(device_class == NULL);
205 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
206 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
207 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
209 if (cod & 0x002000) {
210 device_class->service_class |=
211 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
215 void _bt_convert_addr_string_to_type(unsigned char *addr,
221 ret_if(address == NULL);
222 ret_if(addr == NULL);
224 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
225 addr[i] = strtol(address, &ptr, 16);
226 if (ptr[0] != '\0') {
235 void _bt_convert_addr_type_to_string(char *address,
238 ret_if(address == NULL);
239 ret_if(addr == NULL);
241 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
242 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
243 addr[0], addr[1], addr[2],
244 addr[3], addr[4], addr[5]);
247 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
254 if (dest == NULL || src == NULL)
255 return BLUETOOTH_ERROR_INVALID_PARAM;
258 while (*p != '\0' && i < length) {
259 next = g_utf8_next_char(p);
262 while (count > 0 && ((i + count) < length)) {
269 return BLUETOOTH_ERROR_NONE;
272 gboolean _bt_utf8_validate(char *name)
276 glong items_written = 0;
278 if (FALSE == g_utf8_validate(name, -1, NULL))
281 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
287 if (items_written != g_utf8_strlen(name, -1))
295 static GDBusProxy *profile_gproxy;
296 static GDBusConnection *gconn;
297 static int latest_id = -1;
298 #define BT_RFCOMM_ID_MAX 245
299 static gboolean id_used[BT_RFCOMM_ID_MAX];
300 GDBusNodeInfo *new_conn_node;
302 static const gchar rfcomm_agent_xml[] =
304 " <interface name='org.bluez.Profile1'>"
305 " <method name='NewConnection'>"
306 " <arg type='o' name='object' direction='in'/>"
307 " <arg type='h' name='fd' direction='in'/>"
308 " <arg type='a{sv}' name='properties' direction='in'/>"
310 " <method name='RequestDisconnection'>"
311 " <arg type='o' name='device' direction='in'/>"
316 static void __new_connection_method(GDBusConnection *connection,
318 const gchar *object_path,
319 const gchar *interface_name,
320 const gchar *method_name,
321 GVariant *parameters,
322 GDBusMethodInvocation *invocation,
325 BT_DBG("method %s", method_name);
326 if (g_strcmp0(method_name, "NewConnection") == 0) {
329 GUnixFDList *fd_list;
330 GVariantBuilder *properties;
333 bluetooth_device_address_t remote_addr1;
334 bt_new_connection_cb cb = user_data;
337 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
340 msg = g_dbus_method_invocation_get_message(invocation);
341 fd_list = g_dbus_message_get_unix_fd_list(msg);
342 if (fd_list == NULL) {
343 GQuark quark = g_quark_from_string("rfcomm-app");
344 GError *err = g_error_new(quark, 0, "No fd in message");
345 g_dbus_method_invocation_return_gerror(invocation, err);
351 fd = g_unix_fd_list_get(fd_list, index, NULL);
353 BT_ERR("Invalid fd return");
354 GQuark quark = g_quark_from_string("rfcomm-app");
355 GError *err = g_error_new(quark, 0, "Invalid FD return");
356 g_dbus_method_invocation_return_gerror(invocation, err);
360 BT_INFO("Object Path %s", obj_path);
362 _bt_device_path_to_address(obj_path, addr);
363 _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
364 BT_INFO("fd: %d, address %s", fd, addr);
366 g_dbus_method_invocation_return_value(invocation, NULL);
369 cb(object_path, fd, &remote_addr1);
370 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
371 g_dbus_method_invocation_return_value(invocation, NULL);
376 static const GDBusInterfaceVTable method_table = {
377 __new_connection_method,
382 void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
386 for (i = 0; i < 6; i++)
390 int __rfcomm_assign_id(void)
394 BT_DBG("latest_id: %d", latest_id);
396 index = latest_id + 1;
398 if (index >= BT_RFCOMM_ID_MAX)
401 BT_DBG("index: %d", index);
403 while (id_used[index] == TRUE) {
404 if (index == latest_id) {
405 /* No available ID */
406 BT_ERR("All request ID is used");
412 if (index >= BT_RFCOMM_ID_MAX)
417 id_used[index] = TRUE;
419 BT_DBG("Assigned Id: %d", latest_id);
424 void __rfcomm_delete_id(int id)
426 ret_if(id >= BT_RFCOMM_ID_MAX);
431 /* Next server will use this ID */
435 static GDBusConnection *__get_gdbus_connection()
440 gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
444 BT_ERR("Unable to connect to dbus: %s", err->message);
453 static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
455 GDBusConnection *gconn;
459 return profile_gproxy;
461 gconn = __get_gdbus_connection();
465 profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
468 "org.bluez.ProfileManager1",
471 BT_ERR("Unable to create proxy: %s", err->message);
476 return profile_gproxy;
479 static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
481 GDBusConnection *gconn;
483 GDBusProxy *device_gproxy;
485 gconn = __get_gdbus_connection();
489 device_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
496 BT_ERR("Unable to create proxy: %s", err->message);
501 return device_gproxy;
504 void _bt_unregister_gdbus(int object_id)
506 GDBusConnection *gconn;
508 gconn = __get_gdbus_connection();
512 g_dbus_connection_unregister_object(gconn, object_id);
515 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
517 GDBusConnection *gconn;
519 GError *error = NULL;
521 gconn = __get_gdbus_connection();
525 if (new_conn_node == NULL)
526 new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
528 if (new_conn_node == NULL)
531 id = g_dbus_connection_register_object(gconn, path,
532 new_conn_node->interfaces[0],
536 BT_ERR("Failed to register: %s", error->message);
541 BT_DBG("NEW CONNECTION ID %d", id);
546 static GDBusProxy * __bt_gdbus_get_adapter_proxy()
549 GDBusProxy *manager_proxy = NULL;
550 GDBusProxy *adapter_proxy = NULL;
551 GDBusConnection *conn;
552 GVariant *result = NULL;
553 char *adapter_path = NULL;
555 conn = __get_gdbus_connection();
556 retv_if(conn == NULL, NULL);
558 manager_proxy = g_dbus_proxy_new_sync(conn,
559 G_DBUS_PROXY_FLAGS_NONE, NULL,
562 BT_MANAGER_INTERFACE,
565 if (!manager_proxy) {
566 BT_ERR("Unable to create proxy: %s", err->message);
570 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
571 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
574 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
576 BT_ERR("Fail to get DefaultAdapter");
581 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
582 BT_ERR("Incorrect result\n");
586 g_variant_get(result, "(&o)", &adapter_path);
588 if (adapter_path == NULL ||
589 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
590 BT_ERR("Adapter path is inproper\n");
594 BT_INFO("Adapter Path %s", adapter_path);
596 adapter_proxy = g_dbus_proxy_new_sync(conn,
597 G_DBUS_PROXY_FLAGS_NONE, NULL,
600 BT_ADAPTER_INTERFACE,
603 BT_ERR("DBus Error message: [%s]", err->message);
609 g_object_unref(manager_proxy);
611 g_variant_unref(result);
612 return adapter_proxy;
615 int _bt_register_new_conn_ex(const char *path, const char *bus_name,bt_new_connection_cb cb)
617 GDBusConnection *gconn;
619 GError *error = NULL;
621 gconn = __get_gdbus_connection();
625 if (new_conn_node == NULL)
626 new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
628 if (new_conn_node == NULL)
631 id = g_dbus_connection_register_object(gconn, path,
632 new_conn_node->interfaces[0],
636 BT_ERR("Failed to register: %s", error->message);
641 BT_DBG("NEW CONNECTION ID %d", id);
646 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
648 GVariantBuilder *option_builder;
652 int result = BLUETOOTH_ERROR_NONE;
654 proxy = __bt_gdbus_get_profile_proxy();
656 BT_ERR("Getting profile proxy failed");
657 return BLUETOOTH_ERROR_INTERNAL;
660 option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
661 if (info->authentication)
662 g_variant_builder_add(option_builder, "{sv}",
663 "RequireAuthentication",
664 g_variant_new_boolean(TRUE));
665 if (info->authorization)
666 g_variant_builder_add(option_builder, "{sv}",
667 "RequireAuthorization",
668 g_variant_new_boolean(TRUE));
670 g_variant_builder_add(option_builder, "{sv}",
672 g_variant_new_string(info->role));
674 /* Setting RFCOMM channel to default value 0; would allow bluez to assign
675 * RFCOMM channels based on the availability when two services want
676 * to use the RFCOMM along with SPP. Hence bluez makes sure that no
677 * two services use the same SPP RFCOMM channel. */
678 if (use_default_rfcomm)
679 g_variant_builder_add(option_builder, "{sv}",
681 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
683 g_variant_builder_add(option_builder, "{sv}",
685 g_variant_new_string(info->service));
687 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
688 g_variant_new("(osa{sv})", info->obj_path,
691 G_DBUS_CALL_FLAGS_NONE, -1,
694 BT_ERR("RegisterProfile failed: %s", err->message);
696 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
697 result = BLUETOOTH_ERROR_ACCESS_DENIED;
699 result = BLUETOOTH_ERROR_INTERNAL;
704 g_variant_builder_unref(option_builder);
707 g_variant_unref(ret);
712 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path)
714 GVariantBuilder *option_builder;
718 int result = BLUETOOTH_ERROR_NONE;
720 proxy = __bt_gdbus_get_profile_proxy();
722 BT_ERR("Getting profile proxy failed");
723 return BLUETOOTH_ERROR_INTERNAL;
726 option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
727 if (info->authentication)
728 g_variant_builder_add(option_builder, "{sv}",
729 "RequireAuthentication",
730 g_variant_new_boolean(TRUE));
731 if (info->authorization)
732 g_variant_builder_add(option_builder, "{sv}",
733 "RequireAuthorization",
734 g_variant_new_boolean(TRUE));
736 g_variant_builder_add(option_builder, "{sv}",
738 g_variant_new_string(info->role));
740 /* Setting RFCOMM channel to default value 0; would allow bluez to assign
741 * RFCOMM channels based on the availability when two services want
742 * to use the RFCOMM along with SPP. Hence bluez makes sure that no
743 * two services use the same SPP RFCOMM channel. */
744 if (use_default_rfcomm)
745 g_variant_builder_add(option_builder, "{sv}",
747 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
749 g_variant_builder_add(option_builder, "{sv}",
751 g_variant_new_string(info->service));
753 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile2",
754 g_variant_new("(osssa{sv})", info->obj_path,
759 G_DBUS_CALL_FLAGS_NONE, -1,
762 BT_ERR("RegisterProfile failed: %s", err->message);
764 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
765 result = BLUETOOTH_ERROR_ACCESS_DENIED;
767 result = BLUETOOTH_ERROR_INTERNAL;
772 g_variant_builder_unref(option_builder);
775 g_variant_unref(ret);
780 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
782 GVariantBuilder *option_builder;
786 int result = BLUETOOTH_ERROR_NONE;
788 proxy = __bt_gdbus_get_profile_proxy();
790 BT_ERR("Getting profile proxy failed");
791 return BLUETOOTH_ERROR_INTERNAL;
794 option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
795 if (info->authentication)
796 g_variant_builder_add(option_builder, "{sv}",
797 "RequireAuthentication",
798 g_variant_new_boolean(TRUE));
799 if (info->authorization)
800 g_variant_builder_add(option_builder, "{sv}",
801 "RequireAuthorization",
802 g_variant_new_boolean(TRUE));
804 g_variant_builder_add(option_builder, "{sv}",
806 g_variant_new_string(info->role));
808 /* Setting RFCOMM channel to default value 0; would allow bluez to assign
809 * RFCOMM channels based on the availability when two services want
810 * to use the RFCOMM along with SPP. Hence bluez makes sure that no
811 * two services use the same SPP RFCOMM channel. */
812 if (use_default_rfcomm)
813 g_variant_builder_add(option_builder, "{sv}",
815 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
817 g_variant_builder_add(option_builder, "{sv}",
819 g_variant_new_string(info->service));
821 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
822 g_variant_new("(osa{sv})", info->obj_path,
825 G_DBUS_CALL_FLAGS_NONE, -1,
829 BT_ERR("RegisterProfile failed: %s", err->message);
831 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
832 result = BLUETOOTH_ERROR_ACCESS_DENIED;
834 result = BLUETOOTH_ERROR_INTERNAL;
839 g_variant_builder_unref(option_builder);
842 g_variant_unref(ret);
848 void _bt_unregister_profile(char *path)
854 proxy = __bt_gdbus_get_profile_proxy();
856 BT_ERR("Getting profile proxy failed");
860 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
861 g_variant_new("(o)", path),
862 G_DBUS_CALL_FLAGS_NONE, -1,
865 BT_ERR("UnregisterProfile failed : %s", err->message);
870 g_variant_unref(ret);
875 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
878 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
880 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
882 G_BUS_NAME_OWNER_FLAGS_NONE,
888 BT_DBG("Got bus id %d", bus_id);
892 return g_dbus_node_info_new_for_xml(xml_data, NULL);
895 GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name)
898 char *name = g_strdup(bus_name);
899 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
901 G_BUS_NAME_OWNER_FLAGS_NONE,
907 BT_DBG("Got bus id %d", bus_id);
911 return g_dbus_node_info_new_for_xml(xml_data, NULL);
914 int _bt_connect_profile(char *address, char *uuid, void *cb,
918 GDBusProxy *adapter_proxy;
922 object_path = _bt_get_device_object_path(address);
924 if (object_path == NULL) {
925 GVariant *ret = NULL;
926 BT_ERR("No searched device");
927 adapter_proxy = __bt_gdbus_get_adapter_proxy();
929 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
930 g_variant_new("(s)", address),
931 G_DBUS_CALL_FLAGS_NONE,
936 BT_ERR("CreateDevice Failed: %s", err->message);
940 g_variant_unref(ret);
941 g_object_unref(adapter_proxy);
942 object_path = _bt_get_device_object_path(address);
944 if (object_path == NULL)
945 return BLUETOOTH_ERROR_INTERNAL;
948 proxy = __bt_gdbus_get_device_proxy(object_path);
952 BT_ERR("Error while getting proxy");
953 return BLUETOOTH_ERROR_INTERNAL;
956 g_dbus_proxy_call(proxy, "ConnectProfile",
957 g_variant_new("(s)", uuid),
958 G_DBUS_CALL_FLAGS_NONE,
960 (GAsyncReadyCallback)cb,
963 return BLUETOOTH_ERROR_NONE;
966 int _bt_discover_services(char *address, char *uuid, void *cb,
971 GDBusProxy *adapter_proxy;
973 object_path = _bt_get_device_object_path(address);
974 if (object_path == NULL) {
975 GVariant *ret = NULL;
976 BT_ERR("No searched device");
977 adapter_proxy = __bt_gdbus_get_adapter_proxy();
978 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
979 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
980 g_variant_new("(s)", address),
981 G_DBUS_CALL_FLAGS_NONE,
985 BT_ERR("CreateDevice Failed: %s", err->message);
989 g_variant_unref(ret);
990 g_object_unref(adapter_proxy);
991 object_path = _bt_get_device_object_path(address);
992 if (object_path == NULL)
993 return BLUETOOTH_ERROR_INTERNAL;
995 proxy = __bt_gdbus_get_device_proxy(object_path);
998 BT_ERR("Error while getting proxy");
999 return BLUETOOTH_ERROR_INTERNAL;
1001 g_dbus_proxy_call(proxy, "DiscoverServices",
1002 g_variant_new("(s)", uuid),
1003 G_DBUS_CALL_FLAGS_NONE,
1005 (GAsyncReadyCallback)cb,
1008 return BLUETOOTH_ERROR_NONE;
1011 int _bt_cancel_discovers(char *address)
1015 GDBusProxy *adapter_proxy;
1017 object_path = _bt_get_device_object_path(address);
1018 if (object_path == NULL) {
1019 GVariant *ret = NULL;
1020 BT_ERR("No searched device");
1021 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1022 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1023 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1024 g_variant_new("(s)", address),
1025 G_DBUS_CALL_FLAGS_NONE,
1029 BT_ERR("CreateDevice Failed: %s", err->message);
1030 g_clear_error(&err);
1033 g_variant_unref(ret);
1034 g_object_unref(adapter_proxy);
1035 object_path = _bt_get_device_object_path(address);
1036 if (object_path == NULL)
1037 return BLUETOOTH_ERROR_INTERNAL;
1039 proxy = __bt_gdbus_get_device_proxy(object_path);
1040 g_free(object_path);
1041 g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1043 G_DBUS_CALL_FLAGS_NONE,
1047 BT_ERR("DBus Error message: [%s]", err->message);
1048 g_clear_error(&err);
1049 return BLUETOOTH_ERROR_INTERNAL;
1052 g_object_unref(proxy);
1053 return BLUETOOTH_ERROR_NONE;
1056 int _bt_discover_service_uuids(char *address, char *remote_uuid)
1060 GDBusConnection *gconn;
1062 char **uuid_value = NULL;
1065 GVariant *value = NULL;
1066 GVariant *ret = NULL;
1067 int result = BLUETOOTH_ERROR_INTERNAL;
1069 retv_if(remote_uuid == NULL, BLUETOOTH_ERROR_INTERNAL);
1070 gconn = __get_gdbus_connection();
1071 retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1072 object_path = _bt_get_device_object_path(address);
1073 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1075 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1076 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1078 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1080 BT_ERR("DBus Error: [%s]", err->message);
1081 g_clear_error(&err);
1083 ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1084 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1085 G_DBUS_CALL_FLAGS_NONE,
1089 result = BLUETOOTH_ERROR_INTERNAL;
1090 BT_ERR("DBus Error : %s", err->message);
1091 g_clear_error(&err);
1095 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1096 result = BLUETOOTH_ERROR_INTERNAL;
1100 g_variant_get(ret, "(@a{sv})", &value);
1101 g_variant_unref(ret);
1103 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1104 G_VARIANT_TYPE_STRING_ARRAY);
1106 size = g_variant_get_size(temp_value);
1108 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1109 BT_DBG("Size items %d", size);
1112 g_variant_unref(temp_value);
1113 for (i = 0; uuid_value[i] != NULL; i++) {
1114 BT_DBG("Remote uuids %s", uuid_value[i]);
1115 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
1116 result = BLUETOOTH_ERROR_NONE;
1124 g_object_unref(proxy);
1126 g_variant_unref(value);
1134 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
1138 GDBusConnection *gconn;
1140 GVariant *value = NULL;
1141 GVariant *result = NULL;
1142 unsigned int class = 0x00;
1143 int ret = BLUETOOTH_ERROR_INTERNAL;
1145 gconn = __get_gdbus_connection();
1146 retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1147 object_path = _bt_get_device_object_path(address);
1149 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1151 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1152 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1154 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1156 BT_ERR("DBus Error: [%s]", err->message);
1157 g_clear_error(&err);
1160 result = g_dbus_proxy_call_sync(proxy, "GetAll",
1161 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1162 G_DBUS_CALL_FLAGS_NONE,
1166 ret = BLUETOOTH_ERROR_INTERNAL;
1167 BT_ERR("DBus Error : %s", err->message);
1168 g_clear_error(&err);
1171 if (result == NULL) {
1172 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1173 ret = BLUETOOTH_ERROR_INTERNAL;
1176 g_variant_get(result, "(@a{sv})", &value);
1177 g_variant_unref(result);
1179 GVariant *temp_value = g_variant_lookup_value(value, "Class",
1180 G_VARIANT_TYPE_UINT32);
1181 class = g_variant_get_uint32(temp_value);
1182 _bt_divide_device_class(dev_class, class);
1184 g_variant_unref(temp_value);
1189 g_object_unref(proxy);
1191 g_variant_unref(value);
1197 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
1203 GDBusProxy *adapter_proxy;
1204 object_path = _bt_get_device_object_path(address);
1205 if (object_path == NULL) {
1206 GVariant *ret = NULL;
1207 BT_ERR("No searched device");
1208 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1209 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1210 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1211 g_variant_new("(s)", address),
1212 G_DBUS_CALL_FLAGS_NONE,
1216 BT_ERR("CreateDevice Failed: %s", err->message);
1220 g_variant_unref(ret);
1221 g_object_unref(adapter_proxy);
1222 object_path = _bt_get_device_object_path(address);
1223 if (object_path == NULL)
1224 return BLUETOOTH_ERROR_INTERNAL;
1226 proxy = __bt_gdbus_get_device_proxy(object_path);
1227 g_free(object_path);
1228 if (proxy == NULL) {
1229 BT_ERR("Error while getting proxy");
1230 return BLUETOOTH_ERROR_INTERNAL;
1232 g_dbus_proxy_call(proxy, "DisconnectProfile",
1233 g_variant_new("(s)", uuid),
1234 G_DBUS_CALL_FLAGS_NONE,
1236 (GAsyncReadyCallback)cb,
1239 return BLUETOOTH_ERROR_NONE;
1242 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
1245 GDBusProxy *manager_proxy = NULL;
1246 GVariant *result = NULL;
1247 char *adapter_path = NULL;
1249 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1251 manager_proxy = g_dbus_proxy_new_sync(conn,
1252 G_DBUS_PROXY_FLAGS_NONE, NULL,
1255 BT_MANAGER_INTERFACE,
1258 if (!manager_proxy) {
1259 BT_ERR("Unable to create proxy: %s", err->message);
1263 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
1264 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1267 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
1269 BT_ERR("Fail to get DefaultAdapter");
1274 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
1275 BT_ERR("Incorrect result\n");
1279 g_variant_get(result, "(&o)", &adapter_path);
1281 if (adapter_path == NULL ||
1282 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
1283 BT_ERR("Adapter path is inproper\n");
1288 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
1290 g_variant_unref(result);
1291 g_object_unref(manager_proxy);
1293 return BLUETOOTH_ERROR_NONE;
1296 g_clear_error(&err);
1299 g_variant_unref(result);
1302 g_object_unref(manager_proxy);
1304 return BLUETOOTH_ERROR_INTERNAL;
1308 void _bt_convert_device_path_to_address(const char *device_path,
1309 char *device_address)
1311 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1314 ret_if(device_path == NULL);
1315 ret_if(device_address == NULL);
1317 dev_addr = strstr(device_path, "dev_");
1318 if (dev_addr != NULL) {
1321 g_strlcpy(address, dev_addr, sizeof(address));
1323 while ((pos = strchr(address, '_')) != NULL) {
1327 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1331 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
1333 char *object_path = NULL;
1334 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1335 /* Parse the signature: oa{sa{sv}}} */
1336 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
1338 retv_if(object_path == NULL, NULL);
1339 _bt_convert_device_path_to_address(object_path, device_address);
1341 if (g_strcmp0(address, device_address) == 0) {
1342 return g_strdup(object_path);
1348 char *_bt_get_device_object_path(char *address)
1351 GDBusProxy *proxy = NULL;
1352 GVariant *result = NULL;
1353 GVariantIter *iter = NULL;
1354 GDBusConnection *conn = NULL;
1355 char *object_path = NULL;
1357 conn = _bt_gdbus_get_system_gconn();
1358 retv_if(conn == NULL, NULL);
1360 proxy = g_dbus_proxy_new_sync(conn,
1361 G_DBUS_PROXY_FLAGS_NONE, NULL,
1364 BT_MANAGER_INTERFACE,
1368 BT_ERR("Unable to create proxy: %s", err->message);
1372 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
1373 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1376 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
1378 BT_ERR("Fail to get GetManagedObjects");
1383 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1384 object_path = __bt_extract_device_path(iter, address);
1386 g_variant_unref(result);
1387 g_object_unref(proxy);
1388 g_variant_iter_free(iter);
1392 g_clear_error(&err);
1395 g_variant_unref(result);
1398 g_object_unref(proxy);
1404 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
1407 DBusGProxy *manager_proxy = NULL;
1408 DBusGProxy *adapter_proxy = NULL;
1409 char *adapter_path = NULL;
1411 retv_if(conn == NULL, NULL);
1413 manager_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1414 BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
1416 retv_if(manager_proxy == NULL, NULL);
1418 if (!dbus_g_proxy_call(manager_proxy, "DefaultAdapter", &err,
1419 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
1423 BT_ERR("Getting DefaultAdapter failed: [%s]\n", err->message);
1426 g_object_unref(manager_proxy);
1430 if (adapter_path == NULL || strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
1431 BT_ERR("Adapter path is inproper\n");
1432 g_free(adapter_path);
1433 g_object_unref(manager_proxy);
1437 adapter_proxy = dbus_g_proxy_new_for_name(conn,
1440 BT_ADAPTER_INTERFACE);
1441 g_free(adapter_path);
1442 g_object_unref(manager_proxy);
1444 return adapter_proxy;
1447 void _bt_device_path_to_address(const char *device_path, char *device_address)
1449 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1450 char *dev_addr = NULL;
1452 if (!device_path || !device_address)
1455 dev_addr = strstr(device_path, "dev_");
1456 if (dev_addr != NULL) {
1459 g_strlcpy(address, dev_addr, sizeof(address));
1461 while ((pos = strchr(address, '_')) != NULL) {
1465 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1469 DBusGConnection *__bt_init_system_gconn(void)
1473 if (system_conn == NULL)
1474 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
1479 DBusGConnection *_bt_get_system_gconn(void)
1481 return (system_conn) ? system_conn : __bt_init_system_gconn();
1484 GDBusConnection *_bt_init_system_gdbus_conn(void)
1487 GError *error = NULL;
1488 if (system_gdbus_conn == NULL) {
1490 g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1492 BT_ERR("GDBus connection Error : %s \n",
1494 g_clear_error(&error);
1498 return system_gdbus_conn;
1501 DBusConnection *_bt_get_system_conn(void)
1503 DBusGConnection *g_conn;
1505 if (system_conn == NULL) {
1506 g_conn = __bt_init_system_gconn();
1508 g_conn = system_conn;
1511 retv_if(g_conn == NULL, NULL);
1513 return dbus_g_connection_get_connection(g_conn);
1516 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
1519 char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1520 char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1524 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1526 g_array_append_vals(in_param1, &type, sizeof(int));
1527 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1528 g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1529 g_strlcpy(path_str, path, sizeof(path_str));
1530 g_array_append_vals(in_param3, &path_str, BLUETOOTH_PATH_STRING);
1531 g_array_append_vals(in_param4, &fd, sizeof(int));
1533 ret = _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
1534 in_param1, in_param2, in_param3, in_param4, &out_param);
1536 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1541 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
1544 char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1548 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1550 g_array_append_vals(in_param1, &type, sizeof(int));
1551 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1552 g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1554 ret = _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
1555 in_param1, in_param2, in_param3, in_param4, &out_param);
1557 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1562 int _bt_check_privilege(int service_type, int service_function)
1567 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1569 result = _bt_send_request(service_type, service_function,
1570 in_param1, in_param2, in_param3, in_param4, &out_param);
1572 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1577 GVariant *_bt_get_managed_objects(void)
1579 GDBusConnection *g_conn;
1580 GDBusProxy *manager_proxy = NULL;
1581 GVariant *result = NULL;
1582 GError *error = NULL;
1586 g_conn = _bt_gdbus_get_system_gconn();
1587 retv_if(g_conn == NULL, NULL);
1589 manager_proxy = g_dbus_proxy_new_sync(g_conn,
1590 G_DBUS_PROXY_FLAGS_NONE, NULL,
1593 BT_MANAGER_INTERFACE,
1597 BT_ERR("Unable to create proxy: %s", error->message);
1598 g_clear_error(&error);
1602 result = g_dbus_proxy_call_sync (manager_proxy,
1603 "GetManagedObjects", NULL,
1604 G_DBUS_CALL_FLAGS_NONE, -1,
1608 BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1609 g_clear_error(&error);
1612 g_object_unref(manager_proxy);
1618 BT_EXPORT_API int bluetooth_is_supported(void)
1620 int is_supported = 0;
1625 fd = open(RFKILL_NODE, O_RDONLY);
1627 BT_ERR("Fail to open RFKILL node");
1628 return BLUETOOTH_ERROR_INTERNAL;
1631 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
1632 BT_ERR("Fail to set RFKILL node to non-blocking");
1634 return BLUETOOTH_ERROR_INTERNAL;
1638 len = read(fd, &event, sizeof(event));
1640 BT_ERR("Fail to read events");
1644 if (len != RFKILL_EVENT_SIZE) {
1645 BT_ERR("The size is wrong\n");
1649 if (event.type == RFKILL_TYPE_BLUETOOTH) {
1657 BT_DBG("supported: %d", is_supported);
1659 return is_supported;
1662 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
1666 _bt_gdbus_init_system_gconn();
1667 __bt_init_system_gconn();
1669 ret = _bt_init_event_handler();
1670 if (ret != BLUETOOTH_ERROR_NONE &&
1671 ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
1672 BT_ERR("Fail to init the event handler");
1676 _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
1678 /* Register All events */
1679 ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
1680 if (ret != BLUETOOTH_ERROR_NONE)
1682 ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
1683 if (ret != BLUETOOTH_ERROR_NONE)
1685 ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
1686 if (ret != BLUETOOTH_ERROR_NONE)
1688 ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
1689 if (ret != BLUETOOTH_ERROR_NONE)
1691 ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
1692 if (ret != BLUETOOTH_ERROR_NONE)
1695 _bt_register_name_owner_changed();
1697 return BLUETOOTH_ERROR_NONE;
1699 BT_ERR("Fail to do _bt_register_event()");
1700 bluetooth_unregister_callback();
1704 BT_EXPORT_API int bluetooth_unregister_callback(void)
1708 _bt_destroy_cookie();
1710 ret = _bt_deinit_event_handler();
1711 if (ret != BLUETOOTH_ERROR_NONE) {
1712 BT_ERR("Fail to deinit the event handler");
1715 _bt_unregister_name_owner_changed();
1717 _bt_set_user_data(BT_COMMON, NULL, NULL);
1720 dbus_g_connection_unref(system_conn);
1724 g_object_unref(system_gconn);
1725 system_gconn = NULL;
1727 return BLUETOOTH_ERROR_NONE;