device_google_gs201/usb/usb/Usb.h
Qian-Hao Huang a9ac5d5869 Revert "usb: introduce UsbDataSessionMonitor class"
Revert submission 25524752-usbDataSessionMonitor

Reason for revert: Test for gs101/gs201 boot blocking issue (b/314904288)

Reverted changes: /q/submissionid:25524752-usbDataSessionMonitor

Change-Id: I0f762a9ba8345d2963ebfb2d50cb8a7460885221
2023-12-05 17:13:31 +00:00

139 lines
5 KiB
C++

/*
* Copyright (C) 2021 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.
*/
#pragma once
#include <android-base/file.h>
#include <aidl/android/hardware/usb/BnUsb.h>
#include <aidl/android/hardware/usb/BnUsbCallback.h>
#include <chrono>
#include <pixelusb/UsbOverheatEvent.h>
#include <utils/Log.h>
#define UEVENT_MSG_LEN 2048
// The type-c stack waits for 4.5 - 5.5 secs before declaring a port non-pd.
// The -partner directory would not be created until this is done.
// Having a margin of ~3 secs for the directory and other related bookeeping
// structures created and uvent fired.
#define PORT_TYPE_TIMEOUT 8
namespace aidl {
namespace android {
namespace hardware {
namespace usb {
using ::aidl::android::hardware::usb::IUsbCallback;
using ::aidl::android::hardware::usb::PortRole;
using ::android::base::ReadFileToString;
using ::android::base::WriteStringToFile;
using ::android::hardware::google::pixel::usb::UsbOverheatEvent;
using ::android::hardware::google::pixel::usb::ZoneInfo;
using ::android::hardware::thermal::V2_0::TemperatureType;
using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
using ::android::sp;
using ::android::status_t;
using ::ndk::ScopedAStatus;
using ::std::shared_ptr;
using ::std::string;
constexpr char kGadgetName[] = "11210000.dwc3";
#define NEW_UDC_PATH "/sys/devices/platform/11210000.usb/"
#define ID_PATH NEW_UDC_PATH "dwc3_exynos_otg_id"
#define VBUS_PATH NEW_UDC_PATH "dwc3_exynos_otg_b_sess"
#define USB_DATA_PATH NEW_UDC_PATH "usb_data_enabled"
struct Usb : public BnUsb {
Usb();
ScopedAStatus enableContaminantPresenceDetection(const std::string& in_portName,
bool in_enable, int64_t in_transactionId) override;
ScopedAStatus queryPortStatus(int64_t in_transactionId) override;
ScopedAStatus setCallback(const shared_ptr<IUsbCallback>& in_callback) override;
ScopedAStatus switchRole(const string& in_portName, const PortRole& in_role,
int64_t in_transactionId) override;
ScopedAStatus enableUsbData(const string& in_portName, bool in_enable,
int64_t in_transactionId) override;
ScopedAStatus enableUsbDataWhileDocked(const string& in_portName,
int64_t in_transactionId) override;
ScopedAStatus limitPowerTransfer(const string& in_portName, bool in_limit,
int64_t in_transactionId) override;
ScopedAStatus resetUsbPort(const string& in_portName, int64_t in_transactionId) override;
status_t handleShellCommand(int in, int out, int err, const char** argv,
uint32_t argc) override;
std::shared_ptr<::aidl::android::hardware::usb::IUsbCallback> mCallback;
// Protects mCallback variable
pthread_mutex_t mLock;
// Protects roleSwitch operation
pthread_mutex_t mRoleSwitchLock;
// Threads waiting for the partner to come back wait here
pthread_cond_t mPartnerCV;
// lock protecting mPartnerCV
pthread_mutex_t mPartnerLock;
// Variable to signal partner coming back online after type switch
bool mPartnerUp;
// Usb Overheat object for push suez event
UsbOverheatEvent mOverheat;
// Temperature when connected
float mPluggedTemperatureCelsius;
// Usb Data status
bool mUsbDataEnabled;
// Usb hub vendor command settings for JK level tuning
int mUsbHubVendorCmdValue;
int mUsbHubVendorCmdIndex;
// USB device state monitoring
struct usbDeviceState {
// Usb device state raw strings read from sysfs
std::vector<std::string> states;
// Timestamps of when the usb device states were captured
std::vector<std::chrono::steady_clock::time_point> timestamps;
int portResetCount;
};
struct usbDeviceState mDeviceState;
// Map host device path name to usbDeviceState
std::map<std::string, struct usbDeviceState> mHostStateMap;
// Cache relevant info for USB data session metrics collection when a session starts, including
// the data role, power brick status and the time when the session starts.
PortDataRole mDataRole;
bool mIsPowerBrickConnected;
std::chrono::steady_clock::time_point mDataSessionStart;
// File monitoring through epoll
int mEpollFd;
struct payload {
int fd;
std::string name;
Usb *usb;
};
struct epollEntry {
struct payload payload;
std::function<void(uint32_t)> cb;
};
std::map<std::string, struct epollEntry> mEpollEntries;
private:
pthread_t mPoll;
pthread_t mUsbHost;
};
} // namespace usb
} // namespace hardware
} // namespace android
} // aidl