*
* Connection Manager
*
- * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
static DBusConnection *connection = NULL;
static GSList *notifier_list = NULL;
+static GHashTable *service_hash = NULL;
+
+static const char *notifier_state;
static gint compare_priority(gconstpointer a, gconstpointer b)
{
notifier_list = g_slist_remove(notifier_list, notifier);
}
-static void device_enabled(enum connman_device_type type,
- connman_bool_t enabled)
+#define MAX_TECHNOLOGIES 10
+
+static int connected[MAX_TECHNOLOGIES];
+static int online[MAX_TECHNOLOGIES];
+
+static connman_bool_t notifier_is_online(void)
{
- GSList *list;
- DBusMessage *signal;
- DBusMessageIter entry, value, iter;
- const char *key = "EnabledTechnologies";
+ unsigned int i;
- DBG("type %d enabled %d", type, enabled);
+ __sync_synchronize();
+ for (i = 0; i < MAX_TECHNOLOGIES; i++) {
+ if (online[i] > 0)
+ return TRUE;
+ }
- signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
- CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
- if (signal == NULL)
- goto done;
+ return FALSE;
+}
- dbus_message_iter_init_append(signal, &entry);
+connman_bool_t __connman_notifier_is_connected(void)
+{
+ unsigned int i;
- dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
+ __sync_synchronize();
+ for (i = 0; i < MAX_TECHNOLOGIES; i++) {
+ if (connected[i] > 0)
+ return TRUE;
+ }
- dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
- DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
- &value);
+ return FALSE;
+}
- dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
- DBUS_TYPE_STRING_AS_STRING, &iter);
- __connman_notifier_device_type_list(TRUE, &iter);
- dbus_message_iter_close_container(&value, &iter);
+static const char *evaluate_notifier_state(void)
+{
+ if (notifier_is_online() == TRUE)
+ return "online";
- dbus_message_iter_close_container(&entry, &value);
+ if (__connman_notifier_is_connected() == TRUE)
+ return "ready";
- g_dbus_send_message(connection, signal);
+ if ( __connman_technology_get_offlinemode() == TRUE)
+ return "offline";
-done:
- for (list = notifier_list; list; list = list->next) {
- struct connman_notifier *notifier = list->data;
+ return "idle";
+}
- if (notifier->device_enabled)
- notifier->device_enabled(type, enabled);
- }
+const char *__connman_notifier_get_state(void)
+{
+ return notifier_state;
}
-static void device_registered(enum connman_device_type type,
- connman_bool_t registered)
+static void state_changed(void)
{
- DBusMessage *signal;
- DBusMessageIter entry, value, iter;
- const char *key = "Technologies";
+ const char *state;
- DBG("type %d registered %d", type, registered);
+ state = evaluate_notifier_state();
- signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
- CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
- if (signal == NULL)
+ if (g_strcmp0(state, notifier_state) == 0)
return;
- dbus_message_iter_init_append(signal, &entry);
+ notifier_state = state;
- dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
-
- dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
- DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
- &value);
-
- dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
- DBUS_TYPE_STRING_AS_STRING, &iter);
- __connman_notifier_device_type_list(FALSE, &iter);
- dbus_message_iter_close_container(&value, &iter);
+ connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+ CONNMAN_MANAGER_INTERFACE, "State",
+ DBUS_TYPE_STRING, ¬ifier_state);
+}
- dbus_message_iter_close_container(&entry, &value);
+static void technology_connected(enum connman_service_type type,
+ connman_bool_t connected)
+{
+ DBG("type %d connected %d", type, connected);
- g_dbus_send_message(connection, signal);
+ __connman_technology_set_connected(type, connected);
+ state_changed();
}
-static const char *type2string(enum connman_device_type type)
+void __connman_notifier_connect(enum connman_service_type type)
{
+ DBG("type %d", type);
+
switch (type) {
- case CONNMAN_DEVICE_TYPE_UNKNOWN:
- case CONNMAN_DEVICE_TYPE_MBM:
- case CONNMAN_DEVICE_TYPE_HSO:
- case CONNMAN_DEVICE_TYPE_NOZOMI:
- case CONNMAN_DEVICE_TYPE_HUAWEI:
- case CONNMAN_DEVICE_TYPE_NOVATEL:
- case CONNMAN_DEVICE_TYPE_VENDOR:
+ case CONNMAN_SERVICE_TYPE_UNKNOWN:
+ case CONNMAN_SERVICE_TYPE_SYSTEM:
+ case CONNMAN_SERVICE_TYPE_GPS:
+ case CONNMAN_SERVICE_TYPE_VPN:
+ case CONNMAN_SERVICE_TYPE_GADGET:
+ return;
+ case CONNMAN_SERVICE_TYPE_ETHERNET:
+ case CONNMAN_SERVICE_TYPE_WIFI:
+ case CONNMAN_SERVICE_TYPE_WIMAX:
+ case CONNMAN_SERVICE_TYPE_BLUETOOTH:
+ case CONNMAN_SERVICE_TYPE_CELLULAR:
break;
- case CONNMAN_DEVICE_TYPE_ETHERNET:
- return "ethernet";
- case CONNMAN_DEVICE_TYPE_WIFI:
- return "wifi";
- case CONNMAN_DEVICE_TYPE_WIMAX:
- return "wimax";
- case CONNMAN_DEVICE_TYPE_BLUETOOTH:
- return "bluetooth";
- case CONNMAN_DEVICE_TYPE_GPS:
- return "gps";
}
- return NULL;
+ if (__sync_fetch_and_add(&connected[type], 1) == 0)
+ technology_connected(type, TRUE);
}
-static volatile gint registered[20];
-static volatile gint enabled[20];
-
-void __connman_notifier_device_type_list(gboolean powered,
- DBusMessageIter *iter)
+void __connman_notifier_enter_online(enum connman_service_type type)
{
- int i;
-
- for (i = 0; i < 10; i++) {
- const char *type = type2string(i);
- gint count;
+ DBG("type %d", type);
- if (type == NULL)
- continue;
+ if (__sync_fetch_and_add(&online[type], 1) == 0)
+ state_changed();
+}
- if (powered == TRUE)
- count = g_atomic_int_get(&enabled[i]);
- else
- count = g_atomic_int_get(®istered[i]);
+void __connman_notifier_leave_online(enum connman_service_type type)
+{
+ DBG("type %d", type);
- if (count > 0)
- dbus_message_iter_append_basic(iter,
- DBUS_TYPE_STRING, &type);
- }
+ if (__sync_fetch_and_sub(&online[type], 1) == 1)
+ state_changed();
}
-void __connman_notifier_device_type_register(enum connman_device_type type)
+void __connman_notifier_disconnect(enum connman_service_type type)
{
DBG("type %d", type);
+ __sync_synchronize();
+ if (connected[type] == 0) {
+ connman_error("notifier disconnect underflow");
+ return;
+ }
+
switch (type) {
- case CONNMAN_DEVICE_TYPE_UNKNOWN:
- case CONNMAN_DEVICE_TYPE_MBM:
- case CONNMAN_DEVICE_TYPE_HSO:
- case CONNMAN_DEVICE_TYPE_NOZOMI:
- case CONNMAN_DEVICE_TYPE_HUAWEI:
- case CONNMAN_DEVICE_TYPE_NOVATEL:
- case CONNMAN_DEVICE_TYPE_VENDOR:
+ case CONNMAN_SERVICE_TYPE_UNKNOWN:
+ case CONNMAN_SERVICE_TYPE_SYSTEM:
+ case CONNMAN_SERVICE_TYPE_GPS:
+ case CONNMAN_SERVICE_TYPE_VPN:
+ case CONNMAN_SERVICE_TYPE_GADGET:
return;
- case CONNMAN_DEVICE_TYPE_ETHERNET:
- case CONNMAN_DEVICE_TYPE_WIFI:
- case CONNMAN_DEVICE_TYPE_WIMAX:
- case CONNMAN_DEVICE_TYPE_BLUETOOTH:
- case CONNMAN_DEVICE_TYPE_GPS:
- if (g_atomic_int_exchange_and_add(®istered[type], 1) == 0)
- device_registered(type, TRUE);
+ case CONNMAN_SERVICE_TYPE_ETHERNET:
+ case CONNMAN_SERVICE_TYPE_WIFI:
+ case CONNMAN_SERVICE_TYPE_WIMAX:
+ case CONNMAN_SERVICE_TYPE_BLUETOOTH:
+ case CONNMAN_SERVICE_TYPE_CELLULAR:
break;
}
+
+ if (__sync_fetch_and_sub(&connected[type], 1) != 1)
+ return;
+
+ technology_connected(type, FALSE);
}
-void __connman_notifier_device_type_unregister(enum connman_device_type type)
+void __connman_notifier_default_changed(struct connman_service *service)
{
- DBG("type %d", type);
+ GSList *list;
- switch (type) {
- case CONNMAN_DEVICE_TYPE_UNKNOWN:
- case CONNMAN_DEVICE_TYPE_MBM:
- case CONNMAN_DEVICE_TYPE_HSO:
- case CONNMAN_DEVICE_TYPE_NOZOMI:
- case CONNMAN_DEVICE_TYPE_HUAWEI:
- case CONNMAN_DEVICE_TYPE_NOVATEL:
- case CONNMAN_DEVICE_TYPE_VENDOR:
- return;
- case CONNMAN_DEVICE_TYPE_ETHERNET:
- case CONNMAN_DEVICE_TYPE_WIFI:
- case CONNMAN_DEVICE_TYPE_WIMAX:
- case CONNMAN_DEVICE_TYPE_BLUETOOTH:
- case CONNMAN_DEVICE_TYPE_GPS:
- if (g_atomic_int_dec_and_test(®istered[type]) == TRUE)
- device_registered(type, FALSE);
- break;
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->default_changed)
+ notifier->default_changed(service);
}
}
-void __connman_notifier_device_type_increase(enum connman_device_type type)
+void __connman_notifier_service_add(struct connman_service *service,
+ const char *name)
{
- DBG("type %d", type);
+ GSList *list;
- switch (type) {
- case CONNMAN_DEVICE_TYPE_UNKNOWN:
- case CONNMAN_DEVICE_TYPE_MBM:
- case CONNMAN_DEVICE_TYPE_HSO:
- case CONNMAN_DEVICE_TYPE_NOZOMI:
- case CONNMAN_DEVICE_TYPE_HUAWEI:
- case CONNMAN_DEVICE_TYPE_NOVATEL:
- case CONNMAN_DEVICE_TYPE_VENDOR:
- return;
- case CONNMAN_DEVICE_TYPE_ETHERNET:
- case CONNMAN_DEVICE_TYPE_WIFI:
- case CONNMAN_DEVICE_TYPE_WIMAX:
- case CONNMAN_DEVICE_TYPE_BLUETOOTH:
- case CONNMAN_DEVICE_TYPE_GPS:
- if (g_atomic_int_exchange_and_add(&enabled[type], 1) == 0)
- device_enabled(type, TRUE);
- break;
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->service_add)
+ notifier->service_add(service, name);
}
}
-void __connman_notifier_device_type_decrease(enum connman_device_type type)
+void __connman_notifier_service_remove(struct connman_service *service)
{
- DBG("type %d", type);
+ GSList *list;
- switch (type) {
- case CONNMAN_DEVICE_TYPE_UNKNOWN:
- case CONNMAN_DEVICE_TYPE_MBM:
- case CONNMAN_DEVICE_TYPE_HSO:
- case CONNMAN_DEVICE_TYPE_NOZOMI:
- case CONNMAN_DEVICE_TYPE_HUAWEI:
- case CONNMAN_DEVICE_TYPE_NOVATEL:
- case CONNMAN_DEVICE_TYPE_VENDOR:
- return;
- case CONNMAN_DEVICE_TYPE_ETHERNET:
- case CONNMAN_DEVICE_TYPE_WIFI:
- case CONNMAN_DEVICE_TYPE_WIMAX:
- case CONNMAN_DEVICE_TYPE_BLUETOOTH:
- case CONNMAN_DEVICE_TYPE_GPS:
- if (g_atomic_int_dec_and_test(&enabled[type]) == TRUE)
- device_enabled(type, FALSE);
- break;
+ if (g_hash_table_lookup(service_hash, service) != NULL) {
+ /*
+ * This is a tempory check for consistency. It can be
+ * removed when there are no reports for the following
+ * error message.
+ */
+ connman_error("Service state machine inconsistency detected.");
+
+ g_hash_table_remove(service_hash, service);
+ }
+
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->service_remove)
+ notifier->service_remove(service);
}
}
-void __connman_notifier_offline_mode(connman_bool_t enabled)
+void __connman_notifier_proxy_changed(struct connman_service *service)
{
GSList *list;
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->proxy_changed)
+ notifier->proxy_changed(service);
+ }
+}
+
+static void offlinemode_changed(dbus_bool_t enabled)
+{
DBG("enabled %d", enabled);
- __connman_profile_changed(FALSE);
+ connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+ CONNMAN_MANAGER_INTERFACE, "OfflineMode",
+ DBUS_TYPE_BOOLEAN, &enabled);
+}
+
+void __connman_notifier_offlinemode(connman_bool_t enabled)
+{
+ GSList *list;
+
+ DBG("enabled %d", enabled);
+
+ offlinemode_changed(enabled);
+ state_changed();
for (list = notifier_list; list; list = list->next) {
struct connman_notifier *notifier = list->data;
}
}
+static void notify_idle_state(connman_bool_t idle)
+{
+ GSList *list;
+
+ DBG("idle %d", idle);
+
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->idle_state)
+ notifier->idle_state(idle);
+ }
+}
+
+void __connman_notifier_service_state_changed(struct connman_service *service,
+ enum connman_service_state state)
+{
+ GSList *list;
+ unsigned int old_size;
+ connman_bool_t found;
+
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->service_state_changed)
+ notifier->service_state_changed(service, state);
+ }
+
+ old_size = g_hash_table_size(service_hash);
+ found = g_hash_table_lookup(service_hash, service) != NULL;
+
+ switch (state) {
+ case CONNMAN_SERVICE_STATE_UNKNOWN:
+ case CONNMAN_SERVICE_STATE_FAILURE:
+ case CONNMAN_SERVICE_STATE_DISCONNECT:
+ case CONNMAN_SERVICE_STATE_IDLE:
+ if (found == FALSE)
+ break;
+
+ g_hash_table_remove(service_hash, service);
+ if (old_size == 1)
+ notify_idle_state(TRUE);
+
+ break;
+ case CONNMAN_SERVICE_STATE_ASSOCIATION:
+ case CONNMAN_SERVICE_STATE_CONFIGURATION:
+ case CONNMAN_SERVICE_STATE_READY:
+ case CONNMAN_SERVICE_STATE_ONLINE:
+ if (found == TRUE)
+ break;
+
+ g_hash_table_insert(service_hash, service, service);
+ if (old_size == 0)
+ notify_idle_state(FALSE);
+
+ break;
+ }
+}
+
+void __connman_notifier_ipconfig_changed(struct connman_service *service,
+ struct connman_ipconfig *ipconfig)
+{
+ GSList *list;
+
+ for (list = notifier_list; list; list = list->next) {
+ struct connman_notifier *notifier = list->data;
+
+ if (notifier->ipconfig_changed)
+ notifier->ipconfig_changed(service, ipconfig);
+ }
+}
+
int __connman_notifier_init(void)
{
DBG("");
connection = connman_dbus_get_connection();
+ service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, NULL);
+
+ notifier_state = evaluate_notifier_state();
+
return 0;
}
{
DBG("");
+ g_hash_table_destroy(service_hash);
+ service_hash = NULL;
+
dbus_connection_unref(connection);
}