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 {
40 unsigned int rx_bytes;
41 unsigned int tx_bytes;
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 if (counter->watch > 0)
51 g_dbus_remove_watch(connection, counter->watch);
53 if (counter->timeout > 0)
54 g_source_remove(counter->timeout);
56 g_free(counter->owner);
57 g_free(counter->path);
61 static void owner_disconnect(DBusConnection *connection, void *user_data)
63 struct connman_counter *counter = user_data;
65 DBG("owner %s path %s", counter->owner, counter->path);
67 g_hash_table_remove(owner_mapping, counter->owner);
68 g_hash_table_remove(counter_table, counter->path);
71 static gboolean counter_timeout(gpointer user_data)
73 struct connman_counter *counter = user_data;
75 DBG("owner %s path %s", counter->owner, counter->path);
77 __connman_rtnl_request_update();
82 int __connman_counter_register(const char *owner, const char *path,
83 unsigned int interval)
85 struct connman_counter *counter;
87 DBG("owner %s path %s interval %u", owner, path, interval);
92 counter = g_hash_table_lookup(counter_table, path);
96 counter = g_try_new0(struct connman_counter, 1);
100 counter->owner = g_strdup(owner);
101 counter->path = g_strdup(path);
103 g_hash_table_replace(counter_table, counter->path, counter);
104 g_hash_table_replace(owner_mapping, counter->owner, counter);
106 counter->timeout = g_timeout_add_seconds(interval,
107 counter_timeout, counter);
109 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
110 owner_disconnect, counter, NULL);
112 __connman_rtnl_request_update();
117 int __connman_counter_unregister(const char *owner, const char *path)
119 struct connman_counter *counter;
121 DBG("owner %s path %s", owner, path);
123 counter = g_hash_table_lookup(counter_table, path);
127 if (g_strcmp0(owner, counter->owner) != 0)
130 g_hash_table_remove(owner_mapping, counter->owner);
131 g_hash_table_remove(counter_table, counter->path);
136 static void send_usage(struct connman_counter *counter)
138 DBusMessage *message;
139 DBusMessageIter array, dict;
141 message = dbus_message_new_method_call(counter->owner, counter->path,
142 CONNMAN_COUNTER_INTERFACE, "Usage");
146 dbus_message_set_no_reply(message, TRUE);
148 dbus_message_iter_init_append(message, &array);
150 connman_dbus_dict_open(&array, &dict);
152 connman_dbus_dict_append_basic(&dict, "RX.Bytes",
153 DBUS_TYPE_UINT32, &counter->rx_bytes);
154 connman_dbus_dict_append_basic(&dict, "TX.Bytes",
155 DBUS_TYPE_UINT32, &counter->tx_bytes);
157 connman_dbus_dict_close(&array, &dict);
159 g_dbus_send_message(connection, message);
162 void __connman_counter_notify(unsigned int rx_bytes, unsigned int tx_bytes)
167 g_hash_table_iter_init(&iter, counter_table);
169 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
170 struct connman_counter *counter = value;
172 if (counter->rx_bytes == rx_bytes &&
173 counter->tx_bytes == tx_bytes)
176 counter->rx_bytes = rx_bytes;
177 counter->tx_bytes = tx_bytes;
183 static void release_counter(gpointer key, gpointer value, gpointer user_data)
185 struct connman_counter *counter = value;
186 DBusMessage *message;
188 DBG("owner %s path %s", counter->owner, counter->path);
190 message = dbus_message_new_method_call(counter->owner, counter->path,
191 CONNMAN_COUNTER_INTERFACE, "Release");
195 dbus_message_set_no_reply(message, TRUE);
197 g_dbus_send_message(connection, message);
200 int __connman_counter_init(void)
204 connection = connman_dbus_get_connection();
205 if (connection == NULL)
208 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
209 NULL, remove_counter);
210 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
216 void __connman_counter_cleanup(void)
220 if (connection == NULL)
223 g_hash_table_foreach(counter_table, release_counter, NULL);
225 g_hash_table_destroy(owner_mapping);
226 g_hash_table_destroy(counter_table);
228 dbus_connection_unref(connection);