Add support for automatic connection policy
[connman] / src / device.c
index 85a6d85..b4a4b0f 100644 (file)
@@ -33,11 +33,13 @@ struct connman_device {
        enum connman_device_type type;
        enum connman_device_mode mode;
        enum connman_device_policy policy;
-       gboolean powered;
-       gboolean carrier;
-       gboolean scanning;
-       char *path;
+       connman_bool_t powered;
+       connman_bool_t carrier;
+       connman_bool_t scanning;
+       char *name;
+       char *node;
        char *interface;
+       unsigned int connections;
 
        struct connman_device_driver *driver;
        void *driver_data;
@@ -127,7 +129,7 @@ static enum connman_device_policy string2policy(const char *policy)
                return CONNMAN_DEVICE_POLICY_UNKNOWN;
 }
 
-static int set_powered(struct connman_device *device, gboolean powered)
+static int set_powered(struct connman_device *device, connman_bool_t powered)
 {
        struct connman_device_driver *driver = device->driver;
        int err;
@@ -320,7 +322,7 @@ static DBusMessage *set_property(DBusConnection *conn,
                return __connman_error_permission_denied(msg);
 
        if (g_str_equal(name, "Powered") == TRUE) {
-               gboolean powered;
+               connman_bool_t powered;
                int err;
 
                dbus_message_iter_get_basic(&value, &powered);
@@ -346,7 +348,7 @@ static DBusMessage *set_property(DBusConnection *conn,
                        return __connman_error_failed(msg);
        }
 
-       __connman_element_store(&device->element);
+       __connman_storage_save_device(device);
 
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
@@ -393,6 +395,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);
@@ -583,7 +588,7 @@ static void remove_driver(struct connman_element *element, gpointer user_data)
 
 connman_bool_t __connman_device_has_driver(struct connman_device *device)
 {
-       if (device->driver == NULL)
+       if (device == NULL || device->driver == NULL)
                return FALSE;
 
        return device->registered;
@@ -653,7 +658,8 @@ static void device_destruct(struct connman_element *element)
 
        DBG("element %p name %s", element, element->name);
 
-       g_free(device->path);
+       g_free(device->node);
+       g_free(device->name);
        g_free(device->interface);
 
        g_hash_table_destroy(device->networks);
@@ -744,19 +750,14 @@ void connman_device_unref(struct connman_device *device)
 }
 
 /**
- * connman_device_set_path:
+ * connman_device_get_name:
  * @device: device structure
- * @path: path name
  *
- * Set path name of device
+ * Get unique name of device
  */
-void connman_device_set_path(struct connman_device *device, const char *path)
+const char *connman_device_get_name(struct connman_device *device)
 {
-       g_free(device->element.devpath);
-       device->element.devpath = g_strdup(path);
-
-       g_free(device->path);
-       device->path = g_strdup(path);
+       return device->element.name;
 }
 
 /**
@@ -767,7 +768,7 @@ void connman_device_set_path(struct connman_device *device, const char *path)
  */
 const char *connman_device_get_path(struct connman_device *device)
 {
-       return device->path;
+       return device->element.path;
 }
 
 /**
@@ -884,6 +885,15 @@ int connman_device_set_powered(struct connman_device *device,
 
        g_dbus_send_message(connection, signal);
 
+       if (powered == FALSE)
+               return 0;
+
+       if (device->policy != CONNMAN_DEVICE_POLICY_AUTO)
+               return 0;
+
+       if (device->driver->scan)
+               device->driver->scan(device);
+
        return 0;
 }
 
@@ -932,6 +942,11 @@ int connman_device_set_carrier(struct connman_device *device,
        return 0;
 }
 
+static void connect_known_network(struct connman_device *device)
+{
+       DBG("device %p", device);
+}
+
 /**
  * connman_device_set_scanning:
  * @device: device structure
@@ -972,10 +987,75 @@ int connman_device_set_scanning(struct connman_device *device,
 
        g_dbus_send_message(connection, signal);
 
+       if (scanning == TRUE)
+               return 0;
+
+       if (device->connections > 0)
+               return 0;
+
+       if (device->policy != CONNMAN_DEVICE_POLICY_AUTO)
+               return 0;
+
+       connect_known_network(device);
+
        return 0;
 }
 
 /**
+ * connman_device_set_string:
+ * @device: device structure
+ * @key: unique identifier
+ * @value: string value
+ *
+ * Set string value for specific key
+ */
+int connman_device_set_string(struct connman_device *device,
+                                       const char *key, const char *value)
+{
+       DBG("device %p key %s value %s", device, key, value);
+
+       if (g_str_equal(key, "Name") == TRUE) {
+               g_free(device->name);
+               device->name = g_strdup(value);
+       } else if (g_str_equal(key, "Node") == TRUE) {
+               g_free(device->node);
+               device->node = g_strdup(value);
+       }
+
+       return 0;
+}
+
+/**
+ * connman_device_get_string:
+ * @device: device structure
+ * @key: unique identifier
+ *
+ * Get string value for specific key
+ */
+const char *connman_device_get_string(struct connman_device *device,
+                                                       const char *key)
+{
+       DBG("device %p key %s", device);
+
+       if (g_str_equal(key, "Name") == TRUE)
+               return device->name;
+       else if (g_str_equal(key, "Node") == TRUE)
+               return device->node;
+
+       return NULL;
+}
+
+void __connman_device_increase_connections(struct connman_device *device)
+{
+       device->connections++;
+}
+
+void __connman_device_decrease_connections(struct connman_device *device)
+{
+       device->connections--;
+}
+
+/**
  * connman_device_add_network:
  * @device: device structure
  * @network: network structure
@@ -1160,9 +1240,99 @@ 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;
+       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_free(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)