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
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
static int remote_read(apacket *p, atransport *t)
|
|
|
|
{
|
|
|
|
if(usb_read(t->usb, &p->msg, sizeof(amessage))){
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: read terminated (message)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:12:28 +00:00
|
|
|
if(check_header(p, t)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: check_header failed");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p->msg.data_length) {
|
|
|
|
if(usb_read(t->usb, p->data, p->msg.data_length)){
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: terminated (data)");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_data(p)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: check_data failed");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int remote_write(apacket *p, atransport *t)
|
|
|
|
{
|
|
|
|
unsigned size = p->msg.data_length;
|
|
|
|
|
|
|
|
if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: 1 - write terminated");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(p->msg.data_length == 0) return 0;
|
|
|
|
if(usb_write(t->usb, &p->data, size)) {
|
2015-09-03 00:44:28 +00:00
|
|
|
D("remote usb: 2 - write terminated");
|
2009-03-04 03:32:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_close(atransport *t)
|
|
|
|
{
|
|
|
|
usb_close(t->usb);
|
|
|
|
t->usb = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_kick(atransport *t)
|
|
|
|
{
|
|
|
|
usb_kick(t->usb);
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:43:57 +00:00
|
|
|
void init_usb_transport(atransport *t, usb_handle *h, ConnectionState state)
|
2009-03-04 03:32:55 +00:00
|
|
|
{
|
2015-09-03 00:44:28 +00:00
|
|
|
D("transport: usb");
|
2009-03-04 03:32:55 +00:00
|
|
|
t->close = remote_close;
|
2016-04-18 18:22:34 +00:00
|
|
|
t->SetKickFunction(remote_kick);
|
2009-03-04 03:32:55 +00:00
|
|
|
t->read_from_remote = remote_read;
|
|
|
|
t->write_to_remote = remote_write;
|
|
|
|
t->sync_token = 1;
|
2009-08-08 16:37:44 +00:00
|
|
|
t->connection_state = state;
|
2009-03-04 03:32:55 +00:00
|
|
|
t->type = kTransportUsb;
|
|
|
|
t->usb = h;
|
|
|
|
}
|
|
|
|
|
2016-09-27 04:18:58 +00:00
|
|
|
int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol)
|
2009-03-04 03:32:55 +00:00
|
|
|
{
|
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() {
|
|
|
|
#if defined(_WIN32) || !ADB_HOST
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
|
|
|
|
return enable;
|
|
|
|
#endif
|
|
|
|
}
|