Check interval parameters and request first update
[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 *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         if (interval < 1)
90                 return -EINVAL;
91
92         counter = g_hash_table_lookup(counter_table, path);
93         if (counter != NULL)
94                 return -EEXIST;
95
96         counter = g_try_new0(struct connman_counter, 1);
97         if (counter == NULL)
98                 return -ENOMEM;
99
100         counter->owner = g_strdup(owner);
101         counter->path = g_strdup(path);
102
103         g_hash_table_replace(counter_table, counter->path, counter);
104         g_hash_table_replace(owner_mapping, counter->owner, counter);
105
106         counter->timeout = g_timeout_add_seconds(interval,
107                                                 counter_timeout, counter);
108
109         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
110                                         owner_disconnect, counter, NULL);
111
112         __connman_rtnl_request_update();
113
114         return 0;
115 }
116
117 int __connman_counter_unregister(const char *owner, const char *path)
118 {
119         struct connman_counter *counter;
120
121         DBG("owner %s path %s", owner, path);
122
123         counter = g_hash_table_lookup(counter_table, path);
124         if (counter == NULL)
125                 return -ESRCH;
126
127         if (g_strcmp0(owner, counter->owner) != 0)
128                 return -EACCES;
129
130         g_hash_table_remove(owner_mapping, counter->owner);
131         g_hash_table_remove(counter_table, counter->path);
132
133         return 0;
134 }
135
136 static void send_usage(struct connman_counter *counter)
137 {
138         DBusMessage *message;
139         DBusMessageIter array, dict;
140
141         message = dbus_message_new_method_call(counter->owner, counter->path,
142                                         CONNMAN_COUNTER_INTERFACE, "Usage");
143         if (message == NULL)
144                 return;
145
146         dbus_message_set_no_reply(message, TRUE);
147
148         dbus_message_iter_init_append(message, &array);
149
150         connman_dbus_dict_open(&array, &dict);
151
152         connman_dbus_dict_append_basic(&dict, "RX.Bytes",
153                                         DBUS_TYPE_UINT32, &counter->rx_bytes);
154         connman_dbus_dict_append_basic(&dict, "TX.Bytes",
155                                         DBUS_TYPE_UINT32, &counter->tx_bytes);
156
157         connman_dbus_dict_close(&array, &dict);
158
159         g_dbus_send_message(connection, message);
160 }
161
162 void __connman_counter_notify(unsigned int rx_bytes, unsigned int tx_bytes)
163 {
164         GHashTableIter iter;
165         gpointer key, value;
166
167         g_hash_table_iter_init(&iter, counter_table);
168
169         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
170                 struct connman_counter *counter = value;
171
172                 if (counter->rx_bytes == rx_bytes &&
173                                         counter->tx_bytes == tx_bytes)
174                         continue;
175
176                 counter->rx_bytes = rx_bytes;
177                 counter->tx_bytes = tx_bytes;
178
179                 send_usage(counter);
180         }
181 }
182
183 static void release_counter(gpointer key, gpointer value, gpointer user_data)
184 {
185         struct connman_counter *counter = value;
186         DBusMessage *message;
187
188         DBG("owner %s path %s", counter->owner, counter->path);
189
190         message = dbus_message_new_method_call(counter->owner, counter->path,
191                                         CONNMAN_COUNTER_INTERFACE, "Release");
192         if (message == NULL)
193                 return;
194
195         dbus_message_set_no_reply(message, TRUE);
196
197         g_dbus_send_message(connection, message);
198 }
199
200 int __connman_counter_init(void)
201 {
202         DBG("");
203
204         connection = connman_dbus_get_connection();
205         if (connection == NULL)
206                 return -1;
207
208         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
209                                                         NULL, remove_counter);
210         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
211                                                                 NULL, NULL);
212
213         return 0;
214 }
215
216 void __connman_counter_cleanup(void)
217 {
218         DBG("");
219
220         if (connection == NULL)
221                 return;
222
223         g_hash_table_foreach(counter_table, release_counter, NULL);
224
225         g_hash_table_destroy(owner_mapping);
226         g_hash_table_destroy(counter_table);
227
228         dbus_connection_unref(connection);
229 }