Home | History | Annotate | Download | only in adb
      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 
     17 #define TRACE_TAG TRANSPORT
     18 
     19 #include "sysdeps.h"
     20 #include "transport.h"
     21 
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 
     26 #include "adb.h"
     27 
     28 #if ADB_HOST
     29 
     30 #if defined(__APPLE__)
     31 #define CHECK_PACKET_OVERFLOW 0
     32 #else
     33 #define CHECK_PACKET_OVERFLOW 1
     34 #endif
     35 
     36 // Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
     37 // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
     38 static int UsbReadMessage(usb_handle* h, amessage* msg) {
     39     D("UsbReadMessage");
     40 
     41 #if CHECK_PACKET_OVERFLOW
     42     size_t usb_packet_size = usb_get_max_packet_size(h);
     43     CHECK_GE(usb_packet_size, sizeof(*msg));
     44     CHECK_LT(usb_packet_size, 4096ULL);
     45 
     46     char buffer[4096];
     47     int n = usb_read(h, buffer, usb_packet_size);
     48     if (n != sizeof(*msg)) {
     49         D("usb_read returned unexpected length %d (expected %zu)", n, sizeof(*msg));
     50         return -1;
     51     }
     52     memcpy(msg, buffer, sizeof(*msg));
     53     return n;
     54 #else
     55     return usb_read(h, msg, sizeof(*msg));
     56 #endif
     57 }
     58 
     59 // Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
     60 // to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
     61 static int UsbReadPayload(usb_handle* h, apacket* p) {
     62     D("UsbReadPayload(%d)", p->msg.data_length);
     63 
     64     if (p->msg.data_length > MAX_PAYLOAD) {
     65         return -1;
     66     }
     67 
     68 #if CHECK_PACKET_OVERFLOW
     69     size_t usb_packet_size = usb_get_max_packet_size(h);
     70 
     71     // Round the data length up to the nearest packet size boundary.
     72     // The device won't send a zero packet for packet size aligned payloads,
     73     // so don't read any more packets than needed.
     74     size_t len = p->msg.data_length;
     75     size_t rem_size = len % usb_packet_size;
     76     if (rem_size) {
     77         len += usb_packet_size - rem_size;
     78     }
     79 
     80     p->payload.resize(len);
     81     int rc = usb_read(h, &p->payload[0], p->payload.size());
     82     if (rc != static_cast<int>(p->msg.data_length)) {
     83         return -1;
     84     }
     85 
     86     p->payload.resize(rc);
     87     return rc;
     88 #else
     89     p->payload.resize(p->msg.data_length);
     90     return usb_read(h, &p->payload[0], p->payload.size());
     91 #endif
     92 }
     93 
     94 static int remote_read(apacket* p, usb_handle* usb) {
     95     int n = UsbReadMessage(usb, &p->msg);
     96     if (n < 0) {
     97         D("remote usb: read terminated (message)");
     98         return -1;
     99     }
    100     if (static_cast<size_t>(n) != sizeof(p->msg)) {
    101         D("remote usb: read received unexpected header length %d", n);
    102         return -1;
    103     }
    104     if (p->msg.data_length) {
    105         n = UsbReadPayload(usb, p);
    106         if (n < 0) {
    107             D("remote usb: terminated (data)");
    108             return -1;
    109         }
    110         if (static_cast<uint32_t>(n) != p->msg.data_length) {
    111             D("remote usb: read payload failed (need %u bytes, give %d bytes), skip it",
    112               p->msg.data_length, n);
    113             return -1;
    114         }
    115     }
    116     return 0;
    117 }
    118 
    119 #else
    120 
    121 // On Android devices, we rely on the kernel to provide buffered read.
    122 // So we can recover automatically from EOVERFLOW.
    123 static int remote_read(apacket* p, usb_handle* usb) {
    124     if (usb_read(usb, &p->msg, sizeof(amessage))) {
    125         PLOG(ERROR) << "remote usb: read terminated (message)";
    126         return -1;
    127     }
    128 
    129     if (p->msg.data_length) {
    130         if (p->msg.data_length > MAX_PAYLOAD) {
    131             PLOG(ERROR) << "remote usb: read overflow (data length = " << p->msg.data_length << ")";
    132             return -1;
    133         }
    134 
    135         p->payload.resize(p->msg.data_length);
    136         if (usb_read(usb, &p->payload[0], p->payload.size())) {
    137             PLOG(ERROR) << "remote usb: terminated (data)";
    138             return -1;
    139         }
    140     }
    141 
    142     return 0;
    143 }
    144 #endif
    145 
    146 UsbConnection::~UsbConnection() {
    147     usb_close(handle_);
    148 }
    149 
    150 bool UsbConnection::Read(apacket* packet) {
    151     int rc = remote_read(packet, handle_);
    152     return rc == 0;
    153 }
    154 
    155 bool UsbConnection::Write(apacket* packet) {
    156     unsigned size = packet->msg.data_length;
    157 
    158     if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != 0) {
    159         PLOG(ERROR) << "remote usb: 1 - write terminated";
    160         return false;
    161     }
    162 
    163     if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != 0) {
    164         PLOG(ERROR) << "remote usb: 2 - write terminated";
    165         return false;
    166     }
    167 
    168     return true;
    169 }
    170 
    171 void UsbConnection::Close() {
    172     usb_kick(handle_);
    173 }
    174 
    175 void init_usb_transport(atransport* t, usb_handle* h) {
    176     D("transport: usb");
    177     t->connection.reset(new UsbConnection(h));
    178     t->sync_token = 1;
    179     t->type = kTransportUsb;
    180 }
    181 
    182 int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) {
    183     return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
    184 }
    185 
    186 bool should_use_libusb() {
    187 #if !ADB_HOST
    188     return false;
    189 #else
    190     static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
    191     return enable;
    192 #endif
    193 }
    194