notifier: Change __connman_notifier_count_connected() to *_is_connected()
[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 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 #define MAX_TECHNOLOGIES 10
77
78 static int connected[MAX_TECHNOLOGIES];
79 static int online[MAX_TECHNOLOGIES];
80
81 static unsigned int notifier_count_online(void)
82 {
83         unsigned int i, count = 0;
84
85         __sync_synchronize();
86         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
87                 if (online[i] > 0)
88                         count++;
89         }
90
91         return count;
92 }
93
94 connman_bool_t __connman_notifier_is_connected(void)
95 {
96         unsigned int i;
97
98         __sync_synchronize();
99         for (i = 0; i < MAX_TECHNOLOGIES; i++) {
100                 if (connected[i] > 0)
101                         return TRUE;
102         }
103
104         return FALSE;
105 }
106
107 static const char *evaluate_notifier_state(void)
108 {
109         unsigned int count;
110
111         count = notifier_count_online();
112         if (count > 0)
113                 return "online";
114
115         if (__connman_notifier_is_connected() == TRUE)
116                 return "ready";
117
118         if ( __connman_technology_get_offlinemode() == TRUE)
119                 return "offline";
120
121         return "idle";
122 }
123
124 const char *__connman_notifier_get_state(void)
125 {
126         return notifier_state;
127 }
128
129 static void state_changed(void)
130 {
131         const char *state;
132
133         state = evaluate_notifier_state();
134
135         if (g_strcmp0(state, notifier_state) == 0)
136                 return;
137
138         notifier_state = state;
139
140         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
141                                 CONNMAN_MANAGER_INTERFACE, "State",
142                                                 DBUS_TYPE_STRING, &notifier_state);
143 }
144
145 static void technology_connected(enum connman_service_type type,
146                                                 connman_bool_t connected)
147 {
148         DBG("type %d connected %d", type, connected);
149
150         __connman_technology_set_connected(type, connected);
151         state_changed();
152 }
153
154 void __connman_notifier_connect(enum connman_service_type type)
155 {
156         DBG("type %d", type);
157
158         switch (type) {
159         case CONNMAN_SERVICE_TYPE_UNKNOWN:
160         case CONNMAN_SERVICE_TYPE_SYSTEM:
161         case CONNMAN_SERVICE_TYPE_GPS:
162         case CONNMAN_SERVICE_TYPE_VPN:
163         case CONNMAN_SERVICE_TYPE_GADGET:
164                 return;
165         case CONNMAN_SERVICE_TYPE_ETHERNET:
166         case CONNMAN_SERVICE_TYPE_WIFI:
167         case CONNMAN_SERVICE_TYPE_WIMAX:
168         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169         case CONNMAN_SERVICE_TYPE_CELLULAR:
170                 break;
171         }
172
173         if (__sync_fetch_and_add(&connected[type], 1) == 0)
174                 technology_connected(type, TRUE);
175 }
176
177 void __connman_notifier_online(enum connman_service_type type)
178 {
179         DBG("type %d", type);
180
181         if (__sync_fetch_and_add(&online[type], 1) == 0)
182                 state_changed();
183 }
184
185 void __connman_notifier_disconnect(enum connman_service_type type,
186                                         enum connman_service_state old_state)
187 {
188         DBG("type %d", type);
189
190         __sync_synchronize();
191         if (connected[type] == 0) {
192                 connman_error("notifier disconnect underflow");
193                 return;
194         }
195
196         switch (type) {
197         case CONNMAN_SERVICE_TYPE_UNKNOWN:
198         case CONNMAN_SERVICE_TYPE_SYSTEM:
199         case CONNMAN_SERVICE_TYPE_GPS:
200         case CONNMAN_SERVICE_TYPE_VPN:
201         case CONNMAN_SERVICE_TYPE_GADGET:
202                 return;
203         case CONNMAN_SERVICE_TYPE_ETHERNET:
204         case CONNMAN_SERVICE_TYPE_WIFI:
205         case CONNMAN_SERVICE_TYPE_WIMAX:
206         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
207         case CONNMAN_SERVICE_TYPE_CELLULAR:
208                 break;
209         }
210
211         if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
212                 __sync_fetch_and_sub(&online[type], 1);
213
214         if (__sync_fetch_and_sub(&connected[type], 1) != 1)
215                 return;
216
217         technology_connected(type, FALSE);
218 }
219
220 static void technology_default(enum connman_service_type type)
221 {
222         const char *str;
223
224         str = __connman_service_type2string(type);
225         if (str == NULL)
226                 str = "";
227
228         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
229                         CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
230                                                 DBUS_TYPE_STRING, &str);
231 }
232
233 void __connman_notifier_default_changed(struct connman_service *service)
234 {
235         enum connman_service_type type = connman_service_get_type(service);
236         GSList *list;
237
238         technology_default(type);
239
240         for (list = notifier_list; list; list = list->next) {
241                 struct connman_notifier *notifier = list->data;
242
243                 if (notifier->default_changed)
244                         notifier->default_changed(service);
245         }
246 }
247
248 void __connman_notifier_service_add(struct connman_service *service,
249                                         const char *name)
250 {
251         GSList *list;
252
253         for (list = notifier_list; list; list = list->next) {
254                 struct connman_notifier *notifier = list->data;
255
256                 if (notifier->service_add)
257                         notifier->service_add(service, name);
258         }
259 }
260
261 void __connman_notifier_service_remove(struct connman_service *service)
262 {
263         GSList *list;
264
265         if (g_hash_table_lookup(service_hash, service) != NULL) {
266                 /*
267                  * This is a tempory check for consistency. It can be
268                  * removed when there are no reports for the following
269                  * error message.
270                  */
271                 connman_error("Service state machine inconsistency detected.");
272
273                 g_hash_table_remove(service_hash, service);
274         }
275
276         for (list = notifier_list; list; list = list->next) {
277                 struct connman_notifier *notifier = list->data;
278
279                 if (notifier->service_remove)
280                         notifier->service_remove(service);
281         }
282 }
283
284 void __connman_notifier_proxy_changed(struct connman_service *service)
285 {
286         GSList *list;
287
288         for (list = notifier_list; list; list = list->next) {
289                 struct connman_notifier *notifier = list->data;
290
291                 if (notifier->proxy_changed)
292                         notifier->proxy_changed(service);
293         }
294 }
295
296 static void offlinemode_changed(dbus_bool_t enabled)
297 {
298         DBG("enabled %d", enabled);
299
300         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
301                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
302                                                 DBUS_TYPE_BOOLEAN, &enabled);
303 }
304
305 void __connman_notifier_offlinemode(connman_bool_t enabled)
306 {
307         GSList *list;
308
309         DBG("enabled %d", enabled);
310
311         offlinemode_changed(enabled);
312         state_changed();
313
314         for (list = notifier_list; list; list = list->next) {
315                 struct connman_notifier *notifier = list->data;
316
317                 if (notifier->offline_mode)
318                         notifier->offline_mode(enabled);
319         }
320 }
321
322 static void notify_idle_state(connman_bool_t idle)
323 {
324         GSList *list;
325
326         DBG("idle %d", idle);
327
328         for (list = notifier_list; list; list = list->next) {
329                 struct connman_notifier *notifier = list->data;
330
331                 if (notifier->idle_state)
332                         notifier->idle_state(idle);
333         }
334 }
335
336 void __connman_notifier_service_state_changed(struct connman_service *service,
337                                         enum connman_service_state state)
338 {
339         GSList *list;
340         unsigned int old_size;
341         connman_bool_t found;
342
343         for (list = notifier_list; list; list = list->next) {
344                 struct connman_notifier *notifier = list->data;
345
346                 if (notifier->service_state_changed)
347                         notifier->service_state_changed(service, state);
348         }
349
350         old_size = g_hash_table_size(service_hash);
351         found = g_hash_table_lookup(service_hash, service) != NULL;
352
353         switch (state) {
354         case CONNMAN_SERVICE_STATE_UNKNOWN:
355         case CONNMAN_SERVICE_STATE_FAILURE:
356         case CONNMAN_SERVICE_STATE_DISCONNECT:
357         case CONNMAN_SERVICE_STATE_IDLE:
358                 if (found == FALSE)
359                         break;
360
361                 g_hash_table_remove(service_hash, service);
362                 if (old_size == 1)
363                         notify_idle_state(TRUE);
364
365                 break;
366         case CONNMAN_SERVICE_STATE_ASSOCIATION:
367         case CONNMAN_SERVICE_STATE_CONFIGURATION:
368         case CONNMAN_SERVICE_STATE_READY:
369         case CONNMAN_SERVICE_STATE_ONLINE:
370                 if (found == TRUE)
371                         break;
372
373                 g_hash_table_insert(service_hash, service, service);
374                 if (old_size == 0)
375                         notify_idle_state(FALSE);
376
377                 break;
378         }
379 }
380
381 void __connman_notifier_ipconfig_changed(struct connman_service *service,
382                                         struct connman_ipconfig *ipconfig)
383 {
384         GSList *list;
385
386         for (list = notifier_list; list; list = list->next) {
387                 struct connman_notifier *notifier = list->data;
388
389                 if (notifier->ipconfig_changed)
390                         notifier->ipconfig_changed(service, ipconfig);
391         }
392 }
393
394 int __connman_notifier_init(void)
395 {
396         DBG("");
397
398         connection = connman_dbus_get_connection();
399
400         service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
401                                                 NULL, NULL);
402
403         notifier_state = evaluate_notifier_state();
404
405         return 0;
406 }
407
408 void __connman_notifier_cleanup(void)
409 {
410         DBG("");
411
412         g_hash_table_destroy(service_hash);
413         service_hash = NULL;
414
415         dbus_connection_unref(connection);
416 }