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

Commit 0f85dd16 authored by Chao Yan's avatar Chao Yan
Browse files

Added support for multiple VHAL generators

Using priority queue to schedule multiple VHAL event generators

Bug: 76017041
Test: atest VehicleHALTest
Change-Id: I6bc8071cafd12334dfe37d4f3808530836aec4df
parent 25ae1956
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ cc_library_static {
        "impl/vhal_v2_0/SocketComm.cpp",
        "impl/vhal_v2_0/LinearFakeValueGenerator.cpp",
        "impl/vhal_v2_0/JsonFakeValueGenerator.cpp",
        "impl/vhal_v2_0/GeneratorHub.cpp",
    ],
    local_include_dirs: ["common/include/vhal_v2_0"],
    export_include_dirs: ["impl"],
+14 −8
Original line number Diff line number Diff line
@@ -68,33 +68,39 @@ const int32_t kGenerateFakeDataControllingProperty =
enum class FakeDataCommand : int32_t {
    /**
     * Starts linear fake data generation. Caller must provide additional data:
     *     int32Values[1] - VehicleProperty to which command applies
     *     int32Values[1] - vehicle property to which command applies
     *     int64Values[0] - periodic interval in nanoseconds
     *     floatValues[0] - initial value
     *     floatValues[1] - dispersion defines the min/max value relative to initial value, where
     *                      max = initial_value + dispersion, min = initial_value - dispersion.
     *                      Dispersion should be non-negative, otherwise the behavior is undefined.
     *     floatValues[2] - increment, with every timer tick the value will be incremented by this
     *                      amount. When reaching to max value, the current value will be set to min.
     *                      It should be non-negative, otherwise the behavior is undefined.
     *                      amount. When reaching to max value, the current value will be set to
     *                      min. It should be non-negative, otherwise the behavior is undefined.
     */
    StartLinear = 0,

    /** Stops generating of fake data that was triggered by Start commands.
     *     int32Values[1] - VehicleProperty to which command applies. VHAL will stop the
    /** Stops linear fake data generation that was triggered by StartLinear commands.
     *     int32Values[1] - vehicle property to which command applies. VHAL will stop the
     *                      corresponding linear generation for that property.
     */
    StopLinear = 1,

    /**
     * Starts JSON-based fake data generation. Caller must provide a string value specifying
     * the path to fake value JSON file:
     * Starts JSON-based fake data generation. It iterates through JSON-encoded VHAL events from a
     * file and inject them to VHAL. The iteration can be repeated multiple times or infinitely.
     * Caller must provide additional data:
     *     int32Values[1] - number of iterations. If it is not provided or -1. The iteration will be
     *                      repeated infinite times.
     *     stringValue    - path to the fake values JSON file
     */
    StartJson = 2,

