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>
24 #include <gio/gunixfdlist.h>
26 #include "bt-common.h"
27 #include "bt-event-handler.h"
28 #include "bt-gatt-client.h"
29 #include "bt-internal-types.h"
30 #include "bt-request-sender.h"
32 #include "bluetooth-gatt-client-api.h"
33 #include <arpa/inet.h>
34 static GSList *gatt_characteristic_notify_list;
35 static GSList *gatt_characteristic_write_list = NULL;;
37 #define GATT_DEFAULT_TIMEOUT (6 * 1000) // Dependent on supervision timeout 6 sec
45 } char_descriptor_type_t;
47 static GSList *service_monitor_list = NULL;
49 BT_EXPORT_API int bluetooth_gatt_free_service_property(bt_gatt_service_property_t *svc_pty)
51 BT_CHECK_PARAMETER(svc_pty, return);
53 g_free(svc_pty->uuid);
55 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)
62 BT_CHECK_PARAMETER(char_pty, return);
64 g_free(char_pty->uuid);
65 g_free(char_pty->name);
66 g_free(char_pty->description);
67 g_free(char_pty->val);
69 memset(char_pty, 0, sizeof(bt_gatt_char_property_t));
71 return BLUETOOTH_ERROR_NONE;
74 BT_EXPORT_API int bluetooth_gatt_free_desc_property(bt_gatt_char_descriptor_property_t *desc_pty)
76 BT_CHECK_PARAMETER(desc_pty, return);
78 g_free(desc_pty->uuid);
79 g_free(desc_pty->val);
81 memset(desc_pty, 0, sizeof(bt_gatt_char_descriptor_property_t));
83 return BLUETOOTH_ERROR_NONE;
86 static char **__get_string_array_from_gptr_array(GPtrArray *gp)
88 gchar *gp_path = NULL;
95 path = g_malloc0((gp->len + 1) * sizeof(char *));
97 for (i = 0; i < gp->len; i++) {
98 gp_path = g_ptr_array_index(gp, i);
99 path[i] = g_strdup(gp_path);
100 // BT_DBG("path[%d] : [%s]", i, path[i]);
106 gboolean _bluetooth_gatt_check_service_change_watcher_address(
107 const bluetooth_device_address_t *device_addr)
110 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
112 _bt_convert_addr_type_to_string(device_address,
113 (unsigned char *)device_addr->addr);
115 for (l = service_monitor_list; l != NULL; l = l->next) {
116 char device_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
117 bluetooth_device_address_t *addr = l->data;
119 _bt_convert_addr_type_to_string(device_address2,
120 (unsigned char *)addr->addr);
122 if (!memcmp(device_addr, addr,
123 sizeof(bluetooth_device_address_t)))
130 BT_EXPORT_API int bluetooth_gatt_set_service_change_watcher(
131 const bluetooth_device_address_t *address, gboolean enable)
134 bluetooth_device_address_t *addr = NULL;
135 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
137 _bt_convert_addr_type_to_secure_string(secure_address, (unsigned char *)address->addr);
138 BT_INFO("Set watcher for %s with %d", secure_address, enable);
140 if (enable == TRUE) {
141 if (_bluetooth_gatt_check_service_change_watcher_address(address)
143 BT_INFO("The watcher is already set");
144 return BLUETOOTH_ERROR_NONE;
147 if (service_monitor_list == NULL)
148 _bt_register_manager_subscribe_signal(TRUE);
150 addr = g_malloc0(sizeof(bluetooth_device_address_t));
151 memcpy(addr, address, sizeof(bluetooth_device_address_t));
153 service_monitor_list =
154 g_slist_append(service_monitor_list, addr);
156 for (l = service_monitor_list; l != NULL; l = l->next) {
159 if (!memcmp(address, addr,
160 sizeof(bluetooth_device_address_t))) {
161 service_monitor_list =
162 g_slist_remove(service_monitor_list, addr);
168 if (service_monitor_list == NULL)
169 _bt_register_manager_subscribe_signal(FALSE);
172 return BLUETOOTH_ERROR_NONE;
175 BT_EXPORT_API int bluetooth_gatt_get_service_property(const char *service_handle,
176 bt_gatt_service_property_t *service)
178 GDBusProxy *properties_proxy = NULL;
179 GError *error = NULL;
180 GVariant *result = NULL;
181 GDBusConnection *g_conn;
183 char *char_handle = NULL;
184 GPtrArray *gp_array = NULL ;
185 GVariantIter *property_iter, *char_iter = NULL;
189 BT_CHECK_PARAMETER(service_handle, return);
190 BT_CHECK_PARAMETER(service, return);
191 BT_CHECK_ENABLED(return);
193 g_conn = _bt_get_system_private_conn();
194 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
196 properties_proxy = g_dbus_proxy_new_sync(g_conn,
197 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
200 BT_PROPERTIES_INTERFACE,
203 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
205 result = g_dbus_proxy_call_sync(properties_proxy,
207 g_variant_new("(s)", GATT_SERV_INTERFACE),
208 G_DBUS_CALL_FLAGS_NONE,
215 BT_ERR("Fail to get properties (Error: %s)", error->message);
216 g_clear_error(&error);
218 BT_ERR("Fail to get properties");
219 g_object_unref(properties_proxy);
220 return BLUETOOTH_ERROR_INTERNAL;
223 g_variant_get(result, "(a{sv})", &property_iter);
225 memset(service, 0, sizeof(bt_gatt_service_property_t));
227 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
228 if (!g_strcmp0(key, "UUID")) {
230 service->uuid = g_variant_dup_string(value, &len);
231 bluetooth_get_uuid_name(service->uuid, &name);
232 BT_INFO("%s %s [%s]", service_handle + 37, service->uuid, name);
234 } else if (!g_strcmp0(key, "Primary")) {
235 service->primary = g_variant_get_boolean(value);
237 } else if (!g_strcmp0(key, "Includes")) {
238 g_variant_get(value, "ao", &char_iter);
239 if (char_iter != NULL) {
240 gp_array = g_ptr_array_new();
241 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
242 g_ptr_array_add(gp_array, (gpointer)char_handle);
244 if (gp_array->len != 0) {
245 service->include_handles.count = gp_array->len;
246 service->include_handles.handle =
247 __get_string_array_from_gptr_array(gp_array);
249 g_ptr_array_free(gp_array, TRUE);
250 g_variant_iter_free(char_iter);
252 } else if (!g_strcmp0(key, "Characteristics")) {
253 g_variant_get(value, "ao", &char_iter);
254 if (char_iter != NULL) {
255 gp_array = g_ptr_array_new();
256 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
257 g_ptr_array_add(gp_array, (gpointer)char_handle);
259 if (gp_array->len != 0) {
260 service->char_handle.count = gp_array->len;
261 service->char_handle.handle =
262 __get_string_array_from_gptr_array(gp_array);
264 g_ptr_array_free(gp_array, TRUE);
265 g_variant_iter_free(char_iter);
267 BT_DBG("Characteristics count : %d", service->char_handle.count);
271 service->handle = g_strdup(service_handle);
273 g_variant_iter_free(property_iter);
274 g_variant_unref(result);
275 g_object_unref(properties_proxy);
277 return BLUETOOTH_ERROR_NONE;
280 BT_EXPORT_API int bluetooth_gatt_get_primary_services(
281 const bluetooth_device_address_t *address,
282 bt_gatt_handle_info_t *prim_svc)
284 GVariant *result = NULL;
286 GVariantIter *svc_iter;
287 GVariantIter *interface_iter;
288 char *object_path = NULL;
289 char *interface_str = NULL;
290 const gchar *key = NULL;
291 GVariant *value = NULL;
292 GPtrArray *gp_array = NULL;
293 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
294 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
295 int ret = BLUETOOTH_ERROR_INTERNAL;
299 BT_CHECK_PARAMETER(address, return);
300 BT_CHECK_PARAMETER(prim_svc, return);
301 BT_CHECK_ENABLED(return);
303 result = _bt_get_managed_objects();
307 _bt_convert_addr_type_to_string(device_address,
308 (unsigned char *)address->addr);
310 gp_array = g_ptr_array_new();
311 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
313 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
315 if (object_path == NULL)
318 _bt_convert_device_path_to_address(object_path, temp_address);
320 if (g_strcmp0(temp_address, device_address) != 0)
323 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
324 &interface_str, &svc_iter)) {
325 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
328 BT_DBG("[%d] Object Path : %s", idx++, object_path);
329 while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
330 if (g_strcmp0(key, "Primary") == 0) {
331 if (g_variant_get_boolean(value))
332 g_ptr_array_add(gp_array, (gpointer)object_path);
338 if (gp_array->len == 0) {
339 BT_ERR("gp_array is NULL");
340 ret = BLUETOOTH_ERROR_NOT_FOUND;
342 ret = BLUETOOTH_ERROR_NONE;
343 prim_svc->count = gp_array->len;
344 prim_svc->handle = __get_string_array_from_gptr_array(gp_array);
347 g_ptr_array_free(gp_array, TRUE);
348 g_variant_iter_free(iter);
349 g_variant_unref(result);
354 BT_EXPORT_API int bluetooth_gatt_get_service_from_uuid(bluetooth_device_address_t *address,
355 const char *service_uuid,
356 bt_gatt_service_property_t *service)
358 GVariant *result = NULL;
360 GVariantIter *svc_iter;
361 GVariantIter *interface_iter;
362 char *object_path = NULL;
363 char *interface_str = NULL;
364 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
365 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
366 int ret = BLUETOOTH_ERROR_INTERNAL;
368 BT_CHECK_PARAMETER(address, return);
369 BT_CHECK_PARAMETER(service_uuid, return);
370 BT_CHECK_PARAMETER(service, return);
371 BT_CHECK_ENABLED(return);
373 result = _bt_get_managed_objects();
377 _bt_convert_addr_type_to_string(device_address,
378 (unsigned char *)address->addr);
380 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
382 while (g_variant_iter_loop(iter, "{oa{sa{sv}}}", &object_path,
384 if (object_path == NULL)
387 _bt_convert_device_path_to_address(object_path,
390 if (g_strcmp0(temp_address, device_address) != 0)
393 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
394 &interface_str, &svc_iter)) {
395 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
398 BT_DBG("Object Path: %s", object_path);
399 ret = bluetooth_gatt_get_service_property(object_path,
402 if (ret != BLUETOOTH_ERROR_NONE) {
403 BT_ERR("Get service property failed(0x%08x)", ret);
405 if (service->primary == TRUE &&
406 g_strstr_len(service->uuid, -1,
408 ret = BLUETOOTH_ERROR_NONE;
410 /* release resources */
412 g_variant_iter_free(interface_iter);
413 g_free(interface_str);
414 g_variant_iter_free(svc_iter);
419 bluetooth_gatt_free_service_property(service);
424 g_variant_iter_free(iter);
425 g_variant_unref(result);
430 static void __bluetooth_internal_get_char_cb(GDBusProxy *proxy,
431 GAsyncResult *res, gpointer user_data)
434 GVariant *char_value;
435 GVariantIter *char_iter;
436 GPtrArray *gp_array = NULL;
437 bt_gatt_discovered_char_t svc_char = { 0, };
439 GError *error = NULL;
440 bt_user_info_t *user_info;
444 user_info = _bt_get_user_data(BT_COMMON);
445 svc_char.service_handle = user_data;
447 value = g_dbus_proxy_call_finish(proxy, res, &error);
451 BT_ERR("Get service characteristics failed\n errCode[%x],"
452 "message[%s]\n", error->code, error->message);
453 g_clear_error(&error);
455 BT_ERR("Get service characteristics failed\n");
458 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
459 BLUETOOTH_ERROR_INTERNAL, NULL,
460 user_info->cb, user_info->user_data);
462 g_free(svc_char.service_handle);
463 g_object_unref(proxy);
467 g_variant_get(value, "(v)", &char_value);
468 g_variant_get(char_value, "ao", &char_iter);
470 gp_array = g_ptr_array_new();
471 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
472 g_ptr_array_add(gp_array, (gpointer)char_handle);
474 if (gp_array->len != 0) {
475 svc_char.handle_info.count = gp_array->len;
476 svc_char.handle_info.handle =
477 __get_string_array_from_gptr_array(gp_array);
479 g_ptr_array_free(gp_array, TRUE);
482 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
483 BLUETOOTH_ERROR_NONE, &svc_char,
484 user_info->cb, user_info->user_data);
487 g_strfreev(svc_char.handle_info.handle);
488 g_free(svc_char.service_handle);
489 g_variant_iter_free(char_iter);
490 g_variant_unref(value);
491 g_object_unref(proxy);
492 g_variant_unref(char_value);
495 BT_EXPORT_API int bluetooth_gatt_discover_service_characteristics(
496 const char *service_handle)
498 GDBusProxy *properties_proxy = NULL;
499 GDBusConnection *g_conn;
500 GError *error = NULL;
505 BT_CHECK_PARAMETER(service_handle, return);
506 BT_CHECK_ENABLED(return);
508 g_conn = _bt_get_system_private_conn();
509 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
511 properties_proxy = g_dbus_proxy_new_sync(g_conn,
512 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
515 BT_PROPERTIES_INTERFACE,
518 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
520 handle = g_strdup(service_handle);
521 g_dbus_proxy_call(properties_proxy,
523 g_variant_new("(ss)",
524 GATT_SERV_INTERFACE, "Characteristics"),
525 G_DBUS_CALL_FLAGS_NONE,
528 (GAsyncReadyCallback)__bluetooth_internal_get_char_cb,
532 return BLUETOOTH_ERROR_NONE;
536 static int __get_permission_flag(char *permission)
540 retv_if(permission == NULL, ret);
542 if (!g_strcmp0(permission, "broadcast"))
543 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST;
544 else if (!g_strcmp0(permission, "read"))
545 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ;
546 else if (!g_strcmp0(permission, "write-without-response"))
547 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
548 else if (!g_strcmp0(permission, "write"))
549 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE;
550 else if (!g_strcmp0(permission, "notify"))
551 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
552 else if (!g_strcmp0(permission, "indicate"))
553 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
554 else if (!g_strcmp0(permission, "authenticated-signed-writes"))
555 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE;
556 else if (!g_strcmp0(permission, "reliable-write"))
557 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE;
558 else if (!g_strcmp0(permission, "writable-auxiliaries"))
559 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES;
560 else if (!g_strcmp0(permission, "encrypt-read"))
561 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
562 else if (!g_strcmp0(permission, "encrypt-write"))
563 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
564 else if (!g_strcmp0(permission, "encrypt-authenticated-read"))
565 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
566 else if (!g_strcmp0(permission, "encrypt-authenticated-write"))
567 ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
572 static void __convert_permission_flag_to_str(unsigned int permission)
574 char perm[200] = { 0, };
576 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST)
577 g_strlcat(perm, "broadcast ", sizeof(perm));
578 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ)
579 g_strlcat(perm, "read ", sizeof(perm));
580 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE)
581 g_strlcat(perm, "write-without-response ", sizeof(perm));
582 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE)
583 g_strlcat(perm, "write ", sizeof(perm));
584 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY)
585 g_strlcat(perm, "notify ", sizeof(perm));
586 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE)
587 g_strlcat(perm, "indicate ", sizeof(perm));
588 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE)
589 g_strlcat(perm, "authenticated-signed-writes ", sizeof(perm));
590 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE)
591 g_strlcat(perm, "reliable-write ", sizeof(perm));
592 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES)
593 g_strlcat(perm, "writable-auxiliaries ", sizeof(perm));
594 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ)
595 g_strlcat(perm, "encrypt-read ", sizeof(perm));
596 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE)
597 g_strlcat(perm, "encrypt-write ", sizeof(perm));
598 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ)
599 g_strlcat(perm, "encrypt-authenticated-read ", sizeof(perm));
600 if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE)
601 g_strlcat(perm, "encrypt-authenticated-write ", sizeof(perm));
603 BT_INFO("permission [0x%04x] : %s\n", permission, perm);
607 BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
608 const char *char_handle, bt_gatt_char_property_t *characteristic)
610 GDBusProxy *properties_proxy = NULL;
611 GError *error = NULL;
612 GVariant *value = NULL;
613 GVariant *result = NULL;
614 GByteArray *gb_array = NULL;
615 GPtrArray *gp_array = NULL ;
616 GDBusConnection *g_conn;
620 char *char_desc_handle = NULL;
622 GVariantIter *property_iter;
623 GVariantIter *char_value_iter;
624 GVariantIter *char_perm_iter;
625 GVariantIter *char_desc_iter;
627 BT_CHECK_PARAMETER(char_handle, return);
628 BT_CHECK_PARAMETER(characteristic, return);
630 BT_CHECK_ENABLED(return);
632 g_conn = _bt_get_system_private_conn();
633 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
635 properties_proxy = g_dbus_proxy_new_sync(g_conn,
636 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
639 BT_PROPERTIES_INTERFACE,
642 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
644 result = g_dbus_proxy_call_sync(properties_proxy,
646 g_variant_new("(s)", GATT_CHAR_INTERFACE),
647 G_DBUS_CALL_FLAGS_NONE,
654 BT_ERR("Fail to get properties (Error: %s)", error->message);
655 g_clear_error(&error);
657 BT_ERR("Fail to get properties");
658 g_object_unref(properties_proxy);
659 return BLUETOOTH_ERROR_INTERNAL;
662 g_variant_get(result, "(a{sv})", &property_iter);
664 memset(characteristic, 0, sizeof(bt_gatt_char_property_t));
665 characteristic->handle = g_strdup(char_handle);
667 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
668 if (!g_strcmp0(key, "UUID")) {
670 characteristic->uuid = g_variant_dup_string(value, &len);
671 bluetooth_get_uuid_name(characteristic->uuid, &name);
672 BT_INFO("%s %s [%s]", char_handle + 37, characteristic->uuid, name);
674 } else if (!g_strcmp0(key, "Value")) {
675 gb_array = g_byte_array_new();
676 g_variant_get(value, "ay", &char_value_iter);
677 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
678 // BT_DBG("value of char = %d",char_value);
679 g_byte_array_append(gb_array, &char_value, 1);
681 g_variant_iter_free(char_value_iter);
683 if (gb_array->len != 0) {
684 characteristic->val = g_malloc0(gb_array->len *
685 sizeof(unsigned char));
686 memcpy(characteristic->val, gb_array->data, gb_array->len);
688 characteristic->val_len = gb_array->len;
689 g_byte_array_free(gb_array, TRUE);
690 } else if (!g_strcmp0(key, "Flags")) {
691 g_variant_get(value, "as", &char_perm_iter);
692 characteristic->permission = 0x00;
694 while (g_variant_iter_loop(char_perm_iter, "s", &permission))
695 characteristic->permission |= __get_permission_flag(permission);
696 __convert_permission_flag_to_str(characteristic->permission);
698 g_variant_iter_free(char_perm_iter);
699 } else if (!g_strcmp0(key, "Descriptors")) {
700 g_variant_get(value, "ao", &char_desc_iter);
701 gp_array = g_ptr_array_new();
702 while (g_variant_iter_loop(char_desc_iter, "&o", &char_desc_handle))
703 g_ptr_array_add(gp_array, (gpointer)char_desc_handle);
705 g_variant_iter_free(char_desc_iter);
706 if (gp_array->len != 0) {
707 characteristic->char_desc_handle.count = gp_array->len;
708 characteristic->char_desc_handle.handle =
709 __get_string_array_from_gptr_array(gp_array);
711 g_ptr_array_free(gp_array, TRUE);
715 g_variant_iter_free(property_iter);
716 g_variant_unref(result);
717 g_object_unref(properties_proxy);
719 return BLUETOOTH_ERROR_NONE;
722 void bluetooth_gatt_get_char_from_uuid_cb(GDBusProxy *proxy,
723 GAsyncResult *res, gpointer user_data)
726 GVariantIter *char_iter;
728 GError *error = NULL;
729 bt_user_info_t *user_info;
730 int ret = BLUETOOTH_ERROR_INTERNAL;
731 bt_gatt_char_property_t characteristic;
733 user_info = _bt_get_user_data(BT_COMMON);
735 value = g_dbus_proxy_call_finish(proxy, res, &error);
739 BT_ERR("Get service characteristics failed\n errCode[%x],"
740 "message[%s]\n", error->code, error->message);
741 g_clear_error(&error);
743 BT_ERR("Get service characteristics failed\n");
746 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID,
747 BLUETOOTH_ERROR_INTERNAL, NULL,
748 user_info->cb, user_info->user_data);
750 g_object_unref(proxy);
755 g_variant_get(value, "(ao)", &char_iter);
757 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
760 ret = bluetooth_gatt_get_characteristics_property(char_handle,
763 if (ret != BLUETOOTH_ERROR_NONE) {
764 BT_ERR("Get characteristic property failed(0x%08x)", ret);
766 if (g_strstr_len(characteristic.uuid, -1, user_data)) {
767 ret = BLUETOOTH_ERROR_NONE;
771 bluetooth_gatt_free_char_property(&characteristic);
775 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID, ret,
776 &characteristic, user_info->cb, user_info->user_data);
779 bluetooth_gatt_free_char_property(&characteristic);
780 g_variant_iter_free(char_iter);
781 g_variant_unref(value);
785 BT_EXPORT_API int bluetooth_gatt_get_char_from_uuid(const char *service_handle,
786 const char *char_uuid)
788 GDBusProxy *properties_proxy = NULL;
789 GDBusConnection *g_conn;
790 GError *error = NULL;
793 BT_CHECK_PARAMETER(service_handle, return);
794 BT_CHECK_PARAMETER(char_uuid, return);
795 BT_CHECK_ENABLED(return);
797 g_conn = _bt_get_system_private_conn();
798 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
800 properties_proxy = g_dbus_proxy_new_sync(g_conn,
801 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
804 BT_PROPERTIES_INTERFACE,
807 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
809 uuid = g_strdup(char_uuid);
810 g_dbus_proxy_call(properties_proxy,
812 g_variant_new("(ss)",
813 GATT_SERV_INTERFACE, "Characteristics"),
814 G_DBUS_CALL_FLAGS_NONE,
817 (GAsyncReadyCallback)bluetooth_gatt_get_char_from_uuid_cb,
820 return BLUETOOTH_ERROR_NONE;
823 BT_EXPORT_API int bluetooth_gatt_get_char_descriptor_property(
824 const char *descriptor_handle, bt_gatt_char_descriptor_property_t *descriptor)
826 GDBusProxy *properties_proxy = NULL;
827 GError *error = NULL;
828 GDBusConnection *g_conn;
829 GVariant *result = NULL;
830 GVariantIter *property_iter;
834 GVariant *value = NULL;
835 GByteArray *gb_array = NULL;
836 GVariantIter *desc_value_iter;
838 BT_CHECK_PARAMETER(descriptor_handle, return);
839 BT_CHECK_PARAMETER(descriptor, return);
841 BT_CHECK_ENABLED(return);
843 g_conn = _bt_get_system_private_conn();
844 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
846 properties_proxy = g_dbus_proxy_new_sync(g_conn,
847 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
850 BT_PROPERTIES_INTERFACE,
853 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
855 result = g_dbus_proxy_call_sync(properties_proxy,
857 g_variant_new("(s)", GATT_DESC_INTERFACE),
858 G_DBUS_CALL_FLAGS_NONE,
865 BT_ERR("Fail to get properties (Error: %s)", error->message);
866 g_clear_error(&error);
868 BT_ERR("Fail to get properties");
869 g_object_unref(properties_proxy);
870 return BLUETOOTH_ERROR_INTERNAL;
873 g_variant_get(result, "(a{sv})", &property_iter);
875 memset(descriptor, 0, sizeof(bt_gatt_char_descriptor_property_t));
876 descriptor->handle = g_strdup(descriptor_handle);
878 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
879 if (!g_strcmp0(key, "UUID")) {
881 descriptor->uuid = g_variant_dup_string(value, &len);
882 bluetooth_get_uuid_name(descriptor->uuid, &name);
883 BT_INFO("Descriptor : %s [%s]", descriptor->uuid, name);
885 } else if (!g_strcmp0(key, "Value")) {
886 gb_array = g_byte_array_new();
887 g_variant_get(value, "ay", &desc_value_iter);
888 while (g_variant_iter_loop(desc_value_iter, "y", &char_value)) {
889 BT_DBG("value of descriptor = %d", char_value);
890 g_byte_array_append(gb_array, &char_value, 1);
892 g_variant_iter_free(desc_value_iter);
894 if (gb_array->len != 0) {
895 descriptor->val = g_malloc0(gb_array->len *
896 sizeof(unsigned char));
897 memcpy(descriptor->val, gb_array->data, gb_array->len);
899 descriptor->val_len = gb_array->len;
900 g_byte_array_free(gb_array, TRUE);
904 g_variant_iter_free(property_iter);
905 g_variant_unref(result);
906 g_object_unref(properties_proxy);
908 return BLUETOOTH_ERROR_NONE;
911 static int __bluetooth_get_att_error_code(GError *error, char *handle)
917 BT_ERR("Error : %s [%s]", error->message, handle + 15);
918 str = g_strrstr(error->message, "ATT error: 0x");
921 att_ecode = g_ascii_xdigit_value(str[len - 2]) << 4;
922 att_ecode += g_ascii_xdigit_value(str[len - 1]);
924 return BLUETOOTH_ATT_ERROR_INTERNAL;
927 case BLUETOOTH_ATT_ERROR_READ_NOT_PERMITTED:
928 BT_ERR("Read not permitted");
930 case BLUETOOTH_ATT_ERROR_WRITE_NOT_PERMITTED:
931 BT_ERR("Write not permitted");
933 case BLUETOOTH_ATT_ERROR_AUTHENTICATION:
934 case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION:
935 case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
936 BT_ERR("Not paired");
938 case BLUETOOTH_ATT_ERROR_INVALID_OFFSET:
939 BT_ERR("Invalid offset");
941 case BLUETOOTH_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN:
942 BT_ERR("Invalid Length");
944 case BLUETOOTH_ATT_ERROR_AUTHORIZATION:
945 BT_ERR("Operation not Authorized");
948 BT_ERR("default ecode");
952 if (att_ecode >= 0x80 && att_ecode <= 0x9F)
953 BT_ERR("Application error");
958 static void __bluetooth_internal_read_cb(GObject *source_object,
959 GAsyncResult *res, gpointer user_data)
961 GError *error = NULL;
962 GDBusConnection *system_gconn = NULL;
964 bt_user_info_t *user_info;
966 GByteArray *gp_byte_array = NULL;
969 bt_gatt_resp_data_t *resp_data = user_data;
973 system_gconn = _bt_get_system_private_conn();
974 value = g_dbus_connection_call_finish(system_gconn, res, &error);
976 user_info = _bt_get_user_data(BT_COMMON);
979 BT_ERR("Error : %s [%s]", error->message, resp_data->handle + 15);
980 g_clear_error(&error);
985 g_variant_unref(value);
990 att_ecode = __bluetooth_get_att_error_code(error, resp_data->handle);
991 g_clear_error(&error);
993 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
994 att_ecode, resp_data,
995 user_info->cb, user_info->user_data);
1000 gp_byte_array = g_byte_array_new();
1001 g_variant_get(value, "(ay)", &iter);
1003 while (g_variant_iter_loop(iter, "y", &g_byte))
1004 g_byte_array_append(gp_byte_array, &g_byte, 1);
1006 if (gp_byte_array->len != 0) {
1007 resp_data->len = gp_byte_array->len;
1008 resp_data->value = gp_byte_array->data;
1011 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
1012 BLUETOOTH_ATT_ERROR_NONE, resp_data,
1013 user_info->cb, user_info->user_data);
1016 g_byte_array_free(gp_byte_array, TRUE);
1017 g_variant_iter_free(iter);
1018 g_variant_unref(value);
1023 BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *chr,
1026 GDBusConnection *conn;
1027 bt_gatt_resp_data_t *resp_data;
1028 GVariantBuilder *builder = NULL;
1031 BT_CHECK_PARAMETER(chr, return);
1032 BT_CHECK_ENABLED(return);
1034 conn = _bt_get_system_private_conn();
1035 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1037 resp_data = g_malloc0(sizeof(bt_gatt_resp_data_t));
1038 resp_data->user_data = user_data;
1039 resp_data->handle = (char *)chr;
1041 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1044 g_variant_builder_add(builder, "{sv}", "offset",
1045 g_variant_new("q", offset));
1047 /* Device Object path*/
1048 // g_variant_builder_add(builder, "{sv}", "device",
1049 // g_variant_new_object("o", NULL));
1051 g_dbus_connection_call(conn, BT_BLUEZ_NAME, chr, GATT_CHAR_INTERFACE,
1052 "ReadValue", g_variant_new("(a{sv})", builder),
1053 G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1054 (GAsyncReadyCallback)__bluetooth_internal_read_cb,
1055 (gpointer)resp_data);
1056 g_variant_builder_unref(builder);
1058 return BLUETOOTH_ERROR_NONE;
1061 static void __bluetooth_internal_write_cb(GObject *source_object,
1062 GAsyncResult *res, gpointer user_data)
1064 GError *error = NULL;
1065 GDBusConnection *system_gconn = NULL;
1066 bt_user_info_t *user_info;
1069 bt_gatt_resp_data_t *resp_data = user_data;
1071 system_gconn = _bt_get_system_private_conn();
1072 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1074 user_info = _bt_get_user_data(BT_COMMON);
1077 BT_ERR("Error : %s [%s]", error->message, resp_data->handle + 15);
1078 g_clear_error(&error);
1083 g_variant_unref(value);
1088 att_ecode = __bluetooth_get_att_error_code(error, resp_data->handle);
1089 g_clear_error(&error);
1091 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_CHAR,
1092 att_ecode, resp_data,
1093 user_info->cb, user_info->user_data);
1098 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_CHAR,
1099 BLUETOOTH_ATT_ERROR_NONE, resp_data,
1100 user_info->cb, user_info->user_data);
1103 g_variant_unref(value);
1107 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
1108 const char *char_handle, const guint8 *value, int length)
1110 GVariant *val, *options;
1111 GVariantBuilder *builder1;
1112 GVariantBuilder *builder2;
1113 GError *error = NULL;
1114 GDBusConnection *conn;
1119 BT_CHECK_PARAMETER(char_handle, return);
1120 BT_CHECK_PARAMETER(value, return);
1121 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1122 BT_CHECK_ENABLED(return);
1124 conn = _bt_get_system_private_conn();
1125 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1127 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1128 for (i = 0; i < length; i++)
1129 g_variant_builder_add(builder1, "y", value[i]);
1131 val = g_variant_new("ay", builder1);
1133 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1135 g_variant_builder_add(builder2, "{sv}", "offset",
1136 g_variant_new_uint16(offset));
1138 /* Device Object path*/
1139 // g_variant_builder_add(builder2, "{sv}", "device",
1140 // g_variant_new_object("o", NULL));
1142 options = g_variant_new("a{sv}", builder2);
1144 g_dbus_connection_call(conn,
1147 GATT_CHAR_INTERFACE,
1149 g_variant_new("(@ay@a{sv})",
1152 G_DBUS_CALL_FLAGS_NONE,
1154 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1159 BT_ERR("Set value Failed: %s", error->message);
1160 g_clear_error(&error);
1161 g_variant_builder_unref(builder1);
1162 return BLUETOOTH_ERROR_INTERNAL;
1164 g_variant_builder_unref(builder1);
1165 g_variant_builder_unref(builder2);
1168 return BLUETOOTH_ERROR_NONE;
1171 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
1172 const char *chr, const guint8 *value, int length,
1173 guint8 write_type, gpointer user_data)
1175 GVariant *val, *options;
1176 GVariantBuilder *builder1;
1177 GVariantBuilder *builder2;
1178 GDBusConnection *conn;
1181 int ret = BLUETOOTH_ERROR_NONE;
1182 bt_gatt_resp_data_t *resp_data;
1184 BT_CHECK_PARAMETER(chr, return);
1185 BT_CHECK_PARAMETER(value, return);
1186 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1187 BT_CHECK_ENABLED_INTERNAL(return);
1189 conn = _bt_get_system_private_conn();
1190 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1192 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1194 for (i = 0; i < length; i++)
1195 g_variant_builder_add(builder1, "y", value[i]);
1197 val = g_variant_new("ay", builder1);
1199 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1201 g_variant_builder_add(builder2, "{sv}", "offset",
1202 g_variant_new_uint16(offset));
1204 /* Device Object path*/
1205 // g_variant_builder_add(builder2, "{sv}", "device",
1206 // g_variant_new_object("o", NULL));
1208 options = g_variant_new("a{sv}", builder2);
1210 resp_data = g_malloc0(sizeof(bt_gatt_resp_data_t));
1211 resp_data->user_data = user_data;
1212 resp_data->handle = (char *)chr;
1214 g_dbus_connection_call(conn, BT_BLUEZ_NAME, chr, GATT_CHAR_INTERFACE,
1216 g_variant_new("(y@ay@a{sv})", write_type, val, options),
1218 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1219 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1220 (gpointer)resp_data);
1222 g_variant_builder_unref(builder1);
1223 g_variant_builder_unref(builder2);
1228 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
1229 const char *char_handle, const guint8 *value, int length)
1231 GVariant *val, *options;
1232 GDBusConnection *conn;
1233 GVariantBuilder *builder1;
1234 GVariantBuilder *builder2;
1239 BT_CHECK_PARAMETER(char_handle, return);
1240 BT_CHECK_PARAMETER(value, return);
1241 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1242 BT_CHECK_ENABLED(return);
1244 conn = _bt_get_system_private_conn();
1245 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1247 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1249 for (i = 0; i < length; i++) {
1250 g_variant_builder_add(builder1, "y", value[i]);
1251 BT_DBG("value [] = %d", value[i]);
1254 val = g_variant_new("ay", builder1);
1256 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1258 g_variant_builder_add(builder2, "{sv}", "offset",
1259 g_variant_new_uint16(offset));
1261 /* Device Object path*/
1262 // g_variant_builder_add(builder2, "{sv}", "device",
1263 // g_variant_new_object("o", NULL));
1265 options = g_variant_new("a{sv}", builder2);
1267 g_dbus_connection_call(conn,
1270 GATT_CHAR_INTERFACE,
1272 g_variant_new("(@ay@a{sv})",
1275 G_DBUS_CALL_FLAGS_NONE,
1277 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1280 g_variant_builder_unref(builder1);
1281 g_variant_builder_unref(builder2);
1284 return BLUETOOTH_ERROR_NONE;
1287 static int __bluetooth_gatt_descriptor_iter(const char *char_handle,
1288 bt_gatt_char_property_t *characteristic)
1291 GDBusProxy *properties_proxy = NULL;
1292 GError *error = NULL;
1293 GVariant *value = NULL;
1294 GVariant *result = NULL;
1295 GDBusConnection *g_conn;
1296 int i, ret = BLUETOOTH_ERROR_NONE;
1297 const char *uuid = NULL;
1299 GVariantIter *desc_value_iter, *property_iter;
1301 char_descriptor_type_t desc_type = TYPE_NONE;
1303 g_conn = _bt_get_system_private_conn();
1304 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1306 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1307 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1310 BT_PROPERTIES_INTERFACE,
1313 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1315 result = g_dbus_proxy_call_sync(properties_proxy,
1317 g_variant_new("(s)", GATT_DESC_INTERFACE),
1318 G_DBUS_CALL_FLAGS_NONE,
1324 if (error != NULL) {
1325 BT_ERR("Fail to get properties (Error: %s)", error->message);
1326 g_clear_error(&error);
1328 BT_ERR("Fail to get properties");
1329 g_object_unref(properties_proxy);
1330 return BLUETOOTH_ERROR_INTERNAL;
1332 g_variant_get(result, "(a{sv})", &property_iter);
1333 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1334 if (!g_strcmp0(key, "UUID")) {
1335 uuid = g_variant_get_string(value, &len);
1336 if (g_strcmp0(uuid, GATT_USER_DESC_UUID) == 0) {
1337 BT_DBG("GATT_USER_DESC_UUID");
1338 desc_type = USER_DESC;
1339 } else if (g_strcmp0(uuid, GATT_CHAR_FORMAT) == 0) {
1340 BT_DBG("GATT_CHAR_FORMAT");
1341 desc_type = CHAR_FORMAT;
1342 } else if (g_strcmp0(uuid, GATT_CHAR_CLIENT_CONF) == 0) {
1343 BT_DBG("GATT_CHAR_CLIENT_CONF");
1344 desc_type = CLIENT_CONF;
1345 } else if (g_strcmp0(uuid, GATT_CHAR_SERVER_CONF) == 0) {
1346 BT_DBG("GATT_CHAR_SERVER_CONF");
1347 desc_type = SERVER_CONF;
1349 BT_DBG("descriptor uuid = %s", uuid);
1351 } else if (!g_strcmp0(key, "Value")) {
1352 switch (desc_type) {
1354 BT_DBG("Format descriptor");
1355 g_variant_get(value, "(yyqyq)",
1356 &(characteristic->format.format),
1357 &(characteristic->format.exponent),
1358 &(characteristic->format.unit),
1359 &(characteristic->format.name_space),
1360 &(characteristic->format.description));
1363 BT_DBG("User descriptor");
1364 g_variant_get(value, "ay", &desc_value_iter);
1365 len = g_variant_get_size((GVariant *)desc_value_iter);
1368 characteristic->description = (char *)g_malloc0(len + 1);
1370 for (i = 0; i < len; i++) {
1371 g_variant_iter_loop(desc_value_iter, "y",
1372 &characteristic->description[i]);
1373 BT_DBG("description = %s", characteristic->description);
1375 g_variant_iter_free(desc_value_iter);
1378 BT_DBG(" CLIENT_CONF");
1381 BT_DBG(" SERVER_CONF");
1389 g_variant_iter_free(property_iter);
1390 g_variant_unref(result);
1391 g_object_unref(properties_proxy);
1398 static void bluetooth_gatt_get_char_desc_cb(GDBusProxy *proxy,
1399 GAsyncResult *res, gpointer user_data)
1403 GVariant *char_value;
1404 GVariantIter *char_iter;
1406 GError *error = NULL;
1407 bt_user_info_t *user_info;
1408 bt_gatt_char_property_t characteristic = {0, };
1409 int ret = BLUETOOTH_ERROR_INTERNAL;
1411 user_info = _bt_get_user_data(BT_COMMON);
1413 value = g_dbus_proxy_call_finish(proxy, res, &error);
1414 characteristic.handle = user_data;
1416 if (value == NULL) {
1417 if (error != NULL) {
1418 BT_ERR("Get characteristic descriptor failed\n errCode[%x],"
1419 "message[%s]\n", error->code, error->message);
1420 g_clear_error(&error);
1422 BT_ERR("Get characteristic descriptor failed\n");
1425 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1426 BLUETOOTH_ERROR_INTERNAL, NULL,
1427 user_info->cb, user_info->user_data);
1429 g_free(characteristic.handle);
1430 g_object_unref(proxy);
1434 g_variant_get(value, "(v)", &char_value);
1435 g_variant_get(char_value, "ao", &char_iter);
1437 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
1438 BT_DBG("object path of descriptor = %s", char_handle);
1440 ret = __bluetooth_gatt_descriptor_iter(char_handle,
1442 BT_DBG("Descriptor read status [%d]", ret);
1447 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1448 ret, &characteristic, user_info->cb, user_info->user_data);
1450 bluetooth_gatt_free_char_property(&characteristic);
1452 g_variant_iter_free(char_iter);
1453 g_variant_unref(value);
1454 g_variant_unref(char_value);
1458 BT_EXPORT_API int bltooth_gatt_discover_characteristic_descriptor(
1459 const char *characteristic_handle)
1461 GDBusProxy *properties_proxy = NULL;
1462 GDBusConnection *g_conn;
1464 GError *error = NULL;
1466 BT_CHECK_PARAMETER(characteristic_handle, return);
1467 BT_CHECK_ENABLED(return);
1469 g_conn = _bt_get_system_private_conn();
1470 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1472 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1473 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1475 characteristic_handle,
1476 BT_PROPERTIES_INTERFACE,
1479 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1481 handle = g_strdup(characteristic_handle);
1482 g_dbus_proxy_call(properties_proxy,
1484 g_variant_new("(ss)",
1485 GATT_CHAR_INTERFACE, "Descriptors"),
1486 G_DBUS_CALL_FLAGS_NONE,
1488 (GAsyncReadyCallback)bluetooth_gatt_get_char_desc_cb,
1491 return BLUETOOTH_ERROR_NONE;
1494 static void __bluetooth_internal_read_desc_cb(GObject *source_object,
1498 GError *error = NULL;
1499 GDBusConnection *system_gconn = NULL;
1501 bt_user_info_t *user_info;
1502 GByteArray *gp_byte_array = NULL;
1506 bt_gatt_resp_data_t *resp_data = user_data;
1510 system_gconn = _bt_get_system_private_conn();
1511 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1513 user_info = _bt_get_user_data(BT_COMMON);
1516 BT_ERR("Error : %s [%s]", error->message, resp_data->handle + 15);
1517 g_clear_error(&error);
1522 g_variant_unref(value);
1527 att_ecode = __bluetooth_get_att_error_code(error, resp_data->handle);
1528 g_clear_error(&error);
1530 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1531 att_ecode, resp_data,
1532 user_info->cb, user_info->user_data);
1537 gp_byte_array = g_byte_array_new();
1538 g_variant_get(value, "(ay)", &iter);
1540 while (g_variant_iter_loop(iter, "y", &g_byte))
1541 g_byte_array_append(gp_byte_array, &g_byte, 1);
1543 if (gp_byte_array->len != 0) {
1544 resp_data->len = gp_byte_array->len;
1545 resp_data->value = gp_byte_array->data;
1548 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1549 BLUETOOTH_ATT_ERROR_NONE, resp_data,
1550 user_info->cb, user_info->user_data);
1554 g_byte_array_free(gp_byte_array, TRUE);
1555 g_variant_iter_free(iter);
1556 g_variant_unref(value);
1561 BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *desc,
1564 GDBusConnection *conn;
1565 GVariantBuilder *builder;
1567 bt_gatt_resp_data_t *resp_data;
1571 BT_CHECK_PARAMETER(desc, return);
1572 BT_CHECK_ENABLED(return);
1574 conn = _bt_get_system_private_conn();
1575 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1577 resp_data = g_malloc0(sizeof(bt_gatt_resp_data_t));
1578 resp_data->user_data = user_data;
1579 resp_data->handle = (char *)desc;
1581 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1584 g_variant_builder_add(builder, "{sv}", "offset",
1585 g_variant_new("q", offset));
1586 /* Device Object path*/
1587 // g_variant_builder_add(builder, "{sv}", "device",
1588 // g_variant_new("o", serv_info->serv_path));
1590 g_dbus_connection_call(conn, BT_BLUEZ_NAME, desc, GATT_DESC_INTERFACE,
1591 "ReadValue", g_variant_new("(a{sv})", builder),
1592 G_VARIANT_TYPE("(ay)"),
1593 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1594 (GAsyncReadyCallback)__bluetooth_internal_read_desc_cb,
1595 (gpointer)resp_data);
1596 g_variant_builder_unref(builder);
1599 return BLUETOOTH_ERROR_NONE;
1602 static void __bluetooth_internal_write_desc_cb(GObject *source_object,
1603 GAsyncResult *res, gpointer user_data)
1605 GError *error = NULL;
1606 bt_user_info_t *user_info;
1607 GDBusConnection *system_gconn = NULL;
1609 int att_ecode = BLUETOOTH_ATT_ERROR_NONE;
1610 bt_gatt_resp_data_t *resp_data = user_data;
1614 system_gconn = _bt_get_system_private_conn();
1615 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1617 user_info = _bt_get_user_data(BT_COMMON);
1620 BT_ERR("Error : %s [%s]", error->message, resp_data->handle + 15);
1621 g_clear_error(&error);
1626 g_variant_unref(value);
1631 att_ecode = __bluetooth_get_att_error_code(error, resp_data->handle);
1632 g_clear_error(&error);
1634 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_DESC,
1635 att_ecode, resp_data,
1636 user_info->cb, user_info->user_data);
1641 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_DESC,
1642 BLUETOOTH_ATT_ERROR_NONE, resp_data,
1643 user_info->cb, user_info->user_data);
1646 g_variant_unref(value);
1650 BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(const char *desc,
1651 const guint8 *value, int length, gpointer user_data)
1653 GVariant *val, *options;
1654 GDBusConnection *conn;
1655 GVariantBuilder *builder1;
1656 GVariantBuilder *builder2;
1659 bt_gatt_resp_data_t *resp_data;
1663 BT_CHECK_PARAMETER(desc, return);
1664 BT_CHECK_PARAMETER(value, return);
1665 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1666 BT_CHECK_ENABLED(return);
1668 conn = _bt_get_system_private_conn();
1669 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1671 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1673 for (i = 0; i < length; i++)
1674 g_variant_builder_add(builder1, "y", value[i]);
1676 val = g_variant_new("ay", builder1);
1678 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1680 g_variant_builder_add(builder2, "{sv}", "offset",
1681 g_variant_new_uint16(offset));
1683 /* Device Object path*/
1684 // g_variant_builder_add(builder2, "{sv}", "device",
1685 // g_variant_new_object("o", NULL));
1687 options = g_variant_new("a{sv}", builder2);
1689 resp_data = g_malloc0(sizeof(bt_gatt_resp_data_t));
1690 resp_data->user_data = user_data;
1691 resp_data->handle = (char *)desc;
1693 g_dbus_connection_call(conn, BT_BLUEZ_NAME, desc, GATT_DESC_INTERFACE,
1694 "WriteValue", g_variant_new("(@ay@a{sv})",
1695 val, options), NULL,
1696 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1697 (GAsyncReadyCallback)__bluetooth_internal_write_desc_cb,
1698 (gpointer)resp_data);
1700 g_variant_builder_unref(builder1);
1701 g_variant_builder_unref(builder2);
1704 return BLUETOOTH_ERROR_NONE;
1707 BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle, const char *svc_name)
1709 GDBusConnection *conn;
1710 GError *error = NULL;
1711 int ret = BLUETOOTH_ERROR_NONE;
1713 BT_CHECK_PARAMETER(char_handle, return);
1714 BT_CHECK_ENABLED(return);
1716 BT_INFO_C("### Enable CCCD : %s [%s]", char_handle + 15, svc_name);
1718 conn = _bt_get_system_private_conn();
1719 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1721 g_dbus_connection_call_sync(conn,
1724 GATT_CHAR_INTERFACE,
1728 G_DBUS_CALL_FLAGS_NONE,
1729 GATT_DEFAULT_TIMEOUT, NULL, &error);
1732 g_dbus_error_strip_remote_error(error);
1733 BT_ERR_C("### Watch Failed: %s", error->message);
1734 if (g_strrstr(error->message, "Already notifying"))
1735 ret = BLUETOOTH_ERROR_NONE;
1736 else if (g_strrstr(error->message, "In Progress"))
1737 ret = BLUETOOTH_ERROR_IN_PROGRESS;
1738 else if (g_strrstr(error->message, "Operation is not supported"))
1739 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
1740 /*failed because of either Insufficient Authorization or Write Not Permitted */
1741 else if (g_strrstr(error->message, "Write not permitted") ||
1742 g_strrstr(error->message, "Operation Not Authorized"))
1743 ret = BLUETOOTH_ERROR_PERMISSION_DEINED;
1744 /* failed because of either Insufficient Authentication,
1745 Insufficient Encryption Key Size, or Insufficient Encryption. */
1746 else if (g_strrstr(error->message, "Not paired"))
1747 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1749 ret = BLUETOOTH_ERROR_INTERNAL;
1751 g_clear_error(&error);
1757 BT_EXPORT_API int bluetooth_gatt_unwatch_characteristics(const char *char_handle)
1759 GDBusConnection *conn;
1760 GError *error = NULL;
1761 int ret = BLUETOOTH_ERROR_NONE;
1762 BT_CHECK_PARAMETER(char_handle, return);
1764 BT_CHECK_ENABLED(return);
1766 BT_INFO("Disable CCCD : %s", char_handle);
1768 conn = _bt_get_system_private_conn();
1769 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1771 g_dbus_connection_call_sync(conn,
1774 GATT_CHAR_INTERFACE,
1778 G_DBUS_CALL_FLAGS_NONE,
1779 GATT_DEFAULT_TIMEOUT, NULL, &error);
1782 BT_ERR("Watch Failed: %s", error->message);
1783 g_clear_error(&error);
1784 ret = BLUETOOTH_ERROR_INTERNAL;
1790 void __bt_uuid_hex_to_string(unsigned char *uuid, char *str)
1792 uint32_t uuid0, uuid4;
1793 uint16_t uuid1, uuid2, uuid3, uuid5;
1795 memcpy(&uuid0, &(uuid[0]), 4);
1796 memcpy(&uuid1, &(uuid[4]), 2);
1797 memcpy(&uuid2, &(uuid[6]), 2);
1798 memcpy(&uuid3, &(uuid[8]), 2);
1799 memcpy(&uuid4, &(uuid[10]), 4);
1800 memcpy(&uuid5, &(uuid[14]), 2);
1802 snprintf((char *)str, BLUETOOTH_UUID_STRING_MAX, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
1803 ntohl(uuid0), ntohs(uuid1),
1804 ntohs(uuid2), ntohs(uuid3),
1805 ntohl(uuid4), ntohs(uuid5));
1810 static void __bt_fill_service_handle_informations(bt_services_browse_info_t *props,
1811 bt_gatt_handle_info_t *svcs)
1815 if (props->count == 0)
1818 svcs->count = props->count;
1820 for (count = 0; count < props->count; count++) {
1821 BT_DBG("[%d] %s instance_id [%d] Is Primary [%d]",
1822 count, props->uuids[count], props->inst_id[count], props->primary[count]);
1823 g_strlcpy(svcs->uuids[count], props->uuids[count],
1824 BLUETOOTH_UUID_STRING_MAX);
1825 svcs->inst_id[count] = props->inst_id[count];
1829 static void __bt_fill_char_handle_informations(bt_char_browse_info_t *props, bt_gatt_service_property_t *service)
1832 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
1834 service->char_handle.count = props->count;
1836 /* Before filling all char handles, fill the service's UUID and instance ID */
1837 __bt_uuid_hex_to_string(props->svc_uuid, uuid_string);
1838 service->uuid = g_strdup(uuid_string);
1839 service->primary = TRUE;
1841 /* Now fill all the char handles [UUID and Instance ID's]*/
1842 for (count = 0; count < props->count; count++) {
1843 BT_DBG("[%d] %s instance_id [%d] properties [%d]",
1844 count, props->uuids[count], props->inst_id[count], props->props[count]);
1846 g_strlcpy(service->char_handle.uuids[count],
1847 props->uuids[count],
1848 BLUETOOTH_UUID_STRING_MAX);
1850 service->char_handle.inst_id[count] = props->inst_id[count];
1854 static void __bt_fill_desc_handle_informations(bt_descriptor_browse_info_t *props,
1855 bt_gatt_char_property_t *charc)
1858 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
1860 charc->char_desc_handle.count = props->count;
1862 /* Before filling all desc handles, fill the charac's UUID and instance ID */
1863 __bt_uuid_hex_to_string(props->char_uuid, uuid_string);
1864 charc->uuid = g_strdup(uuid_string);
1866 /* Now fill all the descriptor handles [UUID and Instance ID's]*/
1867 for (count = 0; count < props->count; count++) {
1868 BT_DBG("[%d] %s instance_id [%d]",
1869 count, props->uuids[count], props->inst_id[count]);
1871 g_strlcpy(charc->char_desc_handle.uuids[count],
1872 props->uuids[count],
1873 BLUETOOTH_UUID_STRING_MAX);
1875 charc->char_desc_handle.inst_id[count] = props->inst_id[count];
1877 charc->permission = props->char_props_map;
1878 __convert_permission_flag_to_str(charc->permission);
1882 BT_EXPORT_API int bluetooth_gatt_client_init(
1884 const bluetooth_device_address_t *address,
1885 gatt_client_cb_func_ptr callback_ptr)
1887 int ret = BLUETOOTH_ERROR_NONE;
1888 bt_event_info_t *event_info;
1891 BT_CHECK_PARAMETER(client_id, return);
1892 BT_CHECK_PARAMETER(callback_ptr, return);
1893 BT_CHECK_ENABLED(return);
1896 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1898 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
1900 ret = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_CLIENT_REGISTER,
1901 in_param1, in_param2, in_param3, in_param4, &out_param);
1903 /* App ID -1 is invalid */
1904 if (ret == BLUETOOTH_ERROR_NONE) {
1906 *client_id = g_array_index(out_param, int, 0);
1908 BT_INFO("GATT Client Registered successfully: Client instance ID [%d]", *client_id);
1910 event_info = _bt_event_get_cb_data(BT_GATT_CLIENT_EVENT);
1913 count = (int*)event_info->user_data;
1915 BT_INFO("Total num of GATT client instances [%d]", *count);
1917 /* Increement the count */
1920 BT_INFO("No GATT Client instances found in this application: Set User data");
1921 count = g_malloc0(sizeof(int));
1924 /* Register event handler for GATT */
1925 _bt_register_event(BT_GATT_CLIENT_EVENT,
1926 (void *)callback_ptr,
1928 _bt_set_user_data(BT_GATT_CLIENT, (void *)callback_ptr, NULL);
1931 BT_ERR("GATT Client Registration failed!! ret [%d]", ret);
1933 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1940 BT_EXPORT_API int bluetooth_gatt_client_get_primary_services(
1941 const bluetooth_device_address_t *address, /* Remote GATT Server */
1942 bt_gatt_handle_info_t *prim_svc) /* UUID & instance_id */
1945 int result = BLUETOOTH_ERROR_NONE;
1946 bt_services_browse_info_t service_props;
1948 BT_CHECK_PARAMETER(address, return);
1949 BT_CHECK_PARAMETER(prim_svc, return);
1950 BT_CHECK_ENABLED(return);
1953 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1954 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
1956 result = _bt_send_request(BT_BLUEZ_SERVICE,
1957 BT_GATT_GET_PRIMARY_SERVICES,
1958 in_param1, in_param2, in_param3, in_param4, &out_param);
1960 if (BLUETOOTH_ERROR_NONE != result)
1963 memset(&service_props, 0x00, sizeof(bt_services_browse_info_t));
1965 service_props = g_array_index(
1966 out_param, bt_services_browse_info_t, 0);
1968 __bt_fill_service_handle_informations(
1969 &service_props, prim_svc);
1972 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1976 BT_EXPORT_API int bluetooth_gatt_client_get_service_property(
1977 const char *address,
1978 bt_gatt_handle_property_t *service_handle,
1979 bt_gatt_service_property_t *service)
1981 int result = BLUETOOTH_ERROR_NONE;
1982 bluetooth_gatt_client_svc_prop_info_t svc_prop;
1983 bt_char_browse_info_t char_handles_info;
1985 BT_CHECK_PARAMETER(address, return);
1986 BT_CHECK_PARAMETER(service_handle, return);
1987 BT_CHECK_PARAMETER(service, return);
1988 BT_CHECK_ENABLED(return);
1990 /* Call to bt-service (sync) and send address and service_handle info */
1992 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1994 memset(&svc_prop, 0x00, sizeof(bluetooth_gatt_client_svc_prop_info_t));
1995 /* All characteristics handles are discovered */
1996 memset(&char_handles_info, 0x00, sizeof(bt_char_browse_info_t));
1998 svc_prop.svc.instance_id = service_handle->instance_id;
1999 memcpy(&svc_prop.svc.uuid, &service_handle->uuid, 16);
2001 _bt_convert_addr_string_to_type(svc_prop.device_address.addr, address);
2003 g_array_append_vals(in_param1, &svc_prop, sizeof(bluetooth_gatt_client_svc_prop_info_t));
2005 result = _bt_send_request(BT_BLUEZ_SERVICE,
2006 BT_GATT_GET_SERVICE_PROPERTIES,
2007 in_param1, in_param2, in_param3, in_param4, &out_param);
2009 if (BLUETOOTH_ERROR_NONE != result)
2012 char_handles_info = g_array_index(
2013 out_param, bt_char_browse_info_t, 0);
2014 __bt_fill_char_handle_informations(&char_handles_info,
2016 /* TODO Get all Included Services */
2019 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2023 BT_EXPORT_API int bluetooth_gatt_client_get_characteristics_property(
2024 const char *address,
2025 bt_gatt_handle_property_t *service_handle,
2026 bt_gatt_handle_property_t *char_handle,
2027 bt_gatt_char_property_t *char_property)
2029 int result = BLUETOOTH_ERROR_NONE;
2030 bt_descriptor_browse_info_t desc_handles_info;
2031 bluetooth_gatt_client_char_prop_info_t char_prop;
2033 BT_CHECK_PARAMETER(address, return);
2034 BT_CHECK_PARAMETER(service_handle, return);
2035 BT_CHECK_PARAMETER(char_handle, return);
2036 BT_CHECK_PARAMETER(char_property, return);
2037 BT_CHECK_ENABLED(return);
2040 /* Call to bt-service (sync) and send address service_handle info & char handle info */
2042 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2044 memset(&char_prop, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2045 char_prop.svc.instance_id = service_handle->instance_id;
2046 memcpy(&char_prop.svc.uuid, &service_handle->uuid, 16);
2048 char_prop.characteristic.instance_id = char_handle->instance_id;
2049 memcpy(&char_prop.characteristic.uuid, &char_handle->uuid, 16);
2051 _bt_convert_addr_string_to_type(char_prop.device_address.addr, address);
2053 g_array_append_vals(in_param1, &char_prop, sizeof(bluetooth_gatt_client_char_prop_info_t));
2055 result = _bt_send_request(BT_BLUEZ_SERVICE,
2056 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
2057 in_param1, in_param2, in_param3, in_param4, &out_param);
2059 if (BLUETOOTH_ERROR_NONE != result)
2062 /* All descriptors handles are discovered */
2063 memset(&desc_handles_info, 0x00, sizeof(bt_descriptor_browse_info_t));
2065 desc_handles_info = g_array_index(
2066 out_param, bt_descriptor_browse_info_t, 0);
2068 __bt_fill_desc_handle_informations(&desc_handles_info,
2072 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2074 /* Call to bt-service (sync) and send address, service_handle & char_handle infos */
2076 /* After result is fetched, extract descriptor handles (UUID's & instance_id's) */
2078 /* Fill the info in *char_prop */
2080 /*No: Before returning, call bluetooth_gatt_client_read_characteristic_value as
2081 an asyn function and leave every thing else in the callback */
2084 BT_EXPORT_API int bluetooth_gatt_client_get_char_descriptor_property(
2085 const char *address,
2086 bt_gatt_handle_property_t *service_handle,
2087 bt_gatt_handle_property_t *char_handle,
2088 bt_gatt_handle_property_t *descriptor_handle,
2089 bt_gatt_char_descriptor_property_t *desc_prop)
2091 char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2093 BT_CHECK_PARAMETER(address, return);
2094 BT_CHECK_PARAMETER(service_handle, return);
2095 BT_CHECK_PARAMETER(char_handle, return);
2096 BT_CHECK_PARAMETER(descriptor_handle, return);
2097 BT_CHECK_PARAMETER(desc_prop, return);
2098 /* No Need to Call to bt-service (sync) and send address, service_handle,
2099 char_handle & descriptor handle infos */
2100 /* After result is fetched, extract descriptior handles (UUID's & instance_id's) */
2101 /* Fill the info in *desc_prop */
2102 __bt_uuid_hex_to_string(descriptor_handle->uuid, uuid_string);
2104 /* Before filling all desc handles, fill the charac's UUID and instance ID */
2105 desc_prop->uuid = g_strdup(uuid_string);
2107 return BLUETOOTH_ERROR_NONE;
2108 /* No: Before returning, call bluetooth_gatt_client_read_descriptor_value
2109 as an asyn function and leave every thing else in the callback */
2113 static gboolean bluetooth_gatt_client_notify_channel_watch_cb(GIOChannel *gio,
2114 GIOCondition cond, gpointer data)
2116 bt_gatt_characteristic_notify_info_t *chr_info = (bt_gatt_characteristic_notify_info_t *)data;
2119 BT_ERR("chr_info is invalid");
2123 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
2124 BT_ERR("Error : GIOCondition %d, [%s]", cond, chr_info->UUID);
2125 g_io_channel_shutdown(gio, TRUE, NULL);
2126 g_io_channel_unref(gio);
2128 gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
2134 if (cond & G_IO_IN) {
2135 GIOStatus status = G_IO_STATUS_NORMAL;
2137 char *buffer = NULL;
2139 bt_event_info_t *event_info;
2141 buffer = g_malloc0(chr_info->mtu + 1);
2142 memset(buffer, 0, chr_info->mtu + 1);
2144 status = g_io_channel_read_chars(gio, buffer,
2145 chr_info->mtu, &len, &err);
2146 if (status != G_IO_STATUS_NORMAL) {
2147 BT_ERR("IO Channel read is failed with %d", status);
2150 BT_ERR("IO Channel read error [%s]", err->message);
2151 if (status == G_IO_STATUS_ERROR) {
2152 BT_ERR("cond : %d", cond);
2154 g_io_channel_shutdown(gio, TRUE, NULL);
2155 g_io_channel_unref(gio);
2157 gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
2166 if (len > 0 && len < chr_info->mtu) {
2167 bt_gatt_char_property_t char_val;
2168 // BT_DBG("FD io sending value changed %x %x %x %x %x %x %x %zd \n", buffer[0], buffer[1], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], len);
2170 memcpy(char_val.prop.uuid, chr_info->UUID, 16);
2171 memcpy(char_val.value, buffer, len);
2172 char_val.val_len = len;
2173 memcpy(char_val.address, chr_info->address, 18);
2175 event_info = _bt_event_get_cb_data(BT_GATT_CLIENT_EVENT);
2177 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
2178 BLUETOOTH_ERROR_NONE, &char_val,
2179 event_info->cb, event_info->user_data);
2181 BT_ERR("event_info failed");
2185 BT_ERR("Packet corrupted");
2195 #ifndef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
2196 static bt_gatt_characteristic_notify_info_t * bluetooth_gatt_client_get_characteristic_notify_info(unsigned char *handle , int id)
2200 for (l = gatt_characteristic_notify_list; l != NULL; l = l->next) {
2201 bt_gatt_characteristic_notify_info_t *info = l->data;
2202 if (memcmp(info->UUID, handle, 16) == 0 && info->id == id)
2209 static bt_gatt_characteristic_notify_info_t * bluetooth_gatt_client_create_watch_io(int fd, int id, int mtu, char * address, unsigned char *uuid)
2211 GIOChannel *channel;
2212 bt_gatt_characteristic_notify_info_t *chr_info;
2214 chr_info = g_malloc0(sizeof(bt_gatt_characteristic_notify_info_t));
2215 chr_info->notify_fd = fd;
2217 chr_info->mtu = mtu;
2218 g_strlcpy(chr_info->address, address, 18);
2219 memcpy(chr_info->UUID, uuid, 16);
2221 channel = g_io_channel_unix_new(fd);
2223 chr_info->io_channel = channel;
2225 g_io_channel_set_encoding(channel, NULL, NULL);
2226 g_io_channel_set_buffered(channel, FALSE);
2227 g_io_channel_set_close_on_unref(channel, TRUE);
2228 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
2230 chr_info->watch_id = g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP),
2231 bluetooth_gatt_client_notify_channel_watch_cb, chr_info);
2237 BT_EXPORT_API int bluetooth_gatt_client_watch_characteristics(
2238 const char *address,
2239 bt_gatt_handle_property_t *service_handle,
2240 bt_gatt_handle_property_t *char_handle,
2243 gboolean is_indicate)
2245 int result = BLUETOOTH_ERROR_NONE;
2246 bluetooth_gatt_client_char_prop_info_t param;
2247 bt_gatt_characteristic_notify_info_t *chr_info;
2249 BT_CHECK_PARAMETER(address, return);
2250 BT_CHECK_PARAMETER(service_handle, return);
2251 BT_CHECK_PARAMETER(char_handle, return);
2253 #ifndef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
2254 chr_info = bluetooth_gatt_client_get_characteristic_notify_info(char_handle->uuid , char_handle->instance_id);
2255 if (chr_info && !is_notify) {
2256 BT_INFO("Already CCCD enabled. fd %d", chr_info->notify_fd);
2258 if (chr_info->watch_id > 0)
2259 g_source_remove(chr_info->watch_id);
2261 if (chr_info->io_channel) {
2262 g_io_channel_shutdown(chr_info->io_channel, TRUE, NULL);
2263 g_io_channel_unref(chr_info->io_channel);
2266 gatt_characteristic_notify_list = g_slist_remove(gatt_characteristic_notify_list, chr_info);
2274 /* ASync Function, result expected in callback from bt-service */
2277 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2279 memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2281 memcpy(¶m.svc.uuid, service_handle->uuid, 16);
2282 param.svc.instance_id = service_handle->instance_id;
2284 memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
2285 param.characteristic.instance_id = char_handle->instance_id;
2287 _bt_convert_addr_string_to_type(param.device_address.addr, address);
2289 g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_char_prop_info_t));
2290 g_array_append_vals(in_param2, &client_id, sizeof(int));
2291 g_array_append_vals(in_param3, &is_notify, sizeof(gboolean));
2292 g_array_append_vals(in_param4, &is_indicate, sizeof(gboolean));
2294 #ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
2295 result = _bt_send_request(BT_BLUEZ_SERVICE,
2296 BT_GATT_WATCH_CHARACTERISTIC,
2297 in_param1, in_param2, in_param3, in_param4, &out_param);
2299 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2301 BT_INFO("Result [%d]", result);
2305 GUnixFDList *out_fd_list = NULL;
2306 char *svc_name = NULL;
2309 _bt_convert_uuid_type_to_string(str_uuid, param.svc.uuid);
2310 bluetooth_get_uuid_name(str_uuid, &svc_name);
2312 _bt_convert_uuid_type_to_string(str_uuid, param.characteristic.uuid);
2314 BT_INFO_C("### Request subscription Notify : %s [%s]", str_uuid, svc_name);
2317 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_WATCH_CHARACTERISTIC,
2318 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
2319 if (result != BLUETOOTH_ERROR_NONE) {
2320 BT_ERR("Fail to get Nofify FD. result %d", result);
2321 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2325 if (is_indicate == false) {
2326 if (NULL == out_fd_list) {
2327 BT_ERR("out_fd_list is NULL");
2328 result = BLUETOOTH_ERROR_INTERNAL;
2335 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
2336 BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
2337 fd = fd_list_array[0];
2338 mtu = g_array_index(out_param, int, 0);
2340 BT_INFO("Acquired characteristic Notify FD %d, mtu %d", fd, mtu);
2341 chr_info = bluetooth_gatt_client_create_watch_io(fd, char_handle->instance_id, mtu, (char *)address, char_handle->uuid);
2343 gatt_characteristic_notify_list = g_slist_append(gatt_characteristic_notify_list, chr_info);
2345 g_free(fd_list_array);
2346 g_object_unref(out_fd_list);
2350 /*result = _bt_send_request(BT_BLUEZ_SERVICE,
2351 BT_GATT_WATCH_CHARACTERISTIC,
2352 in_param1, in_param2, in_param3, in_param4, &out_param);*/
2354 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2359 BT_EXPORT_API int bluetooth_gatt_client_read_characteristic_value(
2360 const char *address,
2361 bt_gatt_handle_property_t *service_handle,
2362 bt_gatt_handle_property_t *char_handle)
2364 int result = BLUETOOTH_ERROR_NONE;
2365 bt_user_info_t *user_info;
2366 bluetooth_gatt_client_char_prop_info_t param;
2368 BT_CHECK_PARAMETER(address, return);
2369 BT_CHECK_PARAMETER(service_handle, return);
2370 BT_CHECK_PARAMETER(char_handle, return);
2372 user_info = _bt_get_user_data(BT_GATT_CLIENT);
2373 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
2376 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2378 /* Async Function, result expected in callback from bt-service */
2379 /* Call to bt-service (sync) and send address service_handle info & char handle info */
2380 memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2382 memcpy(¶m.svc.uuid, service_handle->uuid, 16);
2383 param.svc.instance_id = service_handle->instance_id;
2385 memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
2386 param.characteristic.instance_id = char_handle->instance_id;
2388 _bt_convert_addr_string_to_type(param.device_address.addr, address);
2390 g_array_append_vals(in_param1, ¶m,
2391 sizeof(bluetooth_gatt_client_char_prop_info_t));
2393 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_GATT_READ_CHARACTERISTIC,
2394 in_param1, in_param2, in_param3, in_param4,
2395 user_info->cb, user_info->user_data);
2397 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2401 BT_EXPORT_API int bluetooth_gatt_client_read_descriptor_value(
2402 const char *address,
2403 bt_gatt_handle_property_t *service_handle,
2404 bt_gatt_handle_property_t *char_handle,
2405 bt_gatt_handle_property_t *descriptor_handle)
2407 int result = BLUETOOTH_ERROR_NONE;
2408 bt_user_info_t *user_info;
2409 bluetooth_gatt_client_desc_prop_info_t param;
2411 BT_CHECK_PARAMETER(address, return);
2412 BT_CHECK_PARAMETER(service_handle, return);
2413 BT_CHECK_PARAMETER(char_handle, return);
2414 BT_CHECK_PARAMETER(descriptor_handle, return);
2416 user_info = _bt_get_user_data(BT_GATT_CLIENT);
2417 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
2420 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2422 /* Async Function, result expected in callback from bt-service */
2423 memset(¶m, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
2425 memcpy(¶m.svc.uuid, service_handle->uuid, 16);
2426 param.svc.instance_id = service_handle->instance_id;
2428 memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
2429 param.characteristic.instance_id = char_handle->instance_id;
2431 memcpy(¶m.descriptor.uuid, descriptor_handle->uuid, 16);
2432 param.descriptor.instance_id = descriptor_handle->instance_id;
2434 _bt_convert_addr_string_to_type(param.device_address.addr, address);
2436 g_array_append_vals(in_param1, ¶m,
2437 sizeof(bluetooth_gatt_client_desc_prop_info_t));
2439 result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_GATT_READ_DESCRIPTOR_VALUE,
2440 in_param1, in_param2, in_param3, in_param4,
2441 user_info->cb, user_info->user_data);
2443 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2448 static bt_gatt_characteristic_write_info_t * bluetooth_gatt_client_get_characteristic_fd(unsigned char *handle, int id)
2453 for (l = gatt_characteristic_write_list; l != NULL; l = l->next) {
2454 bt_gatt_characteristic_write_info_t *info = l->data;
2456 if (memcmp(info->UUID, handle, 16) == 0 && info->id == id)
2460 _bt_convert_uuid_type_to_string(str, handle);
2461 BT_ERR("Unable to get characteristic fd. [%s] id [ %d]", str, id);
2465 static gboolean bluetooth_gatt_client_write_channel_watch_cb(GIOChannel *gio,
2466 GIOCondition cond, gpointer data)
2468 bt_gatt_characteristic_write_info_t *chr_info = (bt_gatt_characteristic_write_info_t *)data;
2473 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
2475 _bt_convert_uuid_type_to_string(uuid_str, chr_info->UUID);
2476 BT_ERR("Error : GIOCondition %d, [%s]", cond, uuid_str);
2477 g_io_channel_shutdown(gio, TRUE, NULL);
2478 g_io_channel_unref(gio);
2480 gatt_characteristic_write_list = g_slist_remove(gatt_characteristic_write_list, chr_info);
2489 static int bluetooth_gatt_client_write_characteristics_value_to_fd(
2490 int fd, const guint8 *value, int length, int mtu,
2494 int att_result = BLUETOOTH_ERROR_NONE;
2495 BT_CHECK_PARAMETER(value, return);
2496 written = write(fd, value, length);
2497 if (written != length) {
2498 att_result = BLUETOOTH_ERROR_INTERNAL;
2499 BT_ERR("write data failed. ret : %d ", written);
2505 static void bluetooth_gatt_client_create_write_io_channel(int fd, unsigned char * uuid, int id, int mtu)
2507 bt_gatt_characteristic_write_info_t *chr_info;
2508 GIOChannel *channel;
2510 chr_info = g_malloc0(sizeof(bt_gatt_characteristic_write_info_t));
2511 chr_info->write_fd = fd;
2513 chr_info->mtu = mtu;
2515 memcpy(chr_info->UUID, uuid, 16);
2516 channel = g_io_channel_unix_new(fd);
2517 g_io_channel_set_encoding(channel, NULL, NULL);
2518 g_io_channel_set_buffered(channel, FALSE);
2519 g_io_channel_set_close_on_unref(channel, TRUE);
2520 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
2521 g_io_add_watch(channel, (G_IO_ERR | G_IO_HUP | G_IO_NVAL),
2522 bluetooth_gatt_client_write_channel_watch_cb, chr_info);
2524 gatt_characteristic_write_list = g_slist_append(gatt_characteristic_write_list, chr_info);
2528 BT_EXPORT_API int bluetooth_gatt_client_write_characteristic_value_by_type(
2529 const char *address,
2530 bt_gatt_handle_property_t *service_handle,
2531 bt_gatt_handle_property_t *char_handle,
2532 bluetooth_gatt_att_data_t *data,
2533 bluetooth_gatt_write_type_e write_type)
2535 int result = BLUETOOTH_ERROR_NONE;
2536 bt_user_info_t *user_info;
2537 bluetooth_gatt_client_char_prop_info_t param;
2539 BT_CHECK_PARAMETER(address, return);
2540 BT_CHECK_PARAMETER(service_handle, return);
2541 BT_CHECK_PARAMETER(char_handle, return);
2542 BT_CHECK_PARAMETER(data, return);
2544 /* ASync Function, result expected in callback from bt-service */
2545 user_info = _bt_get_user_data(BT_GATT_CLIENT);
2546 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
2549 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2551 memset(¶m, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2553 memcpy(¶m.svc.uuid, service_handle->uuid, 16);
2554 param.svc.instance_id = service_handle->instance_id;
2556 memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
2557 param.characteristic.instance_id = char_handle->instance_id;
2559 _bt_convert_addr_string_to_type(param.device_address.addr, address);
2561 g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_char_prop_info_t));
2562 g_array_append_vals(in_param2, data, sizeof(bluetooth_gatt_att_data_t));
2563 g_array_append_vals(in_param3, &write_type, sizeof(bluetooth_gatt_write_type_e));
2565 #ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
2569 if (write_type == BLUETOOTH_GATT_TYPE_WRITE_NO_RESPONSE) {
2572 bt_gatt_characteristic_write_info_t *info;
2573 info = bluetooth_gatt_client_get_characteristic_fd(char_handle->uuid, service_handle->instance_id);
2576 fd = info->write_fd;
2582 GUnixFDList *out_fd_list = NULL;
2584 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_GATT_ACQUIRE_WRITE,
2585 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
2587 mtu = g_array_index(out_param, int, 0);
2589 if (result != BLUETOOTH_ERROR_NONE) {
2590 BT_ERR("Fail to get Write FD. result %d", result);
2591 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2593 } else if (NULL == out_fd_list) {
2594 BT_ERR("out_fd_list is NULL");
2595 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2596 return BLUETOOTH_ERROR_INTERNAL;
2601 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
2602 fd = fd_list_array[0];
2604 g_free(fd_list_array);
2605 g_object_unref(out_fd_list);
2607 BT_INFO("Acquired characteristic Write FD %d, mtu %d", fd, mtu);
2610 bluetooth_gatt_client_create_write_io_channel(fd, char_handle->uuid, service_handle->instance_id, mtu);
2612 result = bluetooth_gatt_client_write_characteristics_value_to_fd(fd, data->data, data->length, mtu, NULL);
2615 BT_ERR("characteristic info FD is invalid");
2620 result = bluetooth_gatt_client_write_characteristics_value_to_fd(fd, data->data, data->length, mtu, NULL);
2625 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
2626 BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
2627 in_param1, in_param2, in_param3, in_param4,
2628 user_info->cb, user_info->user_data);
2631 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2636 BT_EXPORT_API int bluetooth_gatt_client_write_descriptor_value(
2637 const char *address,
2638 bt_gatt_handle_property_t *service_handle,
2639 bt_gatt_handle_property_t *char_handle,
2640 bt_gatt_handle_property_t *descriptor_handle,
2641 bluetooth_gatt_att_data_t *data,
2642 bluetooth_gatt_write_type_e write_type)
2644 int result = BLUETOOTH_ERROR_NONE;
2645 bt_user_info_t *user_info;
2646 bluetooth_gatt_client_desc_prop_info_t param;
2648 BT_CHECK_PARAMETER(address, return);
2649 BT_CHECK_PARAMETER(service_handle, return);
2650 BT_CHECK_PARAMETER(char_handle, return);
2651 BT_CHECK_PARAMETER(descriptor_handle, return);
2652 BT_CHECK_PARAMETER(data, return);
2654 /* Async Function, result expected in callback from bt-service */
2655 user_info = _bt_get_user_data(BT_GATT_CLIENT);
2656 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
2659 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2661 memset(¶m, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
2663 memcpy(¶m.svc.uuid, service_handle->uuid, 16);
2664 param.svc.instance_id = service_handle->instance_id;
2666 memcpy(¶m.characteristic.uuid, char_handle->uuid, 16);
2667 param.characteristic.instance_id = char_handle->instance_id;
2669 memcpy(¶m.descriptor.uuid, descriptor_handle->uuid, 16);
2670 param.descriptor.instance_id = descriptor_handle->instance_id;
2672 _bt_convert_addr_string_to_type(param.device_address.addr, address);
2674 g_array_append_vals(in_param1, ¶m, sizeof(bluetooth_gatt_client_desc_prop_info_t));
2675 g_array_append_vals(in_param2, data, sizeof(bluetooth_gatt_att_data_t));
2676 g_array_append_vals(in_param3, &write_type, sizeof(bluetooth_gatt_write_type_e));
2678 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
2679 BT_GATT_WRITE_DESCRIPTOR_VALUE,
2680 in_param1, in_param2, in_param3, in_param4,
2681 user_info->cb, user_info->user_data);
2683 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2688 BT_EXPORT_API int bluetooth_gatt_client_set_service_change_watcher(
2689 const bluetooth_device_address_t *address, gboolean enable)
2692 bluetooth_device_address_t *addr = NULL;
2693 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2694 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2695 int result = BLUETOOTH_ERROR_NONE;
2698 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2700 g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
2701 g_array_append_vals(in_param2, &enable, sizeof(gboolean));
2703 _bt_convert_addr_type_to_string(device_address, (unsigned char *)address->addr);
2704 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2705 BT_INFO("%s watcher for [%s]", enable ? "Set":"Unset", secure_address);
2707 if (enable == TRUE) {
2708 if (_bluetooth_gatt_check_service_change_watcher_address(address)
2710 BT_INFO("The watcher is already set");
2714 if (service_monitor_list == NULL) {
2715 //_bt_register_manager_subscribe_signal(TRUE);
2717 result = _bt_send_request(BT_BLUEZ_SERVICE,
2718 BT_GATT_WATCH_SERVICE_CHANGED_INDICATION,
2719 in_param1, in_param2, in_param3, in_param4, &out_param);
2722 if (result == BLUETOOTH_ERROR_NONE) {
2723 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2724 memcpy(addr, address, sizeof(bluetooth_device_address_t));
2726 service_monitor_list =
2727 g_slist_append(service_monitor_list, addr);
2730 for (l = service_monitor_list; l != NULL; l = l->next) {
2733 if (!memcmp(address, addr,
2734 sizeof(bluetooth_device_address_t))) {
2735 service_monitor_list =
2736 g_slist_remove(service_monitor_list, addr);
2742 if (service_monitor_list == NULL) {
2743 //_bt_register_manager_subscribe_signal(FALSE);
2744 result = _bt_send_request(BT_BLUEZ_SERVICE,
2745 BT_GATT_WATCH_SERVICE_CHANGED_INDICATION,
2746 in_param1, in_param2, in_param3, in_param4, &out_param);
2752 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2756 BT_EXPORT_API int bluetooth_gatt_client_deinit(
2761 bt_event_info_t *event_info;
2763 BT_CHECK_ENABLED(return);
2765 BT_INFO("GATT Client Deinit Client instance ID [%d]", client_id);
2768 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
2770 g_array_append_vals(in_param1, &client_id, sizeof(int));
2772 /* Unregistration MUST NOT FAIL */
2773 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GATT_CLIENT_UNREGISTER,
2774 in_param1, in_param2, in_param3, in_param4, &out_param);
2776 if (result != BLUETOOTH_ERROR_NONE)
2777 BT_ERR("GATT Client Unregistration failed result [%d]", result);
2779 /* Unregister event handler if this is the only instance */
2780 event_info = _bt_event_get_cb_data(BT_GATT_CLIENT_EVENT);
2783 count = (int*)event_info->user_data;
2785 BT_DBG("Total num of GATT client instances [%d]", *count);
2788 BT_DBG("Currently only one GATT client instance, so remove it and unregister GATT client events");
2789 _bt_unregister_event(BT_GATT_CLIENT_EVENT);
2790 _bt_set_user_data(BT_GATT_CLIENT, NULL, NULL);
2794 BT_ERR("Impossible that client is created, but no event handler is registered!!!");
2796 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);