Add support for exporting basic interface statistics
[platform/upstream/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 *counter_table;
33 static GHashTable *owner_mapping;
34
35 struct connman_counter {
36         char *owner;
37         char *path;
38         guint timeout;
39         guint watch;
40         unsigned int rx_bytes;
41         unsigned int tx_bytes;
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         if (counter->timeout > 0)
54                 g_source_remove(counter->timeout);
55
56         g_free(counter->owner);
57         g_free(counter->path);
58         g_free(counter);
59 }
60
61 static void owner_disconnect(DBusConnection *connection, void *user_data)
62 {
63         struct connman_counter *counter = user_data;
64
65         DBG("owner %s path %s", counter->owner, counter->path);
66
67         g_hash_table_remove(owner_mapping, counter->owner);
68         g_hash_table_remove(counter_table, counter->path);
69 }
70
71 static gboolean counter_timeout(gpointer user_data)
72 {
73         struct connman_counter *counter = user_data;
74
75         DBG("owner %s path %s", counter->owner, counter->path);
76
77         __connman_rtnl_request_update();
78
79         return TRUE;
80 }
81
82 int __connman_counter_register(const char *owner, const char *path,
83                                                 unsigned int interval)
84 {
85         struct connman_counter *counter;
86
87         DBG("owner %s path %s interval %u", owner, path, interval);
88
89         counter = g_hash_table_lookup(counter_table, path);
90         if (counter != NULL)
91                 return -EEXIST;
92
93         counter = g_try_new0(struct connman_counter, 1);
94         if (counter == NULL)
95                 return -ENOMEM;
96
97         counter->owner = g_strdup(owner);
98         counter->path = g_strdup(path);
99
100         g_hash_table_replace(counter_table, counter->path, counter);
101         g_hash_table_replace(owner_mapping, counter->owner, counter);
102
103         counter->timeout = g_timeout_add_seconds(interval,
104                                                 counter_timeout, counter);
105
106         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
107                                         owner_disconnect, counter, NULL);
108
109         return 0;
110 }
111
112 int __connman_counter_unregister(const char *owner, const char *path)
113 {
114         struct connman_counter *counter;
115
116         DBG("owner %s path %s", owner, path);
117
118         counter = g_hash_table_lookup(counter_table, path);
119         if (counter == NULL)
120                 return -ESRCH;
121
122         if (g_strcmp0(owner, counter->owner) != 0)
123                 return -EACCES;
124
125         g_hash_table_remove(owner_mapping, counter->owner);
126         g_hash_table_remove(counter_table, counter->path);
127
128         return 0;
129 }
130
131 static void send_usage(struct connman_counter *counter)
132 {
133         DBusMessage *message;
134         DBusMessageIter array, dict;
135
136         message = dbus_message_new_method_call(counter->owner, counter->path,
137                                         CONNMAN_COUNTER_INTERFACE, "Usage");
138         if (message == NULL)
139                 return;
140
141         dbus_message_set_no_reply(message, TRUE);
142
143         dbus_message_iter_init_append(message, &array);
144
145         connman_dbus_dict_open(&array, &dict);
146
147         connman_dbus_dict_append_basic(&dict, "RX.Bytes",
148                                         DBUS_TYPE_UINT32, &counter->rx_bytes);
149         connman_dbus_dict_append_basic(&dict, "TX.Bytes",
150                                         DBUS_TYPE_UINT32, &counter->tx_bytes);
151
152         connman_dbus_dict_close(&array, &dict);
153
154         g_dbus_send_message(connection, message);
155 }
156
157 void __connman_counter_notify(unsigned int rx_bytes, unsigned int tx_bytes)
158 {
159         GHashTableIter iter;
160         gpointer key, value;
161
162         g_hash_table_iter_init(&iter, counter_table);
163
164         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
165                 struct connman_counter *counter = value;
166
167                 counter->rx_bytes = rx_bytes;
168                 counter->tx_bytes = tx_bytes;
169
170                 send_usage(counter);
171         }
172 }
173
174 static void release_counter(gpointer key, gpointer value, gpointer user_data)
175 {
176         struct connman_counter *counter = value;
177         DBusMessage *message;
178
179         DBG("owner %s path %s", counter->owner, counter->path);
180
181         message = dbus_message_new_method_call(counter->owner, counter->path,
182                                         CONNMAN_COUNTER_INTERFACE, "Release");
183         if (message == NULL)
184                 return;
185
186         dbus_message_set_no_reply(message, TRUE);
187
188         g_dbus_send_message(connection, message);
189 }
190
191 int __connman_counter_init(void)
192 {
193         DBG("");
194
195         connection = connman_dbus_get_connection();
196         if (connection == NULL)
197                 return -1;
198
199         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
200                                                         NULL, remove_counter);
201         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
202                                                                 NULL, NULL);
203
204         return 0;
205 }
206
207 void __connman_counter_cleanup(void)
208 {
209         DBG("");
210
211         if (connection == NULL)
212                 return;
213
214         g_hash_table_foreach(counter_table, release_counter, NULL);
215
216         g_hash_table_destroy(owner_mapping);
217         g_hash_table_destroy(counter_table);
218
219         dbus_connection_unref(connection);
220 }