The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRANSPORT |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "transport.h" |
| 21 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | static int remote_read(apacket *p, atransport *t) |
| 29 | { |
| 30 | if(usb_read(t->usb, &p->msg, sizeof(amessage))){ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 31 | D("remote usb: read terminated (message)"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | return -1; |
| 33 | } |
| 34 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 35 | if(check_header(p, t)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 36 | D("remote usb: check_header failed"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | if(p->msg.data_length) { |
| 41 | if(usb_read(t->usb, p->data, p->msg.data_length)){ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 42 | D("remote usb: terminated (data)"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if(check_data(p)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 48 | D("remote usb: check_data failed"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int remote_write(apacket *p, atransport *t) |
| 56 | { |
| 57 | unsigned size = p->msg.data_length; |
| 58 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | if(usb_write(t->usb, &p->msg, sizeof(amessage))) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 60 | D("remote usb: 1 - write terminated"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | if(p->msg.data_length == 0) return 0; |
| 64 | if(usb_write(t->usb, &p->data, size)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 65 | D("remote usb: 2 - write terminated"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static void remote_close(atransport *t) |
| 73 | { |
| 74 | usb_close(t->usb); |
| 75 | t->usb = 0; |
| 76 | } |
| 77 | |
| 78 | static void remote_kick(atransport *t) |
| 79 | { |
| 80 | usb_kick(t->usb); |
| 81 | } |
| 82 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 83 | void init_usb_transport(atransport *t, usb_handle *h, ConnectionState state) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 85 | D("transport: usb"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | t->close = remote_close; |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 87 | t->SetKickFunction(remote_kick); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | t->read_from_remote = remote_read; |
| 89 | t->write_to_remote = remote_write; |
| 90 | t->sync_token = 1; |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 91 | t->connection_state = state; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | t->type = kTransportUsb; |
| 93 | t->usb = h; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Josh Gao | 08dda21 | 2016-09-26 21:18:58 -0700 | [diff] [blame] | 96 | int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | { |
Elliott Hughes | 4b8538e | 2014-11-19 22:07:34 -0800 | [diff] [blame] | 98 | return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | } |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 100 | |
| 101 | bool should_use_libusb() { |
| 102 | #if defined(_WIN32) || !ADB_HOST |
| 103 | return false; |
| 104 | #else |
Josh Gao | f6ed63c | 2017-03-02 13:23:56 -0800 | [diff] [blame] | 105 | static bool disable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "0") == 0; |
| 106 | return !disable; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 107 | #endif |
| 108 | } |