Imported Upstream version 1.29
[platform/upstream/connman.git] / src / counter.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <errno.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection;
33
34 static GHashTable *counter_table;
35 static GHashTable *owner_mapping;
36
37 struct connman_counter {
38         char *owner;
39         char *path;
40         unsigned int interval;
41         guint watch;
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         __connman_rtnl_update_interval_remove(counter->interval);
51
52         __connman_service_counter_unregister(counter->path);
53
54         g_free(counter->owner);
55         g_free(counter->path);
56         g_free(counter);
57 }
58
59 static void owner_disconnect(DBusConnection *conn, 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         int err;
74
75         DBG("owner %s path %s interval %u", owner, path, interval);
76
77         counter = g_hash_table_lookup(counter_table, path);
78         if (counter)
79                 return -EEXIST;
80
81         counter = g_try_new0(struct connman_counter, 1);
82         if (!counter)
83                 return -ENOMEM;
84
85         counter->owner = g_strdup(owner);
86         counter->path = g_strdup(path);
87
88         err = __connman_service_counter_register(counter->path);
89         if (err < 0) {
90                 g_free(counter->owner);
91                 g_free(counter->path);
92                 g_free(counter);
93                 return err;
94         }
95
96         g_hash_table_replace(counter_table, counter->path, counter);
97         g_hash_table_replace(owner_mapping, counter->owner, counter);
98
99         counter->interval = interval;
100         __connman_rtnl_update_interval_add(counter->interval);
101
102         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
103                                         owner_disconnect, counter, NULL);
104
105         return 0;
106 }
107
108 int __connman_counter_unregister(const char *owner, const char *path)
109 {
110         struct connman_counter *counter;
111
112         DBG("owner %s path %s", owner, path);
113
114         counter = g_hash_table_lookup(counter_table, path);
115         if (!counter)
116                 return -ESRCH;
117
118         if (g_strcmp0(owner, counter->owner) != 0)
119                 return -EACCES;
120
121         if (counter->watch > 0)
122                 g_dbus_remove_watch(connection, counter->watch);
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 void __connman_counter_send_usage(const char *path,
131                                         DBusMessage *message)
132 {
133         struct connman_counter *counter;
134
135         counter = g_hash_table_lookup(counter_table, path);
136         if (!counter) {
137                 if (message)
138                         dbus_message_unref(message);
139                 return;
140         }
141
142         dbus_message_set_destination(message, counter->owner);
143         dbus_message_set_path(message, counter->path);
144         dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
145         dbus_message_set_member(message, "Usage");
146         dbus_message_set_no_reply(message, TRUE);
147
148         g_dbus_send_message(connection, message);
149 }
150
151 static void release_counter(gpointer key, gpointer value, gpointer user_data)
152 {
153         struct connman_counter *counter = value;
154         DBusMessage *message;
155
156         DBG("owner %s path %s", counter->owner, counter->path);
157
158         if (counter->watch > 0)
159                 g_dbus_remove_watch(connection, counter->watch);
160
161         message = dbus_message_new_method_call(counter->owner, counter->path,
162                                         CONNMAN_COUNTER_INTERFACE, "Release");
163         if (!message)
164                 return;
165
166         dbus_message_set_no_reply(message, TRUE);
167
168         g_dbus_send_message(connection, message);
169 }
170
171 int __connman_counter_init(void)
172 {
173         DBG("");
174
175         connection = connman_dbus_get_connection();
176         if (!connection)
177                 return -1;
178
179         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
180                                                         NULL, remove_counter);
181         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
182                                                                 NULL, NULL);
183
184         return 0;
185 }
186
187 void __connman_counter_cleanup(void)
188 {
189         DBG("");
190
191         if (!connection)
192                 return;
193
194         g_hash_table_foreach(counter_table, release_counter, NULL);
195
196         g_hash_table_destroy(owner_mapping);
197         g_hash_table_destroy(counter_table);
198
199         dbus_connection_unref(connection);
200 }