Configuration of the Network Manager via DBus: how to set the ad hoc mode

Solution 1:

Bookmark:
Configuration of the Network Manager via DBus: how to set the ad hoc mode

If using iwconfig to "activate the wifi via the command line" then it can make the mode Ad-Hoc.

Quoting from man iwconfig

Example :  
                   iwconfig eth0 mode Managed        <-- often wlan0 not the0
                   iwconfig eth0 mode Ad-Hoc                  Freudian ... eth0

Generally the more low level and deeper one goes, the more manually labour intensive the effort becomes, defeating the whole concept and rationale of automation and computers.

However, to use DBus the "easiest" way might be with D-Feet's GUI or qdbus as described here and used below.. Note that argument types are limited in command line input to methods of qdbus and so the matching gdbus command or D-Feet maybe needed.

To get the correct argument values for the required methods, observe the influence of iwconfig, as above, or a working GUI Network Manager with:

dbus-monitor --system "interface='org.freedesktop.NetworkManager'"

while changing the mode paramemter between Ad Hoc and Infrastructure. This will provide the necessary clues for configuration directly on the DBus.
Thus,

qdbus --system --literal  org.freedesktop.NetworkManager

lists

/
/org
/org/freedesktop
/org/freedesktop/NetworkManager
/org/freedesktop/NetworkManager/AccessPoint
/org/freedesktop/NetworkManager/AccessPoint/1385
...
/org/freedesktop/NetworkManager/ActiveConnection
/org/freedesktop/NetworkManager/ActiveConnection/58
/org/freedesktop/NetworkManager/AgentManager
/org/freedesktop/NetworkManager/DHCP4Config
/org/freedesktop/NetworkManager/DHCP4Config/45
/org/freedesktop/NetworkManager/Devices
/org/freedesktop/NetworkManager/Devices/0
/org/freedesktop/NetworkManager/Devices/1        <- my wireless device 
/org/freedesktop/NetworkManager/IP4Config
/org/freedesktop/NetworkManager/IP4Config/44
/org/freedesktop/NetworkManager/Settings
/org/freedesktop/NetworkManager/Settings/0
/org/freedesktop/NetworkManager/Settings/1
...
/org/freedesktop/NetworkManager/Settings/5
/org/freedesktop/NetworkManager/Settings/6

and

qdbus --system --literal  org.freedesktop.NetworkManager                 \
                         /org/freedesktop/NetworkManager/Devices/1       \
                          org.freedesktop.DBus.Properties.GetAll         \
                          org.freedesktop.NetworkManager.Device.Wireless \
| sed -e 's/, "/,\n"/g'

gives the wireless properties with mode in bold:

[Argument: a{sv} {"HwAddress" = [Variant(QString): "74:F0:6D:4B:8E:9B"],
"PermHwAddress" = [Variant(QString): "74:F0:6D:4B:8E:9B"],
"Mode" = [Variant(uint): 2],
"Bitrate" = [Variant(uint): 28900],
"ActiveAccessPoint" = [Variant: [ObjectPath: /org/freedesktop/NetworkManager/AccessPoint/1395]],
"WirelessCapabilities" = [Variant(uint): 127]}]

The pertinent methods in bold of

qdbus --system --literal org.freedesktop.NetworkManager /org/freedesktop/NetworkManager

are

