Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-httpproxy / bt-httpproxy.c
index dbea6cc..4d23471 100644 (file)
@@ -44,8 +44,8 @@ char *http_status_desc_obj_path = NULL;
 char *http_security_obj_path = NULL;
 
 static GMainLoop *main_loop;
-static int property_sub_id = -1;
-static int adapter_sub_id = -1;
+static guint property_sub_id;
+static guint adapter_sub_id;
 static http_request_state req_state;
 
 #ifdef HPS_GATT_DB
@@ -69,7 +69,6 @@ static GSList *hps_char_list = NULL;
 static GDBusConnection *conn;
 static GDBusConnection *g_conn;
 static guint g_owner_id = 0;
-GDBusNodeInfo *hps_node_info = NULL;
 
 char *g_uri = NULL;
 char *g_header = NULL;
@@ -202,6 +201,7 @@ static const GDBusInterfaceVTable hps_method_table = {
 
 static void _bt_hps_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
 {
+       GDBusNodeInfo *node_info = NULL;
        guint object_id;
        GError *error = NULL;
 
@@ -209,14 +209,21 @@ static void _bt_hps_on_bus_acquired(GDBusConnection *connection, const gchar *na
 
        g_conn = connection;
 
+       node_info = g_dbus_node_info_new_for_xml(hps_introspection_xml, &error);
+       if (!node_info) {
+               BT_ERR("Failed to install: %s", error->message);
+               g_clear_error(&error);
+               return;
+       }
+
        object_id = g_dbus_connection_register_object(connection, BT_HPS_OBJECT_PATH,
-                                               hps_node_info->interfaces[0],
+                                               node_info->interfaces[0],
                                                &hps_method_table,
                                                NULL, NULL, &error);
+       g_dbus_node_info_unref(node_info);
        if (object_id == 0) {
                BT_ERR("Failed to register method table: %s", error->message);
                g_error_free(error);
-               g_dbus_node_info_unref(hps_node_info);
        }
 
        return;
@@ -237,7 +244,6 @@ static void _bt_hps_on_name_lost(GDBusConnection *connection,
        BT_DBG("");
        g_object_unref(g_conn);
        g_conn = NULL;
-       g_dbus_node_info_unref(hps_node_info);
        g_bus_unown_name(g_owner_id);
 
        return;
@@ -245,17 +251,10 @@ static void _bt_hps_on_name_lost(GDBusConnection *connection,
 
 int _bt_hps_register_interface(void)
 {
-       GError *error = NULL;
        guint owner_id;
 
        BT_DBG("");
 
-       hps_node_info = g_dbus_node_info_new_for_xml(hps_introspection_xml, &error);
-       if (!hps_node_info) {
-               BT_ERR("Failed to install: %s", error->message);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
        owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                                BT_HPS_SERVICE_NAME,
                                G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -273,7 +272,6 @@ void _bt_hps_unregister_interface(void)
 
        g_object_unref(g_conn);
        g_conn = NULL;
-       g_dbus_node_info_unref(hps_node_info);
        g_bus_unown_name(g_owner_id);
 
        return;
@@ -320,28 +318,24 @@ static void _bt_hps_set_char_value(const char *obj_path, const char* value, int
                if (tmp->data) {
                        struct hps_char_info *char_info = tmp->data;
                        if (!g_strcmp0(char_info->char_path, obj_path)) {
-                               gchar *str = NULL;
-                               if (offset > 0) {
-                                       str = g_strdup(char_info->char_value);
-                                       char_info->char_value = g_try_realloc(char_info->char_value, offset + value_length);
+                               gchar *data = NULL;
+                               if (char_info->char_value == NULL) {
+                                       char_info->char_value = g_malloc0(offset + value_length);
+                                       char_info->value_length = offset + value_length;
+                               } else if (char_info->value_length >= offset + value_length) {
+                                       /* Just change from offset */
+                                       memcpy(&char_info->char_value[offset], value, value_length);
                                } else {
-                                       char_info->char_value = g_try_realloc(char_info->char_value, value_length);
+                                       /* Values crossing pervious allocated limit realloc */
+                                       data = g_memdup(char_info->char_value, char_info->value_length);
+                                       char_info->char_value = g_try_realloc(char_info->char_value, offset + value_length);
+                                       memcpy(char_info->char_value, data, char_info->value_length);
+                                       memcpy(&char_info->char_value[offset], value, value_length);
+                                       char_info->value_length = offset + value_length;
+                                       g_free(data);
                                }
-                               if (char_info->char_value) {
-                                       if (str) {
-                                               memcpy(char_info->char_value, str, strlen(str));
-                                       }
-                                       if (offset > 0) {
-                                               memcpy(&char_info->char_value[offset], value, value_length);
-                                               char_info->value_length = offset + value_length;
-                                       } else {
-                                               memcpy(char_info->char_value, value, value_length);
-                                               char_info->value_length = value_length;
-                                       }
-                                       hps_char_list = g_slist_insert_sorted(hps_char_list,
+                               hps_char_list = g_slist_insert_sorted(hps_char_list,
                                                                        char_info, char_info_cmp);
-                               }
-                               g_free(str);
                                return;
                        }
                }
@@ -363,7 +357,7 @@ static void _bt_hps_set_notify_read_status(const char *obj_path,
                                notify_read_info->offset_status = offset_status;
                                notify_read_info->https_status = https_status;
                                hps_notify_read_list = g_slist_insert_sorted(hps_notify_read_list,
-                                               notify_read_info, notify_info_cmp);
+                                                               notify_read_info, notify_info_cmp);
                                return;
                        }
                }
@@ -380,16 +374,17 @@ static void _bt_hps_set_notify_read_status(const char *obj_path,
                        hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
                }
                return;
-       }
-
-       /* Store Notification information */
-       notify_read_info = g_new0(struct hps_notify_read_info, 1);
-       if (notify_read_info) {
-               notify_read_info->char_path = g_strdup(obj_path);
-               notify_read_info->read_status = read_status;
-               notify_read_info->offset_status = offset_status;
-               notify_read_info->https_status = https_status;
-               hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
+       } else {
+               /* Store Notification information */
+               notify_read_info = g_new0(struct hps_notify_read_info, 1);
+               if (notify_read_info) {
+                       notify_read_info->char_path = g_strdup(obj_path);
+                       notify_read_info->read_status = read_status;
+                       notify_read_info->offset_status = offset_status;
+                       notify_read_info->https_status = https_status;
+                       hps_notify_read_list = g_slist_append(hps_notify_read_list, notify_read_info);
+               }
+               return;
        }
 }
 
@@ -1425,9 +1420,22 @@ int _bt_hps_init_event_receiver()
 void _bt_hps_deinit_event_receiver(void)
 {
        BT_DBG("");
-       g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
-       g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
+
+       if (conn == NULL)
+               return;
+
+       if (property_sub_id > 0) {
+               g_dbus_connection_signal_unsubscribe(conn, property_sub_id);
+               property_sub_id = 0;
+       }
+
+       if (adapter_sub_id) {
+               g_dbus_connection_signal_unsubscribe(conn, adapter_sub_id);
+               adapter_sub_id = 0;
+       }
+
        conn = NULL;
+
        return;
 }