From 5b46a339d02db834a927c45ac3c4bd88006cdc04 Mon Sep 17 00:00:00 2001 From: felipeal Date: Thu, 9 Jul 2020 15:22:22 -0700 Subject: [PATCH] Fixed EmulatedUserHal merge issues. Some changes were not properly auto-merged, and it's quite impossible to cherry-pick them automatically (using git), so this CL is manually fixing it. Test: adb shell lshal debug android.hardware.automotive.vehic le@2.0::IVehicle/default --user-hal Bug: 150413515 Change-Id: Ibb8bef2c1ae8df32369dff5c5e5de76480b3a66f Merged-In: I5c8b99b8027b5ec10ace323d872cd85f9a0d4177 --- .../default/impl/vhal_v2_0/DefaultConfig.h | 16 ++ .../impl/vhal_v2_0/EmulatedUserHal.cpp | 138 ++++++++++++++++++ .../default/impl/vhal_v2_0/EmulatedUserHal.h | 38 ++++- .../impl/vhal_v2_0/EmulatedVehicleHal.cpp | 28 +++- .../impl/vhal_v2_0/EmulatedVehicleHal.h | 6 +- .../impl/vhal_v2_0/VehicleHalServer.cpp | 5 + .../default/impl/vhal_v2_0/VehicleHalServer.h | 2 + 7 files changed, 228 insertions(+), 5 deletions(-) diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/DefaultConfig.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/DefaultConfig.h index b8a606adab..16c33b9d19 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/DefaultConfig.h +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/DefaultConfig.h @@ -1036,6 +1036,22 @@ const ConfigDeclaration kVehicleProperties[]{ .changeMode = VehiclePropertyChangeMode::ON_CHANGE, }, }, + { + .config = + { + .prop = toInt(VehicleProperty::CREATE_USER), + .access = VehiclePropertyAccess::READ_WRITE, + .changeMode = VehiclePropertyChangeMode::ON_CHANGE, + }, + }, + { + .config = + { + .prop = toInt(VehicleProperty::REMOVE_USER), + .access = VehiclePropertyAccess::READ_WRITE, + .changeMode = VehiclePropertyChangeMode::ON_CHANGE, + }, + }, { .config = { diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp index 6a6b12f485..ea38cb3941 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp @@ -30,11 +30,18 @@ namespace impl { constexpr int INITIAL_USER_INFO = static_cast(VehicleProperty::INITIAL_USER_INFO); constexpr int SWITCH_USER = static_cast(VehicleProperty::SWITCH_USER); +constexpr int CREATE_USER = static_cast(VehicleProperty::CREATE_USER); +constexpr int REMOVE_USER = static_cast(VehicleProperty::REMOVE_USER); +constexpr int USER_IDENTIFICATION_ASSOCIATION = + static_cast(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION); bool EmulatedUserHal::isSupported(int32_t prop) { switch (prop) { case INITIAL_USER_INFO: case SWITCH_USER: + case CREATE_USER: + case REMOVE_USER: + case USER_IDENTIFICATION_ASSOCIATION: return true; default: return false; @@ -50,12 +57,58 @@ android::base::Result> EmulatedUserHal::onSetP return onSetInitialUserInfoResponse(value); case SWITCH_USER: return onSetSwitchUserResponse(value); + case CREATE_USER: + return onSetCreateUserResponse(value); + case REMOVE_USER: + ALOGI("REMOVE_USER is FYI only, nothing to do..."); + return {}; + case USER_IDENTIFICATION_ASSOCIATION: + return onSetUserIdentificationAssociation(value); default: return android::base::Error(static_cast(StatusCode::INVALID_ARG)) << "Unsupported property: " << toString(value); } } +android::base::Result> EmulatedUserHal::onGetProperty( + const VehiclePropValue& value) { + ALOGV("onGetProperty(%s)", toString(value).c_str()); + switch (value.prop) { + case INITIAL_USER_INFO: + case SWITCH_USER: + case CREATE_USER: + case REMOVE_USER: + ALOGE("onGetProperty(): %d is only supported on SET", value.prop); + return android::base::Error(static_cast(StatusCode::INVALID_ARG)) + << "only supported on SET"; + case USER_IDENTIFICATION_ASSOCIATION: + return onGetUserIdentificationAssociation(value); + default: + ALOGE("onGetProperty(): %d is not supported", value.prop); + return android::base::Error(static_cast(StatusCode::INVALID_ARG)) + << "not supported by User HAL"; + } +} + +android::base::Result> +EmulatedUserHal::onGetUserIdentificationAssociation(const VehiclePropValue& value) { + if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) { + ALOGI("get(USER_IDENTIFICATION_ASSOCIATION): returning %s", + toString(*mSetUserIdentificationAssociationResponseFromCmd).c_str()); + auto newValue = std::unique_ptr( + new VehiclePropValue(*mSetUserIdentificationAssociationResponseFromCmd)); + // Must use the same requestId + if (value.value.int32Values.size() > 0) { + newValue->value.int32Values[0] = value.value.int32Values[0]; + } else { + ALOGE("get(USER_IDENTIFICATION_ASSOCIATION): no requestId on %s", + toString(value).c_str()); + } + return newValue; + } + return defaultUserIdentificationAssociation(value); +} + android::base::Result> EmulatedUserHal::onSetInitialUserInfoResponse(const VehiclePropValue& value) { if (value.value.int32Values.size() == 0) { @@ -144,6 +197,79 @@ android::base::Result> EmulatedUserHal::onSetS return updatedValue; } +android::base::Result> EmulatedUserHal::onSetCreateUserResponse( + const VehiclePropValue& value) { + if (value.value.int32Values.size() == 0) { + ALOGE("set(CREATE_USER): no int32values, ignoring it: %s", toString(value).c_str()); + return android::base::Error(static_cast(StatusCode::INVALID_ARG)) + << "no int32values on " << toString(value); + } + + if (value.areaId != 0) { + ALOGD("set(CREATE_USER) called from lshal; storing it: %s", toString(value).c_str()); + mCreateUserResponseFromCmd.reset(new VehiclePropValue(value)); + return {}; + } + ALOGD("set(CREATE_USER) called from Android: %s", toString(value).c_str()); + + int32_t requestId = value.value.int32Values[0]; + if (mCreateUserResponseFromCmd != nullptr) { + ALOGI("replying CREATE_USER with lshal value: %s", + toString(*mCreateUserResponseFromCmd).c_str()); + return sendUserHalResponse(std::move(mCreateUserResponseFromCmd), requestId); + } + + // Returns default response + auto updatedValue = std::unique_ptr(new VehiclePropValue); + updatedValue->prop = CREATE_USER; + updatedValue->timestamp = elapsedRealtimeNano(); + updatedValue->value.int32Values.resize(2); + updatedValue->value.int32Values[0] = requestId; + updatedValue->value.int32Values[1] = (int32_t)CreateUserStatus::SUCCESS; + + ALOGI("no lshal response; replying with SUCCESS: %s", toString(*updatedValue).c_str()); + + return updatedValue; +} + +android::base::Result> +EmulatedUserHal::onSetUserIdentificationAssociation(const VehiclePropValue& value) { + if (value.value.int32Values.size() == 0) { + ALOGE("set(USER_IDENTIFICATION_ASSOCIATION): no int32values, ignoring it: %s", + toString(value).c_str()); + return android::base::Error(static_cast(StatusCode::INVALID_ARG)) + << "no int32values on " << toString(value); + } + + if (value.areaId != 0) { + ALOGD("set(USER_IDENTIFICATION_ASSOCIATION) called from lshal; storing it: %s", + toString(value).c_str()); + mSetUserIdentificationAssociationResponseFromCmd.reset(new VehiclePropValue(value)); + return {}; + } + ALOGD("set(USER_IDENTIFICATION_ASSOCIATION) called from Android: %s", toString(value).c_str()); + + int32_t requestId = value.value.int32Values[0]; + if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) { + ALOGI("replying USER_IDENTIFICATION_ASSOCIATION with lshal value: %s", + toString(*mSetUserIdentificationAssociationResponseFromCmd).c_str()); + // Not moving response so it can be used on GET requests + auto copy = std::unique_ptr( + new VehiclePropValue(*mSetUserIdentificationAssociationResponseFromCmd)); + return sendUserHalResponse(std::move(copy), requestId); + } + + // Returns default response + return defaultUserIdentificationAssociation(value); +} + +android::base::Result> +EmulatedUserHal::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", toString(request).c_str()); + return android::base::Error(static_cast(StatusCode::NOT_AVAILABLE)) << "not set by lshal"; +} + android::base::Result> EmulatedUserHal::sendUserHalResponse( std::unique_ptr response, int32_t requestId) { switch (response->areaId) { @@ -189,6 +315,18 @@ void EmulatedUserHal::dump(int fd, std::string indent) { } else { dprintf(fd, "%sNo SwitchUser response\n", indent.c_str()); } + if (mCreateUserResponseFromCmd != nullptr) { + dprintf(fd, "%sCreateUser response: %s\n", indent.c_str(), + toString(*mCreateUserResponseFromCmd).c_str()); + } else { + dprintf(fd, "%sNo CreateUser response\n", indent.c_str()); + } + if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) { + dprintf(fd, "%sSetUserIdentificationAssociation response: %s\n", indent.c_str(), + toString(*mSetUserIdentificationAssociationResponseFromCmd).c_str()); + } else { + dprintf(fd, "%sNo SetUserIdentificationAssociation response\n", indent.c_str()); + } } } // namespace impl diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h index b25efcb4d7..db2f117e3e 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h @@ -46,13 +46,21 @@ class EmulatedUserHal { bool isSupported(int32_t prop); /** - * Lets the emulator handle the property. + * Lets the emulator set the property. * * @return updated property and StatusCode */ android::base::Result> onSetProperty( const VehiclePropValue& value); + /** + * Gets the property value from the emulator. + * + * @return property value and StatusCode + */ + android::base::Result> onGetProperty( + const VehiclePropValue& value); + /** * Shows the User HAL emulation help. */ @@ -97,11 +105,39 @@ class EmulatedUserHal { android::base::Result> onSetSwitchUserResponse( const VehiclePropValue& value); + /** + * Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage. + */ + android::base::Result> onSetCreateUserResponse( + const VehiclePropValue& value); + + /** + * Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for + * usage. + */ + android::base::Result> onSetUserIdentificationAssociation( + const VehiclePropValue& value); + + /** + * Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for + * usage. + */ + android::base::Result> onGetUserIdentificationAssociation( + const VehiclePropValue& value); + + /** + * Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal. + */ + android::base::Result> defaultUserIdentificationAssociation( + const VehiclePropValue& request); + android::base::Result> sendUserHalResponse( std::unique_ptr response, int32_t requestId); std::unique_ptr mInitialUserResponseFromCmd; std::unique_ptr mSwitchUserResponseFromCmd; + std::unique_ptr mCreateUserResponseFromCmd; + std::unique_ptr mSetUserIdentificationAssociationResponseFromCmd; }; } // namespace impl diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp index a08d8e7516..1d516005a9 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp @@ -87,12 +87,14 @@ static std::unique_ptr fillDefaultObd2Frame(size_t numVendorInt return sensorStore; } -EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client) +EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client, + EmulatedUserHal* emulatedUserHal) : mPropStore(propStore), mHvacPowerProps(std::begin(kHvacPowerProperties), std::end(kHvacPowerProperties)), mRecurrentTimer(std::bind(&EmulatedVehicleHal::onContinuousPropertyTimer, this, std::placeholders::_1)), - mVehicleClient(client) { + mVehicleClient(client), + mEmulatedUserHal(emulatedUserHal) { initStaticConfig(); for (size_t i = 0; i < arraysize(kVehicleProperties); i++) { mPropStore->registerProperty(kVehicleProperties[i].config); @@ -105,6 +107,8 @@ EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleH VehicleHal::VehiclePropValuePtr EmulatedVehicleHal::get( const VehiclePropValue& requestedPropValue, StatusCode* outStatus) { auto propId = requestedPropValue.prop; + ALOGV("get(%d)", propId); + auto& pool = *getValuePool(); VehiclePropValuePtr v = nullptr; @@ -118,6 +122,26 @@ VehicleHal::VehiclePropValuePtr EmulatedVehicleHal::get( *outStatus = fillObd2DtcInfo(v.get()); break; default: + if (mEmulatedUserHal != nullptr && mEmulatedUserHal->isSupported(propId)) { + ALOGI("get(): getting value for prop %d from User HAL", propId); + const auto& ret = mEmulatedUserHal->onGetProperty(requestedPropValue); + if (!ret.ok()) { + ALOGE("get(): User HAL returned error: %s", ret.error().message().c_str()); + *outStatus = StatusCode(ret.error().code()); + } else { + auto value = ret.value().get(); + if (value != nullptr) { + ALOGI("get(): User HAL returned value: %s", toString(*value).c_str()); + v = getValuePool()->obtain(*value); + *outStatus = StatusCode::OK; + } else { + ALOGE("get(): User HAL returned null value"); + *outStatus = StatusCode::INTERNAL_ERROR; + } + } + break; + } + auto internalPropValue = mPropStore->readValueOrNull(requestedPropValue); if (internalPropValue != nullptr) { v = getValuePool()->obtain(*internalPropValue); diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h index ebf19951b3..30f6bfa759 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h @@ -30,6 +30,7 @@ #include "vhal_v2_0/VehiclePropertyStore.h" #include "DefaultConfig.h" +#include "EmulatedUserHal.h" #include "EmulatedVehicleConnector.h" #include "GeneratorHub.h" #include "VehicleEmulator.h" @@ -45,8 +46,8 @@ namespace impl { /** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */ class EmulatedVehicleHal : public EmulatedVehicleHalIface { public: - EmulatedVehicleHal(VehiclePropertyStore* propStore, - VehicleHalClient* client); + EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client, + EmulatedUserHal* emulatedUserHal = nullptr); ~EmulatedVehicleHal() = default; // Methods from VehicleHal @@ -86,6 +87,7 @@ private: std::unordered_set mHvacPowerProps; RecurrentTimer mRecurrentTimer; VehicleHalClient* mVehicleClient; + EmulatedUserHal* mEmulatedUserHal; }; } // impl diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp index ad5096e58f..36f25345ae 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp @@ -41,6 +41,10 @@ VehiclePropValuePool* VehicleHalServer::getValuePool() const { return mValuePool; } +EmulatedUserHal* VehicleHalServer::getEmulatedUserHal() { + return &mEmulatedUserHal; +} + void VehicleHalServer::setValuePool(VehiclePropValuePool* valuePool) { if (!valuePool) { LOG(WARNING) << __func__ << ": Setting value pool to nullptr!"; @@ -197,6 +201,7 @@ StatusCode VehicleHalServer::onSetProperty(const VehiclePropValue& value, bool u } return StatusCode::OK; } + LOG(DEBUG) << "onSetProperty(" << value.prop << ")"; // Some properties need to be treated non-trivially switch (value.prop) { diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h index 2841fbee3c..fca78bc822 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h @@ -38,6 +38,8 @@ class VehicleHalServer : public IVehicleServer { // Set the Property Value Pool used in this server void setValuePool(VehiclePropValuePool* valuePool); + EmulatedUserHal* getEmulatedUserHal(); + private: using VehiclePropValuePtr = recyclable_ptr;