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)
45 BT_CHECK_PARAMETER(svc_pty, return);
47 g_free(svc_pty->uuid);
48 g_free(svc_pty->handle);
49 g_strfreev(svc_pty->include_handles.handle);
50 g_strfreev(svc_pty->char_handle.handle);
52 memset(svc_pty, 0, sizeof(bt_gatt_service_property_t));
54 return BLUETOOTH_ERROR_NONE;
57 BT_EXPORT_API int bluetooth_gatt_free_char_property(bt_gatt_char_property_t *char_pty)
59 BT_CHECK_PARAMETER(char_pty, return);
61 g_free(char_pty->uuid);
62 g_free(char_pty->name);
63 g_free(char_pty->description);
64 g_free(char_pty->val);
65 g_free(char_pty->handle);
66 g_strfreev(char_pty->char_desc_handle.handle);
68 memset(char_pty, 0, sizeof(bt_gatt_char_property_t));
70 return BLUETOOTH_ERROR_NONE;
73 BT_EXPORT_API int bluetooth_gatt_free_desc_property(bt_gatt_char_descriptor_property_t *desc_pty)
75 BT_CHECK_PARAMETER(desc_pty, return);
77 g_free(desc_pty->uuid);
78 g_free(desc_pty->val);
79 g_free(desc_pty->handle);
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 };
111 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
113 _bt_convert_addr_type_to_string(device_address,
114 (unsigned char *)device_addr->addr);
116 for (l = service_monitor_list; l != NULL; l = l->next) {
117 char device_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
118 char secure_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
119 bluetooth_device_address_t *addr = l->data;
121 _bt_convert_addr_type_to_string(device_address2,
122 (unsigned char *)addr->addr);
123 _bt_convert_addr_string_to_secure_string(secure_address,
125 _bt_convert_addr_string_to_secure_string(secure_address2,
127 BT_INFO("service_monitor_list [%s] - Input [%s]",
128 secure_address2, secure_address);
130 if (!memcmp(device_addr, addr,
131 sizeof(bluetooth_device_address_t)))
138 BT_EXPORT_API int bluetooth_gatt_set_service_change_watcher(
139 const bluetooth_device_address_t *address, gboolean enable)
142 bluetooth_device_address_t *addr = NULL;
143 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
144 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
146 _bt_convert_addr_type_to_string(device_address,
147 (unsigned char *)address->addr);
148 _bt_convert_addr_string_to_secure_string(secure_address,
150 BT_INFO("Set watcher for %s with %d", secure_address, enable);
152 if (enable == TRUE) {
153 if (service_monitor_list == NULL)
154 _bt_register_manager_subscribe_signal(TRUE);
156 if (_bluetooth_gatt_check_service_change_watcher_address(address)
158 BT_INFO("The watcher is already set");
159 return BLUETOOTH_ERROR_NONE;
161 addr = g_malloc0(sizeof(bluetooth_device_address_t));
162 memcpy(addr, address, sizeof(bluetooth_device_address_t));
164 service_monitor_list =
165 g_slist_append(service_monitor_list, addr);
168 for (l = service_monitor_list; l != NULL; l = l->next) {
170 if (!memcmp(address, addr,
171 sizeof(bluetooth_device_address_t))) {
172 service_monitor_list =
173 g_slist_remove(service_monitor_list, addr);
179 if (service_monitor_list == NULL)
180 _bt_register_manager_subscribe_signal(FALSE);
183 return BLUETOOTH_ERROR_NONE;
186 BT_EXPORT_API int bluetooth_gatt_get_service_property(const char *service_handle,
187 bt_gatt_service_property_t *service)
189 GDBusProxy *properties_proxy = NULL;
190 GError *error = NULL;
191 GVariant *result = NULL;
192 GDBusConnection *g_conn;
194 char *char_handle = NULL;
195 GPtrArray *gp_array = NULL ;
196 GVariantIter *property_iter, *char_iter = NULL;
200 BT_CHECK_PARAMETER(service_handle, return);
201 BT_CHECK_PARAMETER(service, return);
202 BT_CHECK_ENABLED(return);
204 g_conn = _bt_gdbus_get_system_gconn();
205 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
207 properties_proxy = g_dbus_proxy_new_sync(g_conn,
208 G_DBUS_PROXY_FLAGS_NONE, NULL,
211 BT_PROPERTIES_INTERFACE,
214 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
216 result = g_dbus_proxy_call_sync(properties_proxy,
218 g_variant_new("(s)", GATT_SERV_INTERFACE),
219 G_DBUS_CALL_FLAGS_NONE,
226 BT_ERR("Fail to get properties (Error: %s)", error->message);
227 g_clear_error(&error);
229 BT_ERR("Fail to get properties");
230 g_object_unref(properties_proxy);
231 return BLUETOOTH_ERROR_INTERNAL;
234 g_variant_get(result, "(a{sv})", &property_iter);
236 memset(service, 0, sizeof(bt_gatt_service_property_t));
238 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
239 if (!g_strcmp0(key, "UUID")) {
241 service->uuid = g_variant_dup_string(value, &len);
242 _bt_get_uuid_specification_name(service->uuid, &name);
243 BT_INFO("======> Service : %s [%s]", service->uuid, name);
245 } else if (!g_strcmp0(key, "Primary")) {
246 service->primary = g_variant_get_boolean(value);
248 } else if (!g_strcmp0(key, "Includes")) {
249 g_variant_get(value, "ao", &char_iter);
250 gp_array = g_ptr_array_new();
251 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
252 g_ptr_array_add(gp_array, (gpointer)char_handle);
254 if (gp_array->len != 0) {
255 service->include_handles.count = gp_array->len;
256 service->include_handles.handle =
257 __get_string_array_from_gptr_array(gp_array);
259 g_ptr_array_free(gp_array, TRUE);
260 g_variant_iter_free(char_iter);
261 } else if (!g_strcmp0(key, "Characteristics")) {
262 g_variant_get(value, "ao", &char_iter);
263 gp_array = g_ptr_array_new();
264 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
265 g_ptr_array_add(gp_array, (gpointer)char_handle);
267 if (gp_array->len != 0) {
268 service->char_handle.count = gp_array->len;
269 service->char_handle.handle =
270 __get_string_array_from_gptr_array(gp_array);
272 BT_DBG("Characteristics count : %d", service->char_handle.count);
273 g_ptr_array_free(gp_array, TRUE);
274 g_variant_iter_free(char_iter);
278 service->handle = g_strdup(service_handle);
280 g_variant_iter_free(property_iter);
281 g_variant_unref(result);
282 g_object_unref(properties_proxy);
284 return BLUETOOTH_ERROR_NONE;
287 BT_EXPORT_API int bluetooth_gatt_get_primary_services(
288 const bluetooth_device_address_t *address,
289 bt_gatt_handle_info_t *prim_svc)
291 GVariant *result = NULL;
293 GVariantIter *svc_iter;
294 GVariantIter *interface_iter;
295 char *object_path = NULL;
296 char *interface_str = NULL;
297 const gchar *key = NULL;
298 GVariant *value = NULL;
299 GPtrArray *gp_array = NULL;
300 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
301 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
302 int ret = BLUETOOTH_ERROR_INTERNAL;
305 BT_CHECK_PARAMETER(address, return);
306 BT_CHECK_PARAMETER(prim_svc, return);
307 BT_CHECK_ENABLED(return);
309 result = _bt_get_managed_objects();
313 _bt_convert_addr_type_to_string(device_address,
314 (unsigned char *)address->addr);
316 gp_array = g_ptr_array_new();
317 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
319 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
321 if (object_path == NULL)
324 _bt_convert_device_path_to_address(object_path, temp_address);
326 if (g_strcmp0(temp_address, device_address) != 0)
329 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
330 &interface_str, &svc_iter)) {
331 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
334 BT_DBG("Object Path: %s", object_path);
335 while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
336 if (g_strcmp0(key, "Primary") == 0) {
337 if (g_variant_get_boolean(value))
338 g_ptr_array_add(gp_array, (gpointer)object_path);
344 if (gp_array->len == 0) {
345 BT_ERR("gp_array is NULL");
346 ret = BLUETOOTH_ERROR_NOT_FOUND;
348 ret = BLUETOOTH_ERROR_NONE;
349 prim_svc->count = gp_array->len;
350 prim_svc->handle = __get_string_array_from_gptr_array(gp_array);
353 g_ptr_array_free(gp_array, TRUE);
354 g_variant_iter_free(iter);
355 g_variant_unref(result);
360 BT_EXPORT_API int bluetooth_gatt_get_service_from_uuid(bluetooth_device_address_t *address,
361 const char *service_uuid,
362 bt_gatt_service_property_t *service)
364 GVariant *result = NULL;
366 GVariantIter *svc_iter;
367 GVariantIter *interface_iter;
368 char *object_path = NULL;
369 char *interface_str = NULL;
370 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
371 char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
372 int ret = BLUETOOTH_ERROR_INTERNAL;
374 BT_CHECK_PARAMETER(address, return);
375 BT_CHECK_PARAMETER(service_uuid, return);
376 BT_CHECK_PARAMETER(service, return);
377 BT_CHECK_ENABLED(return);
379 result = _bt_get_managed_objects();
383 _bt_convert_addr_type_to_string(device_address,
384 (unsigned char *)address->addr);
386 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
388 while (g_variant_iter_loop(iter, "{oa{sa{sv}}}", &object_path,
390 if (object_path == NULL)
393 _bt_convert_device_path_to_address(object_path,
396 if (g_strcmp0(temp_address, device_address) != 0)
399 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
400 &interface_str, &svc_iter)) {
401 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
404 BT_DBG("Object Path: %s", object_path);
405 ret = bluetooth_gatt_get_service_property(object_path,
408 if (ret != BLUETOOTH_ERROR_NONE) {
409 BT_ERR("Get service property failed(0x%08x)", ret);
411 if (service->primary == TRUE &&
412 g_strstr_len(service->uuid, -1,
414 ret = BLUETOOTH_ERROR_NONE;
418 bluetooth_gatt_free_service_property(service);
423 g_variant_iter_free(iter);
424 g_variant_unref(result);
429 static void __bluetooth_internal_get_char_cb(GDBusProxy *proxy,
430 GAsyncResult *res, gpointer user_data)
433 GVariant *char_value;
434 GVariantIter *char_iter;
435 GPtrArray *gp_array = NULL;
436 bt_gatt_discovered_char_t svc_char = { 0, };
438 GError *error = NULL;
439 bt_user_info_t *user_info;
443 user_info = _bt_get_user_data(BT_COMMON);
444 svc_char.service_handle = user_data;
446 value = g_dbus_proxy_call_finish(proxy, res, &error);
450 BT_ERR("Get service characteristics failed\n errCode[%x],"
451 "message[%s]\n", error->code, error->message);
452 g_clear_error(&error);
454 BT_ERR("Get service characteristics failed\n");
457 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
458 BLUETOOTH_ERROR_INTERNAL, NULL,
459 user_info->cb, user_info->user_data);
461 g_free(svc_char.service_handle);
462 g_object_unref(proxy);
466 g_variant_get(value, "(v)", &char_value);
467 g_variant_get(char_value, "ao", &char_iter);
469 gp_array = g_ptr_array_new();
470 while (g_variant_iter_loop(char_iter, "&o", &char_handle))
471 g_ptr_array_add(gp_array, (gpointer)char_handle);
473 if (gp_array->len != 0) {
474 svc_char.handle_info.count = gp_array->len;
475 svc_char.handle_info.handle =
476 __get_string_array_from_gptr_array(gp_array);
478 g_ptr_array_free(gp_array, TRUE);
481 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DISCOVERED,
482 BLUETOOTH_ERROR_NONE, &svc_char,
483 user_info->cb, user_info->user_data);
486 g_strfreev(svc_char.handle_info.handle);
487 g_free(svc_char.service_handle);
488 g_variant_iter_free(char_iter);
489 g_variant_unref(value);
490 g_object_unref(proxy);
493 BT_EXPORT_API int bluetooth_gatt_discover_service_characteristics(
494 const char *service_handle)
496 GDBusProxy *properties_proxy = NULL;
497 GDBusConnection *g_conn;
498 GError *error = NULL;
503 BT_CHECK_PARAMETER(service_handle, return);
504 BT_CHECK_ENABLED(return);
506 g_conn = _bt_gdbus_get_system_gconn();
507 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
509 properties_proxy = g_dbus_proxy_new_sync(g_conn,
510 G_DBUS_PROXY_FLAGS_NONE, NULL,
513 BT_PROPERTIES_INTERFACE,
516 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
518 handle = g_strdup(service_handle);
519 g_dbus_proxy_call(properties_proxy,
521 g_variant_new("(ss)",
522 GATT_SERV_INTERFACE, "Characteristics"),
523 G_DBUS_CALL_FLAGS_NONE,
526 (GAsyncReadyCallback)__bluetooth_internal_get_char_cb,
530 return BLUETOOTH_ERROR_NONE;
534 static int __get_permission_flag(char *permission)
538 retv_if(permission == NULL, ret);
540 BT_INFO("permission = %s", permission);
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 BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
573 const char *char_handle, bt_gatt_char_property_t *characteristic)
575 GDBusProxy *properties_proxy = NULL;
576 GError *error = NULL;
577 GVariant *value = NULL;
578 GVariant *result = NULL;
579 GByteArray *gb_array = NULL;
580 GPtrArray *gp_array = NULL ;
581 GDBusConnection *g_conn;
585 char *char_desc_handle = NULL;
587 GVariantIter *property_iter;
588 GVariantIter *char_value_iter;
589 GVariantIter *char_perm_iter;
590 GVariantIter *char_desc_iter;
592 BT_CHECK_PARAMETER(char_handle, return);
593 BT_CHECK_PARAMETER(characteristic, return);
595 BT_CHECK_ENABLED(return);
597 g_conn = _bt_gdbus_get_system_gconn();
598 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
600 properties_proxy = g_dbus_proxy_new_sync(g_conn,
601 G_DBUS_PROXY_FLAGS_NONE, NULL,
604 BT_PROPERTIES_INTERFACE,
607 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
609 result = g_dbus_proxy_call_sync(properties_proxy,
611 g_variant_new("(s)", GATT_CHAR_INTERFACE),
612 G_DBUS_CALL_FLAGS_NONE,
619 BT_ERR("Fail to get properties (Error: %s)", error->message);
620 g_clear_error(&error);
622 BT_ERR("Fail to get properties");
623 g_object_unref(properties_proxy);
624 return BLUETOOTH_ERROR_INTERNAL;
627 g_variant_get(result, "(a{sv})", &property_iter);
629 memset(characteristic, 0, sizeof(bt_gatt_char_property_t));
630 characteristic->handle = g_strdup(char_handle);
632 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
633 if (!g_strcmp0(key, "UUID")) {
635 characteristic->uuid = g_variant_dup_string(value, &len);
636 _bt_get_uuid_specification_name(characteristic->uuid, &name);
637 BT_INFO("Characteristic : %s [%s]", characteristic->uuid, name);
639 } else if (!g_strcmp0(key, "Value")) {
640 gb_array = g_byte_array_new();
641 g_variant_get(value, "ay", &char_value_iter);
642 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
643 // BT_DBG("value of char = %d",char_value);
644 g_byte_array_append(gb_array, &char_value, 1);
646 g_variant_iter_free(char_value_iter);
648 if (gb_array->len != 0) {
649 characteristic->val = g_malloc0(gb_array->len *
650 sizeof(unsigned char));
651 memcpy(characteristic->val, gb_array->data, gb_array->len);
653 characteristic->val_len = gb_array->len;
654 g_byte_array_free(gb_array, TRUE);
655 } else if (!g_strcmp0(key, "Flags")) {
656 g_variant_get(value, "as", &char_perm_iter);
657 characteristic->permission = 0x00;
659 while (g_variant_iter_loop(char_perm_iter, "s", &permission))
660 characteristic->permission |= __get_permission_flag(permission);
662 g_variant_iter_free(char_perm_iter);
663 } else if (!g_strcmp0(key, "Descriptors")) {
664 g_variant_get(value, "ao", &char_desc_iter);
665 gp_array = g_ptr_array_new();
666 while (g_variant_iter_loop(char_desc_iter, "&o", &char_desc_handle))
667 g_ptr_array_add(gp_array, (gpointer)char_desc_handle);
669 g_variant_iter_free(char_desc_iter);
670 if (gp_array->len != 0) {
671 characteristic->char_desc_handle.count = gp_array->len;
672 characteristic->char_desc_handle.handle =
673 __get_string_array_from_gptr_array(gp_array);
675 g_ptr_array_free(gp_array, TRUE);
679 g_variant_iter_free(property_iter);
680 g_variant_unref(result);
681 g_object_unref(properties_proxy);
683 return BLUETOOTH_ERROR_NONE;
686 void bluetooth_gatt_get_char_from_uuid_cb(GDBusProxy *proxy,
687 GAsyncResult *res, gpointer user_data)
690 GVariantIter *char_iter;
692 GError *error = NULL;
693 bt_user_info_t *user_info;
694 int ret = BLUETOOTH_ERROR_INTERNAL;
695 bt_gatt_char_property_t characteristic;
697 user_info = _bt_get_user_data(BT_COMMON);
699 value = g_dbus_proxy_call_finish(proxy, res, &error);
703 BT_ERR("Get service characteristics failed\n errCode[%x],"
704 "message[%s]\n", error->code, error->message);
705 g_clear_error(&error);
707 BT_ERR("Get service characteristics failed\n");
710 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID,
711 BLUETOOTH_ERROR_INTERNAL, NULL,
712 user_info->cb, user_info->user_data);
714 g_object_unref(proxy);
719 g_variant_get(value, "(ao)", &char_iter);
721 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
724 ret = bluetooth_gatt_get_characteristics_property(char_handle,
727 if (ret != BLUETOOTH_ERROR_NONE) {
728 BT_ERR("Get characteristic property failed(0x%08x)", ret);
730 if (g_strstr_len(characteristic.uuid, -1, user_data)) {
731 ret = BLUETOOTH_ERROR_NONE;
735 bluetooth_gatt_free_char_property(&characteristic);
739 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_GET_CHAR_FROM_UUID, ret,
740 &characteristic, user_info->cb, user_info->user_data);
743 bluetooth_gatt_free_char_property(&characteristic);
744 g_variant_iter_free(char_iter);
745 g_variant_unref(value);
749 BT_EXPORT_API int bluetooth_gatt_get_char_from_uuid(const char *service_handle,
750 const char *char_uuid)
752 GDBusProxy *properties_proxy = NULL;
753 GDBusConnection *g_conn;
754 GError *error = NULL;
757 BT_CHECK_PARAMETER(service_handle, return);
758 BT_CHECK_PARAMETER(char_uuid, return);
759 BT_CHECK_ENABLED(return);
761 g_conn = _bt_gdbus_get_system_gconn();
762 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
764 properties_proxy = g_dbus_proxy_new_sync(g_conn,
765 G_DBUS_PROXY_FLAGS_NONE, NULL,
768 BT_PROPERTIES_INTERFACE,
771 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
773 uuid = g_strdup(char_uuid);
774 g_dbus_proxy_call(properties_proxy,
776 g_variant_new("(ss)",
777 GATT_SERV_INTERFACE, "Characteristics"),
778 G_DBUS_CALL_FLAGS_NONE,
781 (GAsyncReadyCallback)bluetooth_gatt_get_char_from_uuid_cb,
784 return BLUETOOTH_ERROR_NONE;
787 BT_EXPORT_API int bluetooth_gatt_get_char_descriptor_property(
788 const char *descriptor_handle, bt_gatt_char_descriptor_property_t *descriptor)
790 GDBusProxy *properties_proxy = NULL;
791 GError *error = NULL;
792 GDBusConnection *g_conn;
793 GVariant *result = NULL;
794 GVariantIter *property_iter;
798 GVariant *value = NULL;
799 GByteArray *gb_array = NULL;
800 GVariantIter *desc_value_iter;
802 BT_CHECK_PARAMETER(descriptor_handle, return);
803 BT_CHECK_PARAMETER(descriptor, return);
805 BT_CHECK_ENABLED(return);
807 g_conn = _bt_gdbus_get_system_gconn();
808 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
810 properties_proxy = g_dbus_proxy_new_sync(g_conn,
811 G_DBUS_PROXY_FLAGS_NONE, NULL,
814 BT_PROPERTIES_INTERFACE,
817 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
819 result = g_dbus_proxy_call_sync(properties_proxy,
821 g_variant_new("(s)", GATT_DESC_INTERFACE),
822 G_DBUS_CALL_FLAGS_NONE,
829 BT_ERR("Fail to get properties (Error: %s)", error->message);
830 g_clear_error(&error);
832 BT_ERR("Fail to get properties");
833 g_object_unref(properties_proxy);
834 return BLUETOOTH_ERROR_INTERNAL;
837 g_variant_get(result, "(a{sv})", &property_iter);
839 memset(descriptor, 0, sizeof(bt_gatt_char_descriptor_property_t));
840 descriptor->handle = g_strdup(descriptor_handle);
842 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
843 if (!g_strcmp0(key, "UUID")) {
845 descriptor->uuid = g_variant_dup_string(value, &len);
846 _bt_get_uuid_specification_name(descriptor->uuid, &name);
847 BT_INFO("Descriptor : %s [%s]", descriptor->uuid, name);
849 } else if (!g_strcmp0(key, "Value")) {
850 gb_array = g_byte_array_new();
851 g_variant_get(value, "ay", &desc_value_iter);
852 while (g_variant_iter_loop(desc_value_iter, "y", &char_value)) {
853 BT_DBG("value of descriptor = %d", char_value);
854 g_byte_array_append(gb_array, &char_value, 1);
856 g_variant_iter_free(desc_value_iter);
858 if (gb_array->len != 0) {
859 descriptor->val = g_malloc0(gb_array->len *
860 sizeof(unsigned char));
861 memcpy(descriptor->val, gb_array->data, gb_array->len);
863 descriptor->val_len = gb_array->len;
864 g_byte_array_free(gb_array, TRUE);
868 g_variant_iter_free(property_iter);
869 g_variant_unref(result);
870 g_object_unref(properties_proxy);
872 return BLUETOOTH_ERROR_NONE;
875 static void __bluetooth_internal_read_cb(GObject *source_object,
879 GError *error = NULL;
880 bt_user_info_t *user_info;
881 bt_gatt_char_value_t char_value = { 0, };
882 GDBusConnection *system_gconn = NULL;
884 GByteArray *gp_byte_array = NULL;
887 int ret = BLUETOOTH_ERROR_NONE;
890 user_info = _bt_get_user_data(BT_COMMON);
892 system_gconn = _bt_gdbus_get_system_gconn();
893 value = g_dbus_connection_call_finish(system_gconn, res, &error);
896 BT_ERR("Error : %s \n", error->message);
897 if (g_strrstr(error->message, "Not paired"))
898 ret = BLUETOOTH_ERROR_NOT_PAIRED;
900 ret = BLUETOOTH_ERROR_INTERNAL;
902 g_clear_error(&error);
904 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
906 user_info->cb, user_info->user_data);
912 char_value.char_handle = user_data;
913 gp_byte_array = g_byte_array_new();
914 g_variant_get(value, "(ay)", &iter);
916 while (g_variant_iter_loop(iter, "y", &g_byte))
917 g_byte_array_append(gp_byte_array, &g_byte, 1);
919 if (gp_byte_array->len != 0) {
920 char_value.val_len = gp_byte_array->len;
921 char_value.char_value = gp_byte_array->data;
925 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
926 BLUETOOTH_ERROR_NONE, &char_value,
927 user_info->cb, user_info->user_data);
930 g_free(char_value.char_handle);
931 g_byte_array_free(gp_byte_array, TRUE);
932 g_variant_unref(value);
933 g_variant_iter_free(iter);
938 BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *characteristic)
940 GDBusConnection *conn;
942 GVariantBuilder *builder = NULL;
945 BT_CHECK_PARAMETER(characteristic, return);
946 BT_CHECK_ENABLED(return);
948 conn = _bt_gdbus_get_system_gconn();
949 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
951 handle = g_strdup(characteristic);
953 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
956 g_variant_builder_add(builder, "{sv}", "offset",
957 g_variant_new("q", offset));
959 /* Device Object path*/
960 // g_variant_builder_add(builder, "{sv}", "device",
961 // g_variant_new_object("o", NULL));
963 g_dbus_connection_call(conn,
968 g_variant_new("(a{sv})", builder),
969 G_VARIANT_TYPE("(ay)"),
970 G_DBUS_CALL_FLAGS_NONE,
973 (GAsyncReadyCallback)__bluetooth_internal_read_cb,
975 g_variant_builder_unref(builder);
977 return BLUETOOTH_ERROR_NONE;
980 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
981 const char *char_handle, const guint8 *value, int length)
983 GVariant *val, *options;
984 GVariantBuilder *builder1;
985 GVariantBuilder *builder2;
986 GError *error = NULL;
987 GDBusConnection *conn;
992 BT_CHECK_PARAMETER(char_handle, return);
993 BT_CHECK_PARAMETER(value, return);
994 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
995 BT_CHECK_ENABLED(return);
997 conn = _bt_gdbus_get_system_gconn();
998 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1000 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1001 for (i = 0; i < length; i++)
1002 g_variant_builder_add(builder1, "y", value[i]);
1004 val = g_variant_new("ay", builder1);
1006 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1008 g_variant_builder_add(builder2, "{sv}", "offset",
1009 g_variant_new_uint16(offset));
1011 /* Device Object path*/
1012 // g_variant_builder_add(builder2, "{sv}", "device",
1013 // g_variant_new_object("o", NULL));
1015 options = g_variant_new("a{sv}", builder2);
1017 g_dbus_connection_call_sync(conn,
1020 GATT_CHAR_INTERFACE,
1022 g_variant_new("(@ay@a{sv})", val, options),
1024 G_DBUS_CALL_FLAGS_NONE,
1028 BT_ERR("Set value Failed: %s", error->message);
1029 g_clear_error(&error);
1030 g_variant_builder_unref(builder1);
1031 return BLUETOOTH_ERROR_INTERNAL;
1033 g_variant_builder_unref(builder1);
1034 g_variant_builder_unref(builder2);
1037 return BLUETOOTH_ERROR_NONE;
1040 static void __bluetooth_internal_write_cb(GObject *source_object,
1044 GError *error = NULL;
1045 bt_user_info_t *user_info;
1046 GDBusConnection *system_gconn = NULL;
1048 int result = BLUETOOTH_ERROR_NONE;
1049 guint8 att_ecode = 0;
1051 user_info = _bt_get_user_data(BT_COMMON);
1053 system_gconn = _bt_gdbus_get_system_gconn();
1054 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1057 BT_ERR("Error : %s \n", error->message);
1058 g_clear_error(&error);
1059 result = BLUETOOTH_ERROR_INTERNAL;
1061 g_variant_get(value, "(y)", &att_ecode);
1064 BT_ERR("ATT Error code: %d \n", att_ecode);
1069 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_CHAR,
1071 user_info->cb, user_info->user_data);
1073 BT_ERR("user info is null");
1077 g_variant_unref(value);
1082 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
1083 const char *char_handle, const guint8 *value, int length, guint8 write_type)
1085 GVariant *val, *options;
1086 GVariantBuilder *builder1;
1087 GVariantBuilder *builder2;
1088 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 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1103 for (i = 0; i < length; i++)
1104 g_variant_builder_add(builder1, "y", value[i]);
1106 val = g_variant_new("ay", builder1);
1108 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1110 g_variant_builder_add(builder2, "{sv}", "offset",
1111 g_variant_new_uint16(offset));
1113 /* Device Object path*/
1114 // g_variant_builder_add(builder2, "{sv}", "device",
1115 // g_variant_new_object("o", NULL));
1117 options = g_variant_new("a{sv}", builder2);
1119 g_dbus_connection_call(conn,
1122 GATT_CHAR_INTERFACE,
1124 g_variant_new("(y@ay@a{sv})",
1125 write_type, val, options),
1126 G_VARIANT_TYPE("(y)"),
1127 G_DBUS_CALL_FLAGS_NONE,
1129 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1132 g_variant_builder_unref(builder1);
1133 g_variant_builder_unref(builder2);
1137 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
1138 const char *char_handle, const guint8 *value, int length)
1140 GVariant *val, *options;
1141 GDBusConnection *conn;
1142 GVariantBuilder *builder1;
1143 GVariantBuilder *builder2;
1148 BT_CHECK_PARAMETER(char_handle, return);
1149 BT_CHECK_PARAMETER(value, return);
1150 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1151 BT_CHECK_ENABLED(return);
1153 conn = _bt_gdbus_get_system_gconn();
1154 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1156 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1158 for (i = 0; i < length; i++) {
1159 g_variant_builder_add(builder1, "y", value[i]);
1160 BT_DBG("value [] = %d", value[i]);
1163 val = g_variant_new("ay", builder1);
1165 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1167 g_variant_builder_add(builder2, "{sv}", "offset",
1168 g_variant_new_uint16(offset));
1170 /* Device Object path*/
1171 // g_variant_builder_add(builder2, "{sv}", "device",
1172 // g_variant_new_object("o", NULL));
1174 options = g_variant_new("a{sv}", builder2);
1176 g_dbus_connection_call(conn,
1179 GATT_CHAR_INTERFACE,
1181 g_variant_new("(@ay@a{sv})",
1184 G_DBUS_CALL_FLAGS_NONE,
1186 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1189 g_variant_builder_unref(builder1);
1190 g_variant_builder_unref(builder2);
1193 return BLUETOOTH_ERROR_NONE;
1196 static int __bluetooth_gatt_descriptor_iter(const char *char_handle,
1197 bt_gatt_char_property_t *characteristic)
1200 GDBusProxy *properties_proxy = NULL;
1201 GError *error = NULL;
1202 GVariant *value = NULL;
1203 GVariant *result = NULL;
1204 GDBusConnection *g_conn;
1205 int i, ret = BLUETOOTH_ERROR_NONE;
1206 const char *uuid = NULL;
1208 GVariantIter *desc_value_iter, *property_iter;
1210 char_descriptor_type_t desc_type = TYPE_NONE;
1212 g_conn = _bt_gdbus_get_system_gconn();
1213 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1215 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1216 G_DBUS_PROXY_FLAGS_NONE, NULL,
1219 BT_PROPERTIES_INTERFACE,
1222 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1224 result = g_dbus_proxy_call_sync(properties_proxy,
1226 g_variant_new("(s)", GATT_DESC_INTERFACE),
1227 G_DBUS_CALL_FLAGS_NONE,
1233 if (error != NULL) {
1234 BT_ERR("Fail to get properties (Error: %s)", error->message);
1235 g_clear_error(&error);
1237 BT_ERR("Fail to get properties");
1238 g_object_unref(properties_proxy);
1239 return BLUETOOTH_ERROR_INTERNAL;
1241 g_variant_get(result, "(a{sv})", &property_iter);
1242 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1243 if (!g_strcmp0(key, "UUID")) {
1244 uuid = g_variant_get_string(value, &len);
1245 if (g_strcmp0(uuid, GATT_USER_DESC_UUID) == 0) {
1246 BT_DBG("GATT_USER_DESC_UUID");
1247 desc_type = USER_DESC;
1248 } else if (g_strcmp0(uuid, GATT_CHAR_FORMAT) == 0) {
1249 BT_DBG("GATT_CHAR_FORMAT");
1250 desc_type = CHAR_FORMAT;
1251 } else if (g_strcmp0(uuid, GATT_CHAR_CLIENT_CONF) == 0) {
1252 BT_DBG("GATT_CHAR_CLIENT_CONF");
1253 desc_type = CLIENT_CONF;
1254 } else if (g_strcmp0(uuid, GATT_CHAR_SERVER_CONF) == 0) {
1255 BT_DBG("GATT_CHAR_SERVER_CONF");
1256 desc_type = SERVER_CONF;
1258 BT_DBG("descriptor uuid = %s", uuid);
1260 } else if (!g_strcmp0(key, "Value")) {
1261 switch (desc_type) {
1263 BT_DBG("Format descriptor");
1264 g_variant_get(value, "(yyqyq)",
1265 &(characteristic->format.format),
1266 &(characteristic->format.exponent),
1267 &(characteristic->format.unit),
1268 &(characteristic->format.name_space),
1269 &(characteristic->format.description));
1272 BT_DBG("User descriptor");
1273 g_variant_get(value, "ay", &desc_value_iter);
1274 len = g_variant_get_size((GVariant *)desc_value_iter);
1277 characteristic->description = (char *)g_malloc0(len + 1);
1279 for (i = 0; i < len; i++) {
1280 g_variant_iter_loop(desc_value_iter, "y",
1281 &characteristic->description[i]);
1282 BT_DBG("description = %s", characteristic->description);
1284 g_variant_iter_free(desc_value_iter);
1287 BT_DBG(" CLIENT_CONF");
1290 BT_DBG(" SERVER_CONF");
1298 g_variant_iter_free(property_iter);
1299 g_variant_unref(result);
1300 g_object_unref(properties_proxy);
1307 static void bluetooth_gatt_get_char_desc_cb(GDBusProxy *proxy,
1308 GAsyncResult *res, gpointer user_data)
1312 GVariant *char_value;
1313 GVariantIter *char_iter;
1315 GError *error = NULL;
1316 bt_user_info_t *user_info;
1317 bt_gatt_char_property_t characteristic = {0, };
1318 int ret = BLUETOOTH_ERROR_INTERNAL;
1320 user_info = _bt_get_user_data(BT_COMMON);
1322 value = g_dbus_proxy_call_finish(proxy, res, &error);
1324 if (value == NULL) {
1325 if (error != NULL) {
1326 BT_ERR("Get characteristic descriptor failed\n errCode[%x],"
1327 "message[%s]\n", error->code, error->message);
1328 g_clear_error(&error);
1330 BT_ERR("Get characteristic descriptor failed\n");
1333 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1334 BLUETOOTH_ERROR_INTERNAL, NULL,
1335 user_info->cb, user_info->user_data);
1338 g_object_unref(proxy);
1342 g_variant_get(value, "(v)", &char_value);
1343 g_variant_get(char_value, "ao", &char_iter);
1345 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
1346 BT_DBG("object path of descriptor = %s", char_handle);
1348 ret = __bluetooth_gatt_descriptor_iter(char_handle,
1350 BT_DBG("Descriptor read status [%d]", ret);
1354 characteristic.handle = user_data;
1356 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1357 ret, &characteristic, user_info->cb, user_info->user_data);
1359 bluetooth_gatt_free_char_property(&characteristic);
1361 g_variant_iter_free(char_iter);
1362 g_variant_unref(value);
1366 BT_EXPORT_API int bluetooth_gatt_discover_characteristic_descriptor(
1367 const char *characteristic_handle)
1369 GDBusProxy *properties_proxy = NULL;
1370 GDBusConnection *g_conn;
1372 GError *error = NULL;
1374 BT_CHECK_PARAMETER(characteristic_handle, return);
1375 BT_CHECK_ENABLED(return);
1377 g_conn = _bt_gdbus_get_system_gconn();
1378 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1380 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1381 G_DBUS_PROXY_FLAGS_NONE, NULL,
1383 characteristic_handle,
1384 BT_PROPERTIES_INTERFACE,
1387 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1389 handle = g_strdup(characteristic_handle);
1390 g_dbus_proxy_call(properties_proxy,
1392 g_variant_new("(ss)",
1393 GATT_CHAR_INTERFACE, "Descriptors"),
1394 G_DBUS_CALL_FLAGS_NONE,
1396 (GAsyncReadyCallback)bluetooth_gatt_get_char_desc_cb,
1399 return BLUETOOTH_ERROR_NONE;
1402 static void __bluetooth_internal_read_desc_cb(GObject *source_object,
1406 GError *error = NULL;
1407 bt_user_info_t *user_info;
1408 bt_gatt_char_property_t char_value = { 0, };
1409 GDBusConnection *system_gconn = NULL;
1411 GByteArray *gp_byte_array = NULL;
1416 user_info = _bt_get_user_data(BT_COMMON);
1417 system_gconn = _bt_gdbus_get_system_gconn();
1419 char_value.handle = user_data;
1420 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1423 BT_ERR("Error : %s \n", error->message);
1424 g_clear_error(&error);
1426 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1427 BLUETOOTH_ERROR_INTERNAL, NULL,
1428 user_info->cb, user_info->user_data);
1430 g_free(char_value.handle);
1434 gp_byte_array = g_byte_array_new();
1435 g_variant_get(value, "(ay)", &iter);
1437 while (g_variant_iter_loop(iter, "y", &g_byte))
1438 g_byte_array_append(gp_byte_array, &g_byte, 1);
1440 if (gp_byte_array->len != 0) {
1441 char_value.val_len = (unsigned int)gp_byte_array->len;
1442 char_value.description = (char *)gp_byte_array->data;
1446 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1447 BLUETOOTH_ERROR_NONE, &char_value,
1448 user_info->cb, user_info->user_data);
1451 g_byte_array_free(gp_byte_array, TRUE);
1452 g_free(char_value.handle);
1453 g_variant_unref(value);
1454 g_variant_iter_free(iter);
1459 BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *char_descriptor)
1461 GDBusConnection *conn;
1462 GVariantBuilder *builder;
1467 BT_CHECK_PARAMETER(char_descriptor, return);
1468 BT_CHECK_ENABLED(return);
1470 conn = _bt_gdbus_get_system_gconn();
1471 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1473 handle = g_strdup(char_descriptor);
1475 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1478 g_variant_builder_add(builder, "{sv}", "offset",
1479 g_variant_new("q", offset));
1480 /* Device Object path*/
1481 // g_variant_builder_add(builder, "{sv}", "device",
1482 // g_variant_new("o", serv_info->serv_path));
1484 g_dbus_connection_call(conn,
1487 GATT_DESC_INTERFACE,
1489 g_variant_new("(a{sv})", builder),
1490 G_VARIANT_TYPE("(ay)"),
1491 G_DBUS_CALL_FLAGS_NONE,
1494 (GAsyncReadyCallback)__bluetooth_internal_read_desc_cb,
1496 g_variant_builder_unref(builder);
1499 return BLUETOOTH_ERROR_NONE;
1502 static void __bluetooth_internal_write_desc_cb(GObject *source_object,
1506 GError *error = NULL;
1507 bt_user_info_t *user_info;
1508 GDBusConnection *system_gconn = NULL;
1510 int result = BLUETOOTH_ERROR_NONE;
1511 guint8 att_ecode = 0;
1514 user_info = _bt_get_user_data(BT_COMMON);
1516 system_gconn = _bt_gdbus_get_system_gconn();
1517 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1520 BT_ERR("Error : %s \n", error->message);
1521 g_clear_error(&error);
1522 result = BLUETOOTH_ERROR_INTERNAL;
1524 g_variant_get(value, "(y)", &att_ecode);
1527 BT_ERR("ATT Error code: %d \n", att_ecode);
1532 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_DESC,
1534 user_info->cb, user_info->user_data);
1538 g_variant_unref(value);
1543 BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(
1544 const char *desc_handle, const guint8 *value, int length)
1546 GVariant *val, *options;
1547 GDBusConnection *conn;
1548 GVariantBuilder *builder1;
1549 GVariantBuilder *builder2;
1554 BT_CHECK_PARAMETER(desc_handle, return);
1555 BT_CHECK_PARAMETER(value, return);
1556 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1557 BT_CHECK_ENABLED(return);
1559 conn = _bt_gdbus_get_system_gconn();
1560 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1562 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1564 for (i = 0; i < length; i++)
1565 g_variant_builder_add(builder1, "y", value[i]);
1567 val = g_variant_new("ay", builder1);
1569 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1571 g_variant_builder_add(builder2, "{sv}", "offset",
1572 g_variant_new_uint16(offset));
1574 /* Device Object path*/
1575 // g_variant_builder_add(builder2, "{sv}", "device",
1576 // g_variant_new_object("o", NULL));
1578 options = g_variant_new("a{sv}", builder2);
1580 g_dbus_connection_call(conn,
1583 GATT_DESC_INTERFACE,
1585 g_variant_new("(@ay@a{sv})",
1587 G_VARIANT_TYPE("(y)"),
1588 G_DBUS_CALL_FLAGS_NONE,
1590 (GAsyncReadyCallback)__bluetooth_internal_write_desc_cb,
1593 g_variant_builder_unref(builder1);
1594 g_variant_builder_unref(builder2);
1597 return BLUETOOTH_ERROR_NONE;
1600 BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle)
1602 GDBusConnection *conn;
1603 GError *error = NULL;
1604 int ret = BLUETOOTH_ERROR_NONE;
1606 BT_CHECK_PARAMETER(char_handle, return);
1607 BT_CHECK_ENABLED(return);
1609 BT_INFO_C("### Enable CCCD : %s", char_handle);
1611 conn = _bt_gdbus_get_system_gconn();
1612 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1614 g_dbus_connection_call_sync(conn,
1617 GATT_CHAR_INTERFACE,
1621 G_DBUS_CALL_FLAGS_NONE,
1622 GATT_DEFAULT_TIMEOUT, NULL, &error);
1625 g_dbus_error_strip_remote_error(error);
1626 BT_ERR_C("### Watch Failed: %s", error->message);
1627 if (g_strrstr(error->message, "Already notifying"))
1628 ret = BLUETOOTH_ERROR_NONE;
1629 else if (g_strrstr(error->message, "In Progress"))
1630 ret = BLUETOOTH_ERROR_IN_PROGRESS;
1631 else if (g_strrstr(error->message, "Operation is not supported"))
1632 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
1633 /*failed because of either Insufficient Authorization or Write Not Permitted */
1634 else if (g_strrstr(error->message, "Write not permitted") ||
1635 g_strrstr(error->message, "Operation Not Authorized"))
1636 ret = BLUETOOTH_ERROR_PERMISSION_DEINED;
1637 /* failed because of either Insufficient Authentication,
1638 Insufficient Encryption Key Size, or Insufficient Encryption. */
1639 else if (g_strrstr(error->message, "Not paired"))
1640 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1642 ret = BLUETOOTH_ERROR_INTERNAL;
1644 g_clear_error(&error);
1650 BT_EXPORT_API int bluetooth_gatt_unwatch_characteristics(const char *char_handle)
1653 GDBusConnection *conn;
1654 GError *error = NULL;
1655 int ret = BLUETOOTH_ERROR_NONE;
1656 BT_CHECK_PARAMETER(char_handle, return);
1658 BT_CHECK_ENABLED(return);
1660 BT_INFO("Disable CCCD : %s", char_handle);
1662 conn = _bt_gdbus_get_system_gconn();
1663 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1665 g_dbus_connection_call_sync(conn,
1668 GATT_CHAR_INTERFACE,
1672 G_DBUS_CALL_FLAGS_NONE,
1673 GATT_DEFAULT_TIMEOUT, NULL, &error);
1676 BT_ERR("Watch Failed: %s", error->message);
1677 g_clear_error(&error);
1678 ret = BLUETOOTH_ERROR_INTERNAL;