Add support for roaming/home network statistic
[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         connman_bool_t first_update;
42 };
43
44 static void remove_counter(gpointer user_data)
45 {
46         struct connman_counter *counter = user_data;
47
48         DBG("owner %s path %s", counter->owner, counter->path);
49
50         if (counter->watch > 0)
51                 g_dbus_remove_watch(connection, counter->watch);
52
53         __connman_rtnl_update_interval_remove(counter->interval);
54
55         g_free(counter->owner);
56         g_free(counter->path);
57         g_free(counter);
58 }
59
60 static void owner_disconnect(DBusConnection *connection, void *user_data)
61 {
62         struct connman_counter *counter = user_data;
63
64         DBG("owner %s path %s", counter->owner, counter->path);
65
66         g_hash_table_remove(owner_mapping, counter->owner);
67         g_hash_table_remove(counter_table, counter->path);
68 }
69
70 int __connman_counter_register(const char *owner, const char *path,
71                                                 unsigned int interval)
72 {
73         struct connman_counter *counter;
74
75         DBG("owner %s path %s interval %u", owner, path, interval);
76
77         counter = g_hash_table_lookup(counter_table, path);
78         if (counter != NULL)
79                 return -EEXIST;
80
81         counter = g_try_new0(struct connman_counter, 1);
82         if (counter == NULL)
83                 return -ENOMEM;
84
85         counter->owner = g_strdup(owner);
86         counter->path = g_strdup(path);
87         counter->first_update = TRUE;
88
89         g_hash_table_replace(counter_table, counter->path, counter);
90         g_hash_table_replace(owner_mapping, counter->owner, counter);
91
92         counter->interval = interval;
93         __connman_rtnl_update_interval_add(counter->interval);
94
95         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
96                                         owner_disconnect, counter, NULL);
97
98         return 0;
99 }
100
101 int __connman_counter_unregister(const char *owner, const char *path)
102 {
103         struct connman_counter *counter;
104
105         DBG("owner %s path %s", owner, path);
106
107         counter = g_hash_table_lookup(counter_table, path);
108         if (counter == NULL)
109                 return -ESRCH;
110
111         if (g_strcmp0(owner, counter->owner) != 0)
112                 return -EACCES;
113
114         g_hash_table_remove(owner_mapping, counter->owner);
115         g_hash_table_remove(counter_table, counter->path);
116
117         return 0;
118 }
119
120 static void send_usage(struct connman_counter *counter,
121                                 struct connman_service *service)
122 {
123         DBusMessage *message;
124         const char *service_path;
125
126         message = dbus_message_new_method_call(counter->owner, counter->path,
127                                         CONNMAN_COUNTER_INTERFACE, "Usage");
128         if (message == NULL)
129                 return;
130
131         dbus_message_set_no_reply(message, TRUE);
132
133         service_path = __connman_service_get_path(service);
134         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
135                                         &service_path, DBUS_TYPE_INVALID);
136
137         __connman_service_stats_append(service, message, counter->first_update);
138
139         counter->first_update = FALSE;
140
141         g_dbus_send_message(connection, message);
142 }
143
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)
149 {
150         struct connman_service *service;
151         GHashTableIter iter;
152         gpointer key, value;
153
154         service = g_hash_table_lookup(stats_table, config);
155         if (service == NULL)
156                 return;
157
158         if (__connman_service_stats_update(service,
159                                 rx_packets, tx_packets,
160                                 rx_bytes, tx_bytes,
161                                 rx_errors, tx_errors,
162                                 rx_dropped, tx_dropped) == FALSE) {
163                 /* first update, counters are now initialized */
164                 return;
165         }
166
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;
170
171                 send_usage(counter, service);
172         }
173 }
174
175 static void release_counter(gpointer key, gpointer value, gpointer user_data)
176 {
177         struct connman_counter *counter = value;
178         DBusMessage *message;
179
180         DBG("owner %s path %s", counter->owner, counter->path);
181
182         message = dbus_message_new_method_call(counter->owner, counter->path,
183                                         CONNMAN_COUNTER_INTERFACE, "Release");
184         if (message == NULL)
185                 return;
186
187         dbus_message_set_no_reply(message, TRUE);
188
189         g_dbus_send_message(connection, message);
190 }
191
192 int __connman_counter_add_service(struct connman_service *service)
193 {
194         struct connman_ipconfig *config;
195
196         config = __connman_service_get_ipconfig(service);
197         g_hash_table_replace(stats_table, config, service);
198
199         /*
200          * Trigger a first update to intialize the offset counters
201          * in the service.
202          */
203         __connman_rtnl_request_update();
204
205         return 0;
206 }
207
208 void __connman_counter_remove_service(struct connman_service *service)
209 {
210         struct connman_ipconfig *config;
211
212         config = __connman_service_get_ipconfig(service);
213         g_hash_table_remove(stats_table, config);
214 }
215
216 int __connman_counter_init(void)
217 {
218         DBG("");
219
220         connection = connman_dbus_get_connection();
221         if (connection == NULL)
222                 return -1;
223
224         stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
225                                                         NULL, NULL);
226
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,
230                                                                 NULL, NULL);
231
232         return 0;
233 }
234
235 void __connman_counter_cleanup(void)
236 {
237         DBG("");
238
239         if (connection == NULL)
240                 return;
241
242         g_hash_table_foreach(counter_table, release_counter, NULL);
243
244         g_hash_table_destroy(owner_mapping);
245         g_hash_table_destroy(counter_table);
246
247         g_hash_table_destroy(stats_table);
248
249         dbus_connection_unref(connection);
250 }