[Dextrose] NetworkManager API (was: Re: Some learning)

Ajay Garg ajaygargnsit at gmail.com
Fri Mar 9 12:09:35 EST 2012


SIr,
Regards.

Thanks a ton for the reply.

Thanks, Thanks, Thanks !!!

That solved it !!!

Following is the modified program, along with its output (just one quirk;
as can be seen from the output, occassionally there are (spurious)
'AccessPointAdded' signals received, *with a blank SSID*. Don't know what
the reason may be).


Anyways, here is the program (connection-activation logic excluded) and
output :

================================================================================
"""
LEARNINGS :

A system usually has only one (fixed) wireless device hardware.
Thus, no callbacks for addition/removal of this device are triggered.
(Can't thank you enough Sascha !!! :) :) )

However, this (single) hardware has the capacity to pair with multiple
access-points.

In Networkmanager (at least python APIs), each access-point is
represented by its path, which serves as its key. Using this key, other
properties (notably SSID) can be retreived.



For a wired device, it again is fixed; thus no addition/removal
callbacks (Thanks again Sascha). There is no concept of SSID; a wired
device is just
a medium for physical connection. Moreover, there is one-to-one
correspondence for a wired-device, and the network.
"""

"""
TESTS DONE ::

a. Additon/Removal of USB Modem triggers corresponding callbacks.
b. Addition/Removal of wireless access-points triggers callbacks.
"""


import dbus
from dbus.mainloop.glib import DBusGMainLoop

DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()

"""
The names have been designed, keeping in consistency of the
nomenclature used in "add_signal_receiver" method.
"""
NM_BUS_NAME                    = 'org.freedesktop.NetworkManager'
NM_PATH                        = '/org/freedesktop/NetworkManager'
NM_DBUS_INTERFACE              = 'org.freedesktop.NetworkManager'

"""
Note that the following is not specifically tied to Network Manager.
"""
DBUS_PROPS_DBUS_INTERFACE      = 'org.freedesktop.DBus.Properties'

"""
Some more :)
"""
NM_DEVICE_DBUS_INTERFACE       = 'org.freedesktop.NetworkManager.Device'
NM_WIRED_DEVICE_INTERFACE      =
'org.freedesktop.NetworkManager.Device.Wired'
NM_WIFI_DEVICE_INTERFACE       =
'org.freedesktop.NetworkManager.Device.Wireless'
NM_ACCESS_POINT_DBUS_INTERFACE =
'org.freedesktop.NetworkManager.AccessPoint'


class NMDbusPathProcessor:
    """
    This class encapsulates functions, that would be used for
    processing any networkmanager-dbus-path.
    """

    def get_nm_object(self, nm_dbus_path):
        """
        returns dbus-object for the nm-dbus-path.
        """
        return bus.get_object(NM_BUS_NAME, nm_dbus_path)

    def get_nm_props_interface(self, nm_dbus_path):
        """
        returns the interface, encapsulating the nm-dbus-path.
        """
        nm_object = self.get_nm_object(nm_dbus_path)
        return dbus.Interface(nm_object, DBUS_PROPS_DBUS_INTERFACE)


nm_dbus_path_processor = NMDbusPathProcessor()


class BaseTypeProcessor:
    """
    This class is a virtual class; each subtype of which implements a
    particular hierarchy of NetworkManager architecture. (or kind of :)
    )
    """

    def __init__(self, base_path):
        self._base_path = base_path
        self._base_obj = nm_dbus_path_processor.get_nm_object(base_path)


    def initiate(self):
        """
        Did not put in the __init__, as did not want to get into the
        dynamics of "use-before-initialization" problem.
        """
        self._base_interface = dbus.Interface(self._base_obj,

self.get_interface_identifier())

        """
        List the exiting things.
        """
        self.existing_things()


        """
        Add callbacks for runtime addition and removal.
        """
        bus.add_signal_receiver(self.thing_added_cb,

signal_name=self.get_thing_added_signal_name(),

dbus_interface=self.get_interface_identifier())
        bus.add_signal_receiver(self.thing_removed_cb,

signal_name=self.get_thing_removed_signal_name(),

dbus_interface=self.get_interface_identifier())

    def existing_things(self):
        """
        Returns the existing "things"
        For eg., as a starting point of networkmanager, "things" would
        be devices.
        For a wireless device, "things" would be access-points.
        """
        all_things_func = self.get_all_things_func()
        all_things_nm_dbus_paths = all_things_func()
        for nm_dbus_path in all_things_nm_dbus_paths:
            self.show_info('Found', nm_dbus_path)
            self.extra_processing(nm_dbus_path)


    def thing_added_cb(self, nm_dbus_path):
        """
        callbacks such as 'DeviceAdded', 'AccessPointAdded' always pass
        a dbus-path to the callback method; this dbus-method is the
        key. from which further processing takes place.
        """
        self.show_info('Added', nm_dbus_path)
        self.extra_processing(nm_dbus_path)

    def thing_removed_cb(self, nm_dbus_path):
        """
        callbacks such as 'DeviceRemoved', 'AccessPointRemoved' always
        pass a dbus-path key. NOTE THAT NO FURTHER PROCESSING CAN BE
        DONE ON THIS DBUS-PATH - this is the last-rites method.'
        """
        self.show_info('Removed', nm_dbus_path)
        # Note that extra processing is SIMPLY NOT POSSIBLE.

    def get_thing_type(self):
        return NotImplementedError

    def show_info(self, action, nm_dbus_path):
        print '\n'
        thing_type = self.get_thing_type()
        print action + ' ( ' + thing_type + ' ) :: ' + nm_dbus_path

    def extra_processing(self, nm_dbus_path):
        """
        This is optional. A subtype may choose to add extra
        functionality"""
        pass

    def get_all_things_func(self):
        raise NotImplementedError

    def get_thing_added_signal_name(self):
        raise NotImplementedError

    def get_thing_removed_signal_name(self):
        raise NotImplementedError

    def get_interface_identifier(self):
        raise NotImplementedError


