2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <glib/gprintf.h>
25 #include "bt-common.h"
26 #include "bt-event-handler.h"
27 #include "bt-gatt-client.h"
28 #include "bt-internal-types.h"
29 #include "bt-request-sender.h"
31 #define GATT_DEFAULT_TIMEOUT (6 * 1000) // Dependent on supervision timeout 6 sec
39 } char_descriptor_type_t;
41 static GSList *service_monitor_list = NULL;
43 BT_EXPORT_API int bluetooth_gatt_free_service_property(bt_gatt_service_property_t *svc_pty)
47 BT_CHECK_PARAMETER(svc_pty, return);
49 g_free(svc_pty->uuid);
50 g_free(svc_pty->handle);
51 g_strfreev(svc_pty->include_handles.handle);
52 g_strfreev(svc_pty->char_handle.handle);
54 memset(svc_pty, 0, sizeof(bt_gatt_service_property_t));
57 return BLUETOOTH_ERROR_NONE;
60 BT_EXPORT_API int bluetooth_gatt_free_char_property(bt_gatt_char_property_t *char_pty)
64 BT_CHECK_PARAMETER(char_pty, return);
66 g_free(char_pty->uuid);
67 g_free(char_pty->name);
68 g_free(char_pty->description);
69 g_free(char_pty->val);
70 g_free(char_pty->handle);
71 g_strfreev(char_pty->char_desc_handle.handle);
73 memset(char_pty, 0, sizeof(bt_gatt_char_property_t));
76 return BLUETOOTH_ERROR_NONE;
79 BT_EXPORT_API int bluetooth_gatt_free_desc_property(bt_gatt_char_descriptor_property_t *desc_pty)
83 BT_CHECK_PARAMETER(desc_pty, return);
85 g_free(desc_pty->uuid);
86 g_free(desc_pty->val);
87 g_free(desc_pty->handle);
89 memset(desc_pty, 0, sizeof(bt_gatt_char_descriptor_property_t));
92 return BLUETOOTH_ERROR_NONE;
95 static char **__get_string_array_from_gptr_array(GPtrArray *gp)
97 gchar *gp_path = NULL;
104 path = g_malloc0((gp->len + 1) * sizeof(char *));
106 /* Fix : NULL_RETURNS */
110 for (i = 0; i < gp->len; i++) {
111 gp_path = g_ptr_array_index(gp, i);
112 path[i] = g_strdup(gp_path);
113 BT_DBG("path[%d] : [%s]", i, path[i]);
119 gboolean _bluetooth_gatt_check_service_change_watcher_address(
120 const bluetooth_device_address_t *device_addr)
123 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
124 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
126 _bt_convert_addr_type_to_string(device_address,
127 (unsigned char *)device_addr->addr);
129 for (l = service_monitor_list; l != NULL; l = l->next) {
130 char device_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
131 char secure_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
132 bluetooth_device_address_t *addr = l->data;
134 _bt_convert_addr_type_to_string(device_address2,
135 (unsigned char *)addr->addr);
136 _bt_convert_addr_string_to_secure_string(secure_address,
138 _bt_convert_addr_string_to_secure_string(secure_address2,
140 BT_INFO("service_monitor_list [%s] - Input [%s]",
141 secure_address2, secure_address);
143 if (!memcmp(device_addr, addr,
144 sizeof(bluetooth_device_address_t)))
151 BT_EXPORT_API int bluetooth_gatt_set_service_change_watcher(
152 const bluetooth_device_address_t *address, gboolean enable)
155 bluetooth_device_address_t *addr = NULL;
156 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
157 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
159 _bt_convert_addr_type_to_string(device_address,
160 (unsigned char *)address->addr);
161 _bt_convert_addr_string_to_secure_string(secure_address,
163 BT_INFO("Set watcher for %s with %d", secure_address, enable);
165 if (enable == TRUE) {
166 if (service_monitor_list == NULL) {
167 _bt_register_manager_subscribe_signal(TRUE);
170 if (_bluetooth_gatt_check_service_change_watcher_address(address)
172 BT_INFO("The watcher is already set");
173 return BLUETOOTH_ERROR_NONE;
175 addr = g_malloc0(sizeof(bluetooth_device_address_t));
176 memcpy(addr, address, sizeof(bluetooth_device_address_t));
177 service_monitor_list =
178 g_slist_append(service_monitor_list, addr);
180 if (service_monitor_list == NULL) {
181 BT_ERR("There is NO watcher");
182 return BLUETOOTH_ERROR_NONE;
185 for (l = service_monitor_list; l != NULL; l = l->next) {
187 if (!memcmp(address, addr,
188 sizeof(bluetooth_device_address_t))) {
189 service_monitor_list =
190 g_slist_remove(service_monitor_list, addr);
196 if (service_monitor_list == NULL) {
197 _bt_register_manager_subscribe_signal(FALSE);
201 return BLUETOOTH_ERROR_NONE;
204 BT_EXPORT_API int bluetooth_gatt_get_service_property(const char *service_handle,
205 bt_gatt_service_property_t *service)
207 GDBusProxy *properties_proxy = NULL;
208 GError *error = NULL;
209 GVariant *result = NULL;
210 GDBusConnection *g_conn;
212 char *char_handle = NULL;
213 GPtrArray *gp_array = NULL ;
214 GVariantIter *property_iter, *char_iter = NULL;
218 BT_CHECK_PARAMETER(service_handle, return);
219 BT_CHECK_PARAMETER(service, return);
220 BT_CHECK_ENABLED(return);
222 g_conn = _bt_gdbus_get_system_gconn();
223 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
225 properties_proxy = g_dbus_proxy_new_sync(g_conn,
226 G_DBUS_PROXY_FLAGS_NONE, NULL,
229 BT_PROPERTIES_INTERFACE,
232 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
234 result = g_dbus_proxy_call_sync(properties_proxy,
236 g_variant_new("(s)", GATT_SERV_INTERFACE),
237 G_DBUS_CALL_FLAGS_NONE,
244 BT_ERR("Fail to get properties (Error: %s)", error->message);
245 g_clear_error(&error);
247 BT_ERR("Fail to get properties");
248 g_object_unref(properties_proxy);
249 return BLUETOOTH_ERROR_INTERNAL;
252 g_variant_get(result, "(a{sv})", &property_iter);
254 memset(service, 0, sizeof(bt_gatt_service_property_t));
256 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
257 if (!g_strcmp0(key,"UUID")) {
259 service->uuid = g_variant_dup_string(value,&len);
260 _bt_get_uuid_specification_name(service->uuid, &name);
261 BT_INFO("======> Service : %s [%s]", service->uuid, name);
263 } else if(!g_strcmp0(key, "Primary")) {
264 service->primary = g_variant_get_boolean(value);
266 } else if (!g_strcmp0(key, "Includes")) {
267 g_variant_get(value, "ao", &char_iter);
268 gp_array = g_ptr_array_new();
269 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
270 g_ptr_array_add(gp_array, (gpointer)char_handle);
272 if (gp_array->len != 0) {
273 service->include_handles.count = gp_array->len;
274 service->include_handles.handle =
275 __get_string_array_from_gptr_array(gp_array);
277 g_ptr_array_free(gp_array, TRUE);
279 g_variant_iter_free(char_iter);
280 } else if (!g_strcmp0(key, "Characteristics")) {
281 g_variant_get(value, "ao", &char_iter);
282 gp_array = g_ptr_array_new();
283 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
284 g_ptr_array_add(gp_array, (gpointer)char_handle);
286 if (gp_array->len != 0) {
287 service->char_handle.count = gp_array->len;
288 service->char_handle.handle =
289 __get_string_array_from_gptr_array(gp_array);
291 BT_DBG("Characteristics count : %d",service->char_handle.count);
292 g_ptr_array_free(gp_array, TRUE);
294 g_variant_iter_free(char_iter);
298 service->handle = g_strdup(service_handle);
300 g_variant_iter_free(property_iter);
301 g_variant_unref(result);
302 g_object_unref(properties_proxy);
304 return BLUETOOTH_ERROR_NONE;
307 BT_EXPORT_API int bluetooth_gatt_get_primary_services(
308 const bluetooth_device_address_t *address,
309 bt_gatt_handle_info_t *prim_svc)
311 GVariant *result = NULL;
313 GVariantIter *svc_iter;
314 GVariantIter *interface_iter;
315 char *object_path = NULL;
316 char *interface_str = NULL;
317 const gchar *key = NULL;
318 GVariant *value = NULL;
319 GPtrArray *gp_array = NULL;
320 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
321 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
322 int ret = BLUETOOTH_ERROR_INTERNAL;
325 BT_CHECK_PARAMETER(address, return);
326 BT_CHECK_PARAMETER(prim_svc, return);
327 BT_CHECK_ENABLED(return);
329 result = _bt_get_managed_objects();
333 _bt_convert_addr_type_to_string(device_address,
334 (unsigned char *)address->addr);
336 gp_array = g_ptr_array_new();
337 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
339 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
341 if (object_path == NULL)
344 _bt_convert_device_path_to_address(object_path, temp_address);
346 if (g_strcmp0(temp_address, device_address) != 0)
349 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
350 &interface_str, &svc_iter)) {
351 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
354 BT_DBG("Object Path: %s", object_path);
355 while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
356 if (g_strcmp0(key, "Primary") == 0) {
357 if (g_variant_get_boolean(value))
358 g_ptr_array_add(gp_array, (gpointer)object_path);
364 if (gp_array->len == 0) {
365 BT_ERR("gp_array is NULL");
366 ret = BLUETOOTH_ERROR_NOT_FOUND;
368 ret = BLUETOOTH_ERROR_NONE;
369 prim_svc->count = gp_array->len;
370 prim_svc->handle = __get_string_array_from_gptr_array(gp_array);
373 g_ptr_array_free(gp_array, TRUE);
374 g_variant_iter_free(iter);
375 g_variant_unref(result);
380 BT_EXPORT_API int bluetooth_gatt_get_service_from_uuid(bluetooth_device_address_t *address,
381 const char *service_uuid,
382 bt_gatt_service_property_t *service)
384 GVariant *result = NULL;
386 GVariantIter *svc_iter;
387 GVariantIter *interface_iter;
388 char *object_path = NULL;
389 char *interface_str = NULL;
390 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
391 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
392 int ret = BLUETOOTH_ERROR_INTERNAL;
394 BT_CHECK_PARAMETER(address, return);
395 BT_CHECK_PARAMETER(service_uuid, return);
396 BT_CHECK_PARAMETER(service, return);
397 BT_CHECK_ENABLED(return);
399 result = _bt_get_managed_objects();
403 _bt_convert_addr_type_to_string(device_address,
404 (unsigned char *)address->addr);
406 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
408 while (g_variant_iter_loop(iter, "{oa{sa{sv}}}", &object_path,
410 if (object_path == NULL)
413 _bt_convert_device_path_to_address(object_path,
416 if (g_strcmp0(temp_address, device_address) != 0)
419 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
420 &interface_str, &svc_iter)) {
421 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
424 BT_DBG("Object Path: %s", object_path);
425 ret = bluetooth_gatt_get_service_property(object_path,
428 if (ret != BLUETOOTH_ERROR_NONE) {
429 BT_ERR("Get service property failed(0x%08x)", ret);
431 if (service->primary == TRUE &&
432 g_strstr_len(service->uuid, -1,
434 ret = BLUETOOTH_ERROR_NONE;
438 bluetooth_gatt_free_service_property(service);
443 g_variant_iter_free(iter);
444 g_variant_unref(result);
449 static void __bluetooth_internal_get_char_cb(GDBusProxy *proxy,
450 GAsyncResult *res, gpointer user_data)
453 GVariant *char_value;
454 GVariantIter *char_iter;
455 GPtrArray *gp_array = NULL;
456 bt_gatt_discovered_char_t svc_char = { 0, };
458 GError *error = NULL;
459 bt_user_info_t *user_info;
463 user_info = _bt_get_user_data(BT_COMMON);
464 svc_char.service_handle = user_data;
466 value = g_dbus_proxy_call_finish(proxy, res, &error);
470 BT_ERR("Get service characteristics failed\n errCode[%x],"
471 "message[%s]\n", error->code, error->message);
472 g_clear_error(&error);
474 BT_ERR("Get service characteristics failed\n");
477 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
478 BLUETOOTH_ERROR_INTERNAL, NULL,
479 user_info->cb, user_info->user_data);
481 g_free(svc_char.service_handle);
482 g_object_unref(proxy);
486 g_variant_get(value, "(v)", &char_value);
487 g_variant_get(char_value, "ao", &char_iter);
489 gp_array = g_ptr_array_new();
490 while (g_variant_iter_loop(char_iter, "&o", &char_handle));
491 g_ptr_array_add(gp_array, (gpointer)char_handle);
493 if (gp_array->len != 0) {
494 svc_char.handle_info.count = gp_array->len;
495 svc_char.handle_info.handle =
496 __get_string_array_from_gptr_array(gp_array);
498 g_ptr_array_free(gp_array, TRUE);
501 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
502 BLUETOOTH_ERROR_NONE, &svc_char,
503 user_info->cb, user_info->user_data);
506 g_strfreev(svc_char.handle_info.handle);
507 g_free(svc_char.service_handle);
508 g_variant_iter_free(char_iter);
509 g_object_unref(proxy);
512 BT_EXPORT_API int bluetooth_gatt_discover_service_characteristics(
513 const char *service_handle)
515 GDBusProxy *properties_proxy = NULL;
516 GDBusConnection *g_conn;
517 GError *error = NULL;
522 BT_CHECK_PARAMETER(service_handle, return);
523 BT_CHECK_ENABLED(return);
525 g_conn = _bt_gdbus_get_system_gconn();
526 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
528 properties_proxy = g_dbus_proxy_new_sync(g_conn,
529 G_DBUS_PROXY_FLAGS_NONE, NULL,
532 BT_PROPERTIES_INTERFACE,
535 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
537 handle = g_strdup(service_handle);
538 g_dbus_proxy_call(properties_proxy,
540 g_variant_new("(ss)",
541 GATT_SERV_INTERFACE, "Characteristics"),
542 G_DBUS_CALL_FLAGS_NONE,
545 (GAsyncReadyCallback)__bluetooth_internal_get_char_cb,
549 return BLUETOOTH_ERROR_NONE;
553 static int __get_permission_flag(char *permission)
557 retv_if(permission == NULL, ret);
559 BT_INFO("permission = %s",permission);
561 if (!g_strcmp0(permission, "broadcast")) {
562 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST;
563 } else if (!g_strcmp0(permission, "read")) {
564 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
565 } else if (!g_strcmp0(permission, "write-without-response")) {
566 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
567 } else if (!g_strcmp0(permission, "write")) {
568 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
569 } else if (!g_strcmp0(permission, "notify")) {
570 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
571 } else if (!g_strcmp0(permission, "indicate")) {
572 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
573 } else if (!g_strcmp0(permission, "authenticated-signed-writes")) {
574 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE;
575 } else if (!g_strcmp0(permission, "reliable-write")) {
576 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE;
577 } else if (!g_strcmp0(permission, "writable-auxiliaries")) {
578 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES;
579 } else if (!g_strcmp0(permission, "encrypt-read")) {
580 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
581 } else if (!g_strcmp0(permission, "encrypt-write")) {
582 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
583 } else if (!g_strcmp0(permission, "encrypt-authenticated-read")) {
584 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
585 } else if (!g_strcmp0(permission, "encrypt-authenticated-write")) {
586 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
592 BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
593 const char *char_handle, bt_gatt_char_property_t *characteristic)
595 GDBusProxy *properties_proxy = NULL;
596 GError *error = NULL;
597 GVariant *value = NULL;
598 GVariant *result = NULL;
599 GByteArray *gb_array = NULL;
600 GPtrArray *gp_array = NULL ;
601 GDBusConnection *g_conn;
605 char *char_desc_handle = NULL;
607 GVariantIter *property_iter;
608 GVariantIter *char_value_iter;
609 GVariantIter *char_perm_iter;
610 GVariantIter *char_desc_iter;
613 BT_CHECK_PARAMETER(char_handle, return);
614 BT_CHECK_PARAMETER(characteristic, return);
616 BT_CHECK_ENABLED(return);
618 g_conn = _bt_gdbus_get_system_gconn();
619 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
621 properties_proxy = g_dbus_proxy_new_sync(g_conn,
622 G_DBUS_PROXY_FLAGS_NONE, NULL,
625 BT_PROPERTIES_INTERFACE,
628 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
630 result = g_dbus_proxy_call_sync(properties_proxy,
632 g_variant_new("(s)", GATT_CHAR_INTERFACE),
633 G_DBUS_CALL_FLAGS_NONE,
640 BT_ERR("Fail to get properties (Error: %s)", error->message);
641 g_clear_error(&error);
643 BT_ERR("Fail to get properties");
644 g_object_unref(properties_proxy);
645 return BLUETOOTH_ERROR_INTERNAL;
648 g_variant_get(result, "(a{sv})", &property_iter);
650 memset(characteristic, 0, sizeof(bt_gatt_char_property_t));
651 characteristic->handle = g_strdup(char_handle);
653 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
654 if (!g_strcmp0(key,"UUID")) {
656 characteristic->uuid = g_variant_dup_string(value,&len);
657 _bt_get_uuid_specification_name(characteristic->uuid, &name);
658 BT_INFO("Characteristic : %s [%s]", characteristic->uuid, name);
660 } else if(!g_strcmp0(key, "Value")) {
661 gb_array = g_byte_array_new();
662 g_variant_get(value, "ay", &char_value_iter);
663 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
664 // BT_DBG("value of char = %d",char_value);
665 g_byte_array_append(gb_array, &char_value, 1);
667 g_variant_iter_free(char_value_iter);
669 if (gb_array->len != 0) {
670 characteristic->val = g_malloc0(gb_array->len *
671 sizeof(unsigned char));
672 memcpy(characteristic->val, gb_array->data, gb_array->len);
674 characteristic->val_len = gb_array->len;
675 g_byte_array_free(gb_array, TRUE);
676 } else if (!g_strcmp0(key, "Flags")) {
677 g_variant_get(value, "as", &char_perm_iter);
678 characteristic->permission = 0x00;
679 while (g_variant_iter_loop(char_perm_iter, "s", &permission)) {
680 BT_DBG("permission = %s", permission);
681 characteristic->permission |= __get_permission_flag(permission);
682 BT_DBG("permission check = %d", characteristic->permission);
684 g_variant_iter_free(char_perm_iter);
685 } else if (!g_strcmp0(key, "Descriptors")) {
686 g_variant_get(value, "ao", &char_desc_iter);
687 gp_array = g_ptr_array_new();
688 while (g_variant_iter_loop(char_desc_iter, "&o", &char_desc_handle)) {
689 g_ptr_array_add(gp_array, (gpointer)char_desc_handle);
691 g_variant_iter_free(char_desc_iter);
692 if (gp_array->len != 0) {
693 characteristic->char_desc_handle.count = gp_array->len;
694 characteristic->char_desc_handle.handle =
695 __get_string_array_from_gptr_array(gp_array);
697 g_ptr_array_free(gp_array, TRUE);
701 g_variant_iter_free(property_iter);
702 g_variant_unref(result);
703 g_object_unref(properties_proxy);
706 return BLUETOOTH_ERROR_NONE;
709 void bluetooth_gatt_get_char_from_uuid_cb(GDBusProxy *proxy,
710 GAsyncResult *res, gpointer user_data)
713 GVariantIter *char_iter;
715 GError *error = NULL;
716 bt_user_info_t *user_info;
717 int ret = BLUETOOTH_ERROR_INTERNAL;
718 bt_gatt_char_property_t characteristic;
720 user_info = _bt_get_user_data(BT_COMMON);
722 value = g_dbus_proxy_call_finish(proxy, res, &error);
726 BT_ERR("Get service characteristics failed\n errCode[%x],"
727 "message[%s]\n", error->code, error->message);
728 g_clear_error(&error);
730 BT_ERR("Get service characteristics failed\n");
733 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID,
734 BLUETOOTH_ERROR_INTERNAL, NULL,
735 user_info->cb, user_info->user_data);
737 g_object_unref(proxy);
742 g_variant_get(value, "(ao)", &char_iter);
744 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
747 ret = bluetooth_gatt_get_characteristics_property(char_handle,
750 if (ret != BLUETOOTH_ERROR_NONE) {
751 BT_ERR("Get characteristic property failed(0x%08x)", ret);
753 if (g_strstr_len(characteristic.uuid, -1, user_data)) {
754 ret = BLUETOOTH_ERROR_NONE;
758 bluetooth_gatt_free_char_property(&characteristic);
762 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID, ret,
763 &characteristic, user_info->cb, user_info->user_data);
766 bluetooth_gatt_free_char_property(&characteristic);
767 g_variant_iter_free(char_iter);
771 BT_EXPORT_API int bluetooth_gatt_get_char_from_uuid(const char *service_handle,
772 const char *char_uuid)
774 GDBusProxy *properties_proxy = NULL;
775 GDBusConnection *g_conn;
776 GError *error = NULL;
779 BT_CHECK_PARAMETER(service_handle, return);
780 BT_CHECK_PARAMETER(char_uuid, return);
781 BT_CHECK_ENABLED(return);
783 g_conn = _bt_gdbus_get_system_gconn();
784 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
786 properties_proxy = g_dbus_proxy_new_sync(g_conn,
787 G_DBUS_PROXY_FLAGS_NONE, NULL,
790 BT_PROPERTIES_INTERFACE,
793 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
795 uuid = g_strdup(char_uuid);
796 g_dbus_proxy_call(properties_proxy,
798 g_variant_new("(ss)",
799 GATT_SERV_INTERFACE, "Characteristics"),
800 G_DBUS_CALL_FLAGS_NONE,
803 (GAsyncReadyCallback)bluetooth_gatt_get_char_from_uuid_cb,
806 return BLUETOOTH_ERROR_NONE;
809 BT_EXPORT_API int bluetooth_gatt_get_char_descriptor_property(
810 const char *descriptor_handle, bt_gatt_char_descriptor_property_t *descriptor)
812 GDBusProxy *properties_proxy = NULL;
813 GError *error = NULL;
814 GDBusConnection *g_conn;
815 GVariant *result = NULL;
816 GVariantIter *property_iter;
820 GVariant *value = NULL;
821 GByteArray *gb_array = NULL;
822 GVariantIter *desc_value_iter;
825 BT_CHECK_PARAMETER(descriptor_handle, return);
826 BT_CHECK_PARAMETER(descriptor, return);
828 BT_CHECK_ENABLED(return);
830 g_conn = _bt_gdbus_get_system_gconn();
831 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
833 properties_proxy = g_dbus_proxy_new_sync(g_conn,
834 G_DBUS_PROXY_FLAGS_NONE, NULL,
837 BT_PROPERTIES_INTERFACE,
840 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
842 result = g_dbus_proxy_call_sync(properties_proxy,
844 g_variant_new("(s)", GATT_DESC_INTERFACE),
845 G_DBUS_CALL_FLAGS_NONE,
852 BT_ERR("Fail to get properties (Error: %s)", error->message);
853 g_clear_error(&error);
855 BT_ERR("Fail to get properties");
856 g_object_unref(properties_proxy);
857 return BLUETOOTH_ERROR_INTERNAL;
860 g_variant_get(result, "(a{sv})", &property_iter);
862 memset(descriptor, 0, sizeof(bt_gatt_char_descriptor_property_t));
863 descriptor->handle = g_strdup(descriptor_handle);
865 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
866 if (!g_strcmp0(key,"UUID")) {
868 descriptor->uuid = g_variant_dup_string(value,&len);
869 _bt_get_uuid_specification_name(descriptor->uuid, &name);
870 BT_INFO("Descriptor : %s [%s]",descriptor->uuid, name);
872 } else if (!g_strcmp0(key, "Value")) {
873 gb_array = g_byte_array_new();
874 g_variant_get(value, "ay", &desc_value_iter);
875 while(g_variant_iter_loop(desc_value_iter, "y", &char_value)) {
876 BT_DBG("value of descriptor = %d",char_value);
877 g_byte_array_append(gb_array, &char_value, 1);
879 g_variant_iter_free(desc_value_iter);
881 if (gb_array->len != 0) {
882 descriptor->val = g_malloc0(gb_array->len *
883 sizeof(unsigned char));
884 memcpy(descriptor->val, gb_array->data, gb_array->len);
886 descriptor->val_len = gb_array->len;
887 g_byte_array_free(gb_array, TRUE);
891 g_variant_iter_free(property_iter);
892 g_variant_unref(result);
893 g_object_unref(properties_proxy);
896 return BLUETOOTH_ERROR_NONE;
899 static void __bluetooth_internal_read_cb(GObject *source_object,
903 GError *error = NULL;
904 bt_user_info_t *user_info;
905 bt_gatt_char_value_t char_value = { 0, };
906 GDBusConnection *system_gconn = NULL;
908 GByteArray *gp_byte_array = NULL;
911 int ret = BLUETOOTH_ERROR_NONE;
914 user_info = _bt_get_user_data(BT_COMMON);
916 system_gconn = _bt_gdbus_get_system_gconn();
917 value = g_dbus_connection_call_finish(system_gconn, res, &error);
920 BT_ERR("Error : %s \n", error->message);
921 if (g_strrstr(error->message, "Not paired"))
922 ret = BLUETOOTH_ERROR_NOT_PAIRED;
924 ret = BLUETOOTH_ERROR_INTERNAL;
926 g_clear_error(&error);
928 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
930 user_info->cb, user_info->user_data);
936 char_value.char_handle = user_data;
937 gp_byte_array = g_byte_array_new();
938 g_variant_get(value, "(ay)", &iter);
940 while (g_variant_iter_loop(iter, "y", &g_byte)) {
941 g_byte_array_append(gp_byte_array, &g_byte, 1);
944 if (gp_byte_array->len != 0) {
945 char_value.val_len = gp_byte_array->len;
946 char_value.char_value = gp_byte_array->data;
950 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
951 BLUETOOTH_ERROR_NONE, &char_value,
952 user_info->cb, user_info->user_data);
955 g_free(char_value.char_handle);
956 g_byte_array_free(gp_byte_array, TRUE);
957 g_variant_unref(value);
958 g_variant_iter_free(iter);
963 BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *characteristic)
965 GDBusConnection *conn;
968 BT_CHECK_PARAMETER(characteristic, return);
969 BT_CHECK_ENABLED(return);
971 conn = _bt_gdbus_get_system_gconn();
972 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
974 handle = g_strdup(characteristic);
976 g_dbus_connection_call(conn,
982 G_VARIANT_TYPE("(ay)"),
983 G_DBUS_CALL_FLAGS_NONE,
986 (GAsyncReadyCallback)__bluetooth_internal_read_cb,
989 return BLUETOOTH_ERROR_NONE;
992 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
993 const char *char_handle, const guint8 *value, int length)
996 GVariantBuilder *builder;
997 GError *error = NULL;
998 GDBusConnection *conn;
1002 BT_CHECK_PARAMETER(char_handle, return);
1003 BT_CHECK_PARAMETER(value, return);
1004 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1005 BT_CHECK_ENABLED(return);
1007 conn = _bt_gdbus_get_system_gconn();
1008 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1010 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1012 for (i = 0; i < length; i++) {
1013 g_variant_builder_add(builder, "y", value[i]);
1016 val = g_variant_new("(ay)", builder);
1018 g_dbus_connection_call_sync(conn,
1021 GATT_CHAR_INTERFACE,
1025 G_DBUS_CALL_FLAGS_NONE,
1029 BT_ERR("Set value Failed: %s", error->message);
1030 g_clear_error(&error);
1031 g_variant_builder_unref(builder);
1032 return BLUETOOTH_ERROR_INTERNAL;
1035 g_variant_builder_unref(builder);
1038 return BLUETOOTH_ERROR_NONE;
1041 static void __bluetooth_internal_write_cb(GObject *source_object,
1046 GError *error = NULL;
1047 bt_user_info_t *user_info;
1048 GDBusConnection *system_gconn = NULL;
1050 int result = BLUETOOTH_ERROR_NONE;
1051 guint8 att_ecode = 0;
1053 user_info = _bt_get_user_data(BT_COMMON);
1055 system_gconn = _bt_gdbus_get_system_gconn();
1056 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1059 BT_ERR("Error : %s \n", error->message);
1060 g_clear_error(&error);
1061 result = BLUETOOTH_ERROR_INTERNAL;
1063 g_variant_get(value, "(y)", &att_ecode);
1066 BT_ERR("ATT Error code: %d \n", att_ecode);
1071 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_CHAR,
1073 user_info->cb, user_info->user_data);
1075 BT_ERR("user info is null");
1079 g_variant_unref(value);
1084 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
1085 const char *char_handle, const guint8 *value, int length, guint8 write_type)
1088 GVariantBuilder *builder;
1089 GDBusConnection *conn;
1091 int ret = BLUETOOTH_ERROR_NONE;
1093 BT_CHECK_PARAMETER(char_handle, return);
1094 BT_CHECK_PARAMETER(value, return);
1095 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1096 BT_CHECK_ENABLED_INTERNAL(return);
1098 conn = _bt_gdbus_get_system_gconn();
1099 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1101 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1103 for (i = 0; i < length; i++) {
1104 g_variant_builder_add(builder, "y", value[i]);
1107 val = g_variant_new("ay", builder);
1108 g_dbus_connection_call(conn,
1111 GATT_CHAR_INTERFACE,
1113 g_variant_new("(y@ay)", write_type, val),
1114 G_VARIANT_TYPE("(y)"),
1115 G_DBUS_CALL_FLAGS_NONE,
1117 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1120 g_variant_builder_unref(builder);
1124 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
1125 const char *char_handle, const guint8 *value, int length)
1128 GDBusConnection *conn;
1129 GVariantBuilder *builder;
1133 BT_CHECK_PARAMETER(char_handle, return);
1134 BT_CHECK_PARAMETER(value, return);
1135 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1136 BT_CHECK_ENABLED(return);
1138 conn = _bt_gdbus_get_system_gconn();
1139 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1141 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1143 for (i = 0; i < length; i++) {
1144 g_variant_builder_add(builder, "y", value[i]);
1145 BT_DBG("value [] = %d", value[i]);
1148 val = g_variant_new("(ay)", builder);
1150 g_dbus_connection_call(conn,
1153 GATT_CHAR_INTERFACE,
1157 G_DBUS_CALL_FLAGS_NONE,
1159 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1162 g_variant_builder_unref(builder);
1165 return BLUETOOTH_ERROR_NONE;
1168 static int __bluetooth_gatt_descriptor_iter(const char *char_handle,
1169 bt_gatt_char_property_t *characteristic)
1172 GDBusProxy *properties_proxy = NULL;
1173 GError *error = NULL;
1174 GVariant *value = NULL;
1175 GVariant *result = NULL;
1176 GDBusConnection *g_conn;
1177 int i, ret = BLUETOOTH_ERROR_NONE;
1178 const char *uuid = NULL;
1180 GVariantIter *desc_value_iter, *property_iter;
1182 char_descriptor_type_t desc_type = TYPE_NONE;
1184 g_conn = _bt_gdbus_get_system_gconn();
1185 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1187 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1188 G_DBUS_PROXY_FLAGS_NONE, NULL,
1191 BT_PROPERTIES_INTERFACE,
1194 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1196 result = g_dbus_proxy_call_sync(properties_proxy,
1198 g_variant_new("(s)", GATT_DESC_INTERFACE),
1199 G_DBUS_CALL_FLAGS_NONE,
1205 if (error != NULL) {
1206 BT_ERR("Fail to get properties (Error: %s)", error->message);
1207 g_clear_error(&error);
1209 BT_ERR("Fail to get properties");
1210 g_object_unref(properties_proxy);
1211 return BLUETOOTH_ERROR_INTERNAL;
1213 g_variant_get(result, "(a{sv})", &property_iter);
1214 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1215 if (!g_strcmp0(key, "UUID")) {
1216 uuid = g_variant_get_string(value, &len);
1217 if (g_strcmp0(uuid, GATT_USER_DESC_UUID) == 0) {
1218 BT_DBG("GATT_USER_DESC_UUID");
1219 desc_type = USER_DESC;
1220 } else if (g_strcmp0(uuid, GATT_CHAR_FORMAT) == 0) {
1221 BT_DBG("GATT_CHAR_FORMAT");
1222 desc_type = CHAR_FORMAT;
1223 } else if (g_strcmp0(uuid, GATT_CHAR_CLIENT_CONF) == 0) {
1224 BT_DBG("GATT_CHAR_CLIENT_CONF");
1225 desc_type = CLIENT_CONF;
1226 } else if (g_strcmp0(uuid, GATT_CHAR_SERVER_CONF) == 0) {
1227 BT_DBG("GATT_CHAR_SERVER_CONF");
1228 desc_type = SERVER_CONF;
1230 BT_DBG("descriptor uuid = %s", uuid);
1232 } else if (!g_strcmp0(key, "Value")) {
1233 switch (desc_type) {
1235 BT_DBG("Format descriptor");
1236 g_variant_get(value, "(yyqyq)",
1237 &(characteristic->format.format),
1238 &(characteristic->format.exponent),
1239 &(characteristic->format.unit),
1240 &(characteristic->format.name_space),
1241 &(characteristic->format.description));
1244 BT_DBG("User descriptor");
1245 g_variant_get(value, "ay", &desc_value_iter);
1246 len = g_variant_get_size((GVariant *)desc_value_iter);
1249 characteristic->description = (char *)g_malloc0(len + 1);
1250 if (!characteristic->description) {
1251 ret = BLUETOOTH_ERROR_OUT_OF_MEMORY;
1255 for (i = 0; i < len; i++) {
1256 g_variant_iter_loop(desc_value_iter, "y",
1257 &characteristic->description[i]);
1258 BT_DBG("description = %s", characteristic->description);
1260 g_variant_iter_free(desc_value_iter);
1263 BT_DBG(" CLIENT_CONF");
1266 BT_DBG(" SERVER_CONF");
1275 g_variant_iter_free(property_iter);
1276 g_variant_unref(result);
1277 g_object_unref(properties_proxy);
1284 static void bluetooth_gatt_get_char_desc_cb(GDBusProxy *proxy,
1285 GAsyncResult *res, gpointer user_data)
1289 GVariant *char_value;
1290 GVariantIter *char_iter;
1292 GError *error = NULL;
1293 bt_user_info_t *user_info;
1294 bt_gatt_char_property_t characteristic = {0, };
1295 int ret = BLUETOOTH_ERROR_INTERNAL;
1297 user_info = _bt_get_user_data(BT_COMMON);
1299 value = g_dbus_proxy_call_finish(proxy, res, &error);
1301 if (value == NULL) {
1302 if (error != NULL) {
1303 BT_ERR("Get characteristic descriptor failed\n errCode[%x],"
1304 "message[%s]\n", error->code, error->message);
1305 g_clear_error(&error);
1307 BT_ERR("Get characteristic descriptor failed\n");
1310 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1311 BLUETOOTH_ERROR_INTERNAL, NULL,
1312 user_info->cb, user_info->user_data);
1315 g_object_unref(proxy);
1319 g_variant_get(value, "(v)", &char_value);
1320 g_variant_get(char_value, "ao", &char_iter);
1322 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
1323 BT_DBG("object path of descriptor = %s", char_handle);
1325 ret = __bluetooth_gatt_descriptor_iter(char_handle,
1327 BT_DBG("Descriptor read status [%d]", ret);
1331 characteristic.handle = user_data;
1333 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1334 ret, &characteristic, user_info->cb, user_info->user_data);
1336 bluetooth_gatt_free_char_property(&characteristic);
1338 g_variant_iter_free(char_iter);
1342 BT_EXPORT_API int bluetooth_gatt_discover_characteristic_descriptor(
1343 const char *characteristic_handle)
1345 GDBusProxy *properties_proxy = NULL;
1346 GDBusConnection *g_conn;
1348 GError *error = NULL;
1350 BT_CHECK_PARAMETER(characteristic_handle, return);
1351 BT_CHECK_ENABLED(return);
1353 g_conn = _bt_gdbus_get_system_gconn();
1354 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1356 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1357 G_DBUS_PROXY_FLAGS_NONE, NULL,
1359 characteristic_handle,
1360 BT_PROPERTIES_INTERFACE,
1363 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1365 handle = g_strdup(characteristic_handle);
1366 g_dbus_proxy_call(properties_proxy,
1368 g_variant_new("(ss)",
1369 GATT_CHAR_INTERFACE, "Descriptors"),
1370 G_DBUS_CALL_FLAGS_NONE,
1372 (GAsyncReadyCallback)bluetooth_gatt_get_char_desc_cb,
1375 return BLUETOOTH_ERROR_NONE;
1378 static void __bluetooth_internal_read_desc_cb(GObject *source_object,
1382 GError *error = NULL;
1383 bt_user_info_t *user_info;
1384 bt_gatt_char_property_t char_value = { 0, };
1385 GDBusConnection *system_gconn = NULL;
1387 GByteArray *gp_byte_array = NULL;
1392 user_info = _bt_get_user_data(BT_COMMON);
1393 system_gconn = _bt_gdbus_get_system_gconn();
1395 char_value.handle = user_data;
1396 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1399 BT_ERR("Error : %s \n", error->message);
1400 g_clear_error(&error);
1402 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1403 BLUETOOTH_ERROR_INTERNAL, NULL,
1404 user_info->cb, user_info->user_data);
1406 g_free(char_value.handle);
1410 gp_byte_array = g_byte_array_new();
1411 g_variant_get(value, "(ay)", &iter);
1413 while (g_variant_iter_loop(iter, "y", &g_byte))
1414 g_byte_array_append(gp_byte_array, &g_byte, 1);
1416 if (gp_byte_array->len != 0) {
1417 char_value.val_len = (unsigned int)gp_byte_array->len;
1418 char_value.description = (char *)gp_byte_array->data;
1422 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1423 BLUETOOTH_ERROR_NONE, &char_value,
1424 user_info->cb, user_info->user_data);
1427 g_byte_array_free(gp_byte_array, TRUE);
1428 g_free(char_value.handle);
1429 g_variant_unref(value);
1430 g_variant_iter_free(iter);
1435 BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *char_descriptor)
1437 GDBusConnection *conn;
1441 BT_CHECK_PARAMETER(char_descriptor, return);
1442 BT_CHECK_ENABLED(return);
1444 conn = _bt_gdbus_get_system_gconn();
1445 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1447 handle = g_strdup(char_descriptor);
1449 g_dbus_connection_call(conn,
1452 GATT_DESC_INTERFACE,
1455 G_VARIANT_TYPE("(ay)"),
1456 G_DBUS_CALL_FLAGS_NONE,
1459 (GAsyncReadyCallback)__bluetooth_internal_read_desc_cb,
1463 return BLUETOOTH_ERROR_NONE;
1466 static void __bluetooth_internal_write_desc_cb(GObject *source_object,
1470 GError *error = NULL;
1471 bt_user_info_t *user_info;
1472 GDBusConnection *system_gconn = NULL;
1474 int result = BLUETOOTH_ERROR_NONE;
1475 guint8 att_ecode = 0;
1478 user_info = _bt_get_user_data(BT_COMMON);
1480 system_gconn = _bt_gdbus_get_system_gconn();
1481 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1484 BT_ERR("Error : %s \n", error->message);
1485 g_clear_error(&error);
1486 result = BLUETOOTH_ERROR_INTERNAL;
1488 g_variant_get(value, "(y)", &att_ecode);
1491 BT_ERR("ATT Error code: %d \n", att_ecode);
1496 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_DESC,
1498 user_info->cb, user_info->user_data);
1502 g_variant_unref(value);
1507 BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(
1508 const char *desc_handle, const guint8 *value, int length)
1511 GDBusConnection *conn;
1512 GVariantBuilder *builder;
1516 BT_CHECK_PARAMETER(desc_handle, return);
1517 BT_CHECK_PARAMETER(value, return);
1518 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1519 BT_CHECK_ENABLED(return);
1521 conn = _bt_gdbus_get_system_gconn();
1522 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1524 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1526 for (i = 0; i < length; i++) {
1527 g_variant_builder_add(builder, "y", value[i]);
1530 val = g_variant_new("(ay)", builder);
1532 g_dbus_connection_call(conn,
1535 GATT_DESC_INTERFACE,
1538 G_VARIANT_TYPE("(y)"),
1539 G_DBUS_CALL_FLAGS_NONE,
1541 (GAsyncReadyCallback)__bluetooth_internal_write_desc_cb,
1544 g_variant_builder_unref(builder);
1547 return BLUETOOTH_ERROR_NONE;
1550 #ifndef GATT_NO_RELAY
1551 static int __bluetooth_gatt_watch_characteristics(void)
1553 int result = BLUETOOTH_ERROR_NONE;
1556 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1558 result = _bt_send_request(BT_BLUEZ_SERVICE,
1559 BT_GATT_WATCH_CHARACTERISTIC,
1560 in_param1, in_param2, in_param3, in_param4, &out_param);
1562 if (result != BLUETOOTH_ERROR_NONE)
1563 BT_ERR("Watch Characteristic request failed !");
1565 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1571 BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle)
1573 GDBusConnection *conn;
1574 GError *error = NULL;
1575 int ret = BLUETOOTH_ERROR_NONE;
1577 BT_CHECK_PARAMETER(char_handle, return);
1578 BT_CHECK_ENABLED(return);
1580 BT_INFO_C("### Enable CCCD : %s", char_handle);
1582 conn = _bt_gdbus_get_system_gconn();
1583 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1585 g_dbus_connection_call_sync(conn,
1588 GATT_CHAR_INTERFACE,
1592 G_DBUS_CALL_FLAGS_NONE,
1593 GATT_DEFAULT_TIMEOUT, NULL, &error);
1596 g_dbus_error_strip_remote_error(error);
1597 BT_ERR_C("### Watch Failed: %s", error->message);
1598 if (g_strrstr(error->message, "Already notifying"))
1599 ret = BLUETOOTH_ERROR_NONE;
1600 else if (g_strrstr(error->message, "In Progress"))
1601 ret = BLUETOOTH_ERROR_IN_PROGRESS;
1602 else if (g_strrstr(error->message, "Operation is not supported"))
1603 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
1604 /*failed because of either Insufficient Authorization or Write Not Permitted */
1605 else if (g_strrstr(error->message, "Write not permitted") ||
1606 g_strrstr(error->message, "Operation Not Authorized"))
1607 ret = BLUETOOTH_ERROR_PERMISSION_DEINED;
1608 /* failed because of either Insufficient Authentication,
1609 Insufficient Encryption Key Size, or Insufficient Encryption. */
1610 else if (g_strrstr(error->message, "Not paired"))
1611 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1613 ret = BLUETOOTH_ERROR_INTERNAL;
1615 g_clear_error(&error);
1617 #ifndef GATT_NO_RELAY
1619 /* Register the client sender to bt-service */
1620 ret = __bluetooth_gatt_watch_characteristics();
1627 #ifndef GATT_NO_RELAY
1628 static int __bluetooth_gatt_unwatch_characteristics(void)
1630 int result = BLUETOOTH_ERROR_NONE;
1633 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1635 result = _bt_send_request(BT_BLUEZ_SERVICE,
1636 BT_GATT_UNWATCH_CHARACTERISTIC,
1637 in_param1, in_param2, in_param3, in_param4, &out_param);
1639 if (result != BLUETOOTH_ERROR_NONE)
1640 BT_ERR("Unwatch Characteristic request failed !");
1642 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1648 BT_EXPORT_API int bluetooth_gatt_unwatch_characteristics(const char *char_handle)
1651 GDBusConnection *conn;
1652 GError *error = NULL;
1653 int ret = BLUETOOTH_ERROR_NONE;
1655 BT_CHECK_PARAMETER(char_handle, return);
1657 BT_CHECK_ENABLED(return);
1659 BT_INFO("Disable CCCD : %s", char_handle);
1661 conn = _bt_gdbus_get_system_gconn();
1662 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1664 g_dbus_connection_call_sync(conn,
1667 GATT_CHAR_INTERFACE,
1671 G_DBUS_CALL_FLAGS_NONE,
1672 GATT_DEFAULT_TIMEOUT, NULL, &error);
1675 BT_ERR("Watch Failed: %s", error->message);
1676 g_clear_error(&error);
1677 ret = BLUETOOTH_ERROR_INTERNAL;
1679 #ifndef GATT_NO_RELAY
1681 /* Unregister the client sender to bt-service */
1682 ret = __bluetooth_gatt_unwatch_characteristics();