• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::lock_guard;
54 using ::std::move;
55 using ::std::mutex;
56 using ::std::string;
57 using ::std::thread;
58 using ::std::unique_ptr;
59 using ::std::vector;
60 using namespace std::chrono_literals;
61 
62 struct UsbGadget : public IUsbGadget {
63   UsbGadget();
64   unique_fd mInotifyFd;
65   unique_fd mEventFd;
66   unique_fd mEpollFd;
67 
68   unique_ptr<thread> mMonitor;
69   volatile bool mMonitorCreated;
70   vector<string> mEndpointList;
71   // protects the CV.
72   std::mutex mLock;
73   std::condition_variable mCv;
74 
75   // Makes sure that only one request is processed at a time.
76   std::mutex mLockSetCurrentFunction;
77   uint64_t mCurrentUsbFunctions;
78   bool mCurrentUsbFunctionsApplied;
79 
80   Return<void> setCurrentUsbFunctions(uint64_t functions,
81                                       const sp<IUsbGadgetCallback>& callback,
82                                       uint64_t timeout) override;
83 
84   Return<void> getCurrentUsbFunctions(
85       const sp<IUsbGadgetCallback>& callback) override;
86 
87   private:
88   Status tearDownGadget();
89   Status setupFunctions(uint64_t functions,
90                         const sp<IUsbGadgetCallback>& callback,
91                         uint64_t timeout);
92 };
93 
94 }  // namespace implementation
95 }  // namespace V1_0
96 }  // namespace gadget
97 }  // namespace usb
98 }  // namespace hardware
99 }  // namespace android
100 
101 #endif  // ANDROID_HARDWARE_USB_V1_2_USBGADGET_H
102