5 * Copyright (C) 2007-2012 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 *conn, 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);
138 dbus_message_unref(message);
142 dbus_message_set_destination(message, counter->owner);
143 dbus_message_set_path(message, counter->path);
144 dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
145 dbus_message_set_member(message, "Usage");
146 dbus_message_set_no_reply(message, TRUE);
148 g_dbus_send_message(connection, message);
151 static void release_counter(gpointer key, gpointer value, gpointer user_data)
153 struct connman_counter *counter = value;
154 DBusMessage *message;
156 DBG("owner %s path %s", counter->owner, counter->path);
158 if (counter->watch > 0)
159 g_dbus_remove_watch(connection, counter->watch);
161 message = dbus_message_new_method_call(counter->owner, counter->path,
162 CONNMAN_COUNTER_INTERFACE, "Release");
166 dbus_message_set_no_reply(message, TRUE);
168 g_dbus_send_message(connection, message);
171 int __connman_counter_init(void)
175 connection = connman_dbus_get_connection();
179 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
180 NULL, remove_counter);
181 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
187 void __connman_counter_cleanup(void)
194 g_hash_table_foreach(counter_table, release_counter, NULL);
196 g_hash_table_destroy(owner_mapping);
197 g_hash_table_destroy(counter_table);
199 dbus_connection_unref(connection);