Override VHAL property init value with json

Bug: 150978133
Test: Manual
Change-Id: Iaa45adad3712ca3cc325d52433048b3208109402
This commit is contained in:
Calvin Huang 2020-03-16 18:52:27 -07:00
parent a2d2482aa1
commit d5608a2d5a
4 changed files with 65 additions and 0 deletions

View file

@ -16,8 +16,12 @@
#define LOG_TAG "DefaultVehicleHal_v2_0"
#include <android-base/macros.h>
#include <android-base/properties.h>
#include <android/log.h>
#include <dirent.h>
#include <sys/system_properties.h>
#include <fstream>
#include <regex>
#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<VehiclePropValue> 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);
}

View file

@ -62,6 +62,7 @@ public:
// Methods from EmulatedVehicleHalIface
bool setPropertyFromVehicle(const VehiclePropValue& propValue) override;
std::vector<VehiclePropValue> 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<VehiclePropValue> mVehiclePropertiesOverride;
};
} // impl

View file

@ -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<VehiclePropValue> JsonFakeValueGenerator::getAllEvents() {
return mGenCfg.events;
}
VehiclePropValue JsonFakeValueGenerator::nextEvent() {
VehiclePropValue generatedValue;
if (!hasNext()) {
@ -109,6 +125,7 @@ std::vector<VehiclePropValue> 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<VehiclePropValue> 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)) {

View file

@ -41,9 +41,12 @@ private:
public:
JsonFakeValueGenerator(const VehiclePropValue& request);
JsonFakeValueGenerator(std::string path);
~JsonFakeValueGenerator() = default;
VehiclePropValue nextEvent();
std::vector<VehiclePropValue> getAllEvents();
bool hasNext();