service: Load/store AutoConnect and Favorite properties for ethernet
[framework/connectivity/connman.git] / src / service.c
index f199e59..becbe53 100644 (file)
@@ -79,7 +79,6 @@ struct connman_service {
        char *passphrase;
        char *agent_passphrase;
        connman_bool_t roaming;
-       connman_bool_t network_created;
        struct connman_ipconfig *ipconfig_ipv4;
        struct connman_ipconfig *ipconfig_ipv6;
        struct connman_network *network;
@@ -110,28 +109,9 @@ struct connman_service {
        char **excludes;
        char *pac;
        connman_bool_t wps;
+       int online_check_count;
 };
 
-static void append_path(gpointer value, gpointer user_data)
-{
-       struct connman_service *service = value;
-       DBusMessageIter *iter = user_data;
-
-       if (service->path == NULL)
-               return;
-
-       dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
-                                                       &service->path);
-}
-
-void __connman_service_list(DBusMessageIter *iter, void *user_data)
-{
-       if (service_list == NULL)
-               return;
-
-       g_sequence_foreach(service_list, append_path, iter);
-}
-
 struct find_data {
        const char *path;
        struct connman_service *service;
@@ -198,13 +178,11 @@ static const char *security2string(enum connman_service_security security)
        case CONNMAN_SERVICE_SECURITY_WEP:
                return "wep";
        case CONNMAN_SERVICE_SECURITY_PSK:
+       case CONNMAN_SERVICE_SECURITY_WPA:
+       case CONNMAN_SERVICE_SECURITY_RSN:
                return "psk";
        case CONNMAN_SERVICE_SECURITY_8021X:
                return "ieee8021x";
-       case CONNMAN_SERVICE_SECURITY_WPA:
-               return "wpa";
-       case CONNMAN_SERVICE_SECURITY_RSN:
-               return "rsn";
        }
 
        return NULL;
@@ -317,7 +295,6 @@ static int service_load(struct connman_service *service)
        switch (service->type) {
        case CONNMAN_SERVICE_TYPE_UNKNOWN:
        case CONNMAN_SERVICE_TYPE_SYSTEM:
-       case CONNMAN_SERVICE_TYPE_ETHERNET:
        case CONNMAN_SERVICE_TYPE_GPS:
        case CONNMAN_SERVICE_TYPE_VPN:
        case CONNMAN_SERVICE_TYPE_GADGET:
@@ -378,12 +355,6 @@ static int service_load(struct connman_service *service)
                service->favorite = g_key_file_get_boolean(keyfile,
                                service->identifier, "Favorite", NULL);
 
-               autoconnect = g_key_file_get_boolean(keyfile,
-                               service->identifier, "AutoConnect", &error);
-               if (error == NULL)
-                       service->autoconnect = autoconnect;
-               g_clear_error(&error);
-
                str = g_key_file_get_string(keyfile,
                                service->identifier, "Failure", NULL);
                if (str != NULL) {
@@ -393,6 +364,14 @@ static int service_load(struct connman_service *service)
                        service->error = string2error(str);
                        g_free(str);
                }
+               /* fall through */
+
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+               autoconnect = g_key_file_get_boolean(keyfile,
+                               service->identifier, "AutoConnect", &error);
+               if (error == NULL)
+                       service->autoconnect = autoconnect;
+               g_clear_error(&error);
                break;
        }
 
@@ -487,7 +466,6 @@ static int service_save(struct connman_service *service)
        switch (service->type) {
        case CONNMAN_SERVICE_TYPE_UNKNOWN:
        case CONNMAN_SERVICE_TYPE_SYSTEM:
-       case CONNMAN_SERVICE_TYPE_ETHERNET:
        case CONNMAN_SERVICE_TYPE_GPS:
        case CONNMAN_SERVICE_TYPE_VPN:
        case CONNMAN_SERVICE_TYPE_GADGET:
@@ -533,10 +511,6 @@ static int service_save(struct connman_service *service)
                g_key_file_set_boolean(keyfile, service->identifier,
                                        "Favorite", service->favorite);
 
-               if (service->favorite == TRUE)
-                       g_key_file_set_boolean(keyfile, service->identifier,
-                                       "AutoConnect", service->autoconnect);
-
                if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
                        service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
                        const char *failure = error2string(service->error);
@@ -548,6 +522,12 @@ static int service_save(struct connman_service *service)
                        g_key_file_remove_key(keyfile, service->identifier,
                                                        "Failure", NULL);
                }
+               /* fall through */
+
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+               if (service->favorite == TRUE)
+                       g_key_file_set_boolean(keyfile, service->identifier,
+                                       "AutoConnect", service->autoconnect);
                break;
        }
 
@@ -633,43 +613,6 @@ done:
        return err;
 }
 
