Update copyright information
[platform/upstream/connman.git] / src / notifier.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 = 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 #define MAX_TECHNOLOGIES 10
74
75 static volatile gint registered[MAX_TECHNOLOGIES];
76 static volatile gint enabled[MAX_TECHNOLOGIES];
77 static volatile gint connected[MAX_TECHNOLOGIES];
78
79 void __connman_notifier_list_registered(DBusMessageIter *iter, void *user_data)
80 {
81         int i;
82
83         for (i = 0; i < 10; i++) {
84                 const char *type = __connman_service_type2string(i);
85
86                 if (type == NULL)
87                         continue;
88
89                 if (g_atomic_int_get(&registered[i]) > 0)
90                         dbus_message_iter_append_basic(iter,
91                                                 DBUS_TYPE_STRING, &type);
92         }
93 }
94
95 void __connman_notifier_list_enabled(DBusMessageIter *iter, void *user_data)
96 {
97         int i;
98
99         for (i = 0; i < 10; i++) {
100                 const char *type = __connman_service_type2string(i);
101
102                 if (type == NULL)
103                         continue;
104
105                 if (g_atomic_int_get(&enabled[i]) > 0)
106                         dbus_message_iter_append_basic(iter,
107                                                 DBUS_TYPE_STRING, &type);
108         }
109 }
110
111 void __connman_notifier_list_connected(DBusMessageIter *iter, void *user_data)
112 {
113         int i;
114
115         for (i = 0; i < 10; i++) {
116                 const char *type = __connman_service_type2string(i);
117
118                 if (type == NULL)
119                         continue;
120
121                 if (g_atomic_int_get(&connected[i]) > 0)
122                         dbus_message_iter_append_basic(iter,
123                                                 DBUS_TYPE_STRING, &type);
124         }
125 }
126
127 static void technology_registered(enum connman_service_type type,
128                                                 connman_bool_t registered)
129 {
130         DBG("type %d registered %d", type, registered);
131
132         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
133                 CONNMAN_MANAGER_INTERFACE, "AvailableTechnologies",
134                 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
135 }
136
137 static void technology_enabled(enum connman_service_type type,
138                                                 connman_bool_t enabled)
139 {
140         GSList *list;
141
142         DBG("type %d enabled %d", type, enabled);
143
144         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
145                 CONNMAN_MANAGER_INTERFACE, "EnabledTechnologies",
146                 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
147
148         for (list = notifier_list; list; list = list->next) {
149                 struct connman_notifier *notifier = list->data;
150
151                 if (notifier->service_enabled)
152                         notifier->service_enabled(type, enabled);
153         }
154 }
155
156 static void technology_connected(enum connman_service_type type,
157                                                 connman_bool_t connected)
158 {
159         DBG("type %d connected %d", type, connected);
160
161         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
162                 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
163                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
164 }
165
166 void __connman_notifier_register(enum connman_service_type type)
167 {
168         DBG("type %d", type);
169
170         switch (type) {
171         case CONNMAN_SERVICE_TYPE_UNKNOWN:
172         case CONNMAN_SERVICE_TYPE_SYSTEM:
173         case CONNMAN_SERVICE_TYPE_VPN:
174                 return;
175         case CONNMAN_SERVICE_TYPE_ETHERNET:
176         case CONNMAN_SERVICE_TYPE_WIFI:
177         case CONNMAN_SERVICE_TYPE_WIMAX:
178         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
179         case CONNMAN_SERVICE_TYPE_CELLULAR:
180                 break;
181         }
182
183         if (g_atomic_int_exchange_and_add(&registered[type], 1) == 0)
184                 technology_registered(type, TRUE);
185 }
186
187 void __connman_notifier_unregister(enum connman_service_type type)
188 {
189         DBG("type %d", type);
190
191         switch (type) {
192         case CONNMAN_SERVICE_TYPE_UNKNOWN:
193         case CONNMAN_SERVICE_TYPE_SYSTEM:
194         case CONNMAN_SERVICE_TYPE_VPN:
195                 return;
196         case CONNMAN_SERVICE_TYPE_ETHERNET:
197         case CONNMAN_SERVICE_TYPE_WIFI:
198         case CONNMAN_SERVICE_TYPE_WIMAX:
199         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
200         case CONNMAN_SERVICE_TYPE_CELLULAR:
201                 break;
202         }
203
204         if (g_atomic_int_dec_and_test(&registered[type]) == TRUE)
205                 technology_registered(type, FALSE);
206 }
207
208 void __connman_notifier_enable(enum connman_service_type type)
209 {
210         DBG("type %d", type);
211
212         switch (type) {
213         case CONNMAN_SERVICE_TYPE_UNKNOWN:
214         case CONNMAN_SERVICE_TYPE_SYSTEM:
215         case CONNMAN_SERVICE_TYPE_VPN:
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_exchange_and_add(&enabled[type], 1) == 0)
226                 technology_enabled(type, TRUE);
227 }
228
229 void __connman_notifier_disable(enum connman_service_type type)
230 {
231         DBG("type %d", type);
232
233         switch (type) {
234         case CONNMAN_SERVICE_TYPE_UNKNOWN:
235         case CONNMAN_SERVICE_TYPE_SYSTEM:
236         case CONNMAN_SERVICE_TYPE_VPN:
237                 return;
238         case CONNMAN_SERVICE_TYPE_ETHERNET:
239         case CONNMAN_SERVICE_TYPE_WIFI:
240         case CONNMAN_SERVICE_TYPE_WIMAX:
241         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
242         case CONNMAN_SERVICE_TYPE_CELLULAR:
243                 break;
244         }
245
246         if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
247                 technology_enabled(type, FALSE);
248 }
249
250 void __connman_notifier_connect(enum connman_service_type type)
251 {
252         DBG("type %d", type);
253
254         switch (type) {
255         case CONNMAN_SERVICE_TYPE_UNKNOWN:
256         case CONNMAN_SERVICE_TYPE_SYSTEM:
257         case CONNMAN_SERVICE_TYPE_VPN:
258                 return;
259         case CONNMAN_SERVICE_TYPE_ETHERNET:
260         case CONNMAN_SERVICE_TYPE_WIFI:
261         case CONNMAN_SERVICE_TYPE_WIMAX:
262         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
263         case CONNMAN_SERVICE_TYPE_CELLULAR:
264                 break;
265         }
266
267         if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
268                 technology_connected(type, TRUE);
269 }
270
271 void __connman_notifier_disconnect(enum connman_service_type type)
272 {
273         DBG("type %d", type);
274
275         switch (type) {
276         case CONNMAN_SERVICE_TYPE_UNKNOWN:
277         case CONNMAN_SERVICE_TYPE_SYSTEM:
278         case CONNMAN_SERVICE_TYPE_VPN:
279                 return;
280         case CONNMAN_SERVICE_TYPE_ETHERNET:
281         case CONNMAN_SERVICE_TYPE_WIFI:
282         case CONNMAN_SERVICE_TYPE_WIMAX:
283         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
284         case CONNMAN_SERVICE_TYPE_CELLULAR:
285                 break;
286         }
287
288         if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
289                 technology_connected(type, FALSE);
290 }
291
292 static void technology_default(enum connman_service_type type)
293 {
294         const char *str;
295
296         str = __connman_service_type2string(type);
297         if (str == NULL)
298                 str = "";
299
300         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
301                         CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
302                                                 DBUS_TYPE_STRING, &str);
303 }
304
305 void __connman_notifier_default_changed(struct connman_service *service)
306 {
307         enum connman_service_type type = connman_service_get_type(service);
308         GSList *list;
309
310         technology_default(type);
311
312         for (list = notifier_list; list; list = list->next) {
313                 struct connman_notifier *notifier = list->data;
314
315                 if (notifier->default_changed)
316                         notifier->default_changed(service);
317         }
318 }
319
320 static void offlinemode_changed(dbus_bool_t enabled)
321 {
322         DBG("enabled %d", enabled);
323
324         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
325                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
326                                                 DBUS_TYPE_BOOLEAN, &enabled);
327 }
328
329 void __connman_notifier_offlinemode(connman_bool_t enabled)
330 {
331         GSList *list;
332
333         DBG("enabled %d", enabled);
334
335         __connman_profile_changed(FALSE);
336
337         offlinemode_changed(enabled);
338
339         for (list = notifier_list; list; list = list->next) {
340                 struct connman_notifier *notifier = list->data;
341
342                 if (notifier->offline_mode)
343                         notifier->offline_mode(enabled);
344         }
345 }
346
347 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
348 {
349         DBG("type %d", type);
350
351         switch (type) {
352         case CONNMAN_SERVICE_TYPE_UNKNOWN:
353         case CONNMAN_SERVICE_TYPE_SYSTEM:
354         case CONNMAN_SERVICE_TYPE_VPN:
355                 return FALSE;
356         case CONNMAN_SERVICE_TYPE_ETHERNET:
357         case CONNMAN_SERVICE_TYPE_WIFI:
358         case CONNMAN_SERVICE_TYPE_WIMAX:
359         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
360         case CONNMAN_SERVICE_TYPE_CELLULAR:
361                 break;
362         }
363
364         if (g_atomic_int_get(&enabled[type]) > 0)
365                 return TRUE;
366
367         return FALSE;
368 }
369
370 int __connman_notifier_init(void)
371 {
372         DBG("");
373
374         connection = connman_dbus_get_connection();
375
376         return 0;
377 }
378
379 void __connman_notifier_cleanup(void)
380 {
381         DBG("");
382
383         dbus_connection_unref(connection);
384 }