5ba5324288079e4731cb4713de546c907de76d44
[platform/upstream/connman.git] / src / notifier.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 const char *notifier_state;
36
37 static gint compare_priority(gconstpointer a, gconstpointer b)
38 {
39         const struct connman_notifier *notifier1 = a;
40         const struct connman_notifier *notifier2 = b;
41
42         return notifier2->priority - notifier1->priority;
43 }
44
45 /**
46  * connman_notifier_register:
47  * @notifier: notifier module
48  *
49  * Register a new notifier module
50  *
51  * Returns: %0 on success
52  */
53 int connman_notifier_register(struct connman_notifier *notifier)
54 {
55         DBG("notifier %p name %s", notifier, notifier->name);
56
57         notifier_list = g_slist_insert_sorted(notifier_list, notifier,
58                                                         compare_priority);
59
60         return 0;
61 }
62
63 /**
64  * connman_notifier_unregister:
65  * @notifier: notifier module
66  *
67  * Remove a previously registered notifier module
68  */
69 void connman_notifier_unregister(struct connman_notifier *notifier)
70 {
71         DBG("notifier %p name %s", notifier, notifier->name);
72
73         notifier_list = g_slist_remove(notifier_list, notifier);
74 }
75
76 static int connected[MAX_CONNMAN_SERVICE_TYPES];
77 static int online[MAX_CONNMAN_SERVICE_TYPES];
78
79 static bool notifier_is_online(void)
80 {
81         unsigned int i;
82
83         __sync_synchronize();
84         for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++) {
85                 if (online[i] > 0)
86                         return true;
87         }
88
89         return false;
90 }
91
92 bool __connman_notifier_is_connected(void)
93 {
94         unsigned int i;
95
96         __sync_synchronize();
97         for (i = 0; i < MAX_CONNMAN_SERVICE_TYPES; i++) {
98                 if (connected[i] > 0)
99                         return true;
100         }
101
102         return false;
103 }
104
105 static const char *evaluate_notifier_state(void)
106 {
107         if (notifier_is_online())
108                 return "online";
109
110         if (__connman_notifier_is_connected())
111                 return "ready";
112
113         if (__connman_technology_get_offlinemode())
114                 return "offline";
115
116         return "idle";
117 }
118
119 const char *__connman_notifier_get_state(void)
120 {
121         return notifier_state;
122 }
123
124 static void state_changed(void)
125 {
126         const char *state;
127
128         state = evaluate_notifier_state();
129
130         if (g_strcmp0(state, notifier_state) == 0)
131                 return;
132
133         notifier_state = state;
134
135         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
136                                 CONNMAN_MANAGER_INTERFACE, "State",
137                                         DBUS_TYPE_STRING, &notifier_state);
138 }
139
140 void __connman_notifier_connect(enum connman_service_type type)
141 {
142         DBG("type %d", type);
143
144         switch (type) {
145         case CONNMAN_SERVICE_TYPE_UNKNOWN:
146         case CONNMAN_SERVICE_TYPE_SYSTEM:
147         case CONNMAN_SERVICE_TYPE_GPS:
148         case CONNMAN_SERVICE_TYPE_VPN:
149                 return;
150         case CONNMAN_SERVICE_TYPE_ETHERNET:
151         case CONNMAN_SERVICE_TYPE_GADGET:
152         case CONNMAN_SERVICE_TYPE_WIFI:
153         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
154         case CONNMAN_SERVICE_TYPE_CELLULAR:
155         case CONNMAN_SERVICE_TYPE_P2P:
156                 break;
157         }
158
159         if (__sync_fetch_and_add(&connected[type], 1) == 0) {
160                 __connman_technology_set_connected(type, true);
161                 state_changed();
162         }
163 }
164
165 void __connman_notifier_enter_online(enum connman_service_type type)
166 {
167         DBG("type %d", type);
168
169         if (__sync_fetch_and_add(&online[type], 1) == 0)
170                 state_changed();
171 }
172
173 void __connman_notifier_leave_online(enum connman_service_type type)
174 {
175         DBG("type %d", type);
176
177         if (__sync_fetch_and_sub(&online[type], 1) == 1)
178                 state_changed();
179 }
180
181 void __connman_notifier_disconnect(enum connman_service_type type)
182 {
183         DBG("type %d", type);
184
185         __sync_synchronize();
186         if (connected[type] == 0) {
187                 connman_error("notifier disconnect underflow");
188                 return;
189         }
190
191         switch (type) {
192         case CONNMAN_SERVICE_TYPE_UNKNOWN:
193         case CONNMAN_SERVICE_TYPE_SYSTEM:
194         case CONNMAN_SERVICE_TYPE_GPS:
195         case CONNMAN_SERVICE_TYPE_VPN:
196                 return;
197         case CONNMAN_SERVICE_TYPE_ETHERNET:
198         case CONNMAN_SERVICE_TYPE_GADGET:
199         case CONNMAN_SERVICE_TYPE_WIFI:
200         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
201         case CONNMAN_SERVICE_TYPE_CELLULAR:
202         case CONNMAN_SERVICE_TYPE_P2P:
203                 break;
204         }
205
206         if (__sync_fetch_and_sub(&connected[type], 1) != 1)
207                 return;
208
209         __connman_technology_set_connected(type, false);
210         state_changed();
211 }
212
213 void __connman_notifier_default_changed(struct connman_service *service)
214 {
215         GSList *list;
216
217         for (list = notifier_list; list; list = list->next) {
218                 struct connman_notifier *notifier = list->data;
219
220                 if (notifier->default_changed)
221                         notifier->default_changed(service);
222         }
223 }
224
225 void __connman_notifier_service_add(struct connman_service *service,
226                                         const char *name)
227 {
228         GSList *list;
229
230         for (list = notifier_list; list; list = list->next) {
231                 struct connman_notifier *notifier = list->data;
232
233                 if (notifier->service_add)
234                         notifier->service_add(service, name);
235         }
236 }
237
238 void __connman_notifier_service_remove(struct connman_service *service)
239 {
240         GSList *list;
241
242         if (g_hash_table_lookup(service_hash, service)) {
243                 /*
244                  * This is a tempory check for consistency. It can be
245                  * removed when there are no reports for the following
246                  * error message.
247                  */
248                 connman_error("Service state machine inconsistency detected.");
249
250                 g_hash_table_remove(service_hash, service);
251         }
252
253         for (list = notifier_list; list; list = list->next) {
254                 struct connman_notifier *notifier = list->data;
255
256                 if (notifier->service_remove)
257                         notifier->service_remove(service);
258         }
259 }
260
261 void __connman_notifier_proxy_changed(struct connman_service *service)
262 {
263         GSList *list;
264
265         for (list = notifier_list; list; list = list->next) {
266                 struct connman_notifier *notifier = list->data;
267
268                 if (notifier->proxy_changed)
269                         notifier->proxy_changed(service);
270         }
271 }
272
273 static void offlinemode_changed(dbus_bool_t enabled)
274 {
275         DBG("enabled %d", enabled);
276
277         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
278                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
279                                                 DBUS_TYPE_BOOLEAN, &enabled);
280 }
281
282 void __connman_notifier_offlinemode(bool enabled)
283 {
284         GSList *list;
285
286         DBG("enabled %d", enabled);
287
288         offlinemode_changed(enabled);
289         state_changed();
290
291         for (list = notifier_list; list; list = list->next) {
292                 struct connman_notifier *notifier = list->data;
293
294                 if (notifier->offline_mode)
295                         notifier->offline_mode(enabled);
296         }
297 }
298
299 static void notify_idle_state(bool idle)
300 {
301         GSList *list;
302
303         DBG("idle %d", idle);
304
305         for (list = notifier_list; list; list = list->next) {
306                 struct connman_notifier *notifier = list->data;
307
308                 if (notifier->idle_state)
309                         notifier->idle_state(idle);
310         }
311 }
312
313 void __connman_notifier_service_state_changed(struct connman_service *service,
314                                         enum connman_service_state state)
315 {
316         GSList *list;
317         unsigned int old_size;
318         bool found;
319
320         for (list = notifier_list; list; list = list->next) {
321                 struct connman_notifier *notifier = list->data;
322
323                 if (notifier->service_state_changed)
324                         notifier->service_state_changed(service, state);
325         }
326
327         old_size = g_hash_table_size(service_hash);
328         found = g_hash_table_lookup(service_hash, service);
329
330         switch (state) {
331         case CONNMAN_SERVICE_STATE_UNKNOWN:
332         case CONNMAN_SERVICE_STATE_FAILURE:
333         case CONNMAN_SERVICE_STATE_DISCONNECT:
334         case CONNMAN_SERVICE_STATE_IDLE:
335                 if (!found)
336                         break;
337
338                 g_hash_table_remove(service_hash, service);
339                 if (old_size == 1)
340                         notify_idle_state(true);
341
342                 break;
343         case CONNMAN_SERVICE_STATE_ASSOCIATION:
344         case CONNMAN_SERVICE_STATE_CONFIGURATION:
345         case CONNMAN_SERVICE_STATE_READY:
346         case CONNMAN_SERVICE_STATE_ONLINE:
347                 if (found)
348                         break;
349
350                 g_hash_table_insert(service_hash, service, service);
351                 if (old_size == 0)
352                         notify_idle_state(false);
353
354                 break;
355         }
356 }
357
358 void __connman_notifier_ipconfig_changed(struct connman_service *service,
359                                         struct connman_ipconfig *ipconfig)
360 {
361         GSList *list;
362
363         for (list = notifier_list; list; list = list->next) {
364                 struct connman_notifier *notifier = list->data;
365
366                 if (notifier->ipconfig_changed)
367                         notifier->ipconfig_changed(service, ipconfig);
368         }
369 }
370
371 int __connman_notifier_init(void)
372 {
373         DBG("");
374
375         connection = connman_dbus_get_connection();
376
377         service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
378                                                 NULL, NULL);
379
380         notifier_state = evaluate_notifier_state();
381
382         return 0;
383 }
384
385 void __connman_notifier_cleanup(void)
386 {
387         DBG("");
388
389         g_hash_table_destroy(service_hash);
390         service_hash = NULL;
391
392         dbus_connection_unref(connection);
393 }