diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 05:11:42 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 05:11:42 +0000 |
commit | becceed833bea7e9ebc2b6b681d11de748727a3f (patch) | |
tree | a6f442eba168310a88707d45fb20952ad3f9ec73 /usb/include/pixelusb/MonitorFfs.h | |
parent | f68db2b9637283a61107060b660c8ff865c60337 (diff) | |
parent | c7f3e0ba3acb455437abc7c931aae1afafcb57f2 (diff) | |
download | pixel-aml_per_341614000.tar.gz |
Snap for 10453563 from c7f3e0ba3acb455437abc7c931aae1afafcb57f2 to mainline-permission-releaseaml_per_341711000aml_per_341614000aml_per_341510010aml_per_341410020aml_per_341311000aml_per_341110020aml_per_341110010aml_per_341011100aml_per_341011020aml_per_340916010android14-mainline-permission-release
Change-Id: Idc023fbdc5b38a577e99f595ff2046edfbdb15ff
Diffstat (limited to 'usb/include/pixelusb/MonitorFfs.h')
-rw-r--r-- | usb/include/pixelusb/MonitorFfs.h | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/usb/include/pixelusb/MonitorFfs.h b/usb/include/pixelusb/MonitorFfs.h new file mode 100644 index 00000000..696134b7 --- /dev/null +++ b/usb/include/pixelusb/MonitorFfs.h @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HARDWARE_GOOGLE_PIXEL_USB_MONITORFFS_H_ +#define HARDWARE_GOOGLE_PIXEL_USB_MONITORFFS_H_ + +#include <android-base/unique_fd.h> +#include <pixelusb/CommonUtils.h> + +#include <mutex> +#include <string> +#include <thread> +#include <vector> + +namespace android { +namespace hardware { +namespace google { +namespace pixel { +namespace usb { + +using ::android::base::unique_fd; + +// MonitorFfs automously manages gadget pullup by monitoring +// the ep file status. Restarts the usb gadget when the ep +// owner restarts. +class MonitorFfs { + private: + // Monitors the endpoints Inotify events. + unique_fd mInotifyFd; + // Control pipe for shutting down the mMonitor thread. + // mMonitor exits when SHUTDOWN_MONITOR is written into + // mEventFd/ + unique_fd mEventFd; + // Pools on mInotifyFd and mEventFd. + unique_fd mEpollFd; + std::vector<int> mWatchFd; + + // Maintains the list of Endpoints. + std::vector<std::string> mEndpointList; + // protects the CV. + std::mutex mLock; + std::condition_variable mCv; + // protects mInotifyFd, mEpollFd. + std::mutex mLockFd; + + // Flag to maintain the current status of gadget pullup. + bool mCurrentUsbFunctionsApplied; + + // Thread object that executes the ep monitoring logic. + std::unique_ptr<std::thread> mMonitor; + // Callback to be invoked when gadget is pulled up. + void (*mCallback)(bool functionsApplied, void *payload); + void *mPayload; + // Name of the USB gadget. Used for pullup. + const char *const mGadgetName; + // Monitor State + bool mMonitorRunning; + + public: + MonitorFfs(const char *const gadget); + // Inits all the UniqueFds. + void reset(); + // Starts monitoring endpoints and pullup the gadget when + // the descriptors are written. + bool startMonitor(); + // Waits for timeout_ms for gadget pull up to happen. + // Returns immediately if the gadget is already pulled up. + bool waitForPullUp(int timeout_ms); + // Adds the given fd to the watch list. + bool addInotifyFd(std::string fd); + // Adds the given endpoint to the watch list. + void addEndPoint(std::string ep); + // Registers the async callback from the caller to notify the caller + // when the gadget pull up happens. + void registerFunctionsAppliedCallback(void (*callback)(bool functionsApplied, void *(payload)), + void *payload); + bool isMonitorRunning(); + // Ep monitoring and the gadget pull up logic. + static void *startMonitorFd(void *param); +}; + +} // namespace usb +} // namespace pixel +} // namespace google +} // namespace hardware +} // namespace android + +#endif // HARDWARE_GOOGLE_PIXEL_USB_MONITORFFS_H_ |