1 /* 2 * Copyright (C) 2018 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_0_USBGADGET_H 18 #define ANDROID_HARDWARE_USB_GADGET_V1_0_USBGADGET_H 19 20 #include <android-base/file.h> 21 #include <android-base/properties.h> 22 #include <android-base/unique_fd.h> 23 #include <android/hardware/usb/gadget/1.0/IUsbGadget.h> 24 #include <hidl/MQDescriptor.h> 25 #include <hidl/Status.h> 26 #include <string> 27 #include <sys/epoll.h> 28 #include <sys/eventfd.h> 29 #include <thread> 30 #include <utils/Log.h> 31 #include <chrono> 32 #include <condition_variable> 33 #include <mutex> 34 35 namespace android { 36 namespace hardware { 37 namespace usb { 38 namespace gadget { 39 namespace V1_0 { 40 namespace implementation { 41 42 using ::android::sp; 43 using ::android::base::GetProperty; 44 using ::android::base::SetProperty; 45 using ::android::base::unique_fd; 46 using ::android::base::WriteStringToFile; 47 using ::android::hardware::hidl_array; 48 using ::android::hardware::hidl_memory; 49 using ::android::hardware::hidl_string; 50 using ::android::hardware::hidl_vec; 51 using ::android::hardware::Return; 52 using ::android::hardware::Void; 53 using ::std::chrono::steady_clock; 54 using ::std::lock_guard; 55 using ::std::move; 56 using ::std::mutex; 57 using ::std::string; 58 using ::std::thread; 59 using ::std::unique_ptr; 60 using ::std::vector; 61 using namespace std::chrono; 62 using namespace std::chrono_literals; 63 64 struct UsbGadget : public IUsbGadget { 65 UsbGadget(); 66 unique_fd mInotifyFd; 67 unique_fd mEventFd; 68 unique_fd mEpollFd; 69 70 unique_ptr<thread> mMonitor; 71 volatile bool mMonitorCreated; 72 vector<string> mEndpointList; 73 // protects the CV. 74 std::mutex mLock; 75 std::condition_variable mCv; 76 77 // Makes sure that only one request is processed at a time. 78 std::mutex mLockSetCurrentFunction; 79 uint64_t mCurrentUsbFunctions; 80 bool mCurrentUsbFunctionsApplied; 81 82 Return<void> setCurrentUsbFunctions(uint64_t functions, 83 const sp<IUsbGadgetCallback>& callback, 84 uint64_t timeout) override; 85 86 Return<void> getCurrentUsbFunctions( 87 const sp<IUsbGadgetCallback>& callback) override; 88 89 private: 90 Status tearDownGadget(); 91 Status setupFunctions(uint64_t functions, 92 const sp<IUsbGadgetCallback>& callback, 93 uint64_t timeout); 94 }; 95 96 } // namespace implementation 97 } // namespace V1_0 98 } // namespace gadget 99 } // namespace usb 100 } // namespace hardware 101 } // namespace android 102 103 #endif // ANDROID_HARDWARE_USB_V1_2_USBGADGET_H 104