-static guint changed_timeout = 0;
-
-static gboolean notify_services_changed(gpointer user_data)
-{
-       changed_timeout = 0;
-
-       connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
-                               CONNMAN_MANAGER_INTERFACE, "Services",
-                               DBUS_TYPE_OBJECT_PATH, __connman_service_list,
-                               NULL);
-
-       return FALSE;
-}
-
-static void services_changed(gboolean delayed)
-{
-       DBG("");
-
-       if (changed_timeout > 0) {
-               g_source_remove(changed_timeout);
-               changed_timeout = 0;
-       }
-
-       if (__connman_connection_update_gateway() == TRUE) {
-               notify_services_changed(NULL);
-               return;
-       }
-
-       if (delayed == FALSE) {
-               notify_services_changed(NULL);
-               return;
-       }
-
-       changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
-                                                                NULL);
-}
-
 static enum connman_service_state combine_state(
                                        enum connman_service_state state_a,
                                        enum connman_service_state state_b)
@@ -812,9 +755,9 @@ static void update_nameservers(struct connman_service *service)
        const char *ifname;
 
        if (service->ipconfig_ipv4)
-               ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
+               ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
        else if (service->ipconfig_ipv6)
-               ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
+               ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
        else
                ifname = NULL;
 
@@ -1025,7 +968,8 @@ static void nameserver_add_routes(int index, char **nameservers,
        }
 }
 
-static void nameserver_del_routes(int index, char **nameservers)
+static void nameserver_del_routes(int index, char **nameservers,
+                               enum connman_ipconfig_type type)
 {
        int i, ret, family;
        struct addrinfo hints;
@@ -1044,11 +988,18 @@ static void nameserver_del_routes(int index, char **nameservers)
                else
                        family = addr->ai_family;
 
-               if (family == AF_INET)
-                       connman_inet_del_host_route(index, nameservers[i]);
-               else if (family == AF_INET6)
-                       connman_inet_del_ipv6_host_route(index,
+               switch (family) {
+               case AF_INET:
+                       if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
+                               connman_inet_del_host_route(index,
+                                                       nameservers[i]);
+                       break;
+               case AF_INET6:
+                       if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
+                               connman_inet_del_ipv6_host_route(index,
                                                        nameservers[i]);
+                       break;
+               }
 
                freeaddrinfo(addr);
        }
@@ -1085,7 +1036,8 @@ void __connman_service_nameserver_add_routes(struct connman_service *service,
        }
 }
 
-void __connman_service_nameserver_del_routes(struct connman_service *service)
+void __connman_service_nameserver_del_routes(struct connman_service *service,
+                                       enum connman_ipconfig_type type)
 {
        int index = -1;
 
@@ -1098,9 +1050,10 @@ void __connman_service_nameserver_del_routes(struct connman_service *service)
                index = connman_provider_get_index(service->provider);
 
        if (service->nameservers_config != NULL)
-               nameserver_del_routes(index, service->nameservers_config);
+               nameserver_del_routes(index, service->nameservers_config,
+                                       type);
        else if (service->nameservers != NULL)
-               nameserver_del_routes(index, service->nameservers);
+               nameserver_del_routes(index, service->nameservers, type);
 }
 
 static struct connman_stats *stats_get(struct connman_service *service)
@@ -1216,17 +1169,6 @@ static void default_changed(void)
        __connman_notifier_default_changed(service);
 }
 
-const char *__connman_service_default(void)
-{
-       struct connman_service *service;
-
-       service = get_default();
-       if (service == NULL)
-               return "";
-
-       return __connman_service_type2string(service->type);
-}
-
 static void state_changed(struct connman_service *service)
 {
        const char *str;
@@ -1292,46 +1234,6 @@ static void autoconnect_changed(struct connman_service *service)
                                DBUS_TYPE_BOOLEAN, &service->autoconnect);
 }
 
-static void passphrase_changed(struct connman_service *service)
-{
-       dbus_bool_t required;
-
-       switch (service->type) {
-       case CONNMAN_SERVICE_TYPE_UNKNOWN:
-       case CONNMAN_SERVICE_TYPE_SYSTEM:
-       case CONNMAN_SERVICE_TYPE_ETHERNET:
-       case CONNMAN_SERVICE_TYPE_WIMAX:
-       case CONNMAN_SERVICE_TYPE_BLUETOOTH:
-       case CONNMAN_SERVICE_TYPE_CELLULAR:
-       case CONNMAN_SERVICE_TYPE_GPS:
-       case CONNMAN_SERVICE_TYPE_VPN:
-       case CONNMAN_SERVICE_TYPE_GADGET:
-               return;
-       case CONNMAN_SERVICE_TYPE_WIFI:
-               required = FALSE;
-
-               switch (service->security) {
-               case CONNMAN_SERVICE_SECURITY_UNKNOWN:
-               case CONNMAN_SERVICE_SECURITY_NONE:
-                       break;
-               case CONNMAN_SERVICE_SECURITY_WEP:
-               case CONNMAN_SERVICE_SECURITY_PSK:
-               case CONNMAN_SERVICE_SECURITY_WPA:
-               case CONNMAN_SERVICE_SECURITY_RSN:
-                       if (service->passphrase == NULL)
-                               required = TRUE;
-                       break;
-               case CONNMAN_SERVICE_SECURITY_8021X:
-                       break;
-               }
-               break;
-       }
-
-       connman_dbus_property_changed_basic(service->path,
-                               CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
-                                               DBUS_TYPE_BOOLEAN, &required);
-}
-
 static void append_security(DBusMessageIter *iter, void *user_data)
 {
        struct connman_service *service = user_data;
@@ -2011,7 +1913,6 @@ connman_bool_t __connman_service_session_dec(struct connman_service *service)
 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
                                        struct connman_service *service)
 {
-       dbus_bool_t required;
        const char *str;
 
        str = __connman_service_type2string(service->type);
@@ -2068,31 +1969,6 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
                                                append_ethernet, service);
                break;
        case CONNMAN_SERVICE_TYPE_WIFI:
