From d5608a2d5aac05915b589df46372ba892471935f Mon Sep 17 00:00:00 2001 From: Calvin Huang Date: Mon, 16 Mar 2020 18:52:27 -0700 Subject: [PATCH] Override VHAL property init value with json Bug: 150978133 Test: Manual Change-Id: Iaa45adad3712ca3cc325d52433048b3208109402 --- .../impl/vhal_v2_0/EmulatedVehicleHal.cpp | 35 +++++++++++++++++++ .../impl/vhal_v2_0/EmulatedVehicleHal.h | 3 ++ .../impl/vhal_v2_0/JsonFakeValueGenerator.cpp | 24 +++++++++++++ .../impl/vhal_v2_0/JsonFakeValueGenerator.h | 3 ++ 4 files changed, 65 insertions(+) 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 84354c15f8..bdc52448e7 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 @@ -16,8 +16,12 @@ #define LOG_TAG "DefaultVehicleHal_v2_0" #include +#include #include +#include #include +#include +#include #include "EmulatedVehicleHal.h" #include "JsonFakeValueGenerator.h" @@ -101,6 +105,30 @@ EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore, VehicleH mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue, this, std::placeholders::_1, std::placeholders::_2)); + + mInitVhalValueOverride = + android::base::GetBoolProperty("persist.vendor.vhal_init_value_override", false); + if (mInitVhalValueOverride) { + getAllPropertiesOverride(); + } +} + +void EmulatedVehicleHal::getAllPropertiesOverride() { + if (auto dir = opendir("/vendor/etc/vhaloverride/")) { + std::regex reg_json(".*[.]json", std::regex::icase); + while (auto f = readdir(dir)) { + if (!regex_match(f->d_name, reg_json)) { + continue; + } + std::string file = "/vendor/etc/vhaloverride/" + std::string(f->d_name); + JsonFakeValueGenerator tmpGenerator(file); + + std::vector propvalues = tmpGenerator.getAllEvents(); + mVehiclePropertiesOverride.insert(std::end(mVehiclePropertiesOverride), + std::begin(propvalues), std::end(propvalues)); + } + closedir(dir); + } } VehicleHal::VehiclePropValuePtr EmulatedVehicleHal::get( @@ -277,6 +305,13 @@ void EmulatedVehicleHal::onCreate() { } } else { prop.value = it.initialValue; + if (mInitVhalValueOverride) { + for (auto& itOverride : mVehiclePropertiesOverride) { + if (itOverride.prop == cfg.prop) { + prop.value = itOverride.value; + } + } + } } mPropStore->writeValue(prop, shouldUpdateStatus); } 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 dc05145cd7..cba4b8ae11 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 @@ -62,6 +62,7 @@ public: // Methods from EmulatedVehicleHalIface bool setPropertyFromVehicle(const VehiclePropValue& propValue) override; std::vector getAllProperties() const override; + void getAllPropertiesOverride(); private: constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const { @@ -87,6 +88,8 @@ private: RecurrentTimer mRecurrentTimer; VehicleHalClient* mVehicleClient; bool mInEmulator; + bool mInitVhalValueOverride; + std::vector mVehiclePropertiesOverride; }; } // impl diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.cpp index 8677f837f5..890eb33643 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.cpp +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.cpp @@ -48,6 +48,22 @@ JsonFakeValueGenerator::JsonFakeValueGenerator(const VehiclePropValue& request) mNumOfIterations = v.int32Values.size() < 2 ? -1 : v.int32Values[1]; } +JsonFakeValueGenerator::JsonFakeValueGenerator(std::string path) { + std::ifstream ifs(path); + if (!ifs) { + ALOGE("%s: couldn't open %s for parsing.", __func__, path.c_str()); + } + mGenCfg = { + .index = 0, + .events = parseFakeValueJson(ifs), + }; + mNumOfIterations = mGenCfg.events.size(); +} + +std::vector JsonFakeValueGenerator::getAllEvents() { + return mGenCfg.events; +} + VehiclePropValue JsonFakeValueGenerator::nextEvent() { VehiclePropValue generatedValue; if (!hasNext()) { @@ -109,6 +125,7 @@ std::vector JsonFakeValueGenerator::parseFakeValueJson(std::is Json::Value rawEventValue = rawEvent["value"]; auto& value = event.value; + int32_t count; switch (getPropType(event.prop)) { case VehiclePropertyType::BOOLEAN: case VehiclePropertyType::INT32: @@ -126,6 +143,13 @@ std::vector JsonFakeValueGenerator::parseFakeValueJson(std::is case VehiclePropertyType::STRING: value.stringValue = rawEventValue.asString(); break; + case VehiclePropertyType::INT32_VEC: + value.int32Values.resize(rawEventValue.size()); + count = 0; + for (auto& it : rawEventValue) { + value.int32Values[count++] = it.asInt(); + } + break; case VehiclePropertyType::MIXED: copyMixedValueJson(value, rawEventValue); if (isDiagnosticProperty(event.prop)) { diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.h index 70575f77bf..dc8ff6680c 100644 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.h +++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/JsonFakeValueGenerator.h @@ -41,9 +41,12 @@ private: public: JsonFakeValueGenerator(const VehiclePropValue& request); + JsonFakeValueGenerator(std::string path); + ~JsonFakeValueGenerator() = default; VehiclePropValue nextEvent(); + std::vector getAllEvents(); bool hasNext();