about summary refs log tree commit diff
path: root/examples/python/dbus
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2015-01-22 00:29:39 +0100
committerMichael Biebl <biebl@debian.org>2015-01-22 00:29:39 +0100
commit2c032d8f1c6292c1338a615e6ec40252889ba85c (patch)
tree1f77182220b2b0264288ba4a476ab47e5bc48716 /examples/python/dbus
parent33491bc4279481db8ae47213e34a6d695a0e8830 (diff)
Imported Upstream version 1.0.0 upstream/1.0.0
Diffstat (limited to 'examples/python/dbus')
-rw-r--r--examples/python/dbus/Makefile.am1
-rw-r--r--examples/python/dbus/Makefile.in7
-rwxr-xr-xexamples/python/dbus/add-connection-compat.py66
-rwxr-xr-xexamples/python/dbus/add-connection.py24
-rwxr-xr-xexamples/python/dbus/is-wwan-default.py58
-rwxr-xr-xexamples/python/dbus/update-ip4-method.py33
6 files changed, 143 insertions, 46 deletions
diff --git a/examples/python/dbus/Makefile.am b/examples/python/dbus/Makefile.am
index 0e884884..dd77f1e9 100644
--- a/examples/python/dbus/Makefile.am
+++ b/examples/python/dbus/Makefile.am
@@ -1,6 +1,7 @@
 EXTRA_DIST = \
 	nm-state.py \
 	add-connection.py \
+	add-connection-compat.py \
 	add-system-wifi-connection.py \
 	vpn.py \
 	update-secrets.py \
diff --git a/examples/python/dbus/Makefile.in b/examples/python/dbus/Makefile.in
index 7b543fa7..a505993c 100644
--- a/examples/python/dbus/Makefile.in
+++ b/examples/python/dbus/Makefile.in
@@ -129,6 +129,8 @@ AUTOCONF = @AUTOCONF@
 AUTOHEADER = @AUTOHEADER@
 AUTOMAKE = @AUTOMAKE@
 AWK = @AWK@
+BLUEZ5_CFLAGS = @BLUEZ5_CFLAGS@
+BLUEZ5_LIBS = @BLUEZ5_LIBS@
 CC = @CC@
 CCDEPMODE = @CCDEPMODE@
 CFLAGS = @CFLAGS@
@@ -252,6 +254,7 @@ NEWT_CFLAGS = @NEWT_CFLAGS@
 NEWT_LIBS = @NEWT_LIBS@
 NM = @NM@
 NMEDIT = @NMEDIT@
+NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT = @NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT@
 NM_MAJOR_VERSION = @NM_MAJOR_VERSION@
 NM_MICRO_VERSION = @NM_MICRO_VERSION@
 NM_MINOR_VERSION = @NM_MINOR_VERSION@
@@ -271,6 +274,7 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@
 PACKAGE_URL = @PACKAGE_URL@
 PACKAGE_VERSION = @PACKAGE_VERSION@
 PATH_SEPARATOR = @PATH_SEPARATOR@
+PERL = @PERL@
 PKG_CONFIG = @PKG_CONFIG@
 PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@
 PKG_CONFIG_PATH = @PKG_CONFIG_PATH@
@@ -297,7 +301,7 @@ SYSTEMD_INHIBIT_LIBS = @SYSTEMD_INHIBIT_LIBS@
 SYSTEMD_LOGIN_CFLAGS = @SYSTEMD_LOGIN_CFLAGS@
 SYSTEMD_LOGIN_LIBS = @SYSTEMD_LOGIN_LIBS@
 SYSTEM_CA_PATH = @SYSTEM_CA_PATH@
-UDEV_BASE_DIR = @UDEV_BASE_DIR@
+UDEV_DIR = @UDEV_DIR@
 USE_NLS = @USE_NLS@
 UUID_CFLAGS = @UUID_CFLAGS@
 UUID_LIBS = @UUID_LIBS@
@@ -379,6 +383,7 @@ with_valgrind = @with_valgrind@
 EXTRA_DIST = \
 	nm-state.py \
 	add-connection.py \
+	add-connection-compat.py \
 	add-system-wifi-connection.py \
 	vpn.py \
 	update-secrets.py \
