blob: b278071336b7877266b8f201853937f74d94df23 [file] [log] [blame]
Howard Yen9bfb4352019-11-28 21:14:57 +08001/*
2 * Copyright (C) 2020 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#ifndef ANDROID_HARDWARE_USB_GADGET_V1_1_USBGADGET_H
18#define ANDROID_HARDWARE_USB_GADGET_V1_1_USBGADGET_H
19
20#include <UsbGadgetCommon.h>
21#include <android-base/file.h>
22#include <android-base/properties.h>
23#include <android-base/unique_fd.h>
24#include <android/hardware/usb/gadget/1.1/IUsbGadget.h>
25#include <hidl/MQDescriptor.h>
26#include <hidl/Status.h>
27#include <sys/epoll.h>
28#include <sys/eventfd.h>
29#include <utils/Log.h>
30#include <chrono>
31#include <condition_variable>
32#include <mutex>
33#include <string>
34#include <thread>
35
36namespace android {
37namespace hardware {
38namespace usb {
39namespace gadget {
40namespace V1_1 {
41namespace implementation {
42
43using ::android::sp;
44using ::android::base::GetProperty;
45using ::android::base::SetProperty;
46using ::android::base::unique_fd;
47using ::android::base::WriteStringToFile;
48using ::android::hardware::hidl_array;
49using ::android::hardware::hidl_memory;
50using ::android::hardware::hidl_string;
51using ::android::hardware::hidl_vec;
52using ::android::hardware::Return;
53using ::android::hardware::Void;
54using ::android::hardware::usb::gadget::addAdb;
55using ::android::hardware::usb::gadget::addEpollFd;
56using ::android::hardware::usb::gadget::getVendorFunctions;
57using ::android::hardware::usb::gadget::kDebug;
58using ::android::hardware::usb::gadget::kDisconnectWaitUs;
59using ::android::hardware::usb::gadget::linkFunction;
60using ::android::hardware::usb::gadget::MonitorFfs;
61using ::android::hardware::usb::gadget::resetGadget;
62using ::android::hardware::usb::gadget::setVidPid;
63using ::android::hardware::usb::gadget::unlinkFunctions;
64using ::std::string;
65
66constexpr char kGadgetName[] = "a600000.dwc3";
67static MonitorFfs monitorFfs(kGadgetName);
68
69struct UsbGadget : public IUsbGadget {
70 UsbGadget();
71
72 // Makes sure that only one request is processed at a time.
73 std::mutex mLockSetCurrentFunction;
74 uint64_t mCurrentUsbFunctions;
75 bool mCurrentUsbFunctionsApplied;
76
77 Return<void> setCurrentUsbFunctions(uint64_t functions,
78 const sp<V1_0::IUsbGadgetCallback>& callback,
79 uint64_t timeout) override;
80
81 Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) override;
82
83 Return<Status> reset() override;
84
85 private:
86 V1_0::Status tearDownGadget();
87 V1_0::Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback>& callback,
88 uint64_t timeout);
89};
90
91} // namespace implementation
92} // namespace V1_1
93} // namespace gadget
94} // namespace usb
95} // namespace hardware
96} // namespace android
97
98#endif // ANDROID_HARDWARE_USB_V1_1_USBGADGET_H