device: Return -EOPNOTSUPP if the device does not support scanning
[platform/upstream/connman.git] / src / device.c
index 6ccd477..55a6d8e 100644 (file)
@@ -39,12 +39,11 @@ enum connman_pending_type {
 };
 
 struct connman_device {
-       gint refcount;
+       int refcount;
        enum connman_device_type type;
        enum connman_pending_type powered_pending;      /* Indicates a pending
                                                        enable/disable request */
        connman_bool_t powered;
-       connman_bool_t powered_persistent;
        connman_bool_t scanning;
        connman_bool_t disconnected;
        connman_bool_t reconnect;
@@ -59,7 +58,6 @@ struct connman_device {
        char *devname;
        int phyindex;
        int index;
-       unsigned int connections;
        guint scan_timeout;
        guint pending_timeout;
 
@@ -98,6 +96,14 @@ static void clear_scan_trigger(struct connman_device *device)
        }
 }
 
+static void clear_pending_trigger(struct connman_device *device)
+{
+       if (device->pending_timeout > 0) {
+               g_source_remove(device->pending_timeout);
+               device->pending_timeout = 0;
+       }
+}
+
 static void reset_scan_trigger(struct connman_device *device)
 {
        clear_scan_trigger(device);
@@ -222,11 +228,8 @@ static gboolean device_pending_reset(gpointer user_data)
        DBG("device %p", device);
 
        /* Power request timedout, reset power pending state. */
-       if (device->pending_timeout > 0) {
-               g_source_remove(device->pending_timeout);
-               device->pending_timeout = 0;
-               device->powered_pending = PENDING_NONE;
-       }
+       device->pending_timeout = 0;
+       device->powered_pending = PENDING_NONE;
 
        return FALSE;
 }
@@ -298,10 +301,21 @@ int __connman_device_disable(struct connman_device *device)
        if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
                return -EALREADY;
 
+       device->powered_pending = PENDING_DISABLE;
        device->reconnect = FALSE;
 
        clear_scan_trigger(device);
 
