dbusutil

package
v0.0.0-...-683b059 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package dbusutil provides additional functionality on top of the godbus/dbus package.

Package dbusutil provides additional functionality on top of the godbus/dbus package.

Index

Constants

View Source
const (
	DBusErrorFailed                           = "org.freedesktop.DBus.Error.Failed"
	DBusErrorNoMemory                         = "org.freedesktop.DBus.Error.NoMemory"
	DBusErrorServiceUnknown                   = "org.freedesktop.DBus.Error.ServiceUnknown"
	DBusErrorNameHasNoOwner                   = "org.freedesktop.DBus.Error.NameHasNoOwner"
	DBusErrorNoReply                          = "org.freedesktop.DBus.Error.NoReply"
	DBusErrorIOError                          = "org.freedesktop.DBus.Error.IOError"
	DBusErrorBadAddress                       = "org.freedesktop.DBus.Error.BadAddress"
	DBusErrorNotSupported                     = "org.freedesktop.DBus.Error.NotSupported"
	DBusErrorLimitsExceeded                   = "org.freedesktop.DBus.Error.LimitsExceeded"
	DBusErrorAccessDenied                     = "org.freedesktop.DBus.Error.AccessDenied"
	DBusErrorAuthFailed                       = "org.freedesktop.DBus.Error.AuthFailed"
	DBusErrorNoServer                         = "org.freedesktop.DBus.Error.NoServer"
	DBusErrorTimeout                          = "org.freedesktop.DBus.Error.Timeout"
	DBusErrorNoNetwork                        = "org.freedesktop.DBus.Error.NoNetwork"
	DBusErrorAddressInUse                     = "org.freedesktop.DBus.Error.AddressInUse"
	DBusErrorDisconnected                     = "org.freedesktop.DBus.Error.Disconnected"
	DBusErrorInvalidArgs                      = "org.freedesktop.DBus.Error.InvalidArgs"
	DBusErrorFileNotFound                     = "org.freedesktop.DBus.Error.FileNotFound"
	DBusErrorFileExists                       = "org.freedesktop.DBus.Error.FileExists"
	DBusErrorUnknownMethod                    = "org.freedesktop.DBus.Error.UnknownMethod"
	DBusErrorUnknownObject                    = "org.freedesktop.DBus.Error.UnknownObject"
	DBusErrorUnknownInterface                 = "org.freedesktop.DBus.Error.UnknownInterface"
	DBusErrorUnknownProperty                  = "org.freedesktop.DBus.Error.UnknownProperty"
	DBusErrorPropertyReadOnly                 = "org.freedesktop.DBus.Error.PropertyReadOnly"
	DBusErrorUnixProcessIDUnknown             = "org.freedesktop.DBus.Error.UnixProcessIdUnknown"
	DBusErrorInvalidSignature                 = "org.freedesktop.DBus.Error.InvalidSignature"
	DBusErrorInconsistentMessage              = "org.freedesktop.DBus.Error.InconsistentMessage"
	DBusErrorMatchRuleNotFound                = "org.freedesktop.DBus.Error.MatchRuleNotFound"
	DBusErrorMatchRuleInvalid                 = "org.freedesktop.DBus.Error.MatchRuleInvalid"
	DBusErrorInteractiveAuthorizationRequired = "org.freedesktop.DBus.Error.InteractiveAuthorizationRequired"
)

DBusError strings from https://www.freedesktop.org/software/systemd/man/sd-bus-errors.html

View Source
const (
	// SignalChanSize is the buffer size of channels holding signals.
	SignalChanSize = 10
)

Variables

This section is empty.

Functions

func CallProtoMethod

func CallProtoMethod(ctx context.Context, obj dbus.BusObject, method string, in, out proto.Message) error

CallProtoMethod marshals in, passes it as a byte array arg to method on obj, and unmarshals a byte array arg from the response to out. method should be prefixed by a D-Bus interface name. Both in and out may be nil.

func CallProtoMethodWithSequence

func CallProtoMethodWithSequence(ctx context.Context, obj dbus.BusObject, method string, in, out proto.Message) (dbus.Sequence, error)

CallProtoMethodWithSequence marshals in, passes it as a byte array arg to method on obj, and unmarshals a byte array arg from the response to out. The sequence number indicating the order of the method response on the DBus connection is returned.

This sequence number may be correlated with the sequence number of other method calls and signals on the same DBus connection, such as to implement a race-free subscribe and get-current-state operation.

The method specified should be prefixed by a D-Bus interface name. Both in and out may be nil.

func Connect

func Connect(ctx context.Context, name string, path dbus.ObjectPath) (*dbus.Conn, dbus.BusObject, error)

Connect sets up the D-Bus connection to the service specified by name, path by using SystemBus. This waits for the service to become available but does not validate path existence.

func ConnectNoTiming

func ConnectNoTiming(ctx context.Context, name string, path dbus.ObjectPath) (*dbus.Conn, dbus.BusObject, error)

