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 *stats_table;
33 static GHashTable *counter_table;
34 static GHashTable *owner_mapping;
36 struct connman_stats {
38 unsigned int rx_bytes;
39 unsigned int tx_bytes;
42 struct connman_counter {
45 unsigned int interval;
49 static void remove_stats(gpointer user_data)
51 struct connman_stats *stats = user_data;
53 g_free(stats->interface);
57 static void remove_counter(gpointer user_data)
59 struct connman_counter *counter = user_data;
61 DBG("owner %s path %s", counter->owner, counter->path);
63 if (counter->watch > 0)
64 g_dbus_remove_watch(connection, counter->watch);
66 __connman_rtnl_update_interval_remove(counter->interval);
68 g_free(counter->owner);
69 g_free(counter->path);
73 static void owner_disconnect(DBusConnection *connection, void *user_data)
75 struct connman_counter *counter = user_data;
77 DBG("owner %s path %s", counter->owner, counter->path);
79 g_hash_table_remove(owner_mapping, counter->owner);
80 g_hash_table_remove(counter_table, counter->path);
83 int __connman_counter_register(const char *owner, const char *path,
84 unsigned int interval)
86 struct connman_counter *counter;
88 DBG("owner %s path %s interval %u", owner, path, interval);
90 counter = g_hash_table_lookup(counter_table, path);
94 counter = g_try_new0(struct connman_counter, 1);
98 counter->owner = g_strdup(owner);
99 counter->path = g_strdup(path);
101 g_hash_table_replace(counter_table, counter->path, counter);
102 g_hash_table_replace(owner_mapping, counter->owner, counter);
104 counter->interval = interval;
105 __connman_rtnl_update_interval_add(counter->interval);
107 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
108 owner_disconnect, counter, NULL);
113 int __connman_counter_unregister(const char *owner, const char *path)
115 struct connman_counter *counter;
117 DBG("owner %s path %s", owner, path);
119 counter = g_hash_table_lookup(counter_table, path);
123 if (g_strcmp0(owner, counter->owner) != 0)
126 g_hash_table_remove(owner_mapping, counter->owner);
127 g_hash_table_remove(counter_table, counter->path);
132 static void send_usage(struct connman_counter *counter,
133 struct connman_stats *stats)
135 DBusMessage *message;
136 DBusMessageIter array, dict;
138 message = dbus_message_new_method_call(counter->owner, counter->path,
139 CONNMAN_COUNTER_INTERFACE, "Usage");
143 dbus_message_set_no_reply(message, TRUE);
145 dbus_message_iter_init_append(message, &array);
147 connman_dbus_dict_open(&array, &dict);
149 connman_dbus_dict_append_basic(&dict, "Interface",
150 DBUS_TYPE_STRING, &stats->interface);
151 connman_dbus_dict_append_basic(&dict, "RX.Bytes",
152 DBUS_TYPE_UINT32, &stats->rx_bytes);
153 connman_dbus_dict_append_basic(&dict, "TX.Bytes",
154 DBUS_TYPE_UINT32, &stats->tx_bytes);
156 connman_dbus_dict_close(&array, &dict);
158 g_dbus_send_message(connection, message);
161 void __connman_counter_notify(const char *interface,
162 unsigned int rx_bytes, unsigned int tx_bytes)
164 struct connman_stats *stats;
168 stats = g_hash_table_lookup(stats_table, interface);
172 stats = g_try_new0(struct connman_stats, 1);
176 stats->interface = g_strdup(interface);
178 g_hash_table_replace(stats_table, stats->interface, stats);
181 if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
184 stats->rx_bytes = rx_bytes;
185 stats->tx_bytes = tx_bytes;
187 g_hash_table_iter_init(&iter, counter_table);
189 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
190 struct connman_counter *counter = value;
192 send_usage(counter, stats);
196 static void release_counter(gpointer key, gpointer value, gpointer user_data)
198 struct connman_counter *counter = value;
199 DBusMessage *message;
201 DBG("owner %s path %s", counter->owner, counter->path);
203 message = dbus_message_new_method_call(counter->owner, counter->path,
204 CONNMAN_COUNTER_INTERFACE, "Release");
208 dbus_message_set_no_reply(message, TRUE);
210 g_dbus_send_message(connection, message);
213 int __connman_counter_init(void)
217 connection = connman_dbus_get_connection();
218 if (connection == NULL)
221 stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
224 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
225 NULL, remove_counter);
226 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
232 void __connman_counter_cleanup(void)
236 if (connection == NULL)
239 g_hash_table_foreach(counter_table, release_counter, NULL);
241 g_hash_table_destroy(owner_mapping);
242 g_hash_table_destroy(counter_table);
244 g_hash_table_destroy(stats_table);
246 dbus_connection_unref(connection);