4 * Copyright (c) 2015 -2016 Samsung Electronics Co., Ltd All Rights Reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
31 #include <arpa/inet.h>
35 #include <sys/prctl.h>
37 #include <gio/gunixfdlist.h>
39 #include "bt-hal-dbus-common-utils.h"
42 #include "bt-hal-log.h"
43 #include "bt-hal-msg.h"
44 #include "bt-hal-utils.h"
45 #include "bt-hal-internal.h"
47 #define CASE_RETURN_STR(const) case const: return #const;
50 * This is RFCOMM default Channel Value
52 #define RFCOMM_DEFAULT_PROFILE_CHANNEL 0
54 static char *avrcp_control_path = NULL;
55 static char *avrcp_transport_path = NULL;
57 static GDBusConnection *system_conn;
58 static GDBusConnection *session_conn;
59 static GDBusProxy *manager_gproxy = NULL;
60 static GDBusProxy *adapter_gproxy = NULL;
61 static GDBusProxy *profile_gproxy = NULL;
63 static GDBusProxy *adapter_properties_proxy;
64 static GDBusProxy *avrcp_ctrl_proxy;
66 static GDBusConnection *system_gconn = NULL;
69 GDBusNodeInfo *new_conn_node;
70 static const gchar rfcomm_agent_xml[] =
72 " <interface name='org.bluez.Profile1'>"
73 " <method name='NewConnection'>"
74 " <arg type='o' name='object' direction='in'/>"
75 " <arg type='h' name='fd' direction='in'/>"
76 " <arg type='a{sv}' name='properties' direction='in'/>"
78 " <method name='RequestDisconnection'>"
79 " <arg type='o' name='device' direction='in'/>"
84 GDBusConnection *_bt_hal_gdbus_init_system_gconn(void)
88 if (system_gconn != NULL)
91 system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
94 ERR("Unable to connect to dbus: %s", error->message);
95 g_clear_error(&error);
101 GDBusConnection *_bt_hal_gdbus_get_system_gconn(void)
103 GDBusConnection *local_system_gconn = NULL;
104 GError *error = NULL;
106 if (system_gconn == NULL) {
107 system_gconn = _bt_hal_gdbus_init_system_gconn();
108 } else if (g_dbus_connection_is_closed(system_gconn)) {
110 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
112 if (!local_system_gconn) {
113 ERR("Unable to connect to dbus: %s", error->message);
114 g_clear_error(&error);
117 system_gconn = local_system_gconn;
123 static GDBusProxy *__bt_hal_init_manager_proxy(void)
129 if (system_conn == NULL) {
130 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
131 if (system_conn == NULL)
135 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
136 NULL, BT_HAL_BLUEZ_NAME,
137 BT_HAL_MANAGER_PATH, BT_HAL_MANAGER_INTERFACE, NULL, NULL);
142 manager_gproxy = proxy;
148 static GDBusProxy *__bt_hal_init_adapter_proxy(void)
150 GDBusProxy *manager_proxy;
152 char *adapter_path = NULL;
154 if (system_conn == NULL) {
155 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
156 if (system_conn == NULL)
160 manager_proxy = _bt_hal_get_manager_proxy();
161 if (manager_proxy == NULL)
164 adapter_path = _bt_hal_get_adapter_path();
165 if (adapter_path == NULL)
168 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
169 NULL, BT_HAL_BLUEZ_NAME,
170 adapter_path, BT_HAL_ADAPTER_INTERFACE, NULL, NULL);
172 g_free(adapter_path);
177 adapter_gproxy = proxy;
182 static GDBusProxy *__bt_hal_init_adapter_properties_proxy(void)
184 GDBusProxy *manager_proxy;
186 char *adapter_path = NULL;
188 if (system_conn == NULL) {
189 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
190 if (system_conn == NULL)
194 manager_proxy = _bt_hal_get_manager_proxy();
195 if (manager_proxy == NULL)
198 adapter_path = _bt_hal_get_adapter_path();
199 if (adapter_path == NULL)
202 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
203 NULL, BT_HAL_BLUEZ_NAME,
204 adapter_path, BT_HAL_PROPERTIES_INTERFACE, NULL, NULL);
206 g_free(adapter_path);
211 adapter_properties_proxy = proxy;
216 void _bt_hal_set_control_device_path(const char *path)
222 g_free(avrcp_control_path);
223 DBG("control_path = %s", path);
224 avrcp_control_path = g_strdup(path);
227 void _bt_hal_remove_control_device_path(const char *path)
232 if (avrcp_control_path &&
233 !g_strcmp0(avrcp_control_path, path)) {
234 DBG("control_path = %s", path);
235 g_free(avrcp_control_path);
236 avrcp_control_path = NULL;
240 static char *__bt_hal_extract_control_device_path(GVariantIter *iter, char *address)
242 char *object_path = NULL;
243 char *interface_str = NULL;
244 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
246 /* Parse the signature: oa{sa{sv}}} */
247 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_str)) {
248 if (object_path == NULL)
251 if (g_strcmp0(interface_str, BT_HAL_PLAYER_CONTROL_INTERFACE) == 0) {
252 _bt_hal_convert_device_path_to_address(object_path, device_address);
253 if (g_strcmp0(address, device_address) == 0)
254 return g_strdup(object_path);
260 static char *__bt_hal_extract_transport_device_path(GVariantIter *iter, char *address)
262 char *object_path = NULL;
263 char *interface_str = NULL;
264 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
266 /* Parse the signature: oa{sa{sv}}} */
267 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_str)) {
268 if (object_path == NULL)
271 if (g_strcmp0(interface_str, BT_HAL_MEDIATRANSPORT_INTERFACE) == 0) {
272 _bt_hal_convert_device_path_to_address(object_path, device_address);
273 if (g_strcmp0(address, device_address) == 0)
274 return g_strdup(object_path);
280 static char *__bt_hal_get_control_device_object_path(char *address)
282 char *object_path = NULL;
283 GDBusConnection *conn;
284 GDBusProxy *manager_proxy;
285 GVariant *result = NULL;
286 GVariantIter *iter = NULL;
288 conn = _bt_hal_get_system_gconn();
292 manager_proxy = _bt_hal_get_manager_proxy();
293 if (manager_proxy == NULL)
296 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
298 G_DBUS_CALL_FLAGS_NONE,
303 ERR("Can't get managed objects");
307 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
308 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
309 object_path = __bt_hal_extract_control_device_path(iter, address);
310 g_variant_iter_free(iter);
311 g_variant_unref(result);
315 static char *__bt_hal_get_transport_device_object_path(char *address)
317 char *object_path = NULL;
318 GDBusConnection *conn;
319 GDBusProxy *manager_proxy;
320 GVariant *result = NULL;
321 GVariantIter *iter = NULL;
323 conn = _bt_hal_get_system_gconn();
327 manager_proxy = _bt_hal_get_manager_proxy();
328 if (manager_proxy == NULL)
331 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
333 G_DBUS_CALL_FLAGS_NONE,
338 ERR("Can't get managed objects");
342 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
343 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
344 object_path = __bt_hal_extract_transport_device_path(iter, address);
345 g_variant_iter_free(iter);
346 g_variant_unref(result);
350 char *_bt_hal_get_control_device_path(bt_bdaddr_t *bd_addr)
353 char connected_address[BT_HAL_ADDRESS_STRING_SIZE];
357 if (avrcp_control_path != NULL)
358 return avrcp_control_path;
360 _bt_hal_convert_addr_type_to_string(connected_address, bd_addr->address);
362 DBG("device address = %s", connected_address);
364 control_path = __bt_hal_get_control_device_object_path(connected_address);
365 if (control_path == NULL)
368 avrcp_control_path = control_path;
369 DBG("control_path = %s", control_path);
373 char *_bt_hal_get_transport_device_path(bt_bdaddr_t *bd_addr)
375 char *transport_path;
376 char connected_address[BT_HAL_ADDRESS_STRING_SIZE];
380 if (avrcp_transport_path != NULL)
381 return avrcp_transport_path;
383 _bt_hal_convert_addr_type_to_string(connected_address, bd_addr->address);
385 DBG("device address = %s", connected_address);
387 transport_path = __bt_hal_get_transport_device_object_path(connected_address);
388 if (transport_path == NULL)
391 avrcp_transport_path = transport_path;
392 DBG("transport_path = %s", transport_path);
393 return transport_path;
396 static GDBusConnection *__bt_hal_init_system_gconn(void)
398 if (system_conn == NULL)
399 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
404 static GDBusProxy *__bt_hal_init_avrcp_ctrl_proxy(bt_bdaddr_t *bd_addr)
406 GDBusProxy *manager_proxy;
408 char *control_path = NULL;
410 if (system_conn == NULL) {
411 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
412 if (system_conn == NULL)
416 manager_proxy = _bt_hal_get_manager_proxy();
417 if (manager_proxy == NULL)
420 control_path = _bt_hal_get_control_device_path(bd_addr);
421 if (control_path == NULL)
424 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
425 NULL, BT_HAL_BLUEZ_NAME,
426 control_path, BT_HAL_PLAYER_CONTROL_INTERFACE, NULL, NULL);
431 avrcp_ctrl_proxy = proxy;
435 static GDBusConnection *__bt_hal_init_session_conn(void)
437 if (session_conn == NULL)
438 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
443 GDBusConnection *_bt_hal_get_session_gconn(void)
445 return (session_conn) ? session_conn : __bt_hal_init_session_conn();
448 GDBusConnection *_bt_hal_get_system_gconn(void)
450 return (system_conn) ? system_conn : __bt_hal_init_system_gconn();
453 GDBusProxy *_bt_hal_get_manager_proxy(void)
456 if (manager_gproxy) {
457 const gchar *path = g_dbus_proxy_get_object_path(manager_gproxy);
459 ERR("Already proxy released hence creating new proxy");
460 return __bt_hal_init_manager_proxy();
462 return manager_gproxy;
465 return __bt_hal_init_manager_proxy();
468 GDBusProxy *_bt_hal_get_adapter_proxy(void)
470 if (adapter_gproxy) {
471 const char *path = g_dbus_proxy_get_object_path(adapter_gproxy);
473 ERR("Already proxy released hence creating new proxy");
474 return __bt_hal_init_adapter_proxy();
477 return adapter_gproxy;
479 return __bt_hal_init_adapter_proxy();
483 GDBusProxy *_bt_hal_get_avrcp_ctrl_proxy(bt_bdaddr_t *bd_addr)
485 if (avrcp_ctrl_proxy) {
486 const char *path = g_dbus_proxy_get_object_path(avrcp_ctrl_proxy);
488 ERR("Already proxy released hence creating new proxy");
489 return __bt_hal_init_avrcp_ctrl_proxy(bd_addr);
492 return avrcp_ctrl_proxy;
494 return __bt_hal_init_avrcp_ctrl_proxy(bd_addr);
498 GDBusProxy *_bt_hal_get_avrcp_ctrl_properties_proxy(bt_bdaddr_t *bd_addr)
501 GError *error = NULL;
502 char *control_path = NULL;
503 GDBusConnection *conn = NULL;
506 control_path = _bt_hal_get_control_device_path(bd_addr);
507 if (control_path == NULL)
510 DBG("control_path = %s", control_path);
512 conn = _bt_hal_get_system_gconn();
514 ERR("FAIL to get system connection");
517 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
518 NULL, BT_HAL_BLUEZ_NAME,
519 control_path, BT_HAL_PROPERTIES_INTERFACE, NULL, &error);
522 ERR("Unable to allocate new proxy");
524 ERR("%s", error->message);
525 g_clear_error(&error);
534 GDBusProxy *_bt_hal_get_avrcp_transport_properties_proxy(bt_bdaddr_t *bd_addr)
537 GError *error = NULL;
538 char *transport_path = NULL;
539 GDBusConnection *conn = NULL;
542 transport_path = _bt_hal_get_transport_device_path(bd_addr);
543 if (transport_path == NULL)
546 DBG("transport_path = %s", transport_path);
548 conn = _bt_hal_get_system_gconn();
550 ERR("FAIL to get system connection");
553 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
554 NULL, BT_HAL_BLUEZ_NAME,
555 transport_path, BT_HAL_PROPERTIES_INTERFACE, NULL, &error);
558 ERR("Unable to allocate new proxy");
560 ERR("%s", error->message);
561 g_clear_error(&error);
570 GDBusProxy *_bt_hal_get_adapter_properties_proxy(void)
572 return (adapter_properties_proxy) ? adapter_properties_proxy :
573 __bt_hal_init_adapter_properties_proxy();
576 GDBusProxy *_bt_hal_get_profile_proxy(void)
578 GDBusConnection *gconn;
582 return profile_gproxy;
584 gconn = _bt_hal_get_system_gconn();
586 ERR("_bt_hal_get_system_gconn failed");
590 profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
591 NULL, BT_HAL_BLUEZ_NAME,
593 "org.bluez.ProfileManager1",
596 ERR("Unable to create proxy: %s", err->message);
601 return profile_gproxy;
604 static char *__bt_hal_extract_adapter_path(GVariantIter *iter)
606 char *object_path = NULL;
607 GVariantIter *interface_iter;
608 GVariantIter *svc_iter;
609 char *interface_str = NULL;
611 /* Parse the signature: oa{sa{sv}}} */
612 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
615 if (object_path == NULL)
618 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
619 &interface_str, &svc_iter)) {
620 if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
623 DBG("Object Path: %s", object_path);
624 g_free(interface_str);
625 g_variant_iter_free(svc_iter);
626 g_variant_iter_free(interface_iter);
627 return g_strdup(object_path);
633 char *_bt_hal_get_adapter_path(void)
635 GDBusConnection *conn;
636 GDBusProxy *manager_proxy;
637 GVariant *result = NULL;
638 GVariantIter *iter = NULL;
639 char *adapter_path = NULL;
642 conn = _bt_hal_get_system_gconn();
646 manager_proxy = _bt_hal_get_manager_proxy();
647 if (manager_proxy == NULL)
650 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
652 G_DBUS_CALL_FLAGS_NONE,
657 ERR("Can't get managed objects");
661 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
662 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
664 adapter_path = __bt_hal_extract_adapter_path(iter);
665 g_variant_iter_free(iter);
666 g_variant_unref(result);
671 int _bt_hal_is_adapter_powered(gboolean *powered)
674 GError *error = NULL;
679 proxy = _bt_hal_get_adapter_properties_proxy();
681 return BT_STATUS_FAIL;
683 result = g_dbus_proxy_call_sync(proxy,
685 g_variant_new("(ss)", BT_HAL_ADAPTER_INTERFACE,
687 G_DBUS_CALL_FLAGS_NONE,
693 ERR("Failed to get powered status");
695 ERR("Failed to get powered status (Error: %s)", error->message);
696 g_clear_error(&error);
698 return BT_STATUS_FAIL;
701 g_variant_get(result, "(v)", &temp);
702 *powered = g_variant_get_boolean(temp);
703 INFO("powered: %d", *powered);
705 g_variant_unref(result);
706 g_variant_unref(temp);
707 return BT_STATUS_SUCCESS;
710 void _bt_hal_deinit_bluez_proxy(void)
712 if (manager_gproxy) {
713 g_object_unref(manager_gproxy);
714 manager_gproxy = NULL;
717 if (adapter_gproxy) {
718 g_object_unref(adapter_gproxy);
719 adapter_gproxy = NULL;
721 if (adapter_properties_proxy) {
722 g_object_unref(adapter_properties_proxy);
723 adapter_properties_proxy = NULL;
727 void _bt_hal_deinit_proxys(void)
729 _bt_hal_deinit_bluez_proxy();
732 g_object_unref(system_conn);
737 g_object_unref(session_conn);
742 void _bt_hal_convert_device_path_to_address(const char *device_path,
743 char *device_address)
745 char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
748 if (device_path == NULL || device_address == NULL)
751 dev_addr = strstr(device_path, "dev_");
752 if (dev_addr != NULL) {
755 g_strlcpy(address, dev_addr, sizeof(address));
757 while ((pos = strchr(address, '_')) != NULL)
760 g_strlcpy(device_address, address, BT_HAL_ADDRESS_STRING_SIZE);
764 gboolean _bt_hal_uuid_is_standard(bt_uuid_t *p_uuid)
766 uint32_t uuid0, uuid4;
767 uint16_t uuid1, uuid2, uuid3, uuid5;
768 const char *uuid_name;
769 const char *uuid_name1;
771 memcpy(&uuid0, &(p_uuid->uu[0]), 4);
772 memcpy(&uuid1, &(p_uuid->uu[4]), 2);
773 memcpy(&uuid2, &(p_uuid->uu[6]), 2);
774 memcpy(&uuid3, &(p_uuid->uu[8]), 2);
775 memcpy(&uuid4, &(p_uuid->uu[10]), 4);
776 memcpy(&uuid5, &(p_uuid->uu[14]), 2);
778 uuid_name = _bt_hal_dump_uuid_name(ntohl(uuid0));
779 uuid_name1 = _bt_hal_dump_uuid_name((ntohl(uuid4) >> 16));
781 DBG("UUID Name [%s]", uuid_name);
782 DBG("UUID Name Shifted [%s]", uuid_name1);
784 if (!g_strcmp0(uuid_name, "--"))
791 void _bt_hal_convert_uuid_string_to_type(unsigned char *uuid,
792 const char *device_uuid)
794 uint32_t uuid0, uuid4;
795 uint16_t uuid1, uuid2, uuid3, uuid5;
797 sscanf(device_uuid, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
798 &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
800 uuid0 = htonl(uuid0);
801 uuid1 = htons(uuid1);
802 uuid2 = htons(uuid2);
803 uuid3 = htons(uuid3);
804 uuid4 = htonl(uuid4);
805 uuid5 = htons(uuid5);
807 memcpy(&(uuid[0]), &uuid0, 4);
808 memcpy(&(uuid[4]), &uuid1, 2);
809 memcpy(&(uuid[6]), &uuid2, 2);
810 memcpy(&(uuid[8]), &uuid3, 2);
811 memcpy(&(uuid[10]), &uuid4, 4);
812 memcpy(&(uuid[14]), &uuid5, 2);
815 void _bt_hal_convert_uuid_type_to_string(char *str, const unsigned char *uuid)
827 snprintf(str, BT_HAL_UUID_STRING_LEN,
828 "%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
829 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
830 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
833 void _bt_hal_convert_addr_string_to_type(unsigned char *addr,
839 if (address == NULL || addr == NULL)
842 for (i = 0; i < BT_HAL_ADDRESS_LENGTH_MAX; i++) {
843 addr[i] = strtol(address, &ptr, 16);
845 if (ptr[0] != '\0') {
854 void _bt_hal_convert_addr_type_to_string(char *address,
855 const unsigned char *addr)
857 if (address == NULL || addr == NULL)
860 snprintf(address, BT_HAL_ADDRESS_STRING_SIZE,
861 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
862 addr[0], addr[1], addr[2],
863 addr[3], addr[4], addr[5]);
866 void _bt_hal_print_device_address_t(const bt_hal_device_address_t *addr)
868 DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
869 addr->addr[3], addr->addr[4], addr->addr[5]);
872 void _bt_hal_divide_device_class(bt_hal_device_class_t *device_class,
875 if (device_class == NULL)
878 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
879 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
880 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
882 if (cod & 0x002000) {
883 device_class->service_class |=
884 BT_HAL_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
888 int _bt_hal_copy_utf8_string(char *dest, const char *src, unsigned int length)
895 if (dest == NULL || src == NULL)
896 return BT_HAL_ERROR_INVALID_PARAM;
898 DBG("+src : %s", src);
899 DBG("+dest : %s", dest);
902 while (*p != '\0' && i < length) {
903 next = g_utf8_next_char(p);
906 while (count > 0 && ((i + count) < length)) {
913 return BT_HAL_ERROR_NONE;
916 gboolean _bt_hal_utf8_validate(char *name)
920 glong items_written = 0;
922 if (FALSE == g_utf8_validate(name, -1, NULL))
925 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
931 if (items_written != g_utf8_strlen(name, -1))
938 int _bt_hal_set_socket_non_blocking(int socket_fd)
940 /* Set Nonblocking */
943 arg = fcntl(socket_fd, F_GETFL);
948 if (arg & O_NONBLOCK)
949 ERR("Already Non-blocking \n");
953 if (fcntl(socket_fd, F_SETFL, arg) < 0)
956 return BT_HAL_ERROR_NONE;
959 int _bt_hal_set_non_blocking_tty(int sk)
961 struct termios ti = {0,};
964 err = _bt_hal_set_socket_non_blocking(sk);
967 ERR("Error in set non blocking!\n");
971 tcflush(sk, TCIOFLUSH);
973 /* Switch tty to RAW mode */
975 tcsetattr(sk, TCSANOW, &ti);
977 return BT_HAL_ERROR_NONE;
980 static char *__bt_hal_extract_device_path(GVariantIter *iter, char *address)
982 char *object_path = NULL;
983 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
985 /* Parse the signature: oa{sa{sv}}} */
986 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, NULL)) {
987 if (object_path == NULL)
989 _bt_hal_convert_device_path_to_address(object_path, device_address);
990 if (g_strcmp0(address, device_address) == 0)
991 return g_strdup(object_path);
996 char *_bt_hal_get_device_object_path(char *address)
998 char *object_path = NULL;
999 GDBusConnection *conn;
1000 GDBusProxy *manager_proxy;
1001 GVariant *result = NULL;
1002 GVariantIter *iter = NULL;
1004 conn = _bt_hal_get_system_gconn();
1008 manager_proxy = _bt_hal_get_manager_proxy();
1009 if (manager_proxy == NULL)
1012 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
1014 G_DBUS_CALL_FLAGS_NONE,
1019 ERR("Can't get managed objects");
1023 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
1024 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1025 object_path = __bt_hal_extract_device_path(iter, address);
1026 g_variant_iter_free(iter);
1027 g_variant_unref(result);
1031 char *_bt_hal_convert_error_to_string(int error)
1034 case BT_HAL_ERROR_CANCEL:
1036 case BT_HAL_ERROR_INVALID_PARAM:
1037 return "INVALID_PARAMETER";
1038 case BT_HAL_ERROR_INVALID_DATA:
1039 return "INVALID DATA";
1040 case BT_HAL_ERROR_MEMORY_ALLOCATION:
1041 case BT_HAL_ERROR_OUT_OF_MEMORY:
1042 return "OUT_OF_MEMORY";
1043 case BT_HAL_ERROR_TIMEOUT:
1045 case BT_HAL_ERROR_NO_RESOURCES:
1046 return "NO_RESOURCES";
1047 case BT_HAL_ERROR_INTERNAL:
1049 case BT_HAL_ERROR_NOT_SUPPORT:
1050 return "NOT_SUPPORT";
1051 case BT_HAL_ERROR_DEVICE_NOT_ENABLED:
1052 return "NOT_ENABLED";
1053 case BT_HAL_ERROR_DEVICE_ALREADY_ENABLED:
1054 return "ALREADY_ENABLED";
1055 case BT_HAL_ERROR_DEVICE_BUSY:
1056 return "DEVICE_BUSY";
1057 case BT_HAL_ERROR_ACCESS_DENIED:
1058 return "ACCESS_DENIED";
1059 case BT_HAL_ERROR_MAX_CLIENT:
1060 return "MAX_CLIENT";
1061 case BT_HAL_ERROR_NOT_FOUND:
1063 case BT_HAL_ERROR_SERVICE_SEARCH_ERROR:
1064 return "SERVICE_SEARCH_ERROR";
1065 case BT_HAL_ERROR_PARING_FAILED:
1066 return "PARING_FAILED";
1067 case BT_HAL_ERROR_NOT_PAIRED:
1068 return "NOT_PAIRED";
1069 case BT_HAL_ERROR_SERVICE_NOT_FOUND:
1070 return "SERVICE_NOT_FOUND";
1071 case BT_HAL_ERROR_NOT_CONNECTED:
1072 return "NOT_CONNECTED";
1073 case BT_HAL_ERROR_ALREADY_CONNECT:
1074 return "ALREADY_CONNECT";
1075 case BT_HAL_ERROR_CONNECTION_BUSY:
1076 return "CONNECTION_BUSY";
1077 case BT_HAL_ERROR_CONNECTION_ERROR:
1078 return "CONNECTION_ERROR";
1079 case BT_HAL_ERROR_MAX_CONNECTION:
1080 return "MAX_CONNECTION";
1081 case BT_HAL_ERROR_NOT_IN_OPERATION:
1082 return "NOT_IN_OPERATION";
1083 case BT_HAL_ERROR_CANCEL_BY_USER:
1084 return "CANCEL_BY_USER";
1085 case BT_HAL_ERROR_REGISTRATION_FAILED:
1086 return "REGISTRATION_FAILED";
1087 case BT_HAL_ERROR_IN_PROGRESS:
1088 return "IN_PROGRESS";
1089 case BT_HAL_ERROR_AUTHENTICATION_FAILED:
1090 return "AUTHENTICATION_FAILED";
1091 case BT_HAL_ERROR_HOST_DOWN:
1093 case BT_HAL_ERROR_END_OF_DEVICE_LIST:
1094 return "END_OF_DEVICE_LIST";
1095 case BT_HAL_ERROR_AGENT_ALREADY_EXIST:
1096 return "AGENT_ALREADY_EXIST";
1097 case BT_HAL_ERROR_AGENT_DOES_NOT_EXIST:
1098 return "AGENT_DOES_NOT_EXIST";
1099 case BT_HAL_ERROR_ALREADY_INITIALIZED:
1100 return "ALREADY_INITIALIZED";
1101 case BT_HAL_ERROR_PERMISSION_DEINED:
1102 return "PERMISSION_DEINED";
1103 case BT_HAL_ERROR_ALREADY_DEACTIVATED:
1104 return "ALREADY_DEACTIVATED";
1105 case BT_HAL_ERROR_NOT_INITIALIZED:
1106 return "NOT_INITIALIZED";
1112 char * _bt_hal_convert_disc_reason_to_string(int reason)
1118 return "Connection terminated by local host";
1120 return "Remote user terminated connection";
1127 void _bt_hal_logging_connection(gboolean connect, int addr_type)
1129 static int le_conn = 0;
1130 static int le_disc = 0;
1131 static int edr_conn = 0;
1132 static int edr_disc = 0;
1146 INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
1147 le_conn, le_disc, edr_conn, edr_disc);
1150 void _bt_hal_swap_byte_ordering(char *data, int data_len)
1157 /* Swap to opposite endian */
1158 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
1165 int _bt_hal_byte_arr_cmp(const char *data1, const char *data2, int data_len)
1169 if (data1 == NULL || data2 == NULL)
1171 for (i = 0; i < data_len; i++) {
1172 if (data1[i] != data2[i])
1173 return data1[i] - data2[i];
1177 int _bt_hal_byte_arr_cmp_with_mask(const char *data1, const char *data2,
1178 const char *mask, int data_len)
1183 if (data1 == NULL || data2 == NULL || mask == NULL)
1186 for (i = 0; i < data_len; i++) {
1187 a = data1[i] & mask[i];
1188 b = data2[i] & mask[i];
1190 return (int)(a - b);
1195 int _bt_hal_connect_profile(char *address, char *uuid,
1196 void *cb, gpointer func_data)
1200 GDBusConnection *conn;
1201 GDBusProxy *adapter_proxy;
1202 GError *error = NULL;
1204 conn = _bt_hal_get_system_gconn();
1206 return BT_HAL_ERROR_INTERNAL;
1208 object_path = _bt_hal_get_device_object_path(address);
1209 if (object_path == NULL) {
1210 ERR("No searched device");
1212 adapter_proxy = _bt_hal_get_adapter_proxy();
1213 if (adapter_proxy == NULL)
1214 return BT_HAL_ERROR_INTERNAL;
1216 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1217 g_variant_new("(s)", address),
1218 G_DBUS_CALL_FLAGS_NONE,
1223 if (error != NULL) {
1224 ERR("CreateDevice Fail: %s", error->message);
1225 g_error_free(error);
1228 object_path = _bt_hal_get_device_object_path(address);
1230 if (object_path == NULL)
1231 return BT_HAL_ERROR_INTERNAL;
1233 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1234 NULL, BT_HAL_BLUEZ_NAME,
1235 object_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1236 g_free(object_path);
1238 return BT_HAL_ERROR_INTERNAL;
1241 g_dbus_proxy_call(proxy, "ConnectProfile",
1242 g_variant_new("(s)", uuid),
1243 G_DBUS_CALL_FLAGS_NONE,
1244 BT_HAL_MAX_DBUS_TIMEOUT,
1246 (GAsyncReadyCallback)cb,
1249 return BT_HAL_ERROR_NONE;
1252 int _bt_hal_disconnect_profile(char *address, char *uuid,
1253 void *cb, gpointer func_data)
1257 GDBusConnection *conn;
1259 conn = _bt_hal_get_system_gconn();
1261 return BT_HAL_ERROR_INTERNAL;
1263 object_path = _bt_hal_get_device_object_path(address);
1264 if (object_path == NULL)
1265 return BT_HAL_ERROR_INTERNAL;
1267 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1268 NULL, BT_HAL_BLUEZ_NAME,
1269 object_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1270 g_free(object_path);
1272 return BT_HAL_ERROR_INTERNAL;
1274 g_dbus_proxy_call(proxy, "DisconnectProfile",
1275 g_variant_new("(s)", uuid),
1276 G_DBUS_CALL_FLAGS_NONE,
1277 BT_HAL_MAX_DBUS_TIMEOUT,
1279 (GAsyncReadyCallback)cb,
1282 return BT_HAL_ERROR_NONE;
1285 int _bt_hal_register_profile(bt_hal_register_profile_info_t *info, gboolean use_default_rfcomm)
1287 GVariantBuilder *option_builder;
1291 int result = BT_STATUS_SUCCESS;
1293 proxy = _bt_hal_get_profile_proxy();
1294 if (proxy == NULL) {
1295 ERR("Getting profile proxy failed");
1296 return BT_STATUS_FAIL;
1299 option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1300 if (info->authentication)
1301 g_variant_builder_add(option_builder, "{sv}",
1302 "RequireAuthentication",
1303 g_variant_new_boolean(TRUE));
1304 if (info->authorization)
1305 g_variant_builder_add(option_builder, "{sv}",
1306 "RequireAuthorization",
1307 g_variant_new_boolean(TRUE));
1309 g_variant_builder_add(option_builder, "{sv}",
1311 g_variant_new_string(info->role));
1314 * Setting RFCOMM channel to default value 0; would allow bluez to assign
1315 * RFCOMM channels based on the availability when two services want to use
1316 * the RFCOMM along with SPP. Hence bluez makes sure that no two services
1317 * use the same SPP RFCOMM channel.
1319 if (use_default_rfcomm)
1320 g_variant_builder_add(option_builder, "{sv}",
1322 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1324 g_variant_builder_add(option_builder, "{sv}",
1326 g_variant_new_string(info->service));
1328 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
1329 g_variant_new("(osa{sv})", info->obj_path,
1332 G_DBUS_CALL_FLAGS_NONE, -1,
1335 ERR("RegisterProfile failed: %s", err->message);
1337 if (g_strrstr(err->message, BT_HAL_ACCESS_DENIED_MSG))
1338 result = BT_STATUS_AUTH_REJECTED;
1340 result = BT_STATUS_FAIL;
1342 g_clear_error(&err);
1345 g_variant_builder_unref(option_builder);
1348 g_variant_unref(ret);
1353 void _bt_hal_unregister_profile(char *path)
1359 proxy = _bt_hal_get_profile_proxy();
1360 if (proxy == NULL) {
1361 ERR("Getting profile proxy failed");
1365 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
1366 g_variant_new("(o)", path),
1367 G_DBUS_CALL_FLAGS_NONE, -1,
1370 ERR("UnregisterProfile failed : %s", err->message);
1371 g_clear_error(&err);
1375 g_variant_unref(ret);
1380 static void __hal_new_connection_method(GDBusConnection *connection,
1381 const gchar *sender,
1382 const gchar *object_path,
1383 const gchar *interface_name,
1384 const gchar *method_name,
1385 GVariant *parameters,
1386 GDBusMethodInvocation *invocation,
1389 DBG("method %s", method_name);
1390 if (g_strcmp0(method_name, "NewConnection") == 0) {
1393 GUnixFDList *fd_list;
1395 GVariantBuilder *properties;
1397 bt_bdaddr_t remote_addr1;
1398 char addr[BT_HAL_ADDRESS_STRING_SIZE];
1399 bt_hal_new_connection_cb cb = user_data;
1401 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
1404 msg = g_dbus_method_invocation_get_message(invocation);
1405 fd_list = g_dbus_message_get_unix_fd_list(msg);
1406 if (fd_list == NULL) {
1407 GQuark quark = g_quark_from_string("rfcomm-app");
1408 GError *err = g_error_new(quark, 0, "No fd in message");
1409 g_dbus_method_invocation_return_gerror(invocation, err);
1415 fd = g_unix_fd_list_get(fd_list, index, NULL);
1417 ERR("Invalid fd return");
1418 GQuark quark = g_quark_from_string("rfcomm-app");
1419 GError *err = g_error_new(quark, 0, "Invalid FD return");
1420 g_dbus_method_invocation_return_gerror(invocation, err);
1424 INFO("Object Path %s", obj_path);
1426 _bt_hal_convert_device_path_to_address(obj_path, addr);
1427 _bt_hal_convert_addr_string_to_type(remote_addr1.address, (const char *)addr);
1428 INFO("fd: %d, address %s", fd, addr);
1430 g_dbus_method_invocation_return_value(invocation, NULL);
1433 cb(object_path, fd, &remote_addr1);
1434 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
1435 g_dbus_method_invocation_return_value(invocation, NULL);
1439 static GDBusNodeInfo *_bt_hal_get_gdbus_node(const gchar *xml_data)
1442 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
1444 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1446 G_BUS_NAME_OWNER_FLAGS_NONE,
1452 DBG("Got bus id %d", bus_id);
1456 return g_dbus_node_info_new_for_xml(xml_data, NULL);
1459 static const GDBusInterfaceVTable method_table = {
1460 __hal_new_connection_method,
1465 int _bt_hal_register_new_gdbus_object(const char *path, bt_hal_new_connection_cb cb)
1467 GDBusConnection *gconn;
1469 GError *error = NULL;
1471 gconn = _bt_hal_get_system_gconn();
1475 if (new_conn_node == NULL)
1476 new_conn_node = _bt_hal_get_gdbus_node(rfcomm_agent_xml);
1478 if (new_conn_node == NULL)
1481 id = g_dbus_connection_register_object(gconn, path,
1482 new_conn_node->interfaces[0],
1486 ERR("Failed to register: %s", error->message);
1487 g_error_free(error);
1491 DBG("NEW CONNECTION ID %d", id);
1496 void _bt_hal_unregister_gdbus_object(int object_id)
1498 GDBusConnection *gconn;
1500 gconn = _bt_hal_get_system_gconn();
1504 g_dbus_connection_unregister_object(gconn, object_id);
1507 int _bt_hal_discover_services(char *address, char *uuid, void *cb, gpointer func_data)
1511 GDBusProxy *adapter_proxy;
1513 GDBusConnection *conn;
1515 conn = _bt_hal_get_system_gconn();
1517 ERR("conn == NULL, return");
1518 return BT_STATUS_FAIL;
1521 object_path = _bt_hal_get_device_object_path(address);
1522 if (object_path == NULL) {
1523 GVariant *ret = NULL;
1525 INFO("No searched device");
1526 adapter_proxy = _bt_hal_get_adapter_proxy();
1527 if (adapter_proxy == NULL) {
1528 ERR("adapter_proxy == NULL, return");
1529 return BT_STATUS_FAIL;
1532 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1533 g_variant_new("(s)", address),
1534 G_DBUS_CALL_FLAGS_NONE,
1535 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1538 ERR("CreateDevice Failed: %s", err->message);
1539 g_clear_error(&err);
1543 g_variant_unref(ret);
1545 g_object_unref(adapter_proxy);
1546 object_path = _bt_hal_get_device_object_path(address);
1547 if (object_path == NULL) {
1548 ERR("object_path == NULL, return");
1549 return BT_STATUS_FAIL;
1553 proxy = g_dbus_proxy_new_sync(conn,
1554 G_DBUS_PROXY_FLAGS_NONE, NULL,
1555 BT_HAL_BLUEZ_NAME, object_path,
1556 BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1557 g_free(object_path);
1558 if (proxy == NULL) {
1559 ERR("Error while getting proxy");
1560 return BT_STATUS_FAIL;
1563 g_dbus_proxy_call(proxy, "DiscoverServices",
1564 g_variant_new("(s)", uuid),
1565 G_DBUS_CALL_FLAGS_NONE,
1566 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1567 (GAsyncReadyCallback)cb,
1570 return BT_STATUS_SUCCESS;
1573 int _bt_hal_cancel_discovers(char *address)
1577 GDBusProxy *adapter_proxy;
1579 GDBusConnection *conn;
1581 conn = _bt_hal_get_system_gconn();
1583 return BT_STATUS_FAIL;
1585 object_path = _bt_hal_get_device_object_path(address);
1586 if (object_path == NULL) {
1587 GVariant *ret = NULL;
1588 INFO("No searched device");
1589 adapter_proxy = _bt_hal_get_adapter_proxy();
1590 if (adapter_proxy == NULL) {
1591 ERR("adapter_proxy == NULL, return");
1592 return BT_STATUS_FAIL;
1595 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1596 g_variant_new("(s)", address),
1597 G_DBUS_CALL_FLAGS_NONE,
1598 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1601 ERR("CreateDevice Failed: %s", err->message);
1602 g_clear_error(&err);
1606 g_variant_unref(ret);
1608 g_object_unref(adapter_proxy);
1610 object_path = _bt_hal_get_device_object_path(address);
1611 if (object_path == NULL)
1612 return BT_STATUS_FAIL;
1615 proxy = g_dbus_proxy_new_sync(conn,
1616 G_DBUS_PROXY_FLAGS_NONE, NULL,
1617 BT_HAL_BLUEZ_NAME, object_path,
1618 BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1619 g_free(object_path);
1620 g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1622 G_DBUS_CALL_FLAGS_NONE,
1623 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1626 ERR("DBus Error message: [%s]", err->message);
1627 g_clear_error(&err);
1628 return BT_STATUS_FAIL;
1632 g_object_unref(proxy);
1634 return BT_STATUS_SUCCESS;
1637 int _bt_hal_discover_service_uuids(char *address, char *remote_uuid)
1641 GDBusConnection *gconn;
1643 char **uuid_value = NULL;
1646 GVariant *value = NULL;
1647 GVariant *ret = NULL;
1648 int result = BT_STATUS_FAIL;
1652 if (remote_uuid == NULL) {
1653 ERR("remote_uuid == NULL, return");
1654 return BT_STATUS_FAIL;
1657 gconn = _bt_hal_get_system_gconn();
1658 if (gconn == NULL) {
1659 ERR("gconn == NULL, return");
1660 return BT_STATUS_FAIL;
1663 object_path = _bt_hal_get_device_object_path(address);
1664 if (object_path == NULL) {
1665 ERR("object_path == NULL, return");
1666 return BT_STATUS_FAIL;
1669 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1670 BT_HAL_BLUEZ_NAME, object_path, BT_HAL_PROPERTIES_INTERFACE, NULL,
1672 if (proxy == NULL) {
1673 g_free(object_path);
1674 ERR("proxy == NULL, return");
1675 return BT_STATUS_FAIL;
1679 ERR("DBus Error: [%s]", err->message);
1680 g_clear_error(&err);
1683 ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1684 g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
1685 G_DBUS_CALL_FLAGS_NONE,
1686 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1689 result = BT_STATUS_FAIL;
1690 ERR("DBus Error : %s", err->message);
1691 g_clear_error(&err);
1696 ERR("g_dbus_proxy_call_sync function return NULL");
1697 result = BT_STATUS_FAIL;
1701 g_variant_get(ret, "(@a{sv})", &value);
1702 g_variant_unref(ret);
1704 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1705 G_VARIANT_TYPE_STRING_ARRAY);
1708 size = g_variant_get_size(temp_value);
1711 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1712 DBG("Size items %d", size);
1716 g_variant_unref(temp_value);
1719 for (i = 0; uuid_value[i] != NULL; i++) {
1720 DBG("Remote uuids %s, searched uuid: %s",
1721 uuid_value[i], remote_uuid);
1722 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
1723 result = BT_STATUS_SUCCESS;
1732 g_object_unref(proxy);
1734 g_variant_unref(value);
1742 int bt_hal_gatt_convert_prop2string(
1743 bt_hal_gatt_characteristic_property_t properties,
1744 char *char_properties[])
1748 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1749 char_properties[flag_count] = g_strdup("broadcast");
1752 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_READ) {
1753 char_properties[flag_count] = g_strdup("read");
1756 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1757 char_properties[flag_count] = g_strdup("write-without-response");
1760 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1761 char_properties[flag_count] = g_strdup("write");
1764 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1765 char_properties[flag_count] = g_strdup("notify");
1768 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1769 char_properties[flag_count] = g_strdup("indicate");
1772 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1773 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1776 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1777 char_properties[flag_count] = g_strdup("reliable-write");
1780 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1781 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1784 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1785 char_properties[flag_count] = g_strdup("encrypt-read");
1788 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1789 char_properties[flag_count] = g_strdup("encrypt-write");
1792 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1793 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1796 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1797 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1801 if (flag_count == 0) {
1802 char_properties[flag_count] = g_strdup("read");
1809 int bt_hal_gatt_convert_perm2string(
1810 bt_hal_gatt_permission_t properties,
1811 char *char_properties[])
1815 if (properties & BT_HAL_GATT_PERMISSION_READ) {
1816 char_properties[flag_count] = g_strdup("read");
1819 if (properties & BT_HAL_GATT_PERMISSION_WRITE) {
1820 char_properties[flag_count] = g_strdup("write");
1823 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_READ) {
1824 char_properties[flag_count] = g_strdup("encrypt-read");
1827 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE) {
1828 char_properties[flag_count] = g_strdup("encrypt-write");
1831 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1832 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1835 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1836 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1840 if (flag_count == 0) {
1841 char_properties[flag_count] = g_strdup("read");
1848 gboolean _bt_hal_is_service_enabled(const char *uuid)
1851 GError *error = NULL;
1854 GVariantIter *iter = NULL;
1856 gboolean ret = FALSE;
1860 proxy = _bt_hal_get_adapter_properties_proxy();
1862 DBG("_bt_hal_dbus_get_local_name: Adapter Properties proxy get failed!!!");
1866 result = g_dbus_proxy_call_sync(proxy,
1867 "Get", g_variant_new("(ss)",
1868 BT_HAL_ADAPTER_INTERFACE, "UUIDs"),
1869 G_DBUS_CALL_FLAGS_NONE, -1,
1872 if (error != NULL) {
1873 ERR("Failed to get UUIDs (Error: %s)", error->message);
1874 g_clear_error(&error);
1876 ERR("Failed to get UUIDs");
1880 g_variant_get(result, "(v)", &temp);
1881 g_variant_get(temp, "as", &iter);
1883 ERR("Failed to get UUIDs");
1884 g_variant_unref(result);
1888 while (g_variant_iter_loop(iter, "s", &uuid_str)) {
1889 DBG("UUID string [%s]\n", uuid_str);
1890 if (!strncasecmp(uuid, uuid_str, strlen(uuid))) {
1896 g_variant_iter_free(iter);
1897 g_variant_unref(result);
1898 g_variant_unref(temp);