method QString         org.freedesktop.DBus.Introspectable.Introspect()
method QVariantMap     org.freedesktop.DBus.Properties.GetAll(QString interface)
method QDBusVariant    org.freedesktop.DBus.Properties.Get(QString interface, QString propname)
method void            org.freedesktop.DBus.Properties.Set(QString interface, QString propname, QDBusVariant value)
method QDBusObjectPath org.freedesktop.NetworkManager.ActivateConnection(QDBusObjectPath connection, QDBusObjectPath device, QDBusObjectPath specific_object)
method QDBusObjectPath org.freedesktop.NetworkManager.AddAndActivateConnection(QDBusRawType::a{sa{sv}} connection, QDBusObjectPath device, QDBusObjectPath specific_object, QDBusObjectPath& active_connection)
method void            org.freedesktop.NetworkManager.DeactivateConnection(QDBusObjectPath active_connection)
method void            org.freedesktop.NetworkManager.Enable(bool enable)
method QDBusObjectPath org.freedesktop.NetworkManager.GetDeviceByIpIface(QString iface)
method QList org.freedesktop.NetworkManager.GetDevices()
method QDBusRawType::a{ss} org.freedesktop.NetworkManager.GetPermissions()
method void            org.freedesktop.NetworkManager.SetLogging(QString level, QString domains)
method void            org.freedesktop.NetworkManager.Sleep(bool sleep)
method uint            org.freedesktop.NetworkManager.state()
property read bool org.freedesktop.NetworkManager.NetworkingEnabled
property readwrite bool org.freedesktop.NetworkManager.WimaxEnabled
property read bool org.freedesktop.NetworkManager.WimaxHardwareEnabled
property readwrite bool org.freedesktop.NetworkManager.WirelessEnabled
property read bool org.freedesktop.NetworkManager.WirelessHardwareEnabled
property readwrite bool org.freedesktop.NetworkManager.WwanEnabled
property read bool org.freedesktop.NetworkManager.WwanHardwareEnabled
property read QList org.freedesktop.NetworkManager.ActiveConnections
property read QString org.freedesktop.NetworkManager.Version
property read uint org.freedesktop.NetworkManager.State
signal void org.freedesktop.NetworkManager.CheckPermissions()
signal void org.freedesktop.NetworkManager.DeviceAdded(QDBusObjectPath)
signal void org.freedesktop.NetworkManager.DeviceRemoved(QDBusObjectPath)
signal void org.freedesktop.NetworkManager.PropertiesChanged(QVariantMap)
signal void org.freedesktop.NetworkManager.StateChanged(uint)

and of

qdbus --system --literal org.freedesktop.NetworkManager /org/freedesktop/NetworkManager/Devices/1

are

method QString           org.freedesktop.DBus.Introspectable.Introspect()
method QVariantMap       org.freedesktop.DBus.Properties.GetAll(QString interface)
method QDBusVariant      org.freedesktop.DBus.Properties.Get(QString interface, QString propname)
method void              org.freedesktop.DBus.Properties.Set(QString interface, QString propname, QDBusVariant value)
method void              org.freedesktop.NetworkManager.Device.Disconnect()
method QList<QDBusObjectPath> org.freedesktop.NetworkManager.Device.Wireless.GetAccessPoints()
property read bool            org.freedesktop.NetworkManager.Device.FirmwareMissing
property read bool            org.freedesktop.NetworkManager.Device.Managed
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.ActiveConnection
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.Dhcp4Config
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.Dhcp6Config
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.Ip4Config
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.Ip6Config
property read QDBusRawType::(uu) org.freedesktop.NetworkManager.Device.StateReason
property read QString         org.freedesktop.NetworkManager.Device.Driver
property read QString         org.freedesktop.NetworkManager.Device.Interface
property read QString         org.freedesktop.NetworkManager.Device.IpInterface
property read QString         org.freedesktop.NetworkManager.Device.Udi
property read uint            org.freedesktop.NetworkManager.Device.Capabilities
property read uint            org.freedesktop.NetworkManager.Device.DeviceType
property read uint            org.freedesktop.NetworkManager.Device.Ip4Address
property read uint            org.freedesktop.NetworkManager.Device.State
property read QDBusObjectPath org.freedesktop.NetworkManager.Device.Wireless.ActiveAccessPoint
property read QString         org.freedesktop.NetworkManager.Device.Wireless.HwAddress
property read QString         org.freedesktop.NetworkManager.Device.Wireless.PermHwAddress
property read uint            org.freedesktop.NetworkManager.Device.Wireless.Bitrate
property read uint            org.freedesktop.NetworkManager.Device.Wireless.Mode
property read uint            org.freedesktop.NetworkManager.Device.Wireless.WirelessCapabilities
signal void org.freedesktop.NetworkManager.Device.StateChanged(uint, uint, uint)
signal void org.freedesktop.NetworkManager.Device.Wireless.AccessPointAdded(QDBusObjectPath)
signal void org.freedesktop.NetworkManager.Device.Wireless.AccessPointRemoved(QDBusObjectPath)
signal void org.freedesktop.NetworkManager.Device.Wireless.PropertiesChanged(QVariantMap)

Solution 2:

I just recently had to do that. Here's some C code (depending only on libdbus-1) to set up and connect an ad-hoc network:

  • https://github.com/abrauchli/allnet/blob/e4c05a4cb9c728d9c8f459c5d41002ec15c75910/src/abc/abc-networkmanager.c