class NMAccessPointsProcessor(BaseTypeProcessor):
    """
    Processor for access-points for a (singular) device.
    """

    def __init__(self, wireless_device_nm_path):
        BaseTypeProcessor.__init__(self, wireless_device_nm_path)

    def get_thing_type(self):
        return 'Access Point'

    def get_all_things_func(self):
        return self._base_interface.GetAccessPoints

    def get_thing_added_signal_name(self):
        return 'AccessPointAdded'

    def get_thing_removed_signal_name(self):
        return 'AccessPointRemoved'


class NMWiredAccessPointsProcessor(NMAccessPointsProcessor):
    """
    Processor for access-points for a (singular) wired device.
    """
    """
    THERE IS ABSOLUTELY NO CONCEPT OF SSID FOR A WIRED DEVICE.

    def __init__(self, wired_device_nm_path):
        NMAccessPointsProcessor.__init__(self, wired_device_nm_path)

    def get_interface_identifier(self):
        return NM_WIRED_DEVICE_INTERFACE
    """



class NMWirelessAccessPointsProcessor(NMAccessPointsProcessor):
    """
    Processor for access-points for a (singular) wirelessdevice.
    """

    def __init__(self, wireless_device_nm_path):
        NMAccessPointsProcessor.__init__(self, wireless_device_nm_path)

    def extra_processing(self, nm_wireless_access_point_path):
        print 'SSID is :: ' + self.get_ssid(nm_wireless_access_point_path)

    def get_ssid(self, nm_wireless_access_point_path):
        access_point_obj = bus.get_object(NM_BUS_NAME,
nm_wireless_access_point_path)
        nm_wireless_access_point_props_interface = \

nm_dbus_path_processor.get_nm_props_interface(nm_wireless_access_point_path)
        ssid_dbus_byte_array =
nm_wireless_access_point_props_interface.Get(NM_ACCESS_POINT_DBUS_INTERFACE,
'Ssid')

        ssid = ""
        for c in ssid_dbus_byte_array:
            ssid = ssid + chr(c)

        return ssid

    def get_interface_identifier(self):
        return NM_WIFI_DEVICE_INTERFACE



class NMDevicesProcessor(BaseTypeProcessor):
    """
    Processor for devices.
    """

    def __init__(self, nm_path):
        BaseTypeProcessor.__init__(self, nm_path)

    def get_thing_type(self):
        return 'Device'

    def extra_processing(self, nm_device_path):
        device_type = self.get_nm_device_type(nm_device_path)
        print 'Device Type is :: ' + str(device_type)


        """
        I REPEAT, THERE IS NO CONCEPT OF SSID FOR A WIRED DEVICE.

        if device_type == 1:
            print '\nWired Hardware found !!'
            NMWiredAccessPointsProcessor(nm_device_path).initiate()
        """


        if device_type == 2:
            print '\nWireless Hardware found !!'
            NMWirelessAccessPointsProcessor(nm_device_path).initiate()

    def get_nm_device_type(self, nm_device_path):
        nm_interface =
nm_dbus_path_processor.get_nm_props_interface(nm_device_path)
        return nm_interface.Get(NM_DEVICE_DBUS_INTERFACE, 'DeviceType')

    def get_all_things_func(self):
        return self._base_interface.GetDevices

    def get_thing_added_signal_name(self):
        return 'DeviceAdded'

    def get_thing_removed_signal_name(self):
        return 'DeviceRemoved'

    def get_interface_identifier(self):
        return NM_DBUS_INTERFACE


