-
Notifications
You must be signed in to change notification settings - Fork 22
/
cmd.go
213 lines (177 loc) · 4.46 KB
/
cmd.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
package ipod
import (
"bytes"
"encoding"
"encoding/binary"
"errors"
"fmt"
"sync/atomic"
"log"
"github.com/sirupsen/logrus"
)
// UnknownPayload is a payload that represents an unknown command
type UnknownPayload []byte
type CommandReader interface {
ReadCommand() (*Command, error)
}
type CommandWriter interface {
WriteCommand(*Command) error
}
// Command represents iap packet payload
type Command struct {
ID LingoCmdID
//Optional
Transaction *Transaction
Payload interface{}
}
type Transaction uint16
func NewTransaction(t uint16) *Transaction {
tr := Transaction(t)
return &tr
}
func (tr Transaction) GoString() string {
return fmt.Sprintf("%#04x", tr)
}
func (tr Transaction) String() string {
return fmt.Sprintf("%#04x", uint16(tr))
}
func (tr *Transaction) Copy() *Transaction {
if tr != nil {
ctr := Transaction(*tr)
return &ctr
}
return nil
}
func (tr *Transaction) Delta(d int) *Transaction {
if tr != nil {
return NewTransaction(uint16(int(*tr) + d))
}
return nil
}
type CommandSerde struct {
TrxEnabled bool
}
func (s *CommandSerde) handleCmdID(cmdID LingoCmdID) {
prev := s.TrxEnabled
switch cmdID {
// RequestIdentify
case NewLingoCmdID(LingoGeneralID, 0x00):
s.TrxEnabled = false
// IdentifyDeviceLingoes
case NewLingoCmdID(LingoGeneralID, 0x13):
s.TrxEnabled = false
// StartIDPS
case NewLingoCmdID(LingoGeneralID, 0x38):
s.TrxEnabled = true
}
if prev != s.TrxEnabled {
if s.TrxEnabled {
logrus.Warn("Enabling transaction support")
} else {
logrus.Warn("Disabling transaction support")
}
}
}
func (s *CommandSerde) MarshalCmd(cmd *Command) ([]byte, error) {
pktBuf := &bytes.Buffer{}
if err := marshalLingoCmdID(pktBuf, cmd.ID); err != nil {
return nil, fmt.Errorf("ipod.Command marshal: %v", err)
}
s.handleCmdID(cmd.ID)
if s.TrxEnabled && cmd.Transaction != nil {
binary.Write(pktBuf, binary.BigEndian, *cmd.Transaction)
}
if cmd.Payload == nil {
return nil, fmt.Errorf("ipod.Command marshal: nil payload")
}
if d, ok := cmd.Payload.(encoding.BinaryMarshaler); ok {
payload, err := d.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("ipod.Command marshal: BinaryMarshaler: %v", err)
}
pktBuf.Write(payload)
} else {
err := binary.Write(pktBuf, binary.BigEndian, cmd.Payload)
if err != nil {
return nil, fmt.Errorf("ipod.Command marshal: binary.Write: %v", err)
}
}
return pktBuf.Bytes(), nil
}
func (s *CommandSerde) UnmarshalCmd(pkt []byte) (*Command, error) {
var cmd Command
pktBuf := bytes.NewBuffer(pkt)
if err := unmarshalLingoCmdID(pktBuf, &cmd.ID); err != nil {
return &cmd, fmt.Errorf("ipod.Command unmarshal: %v", err)
}
s.handleCmdID(cmd.ID)
lookup, ok := Lookup(cmd.ID, pktBuf.Len(), s.TrxEnabled)
if !ok {
cmd.Payload = UnknownPayload(pktBuf.Bytes())
return &cmd, fmt.Errorf("ipod.Command unmarshal: unknown cmd %v", cmd.ID)
}
if lookup.Transaction {
var tr Transaction
err := binary.Read(pktBuf, binary.BigEndian, &tr)
if err != nil {
return &cmd, err
}
cmd.Transaction = &tr
}
if d, ok := lookup.Payload.(encoding.BinaryUnmarshaler); ok {
err := d.UnmarshalBinary(pktBuf.Bytes())
if err != nil {
return &cmd, fmt.Errorf("ipod.Command unmarshal: BinaryUnmarshaler: %v", err)
}
} else {
err := binary.Read(pktBuf, binary.BigEndian, lookup.Payload)
if err != nil {
return &cmd, fmt.Errorf("ipod.Command unmarshal: binary.Read: %v", err)
}
}
//cmd.Payload = reflect.Indirect(reflect.ValueOf(lookup.Payload)).Interface()
cmd.Payload = lookup.Payload
return &cmd, nil
}
func BuildCommand(payload interface{}) (*Command, error) {
id, ok := LookupID(payload)
if !ok {
return nil, errors.New("payload not known")
}
return &Command{
ID: id,
Payload: payload,
}, nil
}
func Respond(req *Command, pw CommandWriter, payload interface{}) {
cmd, err := BuildCommand(payload)
if err != nil {
log.Printf("BuildCommand err: %v", err)
return
}
cmd.Transaction = req.Transaction.Copy()
pw.WriteCommand(cmd)
}
var trxCounter uint32
func TrxReset() {
atomic.StoreUint32(&trxCounter, 0)
}
func TrxNext() *Transaction {
trx := atomic.AddUint32(&trxCounter, 1)
return NewTransaction(uint16(trx))
}
func Send(pw CommandWriter, payload interface{}) {
cmd, err := BuildCommand(payload)
if err != nil {
return
}
cmd.Transaction = TrxNext()
pw.WriteCommand(cmd)
}
type CmdBuffer struct {
Commands []*Command
}
func (cbuf *CmdBuffer) WriteCommand(cmd *Command) error {
cbuf.Commands = append(cbuf.Commands, cmd)
return nil
}