Use PropStore on the Server Side to Simulate ECU

Test: build and unit tests

Bug: 188005769

Change-Id: I5aff41acbc4795def84ceb8e363019ff9668120c
diff --git a/automotive/vehicle/2.0/default/Android.bp b/automotive/vehicle/2.0/default/Android.bp
index 21c1a6e..ffa0c13 100644
--- a/automotive/vehicle/2.0/default/Android.bp
+++ b/automotive/vehicle/2.0/default/Android.bp
@@ -134,6 +134,7 @@
     srcs: [
         "common/src/Obd2SensorStore.cpp",
         "common/src/VehicleObjectPool.cpp",
+        "common/src/VehiclePropertyStore.cpp",
         "common/src/VehicleUtils.cpp",
     ],
 }
diff --git a/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehiclePropertyStore.h b/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehiclePropertyStore.h
index 0a243fe..6a02cf3 100644
--- a/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehiclePropertyStore.h
+++ b/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehiclePropertyStore.h
@@ -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 {
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp
index 6b87052..1e46897 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.cpp
@@ -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 @@
     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 @@
     auto updatedPropValue = getValuePool()->obtain(value);
     updatedPropValue->timestamp = elapsedRealtimeNano();
 
+    mServerSidePropStore.writeValue(*updatedPropValue, updateStatus);
     onPropertyValueFromCar(*updatedPropValue, updateStatus);
     return StatusCode::OK;
 }
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h
index 117eadb..2ad75e3 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalServer.h
@@ -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 @@
 // 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 @@
             std::bind(&VehicleHalServer::onFakeValueGenerated, this, std::placeholders::_1)};
 
     VehiclePropValuePool* mValuePool{nullptr};
+    VehiclePropertyStore mServerSidePropStore;
 };
 
 }  // namespace android::hardware::automotive::vehicle::V2_0::impl