Modify build feature names as Tizen 3.0 build option naming rule
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-gatt-service.c
index 88d33f5..d33e0f3 100644 (file)
@@ -29,7 +29,6 @@
 #define NUMBER_OF_FLAGS        10
 
 GDBusConnection *g_conn;
-GDBusNodeInfo *obj_info;
 guint owner_id;
 guint manager_id;
 static gboolean new_service = FALSE;
@@ -190,7 +189,7 @@ static gchar *app_path = NULL;
 #define GATT_CHAR_INTERFACE            "org.bluez.GattCharacteristic1"
 #define GATT_DESC_INTERFACE            "org.bluez.GattDescriptor1"
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
 #define BT_HPS_OBJECT_PATH "/org/projectx/httpproxy"
 #define BT_HPS_INTERFACE_NAME "org.projectx.httpproxy_service"
 #define PROPERTIES_CHANGED "PropertiesChanged"
@@ -235,7 +234,7 @@ static void __bt_gatt_close_gdbus_connection(void)
        BT_DBG("-");
 }
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
 static int __bt_send_event_to_hps(int event, GVariant *var)
 {
        GError *error = NULL;
@@ -300,7 +299,7 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                                        GDBusMethodInvocation *invocation,
                                        gpointer user_data)
 {
-       GSList *l1;
+       GSList *l1 = NULL;
        int len = 0;
        int i = 0;
 
@@ -323,13 +322,19 @@ static void __bt_gatt_manager_method_call(GDBusConnection *connection,
                        GVariantBuilder *svc_builder = NULL;
                        GVariantBuilder *inner_builder = NULL;
 
-                       if (register_pending_cnt > 1) {
+                       if (register_pending_cnt > 1)
                                l1 = g_slist_nth(gatt_services, len - register_pending_cnt);
-                       } else {
+                       else
                                l1 = g_slist_last(gatt_services);
-                       }
+
                        register_pending_cnt--;
 
+                       if (l1 == NULL) {
+                               BT_ERR("gatt service list is NULL");
+                               g_dbus_method_invocation_return_value(invocation, NULL);
+                               return;
+                       }
+
                        struct gatt_service_info *serv_info = l1->data;
                        if (serv_info == NULL) {
                                BT_ERR("service info value is NULL");
@@ -653,7 +658,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                if (svc_info != NULL) {
                        read_req.service_handle = g_strdup(svc_info->serv_path);
                        user_info = _bt_get_user_data(BT_COMMON);
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                        GVariant *param = NULL;
 #endif
 
@@ -672,7 +677,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                        BLUETOOTH_ERROR_NONE, &read_req,
                                        user_info->cb, user_info->user_data);
                        }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                        param = g_variant_new("(sssyq)",
                                        read_req.att_handle,
                                        read_req.service_handle,
@@ -700,7 +705,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                int len = 0;
                struct gatt_service_info *svc_info = NULL;
                struct gatt_req_info *req_info = NULL;
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                GVariant *param = NULL;
 #endif
 
@@ -757,7 +762,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                                BLUETOOTH_ERROR_NONE, &value_change,
                                user_info->cb, user_info->user_data);
                }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
                if (len > 0) {
                        gchar *svc_path;
                        svc_path = g_strdup(svc_info->serv_path);
@@ -1034,22 +1039,22 @@ static GDBusNodeInfo *__bt_gatt_create_method_node_info(
                                const gchar *introspection_data)
 {
        GError *err = NULL;
+       GDBusNodeInfo *node_info = NULL;
 
        if (introspection_data == NULL)
                return NULL;
 
-       if (obj_info == NULL) {
-               BT_DBG("Create new node info");
-               obj_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
 
-               if (err) {
-                       BT_ERR("Unable to create node: %s", err->message);
-                       g_clear_error(&err);
-                       return NULL;
-               }
+       BT_DBG("Create new node info");
+       node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
+
+       if (err) {
+               BT_ERR("Unable to create node: %s", err->message);
+               g_clear_error(&err);
+               return NULL;
        }
 
-       return obj_info;
+       return node_info;
 }
 
 static struct gatt_service_info *__bt_gatt_find_gatt_service_info(
@@ -1129,9 +1134,8 @@ static struct gatt_req_info *__bt_gatt_find_request_info(guint request_id)
        for (l = gatt_requests; l != NULL; l = l->next) {
                struct gatt_req_info *req_info = l->data;
 
-               if (req_info && req_info->request_id == request_id) {
+               if (req_info && req_info->request_id == request_id)
                        return req_info;
-               }
        }
        BT_ERR("Gatt Request not found");
        return NULL;
@@ -1392,6 +1396,8 @@ void register_application_cb(GObject *object, GAsyncResult *res, gpointer user_d
                                                error->code, error->message);
                        g_clear_error(&error);
                }
+       } else {
+               g_variant_unref(result);
        }
 }
 
@@ -1415,6 +1421,8 @@ void unregister_application_cb(GObject *object, GAsyncResult *res,
                                        error->code, error->message);
                        g_clear_error(&error);
                }
