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