-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.go
237 lines (196 loc) · 5.37 KB
/
keyboard.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
package btk
import (
"encoding/hex"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/godbus/dbus"
"github.com/pkg/errors"
"github.com/zserge/hid"
)
const (
hidpHeaderTransMask = 0xf0
hidpTransHandshake = 0x00
hidpTransSetProtocol = 0x60
hidpTransData = 0xa0
hidpHshkSuccessful = 0x00
hidpHshkErrUnknown = 0x0e
protocolKeyboard = 1
protocolMouse = 2
)
func getFirstKeyboard() (kb hid.Device, found bool) {
hid.UsbWalk(func(d hid.Device) {
if found {
return
}
if d.Info().Protocol == protocolKeyboard {
kb = d
found = true
}
})
return
}
// Client represents a bluetooth client
type Client struct {
Dev dbus.ObjectPath
Sintr *Bluetooth
Sctrl *Bluetooth
Done chan struct{}
}
// Keyboard represents a HID keyboard
type Keyboard struct {
sync.Mutex
client *Client
dev hid.Device
sdp string
once sync.Once
}
// Desc returns the HID descriptor of the usb keyboard
func (kb *Keyboard) Desc() string {
return kb.sdp
}
// NewKeyboard returns a new keyboard on the first usb keyboard connected.
func NewKeyboard() (*Keyboard, error) {
dev, ok := getFirstKeyboard()
if !ok {
return nil, errors.New("no hid keyboard found")
}
if err := dev.Open(); err != nil {
return nil, errors.Wrap(err, "failed to open hid device")
}
desc, err := dev.HIDReport()
if err != nil {
return nil, errors.Wrap(err, "failed to get HID descriptor")
}
return &Keyboard{
dev: dev,
sdp: hex.EncodeToString(desc),
}, nil
}
// Client returns the current bluetooth client of the keyboard
func (kb *Keyboard) Client() *Client {
kb.Lock()
defer kb.Unlock()
return kb.client
}
// HandleHID starts a loop to read from the usb keyboard, it blocks until there's
// a fatal error reading from the keyboard, e.g. keyboard disconnection
func (kb *Keyboard) HandleHID() {
defer kb.dev.Close()
for {
// Set timeout to 1 second, so read does not block forever
state, err := kb.dev.Read(-1, time.Second)
if err != nil {
// connection timeout is normal when the keyboard is idle.
// Although inspecting the error message is not a good
// way to check the error, we'll get on with it to
// prevent the too many debug log
if err.Error() != "connection timed out" {
logrus.WithError(err).Errorln("Error in read from keyboard")
}
// TODO: handle fatal error like device disconnection
continue
}
logrus.WithField("state", state).Debugln("Keyboard input")
client := kb.Client()
if client == nil {
continue
}
if _, err := client.Sintr.Write(append([]byte{0xA1}, state...)); err != nil {
logrus.WithError(err).Errorln("Error in write to client")
continue
}
}
}
// Stop close the usb keyboard
func (kb *Keyboard) Stop() {
kb.once.Do(func() {
// Violently close the usb keyboard, HandleHID() will exit on error
kb.dev.Close()
logrus.Warnln("Keyboard stopped")
})
}
// Connect hooks up the given client with the usb keyboard, and start piping
// keypresses to the client. Will return an error if the keyboard is already
// in use
func (kb *Keyboard) Connect(client *Client) error {
kb.Lock()
defer kb.Unlock()
// Only support one connection at a time, since controlling more than
// one device with one keyboard is typically not what we want
if kb.client != nil {
return errors.New("keyboard in use")
}
kb.client = client
if _, err := client.Sctrl.Write([]byte{0xA1, 0x13, 0x03}); err != nil {
return errors.Wrap(err, "failed to send hello on ctrl 1")
}
if _, err := client.Sctrl.Write([]byte{0xA1, 0x13, 0x02}); err != nil {
return errors.Wrap(err, "failed to send hello on ctrl 2")
}
go kb.handleHandshake()
return nil
}
// handleHandshake handles bluetooth handshake messages, and it's also an
// indicator of client disconnection
func (kb *Keyboard) handleHandshake() {
client := kb.client
if client == nil {
return
}
logger := logrus.WithField("client", client.Dev)
logger.Debugln("Start handling handshake")
for {
select {
case <-client.Done:
logger.Debugln("Exit handling handshake")
return
default:
}
r := make([]byte, BUFSIZE)
d, err := client.Sctrl.Read(r)
if err != nil || d < 1 {
// a read error means the client has disconnected
logger.WithError(err).WithField("read", d).
Errorln("Failed to read from sctrl")
kb.Disconnect(client)
continue
}
hsk := []byte{hidpTransHandshake}
msgTyp := r[0] & hidpHeaderTransMask
switch {
case (msgTyp & hidpTransSetProtocol) != 0:
logger.Debugln("handshake set protocol")
hsk[0] |= hidpHshkSuccessful
if _, err := client.Sctrl.Write(hsk); err != nil {
logger.WithError(err).Debugln("handshake set protocol failed")
}
case (msgTyp & hidpTransData) != 0:
logger.Debugln("handshake data")
default:
logger.Debugln("unknown handshake message")
hsk[0] |= hidpHshkErrUnknown
client.Sctrl.Write(hsk)
}
}
}
// Disconnect closes the connection to the given bluetooth client
// Currently this is just some cleanning up. It can't close the actual
// bluetooth connection, and will block on the attempt
// TODO: Find a way to close the connection
func (kb *Keyboard) Disconnect(client *Client) error {
kb.Lock()
defer kb.Unlock()
if client == nil || client.Dev != kb.client.Dev {
return nil
}
logrus.WithField("client", client.Dev).Infoln("Disconnecting")
defer func() {
close(kb.client.Done)
kb.client = nil
}()
if err := client.Sctrl.Close(); err != nil {
return err
}
return client.Sintr.Close()
}