Don't forget to send signals for technology changes
[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 device_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_device_type_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 device_registered(enum connman_device_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_device_type_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_device_type type)
147 {
148         switch (type) {
149         case CONNMAN_DEVICE_TYPE_UNKNOWN:
150         case CONNMAN_DEVICE_TYPE_MBM:
151         case CONNMAN_DEVICE_TYPE_HSO:
152         case CONNMAN_DEVICE_TYPE_NOZOMI:
153         case CONNMAN_DEVICE_TYPE_HUAWEI:
154         case CONNMAN_DEVICE_TYPE_NOVATEL:
155         case CONNMAN_DEVICE_TYPE_VENDOR:
156                 break;
157         case CONNMAN_DEVICE_TYPE_ETHERNET:
158                 return "ethernet";
159         case CONNMAN_DEVICE_TYPE_WIFI:
160                 return "wifi";
161         case CONNMAN_DEVICE_TYPE_WIMAX:
162                 return "wimax";
163         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
164                 return "bluetooth";
165         case CONNMAN_DEVICE_TYPE_GPS:
166                 return "gps";
167         }
168
169         return NULL;
170 }
171
172 static volatile gint registered[20];
173 static volatile gint enabled[20];
174
175 void __connman_notifier_device_type_list(gboolean powered,
176                                                 DBusMessageIter *iter)
177 {
178         int i;
179
180         for (i = 0; i < 10; i++) {
181                 const char *type = type2string(i);
182                 gint count;
183
184                 if (type == NULL)
185                         continue;
186
187                 if (powered == TRUE)
188                         count = g_atomic_int_get(&enabled[i]);
189                 else
190                         count = g_atomic_int_get(&registered[i]);
191
192                 if (count > 0)
193                         dbus_message_iter_append_basic(iter,
194                                                 DBUS_TYPE_STRING, &type);
195         }
196 }
197
198 void __connman_notifier_device_type_register(enum connman_device_type type)
199 {
200         DBG("type %d", type);
201
202         switch (type) {
203         case CONNMAN_DEVICE_TYPE_UNKNOWN:
204         case CONNMAN_DEVICE_TYPE_MBM:
205         case CONNMAN_DEVICE_TYPE_HSO:
206         case CONNMAN_DEVICE_TYPE_NOZOMI:
207         case CONNMAN_DEVICE_TYPE_HUAWEI:
208         case CONNMAN_DEVICE_TYPE_NOVATEL:
209         case CONNMAN_DEVICE_TYPE_VENDOR:
210                 return;
211         case CONNMAN_DEVICE_TYPE_ETHERNET:
212         case CONNMAN_DEVICE_TYPE_WIFI:
213         case CONNMAN_DEVICE_TYPE_WIMAX:
214         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
215         case CONNMAN_DEVICE_TYPE_GPS:
216                 if (g_atomic_int_exchange_and_add(&registered[type], 1) == 0)
217                         device_registered(type, TRUE);
218                 break;
219         }
220 }
221
222 void __connman_notifier_device_type_unregister(enum connman_device_type type)
223 {
224         DBG("type %d", type);
225
226         switch (type) {
227         case CONNMAN_DEVICE_TYPE_UNKNOWN:
228         case CONNMAN_DEVICE_TYPE_MBM:
229         case CONNMAN_DEVICE_TYPE_HSO:
230         case CONNMAN_DEVICE_TYPE_NOZOMI:
231         case CONNMAN_DEVICE_TYPE_HUAWEI:
232         case CONNMAN_DEVICE_TYPE_NOVATEL:
233         case CONNMAN_DEVICE_TYPE_VENDOR:
234                 return;
235         case CONNMAN_DEVICE_TYPE_ETHERNET:
236         case CONNMAN_DEVICE_TYPE_WIFI:
237         case CONNMAN_DEVICE_TYPE_WIMAX:
238         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
239         case CONNMAN_DEVICE_TYPE_GPS:
240                 if (g_atomic_int_dec_and_test(&registered[type]) == TRUE)
241                         device_registered(type, FALSE);
242                 break;
243         }
244 }
245
246 void __connman_notifier_device_type_increase(enum connman_device_type type)
247 {
248         DBG("type %d", type);
249
250         switch (type) {
251         case CONNMAN_DEVICE_TYPE_UNKNOWN:
252         case CONNMAN_DEVICE_TYPE_MBM:
253         case CONNMAN_DEVICE_TYPE_HSO:
254         case CONNMAN_DEVICE_TYPE_NOZOMI:
255         case CONNMAN_DEVICE_TYPE_HUAWEI:
256         case CONNMAN_DEVICE_TYPE_NOVATEL:
257         case CONNMAN_DEVICE_TYPE_VENDOR:
258                 return;
259         case CONNMAN_DEVICE_TYPE_ETHERNET:
260         case CONNMAN_DEVICE_TYPE_WIFI:
261         case CONNMAN_DEVICE_TYPE_WIMAX:
262         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
263         case CONNMAN_DEVICE_TYPE_GPS:
264                 if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
265                         device_enabled(type, TRUE);
266                 break;
267         }
268 }
269
270 void __connman_notifier_device_type_decrease(enum connman_device_type type)
271 {
272         DBG("type %d", type);
273
274         switch (type) {
275         case CONNMAN_DEVICE_TYPE_UNKNOWN:
276         case CONNMAN_DEVICE_TYPE_MBM:
277         case CONNMAN_DEVICE_TYPE_HSO:
278         case CONNMAN_DEVICE_TYPE_NOZOMI:
279         case CONNMAN_DEVICE_TYPE_HUAWEI:
280         case CONNMAN_DEVICE_TYPE_NOVATEL:
281         case CONNMAN_DEVICE_TYPE_VENDOR:
282                 return;
283         case CONNMAN_DEVICE_TYPE_ETHERNET:
284         case CONNMAN_DEVICE_TYPE_WIFI:
285         case CONNMAN_DEVICE_TYPE_WIMAX:
286         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
287         case CONNMAN_DEVICE_TYPE_GPS:
288                 if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
289                         device_enabled(type, FALSE);
290                 break;
291         }
292 }
293
294 void __connman_notifier_offline_mode(connman_bool_t enabled)
295 {
296         GSList *list;
297
298         DBG("enabled %d", enabled);
299
300         __connman_profile_changed(FALSE);
301
302         for (list = notifier_list; list; list = list->next) {
303                 struct connman_notifier *notifier = list->data;
304
305                 if (notifier->offline_mode)
306                         notifier->offline_mode(enabled);
307         }
308 }
309
310 int __connman_notifier_init(void)
311 {
312         DBG("");
313
314         connection = connman_dbus_get_connection();
315
316         return 0;
317 }
318
319 void __connman_notifier_cleanup(void)
320 {
321         DBG("");
322
323         dbus_connection_unref(connection);
324 }