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_enabled(DBusMessageIter *iter, void *user_data)
85 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
86 const char *type = __connman_service_type2string(i);
92 dbus_message_iter_append_basic(iter,
93 DBUS_TYPE_STRING, &type);
97 void __connman_notifier_list_connected(DBusMessageIter *iter, void *user_data)
101 __sync_synchronize();
102 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
103 const char *type = __connman_service_type2string(i);
108 if (connected[i] > 0)
109 dbus_message_iter_append_basic(iter,
110 DBUS_TYPE_STRING, &type);
114 static void technology_registered(enum connman_service_type type,
115 connman_bool_t registered)
117 DBG("type %d registered %d", type, registered);
120 static void technology_enabled(enum connman_service_type type,
121 connman_bool_t enabled)
125 DBG("type %d enabled %d", type, enabled);
127 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
128 CONNMAN_MANAGER_INTERFACE, "EnabledTechnologies",
129 DBUS_TYPE_STRING, __connman_notifier_list_enabled, NULL);
131 for (list = notifier_list; list; list = list->next) {
132 struct connman_notifier *notifier = list->data;
134 if (notifier->service_enabled)
135 notifier->service_enabled(type, enabled);
139 unsigned int __connman_notifier_count_connected(void)
141 unsigned int i, count = 0;
143 __sync_synchronize();
144 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
145 if (connected[i] > 0)
152 const char *__connman_notifier_get_state(void)
154 unsigned int count = __connman_notifier_count_connected();
162 static void state_changed(connman_bool_t connected)
164 unsigned int count = __connman_notifier_count_connected();
165 char *state = "offline";
171 if (connected == FALSE)
177 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
178 CONNMAN_MANAGER_INTERFACE, "State",
179 DBUS_TYPE_STRING, &state);
182 static void technology_connected(enum connman_service_type type,
183 connman_bool_t connected)
185 DBG("type %d connected %d", type, connected);
187 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
188 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
189 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
191 state_changed(connected);
194 void __connman_notifier_register(enum connman_service_type type)
196 DBG("type %d", type);
199 case CONNMAN_SERVICE_TYPE_UNKNOWN:
200 case CONNMAN_SERVICE_TYPE_SYSTEM:
201 case CONNMAN_SERVICE_TYPE_GPS:
202 case CONNMAN_SERVICE_TYPE_VPN:
203 case CONNMAN_SERVICE_TYPE_GADGET:
205 case CONNMAN_SERVICE_TYPE_ETHERNET:
206 case CONNMAN_SERVICE_TYPE_WIFI:
207 case CONNMAN_SERVICE_TYPE_WIMAX:
208 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
209 case CONNMAN_SERVICE_TYPE_CELLULAR:
213 if (__sync_fetch_and_add(®istered[type], 1) == 0)
214 technology_registered(type, TRUE);
217 void __connman_notifier_unregister(enum connman_service_type type)
219 DBG("type %d", type);
221 __sync_synchronize();
222 if (registered[type] == 0) {
223 connman_error("notifier unregister underflow");
228 case CONNMAN_SERVICE_TYPE_UNKNOWN:
229 case CONNMAN_SERVICE_TYPE_SYSTEM:
230 case CONNMAN_SERVICE_TYPE_GPS:
231 case CONNMAN_SERVICE_TYPE_VPN:
232 case CONNMAN_SERVICE_TYPE_GADGET:
234 case CONNMAN_SERVICE_TYPE_ETHERNET:
235 case CONNMAN_SERVICE_TYPE_WIFI:
236 case CONNMAN_SERVICE_TYPE_WIMAX:
237 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
238 case CONNMAN_SERVICE_TYPE_CELLULAR:
242 if (__sync_fetch_and_sub(®istered[type], 1) != 1)
245 technology_registered(type, FALSE);
248 void __connman_notifier_enable(enum connman_service_type type)
250 DBG("type %d", type);
253 case CONNMAN_SERVICE_TYPE_UNKNOWN:
254 case CONNMAN_SERVICE_TYPE_SYSTEM:
255 case CONNMAN_SERVICE_TYPE_GPS:
256 case CONNMAN_SERVICE_TYPE_VPN:
257 case CONNMAN_SERVICE_TYPE_GADGET:
259 case CONNMAN_SERVICE_TYPE_ETHERNET:
260 case CONNMAN_SERVICE_TYPE_WIFI:
261 case CONNMAN_SERVICE_TYPE_WIMAX:
262 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
263 case CONNMAN_SERVICE_TYPE_CELLULAR:
267 if (__sync_fetch_and_add(&enabled[type], 1) == 0)
268 technology_enabled(type, TRUE);
271 void __connman_notifier_disable(enum connman_service_type type)
273 DBG("type %d", type);
275 __sync_synchronize();
276 if (enabled[type] == 0) {
277 connman_error("notifier disable underflow");
282 case CONNMAN_SERVICE_TYPE_UNKNOWN:
283 case CONNMAN_SERVICE_TYPE_SYSTEM:
284 case CONNMAN_SERVICE_TYPE_GPS:
285 case CONNMAN_SERVICE_TYPE_VPN:
286 case CONNMAN_SERVICE_TYPE_GADGET:
288 case CONNMAN_SERVICE_TYPE_ETHERNET:
289 case CONNMAN_SERVICE_TYPE_WIFI:
290 case CONNMAN_SERVICE_TYPE_WIMAX:
291 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
292 case CONNMAN_SERVICE_TYPE_CELLULAR:
296 if (__sync_fetch_and_sub(&enabled[type], 1) != 1)
299 technology_enabled(type, FALSE);
302 void __connman_notifier_connect(enum connman_service_type type)
304 DBG("type %d", type);
307 case CONNMAN_SERVICE_TYPE_UNKNOWN:
308 case CONNMAN_SERVICE_TYPE_SYSTEM:
309 case CONNMAN_SERVICE_TYPE_GPS:
310 case CONNMAN_SERVICE_TYPE_VPN:
311 case CONNMAN_SERVICE_TYPE_GADGET:
313 case CONNMAN_SERVICE_TYPE_ETHERNET:
314 case CONNMAN_SERVICE_TYPE_WIFI:
315 case CONNMAN_SERVICE_TYPE_WIMAX:
316 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
317 case CONNMAN_SERVICE_TYPE_CELLULAR:
321 if (__sync_fetch_and_add(&connected[type], 1) == 0)
322 technology_connected(type, TRUE);
325 void __connman_notifier_disconnect(enum connman_service_type type)
327 DBG("type %d", type);
329 __sync_synchronize();
330 if (connected[type] == 0) {
331 connman_error("notifier disconnect underflow");
336 case CONNMAN_SERVICE_TYPE_UNKNOWN:
337 case CONNMAN_SERVICE_TYPE_SYSTEM:
338 case CONNMAN_SERVICE_TYPE_GPS:
339 case CONNMAN_SERVICE_TYPE_VPN:
340 case CONNMAN_SERVICE_TYPE_GADGET:
342 case CONNMAN_SERVICE_TYPE_ETHERNET:
343 case CONNMAN_SERVICE_TYPE_WIFI:
344 case CONNMAN_SERVICE_TYPE_WIMAX:
345 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
346 case CONNMAN_SERVICE_TYPE_CELLULAR:
350 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
353 technology_connected(type, FALSE);
356 static void technology_default(enum connman_service_type type)
360 str = __connman_service_type2string(type);
364 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
365 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
366 DBUS_TYPE_STRING, &str);
369 void __connman_notifier_default_changed(struct connman_service *service)
371 enum connman_service_type type = connman_service_get_type(service);
375 technology_default(type);
377 interface = connman_service_get_interface(service);
378 __connman_tethering_update_interface(interface);
381 for (list = notifier_list; list; list = list->next) {
382 struct connman_notifier *notifier = list->data;
384 if (notifier->default_changed)
385 notifier->default_changed(service);
389 void __connman_notifier_service_add(struct connman_service *service,
394 for (list = notifier_list; list; list = list->next) {
395 struct connman_notifier *notifier = list->data;
397 if (notifier->service_add)
398 notifier->service_add(service, name);
402 void __connman_notifier_service_remove(struct connman_service *service)
406 if (g_hash_table_lookup(service_hash, service) != NULL) {
408 * This is a tempory check for consistency. It can be
409 * removed when there are no reports for the following
412 connman_error("Service state machine inconsistency detected.");
414 g_hash_table_remove(service_hash, service);
417 for (list = notifier_list; list; list = list->next) {
418 struct connman_notifier *notifier = list->data;
420 if (notifier->service_remove)
421 notifier->service_remove(service);
425 void __connman_notifier_proxy_changed(struct connman_service *service)
429 for (list = notifier_list; list; list = list->next) {
430 struct connman_notifier *notifier = list->data;
432 if (notifier->proxy_changed)
433 notifier->proxy_changed(service);
437 static void offlinemode_changed(dbus_bool_t enabled)
439 DBG("enabled %d", enabled);
441 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
442 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
443 DBUS_TYPE_BOOLEAN, &enabled);
446 void __connman_notifier_offlinemode(connman_bool_t enabled)
450 DBG("enabled %d", enabled);
452 offlinemode_changed(enabled);
454 for (list = notifier_list; list; list = list->next) {
455 struct connman_notifier *notifier = list->data;
457 if (notifier->offline_mode)
458 notifier->offline_mode(enabled);
462 static void notify_idle_state(connman_bool_t idle)
466 DBG("idle %d", idle);
468 for (list = notifier_list; list; list = list->next) {
469 struct connman_notifier *notifier = list->data;
471 if (notifier->idle_state)
472 notifier->idle_state(idle);
476 void __connman_notifier_service_state_changed(struct connman_service *service,
477 enum connman_service_state state)
480 unsigned int old_size;
481 connman_bool_t found;
483 for (list = notifier_list; list; list = list->next) {
484 struct connman_notifier *notifier = list->data;
486 if (notifier->service_state_changed)
487 notifier->service_state_changed(service, state);
490 old_size = g_hash_table_size(service_hash);
491 found = g_hash_table_lookup(service_hash, service) != NULL;
494 case CONNMAN_SERVICE_STATE_UNKNOWN:
495 case CONNMAN_SERVICE_STATE_FAILURE:
496 case CONNMAN_SERVICE_STATE_DISCONNECT:
497 case CONNMAN_SERVICE_STATE_IDLE:
501 g_hash_table_remove(service_hash, service);
503 notify_idle_state(TRUE);
506 case CONNMAN_SERVICE_STATE_ASSOCIATION:
507 case CONNMAN_SERVICE_STATE_CONFIGURATION:
508 case CONNMAN_SERVICE_STATE_READY:
509 case CONNMAN_SERVICE_STATE_ONLINE:
513 g_hash_table_insert(service_hash, service, service);
515 notify_idle_state(FALSE);
521 void __connman_notifier_ipconfig_changed(struct connman_service *service,
522 struct connman_ipconfig *ipconfig)
526 for (list = notifier_list; list; list = list->next) {
527 struct connman_notifier *notifier = list->data;
529 if (notifier->ipconfig_changed)
530 notifier->ipconfig_changed(service, ipconfig);
534 static connman_bool_t technology_supported(enum connman_service_type type)
537 case CONNMAN_SERVICE_TYPE_UNKNOWN:
538 case CONNMAN_SERVICE_TYPE_SYSTEM:
539 case CONNMAN_SERVICE_TYPE_GPS:
540 case CONNMAN_SERVICE_TYPE_VPN:
541 case CONNMAN_SERVICE_TYPE_GADGET:
543 case CONNMAN_SERVICE_TYPE_ETHERNET:
544 case CONNMAN_SERVICE_TYPE_WIFI:
545 case CONNMAN_SERVICE_TYPE_WIMAX:
546 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
547 case CONNMAN_SERVICE_TYPE_CELLULAR:
554 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
556 DBG("type %d", type);
558 if (technology_supported(type) == FALSE)
561 __sync_synchronize();
562 if (registered[type] > 0)
568 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
570 DBG("type %d", type);
572 if (technology_supported(type) == FALSE)
575 __sync_synchronize();
576 if (enabled[type] > 0)
582 int __connman_notifier_init(void)
586 connection = connman_dbus_get_connection();
588 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
595 void __connman_notifier_cleanup(void)
599 g_hash_table_destroy(service_hash);
602 dbus_connection_unref(connection);