Don't attempt scanning if device is switched off
[connman] / src / device.c
index 4696084..f481175 100644 (file)
@@ -42,6 +42,8 @@ struct connman_device {
        struct connman_device_driver *driver;
        void *driver_data;
 
+       connman_bool_t registered;
+
        GHashTable *networks;
 };
 
@@ -60,7 +62,9 @@ static const char *type2description(enum connman_device_type type)
        case CONNMAN_DEVICE_TYPE_BLUETOOTH:
                return "Bluetooth";
        case CONNMAN_DEVICE_TYPE_HSO:
+       case CONNMAN_DEVICE_TYPE_NOZOMI:
        case CONNMAN_DEVICE_TYPE_HUAWEI:
+       case CONNMAN_DEVICE_TYPE_NOVATEL:
                return "Cellular";
        }
 
@@ -83,6 +87,8 @@ static const char *type2string(enum connman_device_type type)
                return "bluetooth";
        case CONNMAN_DEVICE_TYPE_HSO:
        case CONNMAN_DEVICE_TYPE_HUAWEI:
+       case CONNMAN_DEVICE_TYPE_NOZOMI:
+       case CONNMAN_DEVICE_TYPE_NOVATEL:
                return "cellular";
        }
 
@@ -387,6 +393,9 @@ static DBusMessage *propose_scan(DBusConnection *conn,
        if (!device->driver || !device->driver->scan)
                return __connman_error_not_supported(msg);
 
+       if (device->powered == FALSE)
+               return __connman_error_failed(msg);
+
        err = device->driver->scan(device);
        if (err < 0)
                return __connman_error_failed(msg);
@@ -460,6 +469,8 @@ static int register_interface(struct connman_element *element)
                return -EIO;
        }
 
+       device->registered = TRUE;
+
        emit_devices_signal();
 
        return 0;
@@ -467,14 +478,120 @@ static int register_interface(struct connman_element *element)
 
 static void unregister_interface(struct connman_element *element)
 {
+       struct connman_device *device = element->device;
+
        DBG("element %p name %s", element, element->name);
 
+       device->registered = FALSE;
+
        emit_devices_signal();
 
        g_dbus_unregister_interface(connection, element->path,
                                                CONNMAN_DEVICE_INTERFACE);
 }
 
+static void device_enable(struct connman_device *device)
+{
+       DBG("device %p", device);
+
+       if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE ||
+                               device->policy == CONNMAN_DEVICE_POLICY_OFF)
+               return;
+
+       if (device->powered == TRUE)
+               return;
+
+       if (device->driver->enable)
+               device->driver->enable(device);
+}
+
+static void device_disable(struct connman_device *device)
+{
+       DBG("device %p", device);
+
+       if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE)
+               return;
+
+       if (device->powered == FALSE)
+               return;
+
+       g_hash_table_remove_all(device->networks);
+
+       if (device->driver->disable)
+               device->driver->disable(device);
+}
+
+static int setup_device(struct connman_device *device)
+{
+       int err;
+
+       DBG("device %p", device);
+
+       err = register_interface(&device->element);
+       if (err < 0) {
+               if (device->driver->remove)
+                       device->driver->remove(device);
+               device->driver = NULL;
+               return err;
+       }
+
+       device_enable(device);
+
+       return 0;
+}
+
+static void probe_driver(struct connman_element *element, gpointer user_data)
+{
+       struct connman_device_driver *driver = user_data;
+
+       DBG("element %p name %s", element, element->name);
+
+       if (element->device == NULL)
+               return;
+
+       if (driver->probe(element->device) < 0)
+               return;
+
+       element->device->driver = driver;
+
+       setup_device(element->device);
+}
+
+static void remove_device(struct connman_device *device)
+{
+       DBG("device %p", device);
+
+       device_disable(device);
+
+       unregister_interface(&device->element);
+
+       if (device->driver->remove)
+               device->driver->remove(device);
+
+       device->driver = NULL;
+}
+
+static void remove_driver(struct connman_element *element, gpointer user_data)
+{
+       struct connman_device_driver *driver = user_data;
+
+       DBG("element %p name %s", element, element->name);
+
+       if (element->device == NULL)
+               return;
+
+       if (element->device->driver == driver)
+               remove_device(element->device);
+}
+
+connman_bool_t __connman_device_has_driver(struct connman_device *device)
+{
+       if (device->driver == NULL)
+               return FALSE;
+
+       return device->registered;
+}
+
 static GSList *driver_list = NULL;
 
 static gint compare_priority(gconstpointer a, gconstpointer b)
@@ -500,7 +617,8 @@ int connman_device_driver_register(struct connman_device_driver *driver)
        driver_list = g_slist_insert_sorted(driver_list, driver,
                                                        compare_priority);
 
-       //__connman_driver_rescan(&device_driver);
+       __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
+                                               probe_driver, driver);
 
        return 0;
 }
@@ -516,6 +634,9 @@ void connman_device_driver_unregister(struct connman_device_driver *driver)
        DBG("driver %p name %s", driver, driver->name);
 
        driver_list = g_slist_remove(driver_list, driver);
+
+       __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
+                                               remove_driver, driver);
 }
 
 static void unregister_network(gpointer data)
@@ -539,6 +660,7 @@ static void device_destruct(struct connman_element *element)
        g_free(device->interface);
 
        g_hash_table_destroy(device->networks);
