core: Use gcc atomics instead glib's ones
[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 static GHashTable *service_hash = NULL;
34
35 static gint compare_priority(gconstpointer a, gconstpointer b)
36 {
37         const struct connman_notifier *notifier1 = a;
38         const struct connman_notifier *notifier2 = b;
39
40         return notifier2->priority - notifier1->priority;
41 }
42
43 /**
44  * connman_notifier_register:
45  * @notifier: notifier module
46  *
47  * Register a new notifier module
48  *
49  * Returns: %0 on success
50  */
51 int connman_notifier_register(struct connman_notifier *notifier)
52 {
53         DBG("notifier %p name %s", notifier, notifier->name);
54
55         notifier_list = g_slist_insert_sorted(notifier_list, notifier,
56                                                         compare_priority);
57
58         return 0;
59 }
60
61 /**
62  * connman_notifier_unregister:
63  * @notifier: notifier module
64  *
65  * Remove a previously registered notifier module
66  */
67 void connman_notifier_unregister(struct connman_notifier *notifier)
68 {
69         DBG("notifier %p name %s", notifier, notifier->name);
70
71         notifier_list = g_slist_remove(notifier_list, notifier);
72 }
73
74 #define MAX_TECHNOLOGIES 10
75
76 static volatile int registered[MAX_TECHNOLOGIES];
77 static volatile int enabled[MAX_TECHNOLOGIES];
78 static volatile int connected[MAX_TECHNOLOGIES];
79
80 void __connman_notifier_list_registered(DBusMessageIter *iter, void *user_data)
81 {
82         int i;
83
84         __sync_synchronize();
85         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
86                 const char *type = __connman_service_type2string(i);
87
88                 if (type == NULL)
89                         continue;
90
91                 if (registered[i] > 0)
92                         dbus_message_iter_append_basic(iter,
93                                                 DBUS_TYPE_STRING, &type);
94         }
95 }
96
97 void __connman_notifier_list_enabled(DBusMessageIter *iter, void *user_data)
98 {
99         int i;
100
101         __sync_synchronize();
102         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
103                 const char *type = __connman_service_type2string(i);
104
105                 if (type == NULL)
106                         continue;
107
108                 if (enabled[i] > 0)
109                         dbus_message_iter_append_basic(iter,
110                                                 DBUS_TYPE_STRING, &type);
111         }
112 }
113
114 void __connman_notifier_list_connected(DBusMessageIter *iter, void *user_data)
115 {
116         int i;
117
118         __sync_synchronize();
119         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
120                 const char *type = __connman_service_type2string(i);
121
122                 if (type == NULL)
123                         continue;
124
125                 if (connected[i] > 0)
126                         dbus_message_iter_append_basic(iter,
127                                                 DBUS_TYPE_STRING, &type);
128         }
129 }
130
131 static void technology_registered(enum connman_service_type type,
132                                                 connman_bool_t registered)
133 {
134         DBG("type %d registered %d", type, registered);
135
136         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
137                 CONNMAN_MANAGER_INTERFACE, "AvailableTechnologies",
138                 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
139 }
140
141 static void technology_enabled(enum connman_service_type type,
142                                                 connman_bool_t enabled)
143 {
144         GSList *list;
145
146         DBG("type %d enabled %d", type, enabled);
147
148         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
149                 CONNMAN_MANAGER_INTERFACE, "EnabledTechnologies",
150                 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
151
152         for (list = notifier_list; list; list = list->next) {
153                 struct connman_notifier *notifier = list->data;
154
155                 if (notifier->service_enabled)
156                         notifier->service_enabled(type, enabled);
157         }
158 }
159
160 unsigned int __connman_notifier_count_connected(void)
161 {
162         unsigned int i, count = 0;
163
164         __sync_synchronize();
165         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
166                 if (connected[i] > 0)
167                         count++;
168         }
169
170         return count;
171 }
172
173 const char *__connman_notifier_get_state(void)
174 {
175         unsigned int count = __connman_notifier_count_connected();
176
177         if (count > 0)
178                 return "online";
179
180         return "offline";
181 }
182
183 static void state_changed(connman_bool_t connected)
184 {
185         unsigned int count = __connman_notifier_count_connected();
186         char *state = "offline";
187         DBusMessage *signal;
188
189         if (count > 1)
190                 return;
191
192         if (count == 1) {
193                 if (connected == FALSE)
194                         return;
195
196                 state = "online";
197         }
198
199         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
200                                 CONNMAN_MANAGER_INTERFACE, "State",
201                                                 DBUS_TYPE_STRING, &state);
202
203         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
204                                 CONNMAN_MANAGER_INTERFACE, "StateChanged");
205         if (signal == NULL)
206                 return;
207
208         dbus_message_append_args(signal, DBUS_TYPE_STRING, &state,
209                                                         DBUS_TYPE_INVALID);
210
211         g_dbus_send_message(connection, signal);
212 }
213
214 static void technology_connected(enum connman_service_type type,
215                                                 connman_bool_t connected)
216 {
217         DBG("type %d connected %d", type, connected);
218
219         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
220                 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
221                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
222
223         state_changed(connected);
224 }
225
226 void __connman_notifier_register(enum connman_service_type type)
227 {
228         DBG("type %d", type);
229
230         switch (type) {
231         case CONNMAN_SERVICE_TYPE_UNKNOWN:
232         case CONNMAN_SERVICE_TYPE_SYSTEM:
233         case CONNMAN_SERVICE_TYPE_GPS:
234         case CONNMAN_SERVICE_TYPE_VPN:
235         case CONNMAN_SERVICE_TYPE_GADGET:
236                 return;
237         case CONNMAN_SERVICE_TYPE_ETHERNET:
238         case CONNMAN_SERVICE_TYPE_WIFI:
239         case CONNMAN_SERVICE_TYPE_WIMAX:
240         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
241         case CONNMAN_SERVICE_TYPE_CELLULAR:
242                 break;
243         }
244
245         if (__sync_fetch_and_add(&registered[type], 1) == 0)
246                 technology_registered(type, TRUE);
247 }
248
249 void __connman_notifier_unregister(enum connman_service_type type)
250 {
251         DBG("type %d", type);
252
253         __sync_synchronize();
254         if (registered[type] == 0) {
255                 connman_error("notifier unregister underflow");
256                 return;
257         }
258
259         switch (type) {
260         case CONNMAN_SERVICE_TYPE_UNKNOWN:
261         case CONNMAN_SERVICE_TYPE_SYSTEM:
262         case CONNMAN_SERVICE_TYPE_GPS:
263         case CONNMAN_SERVICE_TYPE_VPN:
264         case CONNMAN_SERVICE_TYPE_GADGET:
265                 return;
266         case CONNMAN_SERVICE_TYPE_ETHERNET:
267         case CONNMAN_SERVICE_TYPE_WIFI:
268         case CONNMAN_SERVICE_TYPE_WIMAX:
269         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
270         case CONNMAN_SERVICE_TYPE_CELLULAR:
271                 break;
272         }
273
274         if (__sync_fetch_and_sub(&registered[type], 1) != 1)
275                 return;
276
277         technology_registered(type, FALSE);
278 }
279
280 void __connman_notifier_enable(enum connman_service_type type)
281 {
282         DBG("type %d", type);
283
284         switch (type) {
285         case CONNMAN_SERVICE_TYPE_UNKNOWN:
286         case CONNMAN_SERVICE_TYPE_SYSTEM:
287         case CONNMAN_SERVICE_TYPE_GPS:
288         case CONNMAN_SERVICE_TYPE_VPN:
289         case CONNMAN_SERVICE_TYPE_GADGET:
290                 return;
291         case CONNMAN_SERVICE_TYPE_ETHERNET:
292         case CONNMAN_SERVICE_TYPE_WIFI:
293         case CONNMAN_SERVICE_TYPE_WIMAX:
294         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
295         case CONNMAN_SERVICE_TYPE_CELLULAR:
296                 break;
297         }
298
299         if (__sync_fetch_and_add(&enabled[type], 1) == 0)
300                 technology_enabled(type, TRUE);
301 }
302
303 void __connman_notifier_disable(enum connman_service_type type)
304 {
305         DBG("type %d", type);
306
307         __sync_synchronize();
308         if (enabled[type] == 0) {
309                 connman_error("notifier disable underflow");
310                 return;
311         }
312
313         switch (type) {
314         case CONNMAN_SERVICE_TYPE_UNKNOWN:
315         case CONNMAN_SERVICE_TYPE_SYSTEM:
316         case CONNMAN_SERVICE_TYPE_GPS:
317         case CONNMAN_SERVICE_TYPE_VPN:
318         case CONNMAN_SERVICE_TYPE_GADGET:
319                 return;
320         case CONNMAN_SERVICE_TYPE_ETHERNET:
321         case CONNMAN_SERVICE_TYPE_WIFI:
322         case CONNMAN_SERVICE_TYPE_WIMAX:
323         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
324         case CONNMAN_SERVICE_TYPE_CELLULAR:
325                 break;
326         }
327
328         if (__sync_fetch_and_sub(&enabled[type], 1) != 1)
329                 return;
330
331         technology_enabled(type, FALSE);
332 }
333
334 void __connman_notifier_connect(enum connman_service_type type)
335 {
336         DBG("type %d", type);
337
338         switch (type) {
339         case CONNMAN_SERVICE_TYPE_UNKNOWN:
340         case CONNMAN_SERVICE_TYPE_SYSTEM:
341         case CONNMAN_SERVICE_TYPE_GPS:
342         case CONNMAN_SERVICE_TYPE_VPN:
343         case CONNMAN_SERVICE_TYPE_GADGET:
344                 return;
345         case CONNMAN_SERVICE_TYPE_ETHERNET:
346         case CONNMAN_SERVICE_TYPE_WIFI:
347         case CONNMAN_SERVICE_TYPE_WIMAX:
348         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
349         case CONNMAN_SERVICE_TYPE_CELLULAR:
350                 break;
351         }
352
353         if (__sync_fetch_and_add(&connected[type], 1) == 0)
354                 technology_connected(type, TRUE);
355 }
356
357 void __connman_notifier_disconnect(enum connman_service_type type)
358 {
359         DBG("type %d", type);
360
361         __sync_synchronize();
362         if (connected[type] == 0) {
363                 connman_error("notifier disconnect underflow");
364                 return;
365         }
366
367         switch (type) {
368         case CONNMAN_SERVICE_TYPE_UNKNOWN:
369         case CONNMAN_SERVICE_TYPE_SYSTEM:
370         case CONNMAN_SERVICE_TYPE_GPS:
371         case CONNMAN_SERVICE_TYPE_VPN:
372         case CONNMAN_SERVICE_TYPE_GADGET:
373                 return;
374         case CONNMAN_SERVICE_TYPE_ETHERNET:
375         case CONNMAN_SERVICE_TYPE_WIFI:
376         case CONNMAN_SERVICE_TYPE_WIMAX:
377         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
378         case CONNMAN_SERVICE_TYPE_CELLULAR:
379                 break;
380         }
381
382         if (__sync_fetch_and_sub(&connected[type], 1) != 1)
383                 return;
384
385         technology_connected(type, FALSE);
386 }
387
388 static void technology_default(enum connman_service_type type)
389 {
390         const char *str;
391
392         str = __connman_service_type2string(type);
393         if (str == NULL)
394                 str = "";
395
396         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
397                         CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
398                                                 DBUS_TYPE_STRING, &str);
399 }
400
401 void __connman_notifier_default_changed(struct connman_service *service)
402 {
403         enum connman_service_type type = connman_service_get_type(service);
404         char *interface;
405         GSList *list;
406
407         technology_default(type);
408
409         interface = connman_service_get_interface(service);
410         __connman_tethering_update_interface(interface);
411         g_free(interface);
412
413         for (list = notifier_list; list; list = list->next) {
414                 struct connman_notifier *notifier = list->data;
415
416                 if (notifier->default_changed)
417                         notifier->default_changed(service);
418         }
419 }
420
421 void __connman_notifier_service_add(struct connman_service *service,
422                                         const char *name)
423 {
424         GSList *list;
425
426         for (list = notifier_list; list; list = list->next) {
427                 struct connman_notifier *notifier = list->data;
428
429                 if (notifier->service_add)
430                         notifier->service_add(service, name);
431         }
432 }
433
434 void __connman_notifier_service_remove(struct connman_service *service)
435 {
436         GSList *list;
437
438         if (g_hash_table_lookup(service_hash, service) != NULL) {
439                 /*
440                  * This is a tempory check for consistency. It can be
441                  * removed when there are no reports for the following
442                  * error message.
443                  */
444                 connman_error("Service state machine inconsistency detected.");
445
446                 g_hash_table_remove(service_hash, service);
447         }
448
449         for (list = notifier_list; list; list = list->next) {
450                 struct connman_notifier *notifier = list->data;
451
452                 if (notifier->service_remove)
453                         notifier->service_remove(service);
454         }
455 }
456
457 void __connman_notifier_proxy_changed(struct connman_service *service)
458 {
459         GSList *list;
460
461         for (list = notifier_list; list; list = list->next) {
462                 struct connman_notifier *notifier = list->data;
463
464                 if (notifier->proxy_changed)
465                         notifier->proxy_changed(service);
466         }
467 }
468
469 static void offlinemode_changed(dbus_bool_t enabled)
470 {
471         DBG("enabled %d", enabled);
472
473         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
474                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
475                                                 DBUS_TYPE_BOOLEAN, &enabled);
476 }
477
478 void __connman_notifier_offlinemode(connman_bool_t enabled)
479 {
480         GSList *list;
481
482         DBG("enabled %d", enabled);
483
484         offlinemode_changed(enabled);
485
486         for (list = notifier_list; list; list = list->next) {
487                 struct connman_notifier *notifier = list->data;
488
489                 if (notifier->offline_mode)
490                         notifier->offline_mode(enabled);
491         }
492 }
493
494 static void notify_idle_state(connman_bool_t idle)
495 {
496         GSList *list;
497
498         DBG("idle %d", idle);
499
500         for (list = notifier_list; list; list = list->next) {
501                 struct connman_notifier *notifier = list->data;
502
503                 if (notifier->idle_state)
504                         notifier->idle_state(idle);
505         }
506 }
507
508 void __connman_notifier_service_state_changed(struct connman_service *service,
509                                         enum connman_service_state state)
510 {
511         GSList *list;
512         unsigned int old_size;
513         connman_bool_t found;
514
515         for (list = notifier_list; list; list = list->next) {
516                 struct connman_notifier *notifier = list->data;
517
518                 if (notifier->service_state_changed)
519                         notifier->service_state_changed(service, state);
520         }
521
522         old_size = g_hash_table_size(service_hash);
523         found = g_hash_table_lookup(service_hash, service) != NULL;
524
525         switch (state) {
526         case CONNMAN_SERVICE_STATE_UNKNOWN:
527         case CONNMAN_SERVICE_STATE_DISCONNECT:
528         case CONNMAN_SERVICE_STATE_IDLE:
529                 if (found == FALSE)
530                         break;
531
532                 g_hash_table_remove(service_hash, service);
533                 if (old_size == 1)
534                         notify_idle_state(TRUE);
535
536                 break;
537         case CONNMAN_SERVICE_STATE_ASSOCIATION:
538         case CONNMAN_SERVICE_STATE_CONFIGURATION:
539         case CONNMAN_SERVICE_STATE_READY:
540         case CONNMAN_SERVICE_STATE_ONLINE:
541         case CONNMAN_SERVICE_STATE_FAILURE:
542                 if (found == TRUE)
543                         break;
544
545                 g_hash_table_insert(service_hash, service, service);
546                 if (old_size == 0)
547                         notify_idle_state(FALSE);
548
549                 break;
550         }
551 }
552
553 void __connman_notifier_ipconfig_changed(struct connman_service *service,
554                                         struct connman_ipconfig *ipconfig)
555 {
556         GSList *list;
557
558         for (list = notifier_list; list; list = list->next) {
559                 struct connman_notifier *notifier = list->data;
560
561                 if (notifier->ipconfig_changed)
562                         notifier->ipconfig_changed(service, ipconfig);
563         }
564 }
565
566 static connman_bool_t technology_supported(enum connman_service_type type)
567 {
568         switch (type) {
569         case CONNMAN_SERVICE_TYPE_UNKNOWN:
570         case CONNMAN_SERVICE_TYPE_SYSTEM:
571         case CONNMAN_SERVICE_TYPE_GPS:
572         case CONNMAN_SERVICE_TYPE_VPN:
573         case CONNMAN_SERVICE_TYPE_GADGET:
574                 return FALSE;
575         case CONNMAN_SERVICE_TYPE_ETHERNET:
576         case CONNMAN_SERVICE_TYPE_WIFI:
577         case CONNMAN_SERVICE_TYPE_WIMAX:
578         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
579         case CONNMAN_SERVICE_TYPE_CELLULAR:
580                 break;
581         }
582
583         return TRUE;
584 }
585
586 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
587 {
588         DBG("type %d", type);
589
590         if (technology_supported(type) == FALSE)
591                 return FALSE;
592
593         __sync_synchronize();
594         if (registered[type] > 0)
595                 return TRUE;
596
597         return FALSE;
598 }
599
600 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
601 {
602         DBG("type %d", type);
603
604         if (technology_supported(type) == FALSE)
605                 return FALSE;
606
607         __sync_synchronize();
608         if (enabled[type] > 0)
609                 return TRUE;
610
611         return FALSE;
612 }
613
614 int __connman_notifier_init(void)
615 {
616         DBG("");
617
618         connection = connman_dbus_get_connection();
619
620         service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
621                                                 NULL, NULL);
622
623
624         return 0;
625 }
626
627 void __connman_notifier_cleanup(void)
628 {
629         DBG("");
630
631         g_hash_table_destroy(service_hash);
632         service_hash = NULL;
633
634         dbus_connection_unref(connection);
635 }