ConnectNoTiming, like Connect() but without emitting timing information.

func ConnectPrivateWithAuth

func ConnectPrivateWithAuth(ctx context.Context, uid uint32, name string, path dbus.ObjectPath) (*dbus.Conn, dbus.BusObject, error)

ConnectPrivateWithAuth sets up the D-Bus connection for user with uid to the service specified by name, path by using SystemBusPrivate. And like SystemBusPrivateWithAuth, the connection should be closed after use. This waits for the service to become available.

func DbusEventMonitor

func DbusEventMonitor(ctx context.Context, specs []MatchSpec) (func() ([]CalledMethod, error), error)

DbusEventMonitor monitors the system message bus for the D-Bus calls we want to observe as specified in |specs|. It returns a stop function and error. The stop function stops the D-Bus monitor and return the called methods and/or error.

func GetNextSignal

func GetNextSignal(ctx context.Context, conn *dbus.Conn, spec MatchSpec) (*dbus.Signal, error)

GetNextSignal returns the next signal on conn that is matched by spec.

func IsDBusError

func IsDBusError(err error, name string) bool

IsDBusError checks if err is a wrapped dbus error with given error name.

func ManagedObjects

func ManagedObjects(ctx context.Context, obj dbus.BusObject) (map[string][]dbus.ObjectPath, error)

ManagedObjects gets all the objects managed under the passed object. Returns a map from interface name to a slice of ObjectPaths that have an object with that interface.

func Property

func Property(ctx context.Context, obj dbus.BusObject, p string) (interface{}, error)

Property gets a DBus property from an object. The property name is in "interface.property" format.

func ServiceOwned

func ServiceOwned(ctx context.Context, conn *dbus.Conn, svc string) bool

ServiceOwned returns whether the service in request is already owned.

func SetProperty

func SetProperty(ctx context.Context, obj dbus.BusObject, p string, v interface{}) error

SetProperty sets a DBus property on an object. The property name is in "interface.property" format.

func SystemBus

func SystemBus() (conn *dbus.Conn, err error)

SystemBus returns a shared connection to the system bus, connecting to it if it is not already connected. It should be used in preference to dbus.SystemBus().

func SystemBusPrivate

func SystemBusPrivate(opts ...dbus.ConnOption) (*dbus.Conn, error)

SystemBusPrivate returns a new private connection to the system bus. It should be used in preference to dbus.SystemBusPrivate().

func SystemBusPrivateWithAuth

func SystemBusPrivateWithAuth(ctx context.Context, uid uint32) (*dbus.Conn, error)

SystemBusPrivateWithAuth returns a connection with switched euid. The returned *dbus.Conn should be closed after use.

func WaitForService

func WaitForService(ctx context.Context, conn *dbus.Conn, svc string) error

WaitForService blocks until a D-Bus client on conn takes ownership of the name svc. If the name is already owned, it returns immediately.

Types

type CalledMethod

type CalledMethod struct {
	MethodName string
	Arguments  []interface{}
}

The CalledMethod struct represents a method call and arguments that was observed by DbusEventMonitor.

type DBusObject

type DBusObject struct {
	// contains filtered or unexported fields
}

DBusObject wraps a D-Bus interface, object and connection.

func NewDBusObject

func NewDBusObject(ctx context.Context, service, iface string, path dbus.ObjectPath) (*DBusObject, error)

NewDBusObject creates a DBusObject.

func (*DBusObject) Call

func (d *DBusObject) Call(ctx context.Context, method string, args ...interface{}) *dbus.Call

Call calls the D-Bus method with argument against the designated D-Bus object.

func (*DBusObject) CreateWatcher

func (d *DBusObject) CreateWatcher(ctx context.Context, signalNames ...string) (*SignalWatcher, error)

CreateWatcher returns a SignalWatcher to observe the specified signals.

func (*DBusObject) Get

func (d *DBusObject) Get(ctx context.Context, propName string, val interface{}) error

Get calls org.freedesktop.DBus.Properties.Get and stores the result into val.

func (*DBusObject) GetAll

func (d *DBusObject) GetAll(ctx context.Context) (map[string]interface{}, error)

GetAll calls org.freedesktop.DBus.Properties.GetAll and stores the result into val.

func (*DBusObject) ObjectPath

func (d *DBusObject) ObjectPath() dbus.ObjectPath

ObjectPath returns the path of the D-Bus object.

func (*DBusObject) String

func (d *DBusObject) String() string

String returns the path of the D-Bus object as a string. It is so named to conform to the Stringer interface.

type MatchSpec

type MatchSpec struct {
	// Type contains the message type, e.g. "signal".
	Type string
	// Path contains the path that the message is sent to (for method calls)
	// or emitted from (for signals).
	Path dbus.ObjectPath
	// Sender contains the message sender (typically the sender's connection name, e.g. ":1.2").
	Sender string
	// Interface contains the interface that the message is sent to (for method calls)
	// or emitted from (for signals).
	Interface string
	// Member contains the method or signal name. It does not include the interface.
	Member string
	// Arg0 contains the first argument in the message. Non-string arguments are unsupported.
	// If empty, the argument is not compared.
	Arg0 string
}

