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

Commit 92ee5602 authored by Yu Shan's avatar Yu Shan
Browse files

Add some helper functions for Emulator.

Add some helper function for EmulatedVehicleHardware to use.

Test: atest FakeVehicleHardwareTest
Bug: 215419573
Change-Id: I64da5a0eae3872c608da925d68756a823299672c
parent 81fa064d
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -85,6 +85,12 @@ class FakeVehicleHardware : public IVehicleHardware {
    const std::shared_ptr<VehiclePropValuePool> mValuePool;
    const std::shared_ptr<VehiclePropertyStore> mServerSidePropStore;

    ::android::base::Result<VehiclePropValuePool::RecyclableType> getValue(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;

    ::android::base::Result<void> setValue(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

  private:
    // Expose private methods to unit test.
    friend class FakeVehicleHardwareTestHelper;
+70 −52
Original line number Diff line number Diff line
@@ -405,7 +405,6 @@ Result<void> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& v

StatusCode FakeVehicleHardware::setValues(std::shared_ptr<const SetValuesCallback> callback,
                                          const std::vector<SetValueRequest>& requests) {
    std::vector<VehiclePropValue> updatedValues;
    std::vector<SetValueResult> results;
    for (auto& request : requests) {
        const VehiclePropValue& value = request.value;
@@ -417,22 +416,36 @@ StatusCode FakeVehicleHardware::setValues(std::shared_ptr<const SetValuesCallbac

        SetValueResult setValueResult;
        setValueResult.requestId = request.requestId;

        if (auto result = setValue(value); !result.ok()) {
            ALOGE("failed to set value, error: %s, code: %d", getErrorMsg(result).c_str(),
                  getIntErrorCode(result));
            setValueResult.status = getErrorCode(result);
        } else {
            setValueResult.status = StatusCode::OK;
        }

        results.push_back(std::move(setValueResult));
    }

    // In the real vhal, the values will be sent to Car ECU. We just pretend it is done here and
    // send back the updated property values to client.
    (*callback)(std::move(results));

    return StatusCode::OK;
}

Result<void> FakeVehicleHardware::setValue(const VehiclePropValue& value) {
    bool isSpecialValue = false;
    auto setSpecialValueResult = maybeSetSpecialValue(value, &isSpecialValue);

    if (isSpecialValue) {
        if (!setSpecialValueResult.ok()) {
                ALOGE("failed to set special value for property ID: %d, error: %s, status: %d",
                      propId, getErrorMsg(setSpecialValueResult).c_str(),
                      getIntErrorCode(setSpecialValueResult));
                setValueResult.status = getErrorCode(setSpecialValueResult);
            return Error(getIntErrorCode(setSpecialValueResult))
                   << StringPrintf("failed to set special value for property ID: %d, error: %s",
                                   value.prop, getErrorMsg(setSpecialValueResult).c_str());
        }

            // Special values are already handled.
            results.push_back(std::move(setValueResult));
            continue;
        return {};
    }

    auto updatedValue = mValuePool->obtain(value);
@@ -441,18 +454,12 @@ StatusCode FakeVehicleHardware::setValues(std::shared_ptr<const SetValuesCallbac

    auto writeResult = mServerSidePropStore->writeValue(std::move(updatedValue));
    if (!writeResult.ok()) {
            ALOGE("failed to write value into property store, error: %s, code: %d",
                  getErrorMsg(writeResult).c_str(), getIntErrorCode(writeResult));
            setValueResult.status = getErrorCode(writeResult);
        }
        results.push_back(std::move(setValueResult));
        return Error(getIntErrorCode(writeResult))
               << StringPrintf("failed to write value into property store, error: %s",
                               getErrorMsg(writeResult).c_str());
    }

    // In the real vhal, the values will be sent to Car ECU. We just pretend it is done here and
    // send back the updated property values to client.
    (*callback)(std::move(results));

    return StatusCode::OK;
    return {};
}

StatusCode FakeVehicleHardware::getValues(std::shared_ptr<const GetValuesCallback> callback,
@@ -467,42 +474,53 @@ StatusCode FakeVehicleHardware::getValues(std::shared_ptr<const GetValuesCallbac

        GetValueResult getValueResult;
        getValueResult.requestId = request.requestId;
        bool isSpecialValue = false;

        auto result = maybeGetSpecialValue(value, &isSpecialValue);
        if (isSpecialValue) {
        auto result = getValue(value);
        if (!result.ok()) {
                ALOGE("failed to get special value: %d, error: %s, code: %d", value.prop,
                      getErrorMsg(result).c_str(), getIntErrorCode(result));
            ALOGE("failed to get value, error: %s, code: %d", getErrorMsg(result).c_str(),
                  getIntErrorCode(result));
            getValueResult.status = getErrorCode(result);
        } else {
            getValueResult.status = StatusCode::OK;
            getValueResult.prop = *result.value();
        }
        results.push_back(std::move(getValueResult));
            continue;
    }

    // In a real VHAL implementation, getValue would be async and we would call the callback after
    // we actually received the values from vehicle bus. Here we are getting the result
    // synchronously so we could call the callback here.
    (*callback)(std::move(results));

    return StatusCode::OK;
}

Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::getValue(
        const VehiclePropValue& value) const {
    bool isSpecialValue = false;
    auto result = maybeGetSpecialValue(value, &isSpecialValue);
    if (isSpecialValue) {
        if (!result.ok()) {
            return Error(getIntErrorCode(result))
                   << StringPrintf("failed to get special value: %d, error: %s", value.prop,
                                   getErrorMsg(result).c_str());
        } else {
            return std::move(result);
        }
    }

    auto readResult = mServerSidePropStore->readValue(value);
    if (!readResult.ok()) {
        StatusCode errorCode = getErrorCode(readResult);
        if (errorCode == StatusCode::NOT_AVAILABLE) {
                ALOGW("%s", "value has not been set yet");
            return Error(toInt(errorCode)) << "value has not been set yet";
        } else {
                ALOGE("failed to get value, error: %s, code: %d", getErrorMsg(readResult).c_str(),
                      toInt(errorCode));
            return Error(toInt(errorCode))
                   << "failed to get value, error: " << getErrorMsg(readResult);
        }
            getValueResult.status = errorCode;
        } else {
            getValueResult.status = StatusCode::OK;
            getValueResult.prop = *readResult.value();
        }
        results.push_back(std::move(getValueResult));
    }

    (*callback)(std::move(results));

    return StatusCode::OK;
    return std::move(readResult);
}

DumpResult FakeVehicleHardware::dump(const std::vector<std::string>& options) {