+       } else {
+               g_variant_unref(result);
        }
 }
 
@@ -1572,15 +1580,11 @@ BT_EXPORT_API int bluetooth_gatt_init(void)
        return BLUETOOTH_ERROR_NONE;
 
 failed:
-       if (obj_info)
-               g_dbus_node_info_unref(obj_info);
-
        if (owner_id)
                g_bus_unown_name(owner_id);
 
        g_free(app_path);
 
-       obj_info = NULL;
        app_path = NULL;
        owner_id = 0;
 
@@ -1622,12 +1626,6 @@ BT_EXPORT_API int bluetooth_gatt_deinit()
                g_object_unref(manager_gproxy);
                manager_gproxy = NULL;
 
-               /* Temperary block under codes to avoid TC blocking issue.
-                   But we should unref node info in later. */
-#if 0
-               g_dbus_node_info_unref(obj_info);
-               obj_info = NULL;
-#endif
                __bt_gatt_close_gdbus_connection();
 
                return ret;
@@ -1794,9 +1792,9 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
        char_info->char_id = object_id;
        char_info->char_uuid = g_strdup(char_uuid);
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                char_info->char_flags[i] = char_flags[i];
-               }
+
 
        char_info->flags_length = flag_count;
 
@@ -1812,9 +1810,8 @@ BT_EXPORT_API int bluetooth_gatt_add_new_characteristic(
 
        builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                g_variant_builder_add(builder2, "s", char_flags[i]);
-       }
 
        flags_val = g_variant_new("as", builder2);
        g_variant_builder_add(inner_builder, "{sv}", "Flags",
@@ -1893,9 +1890,8 @@ BT_EXPORT_API int bluetooth_gatt_set_characteristic_value(
 
        builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(builder1, "y", char_value[i]);
-       }
 
        char_val = g_variant_new("ay", builder1);
                g_variant_builder_add(inner_builder, "{sv}", "Value", char_val);
@@ -1954,6 +1950,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
        if (char_info == NULL) {
                g_strfreev(line_argv);
+               g_free(serv_path);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
@@ -1962,6 +1959,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
 
        if (node_info == NULL) {
                g_strfreev(line_argv);
+               g_free(serv_path);
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
@@ -1978,6 +1976,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
                g_error_free(error);
                g_free(path);
                g_strfreev(line_argv);
+               g_free(serv_path);
 
                return BLUETOOTH_ERROR_INTERNAL;
        }
@@ -1990,9 +1989,8 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        desc_info->desc_id = object_id;
        desc_info->desc_uuid = g_strdup(desc_uuid);
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                desc_info->desc_flags[i] = desc_flags[i];
-               }
 
        desc_info->flags_length = flag_count;
 
@@ -2008,9 +2006,8 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
 
        builder2 = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
-       for (i = 0; i < flag_count; i++) {
+       for (i = 0; i < flag_count; i++)
                g_variant_builder_add(builder2, "s", desc_flags[i]);
-       }
 
        flags_val = g_variant_new("as", builder2);
        g_variant_builder_add(inner_builder, "{sv}", "Flags",
@@ -2030,6 +2027,7 @@ BT_EXPORT_API int bluetooth_gatt_add_descriptor(
        *desc_path = g_strdup(path);
 
        g_free(path);
+       g_free(serv_path);
        g_strfreev(line_argv);
        g_variant_builder_unref(inner_builder);
        g_variant_builder_unref(builder);
@@ -2081,9 +2079,9 @@ BT_EXPORT_API int bluetooth_gatt_set_descriptor_value(
 
        builder1 = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
 
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(builder1, "y", desc_value[i]);
-       }
+
        desc_val = g_variant_new("ay", builder1);
        g_variant_builder_add(inner_builder, "{sv}", "Value", desc_val);
 
@@ -2242,6 +2240,7 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
        if (!__bt_gatt_get_service_state(serv_path)) {
                BT_DBG("service not registered for this characteristic \n");
+               g_free(serv_path);
                g_strfreev(line_argv);
                return BLUETOOTH_ERROR_INTERNAL;
        }
@@ -2250,9 +2249,8 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
        invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
 
        inner_builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
-       for (i = 0; i < value_length; i++) {
+       for (i = 0; i < value_length; i++)
                g_variant_builder_add(inner_builder, "y", char_value[i]);
-       }
 
        update_value = g_variant_new("ay", inner_builder);
 
@@ -2283,6 +2281,12 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
                char_info = __bt_gatt_find_gatt_char_info(serv_path, char_path);
                if (char_info == NULL) {
+                       g_free(serv_path);
+                       g_strfreev(line_argv);
+                       g_variant_builder_unref(inner_builder);
+                       g_variant_builder_unref(outer_builder);
+                       g_variant_builder_unref(invalidated_builder);
+
                        return BLUETOOTH_ERROR_INVALID_DATA;
                }
 
@@ -2290,12 +2294,12 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
 
                char_info->char_value = (char *)realloc(char_info->char_value, value_length);
                if (char_info->char_value) {
-                       for (i = 0; i < value_length; i++) {
+                       for (i = 0; i < value_length; i++)
                                char_info->char_value[i] = char_value[i];
-                       }
                }
        }
 
+       g_free(serv_path);
        g_strfreev(line_argv);
        g_variant_builder_unref(inner_builder);
        g_variant_builder_unref(outer_builder);
@@ -2304,14 +2308,56 @@ BT_EXPORT_API int bluetooth_gatt_update_characteristic(
        return err;
 }
 
+static void __bt_gatt_free_descriptor_info(struct gatt_desc_info *desc_info)
+{
+       int i;
+
+       if (!desc_info)
+               return;
+
+       g_free(desc_info->desc_path);
+       g_free(desc_info->desc_uuid);
+       g_free(desc_info->desc_value);
+
+       for (i = 0; i < desc_info->flags_length; i++)
+               g_free(desc_info->desc_flags[i]);
+
+       g_free(desc_info);
+}
+
+static void __bt_gatt_free_characteristic_info(struct gatt_char_info *char_info)
+{
+       int i;
+
+       if (!char_info)
+               return;
+
+       g_free(char_info->char_path);
+       g_free(char_info->char_uuid);
+       g_free(char_info->char_value);
+
+       for (i = 0; i < char_info->flags_length; i++)
+               g_free(char_info->char_flags[i]);
+
+       g_free(char_info);
+}
+
+static void __bt_gatt_free_service_info(struct gatt_service_info *svc_info)
+{
+       if (!svc_info)
+               return;
+
+       g_free(svc_info->serv_path);
+       g_free(svc_info->service_uuid);
+       g_free(svc_info);
+}
+
 BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
 {
-       int i = 0;
        GSList *l, *l1;
        struct gatt_service_info *svc_info;
        gboolean ret;
        int err = BLUETOOTH_ERROR_NONE;
-       GSList *tmp;
 
        BT_DBG("svc_path %s", svc_path);
        svc_info = __bt_gatt_find_gatt_service_info(svc_path);
@@ -2349,16 +2395,9 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                                err = BLUETOOTH_ERROR_INTERNAL;
                        }
 
+                       /* list remove & free */
                        char_info->desc_data = g_slist_remove(char_info->desc_data, desc_info);
-
-                       g_free(desc_info->desc_path);
-                       g_free(desc_info->desc_uuid);
-                       g_free(desc_info->desc_value);
-
-                       for (i = 0; i < desc_info->flags_length; i++)
-                               g_free(desc_info->desc_flags[i]);
-
-                       g_free(desc_info);
+                       __bt_gatt_free_descriptor_info(desc_info);
                }
 
                g_slist_free(char_info->desc_data);
@@ -2373,12 +2412,9 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
                        err = BLUETOOTH_ERROR_INTERNAL;
                }
 
