From 303ce790879f208be48afd3a9560d92dcfc697f6 Mon Sep 17 00:00:00 2001 From: Kamil Krawczyk Date: Fri, 29 Sep 2023 09:16:03 +0200 Subject: [PATCH] added DeviceBridge support --- Device.go | 2 ++ DeviceBridge.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 DeviceBridge.go diff --git a/Device.go b/Device.go index 9c6e0d0..0f64aa9 100644 --- a/Device.go +++ b/Device.go @@ -62,6 +62,8 @@ func DeviceFactory(objectPath dbus.ObjectPath) (Device, error) { return NewDeviceGeneric(objectPath) case NmDeviceTypeIpTunnel: return NewDeviceIpTunnel(objectPath) + case NmDeviceTypeBridge: + return NewDeviceBridge(objectPath) case NmDeviceTypeEthernet: return NewDeviceWired(objectPath) case NmDeviceTypeWifi: diff --git a/DeviceBridge.go b/DeviceBridge.go new file mode 100644 index 0000000..0cc98c3 --- /dev/null +++ b/DeviceBridge.go @@ -0,0 +1,73 @@ +package gonetworkmanager + +import ( + "encoding/json" + + "github.com/godbus/dbus/v5" +) + +const ( + DeviceBridgeInterface = DeviceInterface + ".Bridge" + + // Properties + DeviceBridgePropertyHwAddress = DeviceBridgeInterface + ".HwAddress" // readable s + DeviceBridgePropertySlaves = DeviceBridgeInterface + ".Slaves" // readable as + DeviceBridgePropertyCarrier = DeviceBridgeInterface + ".Carrier" // readable b +) + +type DeviceBridge interface { + Device + + // GetPropertyHwAddress Active hardware address of the device. + GetPropertyHwAddress() (string, error) + + // GetPropertySlaves Array of paths of enslaved and active devices. + GetPropertySlaves() ([]Device, error) + + // GetPropertyCarrier Indicates whether the physical carrier is found (e.g. whether a cable is plugged in or not). + GetPropertyCarrier() (bool, error) +} + +func NewDeviceBridge(objectPath dbus.ObjectPath) (DeviceBridge, error) { + var d deviceBridge + return &d, d.init(NetworkManagerInterface, objectPath) +} + +type deviceBridge struct { + device +} + +func (d *deviceBridge) GetPropertyHwAddress() (string, error) { + return d.getStringProperty(DeviceBridgePropertyHwAddress) +} + +func (d *deviceBridge) GetPropertySlaves() ([]Device, error) { + paths, err := d.getSliceObjectProperty(DeviceBridgePropertySlaves) + if err != nil { + return nil, err + } + devices := make([]Device, len(paths)) + for i, path := range paths { + devices[i], err = DeviceFactory(path) + if err != nil { + return nil, err + } + } + return devices, nil +} + +func (d *deviceBridge) GetPropertyCarrier() (bool, error) { + return d.getBoolProperty(DeviceBridgePropertyCarrier) +} + +func (d *deviceBridge) MarshalJSON() ([]byte, error) { + m, err := d.device.marshalMap() + if err != nil { + return nil, err + } + + m["HwAddress"], _ = d.GetPropertyHwAddress() + m["Slaves"], _ = d.GetPropertySlaves() + m["Carrier"], _ = d.GetPropertyCarrier() + return json.Marshal(m) +}