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