-               g_free(char_info->char_path);
-               g_free(char_info->char_uuid);
-               g_free(char_info->char_value);
-
-               for (i = 0; i < char_info->flags_length; i++)
-                       g_free(char_info->char_flags[i]);
+               /* list remove & free */
+               svc_info->char_data = g_slist_remove(svc_info->char_data, char_info);
+               __bt_gatt_free_characteristic_info(char_info);
        }
 
        g_slist_free(svc_info->char_data);
@@ -2393,28 +2429,19 @@ BT_EXPORT_API int bluetooth_gatt_unregister_service(const char *svc_path)
        }
 
        ret = g_dbus_connection_unregister_object(g_conn, svc_info->prop_id);
-       if (ret) {
+       if (ret)
                BT_DBG("Unregistered the service on properties interface");
-       }
 
-       for (tmp = gatt_services; tmp != NULL; tmp = tmp->next) {
-               struct gatt_service_info *info = tmp->data;
-
-               if (g_strcmp0(info->serv_path, svc_path) == 0) {
-                       gatt_services = g_slist_remove(gatt_services, tmp->data);
-                       g_free(info->serv_path);
-                       g_free(info->service_uuid);
-                       g_free(info);
-               }
-       }
+       /* list remove & free */
+       gatt_services = g_slist_remove(gatt_services, svc_info);
+       __bt_gatt_free_service_info(svc_info);
 
        new_service = FALSE;
 
