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

Commit de6835be authored by Hao Chen's avatar Hao Chen
Browse files

Simplify Connector class APIs and hierachy

Test: Build; unit tests `atest
 packages/services/Car/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/CarPropertyTest.java`

Bug: b/141493212

Change-Id: I6c561d517646760dfff63cb6c6b50c3c5994098a
parent 5f82b989
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -57,10 +57,14 @@ class IVehicleClient {
    virtual std::vector<VehiclePropConfig> getAllPropertyConfig() const = 0;

    // Send the set property request to server
    virtual StatusCode setProperty(const VehiclePropValue& value) = 0;
    // updateStatus indicate if VHal should change the status of the value
    // it should be false except injecting values for e2e tests
    virtual StatusCode setProperty(const VehiclePropValue& value, bool updateStatus) = 0;

    // Receive a new property value from server
    virtual void onPropertyValue(const VehiclePropValue& value) = 0;
    // updateStatus is true if and only if the value is
    // generated by car (ECU/fake generator/injected)
    virtual void onPropertyValue(const VehiclePropValue& value, bool updateStatus) = 0;
};

/**
@@ -84,11 +88,15 @@ class IVehicleServer {

    // Receive the set property request from HAL.
    // Process the setting and return the status code
    virtual StatusCode onSetProperty(const VehiclePropValue& value) = 0;
    // updateStatus indicate if VHal should change the status of the value
    // it should be false except injecting values for e2e tests
    virtual StatusCode onSetProperty(const VehiclePropValue& value, bool updateStatus) = 0;

    // Receive a new property value from car (via direct connection to the car bus or the emulator)
    // and forward the value to HAL
    virtual void onPropertyValueFromCar(const VehiclePropValue& value) = 0;
    // updateStatus is true if and only if the value is
    // generated by car (ECU/fake generator/injected)
    virtual void onPropertyValueFromCar(const VehiclePropValue& value, bool updateStatus) = 0;
};

/**
@@ -118,12 +126,12 @@ class IPassThroughConnector : public VehicleClientType, public VehicleServerType
        return this->onGetAllPropertyConfig();
    }

    StatusCode setProperty(const VehiclePropValue& value) override {
        return this->onSetProperty(value);
    StatusCode setProperty(const VehiclePropValue& value, bool updateStatus) override {
        return this->onSetProperty(value, updateStatus);
    }

    void onPropertyValueFromCar(const VehiclePropValue& value) override {
        return this->onPropertyValue(value);
    void onPropertyValueFromCar(const VehiclePropValue& value, bool updateStatus) override {
        return this->onPropertyValue(value, updateStatus);
    }

    // To be implemented:
+21 −21
Original line number Diff line number Diff line
@@ -30,12 +30,12 @@ namespace V2_0 {

namespace impl {

void EmulatedVehicleClient::onPropertyValue(const VehiclePropValue& value) {
void EmulatedVehicleClient::onPropertyValue(const VehiclePropValue& value, bool updateStatus) {
    if (!mPropCallback) {
        LOG(ERROR) << __func__ << ": PropertyCallBackType is not registered!";
        return;
    }
    return mPropCallback(value);
    return mPropCallback(value, updateStatus);
}

void EmulatedVehicleClient::registerPropertyValueCallback(PropertyCallBackType&& callback) {
@@ -65,12 +65,13 @@ void EmulatedVehicleServer::setValuePool(VehiclePropValuePool* valuePool) {
}

void EmulatedVehicleServer::onFakeValueGenerated(const VehiclePropValue& value) {
    constexpr bool updateStatus = true;
    LOG(DEBUG) << __func__ << ": " << toString(value);
    auto updatedPropValue = getValuePool()->obtain(value);
    if (updatedPropValue) {
        updatedPropValue->timestamp = value.timestamp;
        updatedPropValue->status = VehiclePropertyStatus::AVAILABLE;
        onPropertyValueFromCar(*updatedPropValue);
        onPropertyValueFromCar(*updatedPropValue, updateStatus);
    }
}

@@ -86,6 +87,8 @@ std::vector<VehiclePropConfig> EmulatedVehicleServer::onGetAllPropertyConfig() c
}

StatusCode EmulatedVehicleServer::handleGenerateFakeDataRequest(const VehiclePropValue& request) {
    constexpr bool updateStatus = true;

    LOG(INFO) << __func__;
    const auto& v = request.value;
    if (!v.int32Values.size()) {
@@ -153,9 +156,11 @@ StatusCode EmulatedVehicleServer::handleGenerateFakeDataRequest(const VehiclePro
            int32_t display = request.value.int32Values[3];
            // Send back to HAL
            onPropertyValueFromCar(
                    *createHwInputKeyProp(VehicleHwKeyInputAction::ACTION_DOWN, keyCode, display));
                    *createHwInputKeyProp(VehicleHwKeyInputAction::ACTION_DOWN, keyCode, display),
                    updateStatus);
            onPropertyValueFromCar(
                    *createHwInputKeyProp(VehicleHwKeyInputAction::ACTION_UP, keyCode, display));
                    *createHwInputKeyProp(VehicleHwKeyInputAction::ACTION_UP, keyCode, display),
                    updateStatus);
            break;
        }
        default: {
@@ -191,9 +196,11 @@ VehicleHal::VehiclePropValuePtr EmulatedVehicleServer::createHwInputKeyProp(
    return keyEvent;
}

StatusCode EmulatedVehicleServer::onSetProperty(const VehiclePropValue& value) {
StatusCode EmulatedVehicleServer::onSetProperty(const VehiclePropValue& value, bool updateStatus) {
    // Some properties need to be treated non-trivially
    switch (value.prop) {
        case kGenerateFakeDataControllingProperty:
            return handleGenerateFakeDataRequest(value);
        case AP_POWER_STATE_REPORT:
            switch (value.value.int32Values[0]) {
                case toInt(VehicleApPowerStateReport::DEEP_SLEEP_EXIT):
@@ -201,15 +208,18 @@ StatusCode EmulatedVehicleServer::onSetProperty(const VehiclePropValue& value) {
                case toInt(VehicleApPowerStateReport::WAIT_FOR_VHAL):
                    // CPMS is in WAIT_FOR_VHAL state, simply move to ON
                    // Send back to HAL
                    onPropertyValueFromCar(
                            *createApPowerStateReq(VehicleApPowerStateReq::ON, 0));
                    // ALWAYS update status for generated property value
                    onPropertyValueFromCar(*createApPowerStateReq(VehicleApPowerStateReq::ON, 0),
                                           true /* updateStatus */);
                    break;
                case toInt(VehicleApPowerStateReport::DEEP_SLEEP_ENTRY):
                case toInt(VehicleApPowerStateReport::SHUTDOWN_START):
                    // CPMS is in WAIT_FOR_FINISH state, send the FINISHED command
                    // Send back to HAL
                    // ALWAYS update status for generated property value
                    onPropertyValueFromCar(
                            *createApPowerStateReq(VehicleApPowerStateReq::FINISHED, 0));
                            *createApPowerStateReq(VehicleApPowerStateReq::FINISHED, 0),
                            true /* updateStatus */);
                    break;
                case toInt(VehicleApPowerStateReport::ON):
                case toInt(VehicleApPowerStateReport::SHUTDOWN_POSTPONE):
