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