summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2014-06-20 10:48:23 -0500
committerDan Williams <dcbw@redhat.com>2014-06-20 10:54:03 -0500
commit5a4e42bc5161a62de290d41e8cbfcbfc3144a54e (patch)
tree1c9c225f5a54e41d6692b127b7126ab4aa177be9 /examples
parent0d45284aa7de1f67fcff31e1888054ea5a96870c (diff)
downloadNetworkManager-5a4e42bc5161a62de290d41e8cbfcbfc3144a54e.tar.gz
examples: add Python dbus example indicating if WWAN is the default connection
Diffstat (limited to 'examples')
-rw-r--r--examples/python/dbus/Makefile.am3
-rwxr-xr-xexamples/python/dbus/is-wwan-default.py78
2 files changed, 80 insertions, 1 deletions
diff --git a/examples/python/dbus/Makefile.am b/examples/python/dbus/Makefile.am
index de269ab3f1..0e88488496 100644
--- a/examples/python/dbus/Makefile.am
+++ b/examples/python/dbus/Makefile.am
@@ -9,4 +9,5 @@ EXTRA_DIST = \
disconnect-device.py \
get-active-connection-uuids.py \
list-devices.py \
- update-ip4-method.py
+ update-ip4-method.py \
+ is-wwan-default.py
diff --git a/examples/python/dbus/is-wwan-default.py b/examples/python/dbus/is-wwan-default.py
new file mode 100755
index 0000000000..6131cda160
--- /dev/null
+++ b/examples/python/dbus/is-wwan-default.py
@@ -0,0 +1,78 @@
+#!/usr/bin/env python
+# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+#
+# 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) 2011 - 2012 Red Hat, Inc.
+#
+
+import dbus, sys
+
+# This example indicates whether the default network connection is known to be WWAN
+
+NM_DEVICE_TYPE_MODEM = 8
+NM_DEVICE_TYPE_BLUETOOTH = 5
+NM_SERVICE_NAME = "org.freedesktop.NetworkManager"
+NM_MANAGER_IFACE = "org.freedesktop.NetworkManager"
+DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties"
+
+bus = dbus.SystemBus()
+
+# Exit early if NetworkManager is not running
+proxy = bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus")
+busdaemon = dbus.Interface(proxy, "org.freedesktop.DBus")
+if not busdaemon.NameHasOwner(NM_SERVICE_NAME):
+ print "NetworkManager not running"
+ sys.exit(1)
+
+# Get a proxy for the NetworkManager object
+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
+
+# Look through all active network connections for the default one
+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")
+
+ # 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
+
+if default_is_wwan:
+ print "WWAN is default"
+else:
+ print "WWAN is not default"
+