Define custom error type for StatusCode.
Define a wrapper class for StatusCode to be used as E in
Result<T, E>. By default E is errorno as defined in linux and does
not print out the correct error message.
Migrate existing Result usage in VHAL to Result<T, VhalError>.
Test: atest VehicleUtilsTest
Bug: 219612366
Change-Id: Ic5369b6035bc46afc4c61da26c4b50f7fa9969d8
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
index 9634c80..fa494c6 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
@@ -41,6 +41,8 @@
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 @@
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 @@
// 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);
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
index a78d989..233efc8 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
@@ -68,6 +68,8 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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,8 +345,8 @@
return nullptr;
}
-Result<void> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& value,
- bool* isSpecialValue) {
+Result<void, VhalError> FakeVehicleHardware::maybeSetSpecialValue(const VehiclePropValue& value,
+ bool* isSpecialValue) {
*isSpecialValue = false;
VehiclePropValuePool::RecyclableType updatedValue;
int32_t propId = value.prop;
@@ -357,7 +358,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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);
}
}
diff --git a/automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h b/automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h
index fa6d8f9..ba40f60 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/obd2frame/include/FakeObd2Frame.h
@@ -38,11 +38,11 @@
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);
diff --git a/automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp b/automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp
index 5585fb4..0a4affc 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/obd2frame/src/FakeObd2Frame.cpp
@@ -44,9 +44,10 @@
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 @@
}
}
-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 @@
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 @@
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();
}
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h
index a220146..9c4887c 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h
@@ -23,6 +23,7 @@
#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>
+#include <VehicleUtils.h>
#include <memory>
#include <mutex>
@@ -38,6 +39,8 @@
// 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 @@
// 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 @@
// - 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
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp
index 9b60053..e37f619 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp
@@ -44,6 +44,8 @@
using ::android::base::Error;
using ::android::base::Result;
+using StatusError = android::base::Error<VhalError>;
+
constexpr int32_t INITIAL_USER_INFO = toInt(VehicleProperty::INITIAL_USER_INFO);
constexpr int32_t SWITCH_USER = toInt(VehicleProperty::SWITCH_USER);
constexpr int32_t CREATE_USER = toInt(VehicleProperty::CREATE_USER);
@@ -51,20 +53,24 @@
constexpr int32_t USER_IDENTIFICATION_ASSOCIATION =
toInt(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION);
-Result<int32_t> getRequestId(const VehiclePropValue& value) {
+Result<int32_t, VhalError> getRequestId(const VehiclePropValue& value) {
if (value.value.int32Values.size() < 1) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "no int32Values on property: " << value.toString();
}
return value.value.int32Values[0];
}
-Result<SwitchUserMessageType> getSwitchUserMessageType(const VehiclePropValue& value) {
+Result<SwitchUserMessageType, VhalError> getSwitchUserMessageType(const VehiclePropValue& value) {
if (value.value.int32Values.size() < 2) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "missing switch user message type on property: " << value.toString();
}
- return user_hal_helper::verifyAndCast<SwitchUserMessageType>(value.value.int32Values[1]);
+ auto result = user_hal_helper::verifyAndCast<SwitchUserMessageType>(value.value.int32Values[1]);
+ if (!result.ok()) {
+ return StatusError(StatusCode::INVALID_ARG) << result.error().message();
+ }
+ return result.value();
}
} // namespace
@@ -82,8 +88,7 @@
}
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetProperty(
- const VehiclePropValue& value) {
+FakeUserHal::ValueResultType FakeUserHal::onSetProperty(const VehiclePropValue& value) {
ALOGV("onSetProperty(): %s", value.toString().c_str());
switch (value.prop) {
@@ -99,13 +104,12 @@
case USER_IDENTIFICATION_ASSOCIATION:
return onSetUserIdentificationAssociation(value);
default:
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "Unsupported property: " << value.toString();
}
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onGetProperty(
- const VehiclePropValue& value) const {
+FakeUserHal::ValueResultType FakeUserHal::onGetProperty(const VehiclePropValue& value) const {
ALOGV("onGetProperty(%s)", value.toString().c_str());
switch (value.prop) {
case INITIAL_USER_INFO:
@@ -113,16 +117,16 @@
case CREATE_USER:
case REMOVE_USER:
ALOGE("onGetProperty(): %d is only supported on SET", value.prop);
- return Error(toInt(StatusCode::INVALID_ARG)) << "only supported on SET";
+ return StatusError(StatusCode::INVALID_ARG) << "only supported on SET";
case USER_IDENTIFICATION_ASSOCIATION:
return onGetUserIdentificationAssociation(value);
default:
ALOGE("onGetProperty(): %d is not supported", value.prop);
- return Error(toInt(StatusCode::INVALID_ARG)) << "not supported by User HAL";
+ return StatusError(StatusCode::INVALID_ARG) << "not supported by User HAL";
}
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onGetUserIdentificationAssociation(
+FakeUserHal::ValueResultType FakeUserHal::onGetUserIdentificationAssociation(
const VehiclePropValue& value) const {
std::scoped_lock<std::mutex> lockGuard(mLock);
@@ -143,7 +147,7 @@
return newValue;
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetInitialUserInfoResponse(
+FakeUserHal::ValueResultType FakeUserHal::onSetInitialUserInfoResponse(
const VehiclePropValue& value) {
std::scoped_lock<std::mutex> lockGuard(mLock);
@@ -178,8 +182,7 @@
return updatedValue;
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetSwitchUserResponse(
- const VehiclePropValue& value) {
+FakeUserHal::ValueResultType FakeUserHal::onSetSwitchUserResponse(const VehiclePropValue& value) {
std::scoped_lock<std::mutex> lockGuard(mLock);
auto requestId = getRequestId(value);
@@ -234,8 +237,7 @@
return updatedValue;
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetCreateUserResponse(
- const VehiclePropValue& value) {
+FakeUserHal::ValueResultType FakeUserHal::onSetCreateUserResponse(const VehiclePropValue& value) {
std::scoped_lock<std::mutex> lockGuard(mLock);
auto requestId = getRequestId(value);
@@ -268,7 +270,7 @@
return updatedValue;
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetUserIdentificationAssociation(
+FakeUserHal::ValueResultType FakeUserHal::onSetUserIdentificationAssociation(
const VehiclePropValue& value) {
std::scoped_lock<std::mutex> lockGuard(mLock);
@@ -298,14 +300,14 @@
return defaultUserIdentificationAssociation(value);
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::defaultUserIdentificationAssociation(
+FakeUserHal::ValueResultType FakeUserHal::defaultUserIdentificationAssociation(
const VehiclePropValue& request) {
// TODO(b/159498909): return a response with NOT_ASSOCIATED_ANY_USER for all requested types
ALOGE("no lshal response for %s; replying with NOT_AVAILABLE", request.toString().c_str());
- return Error(toInt(StatusCode::NOT_AVAILABLE)) << "not set by lshal";
+ return StatusError(StatusCode::NOT_AVAILABLE) << "not set by lshal";
}
-Result<VehiclePropValuePool::RecyclableType> FakeUserHal::sendUserHalResponse(
+FakeUserHal::ValueResultType FakeUserHal::sendUserHalResponse(
VehiclePropValuePool::RecyclableType response, int32_t requestId) {
switch (response->areaId) {
case 1:
@@ -319,12 +321,12 @@
case 3:
ALOGD("not generating a property change event because of lshal prop: %s",
response->toString().c_str());
- return Error(toInt(StatusCode::NOT_AVAILABLE))
+ return StatusError(StatusCode::NOT_AVAILABLE)
<< "not generating a property change event because of lshal prop: "
<< response->toString();
default:
ALOGE("invalid action on lshal response: %s", response->toString().c_str());
- return Error(toInt(StatusCode::INTERNAL_ERROR))
+ return StatusError(StatusCode::INTERNAL_ERROR)
<< "invalid action on lshal response: " << response->toString();
}
diff --git a/automotive/vehicle/aidl/impl/utils/common/include/PendingRequestPool.h b/automotive/vehicle/aidl/impl/utils/common/include/PendingRequestPool.h
index dcf5057..4ab8acd 100644
--- a/automotive/vehicle/aidl/impl/utils/common/include/PendingRequestPool.h
+++ b/automotive/vehicle/aidl/impl/utils/common/include/PendingRequestPool.h
@@ -17,6 +17,7 @@
#ifndef android_hardware_automotive_vehicle_aidl_impl_vhal_include_PendingRequestPool_H_
#define android_hardware_automotive_vehicle_aidl_impl_vhal_include_PendingRequestPool_H_
+#include <VehicleUtils.h>
#include <android-base/result.h>
#include <android-base/thread_annotations.h>
@@ -49,9 +50,9 @@
// added. Otherwise, they would be added to the request pool.
// The callback would be called if requests are not finished within {@code mTimeoutInNano}
// seconds.
- android::base::Result<void> addRequests(const void* clientId,
- const std::unordered_set<int64_t>& requestIds,
- std::shared_ptr<const TimeoutCallbackFunc> callback);
+ android::base::Result<void, VhalError> addRequests(
+ const void* clientId, const std::unordered_set<int64_t>& requestIds,
+ std::shared_ptr<const TimeoutCallbackFunc> callback);
// Checks whether the request is currently pending.
bool isRequestPending(const void* clientId, int64_t requestId) const;
diff --git a/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h b/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
index 2c7aa97..cebf95c 100644
--- a/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
+++ b/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
@@ -25,6 +25,7 @@
#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>
+#include <VehicleUtils.h>
#include <android-base/result.h>
#include <android-base/thread_annotations.h>
@@ -42,6 +43,10 @@
// This class is thread-safe, however it uses blocking synchronization across all methods.
class VehiclePropertyStore final {
public:
+ using ValueResultType = android::base::Result<VehiclePropValuePool::RecyclableType, VhalError>;
+ using ValuesResultType =
+ android::base::Result<std::vector<VehiclePropValuePool::RecyclableType>, VhalError>;
+
explicit VehiclePropertyStore(std::shared_ptr<VehiclePropValuePool> valuePool)
: mValuePool(valuePool) {}
@@ -68,8 +73,8 @@
// 'status' would be initialized to {@code VehiclePropertyStatus::AVAILABLE}, if this is to
// override an existing value, the status for the existing value would be used for the
// overridden value.
- android::base::Result<void> writeValue(VehiclePropValuePool::RecyclableType propValue,
- bool updateStatus = false);
+ android::base::Result<void, VhalError> writeValue(
+ VehiclePropValuePool::RecyclableType propValue, bool updateStatus = false);
// Remove a given property value from the property store. The 'propValue' would be used to
// generate the key for the value to remove.
@@ -83,28 +88,26 @@
std::vector<VehiclePropValuePool::RecyclableType> readAllValues() const;
// Read all the values for the property.
- android::base::Result<std::vector<VehiclePropValuePool::RecyclableType>> readValuesForProperty(
- int32_t propId) const;
+ ValuesResultType readValuesForProperty(int32_t propId) const;
// Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
// value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
// not configured.
- android::base::Result<VehiclePropValuePool::RecyclableType> readValue(
+ ValueResultType readValue(
const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request) const;
// Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
// value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
// not configured.
- android::base::Result<VehiclePropValuePool::RecyclableType> readValue(int32_t prop,
- int32_t area = 0,
- int64_t token = 0) const;
+ ValueResultType readValue(int32_t prop, int32_t area = 0, int64_t token = 0) const;
// Get all property configs.
std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> getAllConfigs()
const;
// Get the property config for the requested property.
- android::base::Result<const aidl::android::hardware::automotive::vehicle::VehiclePropConfig*>
+ android::base::Result<const aidl::android::hardware::automotive::vehicle::VehiclePropConfig*,
+ VhalError>
getConfig(int32_t propId) const;
// Set a callback that would be called when a property value has been updated.
@@ -146,8 +149,7 @@
const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue,
const Record& record) const;
- android::base::Result<VehiclePropValuePool::RecyclableType> readValueLocked(
- const RecordId& recId, const Record& record) const;
+ ValueResultType readValueLocked(const RecordId& recId, const Record& record) const;
};
} // namespace vehicle
diff --git a/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h b/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
index 1fc5613..8ef0218 100644
--- a/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
+++ b/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
@@ -190,59 +190,6 @@
return size;
}
-template <class T>
-aidl::android::hardware::automotive::vehicle::StatusCode getErrorCode(
- const android::base::Result<T>& result) {
- if (result.ok()) {
- return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
- }
- return static_cast<aidl::android::hardware::automotive::vehicle::StatusCode>(
- result.error().code());
-}
-
-template <class T>
-int getIntErrorCode(const android::base::Result<T>& result) {
- return toInt(getErrorCode(result));
-}
-
-template <class T>
-std::string getErrorMsg(const android::base::Result<T>& result) {
- if (result.ok()) {
- return "";
- }
- return result.error().message();
-}
-
-template <class T>
-ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T>& result,
- aidl::android::hardware::automotive::vehicle::StatusCode status,
- const std::string& additionalErrorMsg) {
- if (result.ok()) {
- return ndk::ScopedAStatus::ok();
- }
- return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
- toInt(status),
- fmt::format("{}, error: {}", additionalErrorMsg, getErrorMsg(result)).c_str());
-}
-
-template <class T>
-ndk::ScopedAStatus toScopedAStatus(
- const android::base::Result<T>& result,
- aidl::android::hardware::automotive::vehicle::StatusCode status) {
- return toScopedAStatus(result, status, "");
-}
-
-template <class T>
-ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T>& result) {
- return toScopedAStatus(result, getErrorCode(result));
-}
-
-template <class T>
-ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T>& result,
- const std::string& additionalErrorMsg) {
- return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
-}
-
// Check whether the value is valid according to config.
// We check for the following:
// * If the type is INT32, {@code value.int32Values} must contain one element.
@@ -283,6 +230,79 @@
const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value,
const aidl::android::hardware::automotive::vehicle::VehicleAreaConfig* config);
+// VhalError is a wrapper class for {@code StatusCode} that could act as E in {@code Result<T,E>}.
+class VhalError final {
+ public:
+ VhalError() : mCode(aidl::android::hardware::automotive::vehicle::StatusCode::OK) {}
+
+ VhalError(aidl::android::hardware::automotive::vehicle::StatusCode&& code) : mCode(code) {}
+
+ VhalError(const aidl::android::hardware::automotive::vehicle::StatusCode& code) : mCode(code) {}
+
+ aidl::android::hardware::automotive::vehicle::StatusCode value() const;
+
+ inline operator aidl::android::hardware::automotive::vehicle::StatusCode() const {
+ return value();
+ }
+
+ std::string print() const;
+
+ private:
+ aidl::android::hardware::automotive::vehicle::StatusCode mCode;
+};
+
+template <class T>
+aidl::android::hardware::automotive::vehicle::StatusCode getErrorCode(
+ const android::base::Result<T, VhalError>& result) {
+ if (result.ok()) {
+ return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
+ }
+ return result.error().code();
+}
+
+template <class T>
+int getIntErrorCode(const android::base::Result<T, VhalError>& result) {
+ return toInt(getErrorCode(result));
+}
+
+template <class T, class E>
+std::string getErrorMsg(const android::base::Result<T, E>& result) {
+ if (result.ok()) {
+ return "";
+ }
+ return result.error().message();
+}
+
+template <class T, class E>
+ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T, E>& result,
+ aidl::android::hardware::automotive::vehicle::StatusCode status,
+ const std::string& additionalErrorMsg) {
+ if (result.ok()) {
+ return ndk::ScopedAStatus::ok();
+ }
+ return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
+ toInt(status),
+ fmt::format("{}, error: {}", additionalErrorMsg, getErrorMsg(result)).c_str());
+}
+
+template <class T, class E>
+ndk::ScopedAStatus toScopedAStatus(
+ const android::base::Result<T, E>& result,
+ aidl::android::hardware::automotive::vehicle::StatusCode status) {
+ return toScopedAStatus(result, status, "");
+}
+
+template <class T>
+ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T, VhalError>& result) {
+ return toScopedAStatus(result, getErrorCode(result));
+}
+
+template <class T>
+ndk::ScopedAStatus toScopedAStatus(const android::base::Result<T, VhalError>& result,
+ const std::string& additionalErrorMsg) {
+ return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
+}
+
} // namespace vehicle
} // namespace automotive
} // namespace hardware
diff --git a/automotive/vehicle/aidl/impl/utils/common/src/PendingRequestPool.cpp b/automotive/vehicle/aidl/impl/utils/common/src/PendingRequestPool.cpp
index 23a5403..f8a042d 100644
--- a/automotive/vehicle/aidl/impl/utils/common/src/PendingRequestPool.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/src/PendingRequestPool.cpp
@@ -32,11 +32,12 @@
namespace {
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
-using ::android::base::Error;
using ::android::base::Result;
// At least check every 1s.
-constexpr int64_t CHECK_TIME_IN_NANO = 1000000000;
+constexpr int64_t CHECK_TIME_IN_NANO = 1'000'000'000;
+
+using StatusError = android::base::Error<VhalError>;
} // namespace
@@ -72,9 +73,9 @@
}
}
-Result<void> PendingRequestPool::addRequests(const void* clientId,
- const std::unordered_set<int64_t>& requestIds,
- std::shared_ptr<const TimeoutCallbackFunc> callback) {
+Result<void, VhalError> PendingRequestPool::addRequests(
+ const void* clientId, const std::unordered_set<int64_t>& requestIds,
+ std::shared_ptr<const TimeoutCallbackFunc> callback) {
std::scoped_lock<std::mutex> lockGuard(mLock);
std::list<PendingRequest>* pendingRequests;
size_t pendingRequestCount = 0;
@@ -84,7 +85,7 @@
const auto& pendingRequestIds = pendingRequest.requestIds;
for (int64_t requestId : requestIds) {
if (pendingRequestIds.find(requestId) != pendingRequestIds.end()) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "duplicate request ID: " << requestId;
}
}
@@ -96,7 +97,7 @@
}
if (requestIds.size() > MAX_PENDING_REQUEST_PER_CLIENT - pendingRequestCount) {
- return Error(toInt(StatusCode::TRY_AGAIN)) << "too many pending requests";
+ return StatusError(StatusCode::TRY_AGAIN) << "too many pending requests";
}
int64_t currentTime = elapsedRealtimeNano();
diff --git a/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp b/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
index c1fa896..776caed 100644
--- a/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
@@ -36,10 +36,11 @@
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyStatus;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
-using ::android::base::Error;
using ::android::base::Result;
using ::android::base::StringPrintf;
+using StatusError = android::base::Error<VhalError>;
+
bool VehiclePropertyStore::RecordId::operator==(const VehiclePropertyStore::RecordId& other) const {
return area == other.area && token == other.token;
}
@@ -87,12 +88,12 @@
return recId;
}
-Result<VehiclePropValuePool::RecyclableType> VehiclePropertyStore::readValueLocked(
+Result<VehiclePropValuePool::RecyclableType, VhalError> VehiclePropertyStore::readValueLocked(
const RecordId& recId, const Record& record) const REQUIRES(mLock) {
if (auto it = record.values.find(recId); it != record.values.end()) {
return mValuePool->obtain(*(it->second));
}
- return Error(toInt(StatusCode::NOT_AVAILABLE))
+ return StatusError(StatusCode::NOT_AVAILABLE)
<< "Record ID: " << recId.toString() << " is not found";
}
@@ -106,19 +107,19 @@
};
}
-Result<void> VehiclePropertyStore::writeValue(VehiclePropValuePool::RecyclableType propValue,
- bool updateStatus) {
+Result<void, VhalError> VehiclePropertyStore::writeValue(
+ VehiclePropValuePool::RecyclableType propValue, bool updateStatus) {
std::scoped_lock<std::mutex> g(mLock);
int32_t propId = propValue->prop;
VehiclePropertyStore::Record* record = getRecordLocked(propId);
if (record == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG)) << "property: " << propId << " not registered";
+ return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
}
if (!isGlobalProp(propId) && getAreaConfig(*propValue, record->propConfig) == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "no config for property: " << propId << " area: " << propValue->areaId;
}
@@ -130,7 +131,7 @@
VehiclePropertyStatus oldStatus = valueToUpdate->status;
// propValue is outdated and drops it.
if (oldTimestamp > propValue->timestamp) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "outdated timestamp: " << propValue->timestamp;
}
if (!updateStatus) {
@@ -191,15 +192,15 @@
return allValues;
}
-Result<std::vector<VehiclePropValuePool::RecyclableType>>
-VehiclePropertyStore::readValuesForProperty(int32_t propId) const {
+VehiclePropertyStore::ValuesResultType VehiclePropertyStore::readValuesForProperty(
+ int32_t propId) const {
std::scoped_lock<std::mutex> g(mLock);
std::vector<VehiclePropValuePool::RecyclableType> values;
const VehiclePropertyStore::Record* record = getRecordLocked(propId);
if (record == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG)) << "property: " << propId << " not registered";
+ return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
}
for (auto const& [_, value] : record->values) {
@@ -208,28 +209,28 @@
return values;
}
-Result<VehiclePropValuePool::RecyclableType> VehiclePropertyStore::readValue(
+VehiclePropertyStore::ValueResultType VehiclePropertyStore::readValue(
const VehiclePropValue& propValue) const {
std::scoped_lock<std::mutex> g(mLock);
int32_t propId = propValue.prop;
const VehiclePropertyStore::Record* record = getRecordLocked(propId);
if (record == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG)) << "property: " << propId << " not registered";
+ return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
}
VehiclePropertyStore::RecordId recId = getRecordIdLocked(propValue, *record);
return readValueLocked(recId, *record);
}
-Result<VehiclePropValuePool::RecyclableType> VehiclePropertyStore::readValue(int32_t propId,
- int32_t areaId,
- int64_t token) const {
+VehiclePropertyStore::ValueResultType VehiclePropertyStore::readValue(int32_t propId,
+ int32_t areaId,
+ int64_t token) const {
std::scoped_lock<std::mutex> g(mLock);
const VehiclePropertyStore::Record* record = getRecordLocked(propId);
if (record == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG)) << "property: " << propId << " not registered";
+ return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
}
VehiclePropertyStore::RecordId recId{.area = isGlobalProp(propId) ? 0 : areaId, .token = token};
@@ -247,12 +248,12 @@
return configs;
}
-Result<const VehiclePropConfig*> VehiclePropertyStore::getConfig(int32_t propId) const {
+Result<const VehiclePropConfig*, VhalError> VehiclePropertyStore::getConfig(int32_t propId) const {
std::scoped_lock<std::mutex> g(mLock);
const VehiclePropertyStore::Record* record = getRecordLocked(propId);
if (record == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG)) << "property: " << propId << " not registered";
+ return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
}
return &record->propConfig;
diff --git a/automotive/vehicle/aidl/impl/utils/common/src/VehicleUtils.cpp b/automotive/vehicle/aidl/impl/utils/common/src/VehicleUtils.cpp
index 5abde8d..f85728d 100644
--- a/automotive/vehicle/aidl/impl/utils/common/src/VehicleUtils.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/src/VehicleUtils.cpp
@@ -21,6 +21,7 @@
namespace automotive {
namespace vehicle {
+using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::VehicleAreaConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
@@ -204,6 +205,14 @@
return {};
}
+StatusCode VhalError::value() const {
+ return mCode;
+}
+
+std::string VhalError::print() const {
+ return aidl::android::hardware::automotive::vehicle::toString(mCode);
+}
+
} // namespace vehicle
} // namespace automotive
} // namespace hardware
diff --git a/automotive/vehicle/aidl/impl/utils/common/test/PendingRequestPoolTest.cpp b/automotive/vehicle/aidl/impl/utils/common/test/PendingRequestPoolTest.cpp
index 9c9e4b9..734c739 100644
--- a/automotive/vehicle/aidl/impl/utils/common/test/PendingRequestPoolTest.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/test/PendingRequestPoolTest.cpp
@@ -263,7 +263,7 @@
auto result = getPool()->addRequests(reinterpret_cast<const void*>(0),
{static_cast<int64_t>(10000)}, callback);
ASSERT_FALSE(result.ok()) << "adding more pending requests than limit must fail";
- ASSERT_EQ(result.error().code(), toInt(StatusCode::TRY_AGAIN));
+ ASSERT_EQ(result.error().code(), StatusCode::TRY_AGAIN);
getPool()->tryFinishRequests(reinterpret_cast<const void*>(0), requests);
}
diff --git a/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp b/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
index 1f230e7..c8bf1d8 100644
--- a/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
@@ -103,7 +103,7 @@
}
TEST_F(VehiclePropertyStoreTest, testGetConfig) {
- Result<const VehiclePropConfig*> result =
+ Result<const VehiclePropConfig*, VhalError> result =
mStore->getConfig(toInt(VehicleProperty::INFO_FUEL_CAPACITY));
ASSERT_RESULT_OK(result);
@@ -111,10 +111,10 @@
}
TEST_F(VehiclePropertyStoreTest, testGetConfigWithInvalidPropId) {
- Result<const VehiclePropConfig*> result = mStore->getConfig(INVALID_PROP_ID);
+ Result<const VehiclePropConfig*, VhalError> result = mStore->getConfig(INVALID_PROP_ID);
EXPECT_FALSE(result.ok()) << "expect error when getting a config for an invalid property ID";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::INVALID_ARG));
+ EXPECT_EQ(result.error().code(), StatusCode::INVALID_ARG);
}
std::vector<VehiclePropValue> getTestPropValues() {
@@ -184,7 +184,7 @@
auto result = mStore->readValuesForProperty(INVALID_PROP_ID);
EXPECT_FALSE(result.ok()) << "expect error when reading values for an invalid property";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::INVALID_ARG));
+ EXPECT_EQ(result.error().code(), StatusCode::INVALID_ARG);
}
TEST_F(VehiclePropertyStoreTest, testReadValueOk) {
@@ -224,7 +224,7 @@
auto result = mStore->readValue(toInt(VehicleProperty::TIRE_PRESSURE), WHEEL_REAR_LEFT);
EXPECT_FALSE(result.ok()) << "expect error when reading a value that has not been written";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::NOT_AVAILABLE));
+ EXPECT_EQ(result.error().code(), StatusCode::NOT_AVAILABLE);
}
TEST_F(VehiclePropertyStoreTest, testWriteValueError) {
@@ -235,7 +235,7 @@
auto result = mStore->writeValue(std::move(v));
EXPECT_FALSE(result.ok()) << "expect error when writing value for an invalid property ID";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::INVALID_ARG));
+ EXPECT_EQ(result.error().code(), StatusCode::INVALID_ARG);
}
TEST_F(VehiclePropertyStoreTest, testWriteValueNoAreaConfig) {
@@ -248,7 +248,7 @@
auto result = mStore->writeValue(std::move(v));
EXPECT_FALSE(result.ok()) << "expect error when writing value for an area without config";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::INVALID_ARG));
+ EXPECT_EQ(result.error().code(), StatusCode::INVALID_ARG);
}
TEST_F(VehiclePropertyStoreTest, testWriteOutdatedValue) {
@@ -269,7 +269,7 @@
auto result = mStore->writeValue(std::move(v2));
EXPECT_FALSE(result.ok()) << "expect error when writing an outdated value";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::INVALID_ARG));
+ EXPECT_EQ(result.error().code(), StatusCode::INVALID_ARG);
}
TEST_F(VehiclePropertyStoreTest, testToken) {
@@ -317,7 +317,7 @@
auto result = mStore->readValue(values[0]);
EXPECT_FALSE(result.ok()) << "expect error when reading a removed value";
- EXPECT_EQ(result.error().code(), toInt(StatusCode::NOT_AVAILABLE));
+ EXPECT_EQ(result.error().code(), StatusCode::NOT_AVAILABLE);
auto leftTirePressureResult = mStore->readValue(values[1]);
diff --git a/automotive/vehicle/aidl/impl/utils/common/test/VehicleUtilsTest.cpp b/automotive/vehicle/aidl/impl/utils/common/test/VehicleUtilsTest.cpp
index de8b26d..cc29964 100644
--- a/automotive/vehicle/aidl/impl/utils/common/test/VehicleUtilsTest.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/test/VehicleUtilsTest.cpp
@@ -32,6 +32,7 @@
namespace {
+using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::VehicleArea;
using ::aidl::android::hardware::automotive::vehicle::VehicleAreaConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
@@ -39,6 +40,8 @@
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
+using ::android::base::Error;
+using ::android::base::Result;
struct InvalidPropValueTestCase {
std::string name;
@@ -759,6 +762,12 @@
t.join();
}
+TEST(VehicleUtilsTest, testVhalError) {
+ Result<void, VhalError> result = Error<VhalError>(StatusCode::INVALID_ARG) << "error message";
+
+ ASSERT_EQ(result.error().message(), "error message: INVALID_ARG");
+}
+
class InvalidPropValueTest : public testing::TestWithParam<InvalidPropValueTestCase> {};
INSTANTIATE_TEST_SUITE_P(InvalidPropValueTests, InvalidPropValueTest,
diff --git a/automotive/vehicle/aidl/impl/vhal/include/ConnectedClient.h b/automotive/vehicle/aidl/impl/vhal/include/ConnectedClient.h
index 5d88f7c..6ab0e1e 100644
--- a/automotive/vehicle/aidl/impl/vhal/include/ConnectedClient.h
+++ b/automotive/vehicle/aidl/impl/vhal/include/ConnectedClient.h
@@ -21,6 +21,7 @@
#include <IVehicleHardware.h>
#include <VehicleHalTypes.h>
+#include <VehicleUtils.h>
#include <aidl/android/hardware/automotive/vehicle/IVehicleCallback.h>
#include <android-base/result.h>
@@ -57,7 +58,8 @@
// Returns {@code INVALID_ARG} error if any of the requestIds are duplicate with one of the
// pending request IDs or {@code TRY_AGAIN} error if the pending request pool is full and could
// no longer add requests.
- android::base::Result<void> addRequests(const std::unordered_set<int64_t>& requestIds);
+ android::base::Result<void, VhalError> addRequests(
+ const std::unordered_set<int64_t>& requestIds);
// Marks the requests as finished. Returns a list of request IDs that was pending and has been
// finished. It must be a set of the requested request IDs.
diff --git a/automotive/vehicle/aidl/impl/vhal/include/DefaultVehicleHal.h b/automotive/vehicle/aidl/impl/vhal/include/DefaultVehicleHal.h
index 9735ed3..84b6742 100644
--- a/automotive/vehicle/aidl/impl/vhal/include/DefaultVehicleHal.h
+++ b/automotive/vehicle/aidl/impl/vhal/include/DefaultVehicleHal.h
@@ -43,6 +43,7 @@
public:
using CallbackType =
std::shared_ptr<aidl::android::hardware::automotive::vehicle::IVehicleCallback>;
+ using StatusError = android::base::Error<VhalError>;
explicit DefaultVehicleHal(std::unique_ptr<IVehicleHardware> hardware);
@@ -189,14 +190,14 @@
const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>&
requests);
- android::base::Result<void> checkSubscribeOptions(
+ android::base::Result<void, VhalError> checkSubscribeOptions(
const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions>&
options);
- android::base::Result<void> checkReadPermission(
+ android::base::Result<void, VhalError> checkReadPermission(
const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;
- android::base::Result<void> checkWritePermission(
+ android::base::Result<void, VhalError> checkWritePermission(
const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;
android::base::Result<const aidl::android::hardware::automotive::vehicle::VehiclePropConfig*>
diff --git a/automotive/vehicle/aidl/impl/vhal/src/ConnectedClient.cpp b/automotive/vehicle/aidl/impl/vhal/src/ConnectedClient.cpp
index 830b936..5c5f27e 100644
--- a/automotive/vehicle/aidl/impl/vhal/src/ConnectedClient.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/src/ConnectedClient.cpp
@@ -194,7 +194,8 @@
return reinterpret_cast<const void*>(this);
}
-Result<void> ConnectedClient::addRequests(const std::unordered_set<int64_t>& requestIds) {
+Result<void, VhalError> ConnectedClient::addRequests(
+ const std::unordered_set<int64_t>& requestIds) {
return mRequestPool->addRequests(id(), requestIds, getTimeoutCallback());
}
diff --git a/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp b/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
index 52f3631..b50571b 100644
--- a/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/src/DefaultVehicleHal.cpp
@@ -115,7 +115,7 @@
auto result = LargeParcelableBase::parcelableToStableLargeParcelable(vehiclePropConfigs);
if (!result.ok()) {
ALOGE("failed to convert configs to shared memory file, error: %s, code: %d",
- getErrorMsg(result).c_str(), getIntErrorCode(result));
+ result.error().message().c_str(), static_cast<int>(result.error().code()));
return;
}
@@ -475,7 +475,7 @@
if (auto addRequestResult = client->addRequests(hardwareRequestIds); !addRequestResult.ok()) {
ALOGE("setValues[%s], failed to add pending requests, error: %s",
toString(hardwareRequestIds).c_str(), getErrorMsg(addRequestResult).c_str());
- return toScopedAStatus(addRequestResult, StatusCode::INVALID_ARG);
+ return toScopedAStatus(addRequestResult);
}
if (!failedResults.empty()) {
@@ -545,25 +545,25 @@
return vectorToStableLargeParcelable(std::move(configs), output);
}
-Result<void> DefaultVehicleHal::checkSubscribeOptions(
+Result<void, VhalError> DefaultVehicleHal::checkSubscribeOptions(
const std::vector<SubscribeOptions>& options) {
for (const auto& option : options) {
int32_t propId = option.propId;
if (mConfigsByPropId.find(propId) == mConfigsByPropId.end()) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< StringPrintf("no config for property, ID: %" PRId32, propId);
}
const VehiclePropConfig& config = mConfigsByPropId[propId];
if (config.changeMode != VehiclePropertyChangeMode::ON_CHANGE &&
config.changeMode != VehiclePropertyChangeMode::CONTINUOUS) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "only support subscribing to ON_CHANGE or CONTINUOUS property";
}
if (config.access != VehiclePropertyAccess::READ &&
config.access != VehiclePropertyAccess::READ_WRITE) {
- return Error(toInt(StatusCode::ACCESS_DENIED))
+ return StatusError(StatusCode::ACCESS_DENIED)
<< StringPrintf("Property %" PRId32 " has no read access", propId);
}
@@ -572,12 +572,12 @@
float minSampleRate = config.minSampleRate;
float maxSampleRate = config.maxSampleRate;
if (sampleRate < minSampleRate || sampleRate > maxSampleRate) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< StringPrintf("sample rate: %f out of range, must be within %f and %f",
sampleRate, minSampleRate, maxSampleRate);
}
if (!SubscriptionManager::checkSampleRate(sampleRate)) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< "invalid sample rate: " << sampleRate;
}
}
@@ -589,7 +589,7 @@
// Non-global property.
for (int32_t areaId : option.areaIds) {
if (auto areaConfig = getAreaConfig(propId, areaId, config); areaConfig == nullptr) {
- return Error(toInt(StatusCode::INVALID_ARG))
+ return StatusError(StatusCode::INVALID_ARG)
<< StringPrintf("invalid area ID: %" PRId32 " for prop ID: %" PRId32
", not listed in config",
areaId, propId);
@@ -662,33 +662,35 @@
return mVehicleHardware.get();
}
-Result<void> DefaultVehicleHal::checkWritePermission(const VehiclePropValue& value) const {
+Result<void, VhalError> DefaultVehicleHal::checkWritePermission(
+ const VehiclePropValue& value) const {
int32_t propId = value.prop;
auto result = getConfig(propId);
if (!result.ok()) {
- return Error(toInt(StatusCode::INVALID_ARG)) << getErrorMsg(result);
+ return StatusError(StatusCode::INVALID_ARG) << getErrorMsg(result);
}
const VehiclePropConfig* config = result.value();
if (config->access != VehiclePropertyAccess::WRITE &&
config->access != VehiclePropertyAccess::READ_WRITE) {
- return Error(toInt(StatusCode::ACCESS_DENIED))
+ return StatusError(StatusCode::ACCESS_DENIED)
<< StringPrintf("Property %" PRId32 " has no write access", propId);
}
return {};
}
-Result<void> DefaultVehicleHal::checkReadPermission(const VehiclePropValue& value) const {
+Result<void, VhalError> DefaultVehicleHal::checkReadPermission(
+ const VehiclePropValue& value) const {
int32_t propId = value.prop;
auto result = getConfig(propId);
if (!result.ok()) {
- return Error(toInt(StatusCode::INVALID_ARG)) << getErrorMsg(result);
+ return StatusError(StatusCode::INVALID_ARG) << getErrorMsg(result);
}
const VehiclePropConfig* config = result.value();
if (config->access != VehiclePropertyAccess::READ &&
config->access != VehiclePropertyAccess::READ_WRITE) {
- return Error(toInt(StatusCode::ACCESS_DENIED))
+ return StatusError(StatusCode::ACCESS_DENIED)
<< StringPrintf("Property %" PRId32 " has no read access", propId);
}
return {};