device: Include Reason in Disconnect Skip Warning
[framework/connectivity/connman.git] / src / device.c
index a28a7bc..a9f4330 100644 (file)
@@ -40,12 +40,13 @@ struct connman_device {
        connman_bool_t disconnected;
        connman_bool_t reconnect;
        connman_uint16_t scan_interval;
+       connman_uint16_t backoff_interval;
        char *name;
        char *node;
        char *address;
        char *interface;
-       char *control;
        char *ident;
+       char *path;
        int phyindex;
        unsigned int connections;
        guint scan_timeout;
@@ -58,6 +59,8 @@ struct connman_device {
        GHashTable *networks;
 };
 
+#define SCAN_INITIAL_DELAY 10
+
 static gboolean device_scan_trigger(gpointer user_data)
 {
        struct connman_device *device = user_data;
@@ -88,9 +91,23 @@ static void reset_scan_trigger(struct connman_device *device)
        clear_scan_trigger(device);
 
        if (device->scan_interval > 0) {
-               guint interval = device->scan_interval;
+               guint interval;
+
+               if (g_hash_table_size(device->networks) == 0) {
+                       if (device->backoff_interval >= device->scan_interval)
+                               device->backoff_interval = SCAN_INITIAL_DELAY;
+                       interval = device->backoff_interval;
+               } else
+                       interval = device->scan_interval;
+
+               DBG("interval %d", interval);
+
                device->scan_timeout = g_timeout_add_seconds(interval,
                                        device_scan_trigger, device);
+
+               device->backoff_interval *= 2;
+               if (device->backoff_interval > device->scan_interval)
+                       device->backoff_interval = device->scan_interval;
        }
 }
 
@@ -125,6 +142,9 @@ static const char *type2description(enum connman_device_type type)
                return "GPS";
        case CONNMAN_DEVICE_TYPE_CELLULAR:
                return "Cellular";
+       case CONNMAN_DEVICE_TYPE_GADGET:
+               return "Gadget";
+
        }
 
        return NULL;
@@ -148,6 +168,9 @@ static const char *type2string(enum connman_device_type type)
                return "gps";
        case CONNMAN_DEVICE_TYPE_CELLULAR:
                return "cellular";
+       case CONNMAN_DEVICE_TYPE_GADGET:
+               return "gadget";
+
        }
 
        return NULL;
@@ -172,6 +195,9 @@ enum connman_service_type __connman_device_get_service_type(struct connman_devic
                return CONNMAN_SERVICE_TYPE_BLUETOOTH;
        case CONNMAN_DEVICE_TYPE_CELLULAR:
                return CONNMAN_SERVICE_TYPE_CELLULAR;
+       case CONNMAN_DEVICE_TYPE_GADGET:
+               return CONNMAN_SERVICE_TYPE_GADGET;
+
        }
 
        return CONNMAN_SERVICE_TYPE_UNKNOWN;
