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 Gaovoid usb_init() {
211c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    if (should_use_libusb()) {
22aced420f20538eb64137e154b1b5e9a00551ffceNick Kralevich        LOG(DEBUG) << "using libusb backend";
231c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        libusb::usb_init();
241c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    } else {
25aced420f20538eb64137e154b1b5e9a00551ffceNick Kralevich        LOG(DEBUG) << "using native backend";
261c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao        native::usb_init();
271c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    }
281c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
291c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
3001b7bc43e90b951675cabe88313b96f57b2da712Josh Gaovoid usb_cleanup() {
3101b7bc43e90b951675cabe88313b96f57b2da712Josh Gao    if (should_use_libusb()) {
3201b7bc43e90b951675cabe88313b96f57b2da712Josh Gao        libusb::usb_cleanup();
33362b4041f3162ef3d219d6bcebdd5170ae28a662Josh Gao    } else {
34362b4041f3162ef3d219d6bcebdd5170ae28a662Josh Gao        native::usb_cleanup();
3501b7bc43e90b951675cabe88313b96f57b2da712Josh Gao    }
3601b7bc43e90b951675cabe88313b96f57b2da712Josh Gao}
3701b7bc43e90b951675cabe88313b96f57b2da712Josh Gao
381c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_write(usb_handle* h, const void* data, int len) {
391c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb()
401c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               ? libusb::usb_write(reinterpret_cast<libusb::usb_handle*>(h), data, len)
411c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               : native::usb_write(reinterpret_cast<native::usb_handle*>(h), data, len);
421c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
431c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
441c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_read(usb_handle* h, void* data, int len) {
451c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb()
461c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               ? libusb::usb_read(reinterpret_cast<libusb::usb_handle*>(h), data, len)
471c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao               : native::usb_read(reinterpret_cast<native::usb_handle*>(h), data, len);
481c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
491c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
501c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaoint usb_close(usb_handle* h) {
511c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    return should_use_libusb() ? libusb::usb_close(reinterpret_cast<libusb::usb_handle*>(h))
521c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao                               : native::usb_close(reinterpret_cast<native::usb_handle*>(h));
531c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
541c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao
551c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gaovoid usb_kick(usb_handle* h) {
561c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao    should_use_libusb() ? libusb::usb_kick(reinterpret_cast<libusb::usb_handle*>(h))
571c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao                        : native::usb_kick(reinterpret_cast<native::usb_handle*>(h));
581c70e1bcbcced190b351d4fb418f32b4e428f496Josh Gao}
59ef3d343254405cc360b4df843c6e4a843c335012Josh Gao
60ef3d343254405cc360b4df843c6e4a843c335012Josh Gaosize_t usb_get_max_packet_size(usb_handle* h) {
61ef3d343254405cc360b4df843c6e4a843c335012Josh Gao    return should_use_libusb()
62ef3d343254405cc360b4df843c6e4a843c335012Josh Gao               ? libusb::usb_get_max_packet_size(reinterpret_cast<libusb::usb_handle*>(h))
63ef3d343254405cc360b4df843c6e4a843c335012Josh Gao               : native::usb_get_max_packet_size(reinterpret_cast<native::usb_handle*>(h));
64ef3d343254405cc360b4df843c6e4a843c335012Josh Gao}
65