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