/
hal_hid.go
76 lines (65 loc) · 1.38 KB
/
hal_hid.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
package atecc
import (
"context"
"errors"
"fmt"
"io"
"github.com/karalabe/usb"
)
// NewHIDDev returns an object that communicates over HID.
func NewHIDDev(ctx context.Context, cfg IfaceConfig) (*Dev, io.Closer, error) {
if !usb.Supported() {
return nil, nil, errors.New("atecc: hid is not supported")
}
deviceInfos, err := usb.EnumerateHid(cfg.HID.VendorID, cfg.HID.ProductID)
if err != nil {
return nil, nil, fmt.Errorf("atecc: failed to get hid devices: %w", err)
}
for _, di := range deviceInfos {
hid, e := di.Open()
if e != nil {
err = e
continue
}
phy := newHALHID(hid, cfg)
hal, err := newHALKit(ctx, phy, cfg)
if err != nil {
return nil, nil, err
}
var d *Dev
if d, err = New(ctx, hal, cfg); err != nil {
return nil, nil, err
}
return d, hid, err
}
if err != nil {
return nil, nil, fmt.Errorf("atecc: %w", err)
} else {
return nil, nil, errors.New("atecc: no hid devices found")
}
}
type halHID struct {
usb usb.Device
cfg IfaceConfig
}
func newHALHID(
usb usb.Device,
cfg IfaceConfig,
) *halHID {
return &halHID{
usb: usb,
cfg: cfg,
}
}
func (h *halHID) Write(p []byte) (int, error) {
return h.usb.Write(p)
}
func (h *halHID) Read(p []byte) (int, error) {
return h.usb.Read(p)
}
func (h *halHID) Idle() error {
return errors.New("not implemented")
}
func (h *halHID) Wake() error {
return errors.New("not implemented")
}