@@ -1620,41 +1620,47 @@ std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDevi
16201620
16211621 const auto & path = *sysfsRootPathOpt;
16221622
1623- std::shared_ptr<const AssociatedDevice> associatedDevice = std::make_shared<AssociatedDevice>(
1624- AssociatedDevice{.sysfsRootPath = path,
1625- .batteryInfos = readBatteryConfiguration (path),
1626- .lightInfos = readLightsConfiguration (path),
1627- .layoutInfo = readLayoutConfiguration (path)});
1628-
1629- bool associatedDeviceChanged = false ;
1623+ std::shared_ptr<const AssociatedDevice> associatedDevice;
16301624 for (const auto & [id, dev] : mDevices ) {
1631- if (dev->associatedDevice && dev->associatedDevice ->sysfsRootPath = = path) {
1632- if (*associatedDevice != *dev-> associatedDevice ) {
1633- associatedDeviceChanged = true ;
1634- dev-> associatedDevice = associatedDevice;
1635- }
1625+ if (! dev->associatedDevice || dev->associatedDevice ->sysfsRootPath ! = path) {
1626+ continue ;
1627+ }
1628+ if (! associatedDevice) {
1629+ // Found matching associated device for the first time.
16361630 associatedDevice = dev->associatedDevice ;
1631+ // Reload this associated device if needed.
1632+ const auto reloadedDevice = AssociatedDevice (path);
1633+ if (reloadedDevice != *dev->associatedDevice ) {
1634+ ALOGI (" The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s" ,
1635+ path.c_str (), associatedDevice->dump ().c_str ());
1636+ associatedDevice = std::make_shared<AssociatedDevice>(std::move (reloadedDevice));
1637+ }
16371638 }
1639+ // Update the associatedDevice.
1640+ dev->associatedDevice = associatedDevice;
1641+ }
1642+
1643+ if (!associatedDevice) {
1644+ // No existing associated device found for this path, so create a new one.
1645+ associatedDevice = std::make_shared<AssociatedDevice>(path);
16381646 }
1639- ALOGI_IF (associatedDeviceChanged,
1640- " The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s" ,
1641- path.c_str (), associatedDevice->dump ().c_str ());
16421647
16431648 return associatedDevice;
16441649}
16451650
1651+ EventHub::AssociatedDevice::AssociatedDevice (const std::filesystem::path& sysfsRootPath)
1652+ : sysfsRootPath(sysfsRootPath),
1653+ batteryInfos (readBatteryConfiguration(sysfsRootPath)),
1654+ lightInfos(readLightsConfiguration(sysfsRootPath)),
1655+ layoutInfo(readLayoutConfiguration(sysfsRootPath)) {}
1656+
16461657bool EventHub::AssociatedDevice::isChanged () const {
1647- std::unordered_map<int32_t , RawBatteryInfo> newBatteryInfos =
1648- readBatteryConfiguration (sysfsRootPath);
1649- std::unordered_map<int32_t , RawLightInfo> newLightInfos =
1650- readLightsConfiguration (sysfsRootPath);
1651- std::optional<RawLayoutInfo> newLayoutInfo = readLayoutConfiguration (sysfsRootPath);
1652-
1653- if (newBatteryInfos == batteryInfos && newLightInfos == lightInfos &&
1654- newLayoutInfo == layoutInfo) {
1655- return false ;
1656- }
1657- return true ;
1658+ return AssociatedDevice (sysfsRootPath) != *this ;
1659+ }
1660+
1661+ std::string EventHub::AssociatedDevice::dump () const {
1662+ return StringPrintf (" path=%s, numBatteries=%zu, numLight=%zu" , sysfsRootPath.c_str (),
1663+ batteryInfos.size (), lightInfos.size ());
16581664}
16591665
16601666void EventHub::vibrate (int32_t deviceId, const VibrationElement& element) {
@@ -2972,9 +2978,4 @@ void EventHub::monitor() const {
29722978 std::unique_lock<std::mutex> lock (mLock );
29732979}
29742980
2975- std::string EventHub::AssociatedDevice::dump () const {
2976- return StringPrintf (" path=%s, numBatteries=%zu, numLight=%zu" , sysfsRootPath.c_str (),
2977- batteryInfos.size (), lightInfos.size ());
2978- }
2979-
29802981} // namespace android
0 commit comments