Loading services/inputflinger/reader/EventHub.cpp +38 −16 Original line number Diff line number Diff line Loading @@ -1545,6 +1545,14 @@ EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const { } std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t batteryId) const { std::filesystem::path batteryPath; { // Do not read the sysfs node to get the battery state while holding // the EventHub lock. For some peripheral devices, reading battery state // can be broken and take 5+ seconds. Holding the lock in this case would // block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and read the sysfs node outside // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); Loading @@ -1552,18 +1560,20 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba if (it == infos.end()) { return std::nullopt; } batteryPath = it->second.path; } // release lock std::string buffer; // Some devices report battery capacity as an integer through the "capacity" file if (base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::CAPACITY), if (base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::CAPACITY), &buffer)) { return std::stoi(base::Trim(buffer)); } // Other devices report capacity as an enum value POWER_SUPPLY_CAPACITY_LEVEL_XXX // These values are taken from kernel source code include/linux/power_supply.h if (base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::CAPACITY_LEVEL), if (base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::CAPACITY_LEVEL), &buffer)) { // Remove any white space such as trailing new line const auto levelIt = BATTERY_LEVEL.find(base::Trim(buffer)); Loading @@ -1576,15 +1586,27 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba } std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId, int32_t batteryId) const { std::filesystem::path batteryPath; { // Do not read the sysfs node to get the battery state while holding // the EventHub lock. For some peripheral devices, reading battery state // can be broken and take 5+ seconds. Holding the lock in this case would // block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and read the sysfs node outside // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; } batteryPath = it->second.path; } // release lock std::string buffer; if (!base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::STATUS), if (!base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::STATUS), &buffer)) { ALOGE("Failed to read sysfs battery info: %s", strerror(errno)); return std::nullopt; Loading services/inputflinger/reader/InputDevice.cpp +4 −8 Original line number Diff line number Diff line Loading @@ -561,14 +561,6 @@ void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) { for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); }); } std::optional<int32_t> InputDevice::getBatteryCapacity() { return mController ? mController->getBatteryCapacity(DEFAULT_BATTERY_ID) : std::nullopt; } std::optional<int32_t> InputDevice::getBatteryStatus() { return mController ? mController->getBatteryStatus(DEFAULT_BATTERY_ID) : std::nullopt; } bool InputDevice::setLightColor(int32_t lightId, int32_t color) { return mController ? mController->setLightColor(lightId, color) : false; } Loading Loading @@ -636,6 +628,10 @@ void InputDevice::updateLedState(bool reset) { for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); }); } std::optional<int32_t> InputDevice::getBatteryEventHubId() const { return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt; } InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId) : mDevice(device), mContext(device.getContext()), Loading services/inputflinger/reader/InputReader.cpp +32 −12 Original line number Diff line number Diff line Loading @@ -706,23 +706,43 @@ void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType } std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { std::optional<int32_t> eventHubId; { // Do not query the battery state while holding the lock. For some peripheral devices, // reading battery state can be broken and take 5+ seconds. Holding the lock in this case // would block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and get the battery state outside the // lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (device) { return device->getBatteryCapacity(); } return std::nullopt; if (!device) return {}; eventHubId = device->getBatteryEventHubId(); } // release lock if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); } std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { std::optional<int32_t> eventHubId; { // Do not query the battery state while holding the lock. For some peripheral devices, // reading battery state can be broken and take 5+ seconds. Holding the lock in this case // would block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and get the battery state outside the // lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (device) { return device->getBatteryStatus(); } return std::nullopt; if (!device) return {}; eventHubId = device->getBatteryEventHubId(); } // release lock if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); } std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { Loading services/inputflinger/reader/controller/PeripheralController.cpp +4 −0 Original line number Diff line number Diff line Loading @@ -521,4 +521,8 @@ std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) { return light->getLightPlayerId(); } int32_t PeripheralController::getEventHubId() const { return getDeviceContext().getEventHubId(); } } // namespace android services/inputflinger/reader/controller/PeripheralController.h +2 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ public: explicit PeripheralController(InputDeviceContext& deviceContext); ~PeripheralController() override; int32_t getEventHubId() const override; void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; void dump(std::string& dump) override; bool setLightColor(int32_t lightId, int32_t color) override; Loading @@ -43,6 +44,7 @@ public: private: inline int32_t getDeviceId() { return mDeviceContext.getId(); } inline InputDeviceContext& getDeviceContext() { return mDeviceContext; } inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; } InputDeviceContext& mDeviceContext; void configureLights(); Loading Loading
services/inputflinger/reader/EventHub.cpp +38 −16 Original line number Diff line number Diff line Loading @@ -1545,6 +1545,14 @@ EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const { } std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t batteryId) const { std::filesystem::path batteryPath; { // Do not read the sysfs node to get the battery state while holding // the EventHub lock. For some peripheral devices, reading battery state // can be broken and take 5+ seconds. Holding the lock in this case would // block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and read the sysfs node outside // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); Loading @@ -1552,18 +1560,20 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba if (it == infos.end()) { return std::nullopt; } batteryPath = it->second.path; } // release lock std::string buffer; // Some devices report battery capacity as an integer through the "capacity" file if (base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::CAPACITY), if (base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::CAPACITY), &buffer)) { return std::stoi(base::Trim(buffer)); } // Other devices report capacity as an enum value POWER_SUPPLY_CAPACITY_LEVEL_XXX // These values are taken from kernel source code include/linux/power_supply.h if (base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::CAPACITY_LEVEL), if (base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::CAPACITY_LEVEL), &buffer)) { // Remove any white space such as trailing new line const auto levelIt = BATTERY_LEVEL.find(base::Trim(buffer)); Loading @@ -1576,15 +1586,27 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba } std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId, int32_t batteryId) const { std::filesystem::path batteryPath; { // Do not read the sysfs node to get the battery state while holding // the EventHub lock. For some peripheral devices, reading battery state // can be broken and take 5+ seconds. Holding the lock in this case would // block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and read the sysfs node outside // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; } batteryPath = it->second.path; } // release lock std::string buffer; if (!base::ReadFileToString(it->second.path / BATTERY_NODES.at(InputBatteryClass::STATUS), if (!base::ReadFileToString(batteryPath / BATTERY_NODES.at(InputBatteryClass::STATUS), &buffer)) { ALOGE("Failed to read sysfs battery info: %s", strerror(errno)); return std::nullopt; Loading
services/inputflinger/reader/InputDevice.cpp +4 −8 Original line number Diff line number Diff line Loading @@ -561,14 +561,6 @@ void InputDevice::cancelTouch(nsecs_t when, nsecs_t readTime) { for_each_mapper([when, readTime](InputMapper& mapper) { mapper.cancelTouch(when, readTime); }); } std::optional<int32_t> InputDevice::getBatteryCapacity() { return mController ? mController->getBatteryCapacity(DEFAULT_BATTERY_ID) : std::nullopt; } std::optional<int32_t> InputDevice::getBatteryStatus() { return mController ? mController->getBatteryStatus(DEFAULT_BATTERY_ID) : std::nullopt; } bool InputDevice::setLightColor(int32_t lightId, int32_t color) { return mController ? mController->setLightColor(lightId, color) : false; } Loading Loading @@ -636,6 +628,10 @@ void InputDevice::updateLedState(bool reset) { for_each_mapper([reset](InputMapper& mapper) { mapper.updateLedState(reset); }); } std::optional<int32_t> InputDevice::getBatteryEventHubId() const { return mController ? std::make_optional(mController->getEventHubId()) : std::nullopt; } InputDeviceContext::InputDeviceContext(InputDevice& device, int32_t eventHubId) : mDevice(device), mContext(device.getContext()), Loading
services/inputflinger/reader/InputReader.cpp +32 −12 Original line number Diff line number Diff line Loading @@ -706,23 +706,43 @@ void InputReader::flushSensor(int32_t deviceId, InputDeviceSensorType sensorType } std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { std::optional<int32_t> eventHubId; { // Do not query the battery state while holding the lock. For some peripheral devices, // reading battery state can be broken and take 5+ seconds. Holding the lock in this case // would block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and get the battery state outside the // lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (device) { return device->getBatteryCapacity(); } return std::nullopt; if (!device) return {}; eventHubId = device->getBatteryEventHubId(); } // release lock if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); } std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { std::optional<int32_t> eventHubId; { // Do not query the battery state while holding the lock. For some peripheral devices, // reading battery state can be broken and take 5+ seconds. Holding the lock in this case // would block all other event processing during this time. For now, we assume this // call never happens on the InputReader thread and get the battery state outside the // lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (device) { return device->getBatteryStatus(); } return std::nullopt; if (!device) return {}; eventHubId = device->getBatteryEventHubId(); } // release lock if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); } std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { Loading
services/inputflinger/reader/controller/PeripheralController.cpp +4 −0 Original line number Diff line number Diff line Loading @@ -521,4 +521,8 @@ std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) { return light->getLightPlayerId(); } int32_t PeripheralController::getEventHubId() const { return getDeviceContext().getEventHubId(); } } // namespace android
services/inputflinger/reader/controller/PeripheralController.h +2 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ public: explicit PeripheralController(InputDeviceContext& deviceContext); ~PeripheralController() override; int32_t getEventHubId() const override; void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; void dump(std::string& dump) override; bool setLightColor(int32_t lightId, int32_t color) override; Loading @@ -43,6 +44,7 @@ public: private: inline int32_t getDeviceId() { return mDeviceContext.getId(); } inline InputDeviceContext& getDeviceContext() { return mDeviceContext; } inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; } InputDeviceContext& mDeviceContext; void configureLights(); Loading