Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e136587f authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

EventHub: Refactor AssociatedDevice creation logic

Refactor the AssociatedDevice creation logic to make it easier to
implement upcoming changes to AssociatedDevice initialization.

In particular, we want to make it simple to differentiate the case where
an existing AssociatedDevice needs to be updated from the case where a
new AssociatedDevice needs to be created.

Bug: 245989146
Bug: 357090960
Test: Presubmit
Flag: EXEMPT refactor
Change-Id: I8d86f56f3c00f9ae13232a60e338ff7ec246ce3b
parent 6cd42cbd
Loading
Loading
Loading
Loading
+32 −31
Original line number Diff line number Diff line
@@ -1620,41 +1620,47 @@ std::shared_ptr<const EventHub::AssociatedDevice> EventHub::obtainAssociatedDevi

    const auto& path = *sysfsRootPathOpt;

    std::shared_ptr<const AssociatedDevice> associatedDevice = std::make_shared<AssociatedDevice>(
            AssociatedDevice{.sysfsRootPath = path,
                             .batteryInfos = readBatteryConfiguration(path),
                             .lightInfos = readLightsConfiguration(path),
                             .layoutInfo = readLayoutConfiguration(path)});

    bool associatedDeviceChanged = false;
    std::shared_ptr<const AssociatedDevice> associatedDevice;
    for (const auto& [id, dev] : mDevices) {
        if (dev->associatedDevice && dev->associatedDevice->sysfsRootPath == path) {
            if (*associatedDevice != *dev->associatedDevice) {
                associatedDeviceChanged = true;
                dev->associatedDevice = associatedDevice;
        if (!dev->associatedDevice || dev->associatedDevice->sysfsRootPath != path) {
            continue;
        }
        if (!associatedDevice) {
            // Found matching associated device for the first time.
            associatedDevice = dev->associatedDevice;
            // Reload this associated device if needed.
            const auto reloadedDevice = AssociatedDevice(path);
            if (reloadedDevice != *dev->associatedDevice) {
                ALOGI("The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s",
                      path.c_str(), associatedDevice->dump().c_str());
                associatedDevice = std::make_shared<AssociatedDevice>(std::move(reloadedDevice));
            }
        }
    ALOGI_IF(associatedDeviceChanged,
             "The AssociatedDevice changed for path '%s'. Using new AssociatedDevice: %s",
             path.c_str(), associatedDevice->dump().c_str());
        // Update the associatedDevice.
        dev->associatedDevice = associatedDevice;
    }

    if (!associatedDevice) {
        // No existing associated device found for this path, so create a new one.
        associatedDevice = std::make_shared<AssociatedDevice>(path);
    }

    return associatedDevice;
}

EventHub::AssociatedDevice::AssociatedDevice(const std::filesystem::path& sysfsRootPath)
      : sysfsRootPath(sysfsRootPath),
        batteryInfos(readBatteryConfiguration(sysfsRootPath)),
        lightInfos(readLightsConfiguration(sysfsRootPath)),
        layoutInfo(readLayoutConfiguration(sysfsRootPath)) {}

bool EventHub::AssociatedDevice::isChanged() const {
    std::unordered_map<int32_t, RawBatteryInfo> newBatteryInfos =
            readBatteryConfiguration(sysfsRootPath);
    std::unordered_map<int32_t, RawLightInfo> newLightInfos =
            readLightsConfiguration(sysfsRootPath);
    std::optional<RawLayoutInfo> newLayoutInfo = readLayoutConfiguration(sysfsRootPath);

    if (newBatteryInfos == batteryInfos && newLightInfos == lightInfos &&
        newLayoutInfo == layoutInfo) {
        return false;
    return AssociatedDevice(sysfsRootPath) != *this;
}
    return true;

std::string EventHub::AssociatedDevice::dump() const {
    return StringPrintf("path=%s, numBatteries=%zu, numLight=%zu", sysfsRootPath.c_str(),
                        batteryInfos.size(), lightInfos.size());
}

void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
@@ -2972,9 +2978,4 @@ void EventHub::monitor() const {
    std::unique_lock<std::mutex> lock(mLock);
}

std::string EventHub::AssociatedDevice::dump() const {
    return StringPrintf("path=%s, numBatteries=%zu, numLight=%zu", sysfsRootPath.c_str(),
                        batteryInfos.size(), lightInfos.size());
}

} // namespace android
+1 −0
Original line number Diff line number Diff line
@@ -619,6 +619,7 @@ public:
private:
    // Holds information about the sysfs device associated with the Device.
    struct AssociatedDevice {
        AssociatedDevice(const std::filesystem::path& sysfsRootPath);
        // The sysfs root path of the misc device.
        std::filesystem::path sysfsRootPath;
        std::unordered_map<int32_t /*batteryId*/, RawBatteryInfo> batteryInfos;