+       if (device->network) {
+               struct connman_service *service =
+                       __connman_service_lookup_from_network(device->network);
+
+               if (service != NULL)
+                       __connman_service_disconnect(service);
+               else
+                       connman_network_set_connected(device->network, FALSE);
+       }
+
        err = device->driver->disable(device);
        if (err == 0) {
                connman_device_set_powered(device, FALSE);
@@ -439,6 +453,7 @@ static void device_destruct(struct connman_device *device)
 {
        DBG("device %p name %s", device, device->name);
 
+       clear_pending_trigger(device);
        clear_scan_trigger(device);
 
        g_free(device->ident);
@@ -487,8 +502,6 @@ struct connman_device *connman_device_create(const char *node,
        device->type = type;
        device->name = g_strdup(type2description(device->type));
 
-       device->powered_persistent = TRUE;
-
        device->phyindex = -1;
 
        device->backoff_interval = SCAN_INITIAL_DELAY;
@@ -526,11 +539,13 @@ struct connman_device *connman_device_create(const char *node,
  *
  * Increase reference counter of device
  */
-struct connman_device *connman_device_ref(struct connman_device *device)
+struct connman_device *connman_device_ref_debug(struct connman_device *device,
+                               const char *file, int line, const char *caller)
 {
-       DBG("%p", device);
+       DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
+               file, line, caller);
 
-       g_atomic_int_inc(&device->refcount);
+       __sync_fetch_and_add(&device->refcount, 1);
 
        return device;
 }
@@ -541,9 +556,13 @@ struct connman_device *connman_device_ref(struct connman_device *device)
  *
  * Decrease reference counter of device
  */
-void connman_device_unref(struct connman_device *device)
+void connman_device_unref_debug(struct connman_device *device,
+                               const char *file, int line, const char *caller)
 {
-       if (g_atomic_int_dec_and_test(&device->refcount) == FALSE)
+       DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
+               file, line, caller);
+
+       if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
                return;
 
        if (device->driver) {
@@ -666,9 +685,8 @@ int connman_device_set_powered(struct connman_device *device,
        if (device->powered == powered)
                return -EALREADY;
 
-       /* Reset pending request */
-       g_source_remove(device->pending_timeout);
-       device->pending_timeout = 0;
+       clear_pending_trigger(device);
+
        device->powered_pending = PENDING_NONE;
 
        device->powered = powered;
@@ -680,17 +698,17 @@ int connman_device_set_powered(struct connman_device *device,
        else
                __connman_technology_disabled(type);
 
-       if (powered == FALSE) {
-               device->connections = 0;
+       if (powered == FALSE)
                return 0;
-       }
 
        connman_device_set_disconnected(device, FALSE);
        device->scanning = FALSE;
 
        reset_scan_trigger(device);
 
-       if (device->driver && device->driver->scan)
+       if (device->driver && device->driver->scan_fast)
+               device->driver->scan_fast(device);
+       else if (device->driver && device->driver->scan)
                device->driver->scan(device);
 
        return 0;
@@ -709,28 +727,6 @@ static int device_scan(struct connman_device *device)
        return device->driver->scan(device);
 }
 
-int __connman_device_enable_persistent(struct connman_device *device)
-{
-       DBG("device %p", device);
-
-       device->powered_persistent = TRUE;
-
-       __connman_storage_save_device(device);
-
-       return __connman_device_enable(device);
-}
-
-int __connman_device_disable_persistent(struct connman_device *device)
-{
-       DBG("device %p", device);
-
-       device->powered_persistent = FALSE;
-
-       __connman_storage_save_device(device);
-
-       return __connman_device_disable(device);
-}
-
 int __connman_device_disconnect(struct connman_device *device)
 {
        GHashTableIter iter;
@@ -851,9 +847,6 @@ int connman_device_set_scanning(struct connman_device *device,
 
        __connman_device_cleanup_networks(device);
 
-       if (device->connections > 0)
-               return 0;
-
        __connman_service_auto_connect();
 
        return 0;
@@ -877,7 +870,10 @@ int connman_device_set_disconnected(struct connman_device *device,
        device->disconnected = disconnected;
 
        if (disconnected == TRUE)
+       {
                force_scan_trigger(device);
+               device->backoff_interval = SCAN_INITIAL_DELAY;
+       }
 
        return 0;
 }
@@ -951,25 +947,6 @@ const char *connman_device_get_string(struct connman_device *device,
        return NULL;
 }
 
-void __connman_device_increase_connections(struct connman_device *device)
-{
-       if (device == NULL)
-               return;
-
-       device->connections++;
-}
-
-void __connman_device_decrease_connections(struct connman_device *device)
-{
-       if (device == NULL)
-               return;
-
-       device->connections--;
-
-       if (device->connections == 0)
-               device->backoff_interval = SCAN_INITIAL_DELAY;
-}
-
 /**
  * connman_device_add_network:
  * @device: device structure
@@ -1087,7 +1064,13 @@ static gboolean match_driver(struct connman_device *device,
        return FALSE;
 }
 
-static int device_probe(struct connman_device *device)
+/**
+ * connman_device_register:
+ * @device: device structure
+ *
+ * Register device with the system
+ */
+int connman_device_register(struct connman_device *device)
 {
        GSList *list;
 
@@ -1116,29 +1099,6 @@ static int device_probe(struct connman_device *device)
        return __connman_technology_add_device(device);
 }
 
-static void device_remove(struct connman_device *device)
-{
-       DBG("device %p name %s", device, device->name);
-
-       if (device->driver == NULL)
-               return;
-
-       remove_device(device);
-}
-
-/**
- * connman_device_register:
- * @device: device structure
- *
- * Register device with the system
- */
-int connman_device_register(struct connman_device *device)
-{
-       __connman_storage_load_device(device);
-
-       return device_probe(device);
-}
-
 /**
  * connman_device_unregister:
  * @device: device structure
@@ -1147,9 +1107,12 @@ int connman_device_register(struct connman_device *device)
  */
 void connman_device_unregister(struct connman_device *device)
 {
-       __connman_storage_save_device(device);
+       DBG("device %p name %s", device, device->name);
+
+       if (device->driver == NULL)
+               return;
 
-       device_remove(device);
+       remove_device(device);
 }
 
 /**
@@ -1208,7 +1171,7 @@ int __connman_device_request_scan(enum connman_service_type type)
        case CONNMAN_SERVICE_TYPE_GPS:
        case CONNMAN_SERVICE_TYPE_VPN:
        case CONNMAN_SERVICE_TYPE_GADGET:
-               return 0;
+               return -EOPNOTSUPP;
        case CONNMAN_SERVICE_TYPE_WIFI:
        case CONNMAN_SERVICE_TYPE_WIMAX:
                break;
@@ -1235,6 +1198,23 @@ int __connman_device_request_scan(enum connman_service_type type)
        return 0;
 }
 
+int __connman_device_request_hidden_scan(struct connman_device *device,
+                               const char *ssid, unsigned int ssid_len,
+                               const char *identity, const char *passphrase)
+{
+       DBG("device %p", device);
+
+       if (device == NULL || device->driver == NULL ||
+                       device->driver->scan_hidden == NULL)
+               return -EINVAL;
+
+       if (device->scanning == TRUE)
+               return -EALREADY;
+
+       return device->driver->scan_hidden(device, ssid, ssid_len,
+                                       identity, passphrase);
+}
+
 connman_bool_t __connman_device_isfiltered(const char *devname)
 {
        char **pattern;
@@ -1268,72 +1248,6 @@ nodevice:
        return FALSE;
 }
 
-static int device_load(struct connman_device *device)
-{
-       const char *ident = __connman_profile_active_ident();
-       GKeyFile *keyfile;
-       GError *error = NULL;
-       gchar *identifier;
-       connman_bool_t powered;
-
-       DBG("device %p", device);
-
-       keyfile = __connman_storage_open_profile(ident);
-       if (keyfile == NULL)
-               return 0;
-
-       identifier = g_strdup_printf("device_%s", device->name);
-       if (identifier == NULL)
-               goto done;
-
-       powered = g_key_file_get_boolean(keyfile, identifier,
-                                               "Powered", &error);
-       if (error == NULL)
-               device->powered_persistent = powered;
-       g_clear_error(&error);
-
-done:
-       g_free(identifier);
-
-       __connman_storage_close_profile(ident, keyfile, FALSE);
-
-       return 0;
-}
-
-static int device_save(struct connman_device *device)
-{
-       const char *ident = __connman_profile_active_ident();
-       GKeyFile *keyfile;
-       gchar *identifier;
-
-       DBG("device %p", device);
-
-       keyfile = __connman_storage_open_profile(ident);
-       if (keyfile == NULL)
-               return 0;
-
-       identifier = g_strdup_printf("device_%s", device->name);
-       if (identifier == NULL)
-               goto done;
-
-       g_key_file_set_boolean(keyfile, identifier,
-                                       "Powered", device->powered_persistent);
-
-done:
-       g_free(identifier);
-
-       __connman_storage_close_profile(ident, keyfile, TRUE);
-
-       return 0;
-}
-
-static struct connman_storage device_storage = {
-       .name           = "device",
-       .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
-       .device_load    = device_load,
-       .device_save    = device_save,
-};
-
 int __connman_device_init(const char *device, const char *nodevice)
 {
        DBG("");
@@ -1344,7 +1258,7 @@ int __connman_device_init(const char *device, const char *nodevice)
        if (nodevice != NULL)
                nodevice_filter = g_strsplit(nodevice, ",", -1);
 
-       return connman_storage_register(&device_storage);
+       return 0;
 }
 
 void __connman_device_cleanup(void)
@@ -1353,6 +1267,4 @@ void __connman_device_cleanup(void)
 
        g_strfreev(nodevice_filter);
        g_strfreev(device_filter);
-
-       connman_storage_unregister(&device_storage);
 }