|
| 1 | +NetworkManager D-Bus API quickstart |
| 2 | +=================================== |
| 3 | + |
| 4 | +This is a tutorial to understand the structure of the NetworkManager |
| 5 | +D-Bus API and how to use it. |
| 6 | + |
| 7 | +.. note:: |
| 8 | + |
| 9 | + NetworkManager usually uses the system D-Bus, however, sdbus uses the session |
| 10 | + D-Bus by default. It is recommend to call ``sdbus.set_default_bus(sdbus.sd_bus_open_system())`` |
| 11 | + to set the system bus as default bus. |
| 12 | + |
| 13 | +NetworkManager main object |
| 14 | +-------------------------- |
| 15 | + |
| 16 | +This is a static object that contains information about |
| 17 | +entire state of NetworkManager. The Python class |
| 18 | +:py:class:`NetworkManager <sdbus_async.networkmanager.NetworkManager>` |
| 19 | +has a predefined service name and object path. |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + import sdbus |
| 24 | + from sdbus_block.networkmanager import NetworkManager |
| 25 | +
|
| 26 | + sdbus.set_default_bus(sdbus.sd_bus_open_system()) |
| 27 | + network_manager = NetworkManager() |
| 28 | +
|
| 29 | +Devices |
| 30 | +------- |
| 31 | + |
| 32 | +Every device object represents a network device. Device object has a generic |
| 33 | +methods and properties that are universal across all device types and |
| 34 | +a type specific methods. The :py:class:`NetworkDeviceGeneric |
| 35 | +<sdbus_async.networkmanager.NetworkDeviceGeneric>` implements generic methods |
| 36 | +and, for example, :py:class:`NetworkDeviceWireless <sdbus_async.networkmanager.NetworkDeviceWireless>` |
| 37 | +adds Wi-Fi specific methods. |
| 38 | + |
| 39 | +The :py:attr:`device_type <sdbus_async.networkmanager.NetworkManagerDeviceInterfaceAsync.device_type>` |
| 40 | +property and enum :py:class:`DeviceType <sdbus_async.networkmanager.enums.DeviceType>` |
| 41 | +can be used to determine particular type of a device. |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + from sdbus_block.networkmanager import NetworkDeviceGeneric, NetworkDeviceWireless |
| 46 | + from sdbus_block.networkmanager.enums import DeviceType |
| 47 | +
|
| 48 | + all_devices = {path: NetworkDeviceGeneric(path) for path in network_manager.devices} |
| 49 | +
|
| 50 | + wifi_devices = [ |
| 51 | + NetworkDeviceWireless(path) |
| 52 | + for path, device in all_devices.items() |
| 53 | + if device.device_type == DeviceType.WIFI |
| 54 | + ] |
| 55 | +
|
| 56 | +Connection |
| 57 | +---------- |
| 58 | + |
| 59 | +Connection represents a configuration containing an IP address, Wifi password, |
| 60 | +proxy settings and etc... The main object to access connections is the |
| 61 | +:py:class:`NetworkManagerSettings <sdbus_async.networkmanager.NetworkManagerSettings>` |
| 62 | +which has predefined object path. |
| 63 | + |
| 64 | +Each individual connection has a separate path which can be accessed with |
| 65 | +:py:class:`NetworkConnectionSettings <sdbus_async.networkmanager.NetworkConnectionSettings>` |
| 66 | +class. |
| 67 | + |
| 68 | +.. code-block:: python |
| 69 | +
|
| 70 | + from sdbus_block.networkmanager import NetworkManagerSettings |
| 71 | +
|
| 72 | + networwork_manager_settings = NetworkManagerSettings() |
| 73 | +
|
| 74 | + all_connections = [NetworkConnectionSettings(x) for x in networwork_manager_settings.connections] |
| 75 | +
|
| 76 | +The actual connection settings are represented by a complex double nested dictionary |
| 77 | +of D-Bus variants. For convenience a `dataclass <https://docs.python.org/3/library/dataclasses.html>`_ |
| 78 | +based helper is provided. |
| 79 | + |
| 80 | +The :py:meth:`get_profile <sdbus_async.networkmanager.NetworkManagerSettingsConnectionInterfaceAsync.get_profile>` |
| 81 | +and :py:meth:`update_profile <sdbus_async.networkmanager.NetworkManagerSettingsConnectionInterfaceAsync.update_profile>` |
| 82 | +are two main methods to interact with connection settings helper. |
| 83 | + |
| 84 | +.. code-block:: python |
| 85 | +
|
| 86 | + connection = all_connections[0] |
| 87 | + setting_dataclass = connection.get_profile() |
| 88 | + print("uuid:", profile.connection.uuid) |
| 89 | +
|
| 90 | +Active Connection |
| 91 | +----------------- |
| 92 | + |
| 93 | +:py:class:`ActiveConnection <sdbus_async.networkmanager.ActiveConnection>` |
| 94 | +is a product of a Connection being applied to a Device. |
| 95 | + |
| 96 | +For example, :py:meth:`activate_connection <sdbus_async.networkmanager.NetworkManagerInterfaceAsync.activate_connection>` |
| 97 | +of the main NetworkManager object will create new Active Connection |
| 98 | +(therefore configuring network on a device) and return its path. |
| 99 | +The :py:meth:`deactivate_connection <sdbus_async.networkmanager.NetworkManagerInterfaceAsync.deactivate_connection>` |
| 100 | +will remove the active connection and remove the device's network configuration. |
0 commit comments