summaryrefslogtreecommitdiff
path: root/usb/include/pixelusb
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:11:42 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:11:42 +0000
commitbecceed833bea7e9ebc2b6b681d11de748727a3f (patch)
treea6f442eba168310a88707d45fb20952ad3f9ec73 /usb/include/pixelusb
parentf68db2b9637283a61107060b660c8ff865c60337 (diff)
parentc7f3e0ba3acb455437abc7c931aae1afafcb57f2 (diff)
downloadpixel-becceed833bea7e9ebc2b6b681d11de748727a3f.tar.gz
Change-Id: Idc023fbdc5b38a577e99f595ff2046edfbdb15ff
Diffstat (limited to 'usb/include/pixelusb')
-rw-r--r--usb/include/pixelusb/CommonUtils.h78
-rw-r--r--usb/include/pixelusb/MonitorFfs.h101
-rw-r--r--usb/include/pixelusb/UsbGadgetCommon.h181
-rw-r--r--usb/include/pixelusb/UsbOverheatEvent.h28
4 files changed, 205 insertions, 183 deletions
diff --git a/usb/include/pixelusb/CommonUtils.h b/usb/include/pixelusb/CommonUtils.h
new file mode 100644
index 00000000..91244f81
--- /dev/null
+++ b/usb/include/pixelusb/CommonUtils.h
@@ -0,0 +1,78 @@
+/*
+ * 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 <android-base/unique_fd.h>
+
+#include <string>
+
+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";
+constexpr char kUvcEnabled[] = "ro.usb.uvc.enabled";
+
+#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 <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_
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 <android-base/file.h>
-#include <android-base/properties.h>
-#include <android-base/unique_fd.h>
-
-#include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
-
-#include <dirent.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <sys/epoll.h>
-#include <sys/eventfd.h>
-#include <sys/inotify.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <utils/Log.h>
-#include <chrono>
-#include <condition_variable>
-#include <mutex>
-#include <string>
-#include <thread>
-
-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<int> mWatchFd;
-
- // Maintains the list of Endpoints.
- vector<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.
- unique_ptr<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(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..57f6db58 100644
--- a/usb/include/pixelusb/UsbOverheatEvent.h
+++ b/usb/include/pixelusb/UsbOverheatEvent.h
@@ -20,6 +20,7 @@
#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/unique_fd.h>
+#include <android/binder_auto_utils.h>
#include <android/hardware/thermal/2.0/IThermal.h>
#include <android/hardware/thermal/2.0/IThermalChangedCallback.h>
#include <android/hardware/thermal/2.0/types.h>
@@ -82,6 +83,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
@@ -103,24 +107,44 @@ class UsbOverheatEvent : public IServiceNotification, public IThermalChangedCall
unique_ptr<thread> monitor_;
// Maximum overheat temperature recorded
float max_overheat_temp_;
- // Reference to thermal service
+ // Reference to Thermal service
::android::sp<IThermal> 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<void> onRegistration(const hidl_string & /*fully_qualified_name*/,
const hidl_string & /*instance_name*/,
bool /*pre_existing*/) override;
// Thermal service callback
Return<void> 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<UsbOverheatEvent *>(cookie);
+ e->connectAidlThermalService();
+ }
+ }
public:
UsbOverheatEvent(const ZoneInfo &monitored_zone, const std::vector<ZoneInfo> &queried_zones,
const int &monitor_interval_sec);
+ ~UsbOverheatEvent();
// Start monitoring thermal zone for maximum temperature
bool startRecording();
// Stop monitoring thermal zone