Skip to content

Commit 9d63f1c

Browse files
committed
InputReader: Add getter API for the sysfs node path of an InputDevice
Bug: 397208968 Test: Presubmit Flag: EXEMPT bug fix Change-Id: I25f41f41ec1f1e47590eef21b982287caea193a6
1 parent 6f9ad77 commit 9d63f1c

File tree

13 files changed

+81
-3
lines changed

13 files changed

+81
-3
lines changed

services/inputflinger/include/InputReaderBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ class InputReaderInterface {
443443
/* Get the Bluetooth address of an input device, if known. */
444444
virtual std::optional<std::string> getBluetoothAddress(int32_t deviceId) const = 0;
445445

446+
/* Gets the sysfs root path for this device. Returns an empty path if there is none. */
447+
virtual std::filesystem::path getSysfsRootPath(int32_t deviceId) const = 0;
448+
446449
/* Sysfs node change reported. Recreate device if required to incorporate the new sysfs nodes */
447450
virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
448451

services/inputflinger/reader/EventHub.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static nsecs_t processEventTimestamp(const struct input_event& event) {
246246
/**
247247
* Returns the sysfs root path of the input device.
248248
*/
249-
static std::optional<std::filesystem::path> getSysfsRootPath(const char* devicePath) {
249+
static std::optional<std::filesystem::path> getSysfsRootForEvdevDevicePath(const char* devicePath) {
250250
std::error_code errorCode;
251251

252252
// Stat the device path to get the major and minor number of the character file
@@ -1619,7 +1619,7 @@ void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
16191619
std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDeviceLocked(
16201620
const std::filesystem::path& devicePath, const std::shared_ptr<PropertyMap>& config) const {
16211621
const std::optional<std::filesystem::path> sysfsRootPathOpt =
1622-
getSysfsRootPath(devicePath.c_str());
1622+
getSysfsRootForEvdevDevicePath(devicePath.c_str());
16231623
if (!sysfsRootPathOpt) {
16241624
return nullptr;
16251625
}
@@ -2666,6 +2666,18 @@ status_t EventHub::disableDevice(int32_t deviceId) {
26662666
return device->disable();
26672667
}
26682668

2669+
std::filesystem::path EventHub::getSysfsRootPath(int32_t deviceId) const {
2670+
std::scoped_lock _l(mLock);
2671+
Device* device = getDeviceLocked(deviceId);
2672+
if (device == nullptr) {
2673+
ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
2674+
return {};
2675+
}
2676+
2677+
return device->associatedDevice ? device->associatedDevice->sysfsRootPath
2678+
: std::filesystem::path{};
2679+
}
2680+
26692681
// TODO(b/274755573): Shift to uevent handling on native side and remove this method
26702682
// Currently using Java UEventObserver to trigger this which uses UEvent infrastructure that uses a
26712683
// NETLINK socket to observe UEvents. We can create similar infrastructure on Eventhub side to
@@ -2710,6 +2722,10 @@ void EventHub::handleSysfsNodeChangeNotificationsLocked() {
27102722
auto reloadedDevice = AssociatedDevice(dev.associatedDevice->sysfsRootPath,
27112723
dev.associatedDevice->baseDevConfig);
27122724
const bool changed = *dev.associatedDevice != reloadedDevice;
2725+
if (changed) {
2726+
ALOGI("sysfsNodeChanged: Identified change in sysfs nodes for device: %s",
2727+
dev.identifier.name.c_str());
2728+
}
27132729
testedDevices.emplace(dev.associatedDevice, changed);
27142730
return changed;
27152731
};

services/inputflinger/reader/InputDevice.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
136136
} else {
137137
dump += "<none>\n";
138138
}
139+
dump += StringPrintf(INDENT2 "SysfsRootPath: %s\n",
140+
mSysfsRootPath.empty() ? "<none>" : mSysfsRootPath.c_str());
139141
dump += StringPrintf(INDENT2 "HasMic: %s\n", toString(mHasMic));
140142
dump += StringPrintf(INDENT2 "Sources: %s\n",
141143
inputEventSourceToString(deviceInfo.getSources()).c_str());
@@ -195,6 +197,10 @@ void InputDevice::addEmptyEventHubDevice(int32_t eventHubId) {
195197
DevicePair& devicePair = mDevices[eventHubId];
196198
devicePair.second = createMappers(*devicePair.first, readerConfig);
197199

200+
if (mSysfsRootPath.empty()) {
201+
mSysfsRootPath = devicePair.first->getSysfsRootPath();
202+
}
203+
198204
// Must change generation to flag this device as changed
199205
bumpGeneration();
200206
return out;

services/inputflinger/reader/InputReader.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,16 @@ bool InputReader::canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId di
917917
return *associatedDisplayId == displayId;
918918
}
919919

920+
std::filesystem::path InputReader::getSysfsRootPath(int32_t deviceId) const {
921+
std::scoped_lock _l(mLock);
922+
923+
const InputDevice* device = findInputDeviceLocked(deviceId);
924+
if (!device) {
925+
return {};
926+
}
927+
return device->getSysfsRootPath();
928+
}
929+
920930
void InputReader::sysfsNodeChanged(const std::string& sysfsNodePath) {
921931
mEventHub->sysfsNodeChanged(sysfsNodePath);
922932
mEventHub->wake();

services/inputflinger/reader/include/EventHub.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ class EventHubInterface {
399399
/* Disable an input device. Closes file descriptor to that device. */
400400
virtual status_t disableDevice(int32_t deviceId) = 0;
401401

402+
/* Gets the sysfs root path for this device. Returns an empty path if there is none. */
403+
virtual std::filesystem::path getSysfsRootPath(int32_t deviceId) const = 0;
404+
402405
/* Sysfs node changed. Reopen the Eventhub device if any new Peripheral like Light, Battery,
403406
* etc. is detected. */
404407
virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
@@ -614,6 +617,8 @@ class EventHub : public EventHubInterface {
614617

615618
status_t disableDevice(int32_t deviceId) override final;
616619

620+
std::filesystem::path getSysfsRootPath(int32_t deviceId) const override final;
621+
617622
void sysfsNodeChanged(const std::string& sysfsNodePath) override final;
618623

619624
bool setKernelWakeEnabled(int32_t deviceId, bool enabled) override final;

services/inputflinger/reader/include/InputDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class InputDevice {
8181

8282
inline virtual KeyboardType getKeyboardType() const { return mKeyboardType; }
8383

84+
inline std::filesystem::path getSysfsRootPath() const { return mSysfsRootPath; }
85+
8486
bool isEnabled();
8587

8688
void dump(std::string& dump, const std::string& eventHubDevStr);
@@ -209,6 +211,7 @@ class InputDevice {
209211
bool mHasMic;
210212
bool mDropUntilNextSync;
211213
std::optional<bool> mShouldSmoothScroll;
214+
std::filesystem::path mSysfsRootPath;
212215

213216
typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
214217
int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
@@ -471,6 +474,9 @@ class InputDeviceContext {
471474
inline void setKeyboardType(KeyboardType keyboardType) {
472475
return mDevice.setKeyboardType(keyboardType);
473476
}
477+
inline std::filesystem::path getSysfsRootPath() const {
478+
return mEventHub->getSysfsRootPath(mId);
479+
}
474480
inline bool setKernelWakeEnabled(bool enabled) {
475481
return mEventHub->setKernelWakeEnabled(mId, enabled);
476482
}

services/inputflinger/reader/include/InputReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class InputReader : public InputReaderInterface {
118118

119119
std::optional<std::string> getBluetoothAddress(int32_t deviceId) const override;
120120

121+
std::filesystem::path getSysfsRootPath(int32_t deviceId) const override;
122+
121123
void sysfsNodeChanged(const std::string& sysfsNodePath) override;
122124

123125
DeviceId getLastUsedInputDeviceId() override;

services/inputflinger/tests/FakeEventHub.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,14 @@ void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath)
627627
device->sysfsRootPath = sysfsRootPath;
628628
}
629629

630+
std::filesystem::path FakeEventHub::getSysfsRootPath(int32_t deviceId) const {
631+
Device* device = getDevice(deviceId);
632+
if (device == nullptr) {
633+
return {};
634+
}
635+
return device->sysfsRootPath;
636+
}
637+
630638
void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
631639
int32_t foundDeviceId = -1;
632640
Device* foundDevice = nullptr;

services/inputflinger/tests/FakeEventHub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class FakeEventHub : public EventHubInterface {
222222
std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override;
223223
std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
224224
int32_t deviceId, int32_t lightId) const override;
225+
std::filesystem::path getSysfsRootPath(int32_t deviceId) const override;
225226
void sysfsNodeChanged(const std::string& sysfsNodePath) override;
226227
void dump(std::string&) const override {}
227228
void monitor() const override {}

services/inputflinger/tests/InputReader_test.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,10 @@ class InputReaderTest : public testing::Test {
611611
}
612612

613613
void addDevice(int32_t eventHubId, const std::string& name,
614-
ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
614+
ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration,
615+
std::string sysfsRootPath = "") {
615616
mFakeEventHub->addDevice(eventHubId, name, classes);
617+
mFakeEventHub->setSysfsRootPath(eventHubId, sysfsRootPath);
616618

617619
if (configuration) {
618620
mFakeEventHub->addConfigurationMap(eventHubId, configuration);
@@ -664,6 +666,18 @@ TEST_F(InputReaderTest, PolicyGetInputDevices) {
664666
ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
665667
}
666668

669+
TEST_F(InputReaderTest, GetSysfsRootPath) {
670+
constexpr std::string SYSFS_ROOT = "xyz";
671+
ASSERT_NO_FATAL_FAILURE(
672+
addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr, SYSFS_ROOT));
673+
674+
// Should also have received a notification describing the new input device.
675+
ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
676+
InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
677+
678+
ASSERT_EQ(SYSFS_ROOT, mReader->getSysfsRootPath(inputDevice.getId()).string());
679+
}
680+
667681
TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
668682
ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
669683
mFakeEventHub->setSysfsRootPath(1, "xyz");

0 commit comments

Comments
 (0)