blob: 12ad8eed6110bf7ebcd1b1e11670803d5b308f1b [file] [log] [blame]
raychie96a2a82020-10-07 23:23:56 +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_2_USBGADGET_H
18#define ANDROID_HARDWARE_USB_GADGET_V1_2_USBGADGET_H
19
20#include <UsbGadgetCommon.h>
21#include <android-base/file.h>
22#include <android-base/properties.h>
23#include <android-base/strings.h>
24#include <android-base/unique_fd.h>
25#include <android/hardware/usb/gadget/1.2/IUsbGadget.h>
26#include <android/hardware/usb/gadget/1.2/types.h>
27#include <hidl/MQDescriptor.h>
28#include <hidl/Status.h>
29#include <sys/epoll.h>
30#include <sys/eventfd.h>
31#include <utils/Log.h>
32#include <chrono>
33#include <condition_variable>
34#include <mutex>
35#include <string>
36#include <thread>
37
38namespace android {
39namespace hardware {
40namespace usb {
41namespace gadget {
42namespace V1_2 {
43namespace implementation {
44
45using ::android::sp;
46using ::android::base::GetProperty;
47using ::android::base::ReadFileToString;
48using ::android::base::SetProperty;
49using ::android::base::Trim;
50using ::android::base::unique_fd;
51using ::android::base::WriteStringToFile;
52using ::android::hardware::hidl_array;
53using ::android::hardware::hidl_memory;
54using ::android::hardware::hidl_string;
55using ::android::hardware::hidl_vec;
56using ::android::hardware::Return;
57using ::android::hardware::Void;
58using ::android::hardware::usb::gadget::addAdb;
59using ::android::hardware::usb::gadget::addEpollFd;
60using ::android::hardware::usb::gadget::getVendorFunctions;
61using ::android::hardware::usb::gadget::kDebug;
62using ::android::hardware::usb::gadget::kDisconnectWaitUs;
63using ::android::hardware::usb::gadget::linkFunction;
64using ::android::hardware::usb::gadget::MonitorFfs;
65using ::android::hardware::usb::gadget::resetGadget;
66using ::android::hardware::usb::gadget::setVidPid;
67using ::android::hardware::usb::gadget::unlinkFunctions;
68using ::std::string;
69
70constexpr char kGadgetName[] = "11110000.usb";
71static MonitorFfs monitorFfs(kGadgetName);
72
73#define UDC_PATH "/sys/class/udc/11110000.usb/"
74#define SPEED_PATH UDC_PATH "current_speed"
75
76struct UsbGadget : public IUsbGadget {
77 UsbGadget();
78
79 // Makes sure that only one request is processed at a time.
80 std::mutex mLockSetCurrentFunction;
81 uint64_t mCurrentUsbFunctions;
82 bool mCurrentUsbFunctionsApplied;
83 UsbSpeed mUsbSpeed;
84
85 Return<void> setCurrentUsbFunctions(uint64_t functions,
86 const sp<V1_0::IUsbGadgetCallback>& callback,
87 uint64_t timeout) override;
88
89 Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) override;
90
91 Return<Status> reset() override;
92
93 Return<void> getUsbSpeed(const sp<V1_2::IUsbGadgetCallback>& callback) override;
94
95 private:
96 V1_0::Status tearDownGadget();
97 V1_0::Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback>& callback,
98 uint64_t timeout);
99};
100
101} // namespace implementation
102} // namespace V1_2
103} // namespace gadget
104} // namespace usb
105} // namespace hardware
106} // namespace android
107
108#endif // ANDROID_HARDWARE_USB_V1_2_USBGADGET_H