Add place-holder for technology type GPS
[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
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 < 10; 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 < 10; 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 < 10; 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 static void technology_connected(enum connman_service_type type,
157                                                 connman_bool_t connected)
158 {
159         DBG("type %d connected %d", type, connected);
160
161         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
162                 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
163                 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
164 }
165
166 void __connman_notifier_register(enum connman_service_type type)
167 {
168         DBG("type %d", type);
169
170         switch (type) {
171         case CONNMAN_SERVICE_TYPE_UNKNOWN:
172         case CONNMAN_SERVICE_TYPE_SYSTEM:
173         case CONNMAN_SERVICE_TYPE_GPS:
174         case CONNMAN_SERVICE_TYPE_VPN:
175                 return;
176         case CONNMAN_SERVICE_TYPE_ETHERNET:
177         case CONNMAN_SERVICE_TYPE_WIFI:
178         case CONNMAN_SERVICE_TYPE_WIMAX:
179         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
180         case CONNMAN_SERVICE_TYPE_CELLULAR:
181                 break;
182         }
183
184         if (g_atomic_int_exchange_and_add(&registered[type], 1) == 0)
185                 technology_registered(type, TRUE);
186 }
187
188 void __connman_notifier_unregister(enum connman_service_type type)
189 {
190         DBG("type %d", type);
191
192         switch (type) {
193         case CONNMAN_SERVICE_TYPE_UNKNOWN:
194         case CONNMAN_SERVICE_TYPE_SYSTEM:
195         case CONNMAN_SERVICE_TYPE_GPS:
196         case CONNMAN_SERVICE_TYPE_VPN:
197                 return;
198         case CONNMAN_SERVICE_TYPE_ETHERNET:
199         case CONNMAN_SERVICE_TYPE_WIFI:
200         case CONNMAN_SERVICE_TYPE_WIMAX:
201         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
202         case CONNMAN_SERVICE_TYPE_CELLULAR:
203                 break;
204         }
205
206         if (g_atomic_int_dec_and_test(&registered[type]) == TRUE)
207                 technology_registered(type, FALSE);
208 }
209
210 void __connman_notifier_enable(enum connman_service_type type)
211 {
212         DBG("type %d", type);
213
214         switch (type) {
215         case CONNMAN_SERVICE_TYPE_UNKNOWN:
216         case CONNMAN_SERVICE_TYPE_SYSTEM:
217         case CONNMAN_SERVICE_TYPE_GPS:
218         case CONNMAN_SERVICE_TYPE_VPN:
219                 return;
220         case CONNMAN_SERVICE_TYPE_ETHERNET:
221         case CONNMAN_SERVICE_TYPE_WIFI:
222         case CONNMAN_SERVICE_TYPE_WIMAX:
223         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
224         case CONNMAN_SERVICE_TYPE_CELLULAR:
225                 break;
226         }
227
228         if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
229                 technology_enabled(type, TRUE);
230 }
231
232 void __connman_notifier_disable(enum connman_service_type type)
233 {
234         DBG("type %d", type);
235
236         switch (type) {
237         case CONNMAN_SERVICE_TYPE_UNKNOWN:
238         case CONNMAN_SERVICE_TYPE_SYSTEM:
239         case CONNMAN_SERVICE_TYPE_GPS:
240         case CONNMAN_SERVICE_TYPE_VPN:
241                 return;
242         case CONNMAN_SERVICE_TYPE_ETHERNET:
243         case CONNMAN_SERVICE_TYPE_WIFI:
244         case CONNMAN_SERVICE_TYPE_WIMAX:
245         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
246         case CONNMAN_SERVICE_TYPE_CELLULAR:
247                 break;
248         }
249
250         if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
251                 technology_enabled(type, FALSE);
252 }
253
254 void __connman_notifier_connect(enum connman_service_type type)
255 {
256         DBG("type %d", type);
257
258         switch (type) {
259         case CONNMAN_SERVICE_TYPE_UNKNOWN:
260         case CONNMAN_SERVICE_TYPE_SYSTEM:
261         case CONNMAN_SERVICE_TYPE_GPS:
262         case CONNMAN_SERVICE_TYPE_VPN:
263                 return;
264         case CONNMAN_SERVICE_TYPE_ETHERNET:
265         case CONNMAN_SERVICE_TYPE_WIFI:
266         case CONNMAN_SERVICE_TYPE_WIMAX:
267         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
268         case CONNMAN_SERVICE_TYPE_CELLULAR:
269                 break;
270         }
271
272         if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
273                 technology_connected(type, TRUE);
274 }
275
276 void __connman_notifier_disconnect(enum connman_service_type type)
277 {
278         DBG("type %d", type);
279
280         switch (type) {
281         case CONNMAN_SERVICE_TYPE_UNKNOWN:
282         case CONNMAN_SERVICE_TYPE_SYSTEM:
283         case CONNMAN_SERVICE_TYPE_GPS:
284         case CONNMAN_SERVICE_TYPE_VPN:
285                 return;
286         case CONNMAN_SERVICE_TYPE_ETHERNET:
287         case CONNMAN_SERVICE_TYPE_WIFI:
288         case CONNMAN_SERVICE_TYPE_WIMAX:
289         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
290         case CONNMAN_SERVICE_TYPE_CELLULAR:
291                 break;
292         }
293
294         if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
295                 technology_connected(type, FALSE);
296 }
297
298 static void technology_default(enum connman_service_type type)
299 {
300         const char *str;
301
302         str = __connman_service_type2string(type);
303         if (str == NULL)
304                 str = "";
305
306         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
307                         CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
308                                                 DBUS_TYPE_STRING, &str);
309 }
310
311 void __connman_notifier_default_changed(struct connman_service *service)
312 {
313         enum connman_service_type type = connman_service_get_type(service);
314         GSList *list;
315
316         technology_default(type);
317
318         for (list = notifier_list; list; list = list->next) {
319                 struct connman_notifier *notifier = list->data;
320
321                 if (notifier->default_changed)
322                         notifier->default_changed(service);
323         }
324 }
325
326 static void offlinemode_changed(dbus_bool_t enabled)
327 {
328         DBG("enabled %d", enabled);
329
330         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
331                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
332                                                 DBUS_TYPE_BOOLEAN, &enabled);
333 }
334
335 void __connman_notifier_offlinemode(connman_bool_t enabled)
336 {
337         GSList *list;
338
339         DBG("enabled %d", enabled);
340
341         __connman_profile_changed(FALSE);
342
343         offlinemode_changed(enabled);
344
345         for (list = notifier_list; list; list = list->next) {
346                 struct connman_notifier *notifier = list->data;
347
348                 if (notifier->offline_mode)
349                         notifier->offline_mode(enabled);
350         }
351 }
352
353 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
354 {
355         DBG("type %d", type);
356
357         switch (type) {
358         case CONNMAN_SERVICE_TYPE_UNKNOWN:
359         case CONNMAN_SERVICE_TYPE_SYSTEM:
360         case CONNMAN_SERVICE_TYPE_GPS:
361         case CONNMAN_SERVICE_TYPE_VPN:
362                 return FALSE;
363         case CONNMAN_SERVICE_TYPE_ETHERNET:
364         case CONNMAN_SERVICE_TYPE_WIFI:
365         case CONNMAN_SERVICE_TYPE_WIMAX:
366         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
367         case CONNMAN_SERVICE_TYPE_CELLULAR:
368                 break;
369         }
370
371         if (g_atomic_int_get(&enabled[type]) > 0)
372                 return TRUE;
373
374         return FALSE;
375 }
376
377 int __connman_notifier_init(void)
378 {
379         DBG("");
380
381         connection = connman_dbus_get_connection();
382
383         return 0;
384 }
385
386 void __connman_notifier_cleanup(void)
387 {
388         DBG("");
389
390         dbus_connection_unref(connection);
391 }