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 int __bluetooth_get_att_error_code(GError *error)
881 BT_ERR("Error : %s", error->message);
882 str = g_strrstr(error->message, "ATT error: 0x");
885 att_ecode = g_ascii_xdigit_value(str[len-2]) << 4;
886 att_ecode += g_ascii_xdigit_value(str[len-1]);
888 return BLUETOOTH_ATT_ERROR_INTERNAL;
891 case BLUETOOTH_ATT_ERROR_READ_NOT_PERMITTED:
892 BT_ERR("Read not permitted");
894 case BLUETOOTH_ATT_ERROR_WRITE_NOT_PERMITTED:
895 BT_ERR("Write not permitted");
897 case BLUETOOTH_ATT_ERROR_AUTHENTICATION:
899 case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION:
900 case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
901 BT_ERR("Not paired");
903 case BLUETOOTH_ATT_ERROR_INVALID_OFFSET:
904 BT_ERR("Invalid offset");
906 case BLUETOOTH_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN:
907 BT_ERR("Invalid Length");
909 case BLUETOOTH_ATT_ERROR_AUTHORIZATION:
910 BT_ERR("Operation not Authorized");
914 if (att_ecode >= 0x80 && att_ecode <= 0x9F)
915 BT_ERR("Application error");
920 static void __bluetooth_internal_read_cb(GObject *source_object,
924 GError *error = NULL;
925 bt_user_info_t *user_info;
926 bt_gatt_char_value_t char_value = { 0, };
927 GDBusConnection *system_gconn = NULL;
929 GByteArray *gp_byte_array = NULL;
935 user_info = _bt_get_user_data(BT_COMMON);
937 system_gconn = _bt_gdbus_get_system_gconn();
938 value = g_dbus_connection_call_finish(system_gconn, res, &error);
939 char_value.char_handle = user_data;
942 att_ecode = __bluetooth_get_att_error_code(error);
943 g_clear_error(&error);
945 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
947 user_info->cb, user_info->user_data);
949 g_free(char_value.char_handle);
950 g_variant_unref(value);
954 g_variant_get(value, "(ay)", &iter);
956 gp_byte_array = g_byte_array_new();
957 while (g_variant_iter_loop(iter, "y", &g_byte))
958 g_byte_array_append(gp_byte_array, &g_byte, 1);
960 if (gp_byte_array->len != 0) {
961 char_value.val_len = gp_byte_array->len;
962 char_value.char_value = gp_byte_array->data;
966 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_CHAR,
967 BLUETOOTH_ATT_ERROR_NONE, &char_value,
968 user_info->cb, user_info->user_data);
971 g_free(char_value.char_handle);
972 g_byte_array_free(gp_byte_array, TRUE);
973 g_variant_unref(value);
974 g_variant_iter_free(iter);
979 BT_EXPORT_API int bluetooth_gatt_read_characteristic_value(const char *characteristic)
981 GDBusConnection *conn;
983 GVariantBuilder *builder = NULL;
986 BT_CHECK_PARAMETER(characteristic, return);
987 BT_CHECK_ENABLED(return);
989 conn = _bt_gdbus_get_system_gconn();
990 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
992 handle = g_strdup(characteristic);
994 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
997 g_variant_builder_add(builder, "{sv}", "offset",
998 g_variant_new("q", offset));
1000 /* Device Object path*/
1001 // g_variant_builder_add(builder, "{sv}", "device",
1002 // g_variant_new_object("o", NULL));
1004 g_dbus_connection_call(conn,
1007 GATT_CHAR_INTERFACE,
1009 g_variant_new("(a{sv})", builder),
1010 G_VARIANT_TYPE("(ay)"),
1011 G_DBUS_CALL_FLAGS_NONE,
1014 (GAsyncReadyCallback)__bluetooth_internal_read_cb,
1016 g_variant_builder_unref(builder);
1018 return BLUETOOTH_ERROR_NONE;
1021 static void __bluetooth_internal_write_cb(GObject *source_object,
1025 GError *error = NULL;
1026 bt_user_info_t *user_info;
1027 GDBusConnection *system_gconn = NULL;
1029 int att_ecode = BLUETOOTH_ATT_ERROR_NONE;
1031 user_info = _bt_get_user_data(BT_COMMON);
1033 system_gconn = _bt_gdbus_get_system_gconn();
1034 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1037 att_ecode = __bluetooth_get_att_error_code(error);
1038 g_clear_error(&error);
1042 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_CHAR,
1044 user_info->cb, user_info->user_data);
1046 BT_ERR("user info is null");
1050 g_variant_unref(value);
1055 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value(
1056 const char *char_handle, const guint8 *value, int length)
1058 GVariant *val, *options;
1059 GVariantBuilder *builder1;
1060 GVariantBuilder *builder2;
1061 GError *error = NULL;
1062 GDBusConnection *conn;
1067 BT_CHECK_PARAMETER(char_handle, return);
1068 BT_CHECK_PARAMETER(value, return);
1069 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1070 BT_CHECK_ENABLED(return);
1072 conn = _bt_gdbus_get_system_gconn();
1073 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1075 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1076 for (i = 0; i < length; i++)
1077 g_variant_builder_add(builder1, "y", value[i]);
1079 val = g_variant_new("ay", builder1);
1081 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1083 g_variant_builder_add(builder2, "{sv}", "offset",
1084 g_variant_new_uint16(offset));
1086 /* Device Object path*/
1087 // g_variant_builder_add(builder2, "{sv}", "device",
1088 // g_variant_new_object("o", NULL));
1090 options = g_variant_new("a{sv}", builder2);
1092 g_dbus_connection_call(conn,
1095 GATT_CHAR_INTERFACE,
1097 g_variant_new("(@ay@a{sv})",
1100 G_DBUS_CALL_FLAGS_NONE,
1102 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1107 BT_ERR("Set value Failed: %s", error->message);
1108 g_clear_error(&error);
1109 g_variant_builder_unref(builder1);
1110 return BLUETOOTH_ERROR_INTERNAL;
1112 g_variant_builder_unref(builder1);
1113 g_variant_builder_unref(builder2);
1116 return BLUETOOTH_ERROR_NONE;
1119 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_by_type(
1120 const char *char_handle, const guint8 *value, int length, guint8 write_type)
1122 GVariant *val, *options;
1123 GVariantBuilder *builder1;
1124 GVariantBuilder *builder2;
1125 GDBusConnection *conn;
1128 int ret = BLUETOOTH_ERROR_NONE;
1130 BT_CHECK_PARAMETER(char_handle, return);
1131 BT_CHECK_PARAMETER(value, return);
1132 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1133 BT_CHECK_ENABLED_INTERNAL(return);
1135 conn = _bt_gdbus_get_system_gconn();
1136 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1138 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1140 for (i = 0; i < length; i++)
1141 g_variant_builder_add(builder1, "y", value[i]);
1143 val = g_variant_new("ay", builder1);
1145 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1147 g_variant_builder_add(builder2, "{sv}", "offset",
1148 g_variant_new_uint16(offset));
1150 /* Device Object path*/
1151 // g_variant_builder_add(builder2, "{sv}", "device",
1152 // g_variant_new_object("o", NULL));
1154 options = g_variant_new("a{sv}", builder2);
1156 g_dbus_connection_call(conn,
1159 GATT_CHAR_INTERFACE,
1161 g_variant_new("(y@ay@a{sv})",
1162 write_type, val, options),
1164 G_DBUS_CALL_FLAGS_NONE,
1166 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1169 g_variant_builder_unref(builder1);
1170 g_variant_builder_unref(builder2);
1174 BT_EXPORT_API int bluetooth_gatt_set_characteristics_value_request(
1175 const char *char_handle, const guint8 *value, int length)
1177 GVariant *val, *options;
1178 GDBusConnection *conn;
1179 GVariantBuilder *builder1;
1180 GVariantBuilder *builder2;
1185 BT_CHECK_PARAMETER(char_handle, return);
1186 BT_CHECK_PARAMETER(value, return);
1187 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1188 BT_CHECK_ENABLED(return);
1190 conn = _bt_gdbus_get_system_gconn();
1191 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1193 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1195 for (i = 0; i < length; i++) {
1196 g_variant_builder_add(builder1, "y", value[i]);
1197 BT_DBG("value [] = %d", value[i]);
1200 val = g_variant_new("ay", builder1);
1202 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1204 g_variant_builder_add(builder2, "{sv}", "offset",
1205 g_variant_new_uint16(offset));
1207 /* Device Object path*/
1208 // g_variant_builder_add(builder2, "{sv}", "device",
1209 // g_variant_new_object("o", NULL));
1211 options = g_variant_new("a{sv}", builder2);
1213 g_dbus_connection_call(conn,
1216 GATT_CHAR_INTERFACE,
1218 g_variant_new("(@ay@a{sv})",
1221 G_DBUS_CALL_FLAGS_NONE,
1223 (GAsyncReadyCallback)__bluetooth_internal_write_cb,
1226 g_variant_builder_unref(builder1);
1227 g_variant_builder_unref(builder2);
1230 return BLUETOOTH_ERROR_NONE;
1233 static int __bluetooth_gatt_descriptor_iter(const char *char_handle,
1234 bt_gatt_char_property_t *characteristic)
1237 GDBusProxy *properties_proxy = NULL;
1238 GError *error = NULL;
1239 GVariant *value = NULL;
1240 GVariant *result = NULL;
1241 GDBusConnection *g_conn;
1242 int i, ret = BLUETOOTH_ERROR_NONE;
1243 const char *uuid = NULL;
1245 GVariantIter *desc_value_iter, *property_iter;
1247 char_descriptor_type_t desc_type = TYPE_NONE;
1249 g_conn = _bt_gdbus_get_system_gconn();
1250 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1252 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1253 G_DBUS_PROXY_FLAGS_NONE, NULL,
1256 BT_PROPERTIES_INTERFACE,
1259 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1261 result = g_dbus_proxy_call_sync(properties_proxy,
1263 g_variant_new("(s)", GATT_DESC_INTERFACE),
1264 G_DBUS_CALL_FLAGS_NONE,
1270 if (error != NULL) {
1271 BT_ERR("Fail to get properties (Error: %s)", error->message);
1272 g_clear_error(&error);
1274 BT_ERR("Fail to get properties");
1275 g_object_unref(properties_proxy);
1276 return BLUETOOTH_ERROR_INTERNAL;
1278 g_variant_get(result, "(a{sv})", &property_iter);
1279 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1280 if (!g_strcmp0(key, "UUID")) {
1281 uuid = g_variant_get_string(value, &len);
1282 if (g_strcmp0(uuid, GATT_USER_DESC_UUID) == 0) {
1283 BT_DBG("GATT_USER_DESC_UUID");
1284 desc_type = USER_DESC;
1285 } else if (g_strcmp0(uuid, GATT_CHAR_FORMAT) == 0) {
1286 BT_DBG("GATT_CHAR_FORMAT");
1287 desc_type = CHAR_FORMAT;
1288 } else if (g_strcmp0(uuid, GATT_CHAR_CLIENT_CONF) == 0) {
1289 BT_DBG("GATT_CHAR_CLIENT_CONF");
1290 desc_type = CLIENT_CONF;
1291 } else if (g_strcmp0(uuid, GATT_CHAR_SERVER_CONF) == 0) {
1292 BT_DBG("GATT_CHAR_SERVER_CONF");
1293 desc_type = SERVER_CONF;
1295 BT_DBG("descriptor uuid = %s", uuid);
1297 } else if (!g_strcmp0(key, "Value")) {
1298 switch (desc_type) {
1300 BT_DBG("Format descriptor");
1301 g_variant_get(value, "(yyqyq)",
1302 &(characteristic->format.format),
1303 &(characteristic->format.exponent),
1304 &(characteristic->format.unit),
1305 &(characteristic->format.name_space),
1306 &(characteristic->format.description));
1309 BT_DBG("User descriptor");
1310 g_variant_get(value, "ay", &desc_value_iter);
1311 len = g_variant_get_size((GVariant *)desc_value_iter);
1314 characteristic->description = (char *)g_malloc0(len + 1);
1316 for (i = 0; i < len; i++) {
1317 g_variant_iter_loop(desc_value_iter, "y",
1318 &characteristic->description[i]);
1319 BT_DBG("description = %s", characteristic->description);
1321 g_variant_iter_free(desc_value_iter);
1324 BT_DBG(" CLIENT_CONF");
1327 BT_DBG(" SERVER_CONF");
1335 g_variant_iter_free(property_iter);
1336 g_variant_unref(result);
1337 g_object_unref(properties_proxy);
1344 static void bluetooth_gatt_get_char_desc_cb(GDBusProxy *proxy,
1345 GAsyncResult *res, gpointer user_data)
1349 GVariant *char_value;
1350 GVariantIter *char_iter;
1352 GError *error = NULL;
1353 bt_user_info_t *user_info;
1354 bt_gatt_char_property_t characteristic = {0, };
1355 int ret = BLUETOOTH_ERROR_INTERNAL;
1357 user_info = _bt_get_user_data(BT_COMMON);
1359 value = g_dbus_proxy_call_finish(proxy, res, &error);
1360 characteristic.handle = user_data;
1362 if (value == NULL) {
1363 if (error != NULL) {
1364 BT_ERR("Get characteristic descriptor failed\n errCode[%x],"
1365 "message[%s]\n", error->code, error->message);
1366 g_clear_error(&error);
1368 BT_ERR("Get characteristic descriptor failed\n");
1371 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1372 BLUETOOTH_ERROR_INTERNAL, NULL,
1373 user_info->cb, user_info->user_data);
1375 g_free(characteristic.handle);
1376 g_object_unref(proxy);
1380 g_variant_get(value, "(v)", &char_value);
1381 g_variant_get(char_value, "ao", &char_iter);
1383 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
1384 BT_DBG("object path of descriptor = %s", char_handle);
1386 ret = __bluetooth_gatt_descriptor_iter(char_handle,
1388 BT_DBG("Descriptor read status [%d]", ret);
1393 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_SVC_CHAR_DESC_DISCOVERED,
1394 ret, &characteristic, user_info->cb, user_info->user_data);
1396 bluetooth_gatt_free_char_property(&characteristic);
1398 g_free(characteristic.handle);
1399 g_variant_iter_free(char_iter);
1400 g_variant_unref(value);
1404 BT_EXPORT_API int bluetooth_gatt_discover_characteristic_descriptor(
1405 const char *characteristic_handle)
1407 GDBusProxy *properties_proxy = NULL;
1408 GDBusConnection *g_conn;
1410 GError *error = NULL;
1412 BT_CHECK_PARAMETER(characteristic_handle, return);
1413 BT_CHECK_ENABLED(return);
1415 g_conn = _bt_gdbus_get_system_gconn();
1416 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1418 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1419 G_DBUS_PROXY_FLAGS_NONE, NULL,
1421 characteristic_handle,
1422 BT_PROPERTIES_INTERFACE,
1425 retv_if(properties_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1427 handle = g_strdup(characteristic_handle);
1428 g_dbus_proxy_call(properties_proxy,
1430 g_variant_new("(ss)",
1431 GATT_CHAR_INTERFACE, "Descriptors"),
1432 G_DBUS_CALL_FLAGS_NONE,
1434 (GAsyncReadyCallback)bluetooth_gatt_get_char_desc_cb,
1437 return BLUETOOTH_ERROR_NONE;
1440 static void __bluetooth_internal_read_desc_cb(GObject *source_object,
1444 GError *error = NULL;
1445 bt_user_info_t *user_info;
1446 bt_gatt_char_property_t char_value = { 0, };
1447 GDBusConnection *system_gconn = NULL;
1449 GByteArray *gp_byte_array = NULL;
1455 user_info = _bt_get_user_data(BT_COMMON);
1456 system_gconn = _bt_gdbus_get_system_gconn();
1458 char_value.handle = user_data;
1459 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1462 att_ecode = __bluetooth_get_att_error_code(error);
1463 g_clear_error(&error);
1465 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1467 user_info->cb, user_info->user_data);
1469 g_free(char_value.handle);
1470 g_variant_unref(value);
1474 g_variant_get(value, "(ay)", &iter);
1476 gp_byte_array = g_byte_array_new();
1477 while (g_variant_iter_loop(iter, "y", &g_byte))
1478 g_byte_array_append(gp_byte_array, &g_byte, 1);
1480 if (gp_byte_array->len != 0) {
1481 char_value.val_len = (unsigned int)gp_byte_array->len;
1482 char_value.description = (char *)gp_byte_array->data;
1486 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_READ_DESC,
1487 BLUETOOTH_ATT_ERROR_NONE, &char_value,
1488 user_info->cb, user_info->user_data);
1491 g_byte_array_free(gp_byte_array, TRUE);
1492 g_free(char_value.handle);
1493 g_variant_unref(value);
1494 g_variant_iter_free(iter);
1499 BT_EXPORT_API int bluetooth_gatt_read_descriptor_value(const char *char_descriptor)
1501 GDBusConnection *conn;
1502 GVariantBuilder *builder;
1507 BT_CHECK_PARAMETER(char_descriptor, return);
1508 BT_CHECK_ENABLED(return);
1510 conn = _bt_gdbus_get_system_gconn();
1511 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1513 handle = g_strdup(char_descriptor);
1515 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1518 g_variant_builder_add(builder, "{sv}", "offset",
1519 g_variant_new("q", offset));
1520 /* Device Object path*/
1521 // g_variant_builder_add(builder, "{sv}", "device",
1522 // g_variant_new("o", serv_info->serv_path));
1524 g_dbus_connection_call(conn,
1527 GATT_DESC_INTERFACE,
1529 g_variant_new("(a{sv})", builder),
1530 G_VARIANT_TYPE("(ay)"),
1531 G_DBUS_CALL_FLAGS_NONE,
1534 (GAsyncReadyCallback)__bluetooth_internal_read_desc_cb,
1536 g_variant_builder_unref(builder);
1539 return BLUETOOTH_ERROR_NONE;
1542 static void __bluetooth_internal_write_desc_cb(GObject *source_object,
1546 GError *error = NULL;
1547 bt_user_info_t *user_info;
1548 GDBusConnection *system_gconn = NULL;
1550 int att_ecode = BLUETOOTH_ATT_ERROR_NONE;
1553 user_info = _bt_get_user_data(BT_COMMON);
1555 system_gconn = _bt_gdbus_get_system_gconn();
1556 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1559 att_ecode = __bluetooth_get_att_error_code(error);
1560 g_clear_error(&error);
1564 _bt_common_event_cb(BLUETOOTH_EVENT_GATT_WRITE_DESC,
1566 user_info->cb, user_info->user_data);
1570 g_variant_unref(value);
1575 BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(
1576 const char *desc_handle, const guint8 *value, int length)
1578 GVariant *val, *options;
1579 GDBusConnection *conn;
1580 GVariantBuilder *builder1;
1581 GVariantBuilder *builder2;
1586 BT_CHECK_PARAMETER(desc_handle, return);
1587 BT_CHECK_PARAMETER(value, return);
1588 retv_if(length == 0, BLUETOOTH_ERROR_INVALID_PARAM);
1589 BT_CHECK_ENABLED(return);
1591 conn = _bt_gdbus_get_system_gconn();
1592 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1594 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1596 for (i = 0; i < length; i++)
1597 g_variant_builder_add(builder1, "y", value[i]);
1599 val = g_variant_new("ay", builder1);
1601 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1603 g_variant_builder_add(builder2, "{sv}", "offset",
1604 g_variant_new_uint16(offset));
1606 /* Device Object path*/
1607 // g_variant_builder_add(builder2, "{sv}", "device",
1608 // g_variant_new_object("o", NULL));
1610 options = g_variant_new("a{sv}", builder2);
1612 g_dbus_connection_call(conn,
1615 GATT_DESC_INTERFACE,
1617 g_variant_new("(@ay@a{sv})",
1620 G_DBUS_CALL_FLAGS_NONE,
1622 (GAsyncReadyCallback)__bluetooth_internal_write_desc_cb,
1625 g_variant_builder_unref(builder1);
1626 g_variant_builder_unref(builder2);
1629 return BLUETOOTH_ERROR_NONE;
1632 BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle)
1634 GDBusConnection *conn;
1635 GError *error = NULL;
1636 int ret = BLUETOOTH_ERROR_NONE;
1638 BT_CHECK_PARAMETER(char_handle, return);
1639 BT_CHECK_ENABLED(return);
1641 BT_INFO_C("### Enable CCCD : %s", char_handle);
1643 conn = _bt_gdbus_get_system_gconn();
1644 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1646 g_dbus_connection_call_sync(conn,
1649 GATT_CHAR_INTERFACE,
1653 G_DBUS_CALL_FLAGS_NONE,
1654 GATT_DEFAULT_TIMEOUT, NULL, &error);
1657 g_dbus_error_strip_remote_error(error);
1658 BT_ERR_C("### Watch Failed: %s", error->message);
1659 if (g_strrstr(error->message, "Already notifying"))
1660 ret = BLUETOOTH_ERROR_NONE;
1661 else if (g_strrstr(error->message, "In Progress"))
1662 ret = BLUETOOTH_ERROR_IN_PROGRESS;
1663 else if (g_strrstr(error->message, "Operation is not supported"))
1664 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
1665 /*failed because of either Insufficient Authorization or Write Not Permitted */
1666 else if (g_strrstr(error->message, "Write not permitted") ||
1667 g_strrstr(error->message, "Operation Not Authorized"))
1668 ret = BLUETOOTH_ERROR_PERMISSION_DEINED;
1669 /* failed because of either Insufficient Authentication,
1670 Insufficient Encryption Key Size, or Insufficient Encryption. */
1671 else if (g_strrstr(error->message, "Not paired"))
1672 ret = BLUETOOTH_ERROR_NOT_PAIRED;
1674 ret = BLUETOOTH_ERROR_INTERNAL;
1676 g_clear_error(&error);
1682 BT_EXPORT_API int bluetooth_gatt_unwatch_characteristics(const char *char_handle)
1685 GDBusConnection *conn;
1686 GError *error = NULL;
1687 int ret = BLUETOOTH_ERROR_NONE;
1688 BT_CHECK_PARAMETER(char_handle, return);
1690 BT_CHECK_ENABLED(return);
1692 BT_INFO("Disable CCCD : %s", char_handle);
1694 conn = _bt_gdbus_get_system_gconn();
1695 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1697 g_dbus_connection_call_sync(conn,
1700 GATT_CHAR_INTERFACE,
1704 G_DBUS_CALL_FLAGS_NONE,
1705 GATT_DEFAULT_TIMEOUT, NULL, &error);
1708 BT_ERR("Watch Failed: %s", error->message);
1709 g_clear_error(&error);
1710 ret = BLUETOOTH_ERROR_INTERNAL;