Adding connman-test subpackage
[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 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 #if !defined TIZEN_EXT
411 /*
412  * Description: Tethering service is provided by external module in TIZEN
413  */
414         __connman_tethering_update_interface(interface);
415 #endif
416         g_free(interface);
417
418         for (list = notifier_list; list; list = list->next) {
419                 struct connman_notifier *notifier = list->data;
420
421                 if (notifier->default_changed)
422                         notifier->default_changed(service);
423         }
424 }
425
426 void __connman_notifier_service_add(struct connman_service *service,
427                                         const char *name)
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->service_add)
435                         notifier->service_add(service, name);
436         }
437 }
438
439 void __connman_notifier_service_remove(struct connman_service *service)
440 {
441         GSList *list;
442
443         if (g_hash_table_lookup(service_hash, service) != NULL) {
444                 /*
445                  * This is a tempory check for consistency. It can be
446                  * removed when there are no reports for the following
447                  * error message.
448                  */
449                 connman_error("Service state machine inconsistency detected.");
450
451                 g_hash_table_remove(service_hash, service);
452         }
453
454         for (list = notifier_list; list; list = list->next) {
455                 struct connman_notifier *notifier = list->data;
456
457                 if (notifier->service_remove)
458                         notifier->service_remove(service);
459         }
460 }
461
462 void __connman_notifier_proxy_changed(struct connman_service *service)
463 {
464         GSList *list;
465
466         for (list = notifier_list; list; list = list->next) {
467                 struct connman_notifier *notifier = list->data;
468
469                 if (notifier->proxy_changed)
470                         notifier->proxy_changed(service);
471         }
472 }
473
474 static void offlinemode_changed(dbus_bool_t enabled)
475 {
476         DBG("enabled %d", enabled);
477
478         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
479                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
480                                                 DBUS_TYPE_BOOLEAN, &enabled);
481 }
482
483 void __connman_notifier_offlinemode(connman_bool_t enabled)
484 {
485         GSList *list;
486
487         DBG("enabled %d", enabled);
488
489         offlinemode_changed(enabled);
490
491         for (list = notifier_list; list; list = list->next) {
492                 struct connman_notifier *notifier = list->data;
493
494                 if (notifier->offline_mode)
495                         notifier->offline_mode(enabled);
496         }
497 }
498
499 static void notify_idle_state(connman_bool_t idle)
500 {
501         GSList *list;
502
503         DBG("idle %d", idle);
504
505         for (list = notifier_list; list; list = list->next) {
506                 struct connman_notifier *notifier = list->data;
507
508                 if (notifier->idle_state)
509                         notifier->idle_state(idle);
510         }
511 }
512
513 #if defined TIZEN_EXT
514 /*
515  * August 22nd, 2011. TIZEN
516  *
517  * This part is added to send a DBus signal which means scan is completed
518  * because scan UX of a Wi-Fi setting application has an active scan procedure
519  * and it needs scan complete signal whether success or not
520  */
521
522 void __connman_notifier_scan_completed(connman_bool_t success)
523 {
524         DBG("scan completed : %s", success == TRUE ? "success" : "failed");
525
526         connman_dbus_scan_completed_basic(CONNMAN_MANAGER_PATH,
527                                 CONNMAN_MANAGER_INTERFACE, DBUS_TYPE_BOOLEAN, &success);
528 }
529 #endif
530
531 void __connman_notifier_service_state_changed(struct connman_service *service,
532                                         enum connman_service_state state)
533 {
534         GSList *list;
535         unsigned int old_size;
536         connman_bool_t found;
537
538         for (list = notifier_list; list; list = list->next) {
539                 struct connman_notifier *notifier = list->data;
540
541                 if (notifier->service_state_changed)
542                         notifier->service_state_changed(service, state);
543         }
544
545         old_size = g_hash_table_size(service_hash);
546         found = g_hash_table_lookup(service_hash, service) != NULL;
547
548         switch (state) {
549         case CONNMAN_SERVICE_STATE_UNKNOWN:
550         case CONNMAN_SERVICE_STATE_FAILURE:
551         case CONNMAN_SERVICE_STATE_DISCONNECT:
552         case CONNMAN_SERVICE_STATE_IDLE:
553                 if (found == FALSE)
554                         break;
555
556                 g_hash_table_remove(service_hash, service);
557                 if (old_size == 1)
558                         notify_idle_state(TRUE);
559
560                 break;
561         case CONNMAN_SERVICE_STATE_ASSOCIATION:
562         case CONNMAN_SERVICE_STATE_CONFIGURATION:
563         case CONNMAN_SERVICE_STATE_READY:
564         case CONNMAN_SERVICE_STATE_ONLINE:
565                 if (found == TRUE)
566                         break;
567
568                 g_hash_table_insert(service_hash, service, service);
569                 if (old_size == 0)
570                         notify_idle_state(FALSE);
571
572                 break;
573         }
574 }
575
576 void __connman_notifier_ipconfig_changed(struct connman_service *service,
577                                         struct connman_ipconfig *ipconfig)
578 {
579         GSList *list;
580
581         for (list = notifier_list; list; list = list->next) {
582                 struct connman_notifier *notifier = list->data;
583
584                 if (notifier->ipconfig_changed)
585                         notifier->ipconfig_changed(service, ipconfig);
586         }
587 }
588
589 static connman_bool_t technology_supported(enum connman_service_type type)
590 {
591         switch (type) {
592         case CONNMAN_SERVICE_TYPE_UNKNOWN:
593         case CONNMAN_SERVICE_TYPE_SYSTEM:
594         case CONNMAN_SERVICE_TYPE_GPS:
595         case CONNMAN_SERVICE_TYPE_VPN:
596         case CONNMAN_SERVICE_TYPE_GADGET:
597                 return FALSE;
598         case CONNMAN_SERVICE_TYPE_ETHERNET:
599         case CONNMAN_SERVICE_TYPE_WIFI:
600         case CONNMAN_SERVICE_TYPE_WIMAX:
601         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
602         case CONNMAN_SERVICE_TYPE_CELLULAR:
603                 break;
604         }
605
606         return TRUE;
607 }
608
609 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
610 {
611         DBG("type %d", type);
612
613         if (technology_supported(type) == FALSE)
614                 return FALSE;
615
616         __sync_synchronize();
617         if (registered[type] > 0)
618                 return TRUE;
619
620         return FALSE;
621 }
622
623 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
624 {
625         DBG("type %d", type);
626
627         if (technology_supported(type) == FALSE)
628                 return FALSE;
629
630         __sync_synchronize();
631         if (enabled[type] > 0)
632                 return TRUE;
633
634         return FALSE;
635 }
636
637 int __connman_notifier_init(void)
638 {
639         DBG("");
640
641         connection = connman_dbus_get_connection();
642
643         service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
644                                                 NULL, NULL);
645
646
647         return 0;
648 }
649
650 void __connman_notifier_cleanup(void)
651 {
652         DBG("");
653
654         g_hash_table_destroy(service_hash);
655         service_hash = NULL;
656
657         dbus_connection_unref(connection);
658 }