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

Commit 945c430d authored by Hao Chen's avatar Hao Chen
Browse files

Use PropStore on the Server Side to Simulate ECU

Test: build and unit tests

Bug: 188005769

Change-Id: I5aff41acbc4795def84ceb8e363019ff9668120c
parent 5a7cd4c9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -134,6 +134,7 @@ cc_library_static {
    srcs: [
        "common/src/Obd2SensorStore.cpp",
        "common/src/VehicleObjectPool.cpp",
        "common/src/VehiclePropertyStore.cpp",
        "common/src/VehicleUtils.cpp",
    ],
}
+2 −1
Original line number Diff line number Diff line
@@ -19,10 +19,11 @@

#include <cstdint>
#include <unordered_map>
#include <map>
#include <memory>
#include <mutex>

#include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
#include <android/hardware/automotive/vehicle/2.0/types.h>

namespace android {
namespace hardware {
+55 −8
Original line number Diff line number Diff line
@@ -30,6 +30,58 @@

namespace android::hardware::automotive::vehicle::V2_0::impl {

static bool isDiagnosticProperty(VehiclePropConfig propConfig) {
    switch (propConfig.prop) {
        case OBD2_LIVE_FRAME:
        case OBD2_FREEZE_FRAME:
        case OBD2_FREEZE_FRAME_CLEAR:
        case OBD2_FREEZE_FRAME_INFO:
            return true;
    }
    return false;
}

VehicleHalServer::VehicleHalServer() {
    constexpr bool shouldUpdateStatus = true;

    for (auto& it : kVehicleProperties) {
        VehiclePropConfig cfg = it.config;

        mServerSidePropStore.registerProperty(cfg);

        if (isDiagnosticProperty(cfg)) {
            continue;
        }

        // A global property will have only a single area
        int32_t numAreas = isGlobalProp(cfg.prop) ? 1 : cfg.areaConfigs.size();

        for (int i = 0; i < numAreas; i++) {
            int32_t curArea = isGlobalProp(cfg.prop) ? 0 : cfg.areaConfigs[i].areaId;

            // Create a separate instance for each individual zone
            VehiclePropValue prop = {
                    .areaId = curArea,
                    .prop = cfg.prop,
            };

            if (it.initialAreaValues.empty()) {
                prop.value = it.initialValue;
            } else if (auto valueForAreaIt = it.initialAreaValues.find(curArea);
                       valueForAreaIt != it.initialAreaValues.end()) {
                prop.value = valueForAreaIt->second;
            } else {
                LOG(WARNING) << __func__ << " failed to get default value for"
                             << " prop 0x" << std::hex << cfg.prop << " area 0x" << std::hex
                             << curArea;
                prop.status = VehiclePropertyStatus::UNAVAILABLE;
            }

            mServerSidePropStore.writeValue(prop, shouldUpdateStatus);
        }
    }
}

GeneratorHub* VehicleHalServer::getGenerator() {
    return &mGeneratorHub;
}
@@ -55,19 +107,13 @@ void VehicleHalServer::onFakeValueGenerated(const VehiclePropValue& value) {
    if (updatedPropValue) {
        updatedPropValue->timestamp = value.timestamp;
        updatedPropValue->status = VehiclePropertyStatus::AVAILABLE;
        mServerSidePropStore.writeValue(*updatedPropValue, updateStatus);
        onPropertyValueFromCar(*updatedPropValue, updateStatus);
    }
}

std::vector<VehiclePropConfig> VehicleHalServer::onGetAllPropertyConfig() const {
    std::vector<VehiclePropConfig> vehiclePropConfigs;
    constexpr size_t numOfVehiclePropConfigs =
            sizeof(kVehicleProperties) / sizeof(kVehicleProperties[0]);
    vehiclePropConfigs.reserve(numOfVehiclePropConfigs);
    for (auto& it : kVehicleProperties) {
        vehiclePropConfigs.emplace_back(it.config);
    }
    return vehiclePropConfigs;
    return mServerSidePropStore.getAllConfigs();
}

StatusCode VehicleHalServer::handleGenerateFakeDataRequest(const VehiclePropValue& request) {
@@ -278,6 +324,7 @@ StatusCode VehicleHalServer::onSetProperty(const VehiclePropValue& value, bool u
    auto updatedPropValue = getValuePool()->obtain(value);
    updatedPropValue->timestamp = elapsedRealtimeNano();

    mServerSidePropStore.writeValue(*updatedPropValue, updateStatus);
    onPropertyValueFromCar(*updatedPropValue, updateStatus);
    return StatusCode::OK;
}
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#pragma once

#include <vhal_v2_0/VehicleObjectPool.h>
#include <vhal_v2_0/VehiclePropertyStore.h>
#include <vhal_v2_0/VehicleServer.h>

#include "GeneratorHub.h"
@@ -28,6 +29,8 @@ namespace android::hardware::automotive::vehicle::V2_0::impl {
// scenario, the server may be run on a different OS than Android.
class VehicleHalServer : public IVehicleServer {
  public:
    VehicleHalServer();

    // Methods from IVehicleServer

    std::vector<VehiclePropConfig> onGetAllPropertyConfig() const override;
@@ -58,6 +61,7 @@ class VehicleHalServer : public IVehicleServer {
            std::bind(&VehicleHalServer::onFakeValueGenerated, this, std::placeholders::_1)};

    VehiclePropValuePool* mValuePool{nullptr};
    VehiclePropertyStore mServerSidePropStore;
};

}  // namespace android::hardware::automotive::vehicle::V2_0::impl