diff --git a/examples/python/dbus/add-connection-compat.py b/examples/python/dbus/add-connection-compat.py
new file mode 100755
index 00000000..4792ac5e
--- /dev/null
+++ b/examples/python/dbus/add-connection-compat.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2010 - 2012 Red Hat, Inc.
+#
+
+#
+# This example adds a new ethernet connection via the AddConnection()
+# D-Bus call, using backward-compatible settings, so it will work with
+# both old and new versions of NetworkManager. Compare
+# add-connection.py, which only supports NM 1.0 and later.
+#
+# Configuration settings are described at
+# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
+#
+
+import socket, struct, dbus, uuid
+
+# Helper functions
+def ip_to_int(ip_string):
+    return struct.unpack("=I", socket.inet_aton(ip_string))[0]
+
+def int_to_ip(ip_int):
+    return socket.inet_ntoa(struct.pack("=I", ip_int))
+
+s_wired = dbus.Dictionary({'duplex': 'full'})
+s_con = dbus.Dictionary({
+            'type': '802-3-ethernet',
+            'uuid': str(uuid.uuid4()),
+            'id': 'MyConnectionExample'})
+
+addr1 = dbus.Array([ip_to_int("10.1.2.3"), dbus.UInt32(8L), ip_to_int("10.1.2.1")], signature=dbus.Signature('u'))
+s_ip4 = dbus.Dictionary({
+            'addresses': dbus.Array([addr1], signature=dbus.Signature('au')),
+            'method': 'manual'})
+
+s_ip6 = dbus.Dictionary({'method': 'ignore'})
+
+con = dbus.Dictionary({
+    '802-3-ethernet': s_wired,
+    'connection': s_con,
+    'ipv4': s_ip4,
+    'ipv6': s_ip6})
+
+
+print "Creating connection:", s_con['id'], "-", s_con['uuid']
+
+bus = dbus.SystemBus()
+proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
+settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
+
+settings.AddConnection(con)
+
diff --git a/examples/python/dbus/add-connection.py b/examples/python/dbus/add-connection.py
index 6df14c10..5ebb82a3 100755
--- a/examples/python/dbus/add-connection.py
+++ b/examples/python/dbus/add-connection.py
@@ -18,20 +18,17 @@
 #
 
 #
-# This example adds a new ethernet connection via AddConnection() D-Bus call.
+# This example adds a new ethernet connection via the AddConnection()
+# D-Bus call, using the new 'ipv4.address-data' and 'ipv4.gateway'
+# settings introduced in NetworkManager 1.0. Compare
+# add-connection-compat.py, which will work against older versions of
+# NetworkManager as well.
 #
 # Configuration settings are described at
-# https://developer.gnome.org/NetworkManager/0.9/ref-settings.html
+# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
 #
 
