blob: b371788cced2825a57404e957af671a71c532fb2 [file] [log] [blame]
Josh Gao1c70e1b2016-09-28 12:32:45 -07001/*
2 * Copyright (C) 2016 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#pragma once
18
Josh Gaoef3d3432017-05-02 15:01:09 -070019#include <sys/types.h>
20
Josh Gao08718242020-03-27 18:09:56 -070021#include "adb.h"
22#include "transport.h"
23
Josh Gao1c70e1b2016-09-28 12:32:45 -070024// USB host/client interface.
25
26#define ADB_USB_INTERFACE(handle_ref_type) \
27 void usb_init(); \
Josh Gao01b7bc42017-05-09 13:43:35 -070028 void usb_cleanup(); \
Josh Gao1c70e1b2016-09-28 12:32:45 -070029 int usb_write(handle_ref_type h, const void* data, int len); \
30 int usb_read(handle_ref_type h, void* data, int len); \
31 int usb_close(handle_ref_type h); \
Josh Gao3705b342019-03-28 15:47:44 -070032 void usb_reset(handle_ref_type h); \
Josh Gaoef3d3432017-05-02 15:01:09 -070033 void usb_kick(handle_ref_type h); \
34 size_t usb_get_max_packet_size(handle_ref_type)
Josh Gao1c70e1b2016-09-28 12:32:45 -070035
Josh Gao1c70e1b2016-09-28 12:32:45 -070036// Linux and Darwin clients have native and libusb implementations.
37
38namespace libusb {
Josh Gao08718242020-03-27 18:09:56 -070039struct usb_handle;
40ADB_USB_INTERFACE(libusb::usb_handle*);
41} // namespace libusb
Josh Gao1c70e1b2016-09-28 12:32:45 -070042
43namespace native {
Josh Gao08718242020-03-27 18:09:56 -070044struct usb_handle;
45ADB_USB_INTERFACE(native::usb_handle*);
46} // namespace native
Josh Gao1c70e1b2016-09-28 12:32:45 -070047
48// Empty base that both implementations' opaque handles inherit from.
Josh Gao08718242020-03-27 18:09:56 -070049struct usb_handle {};
Josh Gao1c70e1b2016-09-28 12:32:45 -070050
51ADB_USB_INTERFACE(::usb_handle*);
52
Josh Gao1c70e1b2016-09-28 12:32:45 -070053// USB device detection.
54int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol);
Josh Gao5d1756c2017-02-22 17:07:01 -080055
56bool should_use_libusb();
Josh Gao08718242020-03-27 18:09:56 -070057
58struct UsbConnection : public BlockingConnection {
59 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
60 ~UsbConnection();
61
62 bool Read(apacket* packet) override final;
63 bool Write(apacket* packet) override final;
64 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
65
66 void Close() override final;
67 virtual void Reset() override final;
68
69 usb_handle* handle_;
70};