[connman] Added Tizen Wi-Fi Mesh
[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 #if defined TIZEN_EXT_WIFI_MESH
157         case CONNMAN_SERVICE_TYPE_MESH:
158 #endif
159                 break;
160         }
161
162         if (__sync_fetch_and_add(&connected[type], 1) == 0) {
163                 __connman_technology_set_connected(type, true);
164                 state_changed();
165         }
166 }
167
168 void __connman_notifier_enter_online(enum connman_service_type type)
169 {
170         DBG("type %d", type);
171
172         if (__sync_fetch_and_add(&online[type], 1) == 0)
173                 state_changed();
174 }
175
176 void __connman_notifier_leave_online(enum connman_service_type type)
177 {
178         DBG("type %d", type);
179
180         if (__sync_fetch_and_sub(&online[type], 1) == 1)
181                 state_changed();
182 }
183
184 void __connman_notifier_disconnect(enum connman_service_type type)
185 {
186         DBG("type %d", type);
187
188         __sync_synchronize();
189         if (connected[type] == 0) {
190                 connman_error("notifier disconnect underflow");
191                 return;
192         }
193
194         switch (type) {
195         case CONNMAN_SERVICE_TYPE_UNKNOWN:
196         case CONNMAN_SERVICE_TYPE_SYSTEM:
197         case CONNMAN_SERVICE_TYPE_GPS:
198         case CONNMAN_SERVICE_TYPE_VPN:
199                 return;
200         case CONNMAN_SERVICE_TYPE_ETHERNET:
201         case CONNMAN_SERVICE_TYPE_GADGET:
202         case CONNMAN_SERVICE_TYPE_WIFI:
203         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
204         case CONNMAN_SERVICE_TYPE_CELLULAR:
205         case CONNMAN_SERVICE_TYPE_P2P:
206 #if defined TIZEN_EXT_WIFI_MESH
207         case CONNMAN_SERVICE_TYPE_MESH:
208 #endif
209                 break;
210         }
211
212         if (__sync_fetch_and_sub(&connected[type], 1) != 1)
213                 return;
214
215         __connman_technology_set_connected(type, false);
216         state_changed();
217 }
218
219 void __connman_notifier_default_changed(struct connman_service *service)
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->default_changed)
227                         notifier->default_changed(service);
228         }
229 }
230
231 void __connman_notifier_service_add(struct connman_service *service,
232                                         const char *name)
233 {
234         GSList *list;
235
236         for (list = notifier_list; list; list = list->next) {
237                 struct connman_notifier *notifier = list->data;
238
239                 if (notifier->service_add)
240                         notifier->service_add(service, name);
241         }
242 }
243
244 void __connman_notifier_service_remove(struct connman_service *service)
245 {
246         GSList *list;
247
248         if (g_hash_table_lookup(service_hash, service)) {
249                 /*
250                  * This is a tempory check for consistency. It can be
251                  * removed when there are no reports for the following
252                  * error message.
253                  */
254                 connman_error("Service state machine inconsistency detected.");
255
256                 g_hash_table_remove(service_hash, service);
257         }
258
259         for (list = notifier_list; list; list = list->next) {
260                 struct connman_notifier *notifier = list->data;
261
262                 if (notifier->service_remove)
263                         notifier->service_remove(service);
264         }
265 }
266
267 void __connman_notifier_proxy_changed(struct connman_service *service)
268 {
269         GSList *list;
270
271         for (list = notifier_list; list; list = list->next) {
272                 struct connman_notifier *notifier = list->data;
273
274                 if (notifier->proxy_changed)
275                         notifier->proxy_changed(service);
276         }
277 }
278
279 static void offlinemode_changed(dbus_bool_t enabled)
280 {
281         DBG("enabled %d", enabled);
282
283         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
284                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
285                                                 DBUS_TYPE_BOOLEAN, &enabled);
286 }
287
288 void __connman_notifier_offlinemode(bool enabled)
289 {
290         GSList *list;
291
292         DBG("enabled %d", enabled);
293
294         offlinemode_changed(enabled);
295         state_changed();
296
297         for (list = notifier_list; list; list = list->next) {
298                 struct connman_notifier *notifier = list->data;
299
300                 if (notifier->offline_mode)
301                         notifier->offline_mode(enabled);
302         }
303 }
304
305 static void notify_idle_state(bool idle)
306 {
307         GSList *list;
308
309         DBG("idle %d", idle);
310
311         for (list = notifier_list; list; list = list->next) {
312                 struct connman_notifier *notifier = list->data;
313
314                 if (notifier->idle_state)
315                         notifier->idle_state(idle);
316         }
317 }
318
319 void __connman_notifier_service_state_changed(struct connman_service *service,
320                                         enum connman_service_state state)
321 {
322         GSList *list;
323         unsigned int old_size;
324         bool found;
325
326         for (list = notifier_list; list; list = list->next) {
327                 struct connman_notifier *notifier = list->data;
328
329                 if (notifier->service_state_changed)
330                         notifier->service_state_changed(service, state);
331         }
332
333         old_size = g_hash_table_size(service_hash);
334         found = g_hash_table_lookup(service_hash, service);
335
336         switch (state) {
337         case CONNMAN_SERVICE_STATE_UNKNOWN:
338         case CONNMAN_SERVICE_STATE_FAILURE:
339         case CONNMAN_SERVICE_STATE_DISCONNECT:
340         case CONNMAN_SERVICE_STATE_IDLE:
341                 if (!found)
342                         break;
343
344                 g_hash_table_remove(service_hash, service);
345                 if (old_size == 1)
346                         notify_idle_state(true);
347
348                 break;
349         case CONNMAN_SERVICE_STATE_ASSOCIATION:
350         case CONNMAN_SERVICE_STATE_CONFIGURATION:
351         case CONNMAN_SERVICE_STATE_READY:
352         case CONNMAN_SERVICE_STATE_ONLINE:
353                 if (found)
354                         break;
355
356                 g_hash_table_insert(service_hash, service, service);
357                 if (old_size == 0)
358                         notify_idle_state(false);
359
360                 break;
361         }
362 }
363
364 void __connman_notifier_ipconfig_changed(struct connman_service *service,
365                                         struct connman_ipconfig *ipconfig)
366 {
367         GSList *list;
368
369         for (list = notifier_list; list; list = list->next) {
370                 struct connman_notifier *notifier = list->data;
371
372                 if (notifier->ipconfig_changed)
373                         notifier->ipconfig_changed(service, ipconfig);
374         }
375 }
376
377 int __connman_notifier_init(void)
378 {
379         DBG("");
380
381         connection = connman_dbus_get_connection();
382
383         service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
384                                                 NULL, NULL);
385
386         notifier_state = evaluate_notifier_state();
387
388         return 0;
389 }
390
391 void __connman_notifier_cleanup(void)
392 {
393         DBG("");
394
395         g_hash_table_destroy(service_hash);
396         service_hash = NULL;
397
398         dbus_connection_unref(connection);
399 }