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