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_counter {
39 unsigned int interval;
44 struct connman_service *service;
45 connman_bool_t first_update;
48 static void remove_counter(gpointer user_data)
50 struct connman_counter *counter = user_data;
52 DBG("owner %s path %s", counter->owner, counter->path);
54 if (counter->watch > 0)
55 g_dbus_remove_watch(connection, counter->watch);
57 __connman_rtnl_update_interval_remove(counter->interval);
59 g_free(counter->owner);
60 g_free(counter->path);
64 static void remove_data(gpointer user_data)
66 struct counter_data *data = user_data;
71 static void owner_disconnect(DBusConnection *connection, void *user_data)
73 struct connman_counter *counter = user_data;
75 DBG("owner %s path %s", counter->owner, counter->path);
77 g_hash_table_remove(owner_mapping, counter->owner);
78 g_hash_table_remove(counter_table, counter->path);
81 int __connman_counter_register(const char *owner, const char *path,
82 unsigned int interval)
84 struct connman_counter *counter;
86 DBG("owner %s path %s interval %u", owner, path, interval);
88 counter = g_hash_table_lookup(counter_table, path);
92 counter = g_try_new0(struct connman_counter, 1);
96 counter->owner = g_strdup(owner);
97 counter->path = g_strdup(path);
99 g_hash_table_replace(counter_table, counter->path, counter);
100 g_hash_table_replace(owner_mapping, counter->owner, counter);
102 counter->interval = interval;
103 __connman_rtnl_update_interval_add(counter->interval);
105 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
106 owner_disconnect, counter, NULL);
111 int __connman_counter_unregister(const char *owner, const char *path)
113 struct connman_counter *counter;
115 DBG("owner %s path %s", owner, path);
117 counter = g_hash_table_lookup(counter_table, path);
121 if (g_strcmp0(owner, counter->owner) != 0)
124 g_hash_table_remove(owner_mapping, counter->owner);
125 g_hash_table_remove(counter_table, counter->path);
130 static void send_usage(struct connman_counter *counter,
131 struct connman_service *service)
133 DBusMessage *message;
134 DBusMessageIter array, dict;
135 const char *service_path;
136 unsigned long rx_bytes;
137 unsigned long tx_bytes;
140 message = dbus_message_new_method_call(counter->owner, counter->path,
141 CONNMAN_COUNTER_INTERFACE, "Usage");
145 dbus_message_set_no_reply(message, TRUE);
147 service_path = __connman_service_get_path(service);
148 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
149 &service_path, DBUS_TYPE_INVALID);
151 dbus_message_iter_init_append(message, &array);
153 connman_dbus_dict_open(&array, &dict);
155 rx_bytes = __connman_service_stats_get_rx_bytes(service);
156 tx_bytes = __connman_service_stats_get_tx_bytes(service);
157 time = __connman_service_stats_get_time(service);
159 connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
161 connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
163 connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
166 connman_dbus_dict_close(&array, &dict);
168 g_dbus_send_message(connection, message);
171 void __connman_counter_notify(struct connman_ipconfig *config,
172 unsigned int rx_bytes, unsigned int tx_bytes)
174 struct counter_data *data;
178 data = g_hash_table_lookup(stats_table, config);
182 __connman_service_stats_update(data->service, rx_bytes, tx_bytes);
184 if (data->first_update == TRUE) {
185 data->first_update = FALSE;
189 g_hash_table_iter_init(&iter, counter_table);
190 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
191 struct connman_counter *counter = value;
193 send_usage(counter, data->service);
197 static void release_counter(gpointer key, gpointer value, gpointer user_data)
199 struct connman_counter *counter = value;
200 DBusMessage *message;
202 DBG("owner %s path %s", counter->owner, counter->path);
204 message = dbus_message_new_method_call(counter->owner, counter->path,
205 CONNMAN_COUNTER_INTERFACE, "Release");
209 dbus_message_set_no_reply(message, TRUE);
211 g_dbus_send_message(connection, message);
214 int __connman_counter_add_service(struct connman_service *service)
216 struct connman_ipconfig *config;
217 struct counter_data *data;
219 data = g_try_new0(struct counter_data, 1);
223 data->service = service;
224 data->first_update = TRUE;
226 config = __connman_service_get_ipconfig(service);
227 g_hash_table_replace(stats_table, config, data);
230 * Trigger a first update to intialize the offset counters
233 __connman_rtnl_request_update();
238 void __connman_counter_remove_service(struct connman_service *service)
240 struct connman_ipconfig *config;
242 config = __connman_service_get_ipconfig(service);
243 g_hash_table_remove(stats_table, config);
246 int __connman_counter_init(void)
250 connection = connman_dbus_get_connection();
251 if (connection == NULL)
254 stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
257 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
258 NULL, remove_counter);
259 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
265 void __connman_counter_cleanup(void)
269 if (connection == NULL)
272 g_hash_table_foreach(counter_table, release_counter, NULL);
274 g_hash_table_destroy(owner_mapping);
275 g_hash_table_destroy(counter_table);
277 g_hash_table_destroy(stats_table);
279 dbus_connection_unref(connection);