2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-22 22:52:57 +00:00
|
|
|
#define TRACE_TAG TRANSPORT
|
2015-03-19 22:21:08 +00:00
|
|
|
|
2018-08-08 17:33:24 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-03-19 22:21:08 +00:00
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "transport.h"
|
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
#if ADB_HOST
|
|
|
|
|
2017-12-06 23:06:14 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define CHECK_PACKET_OVERFLOW 0
|
|
|
|
#else
|
|
|
|
#define CHECK_PACKET_OVERFLOW 1
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 22:01:09 +00:00
|
|
|
// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
|
|
|
|
static int UsbReadMessage(usb_handle* h, amessage* msg) {
|
|
|
|
D("UsbReadMessage");
|
2017-05-02 22:01:09 +00:00
|
|
|
|
2017-12-06 23:06:14 +00:00
|
|
|
#if CHECK_PACKET_OVERFLOW
|
2017-05-02 22:01:09 +00:00
|
|
|
size_t usb_packet_size = usb_get_max_packet_size(h);
|
2017-08-28 21:43:24 +00:00
|
|
|
CHECK_GE(usb_packet_size, sizeof(*msg));
|
|
|
|
CHECK_LT(usb_packet_size, 4096ULL);
|
2017-05-02 22:01:09 +00:00
|
|
|
|
|
|
|
char buffer[4096];
|
|
|
|
int n = usb_read(h, buffer, usb_packet_size);
|
|
|
|
if (n != sizeof(*msg)) {
|
|
|
|
D("usb_read returned unexpected length %d (expected %zu)", n, sizeof(*msg));
|
|
|
|
return -1;
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
2017-05-02 22:01:09 +00:00
|
|
|
memcpy(msg, buffer, sizeof(*msg));
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
return n;
|
2017-12-06 23:06:14 +00:00
|
|
|
#else
|
|
|
|
return usb_read(h, msg, sizeof(*msg));
|
|
|
|
#endif
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 22:01:09 +00:00
|
|
|
// Call usb_read using a buffer having a multiple of usb_get_max_packet_size() bytes
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
// to avoid overflow. See http://libusb.sourceforge.net/api-1.0/packetoverflow.html.
|
|
|
|
static int UsbReadPayload(usb_handle* h, apacket* p) {
|
2017-05-02 22:01:09 +00:00
|
|
|
D("UsbReadPayload(%d)", p->msg.data_length);
|
|
|
|
|
2018-02-06 02:49:10 +00:00
|
|
|
if (p->msg.data_length > MAX_PAYLOAD) {
|
2018-02-02 22:38:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:06:14 +00:00
|
|
|
#if CHECK_PACKET_OVERFLOW
|
2017-05-02 22:01:09 +00:00
|
|
|
size_t usb_packet_size = usb_get_max_packet_size(h);
|
|
|
|
|
|
|
|
// Round the data length up to the nearest packet size boundary.
|
|
|
|
// The device won't send a zero packet for packet size aligned payloads,
|
|
|
|
// so don't read any more packets than needed.
|
|
|
|
size_t len = p->msg.data_length;
|
|
|
|
size_t rem_size = len % usb_packet_size;
|
|
|
|
if (rem_size) {
|
|
|
|
len += usb_packet_size - rem_size;
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
2018-02-06 02:49:10 +00:00
|
|
|
|
|
|
|
p->payload.resize(len);
|
|
|
|
int rc = usb_read(h, &p->payload[0], p->payload.size());
|
|
|
|
if (rc != static_cast<int>(p->msg.data_length)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->payload.resize(rc);
|
|
|
|
return rc;
|
2017-12-06 23:06:14 +00:00
|
|
|
#else
|
2018-02-06 02:49:10 +00:00
|
|
|
p->payload.resize(p->msg.data_length);
|
|
|
|
return usb_read(h, &p->payload[0], p->payload.size());
|
2017-12-06 23:06:14 +00:00
|
|
|
#endif
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
static int remote_read(apacket* p, usb_handle* usb) {
|
|
|
|
int n = UsbReadMessage(usb, &p->msg);
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
if (n < 0) {
|
|
|
|
D("remote usb: read terminated (message)");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-01-29 04:32:46 +00:00
|
|
|
if (static_cast<size_t>(n) != sizeof(p->msg)) {
|
|
|
|
D("remote usb: read received unexpected header length %d", n);
|
|
|
|
return -1;
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
|
|
|
if (p->msg.data_length) {
|
2018-01-29 04:32:46 +00:00
|
|
|
n = UsbReadPayload(usb, p);
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
if (n < 0) {
|
|
|
|
D("remote usb: terminated (data)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (static_cast<uint32_t>(n) != p->msg.data_length) {
|
|
|
|
D("remote usb: read payload failed (need %u bytes, give %d bytes), skip it",
|
|
|
|
p->msg.data_length, n);
|
2018-01-29 04:32:46 +00:00
|
|
|
return -1;
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// On Android devices, we rely on the kernel to provide buffered read.
|
|
|
|
// So we can recover automatically from EOVERFLOW.
|
2018-01-29 04:32:46 +00:00
|
|
|
static int remote_read(apacket* p, usb_handle* usb) {
|
2018-05-15 23:20:41 +00:00
|
|
|
if (usb_read(usb, &p->msg, sizeof(amessage)) != sizeof(amessage)) {
|
2017-07-26 18:06:55 +00:00
|
|
|
PLOG(ERROR) << "remote usb: read terminated (message)";
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:02:45 +00:00
|
|
|
if (p->msg.data_length) {
|
2018-02-06 02:49:10 +00:00
|
|
|
if (p->msg.data_length > MAX_PAYLOAD) {
|
2018-02-02 22:38:04 +00:00
|
|
|
PLOG(ERROR) << "remote usb: read overflow (data length = " << p->msg.data_length << ")";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-06 02:49:10 +00:00
|
|
|
p->payload.resize(p->msg.data_length);
|
2018-05-15 23:20:41 +00:00
|
|
|
if (usb_read(usb, &p->payload[0], p->payload.size())
|
|
|
|
!= static_cast<int>(p->payload.size())) {
|
2017-07-26 18:06:55 +00:00
|
|
|
PLOG(ERROR) << "remote usb: terminated (data)";
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
#endif
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
UsbConnection::~UsbConnection() {
|
|
|
|
usb_close(handle_);
|
|
|
|
}
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
bool UsbConnection::Read(apacket* packet) {
|
|
|
|
int rc = remote_read(packet, handle_);
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UsbConnection::Write(apacket* packet) {
|
2018-05-15 23:20:41 +00:00
|
|
|
int size = packet->msg.data_length;
|
2018-01-29 04:32:46 +00:00
|
|
|
|
2018-05-15 23:20:41 +00:00
|
|
|
if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != sizeof(packet->msg)) {
|
2017-07-26 18:06:55 +00:00
|
|
|
PLOG(ERROR) << "remote usb: 1 - write terminated";
|
2018-01-29 04:32:46 +00:00
|
|
|
return false;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2018-01-29 04:32:46 +00:00
|
|
|
|
2018-05-15 23:20:41 +00:00
|
|
|
if (packet->msg.data_length != 0 && usb_write(handle_, packet->payload.data(), size) != size) {
|
2017-07-26 18:06:55 +00:00
|
|
|
PLOG(ERROR) << "remote usb: 2 - write terminated";
|
2018-01-29 04:32:46 +00:00
|
|
|
return false;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
return true;
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:47:44 +00:00
|
|
|
void UsbConnection::Reset() {
|
|
|
|
usb_reset(handle_);
|
|
|
|
usb_kick(handle_);
|
|
|
|
}
|
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
void UsbConnection::Close() {
|
|
|
|
usb_kick(handle_);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 00:01:01 +00:00
|
|
|
void init_usb_transport(atransport* t, usb_handle* h) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: usb");
|
2018-03-08 00:51:08 +00:00
|
|
|
auto connection = std::make_unique<UsbConnection>(h);
|
2018-04-25 15:56:41 +00:00
|
|
|
t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(connection)));
|
2009-03-04 03:32:55 +00:00
|
|
|
t->type = kTransportUsb;
|
2018-12-11 21:11:52 +00:00
|
|
|
t->SetUsbHandle(h);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 04:32:46 +00:00
|
|
|
int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) {
|
2014-11-20 06:07:34 +00:00
|
|
|
return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|
2017-02-23 01:07:01 +00:00
|
|
|
|
|
|
|
bool should_use_libusb() {
|
2017-12-08 21:05:40 +00:00
|
|
|
#if !ADB_HOST
|
2017-02-23 01:07:01 +00:00
|
|
|
return false;
|
|
|
|
#else
|
2017-06-26 19:16:30 +00:00
|
|
|
static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
|
|
|
|
return enable;
|
2017-02-23 01:07:01 +00:00
|
|
|
#endif
|
|
|
|
}
|