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 {
38 unsigned int interval;
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 __connman_rtnl_update_interval_remove(counter->interval);
53 __connman_service_counter_unregister(counter->path);
55 g_free(counter->owner);
56 g_free(counter->path);
60 static void owner_disconnect(DBusConnection *connection, void *user_data)
62 struct connman_counter *counter = user_data;
64 DBG("owner %s path %s", counter->owner, counter->path);
66 g_hash_table_remove(owner_mapping, counter->owner);
67 g_hash_table_remove(counter_table, counter->path);
70 int __connman_counter_register(const char *owner, const char *path,
71 unsigned int interval)
73 struct connman_counter *counter;
76 DBG("owner %s path %s interval %u", owner, path, interval);
78 counter = g_hash_table_lookup(counter_table, path);
82 counter = g_try_new0(struct connman_counter, 1);
86 counter->owner = g_strdup(owner);
87 counter->path = g_strdup(path);
89 err = __connman_service_counter_register(counter->path);
91 g_free(counter->owner);
92 g_free(counter->path);
97 g_hash_table_replace(counter_table, counter->path, counter);
98 g_hash_table_replace(owner_mapping, counter->owner, counter);
100 counter->interval = interval;
101 __connman_rtnl_update_interval_add(counter->interval);
103 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
104 owner_disconnect, counter, NULL);
109 int __connman_counter_unregister(const char *owner, const char *path)
111 struct connman_counter *counter;
113 DBG("owner %s path %s", owner, path);
115 counter = g_hash_table_lookup(counter_table, path);
119 if (g_strcmp0(owner, counter->owner) != 0)
122 g_hash_table_remove(owner_mapping, counter->owner);
123 g_hash_table_remove(counter_table, counter->path);
128 void __connman_counter_send_usage(const char *path,
129 DBusMessage *message)
131 struct connman_counter *counter;
133 counter = g_hash_table_lookup(counter_table, path);
137 dbus_message_set_destination(message, counter->owner);
138 dbus_message_set_path(message, counter->path);
139 dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
140 dbus_message_set_member(message, "Usage");
141 dbus_message_set_no_reply(message, TRUE);
143 g_dbus_send_message(connection, message);
146 static void release_counter(gpointer key, gpointer value, gpointer user_data)
148 struct connman_counter *counter = value;
149 DBusMessage *message;
151 DBG("owner %s path %s", counter->owner, counter->path);
153 message = dbus_message_new_method_call(counter->owner, counter->path,
154 CONNMAN_COUNTER_INTERFACE, "Release");
158 dbus_message_set_no_reply(message, TRUE);
160 g_dbus_send_message(connection, message);
163 int __connman_counter_init(void)
167 connection = connman_dbus_get_connection();
168 if (connection == NULL)
171 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
172 NULL, remove_counter);
173 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
179 void __connman_counter_cleanup(void)
183 if (connection == NULL)
186 g_hash_table_foreach(counter_table, release_counter, NULL);
188 g_hash_table_destroy(owner_mapping);
189 g_hash_table_destroy(counter_table);
191 dbus_connection_unref(connection);