@@ -180,6 +206,7 @@ enum connman_service_type __connman_device_get_service_type(struct connman_devic
 int __connman_device_enable(struct connman_device *device)
 {
        int err;
+       enum connman_service_type type;
 
        DBG("device %p %d", device, device->blocked);
 
@@ -192,8 +219,11 @@ int __connman_device_enable(struct connman_device *device)
        if (device->blocked == TRUE)
                return -ENOLINK;
 
+       connman_device_set_disconnected(device, FALSE);
+       device->scanning = FALSE;
+
        err = device->driver->enable(device);
-       if (err < 0) {
+       if (err < 0 && err != -EALREADY) {
                if (err == -EINPROGRESS) {
                        device->powered_pending = TRUE;
                        device->offlinemode = FALSE;
@@ -209,7 +239,8 @@ int __connman_device_enable(struct connman_device *device)
        if (__connman_profile_get_offlinemode() == TRUE)
                __connman_profile_set_offlinemode(FALSE, FALSE);
 
-       __connman_technology_enable_device(device);
+       type = __connman_device_get_service_type(device);
+       __connman_technology_enable(type);
 
        return 0;
 }
@@ -217,6 +248,7 @@ int __connman_device_enable(struct connman_device *device)
 int __connman_device_disable(struct connman_device *device)
 {
        int err;
+       enum connman_service_type type;
 
        DBG("device %p", device);
 
@@ -236,16 +268,19 @@ int __connman_device_disable(struct connman_device *device)
        g_hash_table_remove_all(device->networks);
 
        err = device->driver->disable(device);
-       if (err < 0) {
+       if (err < 0 && err != -EALREADY) {
                if (err == -EINPROGRESS)
                        device->powered_pending = FALSE;
                return err;
        }
 
+       device->connections = 0;
+
        device->powered_pending = FALSE;
        device->powered = FALSE;
 
-       __connman_technology_disable_device(device);
+       type = __connman_device_get_service_type(device);
+       __connman_technology_disable(type);
 
        return 0;
 }
@@ -300,13 +335,9 @@ static void probe_driver(struct connman_element *element, gpointer user_data)
 
 static void remove_device(struct connman_device *device)
 {
-       int err;
-
        DBG("device %p", device);
 
-       err = __connman_device_disable(device);
-       if (err < 0 && err == -EINPROGRESS)
-               __connman_technology_disable_device(device);
+       __connman_device_disable(device);
 
        __connman_technology_remove_device(device);
 
@@ -409,8 +440,8 @@ static void device_destruct(struct connman_element *element)
        g_free(device->node);
        g_free(device->name);
        g_free(device->address);
-       g_free(device->control);
        g_free(device->interface);
+       g_free(device->path);
 
        g_free(device->last_network);
 
@@ -433,6 +464,7 @@ struct connman_device *connman_device_create(const char *node,
        struct connman_device *device;
        const char *str;
        enum connman_service_type service_type;
+       connman_bool_t bg_scan;
 
        DBG("node %s type %d", node, type);
 
@@ -442,6 +474,8 @@ struct connman_device *connman_device_create(const char *node,
 
        DBG("device %p", device);
 
+       bg_scan = connman_setting_get_bool("BackgroundScanning");
+
        __connman_element_initialize(&device->element);
 
        device->element.name = g_strdup(node);
@@ -455,8 +489,6 @@ struct connman_device *connman_device_create(const char *node,
                connman_element_set_string(&device->element,
                                        CONNMAN_PROPERTY_ID_TYPE, str);
 
-       device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
-
        device->type = type;
        device->name = g_strdup(type2description(device->type));
 
@@ -466,27 +498,24 @@ struct connman_device *connman_device_create(const char *node,
 
        service_type = __connman_device_get_service_type(device);
        device->blocked = __connman_technology_get_blocked(service_type);
+       device->backoff_interval = SCAN_INITIAL_DELAY;
 
        switch (type) {
        case CONNMAN_DEVICE_TYPE_UNKNOWN:
-       case CONNMAN_DEVICE_TYPE_VENDOR:
-               device->scan_interval = 0;
-               break;
        case CONNMAN_DEVICE_TYPE_ETHERNET:
-       case CONNMAN_DEVICE_TYPE_WIFI:
-               device->scan_interval = 300;
-               break;
        case CONNMAN_DEVICE_TYPE_WIMAX:
-               device->scan_interval = 0;
-               break;
        case CONNMAN_DEVICE_TYPE_BLUETOOTH:
-               device->scan_interval = 0;
-               break;
+       case CONNMAN_DEVICE_TYPE_CELLULAR:
        case CONNMAN_DEVICE_TYPE_GPS:
+       case CONNMAN_DEVICE_TYPE_GADGET:
+       case CONNMAN_DEVICE_TYPE_VENDOR:
                device->scan_interval = 0;
                break;
-       case CONNMAN_DEVICE_TYPE_CELLULAR:
-               device->scan_interval = 0;
+       case CONNMAN_DEVICE_TYPE_WIFI:
+               if (bg_scan == TRUE)
+                       device->scan_interval = 300;
+               else
+                       device->scan_interval = 0;
                break;
        }
 
@@ -538,17 +567,6 @@ enum connman_device_type connman_device_get_type(struct connman_device *device)
 }
 
 /**
- * connman_device_get_name:
- * @device: device structure
- *
- * Get unique name of device
- */
-const char *connman_device_get_name(struct connman_device *device)
-{
-       return device->element.name;
-}
-
-/**
  * connman_device_set_index:
  * @device: device structure
  * @index: index number
@@ -586,12 +604,11 @@ void __connman_device_set_phyindex(struct connman_device *device,
  * connman_device_set_interface:
  * @device: device structure
  * @interface: interface name
- * @control: control interface
  *
  * Set interface name of device
  */
 void connman_device_set_interface(struct connman_device *device,
-                               const char *interface, const char *control)
+                                               const char *interface)
 {
        g_free(device->element.devname);
        device->element.devname = g_strdup(interface);
@@ -599,9 +616,6 @@ void connman_device_set_interface(struct connman_device *device,
        g_free(device->interface);
        device->interface = g_strdup(interface);
 
-       g_free(device->control);
-       device->control = g_strdup(control);
-
        if (device->name == NULL) {
                const char *str = type2description(device->type);
                if (str != NULL && device->interface != NULL)
@@ -610,11 +624,6 @@ void connman_device_set_interface(struct connman_device *device,
        }
 }
 
-const char *connman_device_get_control(struct connman_device *device)
-{
-       return device->control;
-}
-
 /**
  * connman_device_set_ident:
  * @device: device structure
@@ -644,6 +653,9 @@ const char *connman_device_get_ident(struct connman_device *device)
 int connman_device_set_powered(struct connman_device *device,
                                                connman_bool_t powered)
 {
+       int err;
+       enum connman_service_type type;
+
        DBG("driver %p powered %d", device, powered);
 
        if (device->powered == powered) {
@@ -652,17 +664,22 @@ int connman_device_set_powered(struct connman_device *device,
        }
 
        if (powered == TRUE)
-               __connman_device_enable(device);
+               err = __connman_device_enable(device);
        else
-               __connman_device_disable(device);
+               err = __connman_device_disable(device);
+
+       if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
+               return err;
 
        device->powered = powered;
        device->powered_pending = powered;
 
+       type = __connman_device_get_service_type(device);
+
        if (device->powered == TRUE)
-               __connman_technology_enable_device(device);
+               __connman_technology_enable(type);
        else
-               __connman_technology_disable_device(device);
+               __connman_technology_disable(type);
 
        if (device->offlinemode == TRUE && powered == TRUE)
                return connman_device_set_powered(device, FALSE);
@@ -672,7 +689,7 @@ int connman_device_set_powered(struct connman_device *device,
 
        reset_scan_trigger(device);
 
-       if (device->driver->scan)
+       if (device->driver && device->driver->scan)
                device->driver->scan(device);
 
        return 0;
@@ -766,7 +783,7 @@ int __connman_device_disconnect(struct connman_device *device)
        while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
                struct connman_network *network = value;
 
-               if (__connman_network_get_connecting(network) == TRUE) {
+               if (connman_network_get_connecting(network) == TRUE) {
                        /*
                         * Skip network in the process of connecting.
                         * This is a workaround for WiFi networks serviced
@@ -777,7 +794,7 @@ int __connman_device_disconnect(struct connman_device *device)
                         * will fail.  There is nothing to be gained by
                         * removing the network here anyway.
                         */
-                       connman_warn("Skipping disconnect of %s",
+                       connman_warn("Skipping disconnect of %s, network is connecting.",
                                connman_network_get_identifier(network));
                        continue;
                }
@@ -788,6 +805,14 @@ int __connman_device_disconnect(struct connman_device *device)
        return 0;
 }
 
+static void mark_network_available(gpointer key, gpointer value,
+                                                        gpointer user_data)
+{
+       struct connman_network *network = value;
+
+       connman_network_set_available(network, TRUE);
+}
+
 static void mark_network_unavailable(gpointer key, gpointer value,
                                                        gpointer user_data)
 {
@@ -819,6 +844,20 @@ void __connman_device_cleanup_networks(struct connman_device *device)
                                        remove_unavailable_network, NULL);
 }
 
+connman_bool_t __connman_device_scanning(struct connman_device *device)
+{
+       return device->scanning;
+}
+
+void connman_device_reset_scanning(struct connman_device *device)
+{
+       device->scanning = FALSE;
+
+       g_hash_table_foreach(device->networks,
+                               mark_network_available, NULL);
+
+}
+
 /**
  * connman_device_set_scanning:
  * @device: device structure
@@ -853,9 +892,6 @@ int connman_device_set_scanning(struct connman_device *device,
        if (device->connections > 0)
                return 0;
 
-       if (device->disconnected == TRUE)
-               return 0;
-
        __connman_service_auto_connect();
 
        return 0;
@@ -917,9 +953,14 @@ int connman_device_set_string(struct connman_device *device,
        } else if (g_str_equal(key, "Node") == TRUE) {
                g_free(device->node);
                device->node = g_strdup(value);
+       } else if (g_str_equal(key, "Path") == TRUE) {
+               g_free(device->path);
+               device->path = g_strdup(value);
+       } else {
+               return -EINVAL;
        }
 
-       return connman_element_set_string(&device->element, key, value);
+       return 0;
 }
 
 /**
@@ -942,8 +983,10 @@ const char *connman_device_get_string(struct connman_device *device,
                return device->node;
        else if (g_str_equal(key, "Interface") == TRUE)
                return device->interface;
+       else if (g_str_equal(key, "Path") == TRUE)
+               return device->path;
 
-       return connman_element_get_string(&device->element, key);
+       return NULL;
 }
 
 static void set_offlinemode(struct connman_element *element, gpointer user_data)
@@ -999,6 +1042,9 @@ void __connman_device_decrease_connections(struct connman_device *device)
                return;
 
        device->connections--;
+
+       if (device->connections == 0)
+               device->backoff_interval = SCAN_INITIAL_DELAY;
 }
 
 /**
@@ -1236,7 +1282,6 @@ static int device_load(struct connman_device *device)
        GError *error = NULL;
        gchar *identifier;
        connman_bool_t powered;
-       int val;
 
        DBG("device %p", device);
 
@@ -1254,12 +1299,6 @@ static int device_load(struct connman_device *device)
                device->powered_persistent = powered;
        g_clear_error(&error);
 
-       val = g_key_file_get_integer(keyfile, identifier,
-                                               "ScanInterval", &error);
-       if (error == NULL)
-               device->scan_interval = val;
-       g_clear_error(&error);
-
 done:
        g_free(identifier);
 
@@ -1287,9 +1326,6 @@ static int device_save(struct connman_device *device)
        g_key_file_set_boolean(keyfile, identifier,
                                        "Powered", device->powered_persistent);
 
-       g_key_file_set_integer(keyfile, identifier,
-                                       "ScanInterval", device->scan_interval);
-
 done:
        g_free(identifier);