Loading services/inputflinger/include/InputReaderBase.h +2 −0 Original line number Diff line number Diff line Loading @@ -113,6 +113,8 @@ public: virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0; /* Get battery status of a particular input device. */ virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0; /* Get the device path for the battery of an input device. */ virtual std::optional<std::string> getBatteryDevicePath(int32_t deviceId) = 0; virtual std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) = 0; Loading services/inputflinger/reader/EventHub.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -1555,7 +1555,7 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); const auto& infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; Loading Loading @@ -1596,7 +1596,7 @@ std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId, int32_t batt // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); const auto& infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; Loading services/inputflinger/reader/InputReader.cpp +30 −2 Original line number Diff line number Diff line Loading @@ -721,7 +721,10 @@ std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); } Loading @@ -741,10 +744,35 @@ std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); } std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) { std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (!device) return {}; std::optional<int32_t> eventHubId = device->getBatteryEventHubId(); if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front()); if (!batteryInfo) { ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__, batteryIds.front(), *eventHubId); return {}; } return batteryInfo->path; } std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { std::scoped_lock _l(mLock); Loading services/inputflinger/reader/include/InputReader.h +2 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,8 @@ public: std::optional<int32_t> getBatteryStatus(int32_t deviceId) override; std::optional<std::string> getBatteryDevicePath(int32_t deviceId) override; std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override; std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override; Loading services/inputflinger/tests/InputReader_test.cpp +22 −1 Original line number Diff line number Diff line Loading @@ -81,6 +81,7 @@ static constexpr int32_t THIRD_TRACKING_ID = 2; static constexpr int32_t DEFAULT_BATTERY = 1; static constexpr int32_t BATTERY_STATUS = 4; static constexpr int32_t BATTERY_CAPACITY = 66; static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery"; static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000; static constexpr int32_t LIGHT_COLOR = 0x7F448866; static constexpr int32_t LIGHT_PLAYER_ID = 2; Loading Loading @@ -1017,7 +1018,12 @@ private: std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) const override { return std::nullopt; if (batteryId != DEFAULT_BATTERY) return {}; static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY, .name = "default battery", .flags = InputBatteryClass::CAPACITY, .path = BATTERY_DEVPATH}; return BATTERY_INFO; } std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { Loading Loading @@ -2235,6 +2241,21 @@ TEST_F(InputReaderTest, BatteryGetStatus) { ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS); } TEST_F(InputReaderTest, BatteryGetDevicePath) { constexpr int32_t deviceId = END_RESERVED_ID + 1000; ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY; constexpr int32_t eventHubId = 1; const char* DEVICE_LOCATION = "BLUETOOTH"; std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION); device->addController<FakePeripheralController>(eventHubId); mReader->pushNextDevice(device); ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr)); ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH); } TEST_F(InputReaderTest, LightGetColor) { constexpr int32_t deviceId = END_RESERVED_ID + 1000; ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT; Loading Loading
services/inputflinger/include/InputReaderBase.h +2 −0 Original line number Diff line number Diff line Loading @@ -113,6 +113,8 @@ public: virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0; /* Get battery status of a particular input device. */ virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0; /* Get the device path for the battery of an input device. */ virtual std::optional<std::string> getBatteryDevicePath(int32_t deviceId) = 0; virtual std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) = 0; Loading
services/inputflinger/reader/EventHub.cpp +2 −2 Original line number Diff line number Diff line Loading @@ -1555,7 +1555,7 @@ std::optional<int32_t> EventHub::getBatteryCapacity(int32_t deviceId, int32_t ba // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); const auto& infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; Loading Loading @@ -1596,7 +1596,7 @@ std::optional<int32_t> EventHub::getBatteryStatus(int32_t deviceId, int32_t batt // the lock to prevent event processing from being blocked by this call. std::scoped_lock _l(mLock); const auto infos = getBatteryInfoLocked(deviceId); const auto& infos = getBatteryInfoLocked(deviceId); auto it = infos.find(batteryId); if (it == infos.end()) { return std::nullopt; Loading
services/inputflinger/reader/InputReader.cpp +30 −2 Original line number Diff line number Diff line Loading @@ -721,7 +721,10 @@ std::optional<int32_t> InputReader::getBatteryCapacity(int32_t deviceId) { if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } return mEventHub->getBatteryCapacity(*eventHubId, batteryIds.front()); } Loading @@ -741,10 +744,35 @@ std::optional<int32_t> InputReader::getBatteryStatus(int32_t deviceId) { if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) return {}; if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } return mEventHub->getBatteryStatus(*eventHubId, batteryIds.front()); } std::optional<std::string> InputReader::getBatteryDevicePath(int32_t deviceId) { std::scoped_lock _l(mLock); InputDevice* device = findInputDeviceLocked(deviceId); if (!device) return {}; std::optional<int32_t> eventHubId = device->getBatteryEventHubId(); if (!eventHubId) return {}; const auto batteryIds = mEventHub->getRawBatteryIds(*eventHubId); if (batteryIds.empty()) { ALOGW("%s: There are no battery ids for EventHub device %d", __func__, *eventHubId); return {}; } const auto batteryInfo = mEventHub->getRawBatteryInfo(*eventHubId, batteryIds.front()); if (!batteryInfo) { ALOGW("%s: Failed to get RawBatteryInfo for battery %d of EventHub device %d", __func__, batteryIds.front(), *eventHubId); return {}; } return batteryInfo->path; } std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) { std::scoped_lock _l(mLock); Loading
services/inputflinger/reader/include/InputReader.h +2 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,8 @@ public: std::optional<int32_t> getBatteryStatus(int32_t deviceId) override; std::optional<std::string> getBatteryDevicePath(int32_t deviceId) override; std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override; std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override; Loading
services/inputflinger/tests/InputReader_test.cpp +22 −1 Original line number Diff line number Diff line Loading @@ -81,6 +81,7 @@ static constexpr int32_t THIRD_TRACKING_ID = 2; static constexpr int32_t DEFAULT_BATTERY = 1; static constexpr int32_t BATTERY_STATUS = 4; static constexpr int32_t BATTERY_CAPACITY = 66; static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery"; static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000; static constexpr int32_t LIGHT_COLOR = 0x7F448866; static constexpr int32_t LIGHT_PLAYER_ID = 2; Loading Loading @@ -1017,7 +1018,12 @@ private: std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) const override { return std::nullopt; if (batteryId != DEFAULT_BATTERY) return {}; static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY, .name = "default battery", .flags = InputBatteryClass::CAPACITY, .path = BATTERY_DEVPATH}; return BATTERY_INFO; } std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { Loading Loading @@ -2235,6 +2241,21 @@ TEST_F(InputReaderTest, BatteryGetStatus) { ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS); } TEST_F(InputReaderTest, BatteryGetDevicePath) { constexpr int32_t deviceId = END_RESERVED_ID + 1000; ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY; constexpr int32_t eventHubId = 1; const char* DEVICE_LOCATION = "BLUETOOTH"; std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION); device->addController<FakePeripheralController>(eventHubId); mReader->pushNextDevice(device); ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr)); ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH); } TEST_F(InputReaderTest, LightGetColor) { constexpr int32_t deviceId = END_RESERVED_ID + 1000; ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT; Loading