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

Commit 454d4ef5 authored by Chao Yan's avatar Chao Yan
Browse files

Generate VHAL property mixed type from JSON

Bug: 76017041
Test: atest VehicleHALTest
Change-Id: Iaf2f01f71e2eee6478de0aadd2fe2bd13a385404
parent 698109c7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -431,7 +431,7 @@ void EmulatedVehicleHal::onFakeValueGenerated(const VehiclePropValue& value) {
        mPropStore->writeValue(*updatedPropValue, shouldUpdateStatus);
        auto changeMode = mPropStore->getConfigOrDie(value.prop)->changeMode;
        if (VehiclePropertyChangeMode::ON_CHANGE == changeMode) {
            doHalEvent(move(updatedPropValue));
            doHalEvent(std::move(updatedPropValue));
        }
    }
}
+63 −2
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#define LOG_TAG "JsonFakeValueGenerator"

#include <fstream>
#include <type_traits>
#include <typeinfo>

#include <log/log.h>
#include <vhal_v2_0/VehicleUtils.h>
@@ -122,9 +124,14 @@ std::vector<VehiclePropValue> JsonFakeValueGenerator::parseFakeValueJson(std::is
            case VehiclePropertyType::STRING:
                value.stringValue = rawEventValue.asString();
                break;
            case VehiclePropertyType::MIXED:
                copyMixedValueJson(value, rawEventValue);
                if (isDiagnosticProperty(event.prop)) {
                    value.bytes = generateDiagnosticBytes(value);
                }
                break;
            default:
                ALOGE("%s: unsupported type for property: 0x%x with value: %s", __func__,
                      event.prop, rawEventValue.asString().c_str());
                ALOGE("%s: unsupported type for property: 0x%x", __func__, event.prop);
                continue;
        }
        fakeVhalEvents.push_back(event);
@@ -132,6 +139,60 @@ std::vector<VehiclePropValue> JsonFakeValueGenerator::parseFakeValueJson(std::is
    return fakeVhalEvents;
}

void JsonFakeValueGenerator::copyMixedValueJson(VehiclePropValue::RawValue& dest,
                                                const Json::Value& jsonValue) {
    copyJsonArray(dest.int32Values, jsonValue["int32Values"]);
    copyJsonArray(dest.int64Values, jsonValue["int64Values"]);
    copyJsonArray(dest.floatValues, jsonValue["floatValues"]);
    dest.stringValue = jsonValue["stringValue"].asString();
}

template <typename T>
void JsonFakeValueGenerator::copyJsonArray(hidl_vec<T>& dest, const Json::Value& jsonArray) {
    dest.resize(jsonArray.size());
    for (Json::Value::ArrayIndex i = 0; i < jsonArray.size(); i++) {
        if (std::is_same<T, int32_t>::value) {
            dest[i] = jsonArray[i].asInt();
        } else if (std::is_same<T, int64_t>::value) {
            dest[i] = jsonArray[i].asInt64();
        } else if (std::is_same<T, float>::value) {
            dest[i] = jsonArray[i].asFloat();
        }
    }
}

bool JsonFakeValueGenerator::isDiagnosticProperty(int32_t prop) {
    return prop == (int32_t)VehicleProperty::OBD2_LIVE_FRAME ||
           prop == (int32_t)VehicleProperty::OBD2_FREEZE_FRAME;
}

hidl_vec<uint8_t> JsonFakeValueGenerator::generateDiagnosticBytes(
    const VehiclePropValue::RawValue& diagnosticValue) {
    size_t byteSize = ((size_t)DiagnosticIntegerSensorIndex::LAST_SYSTEM_INDEX +
                       (size_t)DiagnosticFloatSensorIndex::LAST_SYSTEM_INDEX + 2);
    hidl_vec<uint8_t> bytes(byteSize % 8 == 0 ? byteSize / 8 : byteSize / 8 + 1);

    auto& int32Values = diagnosticValue.int32Values;
    for (size_t i = 0; i < int32Values.size(); i++) {
        if (int32Values[i] != 0) {
            setBit(bytes, i);
        }
    }

    auto& floatValues = diagnosticValue.floatValues;
    for (size_t i = 0; i < floatValues.size(); i++) {
        if (floatValues[i] != 0.0) {
            setBit(bytes, i + (size_t)DiagnosticIntegerSensorIndex::LAST_SYSTEM_INDEX + 1);
        }
    }
    return bytes;
}

void JsonFakeValueGenerator::setBit(hidl_vec<uint8_t>& bytes, size_t idx) {
    uint8_t mask = 1 << (idx % 8);
    bytes[idx / 8] |= mask;
}

}  // namespace impl

}  // namespace V2_0
+8 −0
Original line number Diff line number Diff line
@@ -49,6 +49,14 @@ public:

private:
    std::vector<VehiclePropValue> parseFakeValueJson(std::istream& is);
    void copyMixedValueJson(VehiclePropValue::RawValue& dest, const Json::Value& jsonValue);

    template <typename T>
    void copyJsonArray(hidl_vec<T>& dest, const Json::Value& jsonArray);

    bool isDiagnosticProperty(int32_t prop);
    hidl_vec<uint8_t> generateDiagnosticBytes(const VehiclePropValue::RawValue& diagnosticValue);
    void setBit(hidl_vec<uint8_t>& bytes, size_t idx);

private:
    GeneratorCfg mGenCfg;