+       device->networks = NULL;
 }
 
 /**
@@ -576,7 +698,9 @@ struct connman_device *connman_device_create(const char *node,
        case CONNMAN_DEVICE_TYPE_WIMAX:
        case CONNMAN_DEVICE_TYPE_BLUETOOTH:
        case CONNMAN_DEVICE_TYPE_HSO:
+       case CONNMAN_DEVICE_TYPE_NOZOMI:
        case CONNMAN_DEVICE_TYPE_HUAWEI:
+       case CONNMAN_DEVICE_TYPE_NOVATEL:
                device->element.subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
                break;
        case CONNMAN_DEVICE_TYPE_ETHERNET:
@@ -880,6 +1004,8 @@ int connman_device_add_network(struct connman_device *device,
 
        __connman_network_set_device(network, device);
 
+       __connman_storage_load_network(network);
+
        err = connman_element_register((struct connman_element *) network,
                                                        &device->element);
        if (err < 0) {
@@ -933,6 +1059,8 @@ int connman_device_remove_network(struct connman_device *device,
  */
 int connman_device_register(struct connman_device *device)
 {
+       __connman_storage_load_device(device);
+
        return connman_element_register(&device->element, NULL);
 }
 
@@ -944,6 +1072,8 @@ int connman_device_register(struct connman_device *device)
  */
 void connman_device_unregister(struct connman_device *device)
 {
+       __connman_storage_save_device(device);
+
        connman_element_unregister(&device->element);
 }
 
@@ -970,37 +1100,6 @@ void connman_device_set_data(struct connman_device *device, void *data)
        device->driver_data = data;
 }
 
-static void device_enable(struct connman_device *device)
-{
-       DBG("device %p", device);
-
-       if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE ||
-                               device->policy == CONNMAN_DEVICE_POLICY_OFF)
-               return;
-
-       if (device->powered == TRUE)
-               return;
-
-       if (device->driver->enable)
-               device->driver->enable(device);
-}
-
-static void device_disable(struct connman_device *device)
-{
-       DBG("device %p", device);
-
-       if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE)
-               return;
-
-       if (device->powered == FALSE)
-               return;
-
-       g_hash_table_remove_all(device->networks);
-
-       if (device->driver->disable)
-               device->driver->disable(device);
-}
-
 static gboolean match_driver(struct connman_device *device,
                                        struct connman_device_driver *driver)
 {
@@ -1015,7 +1114,6 @@ static int device_probe(struct connman_element *element)
 {
        struct connman_device *device = element->device;
        GSList *list;
-       int err;
 
        DBG("element %p name %s", element, element->name);
 
@@ -1039,16 +1137,7 @@ static int device_probe(struct connman_element *element)
        if (device->driver == NULL)
                return -ENODEV;
 
-       err = register_interface(element);
-       if (err < 0) {
-               if (device->driver->remove)
-                       device->driver->remove(device);
-               return err;
-       }
-
-       device_enable(device);
-
-       return 0;
+       return setup_device(device);
 }
 
 static void device_remove(struct connman_element *element)
@@ -1063,12 +1152,7 @@ static void device_remove(struct connman_element *element)
        if (device->driver == NULL)
                return;
 
-       device_disable(device);
-
-       unregister_interface(element);
-
-       if (device->driver->remove)
-               device->driver->remove(device);
+       remove_device(device);
 }
 
 static struct connman_driver device_driver = {
@@ -1079,12 +1163,108 @@ static struct connman_driver device_driver = {
        .remove         = device_remove,
 };
 
+static int device_load(struct connman_device *device)
+{
+       GKeyFile *keyfile;
+       gchar *pathname, *data = NULL;
+       gsize length;
+       const char *str;
+
+       DBG("device %p", device);
+
+       pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
+                                                       device->element.name);
+       if (pathname == NULL)
+               return -ENOMEM;
+
+       keyfile = g_key_file_new();
+
+       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
+               g_free(pathname);
+               return -ENOENT;
+       }
+
+       g_free(pathname);
+
+       if (g_key_file_load_from_data(keyfile, data, length,
+                                                       0, NULL) == FALSE) {
+               g_free(data);
+               return -EILSEQ;
+       }
+
+       g_free(data);
+
+       str = g_key_file_get_string(keyfile, "Configuration", "Policy", NULL);
+       if (str != NULL)
+               device->policy = string2policy(str);
+
+       g_key_file_free(keyfile);
+
+       return 0;
+}
+
+static int device_save(struct connman_device *device)
+{
+       GKeyFile *keyfile;
+       gchar *pathname, *data = NULL;
+       gsize length;
+       const char *str;
+
+       DBG("device %p", device);
+
+       pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
+                                                       device->element.name);
+       if (pathname == NULL)
+               return -ENOMEM;
+
+       keyfile = g_key_file_new();
+
+       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
+               goto update;
+
+       if (length > 0) {
+               if (g_key_file_load_from_data(keyfile, data, length,
+                                                       0, NULL) == FALSE)
+                       goto done;
+       }
+
+       g_free(data);
+
+update:
+       str = policy2string(device->policy);
+       if (str != NULL)
+               g_key_file_set_string(keyfile, "Configuration", "Policy", str);
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
+
+       g_file_set_contents(pathname, data, length, NULL);
+
+done:
+       g_free(data);
+
+       g_key_file_free(keyfile);
+
+       g_free(pathname);
+
+       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(void)
 {
        DBG("");
 
        connection = connman_dbus_get_connection();
 
+       if (connman_storage_register(&device_storage) < 0)
+               connman_error("Failed to register device storage");
+
        return connman_driver_register(&device_driver);
 }
 
@@ -1094,5 +1274,7 @@ void __connman_device_cleanup(void)
 
        connman_driver_unregister(&device_driver);
 
+       connman_storage_unregister(&device_storage);
+
        dbus_connection_unref(connection);
 }