-       if (gatt_services == NULL) {
+       if (gatt_services == NULL)
                serv_id = 1;
-       } else if (gatt_services->next == NULL) {
+       else if (gatt_services->next == NULL)
                serv_id--;
-       }
 
        return err;
 }
@@ -2434,11 +2461,8 @@ BT_EXPORT_API int bluetooth_gatt_send_response(int request_id, guint req_type,
 
        if (req_info) {
                if (resp_state != BLUETOOTH_ERROR_NONE) {
-
-                       GQuark quark = g_quark_from_string("gatt-server");
-                       GError *err = g_error_new(quark, 0, "Application Error");
-                       g_dbus_method_invocation_return_gerror(req_info->context, err);
-                       g_error_free(err);
+                       g_dbus_method_invocation_return_dbus_error(req_info->context,
+                                               "org.bluez.Error.Failed", "Application Error");
 
                        gatt_requests = g_slist_remove(gatt_requests, req_info);
 
@@ -2498,10 +2522,13 @@ BT_EXPORT_API int bluetooth_gatt_server_set_notification(const char *char_path,
 
        if (!__bt_gatt_get_service_state(serv_path)) {
                BT_DBG("service not registered for this characteristic \n");
+               g_free(serv_path);
                g_strfreev(line_argv);
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+       g_free(serv_path);
+
        outer_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        invalidated_builder = g_variant_builder_new(G_VARIANT_TYPE("as"));