5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 static DBusConnection *connection;
32 static GHashTable *counter_table;
33 static GHashTable *owner_mapping;
35 struct connman_counter {
42 static void remove_counter(gpointer user_data)
44 struct connman_counter *counter = user_data;
46 DBG("owner %s path %s", counter->owner, counter->path);
48 if (counter->watch > 0)
49 g_dbus_remove_watch(connection, counter->watch);
51 if (counter->timeout > 0)
52 g_source_remove(counter->timeout);
54 g_free(counter->owner);
55 g_free(counter->path);
59 static void owner_disconnect(DBusConnection *connection, void *user_data)
61 struct connman_counter *counter = user_data;
63 DBG("owner %s path %s", counter->owner, counter->path);
65 g_hash_table_remove(owner_mapping, counter->owner);
66 g_hash_table_remove(counter_table, counter->path);
69 static gboolean counter_timeout(gpointer user_data)
71 struct connman_counter *counter = user_data;
74 DBG("owner %s path %s", counter->owner, counter->path);
76 message = dbus_message_new_method_call(counter->owner, counter->path,
77 CONNMAN_COUNTER_INTERFACE, "Usage");
81 dbus_message_set_no_reply(message, TRUE);
83 g_dbus_send_message(connection, message);
88 int __connman_counter_register(const char *owner, const char *path,
89 unsigned int interval)
91 struct connman_counter *counter;
93 DBG("owner %s path %s interval %u", owner, path, interval);
95 counter = g_hash_table_lookup(counter_table, path);
99 counter = g_try_new0(struct connman_counter, 1);
103 counter->owner = g_strdup(owner);
104 counter->path = g_strdup(path);
106 g_hash_table_replace(counter_table, counter->path, counter);
107 g_hash_table_replace(owner_mapping, counter->owner, counter);
109 counter->timeout = g_timeout_add_seconds(interval,
110 counter_timeout, counter);
112 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
113 owner_disconnect, counter, NULL);
118 int __connman_counter_unregister(const char *owner, const char *path)
120 struct connman_counter *counter;
122 DBG("owner %s path %s", owner, path);
124 counter = g_hash_table_lookup(counter_table, path);
128 if (g_strcmp0(owner, counter->owner) != 0)
131 g_hash_table_remove(owner_mapping, counter->owner);
132 g_hash_table_remove(counter_table, counter->path);
137 static void release_counter(gpointer key, gpointer value, gpointer user_data)
139 struct connman_counter *counter = value;
140 DBusMessage *message;
142 DBG("owner %s path %s", counter->owner, counter->path);
144 message = dbus_message_new_method_call(counter->owner, counter->path,
145 CONNMAN_COUNTER_INTERFACE, "Release");
149 dbus_message_set_no_reply(message, TRUE);
151 g_dbus_send_message(connection, message);
154 int __connman_counter_init(void)
158 connection = connman_dbus_get_connection();
159 if (connection == NULL)
162 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
163 NULL, remove_counter);
164 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
170 void __connman_counter_cleanup(void)
174 if (connection == NULL)
177 g_hash_table_foreach(counter_table, release_counter, NULL);
179 g_hash_table_destroy(owner_mapping);
180 g_hash_table_destroy(counter_table);
182 dbus_connection_unref(connection);