Disconnect service on removal if still connected
[connman] / src / notifier.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 "connman.h"
27
28 static GSList *notifier_list = NULL;
29
30 static gint compare_priority(gconstpointer a, gconstpointer b)
31 {
32         const struct connman_notifier *notifier1 = a;
33         const struct connman_notifier *notifier2 = b;
34
35         return notifier2->priority - notifier1->priority;
36 }
37
38 /**
39  * connman_notifier_register:
40  * @notifier: notifier module
41  *
42  * Register a new notifier module
43  *
44  * Returns: %0 on success
45  */
46 int connman_notifier_register(struct connman_notifier *notifier)
47 {
48         DBG("notifier %p name %s", notifier, notifier->name);
49
50         notifier_list = g_slist_insert_sorted(notifier_list, notifier,
51                                                         compare_priority);
52
53         return 0;
54 }
55
56 /**
57  * connman_notifier_unregister:
58  * @notifier: notifier module
59  *
60  * Remove a previously registered notifier module
61  */
62 void connman_notifier_unregister(struct connman_notifier *notifier)
63 {
64         DBG("notifier %p name %s", notifier, notifier->name);
65
66         notifier_list = g_slist_remove(notifier_list, notifier);
67 }
68
69 static void device_enabled(enum connman_device_type type,
70                                                 connman_bool_t enabled)
71 {
72         GSList *list;
73
74         for (list = notifier_list; list; list = list->next) {
75                 struct connman_notifier *notifier = list->data;
76
77                 if (notifier->device_enabled)
78                         notifier->device_enabled(type, enabled);
79         }
80
81 }
82
83 static volatile gint enabled[10];
84
85 void __connman_notifier_device_type_increase(enum connman_device_type type)
86 {
87         DBG("type %d", type);
88
89         switch (type) {
90         case CONNMAN_DEVICE_TYPE_UNKNOWN:
91         case CONNMAN_DEVICE_TYPE_HSO:
92         case CONNMAN_DEVICE_TYPE_NOZOMI:
93         case CONNMAN_DEVICE_TYPE_HUAWEI:
94         case CONNMAN_DEVICE_TYPE_NOVATEL:
95         case CONNMAN_DEVICE_TYPE_VENDOR:
96                 return;
97         case CONNMAN_DEVICE_TYPE_ETHERNET:
98         case CONNMAN_DEVICE_TYPE_WIFI:
99         case CONNMAN_DEVICE_TYPE_WIMAX:
100         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
101         case CONNMAN_DEVICE_TYPE_GPS:
102                 if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
103                         device_enabled(type, TRUE);
104                 break;
105         }
106 }
107
108 void __connman_notifier_device_type_decrease(enum connman_device_type type)
109 {
110         DBG("type %d", type);
111
112         switch (type) {
113         case CONNMAN_DEVICE_TYPE_UNKNOWN:
114         case CONNMAN_DEVICE_TYPE_HSO:
115         case CONNMAN_DEVICE_TYPE_NOZOMI:
116         case CONNMAN_DEVICE_TYPE_HUAWEI:
117         case CONNMAN_DEVICE_TYPE_NOVATEL:
118         case CONNMAN_DEVICE_TYPE_VENDOR:
119                 return;
120         case CONNMAN_DEVICE_TYPE_ETHERNET:
121         case CONNMAN_DEVICE_TYPE_WIFI:
122         case CONNMAN_DEVICE_TYPE_WIMAX:
123         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
124         case CONNMAN_DEVICE_TYPE_GPS:
125                 if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
126                         device_enabled(type, FALSE);
127                 break;
128         }
129 }
130
131 void __connman_notifier_offline_mode(connman_bool_t enabled)
132 {
133         GSList *list;
134
135         DBG("enabled %d", enabled);
136
137         for (list = notifier_list; list; list = list->next) {
138                 struct connman_notifier *notifier = list->data;
139
140                 if (notifier->offline_mode)
141                         notifier->offline_mode(enabled);
142         }
143 }
144
145 int __connman_notifier_init(void)
146 {
147         DBG("");
148
149         return 0;
150 }
151
152 void __connman_notifier_cleanup(void)
153 {
154         DBG("");
155 }