Updated connman to version 1.35
[platform/upstream/connman.git] / gdbus / client.c
old mode 100644 (file)
new mode 100755 (executable)
index 3bf883a..9748ae2
@@ -42,6 +42,7 @@ struct GDBusClient {
        DBusConnection *dbus_conn;
        char *service_name;
        char *base_path;
+       char *root_path;
        guint watch;
        guint added_watch;
        guint removed_watch;
@@ -51,13 +52,16 @@ struct GDBusClient {
        GDBusWatchFunction connect_func;
        void *connect_data;
        GDBusWatchFunction disconn_func;
+       gboolean connected;
        void *disconn_data;
        GDBusMessageFunction signal_func;
        void *signal_data;
        GDBusProxyFunction proxy_added;
        GDBusProxyFunction proxy_removed;
+#if !defined TIZEN_EXT
        GDBusClientFunction ready;
        void *ready_data;
+#endif
        GDBusPropertyFunction property_changed;
        void *user_data;
        GList *proxy_list;
@@ -727,6 +731,7 @@ gboolean g_dbus_proxy_set_property_basic(GDBusProxy *proxy,
        return TRUE;
 }
 
+#if !defined TIZEN_EXT
 gboolean g_dbus_proxy_set_property_array(GDBusProxy *proxy,
                                const char *name, int type, const void *value,
                                size_t size, GDBusResultFunction function,
@@ -813,6 +818,7 @@ gboolean g_dbus_proxy_set_property_array(GDBusProxy *proxy,
 
        return TRUE;
 }
+#endif
 
 struct method_call_data {
        GDBusReturnFunction function;
@@ -851,28 +857,30 @@ gboolean g_dbus_proxy_method_call(GDBusProxy *proxy, const char *method,
        if (client == NULL)
                return FALSE;
 
-       data = g_try_new0(struct method_call_data, 1);
-       if (data == NULL)
-               return FALSE;
-
-       data->function = function;
-       data->user_data = user_data;
-       data->destroy = destroy;
-
        msg = dbus_message_new_method_call(client->service_name,
                                proxy->obj_path, proxy->interface, method);
-       if (msg == NULL) {
-               g_free(data);
+       if (msg == NULL)
                return FALSE;
-       }
 
        if (setup) {
                DBusMessageIter iter;
 
                dbus_message_iter_init_append(msg, &iter);
-               setup(&iter, data->user_data);
+               setup(&iter, user_data);
        }
 
+       if (!function)
+               return g_dbus_send_message(client->dbus_conn, msg);
+
+       data = g_try_new0(struct method_call_data, 1);
+       if (data == NULL)
+               return FALSE;
+
+       data->function = function;
+       data->user_data = user_data;
+       data->destroy = destroy;
+
+
        if (g_dbus_send_message_with_reply(client->dbus_conn, msg,
                                        &call, METHOD_CALL_TIMEOUT) == FALSE) {
                dbus_message_unref(msg);
@@ -1071,9 +1079,6 @@ static void parse_managed_objects(GDBusClient *client, DBusMessage *msg)
 
                dbus_message_iter_next(&dict);
        }
-
-       if (client->ready)
-               client->ready(client, client->ready_data);
 }
 
 static void get_managed_objects_reply(DBusPendingCall *call, void *user_data)
@@ -1094,6 +1099,11 @@ static void get_managed_objects_reply(DBusPendingCall *call, void *user_data)
        parse_managed_objects(client, reply);
 
 done:
+#if !defined TIZEN_EXT
+       if (client->ready)
+               client->ready(client, client->ready_data);
+#endif
+
        dbus_message_unref(reply);
 
        dbus_pending_call_unref(client->get_objects_call);
@@ -1106,7 +1116,11 @@ static void get_managed_objects(GDBusClient *client)
 {
        DBusMessage *msg;
 
-       if (!client->proxy_added && !client->proxy_removed) {
+       if (!client->connected)
+               return;
+
+       if ((!client->proxy_added && !client->proxy_removed) ||
+                                                       !client->root_path) {
                refresh_properties(client);
                return;
        }
@@ -1114,9 +1128,10 @@ static void get_managed_objects(GDBusClient *client)
        if (client->get_objects_call != NULL)
                return;
 
-       msg = dbus_message_new_method_call(client->service_name, "/",
-                                       DBUS_INTERFACE_DBUS ".ObjectManager",
-                                                       "GetManagedObjects");
+       msg = dbus_message_new_method_call(client->service_name,
+                                               client->root_path,
+                                               DBUS_INTERFACE_OBJECT_MANAGER,
+                                               "GetManagedObjects");
        if (msg == NULL)
                return;
 
@@ -1141,6 +1156,8 @@ static void service_connect(DBusConnection *conn, void *user_data)
 
        g_dbus_client_ref(client);
 
+       client->connected = TRUE;
+
        if (client->connect_func)
                client->connect_func(conn, client->connect_data);
 
@@ -1153,6 +1170,8 @@ static void service_disconnect(DBusConnection *conn, void *user_data)
 {
        GDBusClient *client = user_data;
 
+       client->connected = FALSE;
+
        g_list_free_full(client->proxy_list, proxy_free);
        client->proxy_list = NULL;
 
@@ -1191,10 +1210,18 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
 GDBusClient *g_dbus_client_new(DBusConnection *connection,
                                        const char *service, const char *path)
 {
+       return g_dbus_client_new_full(connection, service, path, "/");
+}
+
+GDBusClient *g_dbus_client_new_full(DBusConnection *connection,
+                                                       const char *service,
+                                                       const char *path,
+                                                       const char *root_path)
+{
        GDBusClient *client;
        unsigned int i;
 
-       if (connection == NULL)
+       if (!connection || !service)
                return NULL;
 
        client = g_try_new0(GDBusClient, 1);
@@ -1210,6 +1237,8 @@ GDBusClient *g_dbus_client_new(DBusConnection *connection,
        client->dbus_conn = dbus_connection_ref(connection);
        client->service_name = g_strdup(service);
        client->base_path = g_strdup(path);
+       client->root_path = g_strdup(root_path);
+       client->connected = FALSE;
 
        client->match_rules = g_ptr_array_sized_new(1);
        g_ptr_array_set_free_func(client->match_rules, g_free);
@@ -1218,14 +1247,18 @@ GDBusClient *g_dbus_client_new(DBusConnection *connection,
                                                service_connect,
                                                service_disconnect,
                                                client, NULL);
+
+       if (!root_path)
+               return g_dbus_client_ref(client);
+
        client->added_watch = g_dbus_add_signal_watch(connection, service,
-                                               "/",
+                                               client->root_path,
                                                DBUS_INTERFACE_OBJECT_MANAGER,
                                                "InterfacesAdded",
                                                interfaces_added,
                                                client, NULL);
        client->removed_watch = g_dbus_add_signal_watch(connection, service,
-                                               "/",
+                                               client->root_path,
                                                DBUS_INTERFACE_OBJECT_MANAGER,
                                                "InterfacesRemoved",
                                                interfaces_removed,
@@ -1284,7 +1317,11 @@ void g_dbus_client_unref(GDBusClient *client)
 
        g_list_free_full(client->proxy_list, proxy_free);
 
-       if (client->disconn_func)
+       /*
+        * Don't call disconn_func twice if disconnection
+        * was previously reported.
+        */
+       if (client->disconn_func && client->connected)
                client->disconn_func(client->dbus_conn, client->disconn_data);
 
        g_dbus_remove_watch(client->dbus_conn, client->watch);
@@ -1295,6 +1332,7 @@ void g_dbus_client_unref(GDBusClient *client)
 
        g_free(client->service_name);
        g_free(client->base_path);
+       g_free(client->root_path);
 
        g_free(client);
 }
@@ -1335,6 +1373,7 @@ gboolean g_dbus_client_set_signal_watch(GDBusClient *client,
        return TRUE;
 }
 
+#if !defined TIZEN_EXT
 gboolean g_dbus_client_set_ready_watch(GDBusClient *client,
                                GDBusClientFunction ready, void *user_data)
 {
@@ -1346,6 +1385,7 @@ gboolean g_dbus_client_set_ready_watch(GDBusClient *client,
 
        return TRUE;
 }
+#endif
 
 gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
                                        GDBusProxyFunction proxy_added,
@@ -1361,7 +1401,8 @@ gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
        client->property_changed = property_changed;
        client->user_data = user_data;
 
-       get_managed_objects(client);
+       if (proxy_added || proxy_removed || property_changed)
+               get_managed_objects(client);
 
        return TRUE;
 }