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
53 #define BT_AUDIO_SOURCE_MAX 2
55 static char *avrcp_control_path = NULL;
56 static char *avrcp_transport_path = NULL;
58 static GDBusConnection *system_conn;
59 static GDBusConnection *session_conn;
60 static GDBusProxy *manager_gproxy = NULL;
61 static GDBusProxy *adapter_gproxy = NULL;
62 static GDBusProxy *profile_gproxy = NULL;
64 static GDBusProxy *adapter_properties_proxy;
65 static GDBusProxy *avrcp_ctrl_proxy;
68 GDBusProxy *avrcp_ctrl_proxy;
69 char *avrcp_control_path;
73 struct avrcp_proxy proxy_array[BT_AUDIO_SOURCE_MAX];
76 GDBusNodeInfo *new_conn_node;
77 static const gchar rfcomm_agent_xml[] =
79 " <interface name='org.bluez.Profile1'>"
80 " <method name='NewConnection'>"
81 " <arg type='o' name='object' direction='in'/>"
82 " <arg type='h' name='fd' direction='in'/>"
83 " <arg type='a{sv}' name='properties' direction='in'/>"
85 " <method name='RequestDisconnection'>"
86 " <arg type='o' name='device' direction='in'/>"
91 static GDBusConnection *__bt_hal_init_session_conn(void)
93 if (session_conn == NULL)
94 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
99 GDBusConnection *_bt_hal_get_session_gconn(void)
101 return (session_conn) ? session_conn : __bt_hal_init_session_conn();
104 static GDBusConnection *__bt_hal_init_system_gconn(void)
106 GError *error = NULL;
108 if (system_conn != NULL)
111 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
114 ERR("Unable to connect to dbus: %s", error->message);
115 g_clear_error(&error);
121 GDBusConnection *_bt_hal_get_system_gconn(void)
123 GDBusConnection *local_system_gconn = NULL;
124 GError *error = NULL;
126 if (system_conn == NULL) {
127 system_conn = __bt_hal_init_system_gconn();
128 } else if (g_dbus_connection_is_closed(system_conn)) {
130 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
132 if (!local_system_gconn) {
133 ERR("Unable to connect to dbus: %s", error->message);
134 g_clear_error(&error);
137 system_conn = local_system_gconn;
143 static GDBusProxy *__bt_hal_init_manager_proxy(void)
147 if (system_conn == NULL) {
148 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
149 if (system_conn == NULL)
153 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
154 NULL, BT_HAL_BLUEZ_NAME,
155 BT_HAL_MANAGER_PATH, BT_HAL_MANAGER_INTERFACE, NULL, NULL);
160 manager_gproxy = proxy;
165 static GDBusProxy *__bt_hal_init_adapter_proxy(void)
167 GDBusProxy *manager_proxy;
169 char *adapter_path = NULL;
171 if (system_conn == NULL) {
172 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
173 if (system_conn == NULL)
177 manager_proxy = _bt_hal_get_manager_proxy();
178 if (manager_proxy == NULL)
181 adapter_path = _bt_hal_get_adapter_path();
182 if (adapter_path == NULL)
185 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
186 NULL, BT_HAL_BLUEZ_NAME,
187 adapter_path, BT_HAL_ADAPTER_INTERFACE, NULL, NULL);
189 g_free(adapter_path);
194 adapter_gproxy = proxy;
199 static GDBusProxy *__bt_hal_init_adapter_properties_proxy(void)
201 GDBusProxy *manager_proxy;
203 char *adapter_path = NULL;
205 if (system_conn == NULL) {
206 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
207 if (system_conn == NULL)
211 manager_proxy = _bt_hal_get_manager_proxy();
212 if (manager_proxy == NULL)
215 adapter_path = _bt_hal_get_adapter_path();
216 if (adapter_path == NULL)
219 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
220 NULL, BT_HAL_BLUEZ_NAME,
221 adapter_path, BT_HAL_PROPERTIES_INTERFACE, NULL, NULL);
223 g_free(adapter_path);
228 adapter_properties_proxy = proxy;
233 void _bt_hal_set_control_device_path(const char *path)
240 DBG("control_path = %s", path);
242 for (i = 0; i < BT_AUDIO_SOURCE_MAX; i++) {
243 if (proxy_array[i].avrcp_control_path == NULL) {
244 proxy_array[i].avrcp_control_path = g_strdup(path);
245 DBG("PATH %s formed index %d", proxy_array[i].avrcp_control_path, i);
251 void _bt_hal_remove_control_device_path(const char *path)
258 for (i = 0; i < BT_AUDIO_SOURCE_MAX; i++) {
259 if (g_strcmp0(proxy_array[i].avrcp_control_path, path) == 0) {
260 DBG("Clear AVRCP proxy[%d]", i);
261 g_free(proxy_array[i].avrcp_control_path);
262 g_object_unref(proxy_array[i].avrcp_ctrl_proxy);
264 proxy_array[i].avrcp_control_path = NULL;
265 proxy_array[i].avrcp_ctrl_proxy = NULL;
266 memset(proxy_array[i].bd_addr.address, 0, 6);
272 static char *__bt_hal_extract_control_device_path(GVariantIter *iter, char *address)
274 char *object_path = NULL;
275 char *interface_str = NULL;
276 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
278 /* Parse the signature: oa{sa{sv}}} */
279 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_str)) {
280 if (object_path == NULL)
283 if (g_strcmp0(interface_str, BT_HAL_PLAYER_CONTROL_INTERFACE) == 0) {
284 _bt_hal_convert_device_path_to_address(object_path, device_address);
285 if (g_strcmp0(address, device_address) == 0)
286 return g_strdup(object_path);
292 static char *__bt_hal_extract_transport_device_path(GVariantIter *iter, char *address)
294 char *object_path = NULL;
295 char *interface_str = NULL;
296 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
298 /* Parse the signature: oa{sa{sv}}} */
299 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_str)) {
300 if (object_path == NULL)
303 if (g_strcmp0(interface_str, BT_HAL_MEDIATRANSPORT_INTERFACE) == 0) {
304 _bt_hal_convert_device_path_to_address(object_path, device_address);
305 if (g_strcmp0(address, device_address) == 0)
306 return g_strdup(object_path);
312 static char *__bt_hal_get_control_device_object_path(char *address)
314 char *object_path = NULL;
315 GDBusConnection *conn;
316 GDBusProxy *manager_proxy;
317 GVariant *result = NULL;
318 GVariantIter *iter = NULL;
320 conn = _bt_hal_get_system_gconn();
324 manager_proxy = _bt_hal_get_manager_proxy();
325 if (manager_proxy == NULL)
328 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
330 G_DBUS_CALL_FLAGS_NONE,
335 ERR("Can't get managed objects");
339 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
340 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
341 object_path = __bt_hal_extract_control_device_path(iter, address);
342 g_variant_iter_free(iter);
343 g_variant_unref(result);
347 static char *__bt_hal_get_transport_device_object_path(char *address)
349 char *object_path = NULL;
350 GDBusConnection *conn;
351 GDBusProxy *manager_proxy;
352 GVariant *result = NULL;
353 GVariantIter *iter = NULL;
355 conn = _bt_hal_get_system_gconn();
359 manager_proxy = _bt_hal_get_manager_proxy();
360 if (manager_proxy == NULL)
363 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
365 G_DBUS_CALL_FLAGS_NONE,
370 ERR("Can't get managed objects");
374 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
375 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
376 object_path = __bt_hal_extract_transport_device_path(iter, address);
377 g_variant_iter_free(iter);
378 g_variant_unref(result);
382 char *_bt_hal_get_control_device_path(bt_bdaddr_t *bd_addr)
385 char connected_address[BT_HAL_ADDRESS_STRING_SIZE];
387 if (avrcp_control_path != NULL)
388 return avrcp_control_path;
390 _bt_hal_convert_addr_type_to_string(connected_address, bd_addr->address);
392 control_path = __bt_hal_get_control_device_object_path(connected_address);
393 if (control_path == NULL)
396 avrcp_control_path = control_path;
397 DBG("control_path = %s", control_path);
401 char *_bt_hal_get_transport_device_path(bt_bdaddr_t *bd_addr)
403 char *transport_path;
404 char connected_address[BT_HAL_ADDRESS_STRING_SIZE];
406 if (avrcp_transport_path != NULL)
407 return avrcp_transport_path;
409 _bt_hal_convert_addr_type_to_string(connected_address, bd_addr->address);
411 transport_path = __bt_hal_get_transport_device_object_path(connected_address);
412 if (transport_path == NULL)
415 avrcp_transport_path = transport_path;
416 DBG("transport_path = %s", transport_path);
417 return transport_path;
420 static GDBusProxy *__bt_hal_init_avrcp_ctrl_proxy(bt_bdaddr_t *bd_addr)
422 GDBusProxy *manager_proxy;
424 GDBusConnection *gconn = NULL;
427 gconn = _bt_hal_get_system_gconn();
431 manager_proxy = _bt_hal_get_manager_proxy();
432 if (manager_proxy == NULL)
435 for (i = 0; i < BT_AUDIO_SOURCE_MAX; i++) {
436 if (proxy_array[i].avrcp_ctrl_proxy == NULL) {
437 memcpy(proxy_array[i].bd_addr.address, bd_addr->address, 6);
438 DBG("PATH %s formed index %d ", proxy_array[i].avrcp_control_path, i);
443 if (i == BT_AUDIO_SOURCE_MAX) {
444 ERR("NO free arr proxy space found");
448 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
449 NULL, BT_HAL_BLUEZ_NAME,
450 proxy_array[i].avrcp_control_path, BT_HAL_PLAYER_CONTROL_INTERFACE, NULL, NULL);
455 avrcp_ctrl_proxy = proxy;
456 proxy_array[i].avrcp_ctrl_proxy = proxy;
461 GDBusProxy *_bt_hal_get_manager_proxy(void)
463 if (manager_gproxy) {
464 const gchar *path = g_dbus_proxy_get_object_path(manager_gproxy);
466 ERR("Already proxy released hence creating new proxy");
467 return __bt_hal_init_manager_proxy();
469 return manager_gproxy;
471 return __bt_hal_init_manager_proxy();
474 GDBusProxy *_bt_hal_get_adapter_proxy(void)
476 if (adapter_gproxy) {
477 const char *path = g_dbus_proxy_get_object_path(adapter_gproxy);
479 ERR("Already proxy released hence creating new proxy");
480 return __bt_hal_init_adapter_proxy();
483 return adapter_gproxy;
485 return __bt_hal_init_adapter_proxy();
489 GDBusProxy *_bt_hal_get_avrcp_ctrl_proxy(bt_bdaddr_t *bd_addr)
493 for (i = 0; i < BT_AUDIO_SOURCE_MAX; i++) {
494 if (proxy_array[i].avrcp_ctrl_proxy
495 && (!memcmp(proxy_array[i].bd_addr.address, bd_addr->address, 6))) {
496 const gchar *path = g_dbus_proxy_get_object_path(proxy_array[i].avrcp_ctrl_proxy);
499 proxy_array[i].avrcp_ctrl_proxy = NULL;
500 ERR("Already proxy released hence creating new proxy");
501 return __bt_hal_init_avrcp_ctrl_proxy(bd_addr);
504 DBG("address found path PATH %s", path);
505 return proxy_array[i].avrcp_ctrl_proxy;
509 DBG("address NOT found");
511 return __bt_hal_init_avrcp_ctrl_proxy(bd_addr);
514 GDBusProxy *_bt_hal_get_avrcp_ctrl_properties_proxy(bt_bdaddr_t *bd_addr)
517 GError *error = NULL;
518 char *control_path = NULL;
519 GDBusConnection *conn = NULL;
521 control_path = _bt_hal_get_control_device_path(bd_addr);
522 if (control_path == NULL)
525 DBG("control_path = %s", control_path);
527 conn = _bt_hal_get_system_gconn();
529 ERR("FAIL to get system connection");
532 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
533 NULL, BT_HAL_BLUEZ_NAME,
534 control_path, BT_HAL_PROPERTIES_INTERFACE, NULL, &error);
537 ERR("Unable to allocate new proxy");
539 ERR("%s", error->message);
540 g_clear_error(&error);
548 GDBusProxy *_bt_hal_get_avrcp_transport_properties_proxy(bt_bdaddr_t *bd_addr)
551 GError *error = NULL;
552 char *transport_path = NULL;
553 GDBusConnection *conn = NULL;
555 transport_path = _bt_hal_get_transport_device_path(bd_addr);
556 if (transport_path == NULL)
559 DBG("transport_path = %s", transport_path);
561 conn = _bt_hal_get_system_gconn();
563 ERR("FAIL to get system connection");
566 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
567 NULL, BT_HAL_BLUEZ_NAME,
568 transport_path, BT_HAL_PROPERTIES_INTERFACE, NULL, &error);
571 ERR("Unable to allocate new proxy");
573 ERR("%s", error->message);
574 g_clear_error(&error);
582 GDBusProxy *_bt_hal_get_adapter_properties_proxy(void)
584 return (adapter_properties_proxy) ? adapter_properties_proxy :
585 __bt_hal_init_adapter_properties_proxy();
588 GDBusProxy *_bt_hal_get_profile_proxy(void)
590 GDBusConnection *gconn;
594 return profile_gproxy;
596 gconn = _bt_hal_get_system_gconn();
598 ERR("_bt_hal_get_system_gconn failed");
602 profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
603 NULL, BT_HAL_BLUEZ_NAME,
605 "org.bluez.ProfileManager1",
608 ERR("Unable to create proxy: %s", err->message);
613 return profile_gproxy;
616 static char *__bt_hal_extract_adapter_path(GVariantIter *iter)
618 char *object_path = NULL;
619 GVariantIter *interface_iter;
620 GVariantIter *svc_iter;
621 char *interface_str = NULL;
623 /* Parse the signature: oa{sa{sv}}} */
624 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
627 if (object_path == NULL)
630 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
631 &interface_str, &svc_iter)) {
632 if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
635 DBG("Object Path: %s", object_path);
636 g_free(interface_str);
637 g_variant_iter_free(svc_iter);
638 g_variant_iter_free(interface_iter);
639 return g_strdup(object_path);
645 char *_bt_hal_get_adapter_path(void)
647 GDBusConnection *conn;
648 GDBusProxy *manager_proxy;
649 GVariant *result = NULL;
650 GVariantIter *iter = NULL;
651 char *adapter_path = NULL;
653 conn = _bt_hal_get_system_gconn();
657 manager_proxy = _bt_hal_get_manager_proxy();
658 if (manager_proxy == NULL)
661 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
663 G_DBUS_CALL_FLAGS_NONE,
668 ERR("Can't get managed objects");
672 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
673 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
675 adapter_path = __bt_hal_extract_adapter_path(iter);
676 g_variant_iter_free(iter);
677 g_variant_unref(result);
682 int _bt_hal_is_adapter_powered(gboolean *powered)
685 GError *error = NULL;
690 proxy = _bt_hal_get_adapter_properties_proxy();
692 return BT_STATUS_FAIL;
694 result = g_dbus_proxy_call_sync(proxy,
696 g_variant_new("(ss)", BT_HAL_ADAPTER_INTERFACE,
698 G_DBUS_CALL_FLAGS_NONE,
704 ERR("Failed to get powered status");
706 ERR("Failed to get powered status (Error: %s)", error->message);
707 g_clear_error(&error);
709 return BT_STATUS_FAIL;
712 g_variant_get(result, "(v)", &temp);
713 *powered = g_variant_get_boolean(temp);
714 INFO("powered: %d", *powered);
716 g_variant_unref(result);
717 g_variant_unref(temp);
718 return BT_STATUS_SUCCESS;
721 void _bt_hal_deinit_bluez_proxy(void)
723 if (manager_gproxy) {
724 g_object_unref(manager_gproxy);
725 manager_gproxy = NULL;
728 if (adapter_gproxy) {
729 g_object_unref(adapter_gproxy);
730 adapter_gproxy = NULL;
732 if (adapter_properties_proxy) {
733 g_object_unref(adapter_properties_proxy);
734 adapter_properties_proxy = NULL;
738 void _bt_hal_deinit_proxys(void)
740 _bt_hal_deinit_bluez_proxy();
743 g_object_unref(system_conn);
748 g_object_unref(session_conn);
753 GDBusProxy *_bt_hal_get_hid_agent_proxy(void)
755 GDBusConnection *conn;
759 conn = _bt_hal_get_system_gconn();
761 ERR("_bt_hal_get_system_gconn failed");
765 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
766 BT_HAL_HID_SERVICE_NAME, BT_HAL_HID_AGENT_OBJECT_PATH,
767 BT_HAL_HID_SERVICE_INTERFACE, NULL, &err);
770 ERR("Unable to create proxy: %s", err->message);
779 void _bt_hal_convert_device_path_to_address(const char *device_path,
780 char *device_address)
782 char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
785 if (device_path == NULL || device_address == NULL)
788 dev_addr = strstr(device_path, "dev_");
789 if (dev_addr != NULL) {
792 g_strlcpy(address, dev_addr, sizeof(address));
794 while ((pos = strchr(address, '_')) != NULL)
797 g_strlcpy(device_address, address, BT_HAL_ADDRESS_STRING_SIZE);
801 gboolean _bt_hal_uuid_is_standard(bt_uuid_t *p_uuid)
803 uint32_t uuid0, uuid4;
804 uint16_t uuid1, uuid2, uuid3, uuid5;
805 const char *uuid_name;
806 const char *uuid_name1;
808 memcpy(&uuid0, &(p_uuid->uu[0]), 4);
809 memcpy(&uuid1, &(p_uuid->uu[4]), 2);
810 memcpy(&uuid2, &(p_uuid->uu[6]), 2);
811 memcpy(&uuid3, &(p_uuid->uu[8]), 2);
812 memcpy(&uuid4, &(p_uuid->uu[10]), 4);
813 memcpy(&uuid5, &(p_uuid->uu[14]), 2);
815 uuid_name = _bt_hal_dump_uuid_name(ntohl(uuid0));
816 uuid_name1 = _bt_hal_dump_uuid_name((ntohl(uuid4) >> 16));
818 DBG("UUID Name [%s]", uuid_name);
819 DBG("UUID Name Shifted [%s]", uuid_name1);
821 if (!g_strcmp0(uuid_name, "--"))
828 void _bt_hal_convert_uuid_string_to_type(unsigned char *uuid,
829 const char *device_uuid)
831 uint32_t uuid0, uuid4;
832 uint16_t uuid1, uuid2, uuid3, uuid5;
834 sscanf(device_uuid, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
835 &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
837 uuid0 = htonl(uuid0);
838 uuid1 = htons(uuid1);
839 uuid2 = htons(uuid2);
840 uuid3 = htons(uuid3);
841 uuid4 = htonl(uuid4);
842 uuid5 = htons(uuid5);
844 memcpy(&(uuid[0]), &uuid0, 4);
845 memcpy(&(uuid[4]), &uuid1, 2);
846 memcpy(&(uuid[6]), &uuid2, 2);
847 memcpy(&(uuid[8]), &uuid3, 2);
848 memcpy(&(uuid[10]), &uuid4, 4);
849 memcpy(&(uuid[14]), &uuid5, 2);
852 void _bt_hal_convert_uuid_type_to_string(char *str, const unsigned char *uuid)
864 snprintf(str, BT_HAL_UUID_STRING_LEN,
865 "%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",
866 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
867 uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
870 void _bt_hal_convert_addr_string_to_type(unsigned char *addr,
876 if (address == NULL || addr == NULL)
879 for (i = 0; i < BT_HAL_ADDRESS_LENGTH_MAX; i++) {
880 addr[i] = strtol(address, &ptr, 16);
882 if (ptr[0] != '\0') {
891 void _bt_hal_convert_addr_type_to_string(char *address,
892 const unsigned char *addr)
894 if (address == NULL || addr == NULL)
897 snprintf(address, BT_HAL_ADDRESS_STRING_SIZE,
898 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
899 addr[0], addr[1], addr[2],
900 addr[3], addr[4], addr[5]);
903 void _bt_hal_print_device_address_t(const bt_hal_device_address_t *addr)
905 DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
906 addr->addr[3], addr->addr[4], addr->addr[5]);
909 void _bt_hal_divide_device_class(bt_hal_device_class_t *device_class,
912 if (device_class == NULL)
915 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
916 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
917 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
919 if (cod & 0x002000) {
920 device_class->service_class |=
921 BT_HAL_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
925 int _bt_hal_copy_utf8_string(char *dest, const char *src, unsigned int length)
932 if (dest == NULL || src == NULL)
933 return BT_HAL_ERROR_INVALID_PARAM;
935 DBG("+src : %s", src);
936 DBG("+dest : %s", dest);
939 while (*p != '\0' && i < length) {
940 next = g_utf8_next_char(p);
943 while (count > 0 && ((i + count) < length)) {
950 return BT_HAL_ERROR_NONE;
953 gboolean _bt_hal_utf8_validate(char *name)
956 glong items_written = 0;
958 if (FALSE == g_utf8_validate(name, -1, NULL))
961 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
967 if (items_written != g_utf8_strlen(name, -1))
973 int _bt_hal_set_socket_non_blocking(int socket_fd)
975 /* Set Nonblocking */
978 arg = fcntl(socket_fd, F_GETFL);
983 if (arg & O_NONBLOCK)
984 ERR("Already Non-blocking \n");
988 if (fcntl(socket_fd, F_SETFL, arg) < 0)
991 return BT_HAL_ERROR_NONE;
994 int _bt_hal_set_non_blocking_tty(int sk)
996 struct termios ti = {0,};
999 err = _bt_hal_set_socket_non_blocking(sk);
1002 ERR("Error in set non blocking!\n");
1006 tcflush(sk, TCIOFLUSH);
1008 /* Switch tty to RAW mode */
1010 tcsetattr(sk, TCSANOW, &ti);
1012 return BT_HAL_ERROR_NONE;
1015 static char *__bt_hal_extract_device_path(GVariantIter *iter, char *address)
1017 char *object_path = NULL;
1018 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
1020 /* Parse the signature: oa{sa{sv}}} */
1021 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, NULL)) {
1022 if (object_path == NULL)
1024 _bt_hal_convert_device_path_to_address(object_path, device_address);
1025 if (g_strcmp0(address, device_address) == 0)
1026 return g_strdup(object_path);
1031 char *_bt_hal_get_device_object_path(char *address)
1033 char *object_path = NULL;
1034 GDBusConnection *conn;
1035 GDBusProxy *manager_proxy;
1036 GVariant *result = NULL;
1037 GVariantIter *iter = NULL;
1039 conn = _bt_hal_get_system_gconn();
1043 manager_proxy = _bt_hal_get_manager_proxy();
1044 if (manager_proxy == NULL)
1047 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
1049 G_DBUS_CALL_FLAGS_NONE,
1054 ERR("Can't get managed objects");
1058 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
1059 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1060 object_path = __bt_hal_extract_device_path(iter, address);
1061 g_variant_iter_free(iter);
1062 g_variant_unref(result);
1066 GVariant *_bt_hal_get_managed_objects(void)
1068 GDBusConnection *conn;
1069 GDBusProxy *manager_proxy;
1070 GVariant *result = NULL;
1071 GError *error = NULL;
1073 conn = _bt_hal_get_system_gconn();
1077 manager_proxy = _bt_hal_get_manager_proxy();
1078 if (manager_proxy == NULL)
1081 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
1083 G_DBUS_CALL_FLAGS_NONE,
1088 ERR("Can't get managed objects");
1093 ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1094 g_clear_error(&error);
1101 char *_bt_hal_convert_error_to_string(int error)
1104 case BT_HAL_ERROR_CANCEL:
1106 case BT_HAL_ERROR_INVALID_PARAM:
1107 return "INVALID_PARAMETER";
1108 case BT_HAL_ERROR_INVALID_DATA:
1109 return "INVALID DATA";
1110 case BT_HAL_ERROR_MEMORY_ALLOCATION:
1111 case BT_HAL_ERROR_OUT_OF_MEMORY:
1112 return "OUT_OF_MEMORY";
1113 case BT_HAL_ERROR_TIMEOUT:
1115 case BT_HAL_ERROR_NO_RESOURCES:
1116 return "NO_RESOURCES";
1117 case BT_HAL_ERROR_INTERNAL:
1119 case BT_HAL_ERROR_NOT_SUPPORT:
1120 return "NOT_SUPPORT";
1121 case BT_HAL_ERROR_DEVICE_NOT_ENABLED:
1122 return "NOT_ENABLED";
1123 case BT_HAL_ERROR_DEVICE_ALREADY_ENABLED:
1124 return "ALREADY_ENABLED";
1125 case BT_HAL_ERROR_DEVICE_BUSY:
1126 return "DEVICE_BUSY";
1127 case BT_HAL_ERROR_ACCESS_DENIED:
1128 return "ACCESS_DENIED";
1129 case BT_HAL_ERROR_MAX_CLIENT:
1130 return "MAX_CLIENT";
1131 case BT_HAL_ERROR_NOT_FOUND:
1133 case BT_HAL_ERROR_SERVICE_SEARCH_ERROR:
1134 return "SERVICE_SEARCH_ERROR";
1135 case BT_HAL_ERROR_PARING_FAILED:
1136 return "PARING_FAILED";
1137 case BT_HAL_ERROR_NOT_PAIRED:
1138 return "NOT_PAIRED";
1139 case BT_HAL_ERROR_SERVICE_NOT_FOUND:
1140 return "SERVICE_NOT_FOUND";
1141 case BT_HAL_ERROR_NOT_CONNECTED:
1142 return "NOT_CONNECTED";
1143 case BT_HAL_ERROR_ALREADY_CONNECT:
1144 return "ALREADY_CONNECT";
1145 case BT_HAL_ERROR_CONNECTION_BUSY:
1146 return "CONNECTION_BUSY";
1147 case BT_HAL_ERROR_CONNECTION_ERROR:
1148 return "CONNECTION_ERROR";
1149 case BT_HAL_ERROR_MAX_CONNECTION:
1150 return "MAX_CONNECTION";
1151 case BT_HAL_ERROR_NOT_IN_OPERATION:
1152 return "NOT_IN_OPERATION";
1153 case BT_HAL_ERROR_CANCEL_BY_USER:
1154 return "CANCEL_BY_USER";
1155 case BT_HAL_ERROR_REGISTRATION_FAILED:
1156 return "REGISTRATION_FAILED";
1157 case BT_HAL_ERROR_IN_PROGRESS:
1158 return "IN_PROGRESS";
1159 case BT_HAL_ERROR_AUTHENTICATION_FAILED:
1160 return "AUTHENTICATION_FAILED";
1161 case BT_HAL_ERROR_HOST_DOWN:
1163 case BT_HAL_ERROR_END_OF_DEVICE_LIST:
1164 return "END_OF_DEVICE_LIST";
1165 case BT_HAL_ERROR_AGENT_ALREADY_EXIST:
1166 return "AGENT_ALREADY_EXIST";
1167 case BT_HAL_ERROR_AGENT_DOES_NOT_EXIST:
1168 return "AGENT_DOES_NOT_EXIST";
1169 case BT_HAL_ERROR_ALREADY_INITIALIZED:
1170 return "ALREADY_INITIALIZED";
1171 case BT_HAL_ERROR_PERMISSION_DEINED:
1172 return "PERMISSION_DEINED";
1173 case BT_HAL_ERROR_ALREADY_DEACTIVATED:
1174 return "ALREADY_DEACTIVATED";
1175 case BT_HAL_ERROR_NOT_INITIALIZED:
1176 return "NOT_INITIALIZED";
1182 char * _bt_hal_convert_disc_reason_to_string(int reason)
1188 return "Connection terminated by local host";
1190 return "Remote user terminated connection";
1197 int _bt_hal_convert_disc_reason_to_status(int reason)
1201 return BT_STATUS_CONN_TOUT; //"Link loss"
1203 return BT_STATUS_CONN_TERM_LOCAL_HOST; //"Connection terminated by local host";
1205 return BT_STATUS_CONN_TERM_RMT_HOST; //"Connection terminated by local host";
1208 return BT_STATUS_FAIL;
1212 void _bt_hal_logging_connection(gboolean connect, int addr_type)
1214 static int le_conn = 0;
1215 static int le_disc = 0;
1216 static int edr_conn = 0;
1217 static int edr_disc = 0;
1231 INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
1232 le_conn, le_disc, edr_conn, edr_disc);
1235 void _bt_hal_swap_byte_ordering(char *data, int data_len)
1242 /* Swap to opposite endian */
1243 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
1250 int _bt_hal_byte_arr_cmp(const char *data1, const char *data2, int data_len)
1254 if (data1 == NULL || data2 == NULL)
1256 for (i = 0; i < data_len; i++) {
1257 if (data1[i] != data2[i])
1258 return data1[i] - data2[i];
1262 int _bt_hal_byte_arr_cmp_with_mask(const char *data1, const char *data2,
1263 const char *mask, int data_len)
1268 if (data1 == NULL || data2 == NULL || mask == NULL)
1271 for (i = 0; i < data_len; i++) {
1272 a = data1[i] & mask[i];
1273 b = data2[i] & mask[i];
1275 return (int)(a - b);
1280 int _bt_hal_connect_profile(char *address, char *uuid,
1281 void *cb, gpointer func_data)
1285 GDBusConnection *conn;
1286 GDBusProxy *adapter_proxy;
1287 GError *error = NULL;
1289 conn = _bt_hal_get_system_gconn();
1291 return BT_HAL_ERROR_INTERNAL;
1293 object_path = _bt_hal_get_device_object_path(address);
1294 if (object_path == NULL) {
1295 ERR("No searched device");
1297 adapter_proxy = _bt_hal_get_adapter_proxy();
1298 if (adapter_proxy == NULL)
1299 return BT_HAL_ERROR_INTERNAL;
1301 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1302 g_variant_new("(s)", address),
1303 G_DBUS_CALL_FLAGS_NONE,
1308 if (error != NULL) {
1309 ERR("CreateDevice Fail: %s", error->message);
1310 g_error_free(error);
1313 object_path = _bt_hal_get_device_object_path(address);
1315 if (object_path == NULL)
1316 return BT_HAL_ERROR_INTERNAL;
1318 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1319 NULL, BT_HAL_BLUEZ_NAME,
1320 object_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1321 g_free(object_path);
1323 return BT_HAL_ERROR_INTERNAL;
1326 g_dbus_proxy_call(proxy, "ConnectProfile",
1327 g_variant_new("(s)", uuid),
1328 G_DBUS_CALL_FLAGS_NONE,
1329 BT_HAL_MAX_DBUS_TIMEOUT,
1331 (GAsyncReadyCallback)cb,
1334 return BT_HAL_ERROR_NONE;
1337 int _bt_hal_disconnect_profile(char *address, char *uuid,
1338 void *cb, gpointer func_data)
1342 GDBusConnection *conn;
1344 conn = _bt_hal_get_system_gconn();
1346 return BT_HAL_ERROR_INTERNAL;
1348 object_path = _bt_hal_get_device_object_path(address);
1349 if (object_path == NULL)
1350 return BT_HAL_ERROR_INTERNAL;
1352 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1353 NULL, BT_HAL_BLUEZ_NAME,
1354 object_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1355 g_free(object_path);
1357 return BT_HAL_ERROR_INTERNAL;
1359 g_dbus_proxy_call(proxy, "DisconnectProfile",
1360 g_variant_new("(s)", uuid),
1361 G_DBUS_CALL_FLAGS_NONE,
1362 BT_HAL_MAX_DBUS_TIMEOUT,
1364 (GAsyncReadyCallback)cb,
1367 return BT_HAL_ERROR_NONE;
1370 int _bt_hal_register_profile(bt_hal_register_profile_info_t *info, gboolean use_default_rfcomm)
1372 GVariantBuilder *option_builder;
1376 int result = BT_STATUS_SUCCESS;
1378 proxy = _bt_hal_get_profile_proxy();
1379 if (proxy == NULL) {
1380 ERR("Getting profile proxy failed");
1381 return BT_STATUS_FAIL;
1384 option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1385 if (info->authentication)
1386 g_variant_builder_add(option_builder, "{sv}",
1387 "RequireAuthentication",
1388 g_variant_new_boolean(TRUE));
1389 if (info->authorization)
1390 g_variant_builder_add(option_builder, "{sv}",
1391 "RequireAuthorization",
1392 g_variant_new_boolean(TRUE));
1394 g_variant_builder_add(option_builder, "{sv}",
1396 g_variant_new_string(info->role));
1399 * Setting RFCOMM channel to default value 0; would allow bluez to assign
1400 * RFCOMM channels based on the availability when two services want to use
1401 * the RFCOMM along with SPP. Hence bluez makes sure that no two services
1402 * use the same SPP RFCOMM channel.
1404 if (use_default_rfcomm)
1405 g_variant_builder_add(option_builder, "{sv}",
1407 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1409 g_variant_builder_add(option_builder, "{sv}",
1411 g_variant_new_string(info->service));
1413 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
1414 g_variant_new("(osa{sv})", info->obj_path,
1417 G_DBUS_CALL_FLAGS_NONE, -1,
1420 ERR("RegisterProfile failed: %s", err->message);
1422 if (g_strrstr(err->message, BT_HAL_ACCESS_DENIED_MSG))
1423 result = BT_STATUS_AUTH_REJECTED;
1425 result = BT_STATUS_FAIL;
1427 g_clear_error(&err);
1430 g_variant_builder_unref(option_builder);
1433 g_variant_unref(ret);
1438 void _bt_hal_unregister_profile(char *path)
1444 proxy = _bt_hal_get_profile_proxy();
1445 if (proxy == NULL) {
1446 ERR("Getting profile proxy failed");
1450 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
1451 g_variant_new("(o)", path),
1452 G_DBUS_CALL_FLAGS_NONE, -1,
1455 ERR("UnregisterProfile failed : %s", err->message);
1456 g_clear_error(&err);
1460 g_variant_unref(ret);
1465 static void __hal_new_connection_method(GDBusConnection *connection,
1466 const gchar *sender,
1467 const gchar *object_path,
1468 const gchar *interface_name,
1469 const gchar *method_name,
1470 GVariant *parameters,
1471 GDBusMethodInvocation *invocation,
1474 DBG("method %s", method_name);
1475 if (g_strcmp0(method_name, "NewConnection") == 0) {
1478 GUnixFDList *fd_list;
1480 GVariantBuilder *properties;
1482 bt_bdaddr_t remote_addr1;
1483 char addr[BT_HAL_ADDRESS_STRING_SIZE];
1484 bt_hal_new_connection_cb cb = user_data;
1486 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
1489 msg = g_dbus_method_invocation_get_message(invocation);
1490 fd_list = g_dbus_message_get_unix_fd_list(msg);
1491 if (fd_list == NULL) {
1492 GQuark quark = g_quark_from_string("rfcomm-app");
1493 GError *err = g_error_new(quark, 0, "No fd in message");
1494 g_dbus_method_invocation_return_gerror(invocation, err);
1500 fd = g_unix_fd_list_get(fd_list, index, NULL);
1502 ERR("Invalid fd return");
1503 GQuark quark = g_quark_from_string("rfcomm-app");
1504 GError *err = g_error_new(quark, 0, "Invalid FD return");
1505 g_dbus_method_invocation_return_gerror(invocation, err);
1509 INFO("Object Path %s", obj_path);
1511 _bt_hal_convert_device_path_to_address(obj_path, addr);
1512 _bt_hal_convert_addr_string_to_type(remote_addr1.address, (const char *)addr);
1513 INFO("fd: %d, address %s", fd, addr);
1515 g_dbus_method_invocation_return_value(invocation, NULL);
1518 cb(object_path, fd, &remote_addr1);
1521 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
1522 g_dbus_method_invocation_return_value(invocation, NULL);
1526 static GDBusNodeInfo *_bt_hal_get_gdbus_node(const gchar *xml_data)
1529 char *name = g_strdup_printf("org.bt.frwk.p%d", getpid());
1531 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1533 G_BUS_NAME_OWNER_FLAGS_NONE,
1539 DBG("Got bus id %d", bus_id);
1543 return g_dbus_node_info_new_for_xml(xml_data, NULL);
1546 static const GDBusInterfaceVTable method_table = {
1547 __hal_new_connection_method,
1552 int _bt_hal_register_new_gdbus_object(const char *path, bt_hal_new_connection_cb cb)
1554 GDBusConnection *gconn;
1556 GError *error = NULL;
1558 gconn = _bt_hal_get_system_gconn();
1562 if (new_conn_node == NULL)
1563 new_conn_node = _bt_hal_get_gdbus_node(rfcomm_agent_xml);
1565 if (new_conn_node == NULL)
1568 id = g_dbus_connection_register_object(gconn, path,
1569 new_conn_node->interfaces[0],
1573 ERR("Failed to register: %s", error->message);
1574 g_error_free(error);
1578 DBG("NEW CONNECTION ID %d", id);
1583 void _bt_hal_unregister_gdbus_object(int object_id)
1585 GDBusConnection *gconn;
1587 gconn = _bt_hal_get_system_gconn();
1591 g_dbus_connection_unregister_object(gconn, object_id);
1594 int _bt_hal_discover_services(char *address, char *uuid, void *cb, gpointer func_data)
1598 GDBusProxy *adapter_proxy;
1600 GDBusConnection *conn;
1602 conn = _bt_hal_get_system_gconn();
1604 ERR("conn == NULL, return");
1605 return BT_STATUS_FAIL;
1608 object_path = _bt_hal_get_device_object_path(address);
1609 if (object_path == NULL) {
1610 GVariant *ret = NULL;
1612 INFO("No searched device");
1613 adapter_proxy = _bt_hal_get_adapter_proxy();
1614 if (adapter_proxy == NULL) {
1615 ERR("adapter_proxy == NULL, return");
1616 return BT_STATUS_FAIL;
1619 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1620 g_variant_new("(s)", address),
1621 G_DBUS_CALL_FLAGS_NONE,
1622 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1625 ERR("CreateDevice Failed: %s", err->message);
1626 g_clear_error(&err);
1630 g_variant_unref(ret);
1632 g_object_unref(adapter_proxy);
1633 object_path = _bt_hal_get_device_object_path(address);
1634 if (object_path == NULL) {
1635 ERR("object_path == NULL, return");
1636 return BT_STATUS_FAIL;
1640 proxy = g_dbus_proxy_new_sync(conn,
1641 G_DBUS_PROXY_FLAGS_NONE, NULL,
1642 BT_HAL_BLUEZ_NAME, object_path,
1643 BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1644 g_free(object_path);
1645 if (proxy == NULL) {
1646 ERR("Error while getting proxy");
1647 return BT_STATUS_FAIL;
1650 g_dbus_proxy_call(proxy, "DiscoverServices",
1651 g_variant_new("(s)", uuid),
1652 G_DBUS_CALL_FLAGS_NONE,
1653 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1654 (GAsyncReadyCallback)cb,
1657 return BT_STATUS_SUCCESS;
1660 int _bt_hal_cancel_discovers(char *address)
1664 GDBusProxy *adapter_proxy;
1666 GDBusConnection *conn;
1668 conn = _bt_hal_get_system_gconn();
1670 return BT_STATUS_FAIL;
1672 object_path = _bt_hal_get_device_object_path(address);
1673 if (object_path == NULL) {
1674 GVariant *ret = NULL;
1675 INFO("No searched device");
1676 adapter_proxy = _bt_hal_get_adapter_proxy();
1677 if (adapter_proxy == NULL) {
1678 ERR("adapter_proxy == NULL, return");
1679 return BT_STATUS_FAIL;
1682 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1683 g_variant_new("(s)", address),
1684 G_DBUS_CALL_FLAGS_NONE,
1685 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1688 ERR("CreateDevice Failed: %s", err->message);
1689 g_clear_error(&err);
1693 g_variant_unref(ret);
1695 g_object_unref(adapter_proxy);
1697 object_path = _bt_hal_get_device_object_path(address);
1698 if (object_path == NULL)
1699 return BT_STATUS_FAIL;
1702 proxy = g_dbus_proxy_new_sync(conn,
1703 G_DBUS_PROXY_FLAGS_NONE, NULL,
1704 BT_HAL_BLUEZ_NAME, object_path,
1705 BT_HAL_DEVICE_INTERFACE, NULL, NULL);
1706 g_free(object_path);
1707 g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1709 G_DBUS_CALL_FLAGS_NONE,
1710 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1713 ERR("DBus Error message: [%s]", err->message);
1714 g_clear_error(&err);
1715 return BT_STATUS_FAIL;
1719 g_object_unref(proxy);
1721 return BT_STATUS_SUCCESS;
1724 int _bt_hal_discover_service_uuids(char *address, char *remote_uuid)
1728 GDBusConnection *gconn;
1730 char **uuid_value = NULL;
1733 GVariant *value = NULL;
1734 GVariant *ret = NULL;
1735 int result = BT_STATUS_FAIL;
1737 if (remote_uuid == NULL) {
1738 ERR("remote_uuid == NULL, return");
1739 return BT_STATUS_FAIL;
1742 gconn = _bt_hal_get_system_gconn();
1743 if (gconn == NULL) {
1744 ERR("gconn == NULL, return");
1745 return BT_STATUS_FAIL;
1748 object_path = _bt_hal_get_device_object_path(address);
1749 if (object_path == NULL) {
1750 ERR("object_path == NULL, return");
1751 return BT_STATUS_FAIL;
1754 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1755 BT_HAL_BLUEZ_NAME, object_path, BT_HAL_PROPERTIES_INTERFACE, NULL,
1757 if (proxy == NULL) {
1758 g_free(object_path);
1759 ERR("proxy == NULL, return");
1760 return BT_STATUS_FAIL;
1764 ERR("DBus Error: [%s]", err->message);
1765 g_clear_error(&err);
1768 ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1769 g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
1770 G_DBUS_CALL_FLAGS_NONE,
1771 BT_HAL_MAX_DBUS_TIMEOUT, NULL,
1774 result = BT_STATUS_FAIL;
1775 ERR("DBus Error : %s", err->message);
1776 g_clear_error(&err);
1781 ERR("g_dbus_proxy_call_sync function return NULL");
1782 result = BT_STATUS_FAIL;
1786 g_variant_get(ret, "(@a{sv})", &value);
1787 g_variant_unref(ret);
1789 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1790 G_VARIANT_TYPE_STRING_ARRAY);
1793 size = g_variant_get_size(temp_value);
1796 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1797 DBG("Size items %zu", size);
1801 g_variant_unref(temp_value);
1804 for (i = 0; uuid_value[i] != NULL; i++) {
1805 DBG("Remote uuids %s, searched uuid: %s",
1806 uuid_value[i], remote_uuid);
1807 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
1808 result = BT_STATUS_SUCCESS;
1817 g_object_unref(proxy);
1819 g_variant_unref(value);
1826 int bt_hal_gatt_convert_prop2string(
1827 bt_hal_gatt_characteristic_property_t properties,
1828 char *char_properties[])
1832 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_BROADCAST) {
1833 char_properties[flag_count] = g_strdup("broadcast");
1836 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_READ) {
1837 char_properties[flag_count] = g_strdup("read");
1840 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE) {
1841 char_properties[flag_count] = g_strdup("write-without-response");
1844 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE) {
1845 char_properties[flag_count] = g_strdup("write");
1848 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY) {
1849 char_properties[flag_count] = g_strdup("notify");
1852 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE) {
1853 char_properties[flag_count] = g_strdup("indicate");
1856 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE) {
1857 char_properties[flag_count] = g_strdup("authenticated-signed-writes");
1860 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE) {
1861 char_properties[flag_count] = g_strdup("reliable-write");
1864 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES) {
1865 char_properties[flag_count] = g_strdup("writable-auxiliaries");
1868 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ) {
1869 char_properties[flag_count] = g_strdup("encrypt-read");
1872 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE) {
1873 char_properties[flag_count] = g_strdup("encrypt-write");
1876 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ) {
1877 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1880 if (properties & BT_HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE) {
1881 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1885 if (flag_count == 0) {
1886 char_properties[flag_count] = g_strdup("read");
1893 int bt_hal_gatt_convert_perm2string(
1894 bt_hal_gatt_permission_t properties,
1895 char *char_properties[])
1899 if (properties & BT_HAL_GATT_PERMISSION_READ) {
1900 char_properties[flag_count] = g_strdup("read");
1903 if (properties & BT_HAL_GATT_PERMISSION_WRITE) {
1904 char_properties[flag_count] = g_strdup("write");
1907 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_READ) {
1908 char_properties[flag_count] = g_strdup("encrypt-read");
1911 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_WRITE) {
1912 char_properties[flag_count] = g_strdup("encrypt-write");
1915 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_READ) {
1916 char_properties[flag_count] = g_strdup("encrypt-authenticated-read");
1919 if (properties & BT_HAL_GATT_PERMISSION_ENCRYPT_AUTHENTICATED_WRITE) {
1920 char_properties[flag_count] = g_strdup("encrypt-authenticated-write");
1924 if (flag_count == 0) {
1925 char_properties[flag_count] = g_strdup("read");
1932 gboolean _bt_hal_is_service_enabled(const char *uuid)
1935 GError *error = NULL;
1938 GVariantIter *iter = NULL;
1940 gboolean ret = FALSE;
1942 proxy = _bt_hal_get_adapter_properties_proxy();
1944 DBG("_bt_hal_dbus_get_local_name: Adapter Properties proxy get failed!!!");
1948 result = g_dbus_proxy_call_sync(proxy,
1949 "Get", g_variant_new("(ss)",
1950 BT_HAL_ADAPTER_INTERFACE, "UUIDs"),
1951 G_DBUS_CALL_FLAGS_NONE, -1,
1954 if (error != NULL) {
1955 ERR("Failed to get UUIDs (Error: %s)", error->message);
1956 g_clear_error(&error);
1958 ERR("Failed to get UUIDs");
1962 g_variant_get(result, "(v)", &temp);
1963 g_variant_get(temp, "as", &iter);
1965 ERR("Failed to get UUIDs");
1966 g_variant_unref(result);
1970 while (g_variant_iter_loop(iter, "s", &uuid_str)) {
1971 DBG("UUID string [%s]\n", uuid_str);
1972 if (!strncasecmp(uuid, uuid_str, strlen(uuid))) {
1978 g_variant_iter_free(iter);
1979 g_variant_unref(result);
1980 g_variant_unref(temp);