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(void)
181 unsigned int count = __connman_notifier_count_connected();
182 char *state = "offline";
191 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
192 CONNMAN_MANAGER_INTERFACE, "State",
193 DBUS_TYPE_STRING, &state);
195 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
196 CONNMAN_MANAGER_INTERFACE, "StateChanged");
200 dbus_message_append_args(signal, DBUS_TYPE_STRING, &state,
203 g_dbus_send_message(connection, signal);
206 static void technology_connected(enum connman_service_type type,
207 connman_bool_t connected)
209 DBG("type %d connected %d", type, connected);
211 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
212 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
213 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
218 void __connman_notifier_register(enum connman_service_type type)
220 DBG("type %d", type);
223 case CONNMAN_SERVICE_TYPE_UNKNOWN:
224 case CONNMAN_SERVICE_TYPE_SYSTEM:
225 case CONNMAN_SERVICE_TYPE_GPS:
226 case CONNMAN_SERVICE_TYPE_VPN:
227 case CONNMAN_SERVICE_TYPE_GADGET:
229 case CONNMAN_SERVICE_TYPE_ETHERNET:
230 case CONNMAN_SERVICE_TYPE_WIFI:
231 case CONNMAN_SERVICE_TYPE_WIMAX:
232 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
233 case CONNMAN_SERVICE_TYPE_CELLULAR:
237 if (g_atomic_int_exchange_and_add(®istered[type], 1) == 0)
238 technology_registered(type, TRUE);
241 void __connman_notifier_unregister(enum connman_service_type type)
243 DBG("type %d", type);
245 if (g_atomic_int_get(®istered[type]) == 0) {
246 connman_error("notifier unregister underflow");
251 case CONNMAN_SERVICE_TYPE_UNKNOWN:
252 case CONNMAN_SERVICE_TYPE_SYSTEM:
253 case CONNMAN_SERVICE_TYPE_GPS:
254 case CONNMAN_SERVICE_TYPE_VPN:
255 case CONNMAN_SERVICE_TYPE_GADGET:
257 case CONNMAN_SERVICE_TYPE_ETHERNET:
258 case CONNMAN_SERVICE_TYPE_WIFI:
259 case CONNMAN_SERVICE_TYPE_WIMAX:
260 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
261 case CONNMAN_SERVICE_TYPE_CELLULAR:
265 if (g_atomic_int_dec_and_test(®istered[type]) == TRUE)
266 technology_registered(type, FALSE);
269 void __connman_notifier_enable(enum connman_service_type type)
271 DBG("type %d", type);
274 case CONNMAN_SERVICE_TYPE_UNKNOWN:
275 case CONNMAN_SERVICE_TYPE_SYSTEM:
276 case CONNMAN_SERVICE_TYPE_GPS:
277 case CONNMAN_SERVICE_TYPE_VPN:
278 case CONNMAN_SERVICE_TYPE_GADGET:
280 case CONNMAN_SERVICE_TYPE_ETHERNET:
281 case CONNMAN_SERVICE_TYPE_WIFI:
282 case CONNMAN_SERVICE_TYPE_WIMAX:
283 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
284 case CONNMAN_SERVICE_TYPE_CELLULAR:
288 if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
289 technology_enabled(type, TRUE);
292 void __connman_notifier_disable(enum connman_service_type type)
294 DBG("type %d", type);
296 if (g_atomic_int_get(&enabled[type]) == 0) {
297 connman_error("notifier disable underflow");
302 case CONNMAN_SERVICE_TYPE_UNKNOWN:
303 case CONNMAN_SERVICE_TYPE_SYSTEM:
304 case CONNMAN_SERVICE_TYPE_GPS:
305 case CONNMAN_SERVICE_TYPE_VPN:
306 case CONNMAN_SERVICE_TYPE_GADGET:
308 case CONNMAN_SERVICE_TYPE_ETHERNET:
309 case CONNMAN_SERVICE_TYPE_WIFI:
310 case CONNMAN_SERVICE_TYPE_WIMAX:
311 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
312 case CONNMAN_SERVICE_TYPE_CELLULAR:
316 if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
317 technology_enabled(type, FALSE);
320 void __connman_notifier_connect(enum connman_service_type type)
322 DBG("type %d", type);
325 case CONNMAN_SERVICE_TYPE_UNKNOWN:
326 case CONNMAN_SERVICE_TYPE_SYSTEM:
327 case CONNMAN_SERVICE_TYPE_GPS:
328 case CONNMAN_SERVICE_TYPE_VPN:
329 case CONNMAN_SERVICE_TYPE_GADGET:
331 case CONNMAN_SERVICE_TYPE_ETHERNET:
332 case CONNMAN_SERVICE_TYPE_WIFI:
333 case CONNMAN_SERVICE_TYPE_WIMAX:
334 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
335 case CONNMAN_SERVICE_TYPE_CELLULAR:
339 if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
340 technology_connected(type, TRUE);
343 void __connman_notifier_disconnect(enum connman_service_type type)
345 DBG("type %d", type);
347 if (g_atomic_int_get(&connected[type]) == 0) {
348 connman_error("notifier disconnect underflow");
353 case CONNMAN_SERVICE_TYPE_UNKNOWN:
354 case CONNMAN_SERVICE_TYPE_SYSTEM:
355 case CONNMAN_SERVICE_TYPE_GPS:
356 case CONNMAN_SERVICE_TYPE_VPN:
357 case CONNMAN_SERVICE_TYPE_GADGET:
359 case CONNMAN_SERVICE_TYPE_ETHERNET:
360 case CONNMAN_SERVICE_TYPE_WIFI:
361 case CONNMAN_SERVICE_TYPE_WIMAX:
362 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
363 case CONNMAN_SERVICE_TYPE_CELLULAR:
367 if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
368 technology_connected(type, FALSE);
371 static void technology_default(enum connman_service_type type)
375 str = __connman_service_type2string(type);
379 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
380 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
381 DBUS_TYPE_STRING, &str);
384 void __connman_notifier_default_changed(struct connman_service *service)
386 enum connman_service_type type = connman_service_get_type(service);
390 technology_default(type);
392 interface = connman_service_get_interface(service);
393 __connman_tethering_update_interface(interface);
396 for (list = notifier_list; list; list = list->next) {
397 struct connman_notifier *notifier = list->data;
399 if (notifier->default_changed)
400 notifier->default_changed(service);
404 void __connman_notifier_service_add(struct connman_service *service,
409 for (list = notifier_list; list; list = list->next) {
410 struct connman_notifier *notifier = list->data;
412 if (notifier->service_add)
413 notifier->service_add(service, name);
417 void __connman_notifier_service_remove(struct connman_service *service)
421 if (g_hash_table_lookup(service_hash, service) != NULL) {
423 * This is a tempory check for consistency. It can be
424 * removed when there are no reports for the following
427 connman_error("Service state machine inconsistency detected.");
429 g_hash_table_remove(service_hash, service);
432 for (list = notifier_list; list; list = list->next) {
433 struct connman_notifier *notifier = list->data;
435 if (notifier->service_remove)
436 notifier->service_remove(service);
440 void __connman_notifier_proxy_changed(struct connman_service *service)
444 for (list = notifier_list; list; list = list->next) {
445 struct connman_notifier *notifier = list->data;
447 if (notifier->proxy_changed)
448 notifier->proxy_changed(service);
452 static void offlinemode_changed(dbus_bool_t enabled)
454 DBG("enabled %d", enabled);
456 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
457 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
458 DBUS_TYPE_BOOLEAN, &enabled);
461 void __connman_notifier_offlinemode(connman_bool_t enabled)
465 DBG("enabled %d", enabled);
467 offlinemode_changed(enabled);
469 for (list = notifier_list; list; list = list->next) {
470 struct connman_notifier *notifier = list->data;
472 if (notifier->offline_mode)
473 notifier->offline_mode(enabled);
477 static void notify_idle_state(connman_bool_t idle)
481 DBG("idle %d", idle);
483 for (list = notifier_list; list; list = list->next) {
484 struct connman_notifier *notifier = list->data;
486 if (notifier->idle_state)
487 notifier->idle_state(idle);
491 void __connman_notifier_service_state_changed(struct connman_service *service,
492 enum connman_service_state state)
495 unsigned int old_size;
496 connman_bool_t found;
498 for (list = notifier_list; list; list = list->next) {
499 struct connman_notifier *notifier = list->data;
501 if (notifier->service_state_changed)
502 notifier->service_state_changed(service, state);
505 old_size = g_hash_table_size(service_hash);
506 found = g_hash_table_lookup(service_hash, service) != NULL;
509 case CONNMAN_SERVICE_STATE_UNKNOWN:
510 case CONNMAN_SERVICE_STATE_DISCONNECT:
511 case CONNMAN_SERVICE_STATE_IDLE:
515 g_hash_table_remove(service_hash, service);
517 notify_idle_state(TRUE);
520 case CONNMAN_SERVICE_STATE_ASSOCIATION:
521 case CONNMAN_SERVICE_STATE_CONFIGURATION:
522 case CONNMAN_SERVICE_STATE_READY:
523 case CONNMAN_SERVICE_STATE_ONLINE:
524 case CONNMAN_SERVICE_STATE_FAILURE:
528 g_hash_table_insert(service_hash, service, service);
530 notify_idle_state(FALSE);
536 void __connman_notifier_ipconfig_changed(struct connman_service *service,
537 struct connman_ipconfig *ipconfig)
541 for (list = notifier_list; list; list = list->next) {
542 struct connman_notifier *notifier = list->data;
544 if (notifier->ipconfig_changed)
545 notifier->ipconfig_changed(service, ipconfig);
549 static connman_bool_t technology_supported(enum connman_service_type type)
552 case CONNMAN_SERVICE_TYPE_UNKNOWN:
553 case CONNMAN_SERVICE_TYPE_SYSTEM:
554 case CONNMAN_SERVICE_TYPE_GPS:
555 case CONNMAN_SERVICE_TYPE_VPN:
556 case CONNMAN_SERVICE_TYPE_GADGET:
558 case CONNMAN_SERVICE_TYPE_ETHERNET:
559 case CONNMAN_SERVICE_TYPE_WIFI:
560 case CONNMAN_SERVICE_TYPE_WIMAX:
561 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
562 case CONNMAN_SERVICE_TYPE_CELLULAR:
569 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
571 DBG("type %d", type);
573 if (technology_supported(type) == FALSE)
576 if (g_atomic_int_get(®istered[type]) > 0)
582 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
584 DBG("type %d", type);
586 if (technology_supported(type) == FALSE)
589 if (g_atomic_int_get(&enabled[type]) > 0)
595 int __connman_notifier_init(void)
599 connection = connman_dbus_get_connection();
601 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
608 void __connman_notifier_cleanup(void)
612 g_hash_table_destroy(service_hash);
615 dbus_connection_unref(connection);