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_packets;
137 unsigned long tx_packets;
138 unsigned long rx_bytes;
139 unsigned long tx_bytes;
140 unsigned long rx_errors;
141 unsigned long tx_errors;
142 unsigned long rx_dropped;
143 unsigned long tx_dropped;
146 message = dbus_message_new_method_call(counter->owner, counter->path,
147 CONNMAN_COUNTER_INTERFACE, "Usage");
151 dbus_message_set_no_reply(message, TRUE);
153 service_path = __connman_service_get_path(service);
154 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
155 &service_path, DBUS_TYPE_INVALID);
157 dbus_message_iter_init_append(message, &array);
159 connman_dbus_dict_open(&array, &dict);
161 rx_packets = __connman_service_stats_get_rx_packets(service);
162 tx_packets = __connman_service_stats_get_tx_packets(service);
163 rx_bytes = __connman_service_stats_get_rx_bytes(service);
164 tx_bytes = __connman_service_stats_get_tx_bytes(service);
165 rx_errors = __connman_service_stats_get_rx_errors(service);
166 tx_errors = __connman_service_stats_get_tx_errors(service);
167 rx_dropped = __connman_service_stats_get_rx_dropped(service);
168 tx_dropped = __connman_service_stats_get_tx_dropped(service);
169 time = __connman_service_stats_get_time(service);
171 connman_dbus_dict_append_basic(&dict, "RX.Packets", DBUS_TYPE_UINT32,
173 connman_dbus_dict_append_basic(&dict, "TX.Packets", DBUS_TYPE_UINT32,
175 connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
177 connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
179 connman_dbus_dict_append_basic(&dict, "RX.Errors", DBUS_TYPE_UINT32,
181 connman_dbus_dict_append_basic(&dict, "TX.Errors", DBUS_TYPE_UINT32,
183 connman_dbus_dict_append_basic(&dict, "RX.Dropped", DBUS_TYPE_UINT32,
185 connman_dbus_dict_append_basic(&dict, "TX.Dropped", DBUS_TYPE_UINT32,
187 connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
190 connman_dbus_dict_close(&array, &dict);
192 g_dbus_send_message(connection, message);
195 void __connman_counter_notify(struct connman_ipconfig *config,
196 unsigned int rx_packets, unsigned int tx_packets,
197 unsigned int rx_bytes, unsigned int tx_bytes,
198 unsigned int rx_errors, unsigned int tx_errors,
199 unsigned int rx_dropped, unsigned int tx_dropped)
201 struct counter_data *data;
205 data = g_hash_table_lookup(stats_table, config);
209 __connman_service_stats_update(data->service,
210 rx_packets, tx_packets,
212 rx_errors, tx_errors,
213 rx_dropped, tx_dropped);
215 if (data->first_update == TRUE) {
216 data->first_update = FALSE;
220 g_hash_table_iter_init(&iter, counter_table);
221 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
222 struct connman_counter *counter = value;
224 send_usage(counter, data->service);
228 static void release_counter(gpointer key, gpointer value, gpointer user_data)
230 struct connman_counter *counter = value;
231 DBusMessage *message;
233 DBG("owner %s path %s", counter->owner, counter->path);
235 message = dbus_message_new_method_call(counter->owner, counter->path,
236 CONNMAN_COUNTER_INTERFACE, "Release");
240 dbus_message_set_no_reply(message, TRUE);
242 g_dbus_send_message(connection, message);
245 int __connman_counter_add_service(struct connman_service *service)
247 struct connman_ipconfig *config;
248 struct counter_data *data;
250 data = g_try_new0(struct counter_data, 1);
254 data->service = service;
255 data->first_update = TRUE;
257 config = __connman_service_get_ipconfig(service);
258 g_hash_table_replace(stats_table, config, data);
261 * Trigger a first update to intialize the offset counters
264 __connman_rtnl_request_update();
269 void __connman_counter_remove_service(struct connman_service *service)
271 struct connman_ipconfig *config;
273 config = __connman_service_get_ipconfig(service);
274 g_hash_table_remove(stats_table, config);
277 int __connman_counter_init(void)
281 connection = connman_dbus_get_connection();
282 if (connection == NULL)
285 stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
288 counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
289 NULL, remove_counter);
290 owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
296 void __connman_counter_cleanup(void)
300 if (connection == NULL)
303 g_hash_table_foreach(counter_table, release_counter, NULL);
305 g_hash_table_destroy(owner_mapping);
306 g_hash_table_destroy(counter_table);
308 g_hash_table_destroy(stats_table);
310 dbus_connection_unref(connection);