Re: Change the state of iface from disconnected to connected using nmcli



On Thursday 22 of March 2012 11:10:56 Dan Williams wrote:
> On Sun, 2012-03-18 at 23:43 +0530, Abhijeet Rastogi wrote:
> > Hi,
> > 
> > 
> > This is my first post here. I have tried googling and looking at the
> > manpage but no help.
> > 
> > 
> > We can use
> > 
> > 
> > $nmcli dev disconnect iface eth0
> > 
> > 
> > to change the state of eth0 to disconnect. Is there a command to
> > change it's state back to connected? I tried looking at the manpage &
> > I can't seem to find  any option for that.
> 
> At the moment, you need to tell NM to reconnect to something, eg 'nmcli
> con up uuid <uuid>'.  Disconnect places the device into manual mode
> which requires user action to return to automatic mode.  I suppose we
> can add an 'autoconnect' property to each device, which Disconnect()
> would set to false, but which could be changed via the D-Bus properties
> interface (authenticated of course) back to TRUE.  This property would
> follow the internal 'autoconnect_inhibit' device member variable and the
> NMPolicy would listen for changes to this variable and retrigger an auto
> connection check if it changes back to TRUE.  Patches accepted for that.
> 
> Dan
>

Attached are patches adding 'Autoconnect' property to NMDevice for core NM
and libnm-glib as well.

Jirka
>From 4f1ce8b39eb8c722bc57fc853c6b381b90b2294f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes redhat com>
Date: Mon, 14 May 2012 15:32:54 +0200
Subject: [PATCH 1/2] core: add "Autoconnect" property to NMDevice
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It is bound to autoconnect_inhibit private variable (has opposite meaning).
While 'Autoconnect' is TRUE (default value) the device can automatically
activate a connection. If it is changed to FALSE, the device will not
auto-activate until 'Autoconnect' is TRUE again.
Disconnect() method sets 'Autoconnect' to FALSE. NMPolicy monitors the property
and schedules auto activation when FALSE->TRUE transition is made.

Signed-off-by: Jiří Klimeš <jklimes redhat com>
---
 introspection/nm-device.xml |    6 ++++++
 src/nm-device.c             |   28 +++++++++++++++++++++++++++-
 src/nm-device.h             |    4 +++-
 src/nm-policy.c             |   12 +++++++++++-
 4 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/introspection/nm-device.xml b/introspection/nm-device.xml
index d12d477..4b155d5 100644
--- a/introspection/nm-device.xml
+++ b/introspection/nm-device.xml
@@ -91,6 +91,12 @@
         Whether or not this device is managed by NetworkManager.
       </tp:docstring>
     </property>
+    <property name="Autoconnect" type="b" access="readwrite">
+      <tp:docstring>
+        If TRUE, indicates the device can autoconnect.  If FALSE, a manual intervention is required before the device
+        is allowed to autoconnect again, like activating a connection on the device or restarting Networkmanager.
+      </tp:docstring>
+    </property>
     <property name="FirmwareMissing" type="b" access="read">
       <tp:docstring>
         If TRUE, indicates the device is likely missing firmware necessary for
