inet: Create helper function for IP address checking
[framework/connectivity/connman.git] / src / notifier.c
index 91b52ea..b328a2a 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  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
@@ -32,6 +32,8 @@ 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)
 {
        const struct connman_notifier *notifier1 = a;
@@ -73,49 +75,68 @@ void connman_notifier_unregister(struct connman_notifier *notifier)
 
 #define MAX_TECHNOLOGIES 10
 
-static volatile int connected[MAX_TECHNOLOGIES];
+static int connected[MAX_TECHNOLOGIES];
+static int online[MAX_TECHNOLOGIES];
 
-unsigned int __connman_notifier_count_connected(void)
+static connman_bool_t notifier_is_online(void)
 {
-       unsigned int i, count = 0;
+       unsigned int i;
 
        __sync_synchronize();
        for (i = 0; i < MAX_TECHNOLOGIES; i++) {
-               if (connected[i] > 0)
-                       count++;
+               if (online[i] > 0)
+                       return TRUE;
        }
 
-       return count;
+       return FALSE;
 }
 
-const char *__connman_notifier_get_state(void)
+connman_bool_t __connman_notifier_is_connected(void)
 {
-       unsigned int count = __connman_notifier_count_connected();
+       unsigned int i;
+
+       __sync_synchronize();
+       for (i = 0; i < MAX_TECHNOLOGIES; i++) {
+               if (connected[i] > 0)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
 
-       if (count > 0)
+static const char *evaluate_notifier_state(void)
+{
+       if (notifier_is_online() == TRUE)
                return "online";
 
-       return "offline";
+       if (__connman_notifier_is_connected() == TRUE)
+               return "ready";
+
+       if ( __connman_technology_get_offlinemode() == TRUE)
+               return "offline";
+
+       return "idle";
 }
 
-static void state_changed(connman_bool_t connected)
+const char *__connman_notifier_get_state(void)
 {
-       unsigned int count = __connman_notifier_count_connected();
-       char *state = "offline";
+       return notifier_state;
+}
 
-       if (count > 1)
-               return;
+static void state_changed(void)
+{
+       const char *state;
 
-       if (count == 1) {
-               if (connected == FALSE)
-                       return;
+       state = evaluate_notifier_state();
 
-               state = "online";
-       }
+       if (g_strcmp0(state, notifier_state) == 0)
+               return;
+
+       notifier_state = state;
 
        connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
                                CONNMAN_MANAGER_INTERFACE, "State",
-                                               DBUS_TYPE_STRING, &state);
+                                       DBUS_TYPE_STRING, &notifier_state);
 }
 
 static void technology_connected(enum connman_service_type type,
@@ -124,7 +145,7 @@ static void technology_connected(enum connman_service_type type,
        DBG("type %d connected %d", type, connected);
 
        __connman_technology_set_connected(type, connected);
-       state_changed(connected);
+       state_changed();
 }
 
 void __connman_notifier_connect(enum connman_service_type type)
@@ -150,6 +171,22 @@ void __connman_notifier_connect(enum connman_service_type type)
                technology_connected(type, TRUE);
 }
 
+void __connman_notifier_enter_online(enum connman_service_type type)
+{
+       DBG("type %d", type);
+
+       if (__sync_fetch_and_add(&online[type], 1) == 0)
+               state_changed();
+}
+
+void __connman_notifier_leave_online(enum connman_service_type type)
+{
+       DBG("type %d", type);
+
+       if (__sync_fetch_and_sub(&online[type], 1) == 1)
+               state_changed();
+}
+
 void __connman_notifier_disconnect(enum connman_service_type type)
 {
        DBG("type %d", type);
@@ -181,31 +218,10 @@ void __connman_notifier_disconnect(enum connman_service_type type)
        technology_connected(type, FALSE);
 }
 
-static void technology_default(enum connman_service_type type)
-{
-       const char *str;
-
-       str = __connman_service_type2string(type);
-       if (str == NULL)
-               str = "";
-
-       connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
-                       CONNMAN_MANAGER_INTERFACE, "DefaultTechnology",
-                                               DBUS_TYPE_STRING, &str);
-}
-
 void __connman_notifier_default_changed(struct connman_service *service)
 {
-       enum connman_service_type type = connman_service_get_type(service);
-       char *interface;
        GSList *list;
 
-       technology_default(type);
-
-       interface = connman_service_get_interface(service);
-       __connman_tethering_update_interface(interface);
-       g_free(interface);
-
        for (list = notifier_list; list; list = list->next) {
                struct connman_notifier *notifier = list->data;
 
@@ -278,6 +294,7 @@ void __connman_notifier_offlinemode(connman_bool_t enabled)
        DBG("enabled %d", enabled);
 
        offlinemode_changed(enabled);
+       state_changed();
 
        for (list = notifier_list; list; list = list->next) {
                struct connman_notifier *notifier = list->data;
@@ -368,6 +385,7 @@ int __connman_notifier_init(void)
        service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                                NULL, NULL);
 
+       notifier_state = evaluate_notifier_state();
 
        return 0;
 }