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
32 static DBusConnection *connection;
34 static GHashTable *counter_table;
35 static GHashTable *owner_mapping;
37 struct connman_counter {
40 unsigned int interval;
44 static void remove_counter(gpointer user_data)
46 struct connman_counter *counter = user_data;
48 DBG("owner %s path %s", counter->owner, counter->path);
50 __connman_rtnl_update_interval_remove(counter->interval);
52 __connman_service_counter_unregister(counter->path);
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 int __connman_counter_register(const char *owner, const char *path,
70 unsigned int interval)
72 struct connman_counter *counter;
75 DBG("owner %s path %s interval %u", owner, path, interval);
77 counter = g_hash_table_lookup(counter_table, path);
81 counter = g_try_new0(struct connman_counter, 1);
85 counter->owner = g_strdup(owner);
86 counter->path = g_strdup(path);
88 err = __connman_service_counter_register(counter->path);
90 g_free(counter->owner);
91 g_free(counter->path);
96 g_hash_table_replace(counter_table, counter->path, counter);
97 g_hash_table_replace(owner_mapping, counter->owner, counter);
99 counter->interval = interval;
100 __connman_rtnl_update_interval_add(counter->interval);
102 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
103 owner_disconnect, counter, NULL);
108 int __connman_counter_unregister(const char *owner, const char *path)
110 struct connman_counter *counter;
112 DBG("owner %s path %s", owner, path);
114 counter = g_hash_table_lookup(counter_table, path);
118 if (g_strcmp0(owner, counter->owner) != 0)
121 if (counter->watch > 0)
122 g_dbus_remove_watch(connection, counter->watch);
124 g_hash_table_remove(owner_mapping, counter->owner);
125 g_hash_table_remove(counter_table, counter->path);
130 void __connman_counter_send_usage(const char *path,
131 DBusMessage *message)
133 struct connman_counter *counter;
135 counter = g_hash_table_lookup(counter_table, path);
139 dbus_message_set_destination(message, counter->owner);
140 dbus_message_set_path(message, counter->path);
141 dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
142 dbus_message_set_member(message, "Usage");
143 dbus_message_set_no_reply(message, TRUE);
145 g_dbus_send_message(connection, message);
148 static void release_counter(gpointer key, gpointer value, gpointer user_data)
150 struct connman_counter *counter = value;
151 DBusMessage *message;
153 DBG("owner %s path %s", counter->owner, counter->path);
155 if (counter->watch > 0)
156 g_dbus_remove_watch(connection, counter->watch);
158 message = dbus_message_new_method_call(counter->owner, counter->path,
159 CONNMAN_COUNTER_INTERFACE, "Release");
163 dbus_message_set_no_reply(message, TRUE);
165 g_dbus_send_message(connection, message);
168 int __connman_counter_init(void)
172 connection = connman_dbus_get_connection();
173 if (connection == NULL)
176 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
177 NULL, remove_counter);
178 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
184 void __connman_counter_cleanup(void)
188 if (connection == NULL)
191 g_hash_table_foreach(counter_table, release_counter, NULL);
193 g_hash_table_destroy(owner_mapping);
194 g_hash_table_destroy(counter_table);
196 dbus_connection_unref(connection);