diff --git a/src/nm-device.c b/src/nm-device.c
index 2f69be6..175d044 100644
--- a/src/nm-device.c
+++ b/src/nm-device.c
@@ -111,6 +111,7 @@ enum {
 	PROP_ACTIVE_CONNECTION,
 	PROP_DEVICE_TYPE,
 	PROP_MANAGED,
+	PROP_AUTOCONNECT,
 	PROP_FIRMWARE_MISSING,
 	PROP_TYPE_DESC,
 	PROP_RFKILL_TYPE,
@@ -3759,6 +3760,9 @@ set_property (GObject *object, guint prop_id,
 	case PROP_MANAGED:
 		priv->managed = g_value_get_boolean (value);
 		break;
+	case PROP_AUTOCONNECT:
+		priv->autoconnect_inhibit = !g_value_get_boolean (value);
+		break;
 	case PROP_FIRMWARE_MISSING:
 		priv->firmware_missing = g_value_get_boolean (value);
 		break;
@@ -3867,6 +3871,9 @@ get_property (GObject *object, guint prop_id,
 	case PROP_MANAGED:
 		g_value_set_boolean (value, priv->managed);
 		break;
+	case PROP_AUTOCONNECT:
+		g_value_set_boolean (value, !priv->autoconnect_inhibit);
+		break;
 	case PROP_FIRMWARE_MISSING:
 		g_value_set_boolean (value, priv->firmware_missing);
 		break;
@@ -4028,6 +4035,14 @@ nm_device_class_init (NMDeviceClass *klass)
 		                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_object_class_install_property
+		(object_class, PROP_AUTOCONNECT,
+		 g_param_spec_boolean (NM_DEVICE_AUTOCONNECT,
+		                       "Autoconnect",
+		                       "Autoconnect",
+		                       TRUE,
+		                       G_PARAM_READWRITE));
+
+	g_object_class_install_property
 		(object_class, PROP_FIRMWARE_MISSING,
 		 g_param_spec_boolean (NM_DEVICE_FIRMWARE_MISSING,
 		                       "FirmwareMissing",
@@ -4655,7 +4670,6 @@ nm_device_set_dhcp_anycast_address (NMDevice *device, guint8 *addr)
 	}
 }
 
-
 void
 nm_device_clear_autoconnect_inhibit (NMDevice *device)
 {
@@ -4664,3 +4678,15 @@ nm_device_clear_autoconnect_inhibit (NMDevice *device)
 	priv->autoconnect_inhibit = FALSE;
 }
 
+gboolean
+nm_device_get_autoconnect (NMDevice *device)
+{
+	NMDevicePrivate *priv;
+
+	g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
+
+	priv = NM_DEVICE_GET_PRIVATE (device);
+
+	return !NM_DEVICE_GET_PRIVATE (device)->autoconnect_inhibit;
+}
+
diff --git a/src/nm-device.h b/src/nm-device.h
index f4495b9..2de9f64 100644
--- a/src/nm-device.h
+++ b/src/nm-device.h
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2005 - 2011 Red Hat, Inc.
+ * Copyright (C) 2005 - 2012 Red Hat, Inc.
  * Copyright (C) 2006 - 2008 Novell, Inc.
  */
 
@@ -52,6 +52,7 @@
 #define NM_DEVICE_ACTIVE_CONNECTION "active-connection"
 #define NM_DEVICE_DEVICE_TYPE      "device-type" /* ugh */
 #define NM_DEVICE_MANAGED          "managed"
+#define NM_DEVICE_AUTOCONNECT      "autoconnect"
 #define NM_DEVICE_FIRMWARE_MISSING "firmware-missing"
 #define NM_DEVICE_TYPE_DESC        "type-desc"    /* Internal only */
 #define NM_DEVICE_RFKILL_TYPE      "rfkill-type"  /* Internal only */
@@ -245,6 +246,7 @@ void nm_device_set_managed (NMDevice *device,
                             NMDeviceStateReason reason);
 
 void nm_device_clear_autoconnect_inhibit (NMDevice *device);
+gboolean nm_device_get_autoconnect (NMDevice *device);
 
 void nm_device_handle_autoip4_event (NMDevice *self,
                                      const char *event,
diff --git a/src/nm-policy.c b/src/nm-policy.c
index babf164..16cd117 100644
--- a/src/nm-policy.c
+++ b/src/nm-policy.c
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2011 Red Hat, Inc.
+ * Copyright (C) 2004 - 2012 Red Hat, Inc.
  * Copyright (C) 2007 - 2008 Novell, Inc.
  */
 
@@ -1127,6 +1127,15 @@ device_ip_config_changed (NMDevice *device,
 }
 
 static void
+device_autoconnect_changed (NMDevice *device,
+                            GParamSpec *pspec,
+                            gpointer user_data)
+{
+	if (nm_device_get_autoconnect (device))
+		schedule_activate_check ((NMPolicy *) user_data, device, 0);
+}
+
+static void
 wireless_networks_changed (NMDevice *device, GObject *ap, gpointer user_data)
 {
 	schedule_activate_check ((NMPolicy *) user_data, device, 0);
@@ -1169,6 +1178,7 @@ device_added (NMManager *manager, NMDevice *device, gpointer user_data)
 	_connect_device_signal (policy, device, "state-changed", device_state_changed);
 	_connect_device_signal (policy, device, "notify::" NM_DEVICE_IP4_CONFIG, device_ip_config_changed);
 	_connect_device_signal (policy, device, "notify::" NM_DEVICE_IP6_CONFIG, device_ip_config_changed);
+	_connect_device_signal (policy, device, "notify::" NM_DEVICE_AUTOCONNECT, device_autoconnect_changed);
 
 	switch (nm_device_get_device_type (device)) {
 	case NM_DEVICE_TYPE_WIFI:
-- 
1.7.7.6

>From eab7a33cdef5de6974ead6a7352555c4328c74e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes redhat com>
Date: Mon, 14 May 2012 17:09:36 +0200
Subject: [PATCH 2/2] libnm-glib: add 'autoconnect' property for devices
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Signed-off-by: Jiří Klimeš <jklimes redhat com>
---
 libnm-glib/libnm-glib.ver |    2 +
 libnm-glib/nm-device.c    |   68 +++++++++++++++++++++++++++++++++++++++++++++
 libnm-glib/nm-device.h    |    3 ++
 3 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/libnm-glib/libnm-glib.ver b/libnm-glib/libnm-glib.ver
index e5513d7..1bf5441 100644
--- a/libnm-glib/libnm-glib.ver
+++ b/libnm-glib/libnm-glib.ver
@@ -84,6 +84,7 @@ global:
 	nm_device_ethernet_new;
 	nm_device_filter_connections;
 	nm_device_get_active_connection;
+	nm_device_get_autoconnect;
 	nm_device_get_capabilities;
 	nm_device_get_device_type;
 	nm_device_get_dhcp4_config;
@@ -120,6 +121,7 @@ global:
 	nm_device_olpc_mesh_get_hw_address;
 	nm_device_olpc_mesh_get_type;
 	nm_device_olpc_mesh_new;
+	nm_device_set_autoconnect;
 	nm_device_vlan_error_get_type;
 	nm_device_vlan_error_quark;
 	nm_device_vlan_get_carrier;
diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c
index 5346b9d..93a57c2 100644
--- a/libnm-glib/nm-device.c
+++ b/libnm-glib/nm-device.c
@@ -70,6 +70,7 @@ typedef struct {
 	NMDeviceCapabilities capabilities;
 	gboolean managed;
 	gboolean firmware_missing;
+	gboolean autoconnect;
 	NMIP4Config *ip4_config;
 	NMDHCP4Config *dhcp4_config;
 	NMIP6Config *ip6_config;
@@ -91,6 +92,7 @@ enum {
 	PROP_DRIVER,
 	PROP_CAPABILITIES,
 	PROP_MANAGED,
+	PROP_AUTOCONNECT,
 	PROP_FIRMWARE_MISSING,
 	PROP_IP4_CONFIG,
 	PROP_DHCP4_CONFIG,
@@ -153,6 +155,7 @@ register_properties (NMDevice *device)
 		{ NM_DEVICE_DRIVER,            &priv->driver },
 		{ NM_DEVICE_CAPABILITIES,      &priv->capabilities },
 		{ NM_DEVICE_MANAGED,           &priv->managed },
+		{ NM_DEVICE_AUTOCONNECT,       &priv->autoconnect },
 		{ NM_DEVICE_FIRMWARE_MISSING,  &priv->firmware_missing },
 		{ NM_DEVICE_IP4_CONFIG,        &priv->ip4_config, NULL, NM_TYPE_IP4_CONFIG },
 		{ NM_DEVICE_DHCP4_CONFIG,      &priv->dhcp4_config, NULL, NM_TYPE_DHCP4_CONFIG },
@@ -323,6 +326,9 @@ get_property (GObject *object,
 	case PROP_MANAGED:
 		g_value_set_boolean (value, nm_device_get_managed (device));
 		break;
+	case PROP_AUTOCONNECT:
+		g_value_set_boolean (value, nm_device_get_autoconnect (device));
+		break;
 	case PROP_FIRMWARE_MISSING:
 		g_value_set_boolean (value, nm_device_get_firmware_missing (device));
 		break;
@@ -372,12 +378,18 @@ set_property (GObject *object,
 {
 	NMDevice *self = NM_DEVICE (object);
 	NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self);
+	gboolean b;
 
 	switch (prop_id) {
 	case PROP_DEVICE_TYPE:
 		/* Construct only */
 		priv->device_type = g_value_get_uint (value);
 		break;
+	case PROP_AUTOCONNECT:
+		b = g_value_get_boolean (value);
+		if (priv->autoconnect != b)
+			nm_device_set_autoconnect (NM_DEVICE (object), b);
+		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 		break;
@@ -497,6 +509,19 @@ nm_device_class_init (NMDeviceClass *device_class)
 						  G_PARAM_READABLE));
 
 	/**
+	 * NMDevice:autoconnect:
+	 *
+	 * Whether the device can auto-activate a connection.
+	 **/
+	g_object_class_install_property
+		(object_class, PROP_AUTOCONNECT,
+		 g_param_spec_boolean (NM_DEVICE_AUTOCONNECT,
+		                       "Autoconnect",
+		                       "Autoconnect",
+		                       TRUE,
+		                       G_PARAM_READWRITE));
+
+	/**
 	 * NMDevice:firmware-missing:
 	 *
 	 * When %TRUE indicates the device is likely missing firmware required
@@ -915,6 +940,49 @@ nm_device_get_managed (NMDevice *device)
 }
 
 /**
+ * nm_device_get_autoconnect:
+ * @device: a #NMDevice
+ *
+ * Whether the #NMDevice can be autoconnected.
+ *
+ * Returns: %TRUE if the device is allowed to be autoconnected
+ **/
+gboolean
+nm_device_get_autoconnect (NMDevice *device)
+{
+	g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
+
+	_nm_object_ensure_inited (NM_OBJECT (device));
+	return NM_DEVICE_GET_PRIVATE (device)->autoconnect;
+}
+
+/**
+ * nm_device_set_autoconnect:
+ * @device: a #NMDevice
+ * @autoconnect: %TRUE to enable autoconnecting
+ *
+ * Enables or disables automatic activation of the #NMDevice.
+ **/
+void
+nm_device_set_autoconnect (NMDevice *device, gboolean autoconnect)
+{
+	GValue value = {0,};
+
+	g_return_if_fail (NM_IS_DEVICE (device));
+
+	g_value_init (&value, G_TYPE_BOOLEAN);
+	g_value_set_boolean (&value, autoconnect);
+
+
+	NM_DEVICE_GET_PRIVATE (device)->autoconnect = autoconnect;
+
+	_nm_object_set_property (NM_OBJECT (device),
+	                         NM_DBUS_INTERFACE_DEVICE,
+	                         "Autoconnect",
+	                         &value);
+}
+
+/**
  * nm_device_get_firmware_missing:
  * @device: a #NMDevice
  *
diff --git a/libnm-glib/nm-device.h b/libnm-glib/nm-device.h
index 50301dc..63049c6 100644
--- a/libnm-glib/nm-device.h
+++ b/libnm-glib/nm-device.h
@@ -52,6 +52,7 @@ G_BEGIN_DECLS
 #define NM_DEVICE_DRIVER "driver"
 #define NM_DEVICE_CAPABILITIES "capabilities"
 #define NM_DEVICE_MANAGED "managed"
+#define NM_DEVICE_AUTOCONNECT "autoconnect"
 #define NM_DEVICE_FIRMWARE_MISSING "firmware-missing"
 #define NM_DEVICE_IP4_CONFIG "ip4-config"
 #define NM_DEVICE_DHCP4_CONFIG "dhcp4-config"
@@ -100,6 +101,8 @@ const char *         nm_device_get_udi              (NMDevice *device);
 const char *         nm_device_get_driver           (NMDevice *device);
 NMDeviceCapabilities nm_device_get_capabilities     (NMDevice *device);
 gboolean             nm_device_get_managed          (NMDevice *device);
+gboolean             nm_device_get_autoconnect      (NMDevice *device);
+void                 nm_device_set_autoconnect      (NMDevice *device, gboolean autoconnect);
 gboolean             nm_device_get_firmware_missing (NMDevice *device);
 NMIP4Config *        nm_device_get_ip4_config       (NMDevice *device);
 NMDHCP4Config *      nm_device_get_dhcp4_config     (NMDevice *device);
-- 
1.7.7.6



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]