-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidp.go
266 lines (237 loc) · 6.04 KB
/
hidp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package btk
import (
"bytes"
"fmt"
"syscall"
"text/template"
"github.com/Sirupsen/logrus"
"github.com/godbus/dbus"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
const sdpTpl = `
<?xml version="1.0" encoding="UTF-8" ?>
<record>
<attribute id="0x0001">
<sequence>
<uuid value="0x1124" />
</sequence>
</attribute>
<attribute id="0x0004">
<sequence>
<sequence>
<uuid value="0x0100" />
<uint16 value="0x0011" />
</sequence>
<sequence>
<uuid value="0x0011" />
</sequence>
</sequence>
</attribute>
<attribute id="0x0005">
<sequence>
<uuid value="0x1002" />
</sequence>
</attribute>
<attribute id="0x0006">
<sequence>
<uint16 value="0x656e" />
<uint16 value="0x006a" />
<uint16 value="0x0100" />
</sequence>
</attribute>
<attribute id="0x0009">
<sequence>
<sequence>
<uuid value="0x1124" />
<uint16 value="0x0100" />
</sequence>
</sequence>
</attribute>
<attribute id="0x000d">
<sequence>
<sequence>
<sequence>
<uuid value="0x0100" />
<uint16 value="0x0013" />
</sequence>
<sequence>
<uuid value="0x0011" />
</sequence>
</sequence>
</sequence>
</attribute>
<attribute id="0x0100">
<text value="Raspberry Pi Virtual Keyboard" />
</attribute>
<attribute id="0x0101">
<text value="USB > BT Keyboard" />
</attribute>
<attribute id="0x0102">
<text value="Raspberry Pi" />
</attribute>
<attribute id="0x0200">
<uint16 value="0x0100" />
</attribute>
<attribute id="0x0201">
<uint16 value="0x0111" />
</attribute>
<attribute id="0x0202">
<uint8 value="0x40" />
</attribute>
<attribute id="0x0203">
<uint8 value="0x00" />
</attribute>
<attribute id="0x0204">
<boolean value="true" />
</attribute>
<attribute id="0x0205">
<boolean value="true" />
</attribute>
<attribute id="0x0206">
<sequence>
<sequence>
<uint8 value="0x22" />
<text encoding="hex" value="{{.HIDDesc}}" />
</sequence>
</sequence>
</attribute>
<attribute id="0x0207">
<sequence>
<sequence>
<uint16 value="0x0409" />
<uint16 value="0x0100" />
</sequence>
</sequence>
</attribute>
<attribute id="0x020b">
<uint16 value="0x0100" />
</attribute>
<attribute id="0x020c">
<uint16 value="0x0c80" />
</attribute>
<attribute id="0x020d">
<boolean value="false" />
</attribute>
<attribute id="0x020e">
<boolean value="true" />
</attribute>
<attribute id="0x020f">
<uint16 value="0x0640" />
</attribute>
<attribute id="0x0210">
<uint16 value="0x0320" />
</attribute>
</record>
`
// HidProfile represents a dbus profile for the keyboard
type HidProfile struct {
bus *dbus.Conn
path dbus.ObjectPath
uid string
connIntr *Bluetooth
connection chan *Client
disconnection chan *Client
}
// Connection returns a channel of new bluetooth connection
func (p *HidProfile) Connection() chan *Client {
return p.connection
}
// Disconnection is designed to returns a channel of disconnction requests,
// but currently it doesn't work
func (p *HidProfile) Disconnection() chan *Client {
return p.disconnection
}
// Export exports the profile
func (p *HidProfile) Export() error {
return errors.Wrap(
p.bus.Export(p, p.path, "org.bluez.Profile1"),
"failed to export profile",
)
}
// Register registers the profile to dbus
func (p *HidProfile) Register(desc string) error {
callback := make(chan *dbus.Call, 1)
tpl, err := template.New("sdp").Parse(sdpTpl)
if err != nil {
return err
}
sdp := bytes.NewBuffer(nil)
if err := tpl.Execute(sdp, struct{ HIDDesc string }{desc}); err != nil {
return err
}
opts := map[string]dbus.Variant{
"PSM": dbus.MakeVariant(uint16(PSMCTRL)),
"RequireAuthentication": dbus.MakeVariant(true),
"RequireAuthorization": dbus.MakeVariant(true),
"ServiceRecord": dbus.MakeVariant(sdp.String()),
}
if err = p.bus.Object("org.bluez", "/org/bluez").Go(
"org.bluez.ProfileManager1.RegisterProfile",
0, callback, p.path, p.uid, opts,
).Err; err != nil {
return err
}
return (<-callback).Err
}
// Unregister unregisters the profile from dbus
func (p *HidProfile) Unregister() error {
return p.bus.Object("org.bluez", "/org/bluez").Call(
"org.bluez.ProfileManager1.UnregisterProfile",
0, p.path,
).Err
}
// NewHidProfile returns a new HidProfile on the given path
func NewHidProfile(path string) (*HidProfile, error) {
connIntr, err := ListenBluetooth(PSMINTR, 1, false)
if err != nil {
return nil, errors.Wrap(err, "failed to listen bluetooth")
}
bus, err := dbus.SystemBus()
if err != nil {
return nil, errors.Wrap(err, "failed to connect system bus")
}
return &HidProfile{
bus: bus,
path: (dbus.ObjectPath)(path),
connIntr: connIntr,
uid: uuid.NewV4().String(),
connection: make(chan *Client),
disconnection: make(chan *Client),
}, nil
}
// Release is called when the profile is unregisterd
func (p *HidProfile) Release() *dbus.Error {
logrus.Debugln("Release")
return nil
}
// NewConnection handles new bluetooth connections
func (p *HidProfile) NewConnection(dev dbus.ObjectPath, fd dbus.UnixFD, fdProps map[string]dbus.Variant) *dbus.Error {
logrus.Debugln("NewConnection", dev, fd, fdProps)
sintr, err := p.connIntr.Accept()
if err != nil {
logrus.WithError(err).Errorln("Accept failed")
p.connIntr.Close()
return dbus.NewError(fmt.Sprintf("Accept failed: %v", PSMINTR), []interface{}{err})
}
logrus.Infoln("New bluetooth connection")
sctrl, err := NewBluetoothSocket(int(fd))
if err != nil {
logrus.WithError(err).Errorln("Failed to create bluetooth socket")
syscall.Close(int(fd))
return dbus.NewError("failed to create bluetooth socket", []interface{}{err})
}
logrus.Infoln("New bluetooth socket created")
p.connection <- &Client{dev, sintr, sctrl, make(chan struct{})}
return nil
}
// RequestDisconnection ... I don't know how to use this yet
func (p *HidProfile) RequestDisconnection(dev dbus.ObjectPath) *dbus.Error {
logrus.WithField("device", dev).Infoln("Disconnection requested")
return nil
}
// Close shuts down the profile
func (p *HidProfile) Close() {
p.bus.Close()
p.connIntr.Close()
}