Re: Detecting status of network interfaces



On Wed, 2010-02-10 at 20:32 +0800, Chen, Congwu wrote:
> Hello,
> We (SyncEvolution) want to detect whether the network interface is available
> (ethernet/wifi connected or Bluetooth adapter activated) so that we can show
> the synchronization profile based on whether the network is supported locally
> and we may later further incorporate automatic background synchronization. 

Sure.

> Here is what I found from Network manager DBus interface:
> 
> In spec V0.7 (which comes with Network Manager 0.7)
> http://projects.gnome.org/NetworkManager/developers/spec-07.html
> We may probably use:
> org.freedesktop.NetworkManager.GetDevices()
> signal:
> org.freedesktop.NetworkManager.DeviceAdded ( o: device_path ) 
> org.freedesktop.NetworkManager.Device.StateChanged ()
> property:
> org.freedesktop.NetworkManager.Device.State
> 
> Is the above API supposed to work for my intention?
> Is State == NM_DEVICE_STATE_ACTIVATED means network is connected?

There are two ways of doing what you want.  You can either ask NM about
the devices themselves, or you can ask NM about the "active
connections".  An active connection can span more than one device
(bonding for example, though we dont' support that quite yet).  So you
can either:

1) get the device list and check each device's "State" property.  If the
state is ACTIVATED, then you have a network connection that's managed by
NM.

2) get the active connection list, and check each active connection
object's "State" property.  If the state is
NM_ACTIVE_CONNECTION_STATE_ACTIVATED (2, see NetworkManager.h) then
there's an active network connection that's managed by NM.

I've included python examples of this below.  The output will look like
this:

Device eth0 is not activated
Device wlan0 is activated
Connection 'Home WiFI' is activated
Connection 'Work VPN' is activated

> Also it seems that Bluetooth devices were not supported in this version, does this mean
> I have to query Bluez directly to detect whether local Bluetooth device is activated?

Bluetooth device's aren't really supported in NM 0.7, but are in NM 0.8.
There are some workarounds for NM 0.7 (blueman) but these require extra
user configuration.  Might be worth looking at though.

> In spec V0.8 (which is the next version)
> http://projects.gnome.org/NetworkManager/developers/spec-08.html
> I see Bluetooth device is supported. However what is the release schedule? Does this
> mean I need to support both version DBus APIs to work with older distributions?

This week actually.  We just fixed on last but with 3G PIN handling
which was blocking the release.  For your purposes though, the D-Bus API
has not changed between NM 0.7 and 0.8, so the same code should work for
both.

Dan

-------

#!/bin/env python

import dbus

bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")

# Get device-specific state
devices = manager.GetDevices()
for d in devices:
    dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
    prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")

    # Get the device's current state and interface name
    state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
    name = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")

    # and print them out
    if state == 8:   # activated
        print "Device %s is activated" % name
    else:
        print "Device %s is not activated" % name


# Get active connection state
manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections")
for a in active:
    ac_proxy = bus.get_object("org.freedesktop.NetworkManager", a)
    prop_iface = dbus.Interface(ac_proxy, "org.freedesktop.DBus.Properties")
    state = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "State")

    # Connections in NM are a collection of settings that describe everything
    # needed to connect to a specific network.  Lets get those details so we
    # can find the user-readable name of the connection.
    con_path = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "Connection")
    con_service = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "ServiceName")

    # ask the provider of the connection for its details
    service_proxy = bus.get_object(con_service, con_path)
    con_iface = dbus.Interface(service_proxy, "org.freedesktop.NetworkManagerSettings.Connection")
    con_details = con_iface.GetSettings()
    con_name = con_details['connection']['id']

    if state == 2:   # activated
        print "Connection '%s' is activated" % con_name
    else:
        print "Connection '%s' is activating" % con_name





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