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 __connman_rtnl_update_interval_remove(counter->interval);
50 __connman_service_counter_unregister(counter->path);
52 g_free(counter->owner);
53 g_free(counter->path);
57 static void owner_disconnect(DBusConnection *connection, void *user_data)
59 struct connman_counter *counter = user_data;
61 DBG("owner %s path %s", counter->owner, counter->path);
63 g_hash_table_remove(owner_mapping, counter->owner);
64 g_hash_table_remove(counter_table, counter->path);
67 int __connman_counter_register(const char *owner, const char *path,
68 unsigned int interval)
70 struct connman_counter *counter;
73 DBG("owner %s path %s interval %u", owner, path, interval);
75 counter = g_hash_table_lookup(counter_table, path);
79 counter = g_try_new0(struct connman_counter, 1);
83 counter->owner = g_strdup(owner);
84 counter->path = g_strdup(path);
86 err = __connman_service_counter_register(counter->path);
88 g_free(counter->owner);
89 g_free(counter->path);
94 g_hash_table_replace(counter_table, counter->path, counter);
95 g_hash_table_replace(owner_mapping, counter->owner, counter);
97 counter->interval = interval;
98 __connman_rtnl_update_interval_add(counter->interval);
100 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
101 owner_disconnect, counter, NULL);
106 int __connman_counter_unregister(const char *owner, const char *path)
108 struct connman_counter *counter;
110 DBG("owner %s path %s", owner, path);
112 counter = g_hash_table_lookup(counter_table, path);
116 if (g_strcmp0(owner, counter->owner) != 0)
119 if (counter->watch > 0)
120 g_dbus_remove_watch(connection, counter->watch);
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 if (counter->watch > 0)
154 g_dbus_remove_watch(connection, counter->watch);
156 message = dbus_message_new_method_call(counter->owner, counter->path,
157 CONNMAN_COUNTER_INTERFACE, "Release");
161 dbus_message_set_no_reply(message, TRUE);
163 g_dbus_send_message(connection, message);
166 int __connman_counter_init(void)
170 connection = connman_dbus_get_connection();
171 if (connection == NULL)
174 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
175 NULL, remove_counter);
176 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
182 void __connman_counter_cleanup(void)
186 if (connection == NULL)
189 g_hash_table_foreach(counter_table, release_counter, NULL);
191 g_hash_table_destroy(owner_mapping);
192 g_hash_table_destroy(counter_table);
194 dbus_connection_unref(connection);