From a8ce15cdec4388dd501d3dd402baec42759db989 Mon Sep 17 00:00:00 2001 From: Ricky Niu Date: Wed, 26 Oct 2022 16:56:07 +0800 Subject: Add new common USB Gadget Aidl file Bug: 218791946 Test: reboot and check if AIDL service is running. Signed-off-by: Ricky Niu Change-Id: Iae207e785cff6942bb9cdae36b43187d849f39ba --- usb/include/pixelusb/UsbGadgetAidlCommon.h | 182 +++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 usb/include/pixelusb/UsbGadgetAidlCommon.h (limited to 'usb/include/pixelusb') diff --git a/usb/include/pixelusb/UsbGadgetAidlCommon.h b/usb/include/pixelusb/UsbGadgetAidlCommon.h new file mode 100644 index 00000000..f718b352 --- /dev/null +++ b/usb/include/pixelusb/UsbGadgetAidlCommon.h @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2018 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_USBGADGETAIDLCOMMON_H +#define HARDWARE_GOOGLE_PIXEL_USB_USBGADGETAIDLCOMMON_H + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace android { +namespace hardware { +namespace google { +namespace pixel { +namespace usb { + +constexpr int kBufferSize = 512; +constexpr int kMaxFilePathLength = 256; +constexpr int kEpollEvents = 10; +constexpr bool kDebug = false; +constexpr int kDisconnectWaitUs = 100000; +constexpr int kPullUpDelay = 500000; +constexpr int kShutdownMonitor = 100; + +constexpr char kBuildType[] = "ro.build.type"; +constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; +constexpr char kVendorConfig[] = "vendor.usb.config"; +constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; + +#define GADGET_PATH "/config/usb_gadget/g1/" +#define PULLUP_PATH GADGET_PATH "UDC" +#define PERSISTENT_BOOT_MODE "ro.bootmode" +#define VENDOR_ID_PATH GADGET_PATH "idVendor" +#define PRODUCT_ID_PATH GADGET_PATH "idProduct" +#define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass" +#define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass" +#define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol" +#define DESC_USE_PATH GADGET_PATH "os_desc/use" +#define OS_DESC_PATH GADGET_PATH "os_desc/b.1" +#define CONFIG_PATH GADGET_PATH "configs/b.1/" +#define FUNCTIONS_PATH GADGET_PATH "functions/" +#define FUNCTION_NAME "function" +#define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME +#define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis" + +using ::aidl::android::hardware::usb::gadget::GadgetFunction; +using ::aidl::android::hardware::usb::gadget::Status; +using ::android::base::GetProperty; +using ::android::base::SetProperty; +using ::android::base::unique_fd; +using ::android::base::WriteStringToFile; + +using ::std::lock_guard; +using ::std::move; +using ::std::mutex; +using ::std::string; +using ::std::thread; +using ::std::unique_ptr; +using ::std::vector; +using ::std::chrono::microseconds; +using ::std::chrono::steady_clock; +using ::std::literals::chrono_literals::operator""ms; + +// 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; + vector mWatchFd; + + // Maintains the list of Endpoints. + vector 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. + unique_ptr 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(string fd); + // Adds the given endpoint to the watch list. + void addEndPoint(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); +}; + +//**************** Helper functions ************************// + +// Adds the given fd to the epollfd(epfd). +int addEpollFd(const unique_fd &epfd, const unique_fd &fd); +// Removes all the usb functions link in the specified path. +int unlinkFunctions(const char *path); +// Craetes a configfs link for the function. +int linkFunction(const char *function, int index); +// Sets the USB VID and PID. +Status setVidPid(const char *vid, const char *pid); +// Extracts vendor functions from the vendor init properties. +std::string getVendorFunctions(); +// Adds Adb to the usb configuration. +Status addAdb(MonitorFfs *monitorFfs, int *functionCount); +// Adds all applicable generic android usb functions other than ADB. +Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, + bool *ffsEnabled, int *functionCount); +// Pulls down USB gadget. +Status resetGadget(); + +} // namespace usb +} // namespace pixel +} // namespace google +} // namespace hardware +} // namespace android +#endif -- cgit v1.2.3 From c0a76a86a6fd64f304de96084b7bc068193a8097 Mon Sep 17 00:00:00 2001 From: Avichal Rakesh Date: Fri, 6 Jan 2023 02:55:22 -0800 Subject: Create common utils for AIDL Gadget HAL Initial GadgetHAL migration to AIDL missed migrating UsbGadgetUtils to AIDL. Although a UsbGadgetAidlUtils.h was added, it still relied on HIDL interface for the header's implementation. This CL separates out the logic common in HIDL and AIDL to CommonUtils and moves interface specific logic to hidl/ and aidl/ directories. It also adds libpixelusb-aidl build target for AIDL HALs to depend on. Note: UsbOverheatEvent still uses HIDL interfaces, so added a TODO for that. Bug: 218791946 Test: Check logs to verify that AIDL HALs are using the correct implementation Change-Id: Ie63b62b5e612473993763dfd1640b404bc57414c --- usb/include/pixelusb/CommonUtils.h | 77 ++++++++++++ usb/include/pixelusb/MonitorFfs.h | 101 ++++++++++++++++ usb/include/pixelusb/UsbGadgetAidlCommon.h | 182 ----------------------------- usb/include/pixelusb/UsbGadgetCommon.h | 181 ---------------------------- usb/include/pixelusb/UsbOverheatEvent.h | 3 + 5 files changed, 181 insertions(+), 363 deletions(-) create mode 100644 usb/include/pixelusb/CommonUtils.h create mode 100644 usb/include/pixelusb/MonitorFfs.h delete mode 100644 usb/include/pixelusb/UsbGadgetAidlCommon.h delete mode 100644 usb/include/pixelusb/UsbGadgetCommon.h (limited to 'usb/include/pixelusb') diff --git a/usb/include/pixelusb/CommonUtils.h b/usb/include/pixelusb/CommonUtils.h new file mode 100644 index 00000000..5809b4af --- /dev/null +++ b/usb/include/pixelusb/CommonUtils.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2023 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_UTILSCOMMON_H_ +#define HARDWARE_GOOGLE_PIXEL_USB_UTILSCOMMON_H_ + +#include + +#include + +namespace android { +namespace hardware { +namespace google { +namespace pixel { +namespace usb { + +constexpr int kBufferSize = 512; +constexpr int kMaxFilePathLength = 256; +constexpr int kEpollEvents = 10; +constexpr bool kDebug = false; +constexpr int kDisconnectWaitUs = 100000; +constexpr int kPullUpDelay = 500000; +constexpr int kShutdownMonitor = 100; + +constexpr char kBuildType[] = "ro.build.type"; +constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; +constexpr char kVendorConfig[] = "vendor.usb.config"; +constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; + +#define GADGET_PATH "/config/usb_gadget/g1/" +#define PULLUP_PATH GADGET_PATH "UDC" +#define PERSISTENT_BOOT_MODE "ro.bootmode" +#define VENDOR_ID_PATH GADGET_PATH "idVendor" +#define PRODUCT_ID_PATH GADGET_PATH "idProduct" +#define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass" +#define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass" +#define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol" +#define DESC_USE_PATH GADGET_PATH "os_desc/use" +#define OS_DESC_PATH GADGET_PATH "os_desc/b.1" +#define CONFIG_PATH GADGET_PATH "configs/b.1/" +#define FUNCTIONS_PATH GADGET_PATH "functions/" +#define FUNCTION_NAME "function" +#define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME +#define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis" + +// Adds the given fd to the epollfd(epfd). +int addEpollFd(const ::android::base::unique_fd &epfd, const ::android::base::unique_fd &fd); +// Extracts vendor functions from the vendor init properties. +std::string getVendorFunctions(); +// Removes all the usb functions link in the specified path. +int unlinkFunctions(const char *path); +// Creates a configfs link for the function. +int linkFunction(const char *function, int index); +// Sets the USB VID and PID. Returns true on success, false on failure +bool setVidPidCommon(const char *vid, const char *pid); +// Pulls down USB gadget. Returns true on success, false on failure +bool resetGadgetCommon(); +} // namespace usb +} // namespace pixel +} // namespace google +} // namespace hardware +} // namespace android + +#endif // HARDWARE_GOOGLE_PIXEL_USB_UTILSCOMMON_H_ 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 +#include + +#include +#include +#include +#include + +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 mWatchFd; + + // Maintains the list of Endpoints. + std::vector 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 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_ diff --git a/usb/include/pixelusb/UsbGadgetAidlCommon.h b/usb/include/pixelusb/UsbGadgetAidlCommon.h deleted file mode 100644 index f718b352..00000000 --- a/usb/include/pixelusb/UsbGadgetAidlCommon.h +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) 2018 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_USBGADGETAIDLCOMMON_H -#define HARDWARE_GOOGLE_PIXEL_USB_USBGADGETAIDLCOMMON_H - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace android { -namespace hardware { -namespace google { -namespace pixel { -namespace usb { - -constexpr int kBufferSize = 512; -constexpr int kMaxFilePathLength = 256; -constexpr int kEpollEvents = 10; -constexpr bool kDebug = false; -constexpr int kDisconnectWaitUs = 100000; -constexpr int kPullUpDelay = 500000; -constexpr int kShutdownMonitor = 100; - -constexpr char kBuildType[] = "ro.build.type"; -constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; -constexpr char kVendorConfig[] = "vendor.usb.config"; -constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; - -#define GADGET_PATH "/config/usb_gadget/g1/" -#define PULLUP_PATH GADGET_PATH "UDC" -#define PERSISTENT_BOOT_MODE "ro.bootmode" -#define VENDOR_ID_PATH GADGET_PATH "idVendor" -#define PRODUCT_ID_PATH GADGET_PATH "idProduct" -#define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass" -#define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass" -#define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol" -#define DESC_USE_PATH GADGET_PATH "os_desc/use" -#define OS_DESC_PATH GADGET_PATH "os_desc/b.1" -#define CONFIG_PATH GADGET_PATH "configs/b.1/" -#define FUNCTIONS_PATH GADGET_PATH "functions/" -#define FUNCTION_NAME "function" -#define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME -#define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis" - -using ::aidl::android::hardware::usb::gadget::GadgetFunction; -using ::aidl::android::hardware::usb::gadget::Status; -using ::android::base::GetProperty; -using ::android::base::SetProperty; -using ::android::base::unique_fd; -using ::android::base::WriteStringToFile; - -using ::std::lock_guard; -using ::std::move; -using ::std::mutex; -using ::std::string; -using ::std::thread; -using ::std::unique_ptr; -using ::std::vector; -using ::std::chrono::microseconds; -using ::std::chrono::steady_clock; -using ::std::literals::chrono_literals::operator""ms; - -// 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; - vector mWatchFd; - - // Maintains the list of Endpoints. - vector 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. - unique_ptr 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(string fd); - // Adds the given endpoint to the watch list. - void addEndPoint(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); -}; - -//**************** Helper functions ************************// - -// Adds the given fd to the epollfd(epfd). -int addEpollFd(const unique_fd &epfd, const unique_fd &fd); -// Removes all the usb functions link in the specified path. -int unlinkFunctions(const char *path); -// Craetes a configfs link for the function. -int linkFunction(const char *function, int index); -// Sets the USB VID and PID. -Status setVidPid(const char *vid, const char *pid); -// Extracts vendor functions from the vendor init properties. -std::string getVendorFunctions(); -// Adds Adb to the usb configuration. -Status addAdb(MonitorFfs *monitorFfs, int *functionCount); -// Adds all applicable generic android usb functions other than ADB. -Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, - bool *ffsEnabled, int *functionCount); -// Pulls down USB gadget. -Status resetGadget(); - -} // namespace usb -} // namespace pixel -} // namespace google -} // namespace hardware -} // namespace android -#endif diff --git a/usb/include/pixelusb/UsbGadgetCommon.h b/usb/include/pixelusb/UsbGadgetCommon.h deleted file mode 100644 index 964a1d6f..00000000 --- a/usb/include/pixelusb/UsbGadgetCommon.h +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (C) 2018 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_USBGADGETCOMMON_H -#define HARDWARE_GOOGLE_PIXEL_USB_USBGADGETCOMMON_H - -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace android { -namespace hardware { -namespace google { -namespace pixel { -namespace usb { - -constexpr int kBufferSize = 512; -constexpr int kMaxFilePathLength = 256; -constexpr int kEpollEvents = 10; -constexpr bool kDebug = false; -constexpr int kDisconnectWaitUs = 100000; -constexpr int kPullUpDelay = 500000; -constexpr int kShutdownMonitor = 100; - -constexpr char kBuildType[] = "ro.build.type"; -constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; -constexpr char kVendorConfig[] = "vendor.usb.config"; -constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; - -#define GADGET_PATH "/config/usb_gadget/g1/" -#define PULLUP_PATH GADGET_PATH "UDC" -#define PERSISTENT_BOOT_MODE "ro.bootmode" -#define VENDOR_ID_PATH GADGET_PATH "idVendor" -#define PRODUCT_ID_PATH GADGET_PATH "idProduct" -#define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass" -#define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass" -#define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol" -#define DESC_USE_PATH GADGET_PATH "os_desc/use" -#define OS_DESC_PATH GADGET_PATH "os_desc/b.1" -#define CONFIG_PATH GADGET_PATH "configs/b.1/" -#define FUNCTIONS_PATH GADGET_PATH "functions/" -#define FUNCTION_NAME "function" -#define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME -#define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis" - -using ::android::base::GetProperty; -using ::android::base::SetProperty; -using ::android::base::unique_fd; -using ::android::base::WriteStringToFile; -using ::android::hardware::usb::gadget::V1_0::GadgetFunction; -using ::android::hardware::usb::gadget::V1_0::Status; - -using ::std::lock_guard; -using ::std::move; -using ::std::mutex; -using ::std::string; -using ::std::thread; -using ::std::unique_ptr; -using ::std::vector; -using ::std::chrono::microseconds; -using ::std::chrono::steady_clock; -using ::std::literals::chrono_literals::operator""ms; - -// 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; - vector mWatchFd; - - // Maintains the list of Endpoints. - vector 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. - unique_ptr 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(string fd); - // Adds the given endpoint to the watch list. - void addEndPoint(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); -}; - -//**************** Helper functions ************************// - -// Adds the given fd to the epollfd(epfd). -int addEpollFd(const unique_fd &epfd, const unique_fd &fd); -// Removes all the usb functions link in the specified path. -int unlinkFunctions(const char *path); -// Craetes a configfs link for the function. -int linkFunction(const char *function, int index); -// Sets the USB VID and PID. -Status setVidPid(const char *vid, const char *pid); -// Extracts vendor functions from the vendor init properties. -std::string getVendorFunctions(); -// Adds Adb to the usb configuration. -Status addAdb(MonitorFfs *monitorFfs, int *functionCount); -// Adds all applicable generic android usb functions other than ADB. -Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, - bool *ffsEnabled, int *functionCount); -// Pulls down USB gadget. -Status resetGadget(); - -} // namespace usb -} // namespace pixel -} // namespace google -} // namespace hardware -} // namespace android -#endif diff --git a/usb/include/pixelusb/UsbOverheatEvent.h b/usb/include/pixelusb/UsbOverheatEvent.h index de8cc240..286d9e84 100644 --- a/usb/include/pixelusb/UsbOverheatEvent.h +++ b/usb/include/pixelusb/UsbOverheatEvent.h @@ -82,6 +82,9 @@ class ZoneInfo { ZoneInfo(const TemperatureType &type, const string &name, const ThrottlingSeverity &severity); }; +/** + * TODO: Create an AIDL version of this and move to hidl/ + */ class UsbOverheatEvent : public IServiceNotification, public IThermalChangedCallback { private: // To wake up thread to record max temperature -- cgit v1.2.3 From 32ca2b41a8830792c31515334020c9e294c0ae7a Mon Sep 17 00:00:00 2001 From: Avichal Rakesh Date: Wed, 18 Jan 2023 16:06:21 -0800 Subject: Add UVC function support UVC is a newly supported function in the UsbGadget HAL. This CL adds support for the UVC function conditioned on if the prop `ro.usb.uvc.enabled` is set to true. Bug: 242344221 Test: Manually tested that UVC function can be enabled when the property is set, and fails when it is not. Change-Id: I05ebea7c6dd85527554f90760e321a729f161970 --- usb/include/pixelusb/CommonUtils.h | 1 + 1 file changed, 1 insertion(+) (limited to 'usb/include/pixelusb') diff --git a/usb/include/pixelusb/CommonUtils.h b/usb/include/pixelusb/CommonUtils.h index 5809b4af..91244f81 100644 --- a/usb/include/pixelusb/CommonUtils.h +++ b/usb/include/pixelusb/CommonUtils.h @@ -39,6 +39,7 @@ constexpr char kBuildType[] = "ro.build.type"; constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; constexpr char kVendorConfig[] = "vendor.usb.config"; constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; +constexpr char kUvcEnabled[] = "ro.usb.uvc.enabled"; #define GADGET_PATH "/config/usb_gadget/g1/" #define PULLUP_PATH GADGET_PATH "UDC" -- cgit v1.2.3 From 192ce4e515ba3c3d797000eecc81e9f35c7a5483 Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Wed, 8 Feb 2023 14:30:38 -0800 Subject: Update USB HAL to use Therml HIDL wrapper This will try to connect to Thermal AIDL service and fall back to HIDL if not Bug: b/264594715 Test: emul_temp test: verifies that callback is triggered and the USB overheat popup shows as expected when using AIDL or HIDL Test: AIDL service restart test: verified that the service is reconnected and callback is registered/triggered afterwards Change-Id: I7dfd67fad4feeb6934ef4530fc63549febcaef20 --- usb/include/pixelusb/UsbOverheatEvent.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'usb/include/pixelusb') diff --git a/usb/include/pixelusb/UsbOverheatEvent.h b/usb/include/pixelusb/UsbOverheatEvent.h index 286d9e84..57f6db58 100644 --- a/usb/include/pixelusb/UsbOverheatEvent.h +++ b/usb/include/pixelusb/UsbOverheatEvent.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -106,24 +107,44 @@ class UsbOverheatEvent : public IServiceNotification, public IThermalChangedCall unique_ptr monitor_; // Maximum overheat temperature recorded float max_overheat_temp_; - // Reference to thermal service + // Reference to Thermal service ::android::sp thermal_service_; + // Mutex lock for Thermal service + std::mutex thermal_hal_mutex_; + // Death recipient for Thermal AIDL service + ndk::ScopedAIBinder_DeathRecipient thermal_aidl_death_recipient_; + // Whether the Thermal callback is successfully registered + bool is_thermal_callback_registered_; // Thread that polls temperature to record max temp static void *monitorThread(void *param); // Register service notification listener bool registerListener(); // Helper function to wakeup monitor thread void wakeupMonitor(); - // Thermal ServiceNotification listener + // Thermal HIDL Service Notification listener Return onRegistration(const hidl_string & /*fully_qualified_name*/, const hidl_string & /*instance_name*/, bool /*pre_existing*/) override; // Thermal service callback Return notifyThrottling(const Temperature &temperature) override; + // Register Thermal callback + bool registerThermalCallback(const string &service_str); + // Connect Thermal AIDL service + bool connectAidlThermalService(); + // Unregister Thermal callback + void unregisterThermalCallback(); + // Callback for Thermal AIDL service death recipient + static void onThermalAidlBinderDied(void *cookie) { + if (cookie) { + auto *e = static_cast(cookie); + e->connectAidlThermalService(); + } + } public: UsbOverheatEvent(const ZoneInfo &monitored_zone, const std::vector &queried_zones, const int &monitor_interval_sec); + ~UsbOverheatEvent(); // Start monitoring thermal zone for maximum temperature bool startRecording(); // Stop monitoring thermal zone -- cgit v1.2.3