diff --git a/Device.go b/Device.go index aac23a6..604974d 100644 --- a/Device.go +++ b/Device.go @@ -77,6 +77,8 @@ func DeviceFactory(objectPath dbus.ObjectPath) (Device, error) { return NewDeviceWired(objectPath) case NmDeviceTypeWifi: return NewDeviceWireless(objectPath) + case NmDeviceTypeVlan: + return NewDeviceVlan(objectPath) } return d, nil diff --git a/DeviceVlan.go b/DeviceVlan.go new file mode 100644 index 0000000..af7108c --- /dev/null +++ b/DeviceVlan.go @@ -0,0 +1,69 @@ +package gonetworkmanager + +import ( + "encoding/json" + + "github.com/godbus/dbus/v5" +) + +const ( + DeviceVlanInterface = DeviceInterface + ".Vlan" + + // Properties + DeviceVlanPropertyCarrier = DeviceVlanInterface + ".Carrier" // readable b + DeviceVlanPropertyParent = DeviceVlanInterface + ".Parent" // readable o + DeviceVlanPropertyVlanId = DeviceVlanInterface + ".VlanId" // readable u +) + +// DeviceVlan Virtual LAN Device +type DeviceVlan interface { + Device + + // GetPropertyCarrier Indicates whether the physical carrier is found (e.g. whether a cable is plugged in or not). + // DEPRECATED: check for the "carrier" flag in the "InterfaceFlags" property on the "org.freedesktop.NetworkManager.Device" interface. + GetPropertyCarrier() (bool, error) + + // GetPropertyParent The object path of the parent device. + GetPropertyParent() (Device, error) + + // GetPropertyVlanId The VLAN ID of this VLAN interface + GetPropertyVlanId() (uint32, error) +} + +func NewDeviceVlan(objectPath dbus.ObjectPath) (DeviceVlan, error) { + var d deviceVlan + return &d, d.init(NetworkManagerInterface, objectPath) +} + +type deviceVlan struct { + device +} + +func (d *deviceVlan) GetPropertyParent() (Device, error) { + path, err := d.getObjectProperty(DeviceVlanPropertyParent) + if err != nil || path == "/" { + return nil, err + } + return DeviceFactory(path) +} + +func (d *deviceVlan) GetPropertyVlanId() (uint32, error) { + return d.getUint32Property(DeviceVlanPropertyVlanId) +} + +func (d *deviceVlan) GetPropertyCarrier() (bool, error) { + return d.getBoolProperty(DeviceVlanPropertyCarrier) +} + +func (d *deviceVlan) MarshalJSON() ([]byte, error) { + m, err := d.device.marshalMap() + if err != nil { + return nil, err + } + + m["HwAddress"], _ = d.GetPropertyHwAddress() + m["Parent"], _ = d.GetPropertyParent() + m["Carrier"], _ = d.GetPropertyCarrier() + m["VlanId"], _ = d.GetPropertyVlanId() + return json.Marshal(m) +}