-               if (service->passphrase != NULL && limited == FALSE)
-                       connman_dbus_dict_append_basic(dict, "Passphrase",
-                               DBUS_TYPE_STRING, &service->passphrase);
-
-               required = FALSE;
-
-               switch (service->security) {
-               case CONNMAN_SERVICE_SECURITY_UNKNOWN:
-               case CONNMAN_SERVICE_SECURITY_NONE:
-                       break;
-               case CONNMAN_SERVICE_SECURITY_WEP:
-               case CONNMAN_SERVICE_SECURITY_PSK:
-               case CONNMAN_SERVICE_SECURITY_WPA:
-               case CONNMAN_SERVICE_SECURITY_RSN:
-                       if (service->passphrase == NULL)
-                               required = TRUE;
-                       break;
-               case CONNMAN_SERVICE_SECURITY_8021X:
-                       break;
-               }
-
-               connman_dbus_dict_append_basic(dict, "PassphraseRequired",
-                                               DBUS_TYPE_BOOLEAN, &required);
-
-               /* fall through */
        case CONNMAN_SERVICE_TYPE_ETHERNET:
        case CONNMAN_SERVICE_TYPE_WIMAX:
        case CONNMAN_SERVICE_TYPE_BLUETOOTH:
@@ -2132,27 +2008,43 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
                                                append_provider, service);
 }
 
-static void append_struct(gpointer value, gpointer user_data)
+static void append_struct_service(DBusMessageIter *iter,
+               connman_dbus_append_cb_t function,
+               struct connman_service *service)
 {
-       struct connman_service *service = value;
-       DBusMessageIter *iter = user_data;
        DBusMessageIter entry, dict;
 
-       if (service->path == NULL)
-               return;
-
        dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
 
        dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
                                                        &service->path);
 
        connman_dbus_dict_open(&entry, &dict);
-       append_properties(&dict, TRUE, service);
+       if (function != NULL)
+               function(&dict, service);
        connman_dbus_dict_close(&entry, &dict);
 
        dbus_message_iter_close_container(iter, &entry);
 }
 
+static void append_dict_properties(DBusMessageIter *dict, void *user_data)
+{
+       struct connman_service *service = user_data;
+
+       append_properties(dict, TRUE, service);
+}
+
+static void append_struct(gpointer value, gpointer user_data)
+{
+       struct connman_service *service = value;
+       DBusMessageIter *iter = user_data;
+
+       if (service->path == NULL)
+               return;
+
+       append_struct_service(iter, append_dict_properties, service);
+}
+
 void __connman_service_list_struct(DBusMessageIter *iter)
 {
        g_sequence_foreach(service_list, append_struct, iter);
@@ -2205,9 +2097,41 @@ char **connman_service_get_nameservers(struct connman_service *service)
                return NULL;
 
        if (service->nameservers_config != NULL)
-               return service->nameservers_config;
-       else if (service->nameservers != NULL)
-               return service->nameservers;
+               return g_strdupv(service->nameservers_config);
+       else if (service->nameservers != NULL ||
+                                       service->nameservers_auto != NULL) {
+               int len = 0, len_auto = 0, i;
+               char **nameservers;
+
+               if (service->nameservers != NULL)
+                       len = g_strv_length(service->nameservers);
+               if (service->nameservers_auto != NULL)
+                       len_auto = g_strv_length(service->nameservers_auto);
+
+               nameservers = g_try_new0(char *, len + len_auto + 1);
+               if (nameservers == NULL)
+                       return NULL;
+
+               for (i = 0; i < len; i++)
+                       nameservers[i] = g_strdup(service->nameservers[i]);
+
+               for (i = 0; i < len_auto; i++)
+                       nameservers[i + len] =
+                               g_strdup(service->nameservers_auto[i]);
+
+               return nameservers;
+       }
+
+       return NULL;
+}
+
+char **connman_service_get_timeservers(struct connman_service *service)
+{
+       if (service == NULL)
+               return NULL;
+
+       if (service->timeservers != NULL)
+               return service->timeservers;
 
        return NULL;
 }
@@ -2299,33 +2223,6 @@ const char *connman_service_get_proxy_autoconfig(struct connman_service *service
        return NULL;
 }
 
