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 {
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 if (counter->timeout > 0)
67 g_source_remove(counter->timeout);
69 g_free(counter->owner);
70 g_free(counter->path);
74 static void owner_disconnect(DBusConnection *connection, void *user_data)
76 struct connman_counter *counter = user_data;
78 DBG("owner %s path %s", counter->owner, counter->path);
80 g_hash_table_remove(owner_mapping, counter->owner);
81 g_hash_table_remove(counter_table, counter->path);
84 static gboolean counter_timeout(gpointer user_data)
86 struct connman_counter *counter = user_data;
88 DBG("owner %s path %s", counter->owner, counter->path);
90 __connman_rtnl_request_update();
95 int __connman_counter_register(const char *owner, const char *path,
96 unsigned int interval)
98 struct connman_counter *counter;
100 DBG("owner %s path %s interval %u", owner, path, interval);
102 counter = g_hash_table_lookup(counter_table, path);
106 counter = g_try_new0(struct connman_counter, 1);
110 counter->owner = g_strdup(owner);
111 counter->path = g_strdup(path);
113 g_hash_table_replace(counter_table, counter->path, counter);
114 g_hash_table_replace(owner_mapping, counter->owner, counter);
117 counter->timeout = g_timeout_add_seconds(interval,
118 counter_timeout, counter);
120 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
121 owner_disconnect, counter, NULL);
123 __connman_rtnl_request_update();
128 int __connman_counter_unregister(const char *owner, const char *path)
130 struct connman_counter *counter;
132 DBG("owner %s path %s", owner, path);
134 counter = g_hash_table_lookup(counter_table, path);
138 if (g_strcmp0(owner, counter->owner) != 0)
141 g_hash_table_remove(owner_mapping, counter->owner);
142 g_hash_table_remove(counter_table, counter->path);
147 static void send_usage(struct connman_counter *counter,
148 struct connman_stats *stats)
150 DBusMessage *message;
151 DBusMessageIter array, dict;
153 message = dbus_message_new_method_call(counter->owner, counter->path,
154 CONNMAN_COUNTER_INTERFACE, "Usage");
158 dbus_message_set_no_reply(message, TRUE);
160 dbus_message_iter_init_append(message, &array);
162 connman_dbus_dict_open(&array, &dict);
164 connman_dbus_dict_append_basic(&dict, "Interface",
165 DBUS_TYPE_STRING, &stats->interface);
166 connman_dbus_dict_append_basic(&dict, "RX.Bytes",
167 DBUS_TYPE_UINT32, &stats->rx_bytes);
168 connman_dbus_dict_append_basic(&dict, "TX.Bytes",
169 DBUS_TYPE_UINT32, &stats->tx_bytes);
171 connman_dbus_dict_close(&array, &dict);
173 g_dbus_send_message(connection, message);
176 void __connman_counter_notify(const char *interface,
177 unsigned int rx_bytes, unsigned int tx_bytes)
179 struct connman_stats *stats;
183 stats = g_hash_table_lookup(stats_table, interface);
187 stats = g_try_new0(struct connman_stats, 1);
191 stats->interface = g_strdup(interface);
193 g_hash_table_replace(stats_table, stats->interface, stats);
196 if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
199 stats->rx_bytes = rx_bytes;
200 stats->tx_bytes = tx_bytes;
202 g_hash_table_iter_init(&iter, counter_table);
204 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
205 struct connman_counter *counter = value;
207 send_usage(counter, stats);
211 static void release_counter(gpointer key, gpointer value, gpointer user_data)
213 struct connman_counter *counter = value;
214 DBusMessage *message;
216 DBG("owner %s path %s", counter->owner, counter->path);
218 message = dbus_message_new_method_call(counter->owner, counter->path,
219 CONNMAN_COUNTER_INTERFACE, "Release");
223 dbus_message_set_no_reply(message, TRUE);
225 g_dbus_send_message(connection, message);
228 int __connman_counter_init(void)
232 connection = connman_dbus_get_connection();
233 if (connection == NULL)
236 stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
239 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
240 NULL, remove_counter);
241 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
247 void __connman_counter_cleanup(void)
251 if (connection == NULL)
254 g_hash_table_foreach(counter_table, release_counter, NULL);
256 g_hash_table_destroy(owner_mapping);
257 g_hash_table_destroy(counter_table);
259 g_hash_table_destroy(stats_table);
261 dbus_connection_unref(connection);