Track service types and not device types for technologies listing
[platform/upstream/connman.git] / src / notifier.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 = NULL;
31
32 static GSList *notifier_list = NULL;
33
34 static gint compare_priority(gconstpointer a, gconstpointer b)
35 {
36         const struct connman_notifier *notifier1 = a;
37         const struct connman_notifier *notifier2 = b;
38
39         return notifier2->priority - notifier1->priority;
40 }
41
42 /**
43  * connman_notifier_register:
44  * @notifier: notifier module
45  *
46  * Register a new notifier module
47  *
48  * Returns: %0 on success
49  */
50 int connman_notifier_register(struct connman_notifier *notifier)
51 {
52         DBG("notifier %p name %s", notifier, notifier->name);
53
54         notifier_list = g_slist_insert_sorted(notifier_list, notifier,
55                                                         compare_priority);
56
57         return 0;
58 }
59
60 /**
61  * connman_notifier_unregister:
62  * @notifier: notifier module
63  *
64  * Remove a previously registered notifier module
65  */
66 void connman_notifier_unregister(struct connman_notifier *notifier)
67 {
68         DBG("notifier %p name %s", notifier, notifier->name);
69
70         notifier_list = g_slist_remove(notifier_list, notifier);
71 }
72
73 static void technology_enabled(enum connman_device_type type,
74                                                 connman_bool_t enabled)
75 {
76         GSList *list;
77         DBusMessage *signal;
78         DBusMessageIter entry, value, iter;
79         const char *key = "EnabledTechnologies";
80
81         DBG("type %d enabled %d", type, enabled);
82
83         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
84                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
85         if (signal == NULL)
86                 goto done;
87
88         dbus_message_iter_init_append(signal, &entry);
89
90         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
91
92         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
93                         DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
94                                                                 &value);
95
96         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
97                                         DBUS_TYPE_STRING_AS_STRING, &iter);
98         __connman_notifier_list(TRUE, &iter);
99         dbus_message_iter_close_container(&value, &iter);
100
101         dbus_message_iter_close_container(&entry, &value);
102
103         g_dbus_send_message(connection, signal);
104
105 done:
106         for (list = notifier_list; list; list = list->next) {
107                 struct connman_notifier *notifier = list->data;
108
109                 if (notifier->device_enabled)
110                         notifier->device_enabled(type, enabled);
111         }
112 }
113
114 static void technology_registered(enum connman_service_type type,
115                                                 connman_bool_t registered)
116 {
117         DBusMessage *signal;
118         DBusMessageIter entry, value, iter;
119         const char *key = "Technologies";
120
121         DBG("type %d registered %d", type, registered);
122
123         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
124                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
125         if (signal == NULL)
126                 return;
127
128         dbus_message_iter_init_append(signal, &entry);
129
130         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
131
132         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
133                         DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
134                                                                 &value);
135
136         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
137                                         DBUS_TYPE_STRING_AS_STRING, &iter);
138         __connman_notifier_list(FALSE, &iter);
139         dbus_message_iter_close_container(&value, &iter);
140
141         dbus_message_iter_close_container(&entry, &value);
142
143         g_dbus_send_message(connection, signal);
144 }
145
146 static const char *type2string(enum connman_service_type type)
147 {
148         switch (type) {
149         case CONNMAN_SERVICE_TYPE_UNKNOWN:
150                 break;
151         case CONNMAN_SERVICE_TYPE_ETHERNET:
152                 return "ethernet";
153         case CONNMAN_SERVICE_TYPE_WIFI:
154                 return "wifi";
155         case CONNMAN_SERVICE_TYPE_WIMAX:
156                 return "wimax";
157         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
158                 return "bluetooth";
159         case CONNMAN_SERVICE_TYPE_CELLULAR:
160                 return "cellular";
161         }
162
163         return NULL;
164 }
165
166 static volatile gint registered[10];
167 static volatile gint enabled[10];
168
169 void __connman_notifier_list(gboolean powered, DBusMessageIter *iter)
170 {
171         int i;
172
173         for (i = 0; i < 10; i++) {
174                 const char *type = type2string(i);
175                 gint count;
176
177                 if (type == NULL)
178                         continue;
179
180                 if (powered == TRUE)
181                         count = g_atomic_int_get(&enabled[i]);
182                 else
183                         count = g_atomic_int_get(&registered[i]);
184
185                 if (count > 0)
186                         dbus_message_iter_append_basic(iter,
187                                                 DBUS_TYPE_STRING, &type);
188         }
189 }
190
191 void __connman_notifier_register(enum connman_service_type type)
192 {
193         DBG("type %d", type);
194
195         switch (type) {
196         case CONNMAN_SERVICE_TYPE_UNKNOWN:
197                 return;
198         case CONNMAN_SERVICE_TYPE_ETHERNET:
199         case CONNMAN_SERVICE_TYPE_WIFI:
200         case CONNMAN_SERVICE_TYPE_WIMAX:
201         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
202         case CONNMAN_SERVICE_TYPE_CELLULAR:
203                 break;
204         }
205
206         if (g_atomic_int_exchange_and_add(&registered[type], 1) == 0)
207                 technology_registered(type, TRUE);
208 }
209
210 void __connman_notifier_unregister(enum connman_service_type type)
211 {
212         DBG("type %d", type);
213
214         switch (type) {
215         case CONNMAN_SERVICE_TYPE_UNKNOWN:
216                 return;
217         case CONNMAN_SERVICE_TYPE_ETHERNET:
218         case CONNMAN_SERVICE_TYPE_WIFI:
219         case CONNMAN_SERVICE_TYPE_WIMAX:
220         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
221         case CONNMAN_SERVICE_TYPE_CELLULAR:
222                 break;
223         }
224
225         if (g_atomic_int_dec_and_test(&registered[type]) == TRUE)
226                 technology_registered(type, FALSE);
227 }
228
229 void __connman_notifier_enable(enum connman_service_type type)
230 {
231         DBG("type %d", type);
232
233         switch (type) {
234         case CONNMAN_SERVICE_TYPE_UNKNOWN:
235                 return;
236         case CONNMAN_SERVICE_TYPE_ETHERNET:
237         case CONNMAN_SERVICE_TYPE_WIFI:
238         case CONNMAN_SERVICE_TYPE_WIMAX:
239         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
240         case CONNMAN_SERVICE_TYPE_CELLULAR:
241                 break;
242         }
243
244         if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
245                 technology_enabled(type, TRUE);
246 }
247
248 void __connman_notifier_disable(enum connman_service_type type)
249 {
250         DBG("type %d", type);
251
252         switch (type) {
253         case CONNMAN_SERVICE_TYPE_UNKNOWN:
254                 return;
255         case CONNMAN_SERVICE_TYPE_ETHERNET:
256         case CONNMAN_SERVICE_TYPE_WIFI:
257         case CONNMAN_SERVICE_TYPE_WIMAX:
258         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
259         case CONNMAN_SERVICE_TYPE_CELLULAR:
260                 break;
261         }
262
263         if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
264                 technology_enabled(type, FALSE);
265 }
266
267 void __connman_notifier_offline_mode(connman_bool_t enabled)
268 {
269         GSList *list;
270
271         DBG("enabled %d", enabled);
272
273         __connman_profile_changed(FALSE);
274
275         for (list = notifier_list; list; list = list->next) {
276                 struct connman_notifier *notifier = list->data;
277
278                 if (notifier->offline_mode)
279                         notifier->offline_mode(enabled);
280         }
281 }
282
283 int __connman_notifier_init(void)
284 {
285         DBG("");
286
287         connection = connman_dbus_get_connection();
288
289         return 0;
290 }
291
292 void __connman_notifier_cleanup(void)
293 {
294         DBG("");
295
296         dbus_connection_unref(connection);
297 }