Initialize offset counters
[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_bytes;
137         unsigned long tx_bytes;
138         unsigned long time;
139
140         message = dbus_message_new_method_call(counter->owner, counter->path,
141                                         CONNMAN_COUNTER_INTERFACE, "Usage");
142         if (message == NULL)
143                 return;
144
145         dbus_message_set_no_reply(message, TRUE);
146
147         service_path = __connman_service_get_path(service);
148         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
149                                         &service_path, DBUS_TYPE_INVALID);
150
151         dbus_message_iter_init_append(message, &array);
152
153         connman_dbus_dict_open(&array, &dict);
154
155         rx_bytes = __connman_service_stats_get_rx_bytes(service);
156         tx_bytes = __connman_service_stats_get_tx_bytes(service);
157         time = __connman_service_stats_get_time(service);
158
159         connman_dbus_dict_append_basic(&dict, "RX.Bytes", DBUS_TYPE_UINT32,
160                                 &rx_bytes);
161         connman_dbus_dict_append_basic(&dict, "TX.Bytes", DBUS_TYPE_UINT32,
162                                 &tx_bytes);
163         connman_dbus_dict_append_basic(&dict, "Time", DBUS_TYPE_UINT32,
164                                 &time);
165
166         connman_dbus_dict_close(&array, &dict);
167
168         g_dbus_send_message(connection, message);
169 }
170
171 void __connman_counter_notify(struct connman_ipconfig *config,
172                                 unsigned int rx_bytes, unsigned int tx_bytes)
173 {
174         struct counter_data *data;
175         GHashTableIter iter;
176         gpointer key, value;
177
178         data = g_hash_table_lookup(stats_table, config);
179         if (data == NULL)
180                 return;
181
182         __connman_service_stats_update(data->service, rx_bytes, tx_bytes);
183
184         if (data->first_update == TRUE) {
185                 data->first_update = FALSE;
186                 return;
187         }
188
189         g_hash_table_iter_init(&iter, counter_table);
190         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
191                 struct connman_counter *counter = value;
192
193                 send_usage(counter, data->service);
194         }
195 }
196
197 static void release_counter(gpointer key, gpointer value, gpointer user_data)
198 {
199         struct connman_counter *counter = value;
200         DBusMessage *message;
201
202         DBG("owner %s path %s", counter->owner, counter->path);
203
204         message = dbus_message_new_method_call(counter->owner, counter->path,
205                                         CONNMAN_COUNTER_INTERFACE, "Release");
206         if (message == NULL)
207                 return;
208
209         dbus_message_set_no_reply(message, TRUE);
210
211         g_dbus_send_message(connection, message);
212 }
213
214 int __connman_counter_add_service(struct connman_service *service)
215 {
216         struct connman_ipconfig *config;
217         struct counter_data *data;
218
219         data = g_try_new0(struct counter_data, 1);
220         if (data == NULL)
221                 return -ENOMEM;
222
223         data->service = service;
224         data->first_update = TRUE;
225
226         config = __connman_service_get_ipconfig(service);
227         g_hash_table_replace(stats_table, config, data);
228
229         /*
230          * Trigger a first update to intialize the offset counters
231          * in the service.
232          */
233         __connman_rtnl_request_update();
234
235         return 0;
236 }
237
238 void __connman_counter_remove_service(struct connman_service *service)
239 {
240         struct connman_ipconfig *config;
241
242         config = __connman_service_get_ipconfig(service);
243         g_hash_table_remove(stats_table, config);
244 }
245
246 int __connman_counter_init(void)
247 {
248         DBG("");
249
250         connection = connman_dbus_get_connection();
251         if (connection == NULL)
252                 return -1;
253
254         stats_table = g_hash_table_new_full(g_direct_hash, g_str_equal,
255                                                         NULL, remove_data);
256
257         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
258                                                         NULL, remove_counter);
259         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
260                                                                 NULL, NULL);
261
262         return 0;
263 }
264
265 void __connman_counter_cleanup(void)
266 {
267         DBG("");
268
269         if (connection == NULL)
270                 return;
271
272         g_hash_table_foreach(counter_table, release_counter, NULL);
273
274         g_hash_table_destroy(owner_mapping);
275         g_hash_table_destroy(counter_table);
276
277         g_hash_table_destroy(stats_table);
278
279         dbus_connection_unref(connection);
280 }