a457a2260b307269a6c89c395af1ac2c56f1154d
[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_counter {
37         char *owner;
38         char *path;
39         unsigned int interval;
40         guint watch;
41 };
42
43 struct counter_data {
44         struct connman_service *service;
45         connman_bool_t first_update;
46 };
47
48 static void remove_counter(gpointer user_data)
49 {
50         struct connman_counter *counter = user_data;
51
52         DBG("owner %s path %s", counter->owner, counter->path);
53
54         if (counter->watch > 0)
55                 g_dbus_remove_watch(connection, counter->watch);
56
57         __connman_rtnl_update_interval_remove(counter->interval);
58
59         g_free(counter->owner);
60         g_free(counter->path);
61         g_free(counter);
62 }
63
64 static void remove_data(gpointer user_data)
65 {
66         struct counter_data *data = user_data;
67
68         g_free(data);
69 }
70
71 static void owner_disconnect(DBusConnection *connection, void *user_data)
72 {
73         struct connman_counter *counter = user_data;
74
75         DBG("owner %s path %s", counter->owner, counter->path);
76
77         g_hash_table_remove(owner_mapping, counter->owner);
78         g_hash_table_remove(counter_table, counter->path);
79 }
80
81 int __connman_counter_register(const char *owner, const char *path,
82                                                 unsigned int interval)
83 {
84         struct connman_counter *counter;
85
86         DBG("owner %s path %s interval %u", owner, path, interval);
87
88         counter = g_hash_table_lookup(counter_table, path);
89         if (counter != NULL)
90                 return -EEXIST;
91
92         counter = g_try_new0(struct connman_counter, 1);
93         if (counter == NULL)
94                 return -ENOMEM;
95
96         counter->owner = g_strdup(owner);
97         counter->path = g_strdup(path);
98
99         g_hash_table_replace(counter_table, counter->path, counter);
100         g_hash_table_replace(owner_mapping, counter->owner, counter);
101
102         counter->interval = interval;
103         __connman_rtnl_update_interval_add(counter->interval);
104
105         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
106                                         owner_disconnect, counter, NULL);
107
108         return 0;
109 }
110
111 int __connman_counter_unregister(const char *owner, const char *path)
112 {
113         struct connman_counter *counter;
114
115         DBG("owner %s path %s", owner, path);
116
117         counter = g_hash_table_lookup(counter_table, path);
118         if (counter == NULL)
119                 return -ESRCH;
120
121         if (g_strcmp0(owner, counter->owner) != 0)
122                 return -EACCES;
123
124         g_hash_table_remove(owner_mapping, counter->owner);
125         g_hash_table_remove(counter_table, counter->path);
126
127         return 0;
128 }
129
130 static void send_usage(struct connman_counter *counter,
131                                 struct connman_service *service)
132 {
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;
144         unsigned long time;
145
146         message = dbus_message_new_method_call(counter->owner, counter->path,
147                                         CONNMAN_COUNTER_INTERFACE, "Usage");
148         if (message == NULL)
149                 return;
150
151         dbus_message_set_no_reply(message, TRUE);
152
153         service_path = __connman_service_get_path(service);
154         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
155                                         &service_path, DBUS_TYPE_INVALID);
156
157         dbus_message_iter_init_append(message, &array);
158
159         connman_dbus_dict_open(&array, &dict);
160
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);
170
171         connman_dbus_dict_append_basic(&dict, "RX.Packets", DBUS_TYPE_UINT32,
172                                 &rx_packets);
173         connman_dbus_dict_append_basic(&dict, "TX.Packets", DBUS_TYPE_UINT32,
174                                 &tx_packets);
175         connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
176                                 &rx_bytes);
177         connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
178                                 &tx_bytes);
179         connman_dbus_dict_append_basic(&dict, "RX.Errors", DBUS_TYPE_UINT32,
180                                 &rx_errors);
181         connman_dbus_dict_append_basic(&dict, "TX.Errors", DBUS_TYPE_UINT32,
182                                 &tx_errors);
183         connman_dbus_dict_append_basic(&dict, "RX.Dropped", DBUS_TYPE_UINT32,
184                                 &rx_dropped);
185         connman_dbus_dict_append_basic(&dict, "TX.Dropped", DBUS_TYPE_UINT32,
186                                 &tx_dropped);
187         connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
188                                 &time);
189
190         connman_dbus_dict_close(&array, &dict);
191
192         g_dbus_send_message(connection, message);
193 }
194
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)
200 {
201         struct counter_data *data;
202         GHashTableIter iter;
203         gpointer key, value;
204
205         data = g_hash_table_lookup(stats_table, config);
206         if (data == NULL)
207                 return;
208
209         __connman_service_stats_update(data->service,
210                                 rx_packets, tx_packets,
211                                 rx_bytes, tx_bytes,
212                                 rx_errors, tx_errors,
213                                 rx_dropped, tx_dropped);
214
215         if (data->first_update == TRUE) {
216                 data->first_update = FALSE;
217                 return;
218         }
219
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;
223
224                 send_usage(counter, data->service);
225         }
226 }
227
228 static void release_counter(gpointer key, gpointer value, gpointer user_data)
229 {
230         struct connman_counter *counter = value;
231         DBusMessage *message;
232
233         DBG("owner %s path %s", counter->owner, counter->path);
234
235         message = dbus_message_new_method_call(counter->owner, counter->path,
236                                         CONNMAN_COUNTER_INTERFACE, "Release");
237         if (message == NULL)
238                 return;
239
240         dbus_message_set_no_reply(message, TRUE);
241
242         g_dbus_send_message(connection, message);
243 }
244
245 int __connman_counter_add_service(struct connman_service *service)
246 {
247         struct connman_ipconfig *config;
248         struct counter_data *data;
249
250         data = g_try_new0(struct counter_data, 1);
251         if (data == NULL)
252                 return -ENOMEM;
253
254         data->service = service;
255         data->first_update = TRUE;
256
257         config = __connman_service_get_ipconfig(service);
258         g_hash_table_replace(stats_table, config, data);
259
260         /*
261          * Trigger a first update to intialize the offset counters
262          * in the service.
263          */
264         __connman_rtnl_request_update();
265
266         return 0;
267 }
268
269 void __connman_counter_remove_service(struct connman_service *service)
270 {
271         struct connman_ipconfig *config;
272
273         config = __connman_service_get_ipconfig(service);
274         g_hash_table_remove(stats_table, config);
275 }
276
277 int __connman_counter_init(void)
278 {
279         DBG("");
280
281         connection = connman_dbus_get_connection();
282         if (connection == NULL)
283                 return -1;
284
285         stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
286                                                         NULL, remove_data);
287
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,
291                                                                 NULL, NULL);
292
293         return 0;
294 }
295
296 void __connman_counter_cleanup(void)
297 {
298         DBG("");
299
300         if (connection == NULL)
301                 return;
302
303         g_hash_table_foreach(counter_table, release_counter, NULL);
304
305         g_hash_table_destroy(owner_mapping);
306         g_hash_table_destroy(counter_table);
307
308         g_hash_table_destroy(stats_table);
309
310         dbus_connection_unref(connection);
311 }