dbus: Add connman_dbus_setting_changed_*()
[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         unsigned int interval;
39         guint watch;
40 };
41
42 static void remove_counter(gpointer user_data)
43 {
44         struct connman_counter *counter = user_data;
45
46         DBG("owner %s path %s", counter->owner, counter->path);
47
48         __connman_rtnl_update_interval_remove(counter->interval);
49
50         __connman_service_counter_unregister(counter->path);
51
52         g_free(counter->owner);
53         g_free(counter->path);
54         g_free(counter);
55 }
56
57 static void owner_disconnect(DBusConnection *connection, void *user_data)
58 {
59         struct connman_counter *counter = user_data;
60
61         DBG("owner %s path %s", counter->owner, counter->path);
62
63         g_hash_table_remove(owner_mapping, counter->owner);
64         g_hash_table_remove(counter_table, counter->path);
65 }
66
67 int __connman_counter_register(const char *owner, const char *path,
68                                                 unsigned int interval)
69 {
70         struct connman_counter *counter;
71         int err;
72
73         DBG("owner %s path %s interval %u", owner, path, interval);
74
75         counter = g_hash_table_lookup(counter_table, path);
76         if (counter != NULL)
77                 return -EEXIST;
78
79         counter = g_try_new0(struct connman_counter, 1);
80         if (counter == NULL)
81                 return -ENOMEM;
82
83         counter->owner = g_strdup(owner);
84         counter->path = g_strdup(path);
85
86         err = __connman_service_counter_register(counter->path);
87         if (err < 0) {
88                 g_free(counter->owner);
89                 g_free(counter->path);
90                 g_free(counter);
91                 return err;
92         }
93
94         g_hash_table_replace(counter_table, counter->path, counter);
95         g_hash_table_replace(owner_mapping, counter->owner, counter);
96
97         counter->interval = interval;
98         __connman_rtnl_update_interval_add(counter->interval);
99
100         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
101                                         owner_disconnect, counter, NULL);
102
103         return 0;
104 }
105
106 int __connman_counter_unregister(const char *owner, const char *path)
107 {
108         struct connman_counter *counter;
109
110         DBG("owner %s path %s", owner, path);
111
112         counter = g_hash_table_lookup(counter_table, path);
113         if (counter == NULL)
114                 return -ESRCH;
115
116         if (g_strcmp0(owner, counter->owner) != 0)
117                 return -EACCES;
118
119         if (counter->watch > 0)
120                 g_dbus_remove_watch(connection, counter->watch);
121
122         g_hash_table_remove(owner_mapping, counter->owner);
123         g_hash_table_remove(counter_table, counter->path);
124
125         return 0;
126 }
127
128 void __connman_counter_send_usage(const char *path,
129                                         DBusMessage *message)
130 {
131         struct connman_counter *counter;
132
133         counter = g_hash_table_lookup(counter_table, path);
134         if (counter == NULL)
135                 return;
136
137         dbus_message_set_destination(message, counter->owner);
138         dbus_message_set_path(message, counter->path);
139         dbus_message_set_interface(message, CONNMAN_COUNTER_INTERFACE);
140         dbus_message_set_member(message, "Usage");
141         dbus_message_set_no_reply(message, TRUE);
142
143         g_dbus_send_message(connection, message);
144 }
145
146 static void release_counter(gpointer key, gpointer value, gpointer user_data)
147 {
148         struct connman_counter *counter = value;
149         DBusMessage *message;
150
151         DBG("owner %s path %s", counter->owner, counter->path);
152
153         if (counter->watch > 0)
154                 g_dbus_remove_watch(connection, counter->watch);
155
156         message = dbus_message_new_method_call(counter->owner, counter->path,
157                                         CONNMAN_COUNTER_INTERFACE, "Release");
158         if (message == NULL)
159                 return;
160
161         dbus_message_set_no_reply(message, TRUE);
162
163         g_dbus_send_message(connection, message);
164 }
165
166 int __connman_counter_init(void)
167 {
168         DBG("");
169
170         connection = connman_dbus_get_connection();
171         if (connection == NULL)
172                 return -1;
173
174         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
175                                                         NULL, remove_counter);
176         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
177                                                                 NULL, NULL);
178
179         return 0;
180 }
181
182 void __connman_counter_cleanup(void)
183 {
184         DBG("");
185
186         if (connection == NULL)
187                 return;
188
189         g_hash_table_foreach(counter_table, release_counter, NULL);
190
191         g_hash_table_destroy(owner_mapping);
192         g_hash_table_destroy(counter_table);
193
194         dbus_connection_unref(connection);
195 }