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_connected(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 static void technology_registered(enum connman_service_type type,
98 connman_bool_t registered)
100 DBG("type %d registered %d", type, registered);
103 static void technology_enabled(enum connman_service_type type,
104 connman_bool_t enabled)
108 DBG("type %d enabled %d", type, enabled);
110 for (list = notifier_list; list; list = list->next) {
111 struct connman_notifier *notifier = list->data;
113 if (notifier->service_enabled)
114 notifier->service_enabled(type, enabled);
118 unsigned int __connman_notifier_count_connected(void)
120 unsigned int i, count = 0;
122 __sync_synchronize();
123 for (i = 0; i < MAX_TECHNOLOGIES; i++) {
124 if (connected[i] > 0)
131 const char *__connman_notifier_get_state(void)
133 unsigned int count = __connman_notifier_count_connected();
141 static void state_changed(connman_bool_t connected)
143 unsigned int count = __connman_notifier_count_connected();
144 char *state = "offline";
150 if (connected == FALSE)
156 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
157 CONNMAN_MANAGER_INTERFACE, "State",
158 DBUS_TYPE_STRING, &state);
161 static void technology_connected(enum connman_service_type type,
162 connman_bool_t connected)
164 DBG("type %d connected %d", type, connected);
166 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
167 CONNMAN_MANAGER_INTERFACE, "ConnectedTechnologies",
168 DBUS_TYPE_STRING, __connman_notifier_list_connected, NULL);
170 state_changed(connected);
173 void __connman_notifier_register(enum connman_service_type type)
175 DBG("type %d", type);
178 case CONNMAN_SERVICE_TYPE_UNKNOWN:
179 case CONNMAN_SERVICE_TYPE_SYSTEM:
180 case CONNMAN_SERVICE_TYPE_GPS:
181 case CONNMAN_SERVICE_TYPE_VPN:
182 case CONNMAN_SERVICE_TYPE_GADGET:
184 case CONNMAN_SERVICE_TYPE_ETHERNET:
185 case CONNMAN_SERVICE_TYPE_WIFI:
186 case CONNMAN_SERVICE_TYPE_WIMAX:
187 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
188 case CONNMAN_SERVICE_TYPE_CELLULAR:
192 if (__sync_fetch_and_add(®istered[type], 1) == 0)
193 technology_registered(type, TRUE);
196 void __connman_notifier_unregister(enum connman_service_type type)
198 DBG("type %d", type);
200 __sync_synchronize();
201 if (registered[type] == 0) {
202 connman_error("notifier unregister underflow");
207 case CONNMAN_SERVICE_TYPE_UNKNOWN:
208 case CONNMAN_SERVICE_TYPE_SYSTEM:
209 case CONNMAN_SERVICE_TYPE_GPS:
210 case CONNMAN_SERVICE_TYPE_VPN:
211 case CONNMAN_SERVICE_TYPE_GADGET:
213 case CONNMAN_SERVICE_TYPE_ETHERNET:
214 case CONNMAN_SERVICE_TYPE_WIFI:
215 case CONNMAN_SERVICE_TYPE_WIMAX:
216 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
217 case CONNMAN_SERVICE_TYPE_CELLULAR:
221 if (__sync_fetch_and_sub(®istered[type], 1) != 1)
224 technology_registered(type, FALSE);
227 void __connman_notifier_enable(enum connman_service_type type)
229 DBG("type %d", type);
232 case CONNMAN_SERVICE_TYPE_UNKNOWN:
233 case CONNMAN_SERVICE_TYPE_SYSTEM:
234 case CONNMAN_SERVICE_TYPE_GPS:
235 case CONNMAN_SERVICE_TYPE_VPN:
236 case CONNMAN_SERVICE_TYPE_GADGET:
238 case CONNMAN_SERVICE_TYPE_ETHERNET:
239 case CONNMAN_SERVICE_TYPE_WIFI:
240 case CONNMAN_SERVICE_TYPE_WIMAX:
241 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
242 case CONNMAN_SERVICE_TYPE_CELLULAR:
246 if (__sync_fetch_and_add(&enabled[type], 1) == 0)
247 technology_enabled(type, TRUE);
250 void __connman_notifier_disable(enum connman_service_type type)
252 DBG("type %d", type);
254 __sync_synchronize();
255 if (enabled[type] == 0) {
256 connman_error("notifier disable underflow");
261 case CONNMAN_SERVICE_TYPE_UNKNOWN:
262 case CONNMAN_SERVICE_TYPE_SYSTEM:
263 case CONNMAN_SERVICE_TYPE_GPS:
264 case CONNMAN_SERVICE_TYPE_VPN:
265 case CONNMAN_SERVICE_TYPE_GADGET:
267 case CONNMAN_SERVICE_TYPE_ETHERNET:
268 case CONNMAN_SERVICE_TYPE_WIFI:
269 case CONNMAN_SERVICE_TYPE_WIMAX:
270 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
271 case CONNMAN_SERVICE_TYPE_CELLULAR:
275 if (__sync_fetch_and_sub(&enabled[type], 1) != 1)
278 technology_enabled(type, FALSE);
281 void __connman_notifier_connect(enum connman_service_type type)
283 DBG("type %d", type);
286 case CONNMAN_SERVICE_TYPE_UNKNOWN:
287 case CONNMAN_SERVICE_TYPE_SYSTEM:
288 case CONNMAN_SERVICE_TYPE_GPS:
289 case CONNMAN_SERVICE_TYPE_VPN:
290 case CONNMAN_SERVICE_TYPE_GADGET:
292 case CONNMAN_SERVICE_TYPE_ETHERNET:
293 case CONNMAN_SERVICE_TYPE_WIFI:
294 case CONNMAN_SERVICE_TYPE_WIMAX:
295 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
296 case CONNMAN_SERVICE_TYPE_CELLULAR:
300 if (__sync_fetch_and_add(&connected[type], 1) == 0)
301 technology_connected(type, TRUE);
304 void __connman_notifier_disconnect(enum connman_service_type type)
306 DBG("type %d", type);
308 __sync_synchronize();
309 if (connected[type] == 0) {
310 connman_error("notifier disconnect underflow");
315 case CONNMAN_SERVICE_TYPE_UNKNOWN:
316 case CONNMAN_SERVICE_TYPE_SYSTEM:
317 case CONNMAN_SERVICE_TYPE_GPS:
318 case CONNMAN_SERVICE_TYPE_VPN:
319 case CONNMAN_SERVICE_TYPE_GADGET:
321 case CONNMAN_SERVICE_TYPE_ETHERNET:
322 case CONNMAN_SERVICE_TYPE_WIFI:
323 case CONNMAN_SERVICE_TYPE_WIMAX:
324 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
325 case CONNMAN_SERVICE_TYPE_CELLULAR:
329 if (__sync_fetch_and_sub(&connected[type], 1) != 1)
332 technology_connected(type, FALSE);
335 static void technology_default(enum connman_service_type type)
339 str = __connman_service_type2string(type);
343 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
344 CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
345 DBUS_TYPE_STRING, &str);
348 void __connman_notifier_default_changed(struct connman_service *service)
350 enum connman_service_type type = connman_service_get_type(service);
354 technology_default(type);
356 interface = connman_service_get_interface(service);
357 __connman_tethering_update_interface(interface);
360 for (list = notifier_list; list; list = list->next) {
361 struct connman_notifier *notifier = list->data;
363 if (notifier->default_changed)
364 notifier->default_changed(service);
368 void __connman_notifier_service_add(struct connman_service *service,
373 for (list = notifier_list; list; list = list->next) {
374 struct connman_notifier *notifier = list->data;
376 if (notifier->service_add)
377 notifier->service_add(service, name);
381 void __connman_notifier_service_remove(struct connman_service *service)
385 if (g_hash_table_lookup(service_hash, service) != NULL) {
387 * This is a tempory check for consistency. It can be
388 * removed when there are no reports for the following
391 connman_error("Service state machine inconsistency detected.");
393 g_hash_table_remove(service_hash, service);
396 for (list = notifier_list; list; list = list->next) {
397 struct connman_notifier *notifier = list->data;
399 if (notifier->service_remove)
400 notifier->service_remove(service);
404 void __connman_notifier_proxy_changed(struct connman_service *service)
408 for (list = notifier_list; list; list = list->next) {
409 struct connman_notifier *notifier = list->data;
411 if (notifier->proxy_changed)
412 notifier->proxy_changed(service);
416 static void offlinemode_changed(dbus_bool_t enabled)
418 DBG("enabled %d", enabled);
420 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
421 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
422 DBUS_TYPE_BOOLEAN, &enabled);
425 void __connman_notifier_offlinemode(connman_bool_t enabled)
429 DBG("enabled %d", enabled);
431 offlinemode_changed(enabled);
433 for (list = notifier_list; list; list = list->next) {
434 struct connman_notifier *notifier = list->data;
436 if (notifier->offline_mode)
437 notifier->offline_mode(enabled);
441 static void notify_idle_state(connman_bool_t idle)
445 DBG("idle %d", idle);
447 for (list = notifier_list; list; list = list->next) {
448 struct connman_notifier *notifier = list->data;
450 if (notifier->idle_state)
451 notifier->idle_state(idle);
455 void __connman_notifier_service_state_changed(struct connman_service *service,
456 enum connman_service_state state)
459 unsigned int old_size;
460 connman_bool_t found;
462 for (list = notifier_list; list; list = list->next) {
463 struct connman_notifier *notifier = list->data;
465 if (notifier->service_state_changed)
466 notifier->service_state_changed(service, state);
469 old_size = g_hash_table_size(service_hash);
470 found = g_hash_table_lookup(service_hash, service) != NULL;
473 case CONNMAN_SERVICE_STATE_UNKNOWN:
474 case CONNMAN_SERVICE_STATE_FAILURE:
475 case CONNMAN_SERVICE_STATE_DISCONNECT:
476 case CONNMAN_SERVICE_STATE_IDLE:
480 g_hash_table_remove(service_hash, service);
482 notify_idle_state(TRUE);
485 case CONNMAN_SERVICE_STATE_ASSOCIATION:
486 case CONNMAN_SERVICE_STATE_CONFIGURATION:
487 case CONNMAN_SERVICE_STATE_READY:
488 case CONNMAN_SERVICE_STATE_ONLINE:
492 g_hash_table_insert(service_hash, service, service);
494 notify_idle_state(FALSE);
500 void __connman_notifier_ipconfig_changed(struct connman_service *service,
501 struct connman_ipconfig *ipconfig)
505 for (list = notifier_list; list; list = list->next) {
506 struct connman_notifier *notifier = list->data;
508 if (notifier->ipconfig_changed)
509 notifier->ipconfig_changed(service, ipconfig);
513 static connman_bool_t technology_supported(enum connman_service_type type)
516 case CONNMAN_SERVICE_TYPE_UNKNOWN:
517 case CONNMAN_SERVICE_TYPE_SYSTEM:
518 case CONNMAN_SERVICE_TYPE_GPS:
519 case CONNMAN_SERVICE_TYPE_VPN:
520 case CONNMAN_SERVICE_TYPE_GADGET:
522 case CONNMAN_SERVICE_TYPE_ETHERNET:
523 case CONNMAN_SERVICE_TYPE_WIFI:
524 case CONNMAN_SERVICE_TYPE_WIMAX:
525 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
526 case CONNMAN_SERVICE_TYPE_CELLULAR:
533 connman_bool_t __connman_notifier_is_registered(enum connman_service_type type)
535 DBG("type %d", type);
537 if (technology_supported(type) == FALSE)
540 __sync_synchronize();
541 if (registered[type] > 0)
547 connman_bool_t __connman_notifier_is_enabled(enum connman_service_type type)
549 DBG("type %d", type);
551 if (technology_supported(type) == FALSE)
554 __sync_synchronize();
555 if (enabled[type] > 0)
561 int __connman_notifier_init(void)
565 connection = connman_dbus_get_connection();
567 service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
574 void __connman_notifier_cleanup(void)
578 g_hash_table_destroy(service_hash);
581 dbus_connection_unref(connection);