-import socket, struct, dbus, uuid
-
-# Helper functions
-def ip_to_int(ip_string):
-    return struct.unpack("=I", socket.inet_aton(ip_string))[0]
-
-def int_to_ip(ip_int):
-    return socket.inet_ntoa(struct.pack("=I", ip_int))
+import dbus, uuid
 
 s_wired = dbus.Dictionary({'duplex': 'full'})
 s_con = dbus.Dictionary({
@@ -39,9 +36,12 @@ s_con = dbus.Dictionary({
             'uuid': str(uuid.uuid4()),
             'id': 'MyConnectionExample'})
 
-addr1 = dbus.Array([ip_to_int("10.1.2.3"), dbus.UInt32(8L), ip_to_int("10.1.2.1")], signature=dbus.Signature('u'))
+addr1 = dbus.Dictionary({
+    'address': '10.1.2.3',
+    'prefix': dbus.UInt32(8L)})
 s_ip4 = dbus.Dictionary({
-            'addresses': dbus.Array([addr1], signature=dbus.Signature('au')),
+            'address-data': dbus.Array([addr1], signature=dbus.Signature('a{sv}')),
+            'gateway': '10.1.2.1',
             'method': 'manual'})
 
 s_ip6 = dbus.Dictionary({'method': 'ignore'})
diff --git a/examples/python/dbus/is-wwan-default.py b/examples/python/dbus/is-wwan-default.py
index 6131cda1..a4a3ff8e 100755
--- a/examples/python/dbus/is-wwan-default.py
+++ b/examples/python/dbus/is-wwan-default.py
@@ -28,6 +28,8 @@ NM_SERVICE_NAME = "org.freedesktop.NetworkManager"
 NM_MANAGER_IFACE = "org.freedesktop.NetworkManager"
 DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"
 
+NM_ACTIVE_CONNECTION_INTERFACE = "org.freedesktop.NetworkManager.Connection.Active"
+
 bus = dbus.SystemBus()
 
 # Exit early if NetworkManager is not running
@@ -42,34 +44,54 @@ proxy = bus.get_object(NM_SERVICE_NAME, "/org/freedesktop/NetworkManager")
 manager = dbus.Interface(proxy, NM_MANAGER_IFACE)
 props = dbus.Interface(proxy, DBUS_PROPS_IFACE)
 
-default_is_wwan = False
+def found_connection_type(ctype):
+    if ctype == '':
+        print "No active connection"
+    elif ctype in ["gsm", "cdma", "bluetooth"]:
+        print "WWAN is default"
+    else:
+        print "WWAN is not default"
+        sys.exit(0)
+
+# Shortcut #1, for NM 1.0
+try:
+    ctype = props.Get(NM_MANAGER_IFACE, "PrimaryConnectionType")
+    found_connection_type(ctype)
+except KeyError:
+    pass
 
-# Look through all active network connections for the default one
+# Shortcut #2, for NM 0.9.10
+try:
+    primary = props.Get(NM_MANAGER_IFACE, "PrimaryConnection")
+    if not primary:
+        found_connection_type('')
+    primary_proxy = bus.get_object(NM_SERVICE_NAME, primary)
+    primary_props = dbus.Interface(primary_proxy, DBUS_PROPS_IFACE)
+    ctype = primary_props.Get(NM_ACTIVE_CONNECTION_INTERFACE, "Type")
+    found_connection_type(ctype)
+except KeyError:
+    pass
+
+# Fallback for NM 0.9.8 and earlier; look through all active network
+# connections for the default one
+default_is_wwan = False
 active = props.Get(NM_MANAGER_IFACE, "ActiveConnections")
 for a in active:
     a_proxy = bus.get_object(NM_SERVICE_NAME, a)
     a_props = dbus.Interface(a_proxy, DBUS_PROPS_IFACE)
-    all_props = a_props.GetAll("org.freedesktop.NetworkManager.Connection.Active")
+    all_props = a_props.GetAll(NM_ACTIVE_CONNECTION_INTERFACE)
 
     # Ignore this network connection if it's not default for IPv4 or IPv6
     if all_props["Default"] == False and all_props["Default6"] == False:
         continue
 
-    # Shortcut: check for Type property (only present in NM 0.9.9+)
-    try:
-        ctype = all_props["Type"]
-        if ctype in ["gsm", "cdma", "bluetooth"]:
-            default_is_wwan = True
-            break
-    except KeyError:
-        # Fall back to checking the type of the network connection's device
-        dev_path = all_props["Devices"][0]
-        dev_proxy = bus.get_object(NM_SERVICE_NAME, dev_path)
-        dev_props = dbus.Interface(dev_proxy, DBUS_PROPS_IFACE)
-        devtype = dev_props.Get("org.freedesktop.NetworkManager.Device", "DeviceType")
-        if devtype == NM_DEVICE_TYPE_MODEM or devtype == NM_DEVICE_TYPE_BLUETOOTH:
-            default_is_wwan = True
-            break
+    dev_path = all_props["Devices"][0]
+    dev_proxy = bus.get_object(NM_SERVICE_NAME, dev_path)
+    dev_props = dbus.Interface(dev_proxy, DBUS_PROPS_IFACE)
+    devtype = dev_props.Get("org.freedesktop.NetworkManager.Device", "DeviceType")
+    if devtype == NM_DEVICE_TYPE_MODEM or devtype == NM_DEVICE_TYPE_BLUETOOTH:
+        default_is_wwan = True
+        break
 
 if default_is_wwan:
     print "WWAN is default"
diff --git a/examples/python/dbus/update-ip4-method.py b/examples/python/dbus/update-ip4-method.py
index 243738c5..28a22289 100755
--- a/examples/python/dbus/update-ip4-method.py
+++ b/examples/python/dbus/update-ip4-method.py
@@ -21,15 +21,14 @@
 #
 # This example updates a connection's IPv4 method with the Update() method.
 #
+# This uses the new NM 1.0 setting properties. See add-connection-compat.py
+# for a similar example using the backward-compatible properties
+#
 # Configuration settings are described at
-# https://developer.gnome.org/NetworkManager/0.9/ref-settings.html
+# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
 #
 
-import socket, struct, dbus, sys
-
-def ip_to_int(ip_string):
-    return struct.unpack("=I", socket.inet_aton(ip_string))[0]
-
+import dbus, sys
 
 if len(sys.argv) < 3:
     print "Usage: %s <uuid> <auto|static> [address prefix gateway]" % sys.argv[0]
@@ -58,21 +57,25 @@ for c_path in settings.ListConnections():
         continue
 
     # add IPv4 setting if it doesn't yet exist
-    if not c_settings.has_key('ipv4'):
+    if 'ipv4' not in c_settings:
         c_settings['ipv4'] = {}
 
+    # clear existing address info
+    if c_settings['ipv4'].has_key('addresses'):
+        del c_settings['ipv4']['addresses']
+    if c_settings['ipv4'].has_key('address-data'):
+        del c_settings['ipv4']['address-data']
+    if c_settings['ipv4'].has_key('gateway'):
+        del c_settings['ipv4']['gateway']
+
     # set the method and change properties
     c_settings['ipv4']['method'] = method
-    if method == "auto":
-        # remove addresses
-    	c_settings['ipv4']['addresses'] = dbus.Array([], signature=dbus.Signature('au'))
-    elif method == "manual":
+    if method == "manual":
         # Add the static IP address, prefix, and (optional) gateway
-        gw = 0
+        addr = dbus.Dictionary({'address': sys.argv[3], 'prefix': dbus.UInt32(int(sys.argv[4]))})
+        c_settings['ipv4']['address-data'] = dbus.Array([addr], signature=dbus.Signature('a{sv}'))
         if len(sys.argv) == 6:
-            gw = ip_to_int(sys.argv[5])
-        addr = dbus.Array([ip_to_int(sys.argv[3]), dbus.UInt32(int(sys.argv[4])), gw], signature=dbus.Signature('u'))
-        c_settings['ipv4']['addresses'] = dbus.Array([addr], signature=dbus.Signature('au'))
+            c_settings['ipv4']['gateway'] = sys.argv[5]
 
     # Save all the updated settings back to NetworkManager
     c_obj.Update(c_settings)