Documentation
¶
Overview ¶
Package dbus is a pure-Go, CGO-free implementation of the D-Bus wire protocol, authentication, transport, connection and object-export layers, built from scratch against the D-Bus specification with zero non-standard dependencies.
The public surface deliberately mirrors the shape of the widely used github.com/godbus/dbus/v5 API (Conn, Object, Variant, ObjectPath, Signature, Error, Export, RequestName, Emit, MakeVariant) so existing consumers can migrate off cgo/third-party code with near-mechanical edits.
Index ¶
- Constants
- Variables
- func Marshal(order ByteOrder, vs ...interface{}) ([]byte, error)
- func Store(src []interface{}, dsts ...interface{}) error
- func Unmarshal(data []byte, order ByteOrder, sig Signature) ([]interface{}, int, error)
- func WriteMessage(w io.Writer, m *Message, order ByteOrder) error
- type BusObject
- type ByteOrder
- type Call
- type Conn
- func (c *Conn) AddMatch(rule string) error
- func (c *Conn) Close() error
- func (c *Conn) Emit(path ObjectPath, name string, args ...interface{}) error
- func (c *Conn) EmitPropertiesChanged(path ObjectPath, iface string, changed map[string]interface{}) error
- func (c *Conn) Export(v interface{}, path ObjectPath, iface string) error
- func (c *Conn) ExportProperties(path ObjectPath, iface string, props map[string]*Prop) error
- func (c *Conn) Hello() (string, error)
- func (c *Conn) Names() []string
- func (c *Conn) Object(dest string, path ObjectPath) BusObject
- func (c *Conn) ReleaseName(name string) (uint32, error)
- func (c *Conn) RemoveMatch(rule string) error
- func (c *Conn) RemoveSignal(ch chan<- *Signal)
- func (c *Conn) RequestName(name string, flags uint32) (uint32, error)
- func (c *Conn) Signal(ch chan<- *Signal)
- type Error
- type Flags
- type HeaderField
- type Marshaler
- type Message
- type MessageType
- type ObjectPath
- type Prop
- type Signal
- type Signature
- type UnixFD
- type UnixFDIndex
- type Unmarshaler
- type Variant
Constants ¶
const ( NameFlagAllowReplacement uint32 = 1 << 0 NameFlagReplaceExisting uint32 = 1 << 1 NameFlagDoNotQueue uint32 = 1 << 2 NameReplyPrimaryOwner uint32 = 1 NameReplyInQueue uint32 = 2 NameReplyExists uint32 = 3 NameReplyAlreadyOwner uint32 = 4 )
RequestName flags and reply codes (subset mirroring the bus API).
Variables ¶
var DefaultCallTimeout = 30 * time.Second
DefaultCallTimeout is the hard ceiling applied to every method call whose context carries no earlier deadline. It guarantees a misbehaving or silent peer can never make a call block forever.
var ErrClosed = errors.New("dbus: connection closed")
ErrClosed is returned for operations on a closed connection.
Functions ¶
func Marshal ¶
Marshal encodes vs into the D-Bus wire format using byte order, returning the encoded bytes. Alignment is computed as if the encoding begins at an 8-aligned position (as a message body does), so the result may be embedded directly after an 8-padded header.
func Store ¶
func Store(src []interface{}, dsts ...interface{}) error
Store coerces the dynamically-decoded values in src into the pointer targets dsts (as returned by Unmarshal), mirroring github.com/godbus/dbus's Store: a *T receives a T, a *[]X a homogeneous array, a *map a dict, a *struct the fields of a struct, and an interface{} the value verbatim.
Types ¶
type BusObject ¶
type BusObject interface {
Call(method string, flags Flags, args ...interface{}) *Call
CallWithContext(ctx context.Context, method string, flags Flags, args ...interface{}) *Call
GetProperty(name string) (Variant, error)
SetProperty(name string, value interface{}) error
Path() ObjectPath
Destination() string
}
BusObject is a handle to a remote object at a destination and path, on which methods can be called.
type ByteOrder ¶
ByteOrder selects the wire byte order. D-Bus messages are either little-endian ('l') or big-endian ('B'); both are supported and negotiated per message via the header's first byte.
type Call ¶
type Call struct {
Destination string
Path ObjectPath
Method string // "interface.member"
Args []interface{}
Body []interface{}
Err error
}
Call is a completed (or failed) method call: Body holds the reply arguments, Err any error (including a remote *Error).
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a connection to a D-Bus message bus.
func ConnectSessionBus ¶
ConnectSessionBus dials the session bus (DBUS_SESSION_BUS_ADDRESS) and sends Hello, returning a connection with an assigned unique name.
func ConnectSystemBus ¶
ConnectSystemBus dials the system bus and sends Hello.
func Dial ¶
Dial connects to the bus at address, authenticates (EXTERNAL, then ANONYMOUS) and returns a ready connection. It does not send Hello; callers wanting a unique name should use ConnectSessionBus/ConnectSystemBus or call Hello themselves.
func NewConn ¶
NewConn wraps an already-connected, already-authenticated transport (for example one half of a net.Pipe in tests) and starts serving it.
func (*Conn) AddMatch ¶
AddMatch installs a match rule on the bus so matching signals are routed to this connection.
func (*Conn) Emit ¶
func (c *Conn) Emit(path ObjectPath, name string, args ...interface{}) error
Emit sends a signal from the local object at path. name is "interface.member".
func (*Conn) EmitPropertiesChanged ¶
func (c *Conn) EmitPropertiesChanged(path ObjectPath, iface string, changed map[string]interface{}) error
EmitPropertiesChanged emits org.freedesktop.DBus.Properties.PropertiesChanged for the named interface, carrying the changed name/value pairs.
func (*Conn) Export ¶
func (c *Conn) Export(v interface{}, path ObjectPath, iface string) error
Export publishes the exported methods of v under the given object path and interface name. Incoming method calls addressed to that path and interface are dispatched to the matching method by reflection. A method may return any number of D-Bus-representable values optionally followed by a *Error, which (when non-nil) is sent back as an error reply.
func (*Conn) ExportProperties ¶
ExportProperties publishes a set of properties for one interface on one object path, served through org.freedesktop.DBus.Properties. props maps property name to its definition.
func (*Conn) Hello ¶
Hello performs the org.freedesktop.DBus.Hello handshake and records the unique name the bus assigns.
func (*Conn) Object ¶
func (c *Conn) Object(dest string, path ObjectPath) BusObject
Object returns a handle to the object at path on the peer named dest.
func (*Conn) ReleaseName ¶
ReleaseName asks the bus to release a previously requested well-known name.
func (*Conn) RemoveMatch ¶
RemoveMatch removes a previously installed match rule.
func (*Conn) RemoveSignal ¶
RemoveSignal removes a previously registered signal channel.
func (*Conn) RequestName ¶
RequestName asks the bus to assign the well-known name to this connection. On success (primary owner or already owner) the name is recorded.
type Error ¶
type Error struct {
Name string
Body []interface{}
}
Error is a D-Bus error reply. It carries the error name (a valid interface name such as "org.freedesktop.DBus.Error.Failed") and an optional body, whose first element is conventionally a human-readable message string.
func MakeFailedError ¶
MakeFailedError wraps an arbitrary Go error as a generic org.freedesktop.DBus.Error.Failed reply.
type Flags ¶
type Flags byte
Flags is the message flags bitfield.
const ( // FlagNoReplyExpected suppresses the method_return/error reply. FlagNoReplyExpected Flags = 1 << iota // FlagNoAutoStart asks the bus not to auto-start a service. FlagNoAutoStart // FlagAllowInteractiveAuthorization permits interactive authorization. FlagAllowInteractiveAuthorization )
Message flags.
type HeaderField ¶
type HeaderField byte
HeaderField is a message header field code.
const ( FieldPath HeaderField = iota + 1 FieldInterface FieldMember FieldErrorName FieldReplySerial FieldDestination FieldSender FieldSignature FieldUnixFDs )
Header field codes.
type Marshaler ¶
type Marshaler interface {
MarshalDBus(dst []byte, order ByteOrder, pos int) ([]byte, Signature, error)
}
Marshaler is implemented by types that encode themselves to the D-Bus wire format. MarshalDBus must append to and return dst, honouring the alignment implied by having already written pos bytes since the message start, and report the single complete signature it produced.
type Message ¶
type Message struct {
Type MessageType
Flags Flags
Headers map[HeaderField]Variant
Body []interface{}
// contains filtered or unexported fields
}
Message is a decoded D-Bus message: its type, flags, serial, header fields and body values.
func DecodeMessage ¶
DecodeMessage decodes exactly one message from the front of data, returning the message and the total number of bytes it occupied.
func ReadMessage ¶
ReadMessage reads exactly one message from r. It first reads the 16-byte fixed prefix (through the header-array length), then the remaining header, padding and body.
func (*Message) Marshal ¶
Marshal encodes the message into wire bytes using the given byte order. It derives and installs the SIGNATURE header from Body when the body is non-empty, then validates the required header fields.
type MessageType ¶
type MessageType byte
MessageType identifies the kind of a D-Bus message.
const ( TypeInvalid MessageType = iota TypeMethodCall TypeMethodReturn TypeError TypeSignal )
Message types.
type ObjectPath ¶
type ObjectPath string
ObjectPath is a D-Bus object path such as "/org/freedesktop/DBus". It marshals with the 'o' type code.
func (ObjectPath) IsValid ¶
func (p ObjectPath) IsValid() bool
IsValid reports whether p is a syntactically valid object path: a non-empty sequence of "/"-separated elements of [A-Za-z0-9_], with a single leading slash, no trailing slash (except the root "/") and no empty elements.
type Prop ¶
type Prop struct {
Value interface{}
Writable bool
}
Prop is a single exported property: its current value and whether remote peers may write it via org.freedesktop.DBus.Properties.Set.
type Signal ¶
type Signal struct {
Sender string
Path ObjectPath
Name string // "interface.member"
Body []interface{}
}
Signal is a received D-Bus signal, delivered to channels registered with (*Conn).Signal.
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
Signature is a validated D-Bus type signature: a possibly-empty concatenation of complete single types (e.g. "a{sv}", "(ii)", "s"). It marshals with the 'g' type code.
func ParseSignature ¶
ParseSignature validates s and returns it as a Signature. It reports an error for over-length input, unknown type codes, or malformed containers.
func ParseSignatureMust ¶
ParseSignatureMust is ParseSignature for signatures known to be valid at author time (typically string literals): it returns the Signature and panics if s is malformed. It mirrors the like-named convenience in github.com/godbus/dbus/v5 so consumers can migrate with unchanged call sites.
func SignatureOf ¶
func SignatureOf(vs ...interface{}) Signature
SignatureOf returns the concatenated signature of the Go values vs, inferring each type. It panics if any value has no D-Bus representation; callers that cannot guarantee representable inputs should use SignatureOfType.
func SignatureOfType ¶
SignatureOfType returns the signature for a single Go type, or an error if the type cannot be represented on the wire.
type UnixFD ¶
type UnixFD int32
UnixFD is the Go representation of the 'h' (UNIX file descriptor) type. This implementation encodes and decodes the wire index; out-of-band descriptor passing over the socket is not performed.
type UnixFDIndex ¶
type UnixFDIndex uint32
UnixFDIndex is the on-wire index form of a UNIX file descriptor.
type Unmarshaler ¶
Unmarshaler is implemented by types that decode themselves from the D-Bus wire format, returning the number of bytes consumed from src.
type Variant ¶
type Variant struct {
// contains filtered or unexported fields
}
Variant is a D-Bus variant: a value carrying its own single complete type signature. It marshals with the 'v' type code.
func MakeVariant ¶
func MakeVariant(value interface{}) Variant
MakeVariant wraps value in a Variant, inferring its signature from the Go type of value.
func MakeVariantWithSignature ¶
MakeVariantWithSignature wraps value in a Variant with the explicit signature sig (which must be a single complete type).