notifier: Limit Counter Underflow
[framework/connectivity/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 < MAX_TECHNOLOGIES; 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 < MAX_TECHNOLOGIES; 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 < MAX_TECHNOLOGIES; 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 unsigned int __connman_notifier_count_connected(void)
157 {
158         unsigned int i, count = 0;
159
160         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
161                 if (g_atomic_int_get(&connected[i]) > 0)
162                         count++;
163         }
164
165         return count;
166 }
167
168 const char *__connman_notifier_get_state(void)
169 {
170         unsigned int count = __connman_notifier_count_connected();
171
172         if (count > 0)
173                 return "online";
174
175         return "offline";
176 }
177
178 static void state_changed(void)
179 {
180         unsigned int count = __connman_notifier_count_connected();
181         char *state = "offline";
182         DBusMessage *signal;
183
184         if (count > 1)
185                 return;
186
187         if (count > 0)
188                 state = "online";
189
190         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
191                                 CONNMAN_MANAGER_INTERFACE, "State",
192                                                 DBUS_TYPE_STRING, &state);
193
194         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
195                                 CONNMAN_MANAGER_INTERFACE, "StateChanged");
196         if (signal == NULL)
197                 return;
198
199         dbus_message_append_args(signal, DBUS_TYPE_STRING, &state,
200                                                         DBUS_TYPE_INVALID);
201
202         g_dbus_send_message(connection, signal);
203 }
204
205 static void technology_connected(enum connman_service_type type,
206                                                 connman_bool_t connected)
207 {
208         DBG("type %d connected %d", type, connected);
209
210         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
211                 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
212                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
213
214         state_changed();
215 }
216
217 void __connman_notifier_register(enum connman_service_type type)
218 {
219         DBG("type %d", type);
220
221         switch (type) {
222         case CONNMAN_SERVICE_TYPE_UNKNOWN:
223         case CONNMAN_SERVICE_TYPE_SYSTEM:
224         case CONNMAN_SERVICE_TYPE_GPS:
225         case CONNMAN_SERVICE_TYPE_VPN:
226         case CONNMAN_SERVICE_TYPE_GADGET:
227                 return;
228         case CONNMAN_SERVICE_TYPE_ETHERNET:
229         case CONNMAN_SERVICE_TYPE_WIFI:
230         case CONNMAN_SERVICE_TYPE_WIMAX:
231         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
232         case CONNMAN_SERVICE_TYPE_CELLULAR:
233                 break;
234         }
235
236         if (g_atomic_int_exchange_and_add(&registered[type], 1) == 0)
237                 technology_registered(type, TRUE);
238 }
239
240 void __connman_notifier_unregister(enum connman_service_type type)
241 {
242         DBG("type %d", type);
243
244         if (g_atomic_int_get(&registered[type]) == 0) {
245                 connman_error("notifier unregister underflow");
246                 return;
247         }
248
249         switch (type) {
250         case CONNMAN_SERVICE_TYPE_UNKNOWN:
251         case CONNMAN_SERVICE_TYPE_SYSTEM:
252         case CONNMAN_SERVICE_TYPE_GPS:
253         case CONNMAN_SERVICE_TYPE_VPN:
254         case CONNMAN_SERVICE_TYPE_GADGET:
255                 return;
256         case CONNMAN_SERVICE_TYPE_ETHERNET:
257         case CONNMAN_SERVICE_TYPE_WIFI:
258         case CONNMAN_SERVICE_TYPE_WIMAX:
259         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
260         case CONNMAN_SERVICE_TYPE_CELLULAR:
261                 break;
262         }
263
264         if (g_atomic_int_dec_and_test(&registered[type]) == TRUE)
265                 technology_registered(type, FALSE);
266 }
267
268 void __connman_notifier_enable(enum connman_service_type type)
269 {
270         DBG("type %d", type);
271
272         switch (type) {
273         case CONNMAN_SERVICE_TYPE_UNKNOWN:
274         case CONNMAN_SERVICE_TYPE_SYSTEM:
275         case CONNMAN_SERVICE_TYPE_GPS:
276         case CONNMAN_SERVICE_TYPE_VPN:
277         case CONNMAN_SERVICE_TYPE_GADGET:
278                 return;
279         case CONNMAN_SERVICE_TYPE_ETHERNET:
280         case CONNMAN_SERVICE_TYPE_WIFI:
281         case CONNMAN_SERVICE_TYPE_WIMAX:
282         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
283         case CONNMAN_SERVICE_TYPE_CELLULAR:
284                 break;
285         }
286
287         if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
288                 technology_enabled(type, TRUE);
289 }
290
291 void __connman_notifier_disable(enum connman_service_type type)
292 {
293         DBG("type %d", type);
294
295         if (g_atomic_int_get(&enabled[type]) == 0) {
296                 connman_error("notifier disable underflow");
297                 return;
298         }
299
300         switch (type) {
301         case CONNMAN_SERVICE_TYPE_UNKNOWN:
302         case CONNMAN_SERVICE_TYPE_SYSTEM:
303         case CONNMAN_SERVICE_TYPE_GPS:
304         case CONNMAN_SERVICE_TYPE_VPN:
305         case CONNMAN_SERVICE_TYPE_GADGET:
306                 return;
307         case CONNMAN_SERVICE_TYPE_ETHERNET:
308         case CONNMAN_SERVICE_TYPE_WIFI:
309         case CONNMAN_SERVICE_TYPE_WIMAX:
310         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
311         case CONNMAN_SERVICE_TYPE_CELLULAR:
312                 break;
313         }
314
315         if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
316                 technology_enabled(type, FALSE);
317 }
318
319 void __connman_notifier_connect(enum connman_service_type type)
320 {
321         DBG("type %d", type);
322
323         switch (type) {
324         case CONNMAN_SERVICE_TYPE_UNKNOWN:
325         case CONNMAN_SERVICE_TYPE_SYSTEM:
326         case CONNMAN_SERVICE_TYPE_GPS:
327         case CONNMAN_SERVICE_TYPE_VPN:
328         case CONNMAN_SERVICE_TYPE_GADGET:
329                 return;
330         case CONNMAN_SERVICE_TYPE_ETHERNET:
331         case CONNMAN_SERVICE_TYPE_WIFI:
332         case CONNMAN_SERVICE_TYPE_WIMAX:
333         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
334         case CONNMAN_SERVICE_TYPE_CELLULAR:
335                 break;
336         }
337
338         if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
339                 technology_connected(type, TRUE);
340 }
341
342 void __connman_notifier_disconnect(enum connman_service_type type)
343 {
344         DBG("type %d", type);
345
346         if (g_atomic_int_get(&connected[type]) == 0) {
347                 connman_error("notifier disconnect underflow");
348                 return;
349         }
350
351         switch (type) {
352         case CONNMAN_SERVICE_TYPE_UNKNOWN:
353         case CONNMAN_SERVICE_TYPE_SYSTEM:
354         case CONNMAN_SERVICE_TYPE_GPS:
355         case CONNMAN_SERVICE_TYPE_VPN:
356         case CONNMAN_SERVICE_TYPE_GADGET:
357                 return;
358         case CONNMAN_SERVICE_TYPE_ETHERNET:
359         case CONNMAN_SERVICE_TYPE_WIFI:
360         case CONNMAN_SERVICE_TYPE_WIMAX:
361         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
362         case CONNMAN_SERVICE_TYPE_CELLULAR:
363                 break;
364         }
365
366         if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
367                 technology_connected(type, FALSE);
368 }
369
370 static void technology_default(enum connman_service_type type)
371 {
372         const char *str;
373
374         str = __connman_service_type2string(type);
375         if (str == NULL)
376                 str = "";
377
378         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
379                         CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
380                                                 DBUS_TYPE_STRING, &str);
381 }
382
383 void __connman_notifier_default_changed(struct connman_service *service)
384 {
385         enum connman_service_type type = connman_service_get_type(service);
386         char *interface;
387         GSList *list;
388
389         technology_default(type);
390
391         interface = connman_service_get_interface(service);
392         __connman_tethering_update_interface(interface);
393         g_free(interface);
394
395         for (list = notifier_list; list; list = list->next) {
396                 struct connman_notifier *notifier = list->data;
397
398                 if (notifier->default_changed)
399                         notifier->default_changed(service);
400         }
401 }
402
403 void __connman_notifier_service_add(struct connman_service *service)
404 {
405         GSList *list;
406
407         for (list = notifier_list; list; list = list->next) {
408                 struct connman_notifier *notifier = list->data;
409
410                 if (notifier->service_add)
411                         notifier->service_add(service);
412         }
413 }
414
415 void __connman_notifier_service_remove(struct connman_service *service)
416 {
417         GSList *list;
418
419         for (list = notifier_list; list; list = list->next) {
420                 struct connman_notifier *notifier = list->data;
421
422                 if (notifier->service_remove)
423                         notifier->service_remove(service);
424         }
425 }
426
427 void __connman_notifier_proxy_changed(struct connman_service *service)
428 {
429         GSList *list;
430
431         for (list = notifier_list; list; list = list->next) {
432                 struct connman_notifier *notifier = list->data;
433
434                 if (notifier->proxy_changed)
435                         notifier->proxy_changed(service);
436         }
437 }
438
439 static void offlinemode_changed(dbus_bool_t enabled)
440 {
441         DBG("enabled %d", enabled);
442
443         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
444                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
445                                                 DBUS_TYPE_BOOLEAN, &enabled);
446 }
447
448 void __connman_notifier_offlinemode(connman_bool_t enabled)
449 {
450         GSList *list;
451
452         DBG("enabled %d", enabled);
453
454         __connman_profile_changed(FALSE);
455
456         offlinemode_changed(enabled);
457
458         for (list = notifier_list; list; list = list->next) {
459                 struct connman_notifier *notifier = list->data;
460
461                 if (notifier->offline_mode)
462                         notifier->offline_mode(enabled);
463         }
464 }
465
466 void __connman_notifier_service_state_changed(struct connman_service *service,
467                                         enum connman_service_state state)
468 {
469         GSList *list;
470
471         for (list = notifier_list; list; list = list->next) {
472                 struct connman_notifier *notifier = list->data;
473
474                 if (notifier->service_state_changed)
475                         notifier->service_state_changed(service, state);
476         }
477 }
478
479 void __connman_notifier_ipconfig_changed(struct connman_service *service,
480                                         struct connman_ipconfig *ipconfig)
481 {
482         GSList *list;
483
484         for (list = notifier_list; list; list = list->next) {
485                 struct connman_notifier *notifier = list->data;
486
487                 if (notifier->ipconfig_changed)
488                         notifier->ipconfig_changed(service, ipconfig);
489         }
490 }
491
492 static connman_bool_t technology_supported(enum connman_service_type type)
493 {
494         switch (type) {
495         case CONNMAN_SERVICE_TYPE_UNKNOWN:
496         case CONNMAN_SERVICE_TYPE_SYSTEM:
497         case CONNMAN_SERVICE_TYPE_GPS:
498         case CONNMAN_SERVICE_TYPE_VPN:
499         case CONNMAN_SERVICE_TYPE_GADGET:
500                 return FALSE;
501         case CONNMAN_SERVICE_TYPE_ETHERNET:
502         case CONNMAN_SERVICE_TYPE_WIFI:
503         case CONNMAN_SERVICE_TYPE_WIMAX:
504         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
505         case CONNMAN_SERVICE_TYPE_CELLULAR:
506                 break;
507         }
508
509         return TRUE;
510 }
511
512 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
513 {
514         DBG("type %d", type);
515
516         if (technology_supported(type) == FALSE)
517                 return FALSE;
518
519         if (g_atomic_int_get(&registered[type]) > 0)
520                 return TRUE;
521
522         return FALSE;
523 }
524
525 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
526 {
527         DBG("type %d", type);
528
529         if (technology_supported(type) == FALSE)
530                 return FALSE;
531
532         if (g_atomic_int_get(&enabled[type]) > 0)
533                 return TRUE;
534
535         return FALSE;
536 }
537
538 int __connman_notifier_init(void)
539 {
540         DBG("");
541
542         connection = connman_dbus_get_connection();
543
544         return 0;
545 }
546
547 void __connman_notifier_cleanup(void)
548 {
549         DBG("");
550
551         dbus_connection_unref(connection);
552 }