-static void update_timeservers(struct connman_service *service)
-{
-       int i;
-
-       if (service->timeservers == NULL)
-               return;
-
-       switch (service->state) {
-       case CONNMAN_SERVICE_STATE_UNKNOWN:
-       case CONNMAN_SERVICE_STATE_IDLE:
-       case CONNMAN_SERVICE_STATE_ASSOCIATION:
-       case CONNMAN_SERVICE_STATE_CONFIGURATION:
-               return;
-       case CONNMAN_SERVICE_STATE_FAILURE:
-       case CONNMAN_SERVICE_STATE_DISCONNECT:
-               for (i = 0; service->timeservers[i] != NULL; i++)
-                       connman_timeserver_remove(service->timeservers[i]);
-               return;
-       case CONNMAN_SERVICE_STATE_READY:
-       case CONNMAN_SERVICE_STATE_ONLINE:
-               break;
-       }
-
-       for (i = 0; service->timeservers[i] != NULL; i++)
-               connman_timeserver_append(service->timeservers[i]);
-}
-
 int __connman_service_timeserver_append(struct connman_service *service,
                                                const char *timeserver)
 {
@@ -2357,8 +2254,6 @@ int __connman_service_timeserver_append(struct connman_service *service,
        service->timeservers[len] = g_strdup(timeserver);
        service->timeservers[len + 1] = NULL;
 
-       update_timeservers(service);
-
        return 0;
 }
 
@@ -2412,8 +2307,6 @@ int __connman_service_timeserver_remove(struct connman_service *service,
        g_strfreev(service->timeservers);
        service->timeservers = servers;
 
-       update_timeservers(service);
-
        return 0;
 }
 
@@ -2466,8 +2359,6 @@ void __connman_service_set_passphrase(struct connman_service *service,
        g_free(service->passphrase);
        service->passphrase = g_strdup(passphrase);
 
-       passphrase_changed(service);
-
        if (service->network != NULL)
                connman_network_set_string(service->network,
                                        "WiFi.Passphrase",
@@ -2538,11 +2429,11 @@ static int update_proxy_configuration(struct connman_service *service,
                dbus_message_iter_get_basic(&entry, &key);
                dbus_message_iter_next(&entry);
 
-               if (dbus_message_iter_get_arg_type(&entry) !=
-                                                       DBUS_TYPE_VARIANT)
+               if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
                        goto error;
 
                dbus_message_iter_recurse(&entry, &variant);
+
                type = dbus_message_iter_get_arg_type(&variant);
 
                if (g_str_equal(key, "Method") == TRUE) {
@@ -2744,8 +2635,15 @@ static DBusMessage *set_property(DBusConnection *conn,
        if (dbus_message_iter_init(msg, &iter) == FALSE)
                return __connman_error_invalid_arguments(msg);
 
+       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
+               return __connman_error_invalid_arguments(msg);
+
        dbus_message_iter_get_basic(&iter, &name);
        dbus_message_iter_next(&iter);
+
+       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
+               return __connman_error_invalid_arguments(msg);
+
        dbus_message_iter_recurse(&iter, &value);
 
        type = dbus_message_iter_get_arg_type(&value);
@@ -2769,18 +2667,6 @@ static DBusMessage *set_property(DBusConnection *conn,
                autoconnect_changed(service);
 
                service_save(service);
-       } else if (g_str_equal(name, "Passphrase") == TRUE) {
-               const char *passphrase;
-
-               if (type != DBUS_TYPE_STRING)
-                       return __connman_error_invalid_arguments(msg);
-
-               if (service->immutable == TRUE || service->hidden == TRUE)
-                       return __connman_error_not_supported(msg);
-
-               dbus_message_iter_get_basic(&value, &passphrase);
-
-               __connman_service_set_passphrase(service, passphrase);
        } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
                DBusMessageIter entry;
                GString *str;
@@ -2798,7 +2684,8 @@ static DBusMessage *set_property(DBusConnection *conn,
                gw = __connman_ipconfig_get_gateway_from_index(index);
 
                if (gw && strlen(gw))
-                       __connman_service_nameserver_del_routes(service);
+                       __connman_service_nameserver_del_routes(service,
+                                               CONNMAN_IPCONFIG_TYPE_ALL);
 
                dbus_message_iter_recurse(&value, &entry);
 
@@ -2955,16 +2842,6 @@ static DBusMessage *clear_property(DBusConnection *conn,
 
                g_get_current_time(&service->modified);
                service_save(service);
-       } else if (g_str_equal(name, "Passphrase") == TRUE) {
-               if (service->immutable == TRUE || service->hidden == TRUE)
-                       return __connman_error_not_supported(msg);
-
-               g_free(service->passphrase);
-               service->passphrase = NULL;
-
-               passphrase_changed(service);
-
-               service_save(service);
        } else
                return __connman_error_invalid_property(msg);
 
@@ -3242,8 +3119,6 @@ static DBusMessage *remove_service(DBusConnection *conn,
        g_free(service->passphrase);
        service->passphrase = NULL;
 
-       passphrase_changed(service);
-
        set_idle(service);
 
        __connman_service_set_favorite(service, FALSE);
@@ -3395,7 +3270,7 @@ static DBusMessage *move_service(DBusConnection *conn,
                downgrade_state(target);
        }
 
-       services_changed(FALSE);
+       __connman_connection_update_gateway();
 
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
@@ -3422,6 +3297,134 @@ static DBusMessage *reset_counters(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
+static struct _services_notify {
+       int id;
+       GHashTable *add;
+       GHashTable *remove;
+} *services_notify;
+
+static void append_removed(gpointer key, gpointer value, gpointer user_data)
+{
+       char *objpath = key;
+       DBusMessageIter *iter = user_data;
+
+       DBG("removed %s", objpath);
+       dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
+}
+
+static void service_send_removed(void)
+{
+       DBusMessage *signal;
+       DBusMessageIter iter, array;
+
+       signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+                       CONNMAN_MANAGER_INTERFACE, "ServicesRemoved");
+       if (signal == NULL)
+               return;
+
+       dbus_message_iter_init_append(signal, &iter);
+       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+                       DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
+
+       g_hash_table_foreach(services_notify->remove, append_removed, &array);
+
+       dbus_message_iter_close_container(&iter, &array);
+
+       dbus_connection_send(connection, signal, NULL);
+       dbus_message_unref(signal);
+
+       g_hash_table_remove_all(services_notify->remove);
+}
+
+static void service_send_added_foreach(gpointer data, gpointer user_data)
+{
+       struct connman_service *service = data;
+       DBusMessageIter *iter = user_data;
+
+       if (service == NULL || service->path == NULL) {
+               DBG("service %p or path is NULL", service);
+               return;
+       }
+
+       DBG("added %s", service->path);
+
+       if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
+               append_struct(service, iter);
+               g_hash_table_remove(services_notify->add, service->path);
+       } else {
+               append_struct_service(iter, NULL, service);
+       }
+}
+
+static void service_send_added_ordered(DBusMessageIter *iter, void *user_data)
+{
+       g_sequence_foreach(service_list, service_send_added_foreach, iter);
+}
+
+static void service_send_added(void)
+{
+       DBusMessage *signal;
+
+       signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+                       CONNMAN_MANAGER_INTERFACE, "ServicesAdded");
+       if (signal == NULL)
+               return;
+
+       __connman_dbus_append_objpath_dict_array(signal,
+                       service_send_added_ordered, NULL);
+
+       dbus_connection_send(connection, signal, NULL);
+       dbus_message_unref(signal);
+
+       g_hash_table_remove_all(services_notify->add);
+}
+
+static gboolean service_send_signals(gpointer data)
+{
+       if (g_hash_table_size(services_notify->remove) > 0)
+               service_send_removed();
+
+       if (g_hash_table_size(services_notify->add) > 0)
+               service_send_added();
+
+       services_notify->id = 0;
+       return FALSE;
+}
+
+static void service_schedule_signals(void)
+{
+       if (services_notify->id != 0)
+               g_source_remove(services_notify->id);
+
+       services_notify->id = g_timeout_add(100, service_send_signals, NULL);
+}
+
+static void service_schedule_added(struct connman_service *service)
+{
+       DBG("service %p", service);
+
+       g_hash_table_remove(services_notify->remove, service->path);
+       g_hash_table_insert(services_notify->add, service->path, service);
+
+       service_schedule_signals();
+}
+
+static void service_schedule_removed(struct connman_service *service)
+{
+       DBG("service %p %s", service, service->path);
+
+       if (service == NULL || service->path == NULL) {
+               DBG("service %p or path is NULL", service);
+               return;
+       }
+
+       g_hash_table_remove(services_notify->add, service->path);
+       g_hash_table_insert(services_notify->remove, g_strdup(service->path),
+                       NULL);
+
+       service_schedule_signals();
+}
+
 static GDBusMethodTable service_methods[] = {
        { "GetProperties", "",   "a{sv}", get_properties     },
        { "SetProperty",   "sv", "",      set_property       },
@@ -3453,13 +3456,14 @@ static void service_free(gpointer user_data)
        g_hash_table_remove(service_hash, service->identifier);
 
        __connman_notifier_service_remove(service);
+       service_schedule_removed(service);
 
        stats_stop(service);
 
        service->path = NULL;
 
        if (path != NULL) {
-               services_changed(FALSE);
+               __connman_connection_update_gateway();
 
                g_dbus_unregister_interface(connection, path,
                                                CONNMAN_SERVICE_INTERFACE);
@@ -3470,27 +3474,28 @@ static void service_free(gpointer user_data)
 
        if (service->network != NULL) {
                __connman_network_disconnect(service->network);
-               if (service->network_created == TRUE)
-                       connman_network_unref(service->network);
+               connman_network_unref(service->network);
+               service->network = NULL;
        }
 
        if (service->provider != NULL)
                connman_provider_unref(service->provider);
 
        if (service->ipconfig_ipv4 != NULL) {
-               connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
-               connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
-               connman_ipconfig_unref(service->ipconfig_ipv4);
+               __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
+               __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
+               __connman_ipconfig_unref(service->ipconfig_ipv4);
                service->ipconfig_ipv4 = NULL;
        }
 
        if (service->ipconfig_ipv6 != NULL) {
-               connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
-               connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
-               connman_ipconfig_unref(service->ipconfig_ipv6);
+               __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
+               __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
+               __connman_ipconfig_unref(service->ipconfig_ipv6);
                service->ipconfig_ipv6 = NULL;
        }
 
+       g_strfreev(service->timeservers);
        g_strfreev(service->nameservers);
        g_strfreev(service->nameservers_config);
        g_strfreev(service->nameservers_auto);
@@ -3521,33 +3526,6 @@ static void service_free(gpointer user_data)
        g_free(service);
 }
 
-/**
- * __connman_service_put:
- * @service: service structure
- *
- * Release service if no longer needed
- */
-void __connman_service_put(struct connman_service *service)
-{
-       GSequenceIter *iter;
-
-       DBG("service %p", service);
-
-       if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
-               return;
-
-       iter = g_hash_table_lookup(service_hash, service->identifier);
-       if (iter != NULL) {
-               reply_pending(service, ECONNABORTED);
-
-               __connman_service_disconnect(service);
-
-               g_sequence_remove(iter);
-       } else {
-               service_free(service);
-       }
-}
-
 static void stats_init(struct connman_service *service)
 {
        /* home */
@@ -3568,8 +3546,6 @@ static void service_initialize(struct connman_service *service)
        service->refcount = 1;
        service->session_usage_count = 0;
 
-       service->network_created = FALSE;
-
        service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
        service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
 
@@ -3645,9 +3621,12 @@ struct connman_service *connman_service_create(void)
  *
  * Increase reference counter of service
  */
-struct connman_service *connman_service_ref(struct connman_service *service)
+struct connman_service *
+connman_service_ref_debug(struct connman_service *service,
+                       const char *file, int line, const char *caller)
 {
-       DBG("%p", service);
+       DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
+               file, line, caller);
 
        __sync_fetch_and_add(&service->refcount, 1);
 
@@ -3658,11 +3637,30 @@ struct connman_service *connman_service_ref(struct connman_service *service)
  * connman_service_unref:
  * @service: service structure
  *
- * Decrease reference counter of service
+ * Decrease reference counter of service and release service if no
+ * longer needed.
  */
-void connman_service_unref(struct connman_service *service)
+void connman_service_unref_debug(struct connman_service *service,
+                       const char *file, int line, const char *caller)
 {
-       __connman_service_put(service);
+       GSequenceIter *iter;
+
+       DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
+               file, line, caller);
+
+       if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
+               return;
+
+       iter = g_hash_table_lookup(service_hash, service->identifier);
+       if (iter != NULL) {
+               reply_pending(service, ECONNABORTED);
+
+               __connman_service_disconnect(service);
+
+               g_sequence_remove(iter);
+       } else {
+               service_free(service);
+       }
 }
 
 static gint service_compare(gconstpointer a, gconstpointer b,
@@ -3761,10 +3759,10 @@ char *connman_service_get_interface(struct connman_service *service)
 
        if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
                if (service->ipconfig_ipv4)
-                       index = connman_ipconfig_get_index(
+                       index = __connman_ipconfig_get_index(
                                                service->ipconfig_ipv4);
                else if (service->ipconfig_ipv6)
-                       index = connman_ipconfig_get_index(
+                       index = __connman_ipconfig_get_index(
                                                service->ipconfig_ipv6);
                else
                        return NULL;
@@ -3825,6 +3823,23 @@ __connman_service_get_ipconfig(struct connman_service *service, int family)
 
 }
 
+connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
+                                       enum connman_ipconfig_type type)
+{
+       if (service == NULL)
+               return FALSE;
+
+       switch (type) {
+       case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
+               break;
+       case CONNMAN_IPCONFIG_TYPE_IPV4:
+               return is_connected_state(service, service->state_ipv4);
+       case CONNMAN_IPCONFIG_TYPE_IPV6:
+               return is_connected_state(service, service->state_ipv6);
+       }
+
+       return FALSE;
+}
 enum connman_service_security __connman_service_get_security(struct connman_service *service)
 {
        if (service == NULL)
@@ -3877,7 +3892,7 @@ int __connman_service_set_favorite(struct connman_service *service,
 
        g_sequence_sort_changed(iter, service_compare, NULL);
 
-       services_changed(FALSE);
+       __connman_connection_update_gateway();
 
        return 0;
 }
@@ -3944,7 +3959,7 @@ static void report_error_cb(struct connman_service *service,
                __connman_service_connect(service);
        else {
                service_complete(service);
-               services_changed(FALSE);
+               __connman_connection_update_gateway();
                __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
        }
 }
@@ -3965,8 +3980,9 @@ void __connman_service_add_passphrase(struct connman_service *service,
        case CONNMAN_SERVICE_SECURITY_NONE:
        case CONNMAN_SERVICE_SECURITY_WPA:
        case CONNMAN_SERVICE_SECURITY_RSN:
-               DBG("service security '%s' not handled",
-                       security2string(service->security));
+               DBG("service security '%s' (%d) not handled",
+                               security2string(service->security),
+                               service->security);
                break;
        }
 
@@ -3991,7 +4007,7 @@ static void request_input_cb (struct connman_service *service,
 
        if (values_received == FALSE || service->hidden == TRUE) {
                service_complete(service);
-               services_changed(FALSE);
+               __connman_connection_update_gateway();
                __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
                return;
        }
@@ -4081,9 +4097,8 @@ static int service_indicate_state(struct connman_service *service)
                }
        }
 
-       if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
-               connman_timeserver_sync();
-       }
+       if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
+               __connman_timeserver_sync(service);
 
        if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
                connman_bool_t reconnect;
@@ -4154,6 +4169,8 @@ static int service_indicate_state(struct connman_service *service)
                dns_changed(service);
                domain_changed(service);
 
+               __connman_timeserver_stop();
+
                __connman_notifier_disconnect(service->type);
 
                /*
@@ -4180,7 +4197,7 @@ static int service_indicate_state(struct connman_service *service)
        if (iter != NULL)
                g_sequence_sort_changed(iter, service_compare, NULL);
 
-       services_changed(FALSE);
+       __connman_connection_update_gateway();
 
        if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
                default_changed();
@@ -4246,7 +4263,12 @@ int __connman_service_clear_error(struct connman_service *service)
 
 int __connman_service_indicate_default(struct connman_service *service)
 {
-       DBG("service %p", service);
+       struct connman_service *current = get_default();
+
+       DBG("service %p default %p", service, current);
+
+       if (current == service)
+               return 0;
 
        default_changed();
 
@@ -4350,6 +4372,42 @@ static void service_rp_filter(struct connman_service *service,
                connected_networks_count, original_rp_filter);
 }
 
+static gboolean redo_wispr(gpointer user_data)
+{
+       struct connman_service *service = user_data;
+
+       DBG("");
+
+       __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
+
+       return FALSE;
+}
+
+int __connman_service_online_check_failed(struct connman_service *service,
+                                       enum connman_ipconfig_type type)
+{
+       DBG("service %p type %d count %d", service, type,
+                                               service->online_check_count);
+
+       if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
+               /* currently we only retry IPv6 stuff */
+               return 0;
+
+       if (service->online_check_count != 1)
+               return 0;
+
+       service->online_check_count = 0;
+
+       /*
+        * We set the timeout to 1 sec so that we have a chance to get
+        * necessary IPv6 router advertisement messages that might have
+        * DNS data etc.
+        */
+       g_timeout_add_seconds(1, redo_wispr, service);
+
+       return EAGAIN;
+}
+
 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
                                        enum connman_service_state new_state,
                                        enum connman_ipconfig_type type)
@@ -4398,8 +4456,10 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service,
                if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
                        check_proxy_setup(service);
                        service_rp_filter(service, TRUE);
-               } else
+               } else {
+                       service->online_check_count = 1;
                        __connman_wispr_start(service, type);
+               }
                break;
        case CONNMAN_SERVICE_STATE_ONLINE:
                break;
@@ -4681,7 +4741,7 @@ int __connman_service_connect(struct connman_service *service)
                                                        NULL) == -EIO)
                                return -EINPROGRESS;
                }
