5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
30 static DBusConnection *connection = NULL;
32 static GSList *notifier_list = NULL;
33 static GHashTable *service_hash = NULL;
35 static gint compare_priority(gconstpointer a, gconstpointer b)
37 const struct connman_notifier *notifier1 = a;
38 const struct connman_notifier *notifier2 = b;
40 return notifier2->priority - notifier1->priority;
44 * connman_notifier_register:
45 * @notifier: notifier module
47 * Register a new notifier module
49 * Returns: %0 on success
51 int connman_notifier_register(struct connman_notifier *notifier)
53 DBG("notifier %p name %s", notifier, notifier->name);
55 notifier_list = g_slist_insert_sorted(notifier_list, notifier,
62 * connman_notifier_unregister:
63 * @notifier: notifier module
65 * Remove a previously registered notifier module
67 void connman_notifier_unregister(struct connman_notifier *notifier)
69 DBG("notifier %p name %s", notifier, notifier->name);
71 notifier_list = g_slist_remove(notifier_list, notifier);
74 #define MAX_TECHNOLOGIES 10
76 static volatile gint registered[MAX_TECHNOLOGIES];
77 static volatile gint enabled[MAX_TECHNOLOGIES];
78 static volatile gint connected[MAX_TECHNOLOGIES];
80 void __connman_notifier_list_registered(DBusMessageIter *iter, void *user_data)
84 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
85 const char *type = __connman_service_type2string(i);
90 if (g_atomic_int_get(®istered[i]) > 0)
91 dbus_message_iter_append_basic(iter,
92 DBUS_TYPE_STRING, &type);
96 void __connman_notifier_list_enabled(DBusMessageIter *iter, void *user_data)
100 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
101 const char *type = __connman_service_type2string(i);
106 if (g_atomic_int_get(&enabled[i]) > 0)
107 dbus_message_iter_append_basic(iter,
108 DBUS_TYPE_STRING, &type);
112 void __connman_notifier_list_connected(DBusMessageIter *iter, void *user_data)
116 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
117 const char *type = __connman_service_type2string(i);
122 if (g_atomic_int_get(&connected[i]) > 0)
123 dbus_message_iter_append_basic(iter,
124 DBUS_TYPE_STRING, &type);
128 static void technology_registered(enum connman_service_type type,
129 connman_bool_t registered)
131 DBG("type %d registered %d", type, registered);
133 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
134 CONNMAN_MANAGER_INTERFACE, "AvailableTechnologies",
135 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
138 static void technology_enabled(enum connman_service_type type,
139 connman_bool_t enabled)
143 DBG("type %d enabled %d", type, enabled);
145 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
146 CONNMAN_MANAGER_INTERFACE, "EnabledTechnologies",
147 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
149 for (list = notifier_list; list; list = list->next) {
150 struct connman_notifier *notifier = list->data;
152 if (notifier->service_enabled)
153 notifier->service_enabled(type, enabled);
157 unsigned int __connman_notifier_count_connected(void)
159 unsigned int i, count = 0;
161 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
162 if (g_atomic_int_get(&connected[i]) > 0)
169 const char *__connman_notifier_get_state(void)
171 unsigned int count = __connman_notifier_count_connected();
179 static void state_changed(connman_bool_t connected)
181 unsigned int count = __connman_notifier_count_connected();
182 char *state = "offline";
189 if (connected == FALSE)
195 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
196 CONNMAN_MANAGER_INTERFACE, "State",
197 DBUS_TYPE_STRING, &state);
199 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
200 CONNMAN_MANAGER_INTERFACE, "StateChanged");
204 dbus_message_append_args(signal, DBUS_TYPE_STRING, &state,
207 g_dbus_send_message(connection, signal);
210 static void technology_connected(enum connman_service_type type,
211 connman_bool_t connected)
213 DBG("type %d connected %d", type, connected);
215 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
216 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
217 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
219 state_changed(connected);
222 void __connman_notifier_register(enum connman_service_type type)
224 DBG("type %d", type);
227 case CONNMAN_SERVICE_TYPE_UNKNOWN:
228 case CONNMAN_SERVICE_TYPE_SYSTEM:
229 case CONNMAN_SERVICE_TYPE_GPS:
230 case CONNMAN_SERVICE_TYPE_VPN:
231 case CONNMAN_SERVICE_TYPE_GADGET:
233 case CONNMAN_SERVICE_TYPE_ETHERNET:
234 case CONNMAN_SERVICE_TYPE_WIFI:
235 case CONNMAN_SERVICE_TYPE_WIMAX:
236 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
237 case CONNMAN_SERVICE_TYPE_CELLULAR:
241 if (g_atomic_int_exchange_and_add(®istered[type], 1) == 0)
242 technology_registered(type, TRUE);
245 void __connman_notifier_unregister(enum connman_service_type type)
247 DBG("type %d", type);
249 if (g_atomic_int_get(®istered[type]) == 0) {
250 connman_error("notifier unregister underflow");
255 case CONNMAN_SERVICE_TYPE_UNKNOWN:
256 case CONNMAN_SERVICE_TYPE_SYSTEM:
257 case CONNMAN_SERVICE_TYPE_GPS:
258 case CONNMAN_SERVICE_TYPE_VPN:
259 case CONNMAN_SERVICE_TYPE_GADGET:
261 case CONNMAN_SERVICE_TYPE_ETHERNET:
262 case CONNMAN_SERVICE_TYPE_WIFI:
263 case CONNMAN_SERVICE_TYPE_WIMAX:
264 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
265 case CONNMAN_SERVICE_TYPE_CELLULAR:
269 if (g_atomic_int_dec_and_test(®istered[type]) == TRUE)
270 technology_registered(type, FALSE);
273 void __connman_notifier_enable(enum connman_service_type type)
275 DBG("type %d", type);
278 case CONNMAN_SERVICE_TYPE_UNKNOWN:
279 case CONNMAN_SERVICE_TYPE_SYSTEM:
280 case CONNMAN_SERVICE_TYPE_GPS:
281 case CONNMAN_SERVICE_TYPE_VPN:
282 case CONNMAN_SERVICE_TYPE_GADGET:
284 case CONNMAN_SERVICE_TYPE_ETHERNET:
285 case CONNMAN_SERVICE_TYPE_WIFI:
286 case CONNMAN_SERVICE_TYPE_WIMAX:
287 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
288 case CONNMAN_SERVICE_TYPE_CELLULAR:
292 if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
293 technology_enabled(type, TRUE);
296 void __connman_notifier_disable(enum connman_service_type type)
298 DBG("type %d", type);
300 if (g_atomic_int_get(&enabled[type]) == 0) {
301 connman_error("notifier disable underflow");
306 case CONNMAN_SERVICE_TYPE_UNKNOWN:
307 case CONNMAN_SERVICE_TYPE_SYSTEM:
308 case CONNMAN_SERVICE_TYPE_GPS:
309 case CONNMAN_SERVICE_TYPE_VPN:
310 case CONNMAN_SERVICE_TYPE_GADGET:
312 case CONNMAN_SERVICE_TYPE_ETHERNET:
313 case CONNMAN_SERVICE_TYPE_WIFI:
314 case CONNMAN_SERVICE_TYPE_WIMAX:
315 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
316 case CONNMAN_SERVICE_TYPE_CELLULAR:
320 if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
321 technology_enabled(type, FALSE);
324 void __connman_notifier_connect(enum connman_service_type type)
326 DBG("type %d", type);
329 case CONNMAN_SERVICE_TYPE_UNKNOWN:
330 case CONNMAN_SERVICE_TYPE_SYSTEM:
331 case CONNMAN_SERVICE_TYPE_GPS:
332 case CONNMAN_SERVICE_TYPE_VPN:
333 case CONNMAN_SERVICE_TYPE_GADGET:
335 case CONNMAN_SERVICE_TYPE_ETHERNET:
336 case CONNMAN_SERVICE_TYPE_WIFI:
337 case CONNMAN_SERVICE_TYPE_WIMAX:
338 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
339 case CONNMAN_SERVICE_TYPE_CELLULAR:
343 if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
344 technology_connected(type, TRUE);
347 void __connman_notifier_disconnect(enum connman_service_type type)
349 DBG("type %d", type);
351 if (g_atomic_int_get(&connected[type]) == 0) {
352 connman_error("notifier disconnect underflow");
357 case CONNMAN_SERVICE_TYPE_UNKNOWN:
358 case CONNMAN_SERVICE_TYPE_SYSTEM:
359 case CONNMAN_SERVICE_TYPE_GPS:
360 case CONNMAN_SERVICE_TYPE_VPN:
361 case CONNMAN_SERVICE_TYPE_GADGET:
363 case CONNMAN_SERVICE_TYPE_ETHERNET:
364 case CONNMAN_SERVICE_TYPE_WIFI:
365 case CONNMAN_SERVICE_TYPE_WIMAX:
366 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
367 case CONNMAN_SERVICE_TYPE_CELLULAR:
371 if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
372 technology_connected(type, FALSE);
375 static void technology_default(enum connman_service_type type)
379 str = __connman_service_type2string(type);
383 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
384 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
385 DBUS_TYPE_STRING, &str);
388 void __connman_notifier_default_changed(struct connman_service *service)
390 enum connman_service_type type = connman_service_get_type(service);
394 technology_default(type);
396 interface = connman_service_get_interface(service);
397 __connman_tethering_update_interface(interface);
400 for (list = notifier_list; list; list = list->next) {
401 struct connman_notifier *notifier = list->data;
403 if (notifier->default_changed)
404 notifier->default_changed(service);
408 void __connman_notifier_service_add(struct connman_service *service,
413 for (list = notifier_list; list; list = list->next) {
414 struct connman_notifier *notifier = list->data;
416 if (notifier->service_add)
417 notifier->service_add(service, name);
421 void __connman_notifier_service_remove(struct connman_service *service)
425 if (g_hash_table_lookup(service_hash, service) != NULL) {
427 * This is a tempory check for consistency. It can be
428 * removed when there are no reports for the following
431 connman_error("Service state machine inconsistency detected.");
433 g_hash_table_remove(service_hash, service);
436 for (list = notifier_list; list; list = list->next) {
437 struct connman_notifier *notifier = list->data;
439 if (notifier->service_remove)
440 notifier->service_remove(service);
444 void __connman_notifier_proxy_changed(struct connman_service *service)
448 for (list = notifier_list; list; list = list->next) {
449 struct connman_notifier *notifier = list->data;
451 if (notifier->proxy_changed)
452 notifier->proxy_changed(service);
456 static void offlinemode_changed(dbus_bool_t enabled)
458 DBG("enabled %d", enabled);
460 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
461 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
462 DBUS_TYPE_BOOLEAN, &enabled);
465 void __connman_notifier_offlinemode(connman_bool_t enabled)
469 DBG("enabled %d", enabled);
471 offlinemode_changed(enabled);
473 for (list = notifier_list; list; list = list->next) {
474 struct connman_notifier *notifier = list->data;
476 if (notifier->offline_mode)
477 notifier->offline_mode(enabled);
481 static void notify_idle_state(connman_bool_t idle)
485 DBG("idle %d", idle);
487 for (list = notifier_list; list; list = list->next) {
488 struct connman_notifier *notifier = list->data;
490 if (notifier->idle_state)
491 notifier->idle_state(idle);
495 void __connman_notifier_service_state_changed(struct connman_service *service,
496 enum connman_service_state state)
499 unsigned int old_size;
500 connman_bool_t found;
502 for (list = notifier_list; list; list = list->next) {
503 struct connman_notifier *notifier = list->data;
505 if (notifier->service_state_changed)
506 notifier->service_state_changed(service, state);
509 old_size = g_hash_table_size(service_hash);
510 found = g_hash_table_lookup(service_hash, service) != NULL;
513 case CONNMAN_SERVICE_STATE_UNKNOWN:
514 case CONNMAN_SERVICE_STATE_DISCONNECT:
515 case CONNMAN_SERVICE_STATE_IDLE:
519 g_hash_table_remove(service_hash, service);
521 notify_idle_state(TRUE);
524 case CONNMAN_SERVICE_STATE_ASSOCIATION:
525 case CONNMAN_SERVICE_STATE_CONFIGURATION:
526 case CONNMAN_SERVICE_STATE_READY:
527 case CONNMAN_SERVICE_STATE_ONLINE:
528 case CONNMAN_SERVICE_STATE_FAILURE:
532 g_hash_table_insert(service_hash, service, service);
534 notify_idle_state(FALSE);
540 void __connman_notifier_ipconfig_changed(struct connman_service *service,
541 struct connman_ipconfig *ipconfig)
545 for (list = notifier_list; list; list = list->next) {
546 struct connman_notifier *notifier = list->data;
548 if (notifier->ipconfig_changed)
549 notifier->ipconfig_changed(service, ipconfig);
553 static connman_bool_t technology_supported(enum connman_service_type type)
556 case CONNMAN_SERVICE_TYPE_UNKNOWN:
557 case CONNMAN_SERVICE_TYPE_SYSTEM:
558 case CONNMAN_SERVICE_TYPE_GPS:
559 case CONNMAN_SERVICE_TYPE_VPN:
560 case CONNMAN_SERVICE_TYPE_GADGET:
562 case CONNMAN_SERVICE_TYPE_ETHERNET:
563 case CONNMAN_SERVICE_TYPE_WIFI:
564 case CONNMAN_SERVICE_TYPE_WIMAX:
565 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
566 case CONNMAN_SERVICE_TYPE_CELLULAR:
573 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
575 DBG("type %d", type);
577 if (technology_supported(type) == FALSE)
580 if (g_atomic_int_get(®istered[type]) > 0)
586 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
588 DBG("type %d", type);
590 if (technology_supported(type) == FALSE)
593 if (g_atomic_int_get(&enabled[type]) > 0)
599 int __connman_notifier_init(void)
603 connection = connman_dbus_get_connection();
605 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
612 void __connman_notifier_cleanup(void)
616 g_hash_table_destroy(service_hash);
619 dbus_connection_unref(connection);