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;
41 connman_bool_t first_update;
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 __connman_rtnl_update_interval_remove(counter->interval);
55 g_free(counter->owner);
56 g_free(counter->path);
60 static void owner_disconnect(DBusConnection *connection, void *user_data)
62 struct connman_counter *counter = user_data;
64 DBG("owner %s path %s", counter->owner, counter->path);
66 g_hash_table_remove(owner_mapping, counter->owner);
67 g_hash_table_remove(counter_table, counter->path);
70 int __connman_counter_register(const char *owner, const char *path,
71 unsigned int interval)
73 struct connman_counter *counter;
75 DBG("owner %s path %s interval %u", owner, path, interval);
77 counter = g_hash_table_lookup(counter_table, path);
81 counter = g_try_new0(struct connman_counter, 1);
85 counter->owner = g_strdup(owner);
86 counter->path = g_strdup(path);
87 counter->first_update = TRUE;
89 g_hash_table_replace(counter_table, counter->path, counter);
90 g_hash_table_replace(owner_mapping, counter->owner, counter);
92 counter->interval = interval;
93 __connman_rtnl_update_interval_add(counter->interval);
95 counter->watch = g_dbus_add_disconnect_watch(connection, owner,
96 owner_disconnect, counter, NULL);
101 int __connman_counter_unregister(const char *owner, const char *path)
103 struct connman_counter *counter;
105 DBG("owner %s path %s", owner, path);
107 counter = g_hash_table_lookup(counter_table, path);
111 if (g_strcmp0(owner, counter->owner) != 0)
114 g_hash_table_remove(owner_mapping, counter->owner);
115 g_hash_table_remove(counter_table, counter->path);
120 static void send_usage(struct connman_counter *counter,
121 struct connman_service *service)
123 DBusMessage *message;
124 const char *service_path;
126 message = dbus_message_new_method_call(counter->owner, counter->path,
127 CONNMAN_COUNTER_INTERFACE, "Usage");
131 dbus_message_set_no_reply(message, TRUE);
133 service_path = __connman_service_get_path(service);
134 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
135 &service_path, DBUS_TYPE_INVALID);
137 __connman_service_stats_append(service, message, counter->first_update);
139 counter->first_update = FALSE;
141 g_dbus_send_message(connection, message);
144 void __connman_counter_notify(struct connman_ipconfig *config,
145 unsigned int rx_packets, unsigned int tx_packets,
146 unsigned int rx_bytes, unsigned int tx_bytes,
147 unsigned int rx_errors, unsigned int tx_errors,
148 unsigned int rx_dropped, unsigned int tx_dropped)
150 struct connman_service *service;
154 service = g_hash_table_lookup(stats_table, config);
158 if (__connman_service_stats_update(service,
159 rx_packets, tx_packets,
161 rx_errors, tx_errors,
162 rx_dropped, tx_dropped) == FALSE) {
163 /* first update, counters are now initialized */
167 g_hash_table_iter_init(&iter, counter_table);
168 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
169 struct connman_counter *counter = value;
171 send_usage(counter, service);
175 static void release_counter(gpointer key, gpointer value, gpointer user_data)
177 struct connman_counter *counter = value;
178 DBusMessage *message;
180 DBG("owner %s path %s", counter->owner, counter->path);
182 message = dbus_message_new_method_call(counter->owner, counter->path,
183 CONNMAN_COUNTER_INTERFACE, "Release");
187 dbus_message_set_no_reply(message, TRUE);
189 g_dbus_send_message(connection, message);
192 int __connman_counter_add_service(struct connman_service *service)
194 struct connman_ipconfig *config;
196 config = __connman_service_get_ipconfig(service);
197 g_hash_table_replace(stats_table, config, service);
200 * Trigger a first update to intialize the offset counters
203 __connman_rtnl_request_update();
208 void __connman_counter_remove_service(struct connman_service *service)
210 struct connman_ipconfig *config;
212 config = __connman_service_get_ipconfig(service);
213 g_hash_table_remove(stats_table, config);
216 int __connman_counter_init(void)
220 connection = connman_dbus_get_connection();
221 if (connection == NULL)
224 stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
227 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
228 NULL, remove_counter);
229 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
235 void __connman_counter_cleanup(void)
239 if (connection == NULL)
242 g_hash_table_foreach(counter_table, release_counter, NULL);
244 g_hash_table_destroy(owner_mapping);
245 g_hash_table_destroy(counter_table);
247 g_hash_table_destroy(stats_table);
249 dbus_connection_unref(connection);