Release tizen_2.0_beta
[framework/connectivity/connman.git] / src / counter.c
index d897fb5..7f1c014 100644 (file)
 #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include <gdbus.h>
 
 #include "connman.h"
 
 static DBusConnection *connection;
 
-static GHashTable *stats_table;
 static GHashTable *counter_table;
 static GHashTable *owner_mapping;
 
-struct connman_stats {
-       char *interface;
-       unsigned int rx_bytes;
-       unsigned int tx_bytes;
-};
-
 struct connman_counter {
        char *owner;
        char *path;
-       guint timeout;
+       unsigned int interval;
        guint watch;
 };
 
-static void remove_stats(gpointer user_data)
-{
-       struct connman_stats *stats = user_data;
-
-       g_free(stats->interface);
-       g_free(stats);
-}
-
 static void remove_counter(gpointer user_data)
 {
        struct connman_counter *counter = user_data;
 
        DBG("owner %s path %s", counter->owner, counter->path);
 
-       if (counter->watch > 0)
-               g_dbus_remove_watch(connection, counter->watch);
+       __connman_rtnl_update_interval_remove(counter->interval);
 
-       if (counter->timeout > 0)
-               g_source_remove(counter->timeout);
+       __connman_service_counter_unregister(counter->path);
 
        g_free(counter->owner);
        g_free(counter->path);
@@ -81,21 +66,11 @@ static void owner_disconnect(DBusConnection *connection, void *user_data)
        g_hash_table_remove(counter_table, counter->path);
 }
 
-static gboolean counter_timeout(gpointer user_data)
-{
-       struct connman_counter *counter = user_data;
-
-       DBG("owner %s path %s", counter->owner, counter->path);
-
-       __connman_rtnl_request_update();
-
-       return TRUE;
-}
-
 int __connman_counter_register(const char *owner, const char *path,
                                                unsigned int interval)
 {
        struct connman_counter *counter;
+       int err;
 
        DBG("owner %s path %s interval %u", owner, path, interval);
 
@@ -110,18 +85,23 @@ int __connman_counter_register(const char *owner, const char *path,
        counter->owner = g_strdup(owner);
        counter->path = g_strdup(path);
 
+       err = __connman_service_counter_register(counter->path);
+       if (err < 0) {
+               g_free(counter->owner);
+               g_free(counter->path);
+               g_free(counter);
+               return err;
+       }
+
        g_hash_table_replace(counter_table, counter->path, counter);
        g_hash_table_replace(owner_mapping, counter->owner, counter);
 
-       if (interval > 0)
-               counter->timeout = g_timeout_add_seconds(interval,
-                                               counter_timeout, counter);
+       counter->interval = interval;
+       __connman_rtnl_update_interval_add(counter->interval);
 
        counter->watch = g_dbus_add_disconnect_watch(connection, owner,
                                        owner_disconnect, counter, NULL);
 
-       __connman_rtnl_request_update();
-
        return 0;
 }
 
@@ -138,76 +118,33 @@ int __connman_counter_unregister(const char *owner, const char *path)
        if (g_strcmp0(owner, counter->owner) != 0)
                return -EACCES;
 
+       if (counter->watch > 0)
+               g_dbus_remove_watch(connection, counter->watch);
+
        g_hash_table_remove(owner_mapping, counter->owner);
        g_hash_table_remove(counter_table, counter->path);
 
        return 0;
 }
 
-static void send_usage(struct connman_counter *counter,
-                                       struct connman_stats *stats)
+void __connman_counter_send_usage(const char *path,
+                                       DBusMessage *message)
 {
-       DBusMessage *message;
-       DBusMessageIter array, dict;
+       struct connman_counter *counter;
 
-       message = dbus_message_new_method_call(counter->owner, counter->path,
-                                       CONNMAN_COUNTER_INTERFACE, "Usage");
-       if (message == NULL)
+       counter = g_hash_table_lookup(counter_table, path);
+       if (counter == NULL)
                return;
 
+       dbus_message_set_destination(message, counter->owner);
+       dbus_message_set_path(message, counter->path);
+       dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
+       dbus_message_set_member(message, "Usage");
        dbus_message_set_no_reply(message, TRUE);
 
-       dbus_message_iter_init_append(message, &array);
-
-       connman_dbus_dict_open(&array, &dict);
-
-       connman_dbus_dict_append_basic(&dict, "Interface",
-                                       DBUS_TYPE_STRING, &stats->interface);
-       connman_dbus_dict_append_basic(&dict, "RX.Bytes",
-                                       DBUS_TYPE_UINT32, &stats->rx_bytes);
-       connman_dbus_dict_append_basic(&dict, "TX.Bytes",
-                                       DBUS_TYPE_UINT32, &stats->tx_bytes);
-
-       connman_dbus_dict_close(&array, &dict);
-
        g_dbus_send_message(connection, message);
 }
 
-void __connman_counter_notify(const char *interface,
-                               unsigned int rx_bytes, unsigned int tx_bytes)
-{
-       struct connman_stats *stats;
-       GHashTableIter iter;
-       gpointer key, value;
-
-       stats = g_hash_table_lookup(stats_table, interface);
-       if (stats != NULL)
-               goto update;
-
-       stats = g_try_new0(struct connman_stats, 1);
-       if (stats == NULL)
-               return;
-
-       stats->interface = g_strdup(interface);
-
-       g_hash_table_replace(stats_table, stats->interface, stats);
-
-update:
-       if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
-               return;
-
-       stats->rx_bytes = rx_bytes;
-       stats->tx_bytes = tx_bytes;
-
-       g_hash_table_iter_init(&iter, counter_table);
-
-       while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-               struct connman_counter *counter = value;
-
-               send_usage(counter, stats);
-       }
-}
-
 static void release_counter(gpointer key, gpointer value, gpointer user_data)
 {
        struct connman_counter *counter = value;
@@ -215,6 +152,9 @@ static void release_counter(gpointer key, gpointer value, gpointer user_data)
 
        DBG("owner %s path %s", counter->owner, counter->path);
 
+       if (counter->watch > 0)
+               g_dbus_remove_watch(connection, counter->watch);
+
        message = dbus_message_new_method_call(counter->owner, counter->path,
                                        CONNMAN_COUNTER_INTERFACE, "Release");
        if (message == NULL)
@@ -233,9 +173,6 @@ int __connman_counter_init(void)
        if (connection == NULL)
                return -1;
 
-       stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
-                                                       NULL, remove_stats);
-
        counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                        NULL, remove_counter);
        owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
@@ -256,7 +193,5 @@ void __connman_counter_cleanup(void)
        g_hash_table_destroy(owner_mapping);
        g_hash_table_destroy(counter_table);
 
-       g_hash_table_destroy(stats_table);
-
        dbus_connection_unref(connection);
 }