-
Notifications
You must be signed in to change notification settings - Fork 2
/
Initiator.java
397 lines (335 loc) · 11.1 KB
/
Initiator.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package com.ash.beta;
//Copyright 2000 by David Brownell <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
import java.io.IOException;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.widget.TextView;
/**
* This supports all standardized PTP-over-USB operations, including
* operations (and modes) that are optional for all responders.
* Filtering operations invoked on this class may be done on the device,
* or may be emulated on the client side.
* At this time, not all standardized operations are supported.
*
* @version $Id: Initiator.java,v 1.9 2001/04/12 23:13:00 dbrownell Exp $
* @author David Brownell
*/
public class Initiator extends BaselineInitiator
{
/**
* This is essentially a class driver, following Annex D of
* the PTP specification.
* @param tv4
*/
public Initiator (UsbDevice dev, UsbManager mUsbManager, TextView tv4) throws IOException
{
this (getPtpInterface (dev), dev, mUsbManager, tv4 );
}
/**
* This is essentially a class driver, following Annex D of
* the PTP specification.
* @param tv4
*/
public Initiator (UsbInterface intf, UsbDevice dev, UsbManager mUsbManager, TextView tv4) throws IOException
{
super (intf, dev, mUsbManager, tv4);
}
public int copyObject (int handle, int storageId, int parent)
throws IOException
{
return transact3 (Command.CopyObject, null, handle, storageId, parent)
.getCode ();
}
// deleteAll () ... ~0, 0
// deleteAllByType (int formats) ... ~0, formats
// deleteObject (int handle) ... handle, 0 ... object or assoc/hierarchy
public int deleteObject (int handle, int formats)
throws IOException
{
return transact2 (Command.DeleteObject, null, handle, formats)
.getCode ();
}
/**
* Reformats the specified storage unit. All images (and other objects)
* on the media will be erased; write-protected objects will be erased,
* unlike invoking {@link #deleteObject}.
*
* @param storageId the storage unit to reformat
* @param formatCode normally zero. Otherwise, a value as defined
* in table 12 of the PTP specification, selecting among types of
* filesystem supported by the device (flat, hierarchical, DCF,
* vendor-defined, and so on).
* @return response code
*/
public int formatStore (int storageId, int formatCode)
throws IOException
{
return transact2 (Command.FormatStore, null, storageId, formatCode)
.getCode ();
}
/**
* Fills out the provided device property description.
*
* @param propcode code identifying the property of interest
* @param desc description to be filled; it may be a subtype
* associated with with domain-specific methods
* @return response code
*/
public int getDevicePropDesc (int propcode, DevicePropDesc desc)
throws IOException
{
return transact1 (Command.GetDevicePropDesc, desc, propcode)
.getCode ();
}
/**
* Gets the value of the specified device property.
* The same value is also seen through getDevicePropDesc.
*
* @param propcode code identifying the property of interest
* @param value the value to be filled
* @return response code
*/
public int getDevicePropValue (int propcode, DevicePropValue value)
throws IOException
{
Response response;
int code;
response = transact1 (Command.GetDevicePropValue, value, propcode);
return response.getCode ();
}
/**
* Returns an array of image handles in the store (or stores)
* specified; uses device-side filtering when it's available.
* Device-side filtering can substantially reduce USB traffic.
*
* @param storageID either ~0 to indicate all media, or the
* ID for some particular store.
* @param association either zero, or ~0 to specify the root
* of a store; or else an association handle.
*/
public int [] getImageHandles (int storageID, int association)
throws IOException
{
Data data = new Data (factory);
Response response = transact3 (Command.GetObjectHandles, data,
storageID, ~0, association);
// Some devices may support this filtering directly.
switch (response.getCode ()) {
case Response.OK:
return data.nextS32Array ();
case Response.SpecificationByFormatUnsupported:
break;
default:
throw new IOException (response.toString ());
}
// FIXME: client/server filter xparency for all format codes.
// For those that don't, do it ourselves.
int handles [];
int retval [];
int count = 0;
handles = getObjectHandles (storageID, 0, association);
for (int i = 0; i < handles.length; i++) {
ObjectInfo info = getObjectInfo (handles [i]);
if (!info.isImage ())
handles [i] = 0;
else
count++;
}
retval = new int [count];
count = 0;
for (int i = 0; i < handles.length; i++) {
if (handles [i] != 0)
retval [count++] = handles [i];
}
return retval;
}
/**
* Returns the number of images in the specified association on
* the specified store; uses device-side filtering when it's available.
*
* @param storageID either ~0 to indicate all media, or the
* ID for some particular store.
* @param association 0, or the object handle for some association
*/
public int getNumImages (int storageID, int association)
throws IOException
{
Data data = new Data (factory);
Response response = transact3 (Command.GetNumObjects, data,
storageID, ~0, association);
switch (response.getCode ()) {
case Response.OK:
return response.getParam1 ();
case Response.SpecificationByFormatUnsupported:
break;
default:
throw new IOException (response.toString ());
}
return getImageHandles (storageID, association).length;
}
public Response getPartialObject (int handle, int offset, int count)
throws IOException
{
return transact3 (Command.GetPartialObject, null,
handle, offset, count);
}
/**
* Starts the capture of one (or more) new
* data objects, according to current device properties.
* The capture will complete without issuing further commands.
*
* @see #initiateOpenCapture
*
* @param storageId Where to store the object(s), or zero to
* let the device choose.
* @param formatCode Type of object(s) to capture, or zero to
* use the device default.
*
* @return status code indicating whether capture started;
* CaptureComplete events provide capture status, and
* ObjectAdded events provide per-object status.
*/
public int initiateCapture (int storageId, int formatCode)
throws IOException
{
return transact2 (Command.InitiateCapture, null, storageId, formatCode)
.getCode ();
}
/**
* Starts an open-ended capture of new data objects, according to
* current device properties. Capturing
* will continue until {@link #terminateOpenCapture} is invoked.
* Intended applications include "manually" controlled exposures,
* and capture of time-based data such as audio or video.
*
* @see #initiateCapture
*
* @param storageId Where to store the object(s), or zero to
* let the device choose.
* @param formatCode Type of object(s) to capture, or zero to
* use the device default.
*
* @return status code indicating whether capture started;
* CaptureComplete events provide capture status, and
* ObjectAdded events provide per-object status.
*/
public int initiateOpenCapture (int storageId, int formatCode)
throws IOException
{
return transact2 (Command.InitiateOpenCapture, null,
storageId, formatCode)
.getCode ();
}
public int moveObject (int handle, int storageId, int parent)
throws IOException
{
return transact3 (Command.MoveObject, null, handle, storageId, parent)
.getCode ();
}
/**
* Sends a PowerDown command to the device, causing it to power
* down and close all currently open sessions.
*
* @return response code
*/
public int powerDown ()
throws IOException
{
Response response = transact0 (Command.PowerDown, null);
int code;
code = response.getCode ();
if (code == Response.OK) {
// FIXME: mark session as closed
}
return code;
}
/**
* Sends a ResetDevice command to the device, putting it into a
* default state and closing all open sessions.
*/
public int resetDevice ()
throws IOException
{
Response response = transact0 (Command.ResetDevice, null);
int code;
// FIXME: how is this different from class-specific reset() ??
// This issue should get addressed in v1.1 of the PTP spec. Ideally,
// when PTP runs over USB resetDevice is mapped to the class request.
code = response.getCode ();
if (code == Response.OK) {
// FIXME: mark session as closed
}
return code;
}
/**
* Resets the value of the specified device property to
* the factory default.
*
* @param propcode code identifying the property of interest
* @return response code
*/
public int resetDevicePropValue (int propcode)
throws IOException
{
return transact1 (Command.SetDevicePropValue, null, propcode)
.getCode ();
}
/**
* Sends a SelfTest command to the device.
*
* @param type typically zero, or a vendor-defined code
* @return response code
*/
public int selfTest (int type)
throws IOException
{
return transact1 (Command.SelfTest, null, type)
.getCode ();
}
/**
* Sets the value of the specified device property.
* The same value is also seen through getDevicePropDesc.
*
* @param propcode code identifying the property of interest
* @param value the value to be assigned
* @return response code
*/
public int setDevicePropValue (int propcode, DevicePropValue value)
throws IOException
{
return transact1 (Command.SetDevicePropValue, value, propcode)
.getCode ();
}
public int setObjectProtection (int handle, int status)
throws IOException
{
return transact2 (Command.SetObjectProtection, null, handle, status)
.getCode ();
}
/**
* Closes the current session's open capture operation.
* @see #initiateOpenCapture
*/
public int terminateOpenCapture ()
throws IOException
{
return transact1 (Command.TerminateOpenCapture, null, getSessionId ())
.getCode ();
}
}