1 package dbus
2
3 import (
4 "context"
5 "errors"
6 "strings"
7 )
8
9
10
11 type BusObject interface {
12 Call(method string, flags Flags, args ...interface{}) *Call
13 CallWithContext(ctx context.Context, method string, flags Flags, args ...interface{}) *Call
14 Go(method string, flags Flags, ch chan *Call, args ...interface{}) *Call
15 GoWithContext(ctx context.Context, method string, flags Flags, ch chan *Call, args ...interface{}) *Call
16 AddMatchSignal(iface, member string, options ...MatchOption) *Call
17 RemoveMatchSignal(iface, member string, options ...MatchOption) *Call
18 GetProperty(p string) (Variant, error)
19 StoreProperty(p string, value interface{}) error
20 SetProperty(p string, v interface{}) error
21 Destination() string
22 Path() ObjectPath
23 }
24
25
26 type Object struct {
27 conn *Conn
28 dest string
29 path ObjectPath
30 }
31
32
33 func (o *Object) Call(method string, flags Flags, args ...interface{}) *Call {
34 return <-o.createCall(context.Background(), method, flags, make(chan *Call, 1), args...).Done
35 }
36
37
38 func (o *Object) CallWithContext(ctx context.Context, method string, flags Flags, args ...interface{}) *Call {
39 return <-o.createCall(ctx, method, flags, make(chan *Call, 1), args...).Done
40 }
41
42
43
44
45
46
47 func (o *Object) AddMatchSignal(iface, member string, options ...MatchOption) *Call {
48 base := []MatchOption{
49 withMatchType("signal"),
50 WithMatchInterface(iface),
51 WithMatchMember(member),
52 }
53
54 options = append(base, options...)
55 return o.conn.BusObject().Call(
56 "org.freedesktop.DBus.AddMatch",
57 0,
58 formatMatchOptions(options),
59 )
60 }
61
62
63
64
65
66 func (o *Object) RemoveMatchSignal(iface, member string, options ...MatchOption) *Call {
67 base := []MatchOption{
68 withMatchType("signal"),
69 WithMatchInterface(iface),
70 WithMatchMember(member),
71 }
72
73 options = append(base, options...)
74 return o.conn.BusObject().Call(
75 "org.freedesktop.DBus.RemoveMatch",
76 0,
77 formatMatchOptions(options),
78 )
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 func (o *Object) Go(method string, flags Flags, ch chan *Call, args ...interface{}) *Call {
93 return o.createCall(context.Background(), method, flags, ch, args...)
94 }
95
96
97 func (o *Object) GoWithContext(ctx context.Context, method string, flags Flags, ch chan *Call, args ...interface{}) *Call {
98 return o.createCall(ctx, method, flags, ch, args...)
99 }
100
101 func (o *Object) createCall(ctx context.Context, method string, flags Flags, ch chan *Call, args ...interface{}) *Call {
102 if ctx == nil {
103 panic("nil context")
104 }
105 iface := ""
106 i := strings.LastIndex(method, ".")
107 if i != -1 {
108 iface = method[:i]
109 }
110 method = method[i+1:]
111 msg := new(Message)
112 msg.Type = TypeMethodCall
113 msg.Flags = flags & (FlagNoAutoStart | FlagNoReplyExpected)
114 msg.Headers = make(map[HeaderField]Variant)
115 msg.Headers[FieldPath] = MakeVariant(o.path)
116 msg.Headers[FieldDestination] = MakeVariant(o.dest)
117 msg.Headers[FieldMember] = MakeVariant(method)
118 if iface != "" {
119 msg.Headers[FieldInterface] = MakeVariant(iface)
120 }
121 msg.Body = args
122 if len(args) > 0 {
123 msg.Headers[FieldSignature] = MakeVariant(SignatureOf(args...))
124 }
125 return o.conn.SendWithContext(ctx, msg, ch)
126 }
127
128
129
130 func (o *Object) GetProperty(p string) (Variant, error) {
131 var result Variant
132 err := o.StoreProperty(p, &result)
133 return result, err
134 }
135
136
137
138
139 func (o *Object) StoreProperty(p string, value interface{}) error {
140 idx := strings.LastIndex(p, ".")
141 if idx == -1 || idx+1 == len(p) {
142 return errors.New("dbus: invalid property " + p)
143 }
144
145 iface := p[:idx]
146 prop := p[idx+1:]
147
148 return o.Call("org.freedesktop.DBus.Properties.Get", 0, iface, prop).
149 Store(value)
150 }
151
152
153
154 func (o *Object) SetProperty(p string, v interface{}) error {
155 idx := strings.LastIndex(p, ".")
156 if idx == -1 || idx+1 == len(p) {
157 return errors.New("dbus: invalid property " + p)
158 }
159
160 iface := p[:idx]
161 prop := p[idx+1:]
162
163 return o.Call("org.freedesktop.DBus.Properties.Set", 0, iface, prop, v).Err
164 }
165
166
167 func (o *Object) Destination() string {
168 return o.dest
169 }
170
171
172 func (o *Object) Path() ObjectPath {
173 return o.path
174 }
175
View as plain text