-
Notifications
You must be signed in to change notification settings - Fork 85
/
ops.go
220 lines (186 loc) · 5.92 KB
/
ops.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
package mtp
import (
"bytes"
"fmt"
"io"
"log"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// OpenSession opens a session, which is necesary for any command that
// queries or modifies storage. It is an error to open a session
// twice. If OpenSession() fails, it will not attempt to close the device.
func (d *Device) OpenSession() error {
if d.session != nil {
return fmt.Errorf("session already open")
}
var req, rep Container
req.Code = OC_OpenSession
// avoid 0xFFFFFFFF and 0x00000000 for session IDs.
sid := uint32(rand.Int31()) | 1
req.Param = []uint32{sid} // session
// If opening the session fails, we want to be able to reset
// the device, so don't do sanity checks afterwards.
if err := d.runTransaction(&req, &rep, nil, nil, 0); err != nil {
return err
}
d.session = &sessionData{
tid: 1,
sid: sid,
}
return nil
}
// Closes a sessions. This is done automatically if the device is closed.
func (d *Device) CloseSession() error {
var req, rep Container
req.Code = OC_CloseSession
err := d.RunTransaction(&req, &rep, nil, nil, 0)
d.session = nil
return err
}
func (d *Device) GetData(req *Container, info interface{}) error {
var buf bytes.Buffer
var rep Container
if err := d.RunTransaction(req, &rep, &buf, nil, 0); err != nil {
return err
}
err := Decode(&buf, info)
if d.MTPDebug && err == nil {
log.Printf("MTP decoded %#v", info)
}
return err
}
func (d *Device) GetDeviceInfo(info *DeviceInfo) error {
var req Container
req.Code = OC_GetDeviceInfo
return d.GetData(&req, info)
}
func (d *Device) GetStorageIDs(info *Uint32Array) error {
var req Container
req.Code = OC_GetStorageIDs
return d.GetData(&req, info)
}
func (d *Device) GetObjectPropDesc(objPropCode, objFormatCode uint16, info *ObjectPropDesc) error {
var req Container
req.Code = OC_MTP_GetObjectPropDesc
req.Param = []uint32{uint32(objPropCode), uint32(objFormatCode)}
return d.GetData(&req, info)
}
func (d *Device) GetObjectPropValue(objHandle uint32, objPropCode uint16, value interface{}) error {
var req Container
req.Code = OC_MTP_GetObjectPropValue
req.Param = []uint32{objHandle, uint32(objPropCode)}
return d.GetData(&req, value)
}
func (d *Device) SetObjectPropValue(objHandle uint32, objPropCode uint16, value interface{}) error {
var req, rep Container
req.Code = OC_MTP_SetObjectPropValue
req.Param = []uint32{objHandle, uint32(objPropCode)}
return d.SendData(&req, &rep, value)
}
func (d *Device) SendData(req *Container, rep *Container, value interface{}) error {
var buf bytes.Buffer
if err := Encode(&buf, value); err != nil {
return err
}
if d.MTPDebug {
log.Printf("MTP encoded %#v", value)
}
return d.RunTransaction(req, rep, nil, &buf, int64(buf.Len()))
}
func (d *Device) GetObjectPropsSupported(objFormatCode uint16, props *Uint16Array) error {
var req Container
req.Code = OC_MTP_GetObjectPropsSupported
req.Param = []uint32{uint32(objFormatCode)}
return d.GetData(&req, props)
}
func (d *Device) GetDevicePropDesc(propCode uint16, info *DevicePropDesc) error {
var req Container
req.Code = OC_GetDevicePropDesc
req.Param = append(req.Param, uint32(propCode))
return d.GetData(&req, info)
}
func (d *Device) SetDevicePropValue(propCode uint32, src interface{}) error {
var req, rep Container
req.Code = OC_SetDevicePropValue
req.Param = []uint32{propCode}
return d.SendData(&req, &rep, src)
}
func (d *Device) GetDevicePropValue(propCode uint32, dest interface{}) error {
var req Container
req.Code = OC_GetDevicePropValue
req.Param = []uint32{propCode}
return d.GetData(&req, dest)
}
func (d *Device) ResetDevicePropValue(propCode uint32) error {
var req, rep Container
req.Code = OC_ResetDevicePropValue
req.Param = []uint32{propCode}
return d.RunTransaction(&req, &rep, nil, nil, 0)
}
func (d *Device) GetStorageInfo(ID uint32, info *StorageInfo) error {
var req Container
req.Code = OC_GetStorageInfo
req.Param = []uint32{ID}
return d.GetData(&req, info)
}
func (d *Device) GetObjectHandles(storageID, objFormatCode, parent uint32, info *Uint32Array) error {
var req Container
req.Code = OC_GetObjectHandles
req.Param = []uint32{storageID, objFormatCode, parent}
return d.GetData(&req, info)
}
func (d *Device) GetObjectInfo(handle uint32, info *ObjectInfo) error {
var req Container
req.Code = OC_GetObjectInfo
req.Param = []uint32{handle}
return d.GetData(&req, info)
}
func (d *Device) GetNumObjects(storageId uint32, formatCode uint16, parent uint32) (uint32, error) {
var req, rep Container
req.Code = OC_GetNumObjects
req.Param = []uint32{storageId, uint32(formatCode), parent}
if err := d.RunTransaction(&req, &rep, nil, nil, 0); err != nil {
return 0, err
}
return rep.Param[0], nil
}
func (d *Device) DeleteObject(handle uint32) error {
var req, rep Container
req.Code = OC_DeleteObject
req.Param = []uint32{handle, 0x0}
return d.RunTransaction(&req, &rep, nil, nil, 0)
}
func (d *Device) SendObjectInfo(wantStorageID, wantParent uint32, info *ObjectInfo) (storageID, parent, handle uint32, err error) {
var req, rep Container
req.Code = OC_SendObjectInfo
req.Param = []uint32{wantStorageID, wantParent}
if err = d.SendData(&req, &rep, info); err != nil {
return
}
if len(rep.Param) < 3 {
err = fmt.Errorf("SendObjectInfo: got %v, need 3 response parameters", rep.Param)
return
}
return rep.Param[0], rep.Param[1], rep.Param[2], nil
}
func (d *Device) SendObject(r io.Reader, size int64) error {
var req, rep Container
req.Code = OC_SendObject
return d.RunTransaction(&req, &rep, nil, r, size)
}
func (d *Device) GetObject(handle uint32, w io.Writer) error {
var req, rep Container
req.Code = OC_GetObject
req.Param = []uint32{handle}
return d.RunTransaction(&req, &rep, w, nil, 0)
}
func (d *Device) GetPartialObject(handle uint32, w io.Writer, offset uint32, size uint32) error {
var req, rep Container
req.Code = OC_ANDROID_GET_PARTIAL_OBJECT64
req.Param = []uint32{handle, offset, size}
return d.RunTransaction(&req, &rep, w, nil, 0)
}