-               reply_pending(service, err);
+               reply_pending(service, -err);
        }
 
        return err;
@@ -4849,16 +4909,16 @@ static int service_register(struct connman_service *service)
        if (iter != NULL)
                g_sequence_sort_changed(iter, service_compare, NULL);
 
-       services_changed(TRUE);
+       __connman_connection_update_gateway();
 
        return 0;
 }
 
 static void service_up(struct connman_ipconfig *ipconfig)
 {
-       struct connman_service *service = connman_ipconfig_get_data(ipconfig);
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
 
-       DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
 
        link_changed(service);
 
@@ -4868,23 +4928,23 @@ static void service_up(struct connman_ipconfig *ipconfig)
 
 static void service_down(struct connman_ipconfig *ipconfig)
 {
-       DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
 }
 
 static void service_lower_up(struct connman_ipconfig *ipconfig)
 {
-       struct connman_service *service = connman_ipconfig_get_data(ipconfig);
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
 
-       DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
 
        stats_start(service);
 }
 
 static void service_lower_down(struct connman_ipconfig *ipconfig)
 {
-       struct connman_service *service = connman_ipconfig_get_data(ipconfig);
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
 
-       DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
 
        stats_stop(service);
        service_save(service);
@@ -4892,11 +4952,11 @@ static void service_lower_down(struct connman_ipconfig *ipconfig)
 
 static void service_ip_bound(struct connman_ipconfig *ipconfig)
 {
-       struct connman_service *service = connman_ipconfig_get_data(ipconfig);
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
        enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
        enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
 
-       DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
 
        type = __connman_ipconfig_get_config_type(ipconfig);
        method = __connman_ipconfig_get_method(ipconfig);
@@ -4915,11 +4975,11 @@ static void service_ip_bound(struct connman_ipconfig *ipconfig)
 
 static void service_ip_release(struct connman_ipconfig *ipconfig)
 {
-       struct connman_service *service = connman_ipconfig_get_data(ipconfig);
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
        enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
        enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
 
-       DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
+       DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
 
        type = __connman_ipconfig_get_config_type(ipconfig);
        method = __connman_ipconfig_get_method(ipconfig);
@@ -4954,28 +5014,28 @@ static const struct connman_ipconfig_ops service_ops = {
 static void setup_ip4config(struct connman_service *service, int index,
                        enum connman_ipconfig_method method)
 {
-       service->ipconfig_ipv4 = connman_ipconfig_create(index,
+       service->ipconfig_ipv4 = __connman_ipconfig_create(index,
                                                CONNMAN_IPCONFIG_TYPE_IPV4);
        if (service->ipconfig_ipv4 == NULL)
                return;
 
-       connman_ipconfig_set_method(service->ipconfig_ipv4, method);
+       __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
 
-       connman_ipconfig_set_data(service->ipconfig_ipv4, service);
+       __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
 
-       connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
+       __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
 }
 
 static void setup_ip6config(struct connman_service *service, int index)
 {
-       service->ipconfig_ipv6 = connman_ipconfig_create(index,
+       service->ipconfig_ipv6 = __connman_ipconfig_create(index,
                                                CONNMAN_IPCONFIG_TYPE_IPV6);
        if (service->ipconfig_ipv6 == NULL)
                return;
 
-       connman_ipconfig_set_data(service->ipconfig_ipv6, service);
+       __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
 
-       connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
+       __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
 }
 
 void __connman_service_read_ip4config(struct connman_service *service)
@@ -5080,11 +5140,11 @@ struct connman_service *__connman_service_lookup_from_index(int index)
        while (g_sequence_iter_is_end(iter) == FALSE) {
                service = g_sequence_get(iter);
 
-               if (connman_ipconfig_get_index(service->ipconfig_ipv4)
+               if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
                                                        == index)
                        return service;
 
-               if (connman_ipconfig_get_index(service->ipconfig_ipv6)
+               if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
                                                        == index)
                        return service;
 
@@ -5218,13 +5278,14 @@ static void update_from_network(struct connman_service *service,
                service->wps = connman_network_get_bool(network, "WiFi.WPS");
 
        if (service->strength > strength && service->network != NULL) {
-               service->network = network;
+               connman_network_unref(service->network);
+               service->network = connman_network_ref(network);
 
                strength_changed(service);
        }
 
        if (service->network == NULL)
-               service->network = network;
+               service->network = connman_network_ref(network);
 
        iter = g_hash_table_lookup(service_hash, service->identifier);
        if (iter != NULL)
@@ -5271,7 +5332,7 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne
 
        if (service->path != NULL) {
                update_from_network(service, network);
-               services_changed(TRUE);
+               __connman_connection_update_gateway();
                return service;
        }
 
@@ -5316,6 +5377,7 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne
        }
 
        __connman_notifier_service_add(service, service->name);
+       service_schedule_added(service);
 
        return service;
 }
@@ -5398,7 +5460,7 @@ void __connman_service_remove_from_network(struct connman_network *network)
        __connman_connection_gateway_remove(service,
                                        CONNMAN_IPCONFIG_TYPE_ALL);
 
-       __connman_service_put(service);
+       connman_service_unref(service);
 }
 
 /**
@@ -5434,6 +5496,7 @@ __connman_service_create_from_provider(struct connman_provider *provider)
        service->userconnect = TRUE;
 
        service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
+       service->state = combine_state(service->state_ipv4, service->state_ipv6);
 
        str = connman_provider_get_string(provider, "Name");
        if (str != NULL) {
@@ -5457,6 +5520,7 @@ __connman_service_create_from_provider(struct connman_provider *provider)
        service_register(service);
 
        __connman_notifier_service_add(service, service->name);
+       service_schedule_added(service);
 
        return service;
 }
@@ -5472,6 +5536,11 @@ int __connman_service_init(void)
 
        service_list = g_sequence_new(service_free);
 
+       services_notify = g_new0(struct _services_notify, 1);
+       services_notify->remove = g_hash_table_new_full(g_str_hash,
+                       g_str_equal, g_free, NULL);
+       services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
+
        return 0;
 }
 
@@ -5491,5 +5560,13 @@ void __connman_service_cleanup(void)
        g_slist_free(counter_list);
        counter_list = NULL;
 
+       if (services_notify->id != 0) {
+               g_source_remove(services_notify->id);
+               service_send_signals(NULL);
+               g_hash_table_destroy(services_notify->remove);
+               g_hash_table_destroy(services_notify->add);
+       }
+       g_free(services_notify);
+
        dbus_connection_unref(connection);
 }