Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-client.c
index 9819f1e..ba8f797 100644 (file)
@@ -107,28 +107,12 @@ gboolean _bluetooth_gatt_check_service_change_watcher_address(
                const bluetooth_device_address_t *device_addr)
 {
        GSList *l;
-       char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
-       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
-
-       _bt_convert_addr_type_to_string(device_address,
-                       (unsigned char *)device_addr->addr);
 
        for (l = service_monitor_list; l != NULL; l = l->next) {
-               char device_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
-               char secure_address2[BT_ADDRESS_STRING_SIZE] = { 0 };
                bluetooth_device_address_t *addr = l->data;
 
-               _bt_convert_addr_type_to_string(device_address2,
-                               (unsigned char *)addr->addr);
-               _bt_convert_addr_string_to_secure_string(secure_address,
-                               device_address);
-               _bt_convert_addr_string_to_secure_string(secure_address2,
-                               device_address2);
-               BT_INFO("service_monitor_list [%s] - Input [%s]",
-                               secure_address2, secure_address);
-
                if (!memcmp(device_addr, addr,
-                               sizeof(bluetooth_device_address_t)))
+                           sizeof(bluetooth_device_address_t)))
                        return TRUE;
        }
 
@@ -140,35 +124,33 @@ BT_EXPORT_API int bluetooth_gatt_set_service_change_watcher(
 {
        GSList *l;
        bluetooth_device_address_t *addr = NULL;
-       char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
 
-       _bt_convert_addr_type_to_string(device_address,
-                       (unsigned char *)address->addr);
-       _bt_convert_addr_string_to_secure_string(secure_address,
-                       device_address);
+       _bt_convert_addr_type_to_secure_string(secure_address, (unsigned char *)address->addr);
        BT_INFO("Set watcher for %s with %d", secure_address, enable);
 
        if (enable == TRUE) {
-               if (service_monitor_list == NULL)
-                       _bt_register_manager_subscribe_signal(TRUE);
-
                if (_bluetooth_gatt_check_service_change_watcher_address(address)
                                == TRUE) {
                        BT_INFO("The watcher is already set");
                        return BLUETOOTH_ERROR_NONE;
                }
+
+               if (service_monitor_list == NULL) {
+                       _bt_register_manager_subscribe_signal(TRUE);
+               }
+
                addr = g_malloc0(sizeof(bluetooth_device_address_t));
                memcpy(addr, address, sizeof(bluetooth_device_address_t));
 
                service_monitor_list =
                        g_slist_append(service_monitor_list, addr);
        } else {
-
                for (l = service_monitor_list; l != NULL; l = l->next) {
                        addr = l->data;
+
                        if (!memcmp(address, addr,
-                                       sizeof(bluetooth_device_address_t))) {
+                                   sizeof(bluetooth_device_address_t))) {
                                service_monitor_list =
                                        g_slist_remove(service_monitor_list, addr);
                                g_free(addr);
@@ -239,39 +221,43 @@ BT_EXPORT_API int bluetooth_gatt_get_service_property(const char *service_handle
                if (!g_strcmp0(key, "UUID")) {
                        char *name = NULL;
                        service->uuid = g_variant_dup_string(value, &len);
-                       _bt_get_uuid_specification_name(service->uuid, &name);
-                       BT_INFO("======> Service : %s [%s]", service->uuid, name);
+                       bluetooth_get_uuid_name(service->uuid, &name);
+                       BT_INFO("%s %s [%s]", service_handle + 37, service->uuid, name);
                        g_free(name);
                } else if (!g_strcmp0(key, "Primary")) {
                        service->primary = g_variant_get_boolean(value);
 
                } else if (!g_strcmp0(key, "Includes")) {
                        g_variant_get(value, "ao", &char_iter);
-                       gp_array = g_ptr_array_new();
-                       while (g_variant_iter_loop(char_iter, "&o", &char_handle))
-                               g_ptr_array_add(gp_array, (gpointer)char_handle);
-
-                       if (gp_array->len != 0) {
-                               service->include_handles.count = gp_array->len;
-                               service->include_handles.handle =
-                                               __get_string_array_from_gptr_array(gp_array);
+                       if (char_iter != NULL) {
+                               gp_array = g_ptr_array_new();
+                               while (g_variant_iter_loop(char_iter, "&o", &char_handle))
+                                       g_ptr_array_add(gp_array, (gpointer)char_handle);
+
+                               if (gp_array->len != 0) {
+                                       service->include_handles.count = gp_array->len;
+                                       service->include_handles.handle =
+                                                       __get_string_array_from_gptr_array(gp_array);
+                               }
+                               g_ptr_array_free(gp_array, TRUE);
+                               g_variant_iter_free(char_iter);
                        }
-                       g_ptr_array_free(gp_array, TRUE);
-                       g_variant_iter_free(char_iter);
                } else if (!g_strcmp0(key, "Characteristics")) {
                        g_variant_get(value, "ao", &char_iter);
-                       gp_array = g_ptr_array_new();
-                       while (g_variant_iter_loop(char_iter, "&o", &char_handle))
-                               g_ptr_array_add(gp_array, (gpointer)char_handle);
-
-                       if (gp_array->len != 0) {
-                               service->char_handle.count = gp_array->len;
-                               service->char_handle.handle =
-                                               __get_string_array_from_gptr_array(gp_array);
+                       if (char_iter != NULL) {
+                               gp_array = g_ptr_array_new();
+                               while (g_variant_iter_loop(char_iter, "&o", &char_handle))
+                                       g_ptr_array_add(gp_array, (gpointer)char_handle);
+
+                               if (gp_array->len != 0) {
+                                       service->char_handle.count = gp_array->len;
+                                       service->char_handle.handle =
+                                                       __get_string_array_from_gptr_array(gp_array);
+                               }
+                               g_ptr_array_free(gp_array, TRUE);
+                               g_variant_iter_free(char_iter);
                        }
                        BT_DBG("Characteristics count : %d", service->char_handle.count);
-                       g_ptr_array_free(gp_array, TRUE);
-                       g_variant_iter_free(char_iter);
                }
        }
 
@@ -300,8 +286,9 @@ BT_EXPORT_API int bluetooth_gatt_get_primary_services(
        char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        char temp_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        int ret = BLUETOOTH_ERROR_INTERNAL;
+       int idx = 0;
 
-       BT_INFO("+");
+       BT_DBG("+");
        BT_CHECK_PARAMETER(address, return);
        BT_CHECK_PARAMETER(prim_svc, return);
        BT_CHECK_ENABLED(return);
@@ -331,7 +318,7 @@ BT_EXPORT_API int bluetooth_gatt_get_primary_services(
                        if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
                                continue;
 
-                       BT_DBG("Object Path: %s", object_path);
+                       BT_DBG("[%d] Object Path : %s", idx++, object_path);
                        while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
                                if (g_strcmp0(key, "Primary") == 0) {
                                        if (g_variant_get_boolean(value))
@@ -537,8 +524,6 @@ static int __get_permission_flag(char *permission)
 
        retv_if(permission == NULL, ret);
 
-       BT_INFO("permission = %s", permission);
-
        if (!g_strcmp0(permission, "broadcast"))
                ret = BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST;
        else if (!g_strcmp0(permission, "read"))
@@ -569,6 +554,41 @@ static int __get_permission_flag(char *permission)
        return ret;
 }
 
+static void __convert_permission_flag_to_str(unsigned int permission)
+{
+       char perm[200] = { 0, };
+
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_BROADCAST)
+               g_strlcat(perm, "broadcast ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_READ)
+               g_strlcat(perm, "read ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE)
+               g_strlcat(perm, "write-without-response ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITE)
+               g_strlcat(perm, "write ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_NOTIFY)
+               g_strlcat(perm, "notify ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_INDICATE)
+               g_strlcat(perm, "indicate ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE)
+               g_strlcat(perm, "authenticated-signed-writes ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE)
+               g_strlcat(perm, "reliable-write ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES)
+               g_strlcat(perm, "writable-auxiliaries ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ)
+               g_strlcat(perm, "encrypt-read ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE)
+               g_strlcat(perm, "encrypt-write ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ)
+               g_strlcat(perm, "encrypt-authenticated-read ", sizeof(perm));
+       if (permission & BLUETOOTH_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE)
+               g_strlcat(perm, "encrypt-authenticated-write ", sizeof(perm));
+
+       BT_INFO("permission [0x%04x] : %s\n", permission, perm);
+       return;
+}
+
 BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
                const char *char_handle, bt_gatt_char_property_t *characteristic)
 {
@@ -633,8 +653,8 @@ BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
                if (!g_strcmp0(key, "UUID")) {
                        char *name = NULL;
                        characteristic->uuid = g_variant_dup_string(value, &len);
-                       _bt_get_uuid_specification_name(characteristic->uuid, &name);
-                       BT_INFO("Characteristic : %s [%s]", characteristic->uuid, name);
+                       bluetooth_get_uuid_name(characteristic->uuid, &name);
+                       BT_INFO("%s %s [%s]", char_handle + 37, characteristic->uuid, name);
                        g_free(name);
                } else if (!g_strcmp0(key, "Value")) {
                        gb_array = g_byte_array_new();
@@ -658,6 +678,7 @@ BT_EXPORT_API int bluetooth_gatt_get_characteristics_property(
 
                        while (g_variant_iter_loop(char_perm_iter, "s", &permission))
                                characteristic->permission |= __get_permission_flag(permission);
+                       __convert_permission_flag_to_str(characteristic->permission);
 
                        g_variant_iter_free(char_perm_iter);
                } else if (!g_strcmp0(key, "Descriptors")) {
@@ -843,7 +864,7 @@ BT_EXPORT_API int bluetooth_gatt_get_char_descriptor_property(
                if (!g_strcmp0(key, "UUID")) {
                        char *name = NULL;
                        descriptor->uuid = g_variant_dup_string(value, &len);
-                       _bt_get_uuid_specification_name(descriptor->uuid, &name);
+                       bluetooth_get_uuid_name(descriptor->uuid, &name);
                        BT_INFO("Descriptor : %s [%s]", descriptor->uuid, name);
                        g_free(name);
                } else if (!g_strcmp0(key, "Value")) {
@@ -882,8 +903,8 @@ static int __bluetooth_get_att_error_code(GError *error)
        str = g_strrstr(error->message, "ATT error: 0x");
        if (str) {
                len = strlen(str);
-               att_ecode =  g_ascii_xdigit_value(str[len-2]) << 4;
-               att_ecode += g_ascii_xdigit_value(str[len-1]);
+               att_ecode =  g_ascii_xdigit_value(str[len - 2]) << 4;
+               att_ecode += g_ascii_xdigit_value(str[len - 1]);
        } else
                return BLUETOOTH_ATT_ERROR_INTERNAL;
 
@@ -895,7 +916,6 @@ static int __bluetooth_get_att_error_code(GError *error)
                BT_ERR("Write not permitted");
                break;
        case BLUETOOTH_ATT_ERROR_AUTHENTICATION:
-               break;
        case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION:
        case BLUETOOTH_ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
                BT_ERR("Not paired");
@@ -1658,7 +1678,7 @@ BT_EXPORT_API int bluetooth_gatt_write_descriptor_value(const char *desc,
        return BLUETOOTH_ERROR_NONE;
 }
 
-BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle)
+BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle, const char *svc_name)
 {
        GDBusConnection *conn;
        GError *error = NULL;
@@ -1667,7 +1687,7 @@ BT_EXPORT_API int bluetooth_gatt_watch_characteristics(const char *char_handle)
        BT_CHECK_PARAMETER(char_handle, return);
        BT_CHECK_ENABLED(return);
 
-       BT_INFO_C("### Enable CCCD : %s", char_handle);
+       BT_INFO_C("### Enable CCCD : %s [%s]", char_handle + 15, svc_name);
 
        conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);