Centralize rntl update timers
[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         unsigned int interval;
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         __connman_rtnl_update_interval_remove(counter->interval);
67
68         g_free(counter->owner);
69         g_free(counter->path);
70         g_free(counter);
71 }
72
73 static void owner_disconnect(DBusConnection *connection, void *user_data)
74 {
75         struct connman_counter *counter = user_data;
76
77         DBG("owner %s path %s", counter->owner, counter->path);
78
79         g_hash_table_remove(owner_mapping, counter->owner);
80         g_hash_table_remove(counter_table, counter->path);
81 }
82
83 int __connman_counter_register(const char *owner, const char *path,
84                                                 unsigned int interval)
85 {
86         struct connman_counter *counter;
87
88         DBG("owner %s path %s interval %u", owner, path, interval);
89
90         counter = g_hash_table_lookup(counter_table, path);
91         if (counter != NULL)
92                 return -EEXIST;
93
94         counter = g_try_new0(struct connman_counter, 1);
95         if (counter == NULL)
96                 return -ENOMEM;
97
98         counter->owner = g_strdup(owner);
99         counter->path = g_strdup(path);
100
101         g_hash_table_replace(counter_table, counter->path, counter);
102         g_hash_table_replace(owner_mapping, counter->owner, counter);
103
104         counter->interval = interval;
105         __connman_rtnl_update_interval_add(counter->interval);
106
107         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
108                                         owner_disconnect, counter, NULL);
109
110         return 0;
111 }
112
113 int __connman_counter_unregister(const char *owner, const char *path)
114 {
115         struct connman_counter *counter;
116
117         DBG("owner %s path %s", owner, path);
118
119         counter = g_hash_table_lookup(counter_table, path);
120         if (counter == NULL)
121                 return -ESRCH;
122
123         if (g_strcmp0(owner, counter->owner) != 0)
124                 return -EACCES;
125
126         g_hash_table_remove(owner_mapping, counter->owner);
127         g_hash_table_remove(counter_table, counter->path);
128
129         return 0;
130 }
131
132 static void send_usage(struct connman_counter *counter,
133                                         struct connman_stats *stats)
134 {
135         DBusMessage *message;
136         DBusMessageIter array, dict;
137
138         message = dbus_message_new_method_call(counter->owner, counter->path,
139                                         CONNMAN_COUNTER_INTERFACE, "Usage");
140         if (message == NULL)
141                 return;
142
143         dbus_message_set_no_reply(message, TRUE);
144
145         dbus_message_iter_init_append(message, &array);
146
147         connman_dbus_dict_open(&array, &dict);
148
149         connman_dbus_dict_append_basic(&dict, "Interface",
150                                         DBUS_TYPE_STRING, &stats->interface);
151         connman_dbus_dict_append_basic(&dict, "RX.Bytes",
152                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
153         connman_dbus_dict_append_basic(&dict, "TX.Bytes",
154                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
155
156         connman_dbus_dict_close(&array, &dict);
157
158         g_dbus_send_message(connection, message);
159 }
160
161 void __connman_counter_notify(const char *interface,
162                                 unsigned int rx_bytes, unsigned int tx_bytes)
163 {
164         struct connman_stats *stats;
165         GHashTableIter iter;
166         gpointer key, value;
167
168         stats = g_hash_table_lookup(stats_table, interface);
169         if (stats != NULL)
170                 goto update;
171
172         stats = g_try_new0(struct connman_stats, 1);
173         if (stats == NULL)
174                 return;
175
176         stats->interface = g_strdup(interface);
177
178         g_hash_table_replace(stats_table, stats->interface, stats);
179
180 update:
181         if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
182                 return;
183
184         stats->rx_bytes = rx_bytes;
185         stats->tx_bytes = tx_bytes;
186
187         g_hash_table_iter_init(&iter, counter_table);
188
189         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
190                 struct connman_counter *counter = value;
191
192                 send_usage(counter, stats);
193         }
194 }
195
196 static void release_counter(gpointer key, gpointer value, gpointer user_data)
197 {
198         struct connman_counter *counter = value;
199         DBusMessage *message;
200
201         DBG("owner %s path %s", counter->owner, counter->path);
202
203         message = dbus_message_new_method_call(counter->owner, counter->path,
204                                         CONNMAN_COUNTER_INTERFACE, "Release");
205         if (message == NULL)
206                 return;
207
208         dbus_message_set_no_reply(message, TRUE);
209
210         g_dbus_send_message(connection, message);
211 }
212
213 int __connman_counter_init(void)
214 {
215         DBG("");
216
217         connection = connman_dbus_get_connection();
218         if (connection == NULL)
219                 return -1;
220
221         stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
222                                                         NULL, remove_stats);
223
224         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
225                                                         NULL, remove_counter);
226         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
227                                                                 NULL, NULL);
228
229         return 0;
230 }
231
232 void __connman_counter_cleanup(void)
233 {
234         DBG("");
235
236         if (connection == NULL)
237                 return;
238
239         g_hash_table_foreach(counter_table, release_counter, NULL);
240
241         g_hash_table_destroy(owner_mapping);
242         g_hash_table_destroy(counter_table);
243
244         g_hash_table_destroy(stats_table);
245
246         dbus_connection_unref(connection);
247 }