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);
89 counter = g_hash_table_lookup(counter_table, path);
93 counter = g_try_new0(struct connman_counter, 1);
97 counter->owner = g_strdup(owner);
98 counter->path = g_strdup(path);
100 g_hash_table_replace(counter_table, counter->path, counter);
101 g_hash_table_replace(owner_mapping, counter->owner, counter);
103 counter->timeout = g_timeout_add_seconds(interval,
104 counter_timeout, counter);
106 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
107 owner_disconnect, counter, NULL);
112 int __connman_counter_unregister(const char *owner, const char *path)
114 struct connman_counter *counter;
116 DBG("owner %s path %s", owner, path);
118 counter = g_hash_table_lookup(counter_table, path);
122 if (g_strcmp0(owner, counter->owner) != 0)
125 g_hash_table_remove(owner_mapping, counter->owner);
126 g_hash_table_remove(counter_table, counter->path);
131 static void send_usage(struct connman_counter *counter)
133 DBusMessage *message;
134 DBusMessageIter array, dict;
136 message = dbus_message_new_method_call(counter->owner, counter->path,
137 CONNMAN_COUNTER_INTERFACE, "Usage");
141 dbus_message_set_no_reply(message, TRUE);
143 dbus_message_iter_init_append(message, &array);
145 connman_dbus_dict_open(&array, &dict);
147 connman_dbus_dict_append_basic(&dict, "RX.Bytes",
148 DBUS_TYPE_UINT32, &counter->rx_bytes);
149 connman_dbus_dict_append_basic(&dict, "TX.Bytes",
150 DBUS_TYPE_UINT32, &counter->tx_bytes);
152 connman_dbus_dict_close(&array, &dict);
154 g_dbus_send_message(connection, message);
157 void __connman_counter_notify(unsigned int rx_bytes, unsigned int tx_bytes)
162 g_hash_table_iter_init(&iter, counter_table);
164 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
165 struct connman_counter *counter = value;
167 counter->rx_bytes = rx_bytes;
168 counter->tx_bytes = tx_bytes;
174 static void release_counter(gpointer key, gpointer value, gpointer user_data)
176 struct connman_counter *counter = value;
177 DBusMessage *message;
179 DBG("owner %s path %s", counter->owner, counter->path);
181 message = dbus_message_new_method_call(counter->owner, counter->path,
182 CONNMAN_COUNTER_INTERFACE, "Release");
186 dbus_message_set_no_reply(message, TRUE);
188 g_dbus_send_message(connection, message);
191 int __connman_counter_init(void)
195 connection = connman_dbus_get_connection();
196 if (connection == NULL)
199 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
200 NULL, remove_counter);
201 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
207 void __connman_counter_cleanup(void)
211 if (connection == NULL)
214 g_hash_table_foreach(counter_table, release_counter, NULL);
216 g_hash_table_destroy(owner_mapping);
217 g_hash_table_destroy(counter_table);
219 dbus_connection_unref(connection);