Add empty dictionary for roaming counters for now
[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         /* home counter */
160         connman_dbus_dict_open(&array, &dict);
161
162         rx_packets = __connman_service_stats_get_rx_packets(service);
163         tx_packets = __connman_service_stats_get_tx_packets(service);
164         rx_bytes = __connman_service_stats_get_rx_bytes(service);
165         tx_bytes = __connman_service_stats_get_tx_bytes(service);
166         rx_errors = __connman_service_stats_get_rx_errors(service);
167         tx_errors = __connman_service_stats_get_tx_errors(service);
168         rx_dropped = __connman_service_stats_get_rx_dropped(service);
169         tx_dropped = __connman_service_stats_get_tx_dropped(service);
170         time = __connman_service_stats_get_time(service);
171
172         connman_dbus_dict_append_basic(&dict, "RX.Packets", DBUS_TYPE_UINT32,
173                                 &rx_packets);
174         connman_dbus_dict_append_basic(&dict, "TX.Packets", DBUS_TYPE_UINT32,
175                                 &tx_packets);
176         connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
177                                 &rx_bytes);
178         connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
179                                 &tx_bytes);
180         connman_dbus_dict_append_basic(&dict, "RX.Errors", DBUS_TYPE_UINT32,
181                                 &rx_errors);
182         connman_dbus_dict_append_basic(&dict, "TX.Errors", DBUS_TYPE_UINT32,
183                                 &tx_errors);
184         connman_dbus_dict_append_basic(&dict, "RX.Dropped", DBUS_TYPE_UINT32,
185                                 &rx_dropped);
186         connman_dbus_dict_append_basic(&dict, "TX.Dropped", DBUS_TYPE_UINT32,
187                                 &tx_dropped);
188         connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
189                                 &time);
190
191         connman_dbus_dict_close(&array, &dict);
192
193         /* roaming counter */
194         connman_dbus_dict_open(&array, &dict);
195
196         connman_dbus_dict_close(&array, &dict);
197
198         g_dbus_send_message(connection, message);
199 }
200
201 void __connman_counter_notify(struct connman_ipconfig *config,
202                         unsigned int rx_packets, unsigned int tx_packets,
203                         unsigned int rx_bytes, unsigned int tx_bytes,
204                         unsigned int rx_errors, unsigned int tx_errors,
205                         unsigned int rx_dropped, unsigned int tx_dropped)
206 {
207         struct counter_data *data;
208         GHashTableIter iter;
209         gpointer key, value;
210
211         data = g_hash_table_lookup(stats_table, config);
212         if (data == NULL)
213                 return;
214
215         __connman_service_stats_update(data->service,
216                                 rx_packets, tx_packets,
217                                 rx_bytes, tx_bytes,
218                                 rx_errors, tx_errors,
219                                 rx_dropped, tx_dropped);
220
221         if (data->first_update == TRUE) {
222                 data->first_update = FALSE;
223                 return;
224         }
225
226         g_hash_table_iter_init(&iter, counter_table);
227         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
228                 struct connman_counter *counter = value;
229
230                 send_usage(counter, data->service);
231         }
232 }
233
234 static void release_counter(gpointer key, gpointer value, gpointer user_data)
235 {
236         struct connman_counter *counter = value;
237         DBusMessage *message;
238
239         DBG("owner %s path %s", counter->owner, counter->path);
240
241         message = dbus_message_new_method_call(counter->owner, counter->path,
242                                         CONNMAN_COUNTER_INTERFACE, "Release");
243         if (message == NULL)
244                 return;
245
246         dbus_message_set_no_reply(message, TRUE);
247
248         g_dbus_send_message(connection, message);
249 }
250
251 int __connman_counter_add_service(struct connman_service *service)
252 {
253         struct connman_ipconfig *config;
254         struct counter_data *data;
255
256         data = g_try_new0(struct counter_data, 1);
257         if (data == NULL)
258                 return -ENOMEM;
259
260         data->service = service;
261         data->first_update = TRUE;
262
263         config = __connman_service_get_ipconfig(service);
264         g_hash_table_replace(stats_table, config, data);
265
266         /*
267          * Trigger a first update to intialize the offset counters
268          * in the service.
269          */
270         __connman_rtnl_request_update();
271
272         return 0;
273 }
274
275 void __connman_counter_remove_service(struct connman_service *service)
276 {
277         struct connman_ipconfig *config;
278
279         config = __connman_service_get_ipconfig(service);
280         g_hash_table_remove(stats_table, config);
281 }
282
283 int __connman_counter_init(void)
284 {
285         DBG("");
286
287         connection = connman_dbus_get_connection();
288         if (connection == NULL)
289                 return -1;
290
291         stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
292                                                         NULL, remove_data);
293
294         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
295                                                         NULL, remove_counter);
296         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
297                                                                 NULL, NULL);
298
299         return 0;
300 }
301
302 void __connman_counter_cleanup(void)
303 {
304         DBG("");
305
306         if (connection == NULL)
307                 return;
308
309         g_hash_table_foreach(counter_table, release_counter, NULL);
310
311         g_hash_table_destroy(owner_mapping);
312         g_hash_table_destroy(counter_table);
313
314         g_hash_table_destroy(stats_table);
315
316         dbus_connection_unref(connection);
317 }