    /**
     * Stops JSON-based fake data generation. No additional arguments needed.
     * Stops JSON-based fake data generation. As multiple JSON-based generation can happen at the
     * same time. Caller must provide the path of fake value JSON file to stop the corresponding
     * generation:
     *     stringValue    - path to the fake values JSON file
     */
    StopJson = 3,

+41 −8
Original line number Diff line number Diff line
@@ -92,10 +92,8 @@ EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore)
      mHvacPowerProps(std::begin(kHvacPowerProperties), std::end(kHvacPowerProperties)),
      mRecurrentTimer(
          std::bind(&EmulatedVehicleHal::onContinuousPropertyTimer, this, std::placeholders::_1)),
      mLinearFakeValueGenerator(std::make_unique<LinearFakeValueGenerator>(
          std::bind(&EmulatedVehicleHal::onFakeValueGenerated, this, std::placeholders::_1))),
      mJsonFakeValueGenerator(std::make_unique<JsonFakeValueGenerator>(
          std::bind(&EmulatedVehicleHal::onFakeValueGenerated, this, std::placeholders::_1))) {
      mGeneratorHub(
          std::bind(&EmulatedVehicleHal::onFakeValueGenerated, this, std::placeholders::_1)) {
    initStaticConfig();
    for (size_t i = 0; i < arraysize(kVehicleProperties); i++) {
        mPropStore->registerProperty(kVehicleProperties[i].config);
@@ -343,19 +341,54 @@ StatusCode EmulatedVehicleHal::handleGenerateFakeDataRequest(const VehiclePropVa
    switch (command) {
        case FakeDataCommand::StartLinear: {
            ALOGI("%s, FakeDataCommand::StartLinear", __func__);
            return mLinearFakeValueGenerator->start(request);
            if (v.int32Values.size() < 2) {
                ALOGE("%s: expected property ID in int32Values", __func__);
                return StatusCode::INVALID_ARG;
            }
            if (!v.int64Values.size()) {
                ALOGE("%s: interval is not provided in int64Values", __func__);
                return StatusCode::INVALID_ARG;
            }
            if (v.floatValues.size() < 3) {
                ALOGE("%s: expected at least 3 elements in floatValues, got: %zu", __func__,
                      v.floatValues.size());
                return StatusCode::INVALID_ARG;
            }
            int32_t cookie = v.int32Values[1];
            mGeneratorHub.registerGenerator(cookie,
                                            std::make_unique<LinearFakeValueGenerator>(request));
            break;
        }
        case FakeDataCommand::StartJson: {
            ALOGI("%s, FakeDataCommand::StartJson", __func__);
            return mJsonFakeValueGenerator->start(request);
            if (v.stringValue.empty()) {
                ALOGE("%s: path to JSON file is missing", __func__);
                return StatusCode::INVALID_ARG;
            }
            int32_t cookie = std::hash<std::string>()(v.stringValue);
            mGeneratorHub.registerGenerator(cookie,
                                            std::make_unique<JsonFakeValueGenerator>(request));
            break;
        }
        case FakeDataCommand::StopLinear: {
            ALOGI("%s, FakeDataCommand::StopLinear", __func__);
            return mLinearFakeValueGenerator->stop(request);
            if (v.int32Values.size() < 2) {
                ALOGE("%s: expected property ID in int32Values", __func__);
                return StatusCode::INVALID_ARG;
            }
            int32_t cookie = v.int32Values[1];
            mGeneratorHub.unregisterGenerator(cookie);
            break;
        }
        case FakeDataCommand::StopJson: {
            ALOGI("%s, FakeDataCommand::StopJson", __func__);
            return mJsonFakeValueGenerator->stop(request);
            if (v.stringValue.empty()) {
                ALOGE("%s: path to JSON file is missing", __func__);
                return StatusCode::INVALID_ARG;
            }
            int32_t cookie = std::hash<std::string>()(v.stringValue);
            mGeneratorHub.unregisterGenerator(cookie);
            break;
        }
        case FakeDataCommand::KeyPress: {
            ALOGI("%s, FakeDataCommand::KeyPress", __func__);
+2 −4
Original line number Diff line number Diff line
@@ -30,8 +30,7 @@
#include "vhal_v2_0/VehiclePropertyStore.h"

#include "DefaultConfig.h"
#include "FakeValueGenerator.h"

#include "GeneratorHub.h"
#include "VehicleEmulator.h"

namespace android {
@@ -85,8 +84,7 @@ private:
    VehiclePropertyStore* mPropStore;
    std::unordered_set<int32_t> mHvacPowerProps;
    RecurrentTimer mRecurrentTimer;
    std::unique_ptr<FakeValueGenerator> mLinearFakeValueGenerator;
    std::unique_ptr<FakeValueGenerator> mJsonFakeValueGenerator;
    GeneratorHub mGeneratorHub;
};

}  // impl
+11 −17
Original line number Diff line number Diff line
@@ -27,28 +27,22 @@ namespace V2_0 {

namespace impl {

using OnHalEvent = std::function<void(const VehiclePropValue& event)>;
using MuxGuard = std::lock_guard<std::mutex>;

class FakeValueGenerator {
public:
    virtual ~FakeValueGenerator() = default;
    /**
     * Starts generating VHAL events
     *
     * @param request in VehiclePropValue with required information to start fake data generation
     * @return StatusCode of the start request
     */
    virtual StatusCode start(const VehiclePropValue& request) = 0;
    /**
     * Stops generating VHAL events
     * @param request in VehiclePropValue with required information to stop fake data generation
     * @return StatusCode of the stop request
     */
    virtual StatusCode stop(const VehiclePropValue& request) = 0;

    virtual VehiclePropValue nextEvent() = 0;

    virtual bool hasNext() = 0;
};

}  // impl
using Clock = std::chrono::steady_clock;
using Nanos = std::chrono::nanoseconds;
using TimePoint = std::chrono::time_point<Clock, Nanos>;

using FakeValueGeneratorPtr = std::unique_ptr<FakeValueGenerator>;

}  // namespace impl

}  // namespace V2_0
}  // namespace vehicle
Loading