@@ -226,21 +236,11 @@ StatusCode EmulatedVehicleServer::onSetProperty(const VehiclePropValue& value) {
    }

    // In the real vhal, the value will be sent to Car ECU.
    // We just pretend it is done here.
    // We just pretend it is done here and send back to HAL
    onPropertyValueFromCar(value, updateStatus);
    return StatusCode::OK;
}

StatusCode EmulatedVehicleServer::onSetPropertyFromVehicle(const VehiclePropValue& value) {
    if (value.prop == kGenerateFakeDataControllingProperty) {
        auto status = handleGenerateFakeDataRequest(value);
        return status;
    } else {
        // Send back to HAL
        onPropertyValueFromCar(value);
        return StatusCode::OK;
    }
}

EmulatedPassthroughConnectorPtr makeEmulatedPassthroughConnector() {
    return std::make_unique<EmulatedPassthroughConnector>();
}
+5 −17
Original line number Diff line number Diff line
@@ -35,13 +35,10 @@ namespace impl {
class EmulatedVehicleClient : public IVehicleClient {
  public:
    // Type of callback function for handling the new property values
    using PropertyCallBackType = std::function<void(const VehiclePropValue&)>;
    using PropertyCallBackType = std::function<void(const VehiclePropValue&, bool updateStatus)>;

    // Method from IVehicleClient
    void onPropertyValue(const VehiclePropValue& value) override;

    // Request to change the value on the VEHICLE side (for testing)
    virtual StatusCode setPropertyFromVehicle(const VehiclePropValue& value) = 0;
    void onPropertyValue(const VehiclePropValue& value, bool updateStatus) override;

    void registerPropertyValueCallback(PropertyCallBackType&& callback);

@@ -55,10 +52,7 @@ class EmulatedVehicleServer : public IVehicleServer {

    std::vector<VehiclePropConfig> onGetAllPropertyConfig() const override;

    StatusCode onSetProperty(const VehiclePropValue& value) override;

    // Process the request to change the value on the VEHICLE side (for testing)
    StatusCode onSetPropertyFromVehicle(const VehiclePropValue& value);
    StatusCode onSetProperty(const VehiclePropValue& value, bool updateStatus) override;

    // Set the Property Value Pool used in this server
    void setValuePool(VehiclePropValuePool* valuePool);
@@ -85,16 +79,10 @@ class EmulatedVehicleServer : public IVehicleServer {
    VehiclePropValuePool* mValuePool{nullptr};
};

class EmulatedPassthroughConnector
    : public IPassThroughConnector<EmulatedVehicleClient, EmulatedVehicleServer> {
  public:
    StatusCode setPropertyFromVehicle(const VehiclePropValue& value) override {
        return this->onSetPropertyFromVehicle(value);
    }
};

// Helper functions

using EmulatedPassthroughConnector =
        IPassThroughConnector<EmulatedVehicleClient, EmulatedVehicleServer>;
using EmulatedPassthroughConnectorPtr = std::unique_ptr<EmulatedPassthroughConnector>;

EmulatedPassthroughConnectorPtr makeEmulatedPassthroughConnector();
+16 −17
Original line number Diff line number Diff line
@@ -98,8 +98,9 @@ EmulatedVehicleHal::EmulatedVehicleHal(VehiclePropertyStore* propStore,
    for (size_t i = 0; i < arraysize(kVehicleProperties); i++) {
        mPropStore->registerProperty(kVehicleProperties[i].config);
    }
    mVehicleClient->registerPropertyValueCallback(
        std::bind(&EmulatedVehicleHal::onPropertyValue, this, std::placeholders::_1));
    mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue,
                                                            this, std::placeholders::_1,
                                                            std::placeholders::_2));
}

VehicleHal::VehiclePropValuePtr EmulatedVehicleHal::get(
@@ -131,7 +132,7 @@ VehicleHal::VehiclePropValuePtr EmulatedVehicleHal::get(
}

StatusCode EmulatedVehicleHal::set(const VehiclePropValue& propValue) {
    static constexpr bool shouldUpdateStatus = false;
    constexpr bool updateStatus = false;

    // set the value from vehcile side, used in end to end test.
    if (propValue.prop == kSetIntPropertyFromVehcileForTest) {
@@ -164,8 +165,12 @@ StatusCode EmulatedVehicleHal::set(const VehiclePropValue& propValue) {
    }

    if (propValue.prop == kGenerateFakeDataControllingProperty) {
        // send the generator controlling request to the server
        auto status = mVehicleClient->setPropertyFromVehicle(propValue);
        // Send the generator controlling request to the server.
        // 'updateStatus' flag is only for the value sent by setProperty (propValue in this case)
        // instead of the generated values triggered by it. 'propValue' works as a control signal
        // here, since we never send the control signal back, the value of 'updateStatus' flag
        // does not matter here.
        auto status = mVehicleClient->setProperty(propValue, updateStatus);
        if (status != StatusCode::OK) {
            return status;
        }
@@ -216,18 +221,11 @@ StatusCode EmulatedVehicleHal::set(const VehiclePropValue& propValue) {
    updatedPropValue->timestamp = elapsedRealtimeNano();

    // Send the value to the vehicle server, the server will talk to the (real or emulated) car
    auto setValueStatus = mVehicleClient->setProperty(*updatedPropValue);
    auto setValueStatus = mVehicleClient->setProperty(*updatedPropValue, updateStatus);
    if (setValueStatus != StatusCode::OK) {
        return setValueStatus;
    }

    if (!mPropStore->writeValue(*updatedPropValue, shouldUpdateStatus)) {
        return StatusCode::INTERNAL_ERROR;
    }

    getEmulatorOrDie()->doSetValueFromClient(*updatedPropValue);
    doHalEvent(std::move(updatedPropValue));

    return StatusCode::OK;
}

@@ -347,18 +345,19 @@ bool EmulatedVehicleHal::isContinuousProperty(int32_t propId) const {
}

bool EmulatedVehicleHal::setPropertyFromVehicle(const VehiclePropValue& propValue) {
    return mVehicleClient->setPropertyFromVehicle(propValue) == StatusCode::OK;
    constexpr bool updateStatus = true;
    return mVehicleClient->setProperty(propValue, updateStatus) == StatusCode::OK;
}

std::vector<VehiclePropValue> EmulatedVehicleHal::getAllProperties() const  {
    return mPropStore->readAllValues();
}

void EmulatedVehicleHal::onPropertyValue(const VehiclePropValue& value) {
    static constexpr bool shouldUpdateStatus = true;
void EmulatedVehicleHal::onPropertyValue(const VehiclePropValue& value, bool updateStatus) {
    VehiclePropValuePtr updatedPropValue = getValuePool()->obtain(value);

    if (mPropStore->writeValue(*updatedPropValue, shouldUpdateStatus)) {
    if (mPropStore->writeValue(*updatedPropValue, updateStatus)) {
        getEmulatorOrDie()->doSetValueFromClient(*updatedPropValue);
        doHalEvent(std::move(updatedPropValue));
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ private:
    }

    StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request);
    void onPropertyValue(const VehiclePropValue& value);
    void onPropertyValue(const VehiclePropValue& value, bool updateStatus);

    void onContinuousPropertyTimer(const std::vector<int32_t>& properties);
    bool isContinuousProperty(int32_t propId) const;