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

Commit 65cabc25 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Define custom error type for StatusCode." into tm-dev am: ef480a84

Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/17048690

Change-Id: I9e8b91b061146f8895001ad279443361c6df33c6
parents f4fd0529 ef480a84
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -41,6 +41,8 @@ namespace fake {

class FakeVehicleHardware : public IVehicleHardware {
  public:
    using ValueResultType = android::base::Result<VehiclePropValuePool::RecyclableType, VhalError>;

    FakeVehicleHardware();

    explicit FakeVehicleHardware(std::unique_ptr<VehiclePropValuePool> valuePool);
@@ -85,10 +87,10 @@ class FakeVehicleHardware : public IVehicleHardware {
    const std::shared_ptr<VehiclePropValuePool> mValuePool;
    const std::shared_ptr<VehiclePropertyStore> mServerSidePropStore;

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

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

  private:
@@ -115,19 +117,19 @@ class FakeVehicleHardware : public IVehicleHardware {
    // Override the properties using config files in 'overrideDir'.
    void overrideProperties(const char* overrideDir);

    android::base::Result<void> maybeSetSpecialValue(
    android::base::Result<void, VhalError> maybeSetSpecialValue(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value,
            bool* isSpecialValue);
    android::base::Result<VehiclePropValuePool::RecyclableType> maybeGetSpecialValue(
    ValueResultType maybeGetSpecialValue(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value,
            bool* isSpecialValue) const;
    android::base::Result<void> setApPowerStateReport(
    android::base::Result<void, VhalError> setApPowerStateReport(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
    VehiclePropValuePool::RecyclableType createApPowerStateReq(
            aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReq state);
    android::base::Result<void> setUserHalProp(
    android::base::Result<void, VhalError> setUserHalProp(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
    android::base::Result<VehiclePropValuePool::RecyclableType> getUserHalProp(
    ValueResultType getUserHalProp(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;
    bool isHvacPropAndHvacNotAvailable(int32_t propId);

+25 −24
Original line number Diff line number Diff line
@@ -68,6 +68,8 @@ using ::android::base::Result;
using ::android::base::StartsWith;
using ::android::base::StringPrintf;

using StatusError = android::base::Error<VhalError>;

const char* VENDOR_OVERRIDE_DIR = "/vendor/etc/automotive/vhaloverride/";
const char* OVERRIDE_PROPERTY = "persist.vendor.vhal_init_value_override";

@@ -191,13 +193,13 @@ VehiclePropValuePool::RecyclableType FakeVehicleHardware::createApPowerStateReq(
    return req;
}

Result<void> FakeVehicleHardware::setApPowerStateReport(const VehiclePropValue& value) {
Result<void, VhalError> FakeVehicleHardware::setApPowerStateReport(const VehiclePropValue& value) {
    auto updatedValue = mValuePool->obtain(value);
    updatedValue->timestamp = elapsedRealtimeNano();

    if (auto writeResult = mServerSidePropStore->writeValue(std::move(updatedValue));
        !writeResult.ok()) {
        return Error(getIntErrorCode(writeResult))
        return StatusError(getErrorCode(writeResult))
               << "failed to write value into property store, error: " << getErrorMsg(writeResult);
    }

@@ -224,7 +226,7 @@ Result<void> FakeVehicleHardware::setApPowerStateReport(const VehiclePropValue&
            if (auto writeResult =
                        mServerSidePropStore->writeValue(std::move(prop), /*updateStatus=*/true);
                !writeResult.ok()) {
                return Error(getIntErrorCode(writeResult))
                return StatusError(getErrorCode(writeResult))
                       << "failed to write AP_POWER_STATE_REQ into property store, error: "
                       << getErrorMsg(writeResult);
            }
@@ -241,7 +243,7 @@ Result<void> FakeVehicleHardware::setApPowerStateReport(const VehiclePropValue&
            if (auto writeResult =
                        mServerSidePropStore->writeValue(std::move(prop), /*updateStatus=*/true);
                !writeResult.ok()) {
                return Error(getIntErrorCode(writeResult))
                return StatusError(getErrorCode(writeResult))
                       << "failed to write AP_POWER_STATE_REQ into property store, error: "
                       << getErrorMsg(writeResult);
            }
@@ -268,10 +270,10 @@ bool FakeVehicleHardware::isHvacPropAndHvacNotAvailable(int32_t propId) {
    return false;
}

Result<void> FakeVehicleHardware::setUserHalProp(const VehiclePropValue& value) {
Result<void, VhalError> FakeVehicleHardware::setUserHalProp(const VehiclePropValue& value) {
    auto result = mFakeUserHal->onSetProperty(value);
    if (!result.ok()) {
        return Error(getIntErrorCode(result))
        return StatusError(getErrorCode(result))
               << "onSetProperty(): HAL returned error: " << getErrorMsg(result);
    }
    auto& updatedValue = result.value();
@@ -280,7 +282,7 @@ Result<void> FakeVehicleHardware::setUserHalProp(const VehiclePropValue& value)
              updatedValue->toString().c_str());
        if (auto writeResult = mServerSidePropStore->writeValue(std::move(result.value()));
            !writeResult.ok()) {
            return Error(getIntErrorCode(writeResult))
            return StatusError(getErrorCode(writeResult))
                   << "failed to write value into property store, error: "
                   << getErrorMsg(writeResult);
        }
@@ -288,14 +290,14 @@ Result<void> FakeVehicleHardware::setUserHalProp(const VehiclePropValue& value)
    return {};
}

Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::getUserHalProp(
FakeVehicleHardware::ValueResultType FakeVehicleHardware::getUserHalProp(
        const VehiclePropValue& value) const {
    auto propId = value.prop;
    ALOGI("get(): getting value for prop %d from User HAL", propId);

    auto result = mFakeUserHal->onGetProperty(value);
    if (!result.ok()) {
        return Error(getIntErrorCode(result))
        return StatusError(getErrorCode(result))
               << "get(): User HAL returned error: " << getErrorMsg(result);
    } else {
        auto& gotValue = result.value();
@@ -304,17 +306,16 @@ Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::getUserHalProp
            gotValue->timestamp = elapsedRealtimeNano();
            return result;
        } else {
            return Error(toInt(StatusCode::INTERNAL_ERROR))
                   << "get(): User HAL returned null value";
            return StatusError(StatusCode::INTERNAL_ERROR) << "get(): User HAL returned null value";
        }
    }
}

Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::maybeGetSpecialValue(
FakeVehicleHardware::ValueResultType FakeVehicleHardware::maybeGetSpecialValue(
        const VehiclePropValue& value, bool* isSpecialValue) const {
    *isSpecialValue = false;
    int32_t propId = value.prop;
    Result<VehiclePropValuePool::RecyclableType> result;
    ValueResultType result;

    if (mFakeUserHal->isSupported(propId)) {
        *isSpecialValue = true;
@@ -344,7 +345,7 @@ Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::maybeGetSpecia
    return nullptr;
}

Result<void> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& value,
Result<void, VhalError> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& value,
                                                                  bool* isSpecialValue) {
    *isSpecialValue = false;
    VehiclePropValuePool::RecyclableType updatedValue;
@@ -357,7 +358,7 @@ Result<void> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& v

    if (isHvacPropAndHvacNotAvailable(propId)) {
        *isSpecialValue = true;
        return Error(toInt(StatusCode::NOT_AVAILABLE)) << "hvac not available";
        return StatusError(StatusCode::NOT_AVAILABLE) << "hvac not available";
    }

    switch (propId) {
@@ -396,7 +397,7 @@ Result<void> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& v
            updatedValue->areaId = value.areaId;
            if (auto writeResult = mServerSidePropStore->writeValue(std::move(updatedValue));
                !writeResult.ok()) {
                return Error(getIntErrorCode(writeResult))
                return StatusError(getErrorCode(writeResult))
                       << "failed to write value into property store, error: "
                       << getErrorMsg(writeResult);
            }
@@ -441,13 +442,13 @@ StatusCode FakeVehicleHardware::setValues(std::shared_ptr<const SetValuesCallbac
    return StatusCode::OK;
}

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

    if (isSpecialValue) {
        if (!setSpecialValueResult.ok()) {
            return Error(getIntErrorCode(setSpecialValueResult))
            return StatusError(getErrorCode(setSpecialValueResult))
                   << StringPrintf("failed to set special value for property ID: %d, error: %s",
                                   value.prop, getErrorMsg(setSpecialValueResult).c_str());
        }
@@ -460,7 +461,7 @@ Result<void> FakeVehicleHardware::setValue(const VehiclePropValue& value) {

    auto writeResult = mServerSidePropStore->writeValue(std::move(updatedValue));
    if (!writeResult.ok()) {
        return Error(getIntErrorCode(writeResult))
        return StatusError(getErrorCode(writeResult))
               << StringPrintf("failed to write value into property store, error: %s",
                               getErrorMsg(writeResult).c_str());
    }
@@ -501,13 +502,13 @@ StatusCode FakeVehicleHardware::getValues(std::shared_ptr<const GetValuesCallbac
    return StatusCode::OK;
}

Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::getValue(
FakeVehicleHardware::ValueResultType FakeVehicleHardware::getValue(
        const VehiclePropValue& value) const {
    bool isSpecialValue = false;
    auto result = maybeGetSpecialValue(value, &isSpecialValue);
    if (isSpecialValue) {
        if (!result.ok()) {
            return Error(getIntErrorCode(result))
            return StatusError(getErrorCode(result))
                   << StringPrintf("failed to get special value: %d, error: %s", value.prop,
                                   getErrorMsg(result).c_str());
        } else {
@@ -519,9 +520,9 @@ Result<VehiclePropValuePool::RecyclableType> FakeVehicleHardware::getValue(
    if (!readResult.ok()) {
        StatusCode errorCode = getErrorCode(readResult);
        if (errorCode == StatusCode::NOT_AVAILABLE) {
            return Error(toInt(errorCode)) << "value has not been set yet";
            return StatusError(errorCode) << "value has not been set yet";
        } else {
            return Error(toInt(errorCode))
            return StatusError(errorCode)
                   << "failed to get value, error: " << getErrorMsg(readResult);
        }
    }
+3 −3
Original line number Diff line number Diff line
@@ -38,11 +38,11 @@ class FakeObd2Frame final {
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig);
    void initObd2FreezeFrame(
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig);
    android::base::Result<VehiclePropValuePool::RecyclableType> getObd2FreezeFrame(
    android::base::Result<VehiclePropValuePool::RecyclableType, VhalError> getObd2FreezeFrame(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue&
                    requestedPropValue) const;
    android::base::Result<VehiclePropValuePool::RecyclableType> getObd2DtcInfo() const;
    android::base::Result<void> clearObd2FreezeFrames(
    android::base::Result<VehiclePropValuePool::RecyclableType, VhalError> getObd2DtcInfo() const;
    android::base::Result<void, VhalError> clearObd2FreezeFrames(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
    static bool isDiagnosticProperty(
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& propConfig);
+11 −10
Original line number Diff line number Diff line
@@ -44,9 +44,10 @@ using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
using ::android::base::Error;
using ::android::base::Result;

using StatusError = android::base::Error<VhalError>;

std::unique_ptr<Obd2SensorStore> FakeObd2Frame::fillDefaultObd2Frame(size_t numVendorIntegerSensors,
                                                                     size_t numVendorFloatSensors) {
    std::unique_ptr<Obd2SensorStore> sensorStore(new Obd2SensorStore(
@@ -126,37 +127,37 @@ void FakeObd2Frame::initObd2FreezeFrame(const VehiclePropConfig& propConfig) {
    }
}

Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2FreezeFrame(
Result<VehiclePropValuePool::RecyclableType, VhalError> FakeObd2Frame::getObd2FreezeFrame(
        const VehiclePropValue& requestedPropValue) const {
    if (requestedPropValue.value.int64Values.size() != 1) {
        return Error(toInt(StatusCode::INVALID_ARG))
        return StatusError(StatusCode::INVALID_ARG)
               << "asked for OBD2_FREEZE_FRAME without valid timestamp";
    }
    auto readValuesResult = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME);
    if (!readValuesResult.ok()) {
        return Error(toInt(StatusCode::INTERNAL_ERROR))
        return StatusError(StatusCode::INTERNAL_ERROR)
               << "failed to read OBD2_FREEZE_FRAME property: "
               << readValuesResult.error().message();
    }
    if (readValuesResult.value().size() == 0) {
        // Should no freeze frame be available at the given timestamp, a response of NOT_AVAILABLE
        // must be returned by the implementation
        return Error(toInt(StatusCode::NOT_AVAILABLE));
        return StatusError(StatusCode::NOT_AVAILABLE);
    }
    auto timestamp = requestedPropValue.value.int64Values[0];
    auto readValueResult = mPropStore->readValue(OBD2_FREEZE_FRAME, /*area=*/0, timestamp);
    if (!readValueResult.ok()) {
        return Error(toInt(StatusCode::INVALID_ARG))
        return StatusError(StatusCode::INVALID_ARG)
               << "asked for OBD2_FREEZE_FRAME at invalid timestamp";
    }
    return readValueResult;
}

Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2DtcInfo() const {
Result<VehiclePropValuePool::RecyclableType, VhalError> FakeObd2Frame::getObd2DtcInfo() const {
    std::vector<int64_t> timestamps;
    auto result = mPropStore->readValuesForProperty(OBD2_FREEZE_FRAME);
    if (!result.ok()) {
        return Error(toInt(StatusCode::INTERNAL_ERROR))
        return StatusError(StatusCode::INTERNAL_ERROR)
               << "failed to read OBD2_FREEZE_FRAME property: " << result.error().message();
    }
    for (const auto& freezeFrame : result.value()) {
@@ -169,7 +170,7 @@ Result<VehiclePropValuePool::RecyclableType> FakeObd2Frame::getObd2DtcInfo() con
    return outValue;
}

Result<void> FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propValue) {
Result<void, VhalError> FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propValue) {
    if (propValue.value.int64Values.size() == 0) {
        mPropStore->removeValuesForProperty(OBD2_FREEZE_FRAME);
        return {};
@@ -177,7 +178,7 @@ Result<void> FakeObd2Frame::clearObd2FreezeFrames(const VehiclePropValue& propVa
    for (int64_t timestamp : propValue.value.int64Values) {
        auto result = mPropStore->readValue(OBD2_FREEZE_FRAME, 0, timestamp);
        if (!result.ok()) {
            return Error(toInt(StatusCode::INVALID_ARG))
            return StatusError(StatusCode::INVALID_ARG)
                   << "asked for OBD2_FREEZE_FRAME at invalid timestamp, error: %s"
                   << result.error().message();
        }
+13 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@

#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>
#include <VehicleUtils.h>

#include <memory>
#include <mutex>
@@ -38,6 +39,8 @@ constexpr char kUserHalDumpOption[] = "--user-hal";
// Class used to emulate a real User HAL behavior through lshal debug requests.
class FakeUserHal final {
  public:
    using ValueResultType = android::base::Result<VehiclePropValuePool::RecyclableType, VhalError>;

    explicit FakeUserHal(std::shared_ptr<VehiclePropValuePool> valuePool) : mValuePool(valuePool) {}

    ~FakeUserHal() = default;
@@ -48,13 +51,13 @@ class FakeUserHal final {
    // Lets the emulator set the property.
    //
    // @return updated property and StatusCode
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetProperty(
    ValueResultType onSetProperty(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Gets the property value from the emulator.
    //
    // @return property value and StatusCode
    android::base::Result<VehiclePropValuePool::RecyclableType> onGetProperty(
    ValueResultType onGetProperty(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;

    // Shows the User HAL emulation help.
@@ -93,34 +96,33 @@ class FakeUserHal final {
    // - if it's 2, reply with mInitialUserResponseFromCmd but a wrong request id (so Android can
    // test this error scenario)
    // - if it's 3, then don't send a property change (so Android can emulate a timeout)
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetInitialUserInfoResponse(
    ValueResultType onSetInitialUserInfoResponse(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate SWITCH_USER - see onSetInitialUserInfoResponse() for usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetSwitchUserResponse(
    ValueResultType onSetSwitchUserResponse(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetCreateUserResponse(
    ValueResultType onSetCreateUserResponse(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
    // usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetUserIdentificationAssociation(
    ValueResultType onSetUserIdentificationAssociation(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
    // usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onGetUserIdentificationAssociation(
    ValueResultType onGetUserIdentificationAssociation(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;

    // Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal.
    static android::base::Result<VehiclePropValuePool::RecyclableType>
    defaultUserIdentificationAssociation(
    static ValueResultType defaultUserIdentificationAssociation(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request);

    android::base::Result<VehiclePropValuePool::RecyclableType> sendUserHalResponse(
            VehiclePropValuePool::RecyclableType response, int32_t requestId);
    ValueResultType sendUserHalResponse(VehiclePropValuePool::RecyclableType response,
                                        int32_t requestId);
};

}  // namespace fake
Loading