forked from oandrew/ipod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
report_def.go
104 lines (90 loc) · 2.93 KB
/
report_def.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
package hid
import (
"errors"
"fmt"
)
// ReportDir is the report direction
type ReportDir uint8
const (
// Device to host (ipod->accessory)
ReportDirAccIn ReportDir = 0
// Host to device (accessory->ipod)
ReportDirAccOut ReportDir = 1
)
// ReportDef represents a hid report type from the descriptor
type ReportDef struct {
// id
ID int
// lenth of the report not including the id.
Len int
// direction
Dir ReportDir
}
// MaxPayload returns the maximum payload that a report can fit
func (def ReportDef) MaxPayload() int {
return def.Len - 1
}
// ReportDefs is a collection of ReportDef
type ReportDefs []ReportDef
// DefaultReportDefs is a default set of report types
// for use with ipod-gadget
var DefaultReportDefs = ReportDefs{
ReportDef{ID: 0x01, Len: 12, Dir: ReportDirAccIn},
ReportDef{ID: 0x02, Len: 14, Dir: ReportDirAccIn},
ReportDef{ID: 0x03, Len: 20, Dir: ReportDirAccIn},
ReportDef{ID: 0x04, Len: 63, Dir: ReportDirAccIn},
ReportDef{ID: 0x05, Len: 8, Dir: ReportDirAccIn},
ReportDef{ID: 0x06, Len: 10, Dir: ReportDirAccIn},
ReportDef{ID: 0x07, Len: 14, Dir: ReportDirAccIn},
ReportDef{ID: 0x08, Len: 20, Dir: ReportDirAccIn},
ReportDef{ID: 0x09, Len: 63, Dir: ReportDirAccIn},
}
var LegacyReportDefs = ReportDefs{
ReportDef{ID: 0x01, Len: 5, Dir: ReportDirAccIn},
ReportDef{ID: 0x02, Len: 9, Dir: ReportDirAccIn},
ReportDef{ID: 0x03, Len: 13, Dir: ReportDirAccIn},
ReportDef{ID: 0x04, Len: 17, Dir: ReportDirAccIn},
ReportDef{ID: 0x05, Len: 25, Dir: ReportDirAccIn},
ReportDef{ID: 0x06, Len: 49, Dir: ReportDirAccIn},
ReportDef{ID: 0x07, Len: 95, Dir: ReportDirAccIn},
ReportDef{ID: 0x08, Len: 193, Dir: ReportDirAccIn},
ReportDef{ID: 0x09, Len: 257, Dir: ReportDirAccIn},
ReportDef{ID: 0x0A, Len: 385, Dir: ReportDirAccIn},
ReportDef{ID: 0x0B, Len: 513, Dir: ReportDirAccIn},
ReportDef{ID: 0x0C, Len: 767, Dir: ReportDirAccIn},
ReportDef{ID: 0x0D, Len: 5, Dir: ReportDirAccOut},
ReportDef{ID: 0x0E, Len: 9, Dir: ReportDirAccOut},
ReportDef{ID: 0x0F, Len: 13, Dir: ReportDirAccOut},
ReportDef{ID: 0x10, Len: 17, Dir: ReportDirAccOut},
ReportDef{ID: 0x11, Len: 25, Dir: ReportDirAccOut},
ReportDef{ID: 0x12, Len: 49, Dir: ReportDirAccOut},
ReportDef{ID: 0x13, Len: 95, Dir: ReportDirAccOut},
ReportDef{ID: 0x14, Len: 193, Dir: ReportDirAccOut},
ReportDef{ID: 0x15, Len: 255, Dir: ReportDirAccOut},
}
// Pick finds the best matching report type
func (defs ReportDefs) Pick(payloadSize int, dir ReportDir) (ReportDef, error) {
var def *ReportDef
for i := range defs {
if defs[i].Dir == dir {
def = &defs[i]
if defs[i].MaxPayload() >= payloadSize {
break
}
}
}
if def == nil {
return ReportDef{}, errors.New("no matching report found")
} else {
return *def, nil
}
}
// Find finds the report type based on id
func (defs ReportDefs) Find(id int) (ReportDef, error) {
for i := range defs {
if defs[i].ID == id {
return defs[i], nil
}
}
return ReportDef{}, fmt.Errorf("report id no found: %#v", id)
}