Skip to content

Commit cde8db3

Browse files
committed
Isolation the logic for doing complex transfers into a separate file
1 parent af26a4f commit cde8db3

File tree

3 files changed

+77
-50
lines changed

3 files changed

+77
-50
lines changed

src/org/cgutman/usbip/service/UsbIpService.java

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.cgutman.usbip.server.protocol.dev.UsbIpSubmitUrbReply;
2222
import org.cgutman.usbip.usb.DescriptorReader;
2323
import org.cgutman.usbip.usb.UsbDeviceDescriptor;
24+
import org.cgutman.usbip.usb.XferUtils;
2425

2526
import android.annotation.SuppressLint;
2627
import android.app.PendingIntent;
@@ -286,46 +287,18 @@ public void run() {
286287
buff.array().length, msg.direction == UsbIpDevicePacket.USBIP_DIR_IN ? "in" : "out",
287288
selectedEndpoint.getEndpointNumber());
288289

289-
// Bulk is handled via a helper method
290-
byte[] bulkBuffer = buff.array();
291-
292-
int bytesTransferred = 0;
293-
while (bytesTransferred < bulkBuffer.length) {
294-
byte[] remainingBuffer = new byte[bulkBuffer.length - bytesTransferred];
295-
296-
if (msg.direction == UsbIpDevicePacket.USBIP_DIR_OUT) {
297-
// Copy input data into the new buffer
298-
System.arraycopy(bulkBuffer, bytesTransferred, remainingBuffer, 0, remainingBuffer.length);
299-
}
300-
301-
int res = context.devConn.bulkTransfer(selectedEndpoint, remainingBuffer,
302-
remainingBuffer.length, msg.interval);
303-
if (res < 0) {
304-
// Failed transfer terminates the bulk transfer
305-
System.err.println("Bulk Xfer failed: "+res);
306-
sendReply(replyOut, reply, ProtoDefs.ST_NA);
307-
return;
308-
}
309-
310-
if (msg.direction == UsbIpDevicePacket.USBIP_DIR_IN) {
311-
// Copy output data into the original buffer
312-
System.arraycopy(remainingBuffer, 0, bulkBuffer, bytesTransferred, res);
313-
}
314-
315-
bytesTransferred += res;
316-
317-
if (res < selectedEndpoint.getMaxPacketSize()) {
318-
// A packet less than the maximum size for this endpoint
319-
// indicates the transfer has ended
320-
break;
321-
}
322-
}
290+
int res = XferUtils.doBulkTransfer(context.devConn,selectedEndpoint, buff.array(), msg.interval);
323291

324292
System.out.printf("Bulk transfer complete with %d bytes (wanted %d)\n",
325-
bytesTransferred, msg.transferBufferLength);
293+
res, msg.transferBufferLength);
326294

327-
reply.actualLength = bytesTransferred;
328-
reply.status = ProtoDefs.ST_OK;
295+
if (res < 0) {
296+
reply.status = ProtoDefs.ST_NA;
297+
}
298+
else {
299+
reply.actualLength = res;
300+
reply.status = ProtoDefs.ST_OK;
301+
}
329302
sendReply(replyOut, reply, reply.status);
330303
}
331304
else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
@@ -429,25 +402,22 @@ public void submitUrbRequest(OutputStream replyOut, UsbIpSubmitUrb msg) {
429402
short value = bb.getShort();
430403
short index = bb.getShort();
431404
short length = bb.getShort();
432-
433-
System.out.printf("%x %x %x %x %x\n",
434-
requestType, request, value, index, length);
435405

436406
if (length != 0) {
437407
reply.inData = new byte[length];
438408
}
439-
int res = devConn.controlTransfer(requestType, request, value,
440-
index,
441-
(requestType & 0x80) != 0 ? reply.inData : msg.outData,
442-
length, msg.interval);
409+
410+
int res = XferUtils.doControlTransfer(devConn, requestType, request, value, index,
411+
(requestType & 0x80) != 0 ? reply.inData : msg.outData, length, msg.interval);
443412
if (res < 0) {
444-
System.err.println("Control Xfer failed: "+res);
445-
sendReply(replyOut, reply, ProtoDefs.ST_NA);
446-
return;
413+
reply.status = ProtoDefs.ST_NA;
414+
}
415+
else {
416+
reply.actualLength = res;
417+
reply.status = ProtoDefs.ST_OK;
447418
}
448419

449-
reply.actualLength = res;
450-
sendReply(replyOut, reply, ProtoDefs.ST_OK);
420+
sendReply(replyOut, reply, reply.status);
451421
return;
452422
}
453423
else {

src/org/cgutman/usbip/usb/DescriptorReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static UsbDeviceDescriptor readDeviceDescriptor(UsbDeviceConnection devCo
1313
byte[] descriptorBuffer = new byte[UsbDeviceDescriptor.DESCRIPTOR_SIZE];
1414

1515

16-
int res = devConn.controlTransfer(GET_DESCRIPTOR_REQUEST_TYPE,
16+
int res = XferUtils.doControlTransfer(devConn, GET_DESCRIPTOR_REQUEST_TYPE,
1717
GET_DESCRIPTOR_REQUEST,
1818
(DEVICE_DESCRIPTOR_TYPE << 8) | 0x00, // Devices only have 1 descriptor
1919
0, descriptorBuffer, descriptorBuffer.length, 0);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.cgutman.usbip.usb;
2+
3+
import android.hardware.usb.UsbConstants;
4+
import android.hardware.usb.UsbDeviceConnection;
5+
import android.hardware.usb.UsbEndpoint;
6+
7+
public class XferUtils {
8+
9+
public static int doBulkTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int interval) {
10+
int bytesTransferred = 0;
11+
while (bytesTransferred < buff.length) {
12+
byte[] remainingBuffer = new byte[buff.length - bytesTransferred];
13+
14+
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
15+
// Copy input data into the new buffer
16+
System.arraycopy(buff, bytesTransferred, remainingBuffer, 0, remainingBuffer.length);
17+
}
18+
19+
int res = devConn.bulkTransfer(endpoint, remainingBuffer,
20+
remainingBuffer.length, interval);
21+
if (res < 0) {
22+
// Failed transfer terminates the bulk transfer
23+
System.err.println("Bulk Xfer failed: "+res);
24+
return -1;
25+
}
26+
27+
if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
28+
// Copy output data into the original buffer
29+
System.arraycopy(remainingBuffer, 0, buff, bytesTransferred, res);
30+
}
31+
32+
bytesTransferred += res;
33+
34+
if (res < endpoint.getMaxPacketSize()) {
35+
// A packet less than the maximum size for this endpoint
36+
// indicates the transfer has ended
37+
break;
38+
}
39+
}
40+
41+
return bytesTransferred;
42+
}
43+
44+
public static int doControlTransfer(UsbDeviceConnection devConn, int requestType,
45+
int request, int value, int index, byte[] buff, int length, int interval) {
46+
System.out.printf("SETUP: %x %x %x %x %x\n",
47+
requestType, request, value, index, length);
48+
49+
int res = devConn.controlTransfer(requestType, request, value,
50+
index, buff, length, interval);
51+
if (res < 0) {
52+
System.err.println("Control Xfer failed: "+res);
53+
}
54+
55+
return res;
56+
}
57+
}

0 commit comments

Comments
 (0)