forked from oandrew/ipod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
278 lines (236 loc) · 7.85 KB
/
handler.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
267
268
269
270
271
272
273
274
275
276
277
278
package general
import (
"bytes"
"github.com/teostofell/ipod"
)
type DeviceGeneral interface {
UIMode() UIMode
SetUIMode(UIMode)
Name() string
SoftwareVersion() (major, minor, rev uint8)
SerialNum() string
LingoProtocolVersion(lingo uint8) (major, minor uint8)
LingoOptions(ling uint8) uint64
PrefSettingID(classID uint8) uint8
SetPrefSettingID(classID, settingID uint8, restoreOnExit bool)
StartIDPS()
EndIDPS(status AccEndIDPSStatus)
SetToken(token FIDTokenValue) error
AccAuthCert(cert []byte)
SetEventNotificationMask(mask uint64)
EventNotificationMask() uint64
SupportedEventNotificationMask() uint64
CancelCommand(lingo uint8, cmd uint16, transaction uint16)
MaxPayload() uint16
}
func ackSuccess(req *ipod.Command) *ACK {
return &ACK{Status: ACKStatusSuccess, CmdID: uint8(req.ID.CmdID())}
}
func ackPending(req *ipod.Command, maxWait uint32) *ACKPending {
return &ACKPending{Status: ACKStatusPending, CmdID: uint8(req.ID.CmdID()), MaxWait: maxWait}
}
func ack(req *ipod.Command, status ACKStatus) *ACK {
return &ACK{Status: status, CmdID: uint8(req.ID.CmdID())}
}
func ackFIDTokenValue(tokenValue FIDTokenValue) FIDTokenValueACK {
ackToken := func(token interface{}) interface{} {
switch t := token.(type) {
case *FIDIdentifyToken:
return []byte{0x00}
case *FIDAccCapsToken:
return []byte{0x00}
case *FIDAccInfoToken:
return []byte{0x00, t.AccInfoType}
case *FIDiPodPreferenceToken:
return []byte{0x00, t.PrefClass}
case *FIDEAProtocolToken:
return []byte{0x00, t.ProtocolIndex}
case *FIDBundleSeedIDPrefToken:
return []byte{0x00}
case *FIDScreenInfoToken:
return []byte{0x00}
case *FIDEAProtocolMetadataToken:
return []byte{0x00}
case *FIDMicrophoneCapsToken:
return []byte{0x00}
default:
return nil
}
}
return FIDTokenValueACK{
ID: tokenValue.ID,
ACK: ackToken(tokenValue.Token),
}
}
func ackFIDTokenValues(tokens *SetFIDTokenValues) *RetFIDTokenValueACKs {
acks := make([]FIDTokenValueACK, len(tokens.FIDTokenValues))
for i := range tokens.FIDTokenValues {
acks[i] = ackFIDTokenValue(tokens.FIDTokenValues[i])
}
return &RetFIDTokenValueACKs{
FIDTokenValueACKs: acks,
}
}
var accCertBuf bytes.Buffer
func HandleGeneral(req *ipod.Command, tr ipod.CommandWriter, dev DeviceGeneral) error {
switch msg := req.Payload.(type) {
case *RequestRemoteUIMode:
ipod.Respond(req, tr, &ReturnRemoteUIMode{
Mode: ipod.BoolToByte(dev.UIMode() == UIModeExtended),
})
case *EnterRemoteUIMode:
if dev.UIMode() == UIModeExtended {
ipod.Respond(req, tr, ackSuccess(req))
} else {
ipod.Respond(req, tr, ackPending(req, 300))
dev.SetUIMode(UIModeExtended)
ipod.Respond(req, tr, ackSuccess(req))
}
case *ExitRemoteUIMode:
if dev.UIMode() != UIModeExtended {
ipod.Respond(req, tr, ackSuccess(req))
} else {
ipod.Respond(req, tr, ackPending(req, 300))
dev.SetUIMode(UIModeStandart)
ipod.Respond(req, tr, ackSuccess(req))
}
case *RequestiPodName:
ipod.Respond(req, tr, &ReturniPodName{Name: ipod.StringToBytes(dev.Name())})
case *RequestiPodSoftwareVersion:
var resp ReturniPodSoftwareVersion
resp.Major, resp.Minor, resp.Rev = dev.SoftwareVersion()
ipod.Respond(req, tr, &resp)
case *RequestiPodSerialNum:
ipod.Respond(req, tr, &ReturniPodSerialNum{Serial: ipod.StringToBytes(dev.SerialNum())})
case *RequestiPodModelNum:
ipod.Respond(req, tr, &ReturniPodModelNum{
// iphone 4
ModelID: 0x00111349,
Model: ipod.StringToBytes("MC676"),
})
case *RequestLingoProtocolVersion:
var resp ReturnLingoProtocolVersion
resp.Lingo = msg.Lingo
resp.Major, resp.Minor = dev.LingoProtocolVersion(msg.Lingo)
ipod.Respond(req, tr, &resp)
case *RequestTransportMaxPayloadSize:
ipod.Respond(req, tr, &ReturnTransportMaxPayloadSize{MaxPayload: dev.MaxPayload()})
case *IdentifyDeviceLingoes:
ipod.Respond(req, tr, ackSuccess(req))
if msg.DeviceID != 0x00 {
//ipod.Send(tr, &GetDevAuthenticationInfo{})
ipod.Respond(req, tr, &GetDevAuthenticationInfo{})
}
//GetDevAuthenticationInfo
case *RetDevAuthenticationInfo:
if msg.Major >= 2 {
if msg.CertCurrentSection == 0 {
accCertBuf.Reset()
}
accCertBuf.Write(msg.CertData)
if msg.CertCurrentSection < msg.CertMaxSection {
ipod.Respond(req, tr, ackSuccess(req))
} else {
ipod.Respond(req, tr, &AckDevAuthenticationInfo{Status: DevAuthInfoStatusSupported})
dev.AccAuthCert(accCertBuf.Bytes())
ipod.Respond(req, tr, &GetDevAuthenticationSignatureV2{Counter: 0})
}
} else {
ipod.Respond(req, tr, &AckDevAuthenticationInfo{Status: DevAuthInfoStatusSupported})
}
// GetDevAuthenticationSignatureV1
// case *RetDevAuthenticationSignatureV1:
// ipod.Respond(req, tr, ackDevAuthenticationStatus{Status: DevAuthStatusPassed})
// // GetDevAuthenticationSignatureV2
// case *RetDevAuthenticationSignatureV2:
// ipod.Respond(req, tr, ackDevAuthenticationStatus{Status: DevAuthStatusPassed})
case *RetDevAuthenticationSignature:
ipod.Respond(req, tr, &AckDevAuthenticationStatus{Status: DevAuthStatusPassed})
case *GetiPodAuthenticationInfo:
ipod.Respond(req, tr, &RetiPodAuthenticationInfo{
Major: 1, Minor: 1,
CertCurrentSection: 0, CertMaxSection: 0, CertData: []byte{},
})
case *AckiPodAuthenticationInfo:
// pass
case *GetiPodAuthenticationSignature:
ipod.Respond(req, tr, &RetiPodAuthenticationSignature{Signature: msg.Challenge})
case *AckiPodAuthenticationStatus:
// pass
// revisit
case *GetiPodOptions:
ipod.Respond(req, tr, &RetiPodOptions{Options: 0x00})
// GetAccessoryInfo
// check back might be useful
case *RetAccessoryInfo:
// pass
case *GetiPodPreferences:
ipod.Respond(req, tr, &RetiPodPreferences{
PrefClassID: msg.PrefClassID,
PrefClassSettingID: dev.PrefSettingID(msg.PrefClassID),
})
case *SetiPodPreferences:
dev.SetPrefSettingID(msg.PrefClassID, msg.PrefClassSettingID, ipod.ByteToBool(msg.RestoreOnExit))
ipod.Respond(req, tr, ackSuccess(req))
case *GetUIMode:
ipod.Respond(req, tr, &RetUIMode{UIMode: dev.UIMode()})
case *SetUIMode:
ipod.Respond(req, tr, ackSuccess(req))
case *StartIDPS:
ipod.TrxReset()
dev.StartIDPS()
ipod.Respond(req, tr, ackSuccess(req))
case *SetFIDTokenValues:
for _, token := range msg.FIDTokenValues {
dev.SetToken(token)
}
ipod.Respond(req, tr, ackFIDTokenValues(msg))
case *EndIDPS:
dev.EndIDPS(msg.AccEndIDPSStatus)
switch msg.AccEndIDPSStatus {
case AccEndIDPSStatusContinue:
ipod.Respond(req, tr, &IDPSStatus{Status: IDPSStatusOK})
ipod.Send(tr, &GetDevAuthenticationInfo{})
// get dev auth info
case AccEndIDPSStatusReset:
ipod.Respond(req, tr, &IDPSStatus{Status: IDPSStatusTimeLimitNotExceeded})
case AccEndIDPSStatusAbandon:
ipod.Respond(req, tr, &IDPSStatus{Status: IDPSStatusWillNotAccept})
case AccEndIDPSStatusNewLink:
//pass
}
// SetAccStatusNotification, RetAccStatusNotification
case *AccessoryStatusNotification:
// iPodNotification later
case *SetEventNotification:
dev.SetEventNotificationMask(msg.EventMask)
ipod.Respond(req, tr, ackSuccess(req))
case *GetiPodOptionsForLingo:
ipod.Respond(req, tr, &RetiPodOptionsForLingo{
LingoID: msg.LingoID,
Options: dev.LingoOptions(msg.LingoID),
})
case *GetEventNotification:
ipod.Respond(req, tr, &RetEventNotification{
EventMask: dev.EventNotificationMask(),
})
case *GetSupportedEventNotification:
ipod.Respond(req, tr, &RetSupportedEventNotification{
EventMask: dev.SupportedEventNotificationMask(),
})
case *CancelCommand:
dev.CancelCommand(msg.LingoID, msg.CmdID, msg.TransactionID)
ipod.Respond(req, tr, ackSuccess(req))
case *SetAvailableCurrent:
// notify acc
case *RequestApplicationLaunch:
ipod.Respond(req, tr, ack(req, ACKStatusFailed))
case *GetNowPlayingFocusApp:
ipod.Respond(req, tr, &RetNowPlayingFocusApp{AppID: ipod.StringToBytes("")})
case ipod.UnknownPayload:
ipod.Respond(req, tr, ack(req, ACKStatusUnkownID))
default:
_ = msg
}
return nil
}