diff options
Diffstat (limited to 'adb/transport_usb.cpp')
-rw-r--r-- | adb/transport_usb.cpp | 81 |
1 files changed, 31 insertions, 50 deletions
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp index 347482016..73e8e15df 100644 --- a/adb/transport_usb.cpp +++ b/adb/transport_usb.cpp @@ -80,25 +80,18 @@ static int UsbReadPayload(usb_handle* h, apacket* p) { #endif } -static int remote_read(apacket* p, atransport* t) { - int n = UsbReadMessage(t->usb, &p->msg); +static int remote_read(apacket* p, usb_handle* usb) { + int n = UsbReadMessage(usb, &p->msg); if (n < 0) { D("remote usb: read terminated (message)"); return -1; } - if (static_cast<size_t>(n) != sizeof(p->msg) || !check_header(p, t)) { - D("remote usb: check_header failed, skip it"); - goto err_msg; - } - if (t->GetConnectionState() == kCsOffline) { - // If we read a wrong msg header declaring a large message payload, don't read its payload. - // Otherwise we may miss true messages from the device. - if (p->msg.command != A_CNXN && p->msg.command != A_AUTH) { - goto err_msg; - } + if (static_cast<size_t>(n) != sizeof(p->msg)) { + D("remote usb: read received unexpected header length %d", n); + return -1; } if (p->msg.data_length) { - n = UsbReadPayload(t->usb, p); + n = UsbReadPayload(usb, p); if (n < 0) { D("remote usb: terminated (data)"); return -1; @@ -106,34 +99,24 @@ static int remote_read(apacket* p, atransport* t) { 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); - goto err_msg; + return -1; } } return 0; - -err_msg: - p->msg.command = 0; - return 0; } #else // On Android devices, we rely on the kernel to provide buffered read. // So we can recover automatically from EOVERFLOW. -static int remote_read(apacket *p, atransport *t) -{ - if (usb_read(t->usb, &p->msg, sizeof(amessage))) { +static int remote_read(apacket* p, usb_handle* usb) { + if (usb_read(usb, &p->msg, sizeof(amessage))) { PLOG(ERROR) << "remote usb: read terminated (message)"; return -1; } - if (!check_header(p, t)) { - LOG(ERROR) << "remote usb: check_header failed"; - return -1; - } - if (p->msg.data_length) { - if (usb_read(t->usb, p->data, p->msg.data_length)) { + if (usb_read(usb, p->data, p->msg.data_length)) { PLOG(ERROR) << "remote usb: terminated (data)"; return -1; } @@ -143,45 +126,43 @@ static int remote_read(apacket *p, atransport *t) } #endif -static int remote_write(apacket *p, atransport *t) -{ - unsigned size = p->msg.data_length; +UsbConnection::~UsbConnection() { + usb_close(handle_); +} - if (usb_write(t->usb, &p->msg, sizeof(amessage))) { +bool UsbConnection::Read(apacket* packet) { + int rc = remote_read(packet, handle_); + return rc == 0; +} + +bool UsbConnection::Write(apacket* packet) { + unsigned size = packet->msg.data_length; + + if (usb_write(handle_, &packet->msg, sizeof(packet->msg)) != 0) { PLOG(ERROR) << "remote usb: 1 - write terminated"; - return -1; + return false; } - if (p->msg.data_length == 0) return 0; - if (usb_write(t->usb, &p->data, size)) { + + if (packet->msg.data_length != 0 && usb_write(handle_, &packet->data, size) != 0) { PLOG(ERROR) << "remote usb: 2 - write terminated"; - return -1; + return false; } - return 0; -} - -static void remote_close(atransport* t) { - usb_close(t->usb); - t->usb = 0; + return true; } -static void remote_kick(atransport* t) { - usb_kick(t->usb); +void UsbConnection::Close() { + usb_kick(handle_); } void init_usb_transport(atransport* t, usb_handle* h) { D("transport: usb"); - t->close = remote_close; - t->SetKickFunction(remote_kick); - t->SetWriteFunction(remote_write); - t->read_from_remote = remote_read; + t->connection.reset(new UsbConnection(h)); t->sync_token = 1; t->type = kTransportUsb; - t->usb = h; } -int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) -{ +int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol) { return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL); } |