usb_dispatch.cpp revision 1c70e1bcbcced190b351d4fb418f32b4e428f496
11c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao/*
21c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * Copyright (C) 2017 The Android Open Source Project
31c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao *
41c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * Licensed under the Apache License, Version 2.0 (the "License");
51c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * you may not use this file except in compliance with the License.
61c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * You may obtain a copy of the License at
71c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao *
81c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao *      http://www.apache.org/licenses/LICENSE-2.0
91c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao *
101c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * Unless required by applicable law or agreed to in writing, software
111c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * distributed under the License is distributed on an "AS IS" BASIS,
121c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * See the License for the specific language governing permissions and
141c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao * limitations under the License.
151c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao */
161c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
171c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao#include <android-base/logging.h>
181c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao#include "usb.h"
191c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
201c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaostatic bool should_use_libusb() {
211c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    static bool enable = getenv("ADB_LIBUSB") && strcmp(getenv("ADB_LIBUSB"), "1") == 0;
221c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return enable;
231c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
241c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
251c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaovoid usb_init() {
261c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    if (should_use_libusb()) {
271c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        LOG(INFO) << "using libusb backend";
281c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        libusb::usb_init();
291c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    } else {
301c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        LOG(INFO) << "using native backend";
311c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        native::usb_init();
321c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    }
331c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
341c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
351c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_write(usb_handle* h, const void* data, int len) {
361c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb()
371c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               ? libusb::usb_write(reinterpret_cast<libusb::usb_handle*>(h), data, len)
381c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               : native::usb_write(reinterpret_cast<native::usb_handle*>(h), data, len);
391c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
401c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
411c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_read(usb_handle* h, void* data, int len) {
421c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb()
431c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               ? libusb::usb_read(reinterpret_cast<libusb::usb_handle*>(h), data, len)
441c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               : native::usb_read(reinterpret_cast<native::usb_handle*>(h), data, len);
451c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
461c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
471c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_close(usb_handle* h) {
481c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb() ? libusb::usb_close(reinterpret_cast<libusb::usb_handle*>(h))
491c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao                               : native::usb_close(reinterpret_cast<native::usb_handle*>(h));
501c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
511c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
521c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaovoid usb_kick(usb_handle* h) {
531c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    should_use_libusb() ? libusb::usb_kick(reinterpret_cast<libusb::usb_handle*>(h))
541c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao                        : native::usb_kick(reinterpret_cast<native::usb_handle*>(h));
551c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
56