1
1
package org .cgutman .usbip .service ;
2
2
3
3
import java .io .IOException ;
4
- import java .io . OutputStream ;
4
+ import java .net . Socket ;
5
5
import java .nio .ByteBuffer ;
6
6
import java .nio .ByteOrder ;
7
7
import java .util .ArrayList ;
40
40
import android .hardware .usb .UsbInterface ;
41
41
import android .hardware .usb .UsbManager ;
42
42
import android .hardware .usb .UsbRequest ;
43
+ import android .net .wifi .WifiManager ;
44
+ import android .net .wifi .WifiManager .WifiLock ;
43
45
import android .os .Build ;
44
46
import android .os .IBinder ;
47
+ import android .os .PowerManager ;
48
+ import android .os .PowerManager .WakeLock ;
45
49
import android .util .SparseArray ;
46
50
47
51
public class UsbIpService extends Service implements UsbRequestHandler {
48
52
49
53
private UsbManager usbManager ;
54
+
50
55
private SparseArray <AttachedDeviceContext > connections ;
51
56
private SparseArray <Boolean > permission ;
52
57
private UsbIpServer server ;
58
+ private WakeLock cpuWakeLock ;
59
+ private WifiLock wifiLock ;
53
60
54
61
private static final int NOTIFICATION_ID = 100 ;
55
62
@@ -100,20 +107,39 @@ private void updateNotification() {
100
107
public void onCreate () {
101
108
super .onCreate ();
102
109
103
- usbManager = (UsbManager ) getSystemService (Context .USB_SERVICE );
110
+ usbManager = (UsbManager ) getSystemService (Context .USB_SERVICE );
104
111
connections = new SparseArray <AttachedDeviceContext >();
105
112
permission = new SparseArray <Boolean >();
106
113
107
114
usbPermissionIntent = PendingIntent .getBroadcast (this , 0 , new Intent (ACTION_USB_PERMISSION ), 0 );
108
115
IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION );
109
116
registerReceiver (usbReceiver , filter );
110
117
118
+ PowerManager pm = (PowerManager ) getSystemService (Context .POWER_SERVICE );
119
+ WifiManager wm = (WifiManager ) getSystemService (Context .WIFI_SERVICE );
120
+
121
+ cpuWakeLock = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , "USB/IP Service" );
122
+ cpuWakeLock .acquire ();
123
+
124
+ wifiLock = wm .createWifiLock (WifiManager .WIFI_MODE_FULL_HIGH_PERF , "USB/IP Service" );
125
+ wifiLock .acquire ();
126
+
111
127
server = new UsbIpServer ();
112
128
server .start (this );
113
129
114
130
updateNotification ();
115
131
}
116
132
133
+ public void onDestroy () {
134
+ super .onDestroy ();
135
+
136
+ server .stop ();
137
+ unregisterReceiver (usbReceiver );
138
+
139
+ wifiLock .release ();
140
+ cpuWakeLock .release ();
141
+ }
142
+
117
143
@ Override
118
144
public IBinder onBind (Intent intent ) {
119
145
// Not currently bindable
@@ -289,12 +315,12 @@ public static void dumpInterfaces(UsbDevice dev) {
289
315
}
290
316
}
291
317
292
- private static void sendReply (OutputStream out , UsbIpSubmitUrbReply reply , int status ) {
318
+ private static void sendReply (Socket s , UsbIpSubmitUrbReply reply , int status ) {
293
319
reply .status = status ;
294
320
try {
295
321
// We need to synchronize to avoid writing on top of ourselves
296
- synchronized (out ) {
297
- out .write (reply .serialize ());
322
+ synchronized (s ) {
323
+ s . getOutputStream () .write (reply .serialize ());
298
324
}
299
325
} catch (IOException e ) {
300
326
e .printStackTrace ();
@@ -303,7 +329,7 @@ private static void sendReply(OutputStream out, UsbIpSubmitUrbReply reply, int s
303
329
304
330
// FIXME: This dispatching could use some refactoring so we don't have to pass
305
331
// a million parameters to this guy
306
- private void dispatchRequest (final AttachedDeviceContext context , final OutputStream replyOut ,
332
+ private void dispatchRequest (final AttachedDeviceContext context , final Socket s ,
307
333
final UsbEndpoint selectedEndpoint , final ByteBuffer buff , final UsbIpSubmitUrb msg ) {
308
334
context .requestPool .submit (new Runnable () {
309
335
@ Override
@@ -333,7 +359,7 @@ public void run() {
333
359
reply .actualLength = res ;
334
360
reply .status = ProtoDefs .ST_OK ;
335
361
}
336
- sendReply (replyOut , reply , reply .status );
362
+ sendReply (s , reply , reply .status );
337
363
}
338
364
else if (selectedEndpoint .getType () == UsbConstants .USB_ENDPOINT_XFER_INT ) {
339
365
System .out .printf ("Interrupt transfer - %d bytes %s on EP %d\n " ,
@@ -352,7 +378,7 @@ else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
352
378
req .setClientData (urbCtxt );
353
379
if (!req .queue (buff , msg .transferBufferLength )) {
354
380
System .err .println ("Failed to queue request" );
355
- sendReply (replyOut , reply , ProtoDefs .ST_NA );
381
+ sendReply (s , reply , ProtoDefs .ST_NA );
356
382
req .close ();
357
383
return ;
358
384
}
@@ -397,30 +423,30 @@ else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
397
423
// so I'm not going to...
398
424
reply .startFrame = 0 ;
399
425
400
- sendReply (replyOut , reply , reply .status );
426
+ sendReply (s , reply , reply .status );
401
427
}
402
428
else {
403
429
System .err .println ("Unsupported endpoint type: " +selectedEndpoint .getType ());
404
- sendReply (replyOut , reply , ProtoDefs .ST_NA );
430
+ sendReply (s , reply , ProtoDefs .ST_NA );
405
431
}
406
432
}
407
433
});
408
434
}
409
435
410
436
@ Override
411
- public void submitUrbRequest (OutputStream replyOut , UsbIpSubmitUrb msg ) {
437
+ public void submitUrbRequest (Socket s , UsbIpSubmitUrb msg ) {
412
438
UsbIpSubmitUrbReply reply = new UsbIpSubmitUrbReply (msg .seqNum ,
413
439
msg .devId , msg .direction , msg .ep );
414
440
415
441
UsbDevice dev = getDevice (msg .devId );
416
442
if (dev == null ) {
417
- sendReply (replyOut , reply , ProtoDefs .ST_NA );
443
+ sendReply (s , reply , ProtoDefs .ST_NA );
418
444
return ;
419
445
}
420
446
421
447
AttachedDeviceContext context = connections .get (msg .devId );
422
448
if (context == null ) {
423
- sendReply (replyOut , reply , ProtoDefs .ST_NA );
449
+ sendReply (s , reply , ProtoDefs .ST_NA );
424
450
return ;
425
451
}
426
452
@@ -451,7 +477,7 @@ public void submitUrbRequest(OutputStream replyOut, UsbIpSubmitUrb msg) {
451
477
reply .status = ProtoDefs .ST_OK ;
452
478
}
453
479
454
- sendReply (replyOut , reply , reply .status );
480
+ sendReply (s , reply , reply .status );
455
481
return ;
456
482
}
457
483
else {
@@ -490,7 +516,7 @@ public void submitUrbRequest(OutputStream replyOut, UsbIpSubmitUrb msg) {
490
516
491
517
if (selectedEndpoint == null ) {
492
518
System .err .println ("EP not found: " +msg .ep );
493
- sendReply (replyOut , reply , ProtoDefs .ST_NA );
519
+ sendReply (s , reply , ProtoDefs .ST_NA );
494
520
return ;
495
521
}
496
522
@@ -505,7 +531,7 @@ public void submitUrbRequest(OutputStream replyOut, UsbIpSubmitUrb msg) {
505
531
}
506
532
507
533
// Dispatch this request asynchronously
508
- dispatchRequest (context , replyOut , selectedEndpoint , buff , msg );
534
+ dispatchRequest (context , s , selectedEndpoint , buff , msg );
509
535
}
510
536
}
511
537
0 commit comments