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

Commit 515f5b5e authored by Andrei Ciubotariu's avatar Andrei Ciubotariu
Browse files

BatteryMonitor: Ensure batteryHealthData has value when needed

Since batteryHealthData is annotated in the AIDL with @nullable,
ensure that the std::optional that encloses it has a value when
a field needs to be assigned to batteryHealthData.

If no batteryHealthData updates are needed, the optional remains
"valueless".

Bug: 396737015
Test: Manual
Flag: EXEMPT bugfix
Change-Id: Ica6f20656a566054d7cbaaf339813be6e0e93b78
parent 9e6bd0b8
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -131,6 +131,7 @@ static void initHealthInfo(HealthInfo* health_info) {
                    (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
            .batteryStatus = BatteryStatus::UNKNOWN,
            .batteryHealth = BatteryHealth::UNKNOWN,
            .batteryHealthData = std::nullopt,
    };
}

@@ -360,6 +361,14 @@ static bool isScopedPowerSupply(const char* name) {
    return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
}

static BatteryHealthData *ensureBatteryHealthData(HealthInfo *info) {
    if (!info->batteryHealthData.has_value()) {
        return &info->batteryHealthData.emplace();
    }

    return &info->batteryHealthData.value();
}

void BatteryMonitor::updateValues(void) {
    initHealthInfo(mHealthInfo.get());

@@ -402,15 +411,15 @@ void BatteryMonitor::updateValues(void) {
        mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);

    if (!mHealthdConfig->batteryStateOfHealthPath.empty())
        mHealthInfo->batteryHealthData->batteryStateOfHealth =
        ensureBatteryHealthData(mHealthInfo.get())->batteryStateOfHealth =
                getIntField(mHealthdConfig->batteryStateOfHealthPath);

    if (!mHealthdConfig->batteryManufacturingDatePath.empty())
        mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
        ensureBatteryHealthData(mHealthInfo.get())->batteryManufacturingDateSeconds =
                getIntField(mHealthdConfig->batteryManufacturingDatePath);

    if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
        mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
        ensureBatteryHealthData(mHealthInfo.get())->batteryFirstUsageSeconds =
                getIntField(mHealthdConfig->batteryFirstUsageDatePath);

    mHealthInfo->batteryTemperatureTenthsCelsius =