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