Usage statistics structure directly
[framework/connectivity/connman.git] / src / counter.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusConnection *connection;
31
32 static GHashTable *stats_table;
33 static GHashTable *counter_table;
34 static GHashTable *owner_mapping;
35
36 struct connman_stats {
37         char *interface;
38         unsigned int rx_bytes;
39         unsigned int tx_bytes;
40 };
41
42 struct connman_counter {
43         char *owner;
44         char *path;
45         guint timeout;
46         guint watch;
47 };
48
49 static void remove_stats(gpointer user_data)
50 {
51         struct connman_stats *stats = user_data;
52
53         g_free(stats->interface);
54         g_free(stats);
55 }
56
57 static void remove_counter(gpointer user_data)
58 {
59         struct connman_counter *counter = user_data;
60
61         DBG("owner %s path %s", counter->owner, counter->path);
62
63         if (counter->watch > 0)
64                 g_dbus_remove_watch(connection, counter->watch);
65
66         if (counter->timeout > 0)
67                 g_source_remove(counter->timeout);
68
69         g_free(counter->owner);
70         g_free(counter->path);
71         g_free(counter);
72 }
73
74 static void owner_disconnect(DBusConnection *connection, void *user_data)
75 {
76         struct connman_counter *counter = user_data;
77
78         DBG("owner %s path %s", counter->owner, counter->path);
79
80         g_hash_table_remove(owner_mapping, counter->owner);
81         g_hash_table_remove(counter_table, counter->path);
82 }
83
84 static gboolean counter_timeout(gpointer user_data)
85 {
86         struct connman_counter *counter = user_data;
87
88         DBG("owner %s path %s", counter->owner, counter->path);
89
90         __connman_rtnl_request_update();
91
92         return TRUE;
93 }
94
95 int __connman_counter_register(const char *owner, const char *path,
96                                                 unsigned int interval)
97 {
98         struct connman_counter *counter;
99
100         DBG("owner %s path %s interval %u", owner, path, interval);
101
102         if (interval < 1)
103                 return -EINVAL;
104
105         counter = g_hash_table_lookup(counter_table, path);
106         if (counter != NULL)
107                 return -EEXIST;
108
109         counter = g_try_new0(struct connman_counter, 1);
110         if (counter == NULL)
111                 return -ENOMEM;
112
113         counter->owner = g_strdup(owner);
114         counter->path = g_strdup(path);
115
116         g_hash_table_replace(counter_table, counter->path, counter);
117         g_hash_table_replace(owner_mapping, counter->owner, counter);
118
119         counter->timeout = g_timeout_add_seconds(interval,
120                                                 counter_timeout, counter);
121
122         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
123                                         owner_disconnect, counter, NULL);
124
125         __connman_rtnl_request_update();
126
127         return 0;
128 }
129
130 int __connman_counter_unregister(const char *owner, const char *path)
131 {
132         struct connman_counter *counter;
133
134         DBG("owner %s path %s", owner, path);
135
136         counter = g_hash_table_lookup(counter_table, path);
137         if (counter == NULL)
138                 return -ESRCH;
139
140         if (g_strcmp0(owner, counter->owner) != 0)
141                 return -EACCES;
142
143         g_hash_table_remove(owner_mapping, counter->owner);
144         g_hash_table_remove(counter_table, counter->path);
145
146         return 0;
147 }
148
149 static void send_usage(struct connman_counter *counter,
150                                         struct connman_stats *stats)
151 {
152         DBusMessage *message;
153         DBusMessageIter array, dict;
154
155         message = dbus_message_new_method_call(counter->owner, counter->path,
156                                         CONNMAN_COUNTER_INTERFACE, "Usage");
157         if (message == NULL)
158                 return;
159
160         dbus_message_set_no_reply(message, TRUE);
161
162         dbus_message_iter_init_append(message, &array);
163
164         connman_dbus_dict_open(&array, &dict);
165
166         connman_dbus_dict_append_basic(&dict, "Interface",
167                                         DBUS_TYPE_STRING, &stats->interface);
168         connman_dbus_dict_append_basic(&dict, "RX.Bytes",
169                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
170         connman_dbus_dict_append_basic(&dict, "TX.Bytes",
171                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
172
173         connman_dbus_dict_close(&array, &dict);
174
175         g_dbus_send_message(connection, message);
176 }
177
178 void __connman_counter_notify(const char *interface,
179                                 unsigned int rx_bytes, unsigned int tx_bytes)
180 {
181         struct connman_stats *stats;
182         GHashTableIter iter;
183         gpointer key, value;
184
185         stats = g_hash_table_lookup(stats_table, interface);
186         if (stats != NULL)
187                 goto update;
188
189         stats = g_try_new0(struct connman_stats, 1);
190         if (stats == NULL)
191                 return;
192
193         stats->interface = g_strdup(interface);
194
195         g_hash_table_replace(stats_table, stats->interface, stats);
196
197 update:
198         if (stats->rx_bytes == rx_bytes && stats->rx_bytes == tx_bytes)
199                 return;
200
201         stats->rx_bytes = rx_bytes;
202         stats->tx_bytes = tx_bytes;
203
204         g_hash_table_iter_init(&iter, counter_table);
205
206         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
207                 struct connman_counter *counter = value;
208
209                 send_usage(counter, stats);
210         }
211 }
212
213 static void release_counter(gpointer key, gpointer value, gpointer user_data)
214 {
215         struct connman_counter *counter = value;
216         DBusMessage *message;
217
218         DBG("owner %s path %s", counter->owner, counter->path);
219
220         message = dbus_message_new_method_call(counter->owner, counter->path,
221                                         CONNMAN_COUNTER_INTERFACE, "Release");
222         if (message == NULL)
223                 return;
224
225         dbus_message_set_no_reply(message, TRUE);
226
227         g_dbus_send_message(connection, message);
228 }
229
230 int __connman_counter_init(void)
231 {
232         DBG("");
233
234         connection = connman_dbus_get_connection();
235         if (connection == NULL)
236                 return -1;
237
238         stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
239                                                         NULL, remove_stats);
240
241         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
242                                                         NULL, remove_counter);
243         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
244                                                                 NULL, NULL);
245
246         return 0;
247 }
248
249 void __connman_counter_cleanup(void)
250 {
251         DBG("");
252
253         if (connection == NULL)
254                 return;
255
256         g_hash_table_foreach(counter_table, release_counter, NULL);
257
258         g_hash_table_destroy(owner_mapping);
259         g_hash_table_destroy(counter_table);
260
261         g_hash_table_destroy(stats_table);
262
263         dbus_connection_unref(connection);
264 }