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 int registered[MAX_TECHNOLOGIES];
77 static volatile int enabled[MAX_TECHNOLOGIES];
78 static volatile int connected[MAX_TECHNOLOGIES];
80 void __connman_notifier_list_registered(DBusMessageIter *iter, void *user_data)
85 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
86 const char *type = __connman_service_type2string(i);
91 if (registered[i] > 0)
92 dbus_message_iter_append_basic(iter,
93 DBUS_TYPE_STRING, &type);
97 void __connman_notifier_list_enabled(DBusMessageIter *iter, void *user_data)
101 __sync_synchronize();
102 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
103 const char *type = __connman_service_type2string(i);
109 dbus_message_iter_append_basic(iter,
110 DBUS_TYPE_STRING, &type);
114 void __connman_notifier_list_connected(DBusMessageIter *iter, void *user_data)
118 __sync_synchronize();
119 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
120 const char *type = __connman_service_type2string(i);
125 if (connected[i] > 0)
126 dbus_message_iter_append_basic(iter,
127 DBUS_TYPE_STRING, &type);
131 static void technology_registered(enum connman_service_type type,
132 connman_bool_t registered)
134 DBG("type %d registered %d", type, registered);
136 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
137 CONNMAN_MANAGER_INTERFACE, "AvailableTechnologies",
138 DBUS_TYPE_STRING, __connman_notifier_list_registered, NULL);
141 static void technology_enabled(enum connman_service_type type,
142 connman_bool_t enabled)
146 DBG("type %d enabled %d", type, enabled);
148 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
149 CONNMAN_MANAGER_INTERFACE, "EnabledTechnologies",
150 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
152 for (list = notifier_list; list; list = list->next) {
153 struct connman_notifier *notifier = list->data;
155 if (notifier->service_enabled)
156 notifier->service_enabled(type, enabled);
160 unsigned int __connman_notifier_count_connected(void)
162 unsigned int i, count = 0;
164 __sync_synchronize();
165 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
166 if (connected[i] > 0)
173 const char *__connman_notifier_get_state(void)
175 unsigned int count = __connman_notifier_count_connected();
183 static void state_changed(connman_bool_t connected)
185 unsigned int count = __connman_notifier_count_connected();
186 char *state = "offline";
193 if (connected == FALSE)
199 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
200 CONNMAN_MANAGER_INTERFACE, "State",
201 DBUS_TYPE_STRING, &state);
203 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
204 CONNMAN_MANAGER_INTERFACE, "StateChanged");
208 dbus_message_append_args(signal, DBUS_TYPE_STRING, &state,
211 g_dbus_send_message(connection, signal);
214 static void technology_connected(enum connman_service_type type,
215 connman_bool_t connected)
217 DBG("type %d connected %d", type, connected);
219 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
220 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
221 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
223 state_changed(connected);
226 void __connman_notifier_register(enum connman_service_type type)
228 DBG("type %d", type);
231 case CONNMAN_SERVICE_TYPE_UNKNOWN:
232 case CONNMAN_SERVICE_TYPE_SYSTEM:
233 case CONNMAN_SERVICE_TYPE_GPS:
234 case CONNMAN_SERVICE_TYPE_VPN:
235 case CONNMAN_SERVICE_TYPE_GADGET:
237 case CONNMAN_SERVICE_TYPE_ETHERNET:
238 case CONNMAN_SERVICE_TYPE_WIFI:
239 case CONNMAN_SERVICE_TYPE_WIMAX:
240 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
241 case CONNMAN_SERVICE_TYPE_CELLULAR:
245 if (__sync_fetch_and_add(®istered[type], 1) == 0)
246 technology_registered(type, TRUE);
249 void __connman_notifier_unregister(enum connman_service_type type)
251 DBG("type %d", type);
253 __sync_synchronize();
254 if (registered[type] == 0) {
255 connman_error("notifier unregister underflow");
260 case CONNMAN_SERVICE_TYPE_UNKNOWN:
261 case CONNMAN_SERVICE_TYPE_SYSTEM:
262 case CONNMAN_SERVICE_TYPE_GPS:
263 case CONNMAN_SERVICE_TYPE_VPN:
264 case CONNMAN_SERVICE_TYPE_GADGET:
266 case CONNMAN_SERVICE_TYPE_ETHERNET:
267 case CONNMAN_SERVICE_TYPE_WIFI:
268 case CONNMAN_SERVICE_TYPE_WIMAX:
269 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
270 case CONNMAN_SERVICE_TYPE_CELLULAR:
274 if (__sync_fetch_and_sub(®istered[type], 1) != 1)
277 technology_registered(type, FALSE);
280 void __connman_notifier_enable(enum connman_service_type type)
282 DBG("type %d", type);
285 case CONNMAN_SERVICE_TYPE_UNKNOWN:
286 case CONNMAN_SERVICE_TYPE_SYSTEM:
287 case CONNMAN_SERVICE_TYPE_GPS:
288 case CONNMAN_SERVICE_TYPE_VPN:
289 case CONNMAN_SERVICE_TYPE_GADGET:
291 case CONNMAN_SERVICE_TYPE_ETHERNET:
292 case CONNMAN_SERVICE_TYPE_WIFI:
293 case CONNMAN_SERVICE_TYPE_WIMAX:
294 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
295 case CONNMAN_SERVICE_TYPE_CELLULAR:
299 if (__sync_fetch_and_add(&enabled[type], 1) == 0)
300 technology_enabled(type, TRUE);
303 void __connman_notifier_disable(enum connman_service_type type)
305 DBG("type %d", type);
307 __sync_synchronize();
308 if (enabled[type] == 0) {
309 connman_error("notifier disable underflow");
314 case CONNMAN_SERVICE_TYPE_UNKNOWN:
315 case CONNMAN_SERVICE_TYPE_SYSTEM:
316 case CONNMAN_SERVICE_TYPE_GPS:
317 case CONNMAN_SERVICE_TYPE_VPN:
318 case CONNMAN_SERVICE_TYPE_GADGET:
320 case CONNMAN_SERVICE_TYPE_ETHERNET:
321 case CONNMAN_SERVICE_TYPE_WIFI:
322 case CONNMAN_SERVICE_TYPE_WIMAX:
323 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
324 case CONNMAN_SERVICE_TYPE_CELLULAR:
328 if (__sync_fetch_and_sub(&enabled[type], 1) != 1)
331 technology_enabled(type, FALSE);
334 void __connman_notifier_connect(enum connman_service_type type)
336 DBG("type %d", type);
339 case CONNMAN_SERVICE_TYPE_UNKNOWN:
340 case CONNMAN_SERVICE_TYPE_SYSTEM:
341 case CONNMAN_SERVICE_TYPE_GPS:
342 case CONNMAN_SERVICE_TYPE_VPN:
343 case CONNMAN_SERVICE_TYPE_GADGET:
345 case CONNMAN_SERVICE_TYPE_ETHERNET:
346 case CONNMAN_SERVICE_TYPE_WIFI:
347 case CONNMAN_SERVICE_TYPE_WIMAX:
348 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
349 case CONNMAN_SERVICE_TYPE_CELLULAR:
353 if (__sync_fetch_and_add(&connected[type], 1) == 0)
354 technology_connected(type, TRUE);
357 void __connman_notifier_disconnect(enum connman_service_type type)
359 DBG("type %d", type);
361 __sync_synchronize();
362 if (connected[type] == 0) {
363 connman_error("notifier disconnect underflow");
368 case CONNMAN_SERVICE_TYPE_UNKNOWN:
369 case CONNMAN_SERVICE_TYPE_SYSTEM:
370 case CONNMAN_SERVICE_TYPE_GPS:
371 case CONNMAN_SERVICE_TYPE_VPN:
372 case CONNMAN_SERVICE_TYPE_GADGET:
374 case CONNMAN_SERVICE_TYPE_ETHERNET:
375 case CONNMAN_SERVICE_TYPE_WIFI:
376 case CONNMAN_SERVICE_TYPE_WIMAX:
377 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
378 case CONNMAN_SERVICE_TYPE_CELLULAR:
382 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
385 technology_connected(type, FALSE);
388 static void technology_default(enum connman_service_type type)
392 str = __connman_service_type2string(type);
396 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
397 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
398 DBUS_TYPE_STRING, &str);
401 void __connman_notifier_default_changed(struct connman_service *service)
403 enum connman_service_type type = connman_service_get_type(service);
407 technology_default(type);
409 interface = connman_service_get_interface(service);
410 __connman_tethering_update_interface(interface);
413 for (list = notifier_list; list; list = list->next) {
414 struct connman_notifier *notifier = list->data;
416 if (notifier->default_changed)
417 notifier->default_changed(service);
421 void __connman_notifier_service_add(struct connman_service *service,
426 for (list = notifier_list; list; list = list->next) {
427 struct connman_notifier *notifier = list->data;
429 if (notifier->service_add)
430 notifier->service_add(service, name);
434 void __connman_notifier_service_remove(struct connman_service *service)
438 if (g_hash_table_lookup(service_hash, service) != NULL) {
440 * This is a tempory check for consistency. It can be
441 * removed when there are no reports for the following
444 connman_error("Service state machine inconsistency detected.");
446 g_hash_table_remove(service_hash, service);
449 for (list = notifier_list; list; list = list->next) {
450 struct connman_notifier *notifier = list->data;
452 if (notifier->service_remove)
453 notifier->service_remove(service);
457 void __connman_notifier_proxy_changed(struct connman_service *service)
461 for (list = notifier_list; list; list = list->next) {
462 struct connman_notifier *notifier = list->data;
464 if (notifier->proxy_changed)
465 notifier->proxy_changed(service);
469 static void offlinemode_changed(dbus_bool_t enabled)
471 DBG("enabled %d", enabled);
473 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
474 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
475 DBUS_TYPE_BOOLEAN, &enabled);
478 void __connman_notifier_offlinemode(connman_bool_t enabled)
482 DBG("enabled %d", enabled);
484 offlinemode_changed(enabled);
486 for (list = notifier_list; list; list = list->next) {
487 struct connman_notifier *notifier = list->data;
489 if (notifier->offline_mode)
490 notifier->offline_mode(enabled);
494 static void notify_idle_state(connman_bool_t idle)
498 DBG("idle %d", idle);
500 for (list = notifier_list; list; list = list->next) {
501 struct connman_notifier *notifier = list->data;
503 if (notifier->idle_state)
504 notifier->idle_state(idle);
508 void __connman_notifier_service_state_changed(struct connman_service *service,
509 enum connman_service_state state)
512 unsigned int old_size;
513 connman_bool_t found;
515 for (list = notifier_list; list; list = list->next) {
516 struct connman_notifier *notifier = list->data;
518 if (notifier->service_state_changed)
519 notifier->service_state_changed(service, state);
522 old_size = g_hash_table_size(service_hash);
523 found = g_hash_table_lookup(service_hash, service) != NULL;
526 case CONNMAN_SERVICE_STATE_UNKNOWN:
527 case CONNMAN_SERVICE_STATE_FAILURE:
528 case CONNMAN_SERVICE_STATE_DISCONNECT:
529 case CONNMAN_SERVICE_STATE_IDLE:
533 g_hash_table_remove(service_hash, service);
535 notify_idle_state(TRUE);
538 case CONNMAN_SERVICE_STATE_ASSOCIATION:
539 case CONNMAN_SERVICE_STATE_CONFIGURATION:
540 case CONNMAN_SERVICE_STATE_READY:
541 case CONNMAN_SERVICE_STATE_ONLINE:
545 g_hash_table_insert(service_hash, service, service);
547 notify_idle_state(FALSE);
553 void __connman_notifier_ipconfig_changed(struct connman_service *service,
554 struct connman_ipconfig *ipconfig)
558 for (list = notifier_list; list; list = list->next) {
559 struct connman_notifier *notifier = list->data;
561 if (notifier->ipconfig_changed)
562 notifier->ipconfig_changed(service, ipconfig);
566 static connman_bool_t technology_supported(enum connman_service_type type)
569 case CONNMAN_SERVICE_TYPE_UNKNOWN:
570 case CONNMAN_SERVICE_TYPE_SYSTEM:
571 case CONNMAN_SERVICE_TYPE_GPS:
572 case CONNMAN_SERVICE_TYPE_VPN:
573 case CONNMAN_SERVICE_TYPE_GADGET:
575 case CONNMAN_SERVICE_TYPE_ETHERNET:
576 case CONNMAN_SERVICE_TYPE_WIFI:
577 case CONNMAN_SERVICE_TYPE_WIMAX:
578 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
579 case CONNMAN_SERVICE_TYPE_CELLULAR:
586 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
588 DBG("type %d", type);
590 if (technology_supported(type) == FALSE)
593 __sync_synchronize();
594 if (registered[type] > 0)
600 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
602 DBG("type %d", type);
604 if (technology_supported(type) == FALSE)
607 __sync_synchronize();
608 if (enabled[type] > 0)
614 int __connman_notifier_init(void)
618 connection = connman_dbus_get_connection();
620 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
627 void __connman_notifier_cleanup(void)
631 g_hash_table_destroy(service_hash);
634 dbus_connection_unref(connection);