MatchSpec specifies messages that should be received by a D-Bus client. Empty fields are disregarded.

func (MatchSpec) MatchesSignal

func (ms MatchSpec) MatchesSignal(sig *dbus.Signal) bool

MatchesSignal returns true if sig is matched by the spec.

func (MatchSpec) String

func (ms MatchSpec) String() string

String returns a match rule that can be passed to the bus's AddMatch or RemoveMatch method to start or stop receiving messages described by the spec.

type Properties

type Properties struct {
	// contains filtered or unexported fields
}

Properties wraps D-Bus object properties.

func NewDBusProperties

func NewDBusProperties(ctx context.Context, d *DBusObject) (*Properties, error)

NewDBusProperties creates a new Properties object and populates it with the object's properties using org.freedesktop.DBus.Properties.GetAll. The dbus call may fail with DBusErrorUnknownObject if the DBusObject is not valid. Callers can us IsDBusError to test for that case.

func NewProperties

func NewProperties(p map[string]interface{}) *Properties

NewProperties creates a new Properties object with raw data.

func (*Properties) Get

func (p *Properties) Get(prop string) (interface{}, error)

Get returns property value.

func (*Properties) GetBool

func (p *Properties) GetBool(prop string) (bool, error)

GetBool returns the property value as a boolean.

func (*Properties) GetInt32

func (p *Properties) GetInt32(prop string) (int32, error)

GetInt32 returns the property value as int32.

func (*Properties) GetObjectPath

func (p *Properties) GetObjectPath(prop string) (dbus.ObjectPath, error)

GetObjectPath returns the DBus ObjectPath of the given property name.

func (*Properties) GetObjectPaths

func (p *Properties) GetObjectPaths(prop string) ([]dbus.ObjectPath, error)

GetObjectPaths returns the list of DBus ObjectPaths of the given property name.

func (*Properties) GetString

func (p *Properties) GetString(prop string) (string, error)

GetString returns string property value.

func (*Properties) GetStrings

func (p *Properties) GetStrings(prop string) ([]string, error)

GetStrings returns property value as string array.

func (*Properties) GetUint16

func (p *Properties) GetUint16(prop string) (uint16, error)

GetUint16 returns the property value as uint16.

func (*Properties) GetUint16s

func (p *Properties) GetUint16s(prop string) ([]uint16, error)

GetUint16s returns the property value as uint16 array.

func (*Properties) GetUint32

func (p *Properties) GetUint32(prop string) (uint32, error)

GetUint32 returns the property value as a uint32.

func (*Properties) GetUint8

func (p *Properties) GetUint8(prop string) (uint8, error)

GetUint8 returns the property value as uint8.

func (*Properties) Has

func (p *Properties) Has(prop string) bool

Has returns whether property exist.

type PropertyHolder

type PropertyHolder struct {
	*DBusObject
}

PropertyHolder provides methods to access properties of a DBus object. The DBus object must provides GetProperties and SetProperty methods, and a PropertyChanged signal.

func NewPropertyHolder

func NewPropertyHolder(ctx context.Context, service, iface string, path dbus.ObjectPath) (*PropertyHolder, error)

NewPropertyHolder creates a DBus object with the given service, interface and path which can be used for accessing and setting properties.

func (*PropertyHolder) GetProperties

func (h *PropertyHolder) GetProperties(ctx context.Context) (*Properties, error)

GetProperties calls NewDBusProperties with the PropertyHolder object and returns the result.

func (*PropertyHolder) SetProperty

func (h *PropertyHolder) SetProperty(ctx context.Context, prop string, value interface{}) error

SetProperty calls Set method on the interface to set property to the given value.

type SignalWatcher

type SignalWatcher struct {
	// Signals passes signals matched by any of the MatchSpecs passed to NewSignalWatcher.
	// This channel is buffered but must be serviced regularly; otherwise incoming
	// signals may be dropped.
	Signals chan *dbus.Signal
	// contains filtered or unexported fields
}

SignalWatcher watches for and returns D-Bus signals matched by one or more MatchSpecs.

func NewSignalWatcher

func NewSignalWatcher(ctx context.Context, conn *dbus.Conn, specs ...MatchSpec) (*SignalWatcher, error)

NewSignalWatcher returns a new SignalWatcher that will return signals on conn matched by specs.

func NewSignalWatcherForSystemBus

func NewSignalWatcherForSystemBus(ctx context.Context, spec ...MatchSpec) (*SignalWatcher, error)

NewSignalWatcherForSystemBus is a convenience function that calls NewSignalWatcher with a shared connection to the system bus.

func (*SignalWatcher) Close

func (sw *SignalWatcher) Close(ctx context.Context) error

Close stops watching for signals.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL