Handle several counters simultaniously
[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         if (counter->watch > 0)
49                 g_dbus_remove_watch(connection, counter->watch);
50
51         __connman_rtnl_update_interval_remove(counter->interval);
52
53         __connman_service_counter_unregister(counter->path);
54
55         g_free(counter->owner);
56         g_free(counter->path);
57         g_free(counter);
58 }
59
60 static void owner_disconnect(DBusConnection *connection, void *user_data)
61 {
62         struct connman_counter *counter = user_data;
63
64         DBG("owner %s path %s", counter->owner, counter->path);
65
66         g_hash_table_remove(owner_mapping, counter->owner);
67         g_hash_table_remove(counter_table, counter->path);
68 }
69
70 int __connman_counter_register(const char *owner, const char *path,
71                                                 unsigned int interval)
72 {
73         struct connman_counter *counter;
74         int err;
75
76         DBG("owner %s path %s interval %u", owner, path, interval);
77
78         counter = g_hash_table_lookup(counter_table, path);
79         if (counter != NULL)
80                 return -EEXIST;
81
82         counter = g_try_new0(struct connman_counter, 1);
83         if (counter == NULL)
84                 return -ENOMEM;
85
86         counter->owner = g_strdup(owner);
87         counter->path = g_strdup(path);
88
89         err = __connman_service_counter_register(counter->path);
90         if (err < 0) {
91                 g_free(counter->owner);
92                 g_free(counter->path);
93                 g_free(counter);
94                 return err;
95         }
96
97         g_hash_table_replace(counter_table, counter->path, counter);
98         g_hash_table_replace(owner_mapping, counter->owner, counter);
99
100         counter->interval = interval;
101         __connman_rtnl_update_interval_add(counter->interval);
102
103         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
104                                         owner_disconnect, counter, NULL);
105
106         return 0;
107 }
108
109 int __connman_counter_unregister(const char *owner, const char *path)
110 {
111         struct connman_counter *counter;
112
113         DBG("owner %s path %s", owner, path);
114
115         counter = g_hash_table_lookup(counter_table, path);
116         if (counter == NULL)
117                 return -ESRCH;
118
119         if (g_strcmp0(owner, counter->owner) != 0)
120                 return -EACCES;
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         message = dbus_message_new_method_call(counter->owner, counter->path,
154                                         CONNMAN_COUNTER_INTERFACE, "Release");
155         if (message == NULL)
156                 return;
157
158         dbus_message_set_no_reply(message, TRUE);
159
160         g_dbus_send_message(connection, message);
161 }
162
163 int __connman_counter_init(void)
164 {
165         DBG("");
166
167         connection = connman_dbus_get_connection();
168         if (connection == NULL)
169                 return -1;
170
171         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
172                                                         NULL, remove_counter);
173         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
174                                                                 NULL, NULL);
175
176         return 0;
177 }
178
179 void __connman_counter_cleanup(void)
180 {
181         DBG("");
182
183         if (connection == NULL)
184                 return;
185
186         g_hash_table_foreach(counter_table, release_counter, NULL);
187
188         g_hash_table_destroy(owner_mapping);
189         g_hash_table_destroy(counter_table);
190
191         dbus_connection_unref(connection);
192 }