try:
    bus = dbus.SystemBus()
    NMDevicesProcessor(NM_PATH).initiate()
except dbus.DBusException, e:
    print e




import gobject
gobject.MainLoop().run()
=============================================================================================




=============================================================================================
[ajay at ajay ~]$ python learn_networkmanager.py


Found ( Device ) :: /org/freedesktop/NetworkManager/Devices/0
Device Type is :: 2

Wireless Hardware found !!


Found ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/13
SSID is :: sr


Found ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/11
SSID is :: RT2561_6


Found ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/0
SSID is :: NegiJuhi


Found ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/2
SSID is :: Ad-hoc Network 1


Found ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/1
SSID is :: vanshika


Found ( Device ) :: /org/freedesktop/NetworkManager/Devices/1
Device Type is :: 1


Found ( Device ) :: /org/freedesktop/NetworkManager/Devices/2
Device Type is :: 3


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/14
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/15
SSID is :: 24651045


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/16
SSID is :: star


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/17
SSID is :: MTNL


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/18
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/2


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/14


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/12


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/19
SSID is :: Ad-hoc Network 1


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/15


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/20
SSID is :: 24651045


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/21
SSID is :: MANUTD


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/22
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/23
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/24
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/20


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/17


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/11


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/6


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/25
SSID is :: RT2561_6


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/24


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/23


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/22


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/16


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/18


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/13


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/10


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/9


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/8


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/7


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/5


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/4


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/3


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/1


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/26
SSID is :: vanshika


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/27
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/28
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/29
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/30
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/21


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/31
SSID is :: sr


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/32
SSID is :: 24651045


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/33
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/34
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/35
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/30


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/36
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/37
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/34


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/32


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/28


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/27


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/38
SSID is :: star


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/39
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/40
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/41
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/37


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/36


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/35


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/33


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/31


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/42
SSID is :: sr


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/43
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/44
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/45
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/46
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/47
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/40


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/39


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/29


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/48
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/49
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/50
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/45


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/44


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/43


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/42


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/51
SSID is :: MTNL


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/47


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/38


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/52
SSID is ::


Removed ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/25


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/53
SSID is :: RT2561_6


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/54
SSID is :: MANUTD


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/55
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/56
SSID is ::


Added ( Access Point ) :: /org/freedesktop/NetworkManager/AccessPoint/57
SSID is ::

Removed ( Device ) :: /org/freedesktop/NetworkManager/Devices/2

===================================================================================


Thanks a ton again :) :) :)

Regards,
Ajay


On Wed, Mar 7, 2012 at 4:06 AM, Sascha Silbe <silbe at activitycentral.com>wrote:

> Excerpts from Ajay Garg's message of 2012-03-03 04:46:33 +0100:
>
> > I have written a very basic python program, that utilizes Networkmanager
> > dbus-apis, to detect addition/removal of network devices.
> > It's working as expected, when I insert a 3G USB modem (both addition and
> > removal callbacks are hit as expected).
> >
> > However, when I try this with
> >
> > a. Inserting/Removing a wired network jack
> > b. Enable/Disable Wireless from the Dell Keyboard
> > c. On/Off the wireless modem-cum-router-cum-accesspoint
> >
> > neither of the callbacks is hit.
>
> Yes, that's expected. The signals you connect to are for network
> _hardware_ (dis)appearing.
>
> a) is just making a new (wired) network on the existing wired ethernet
>   interface available. NM will usually connect automatically to this
>   new network ASAP.
>
> b) depends on how its implemented in hardware. On the XO-1, it would
>   power down the wireless network module completely, causing it to drop
>   off from USB and thus really cause a DeviceRemoved / DeviceAdded
>   signal. On most systems, only the radio gets disabled, so the device
>   will stay visible.
>
> c) is just making a wireless network (un)available. Depending on
>   settings, NM may or may not try to connect to it automatically. In
>   any case there's no hardware that (dis)appears, just a network
>   (Basic Service Set (BSS) in IEEE 802.11-speak).
>
>
> If you want to see NM connecting to networks, you can either listen to
> the StateChanged signal on each device (to see when/if you're connected)
> or monitor [1] the (global) ActiveConnections property (which gives you
> a list of object paths of the active connection objects [2] that you can
> examine further).
>
> Sascha
>
> [1]
> http://projects.gnome.org/NetworkManager/developers/spec-08.html#org.freedesktop.NetworkManager.PropertiesChanged
> [2]
> http://projects.gnome.org/NetworkManager/developers/spec-08.html#org.freedesktop.NetworkManager.Connection.Active
> --
> http://sascha.silbe.org/
> http://www.infra-silbe.de/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sugarlabs.org/archive/dextrose/attachments/20120309/df11665d/attachment-0001.html>


More information about the Dextrose mailing list