Merge "Vehicle HAL client reads the config from the server instead of DefaultConfigs.h" into sc-dev
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.cpp
index ed3f4a2..eae58d0 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.cpp
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.cpp
@@ -21,7 +21,6 @@
 #include <android-base/logging.h>
 #include <utils/SystemClock.h>
 
-#include "DefaultConfig.h"
 #include "EmulatedVehicleConnector.h"
 #include "JsonFakeValueGenerator.h"
 #include "LinearFakeValueGenerator.h"
@@ -39,6 +38,10 @@
     return &mEmulatedUserHal;
 }
 
+void EmulatedVehicleConnector::triggerSendAllValues() {
+    sendAllValuesToClient();
+}
+
 StatusCode EmulatedVehicleConnector::onSetProperty(const VehiclePropValue& value,
                                                    bool updateStatus) {
     if (mEmulatedUserHal.isSupported(value.prop)) {
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.h
index 4c6c661..31ac7d8 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.h
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleConnector.h
@@ -33,11 +33,13 @@
 
 class EmulatedVehicleConnector : public IPassThroughConnector<VehicleHalClient, VehicleHalServer> {
   public:
-    EmulatedVehicleConnector() {}
+    EmulatedVehicleConnector() = default;
 
     EmulatedUserHal* getEmulatedUserHal();
 
     // Methods from VehicleHalServer
+    void triggerSendAllValues() override;
+
     StatusCode onSetProperty(const VehiclePropValue& value, bool updateStatus) override;
 
     bool onDump(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
index 1608e52..e8b79dc 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
@@ -105,9 +105,6 @@
       mVehicleClient(client),
       mEmulatedUserHal(emulatedUserHal) {
     initStaticConfig();
-    for (size_t i = 0; i < arraysize(kVehicleProperties); i++) {
-        mPropStore->registerProperty(kVehicleProperties[i].config);
-    }
     mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue,
                                                             this, std::placeholders::_1,
                                                             std::placeholders::_2));
@@ -180,7 +177,13 @@
                 v = getValuePool()->obtain(*internalPropValue);
             }
 
-            *outStatus = v != nullptr ? StatusCode::OK : StatusCode::INVALID_ARG;
+            if (!v) {
+                *outStatus = StatusCode::INVALID_ARG;
+            } else if (v->status == VehiclePropertyStatus::AVAILABLE) {
+                *outStatus = StatusCode::OK;
+            } else {
+                *outStatus = StatusCode::TRY_AGAIN;
+            }
             break;
     }
     if (v.get()) {
@@ -280,57 +283,41 @@
 void EmulatedVehicleHal::onCreate() {
     static constexpr bool shouldUpdateStatus = true;
 
-    for (auto& it : kVehicleProperties) {
-        VehiclePropConfig cfg = it.config;
-        int32_t numAreas = cfg.areaConfigs.size();
+    auto configs = mVehicleClient->getAllPropertyConfig();
 
+    for (const auto& cfg : configs) {
         if (isDiagnosticProperty(cfg)) {
             // do not write an initial empty value for the diagnostic properties
             // as we will initialize those separately.
             continue;
         }
 
-        // A global property will have only a single area
-        if (isGlobalProp(cfg.prop)) {
-            numAreas = 1;
-        }
+        int32_t numAreas = isGlobalProp(cfg.prop) ? 0 : cfg.areaConfigs.size();
 
         for (int i = 0; i < numAreas; i++) {
-            int32_t curArea;
-
-            if (isGlobalProp(cfg.prop)) {
-                curArea = 0;
-            } else {
-                curArea = cfg.areaConfigs[i].areaId;
-            }
+            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,
+                    .status = VehiclePropertyStatus::UNAVAILABLE,
             };
 
-            if (it.initialAreaValues.size() > 0) {
-                auto valueForAreaIt = it.initialAreaValues.find(curArea);
-                if (valueForAreaIt != it.initialAreaValues.end()) {
-                    prop.value = valueForAreaIt->second;
-                } else {
-                    ALOGW("%s failed to get default value for prop 0x%x area 0x%x",
-                            __func__, cfg.prop, curArea);
-                }
-            } else {
-                prop.value = it.initialValue;
-                if (mInitVhalValueOverride) {
-                    for (auto& itOverride : mVehiclePropertiesOverride) {
-                        if (itOverride.prop == cfg.prop) {
-                            prop.value = itOverride.value;
-                        }
+            if (mInitVhalValueOverride) {
+                for (auto& itOverride : mVehiclePropertiesOverride) {
+                    if (itOverride.prop == cfg.prop) {
+                        prop.status = VehiclePropertyStatus::AVAILABLE;
+                        prop.value = itOverride.value;
                     }
                 }
             }
             mPropStore->writeValue(prop, shouldUpdateStatus);
         }
     }
+
+    mVehicleClient->triggerSendAllValues();
+
     initObd2LiveFrame(*mPropStore->getConfigOrDie(OBD2_LIVE_FRAME));
     initObd2FreezeFrame(*mPropStore->getConfigOrDie(OBD2_FREEZE_FRAME));
     mInEmulator = isInEmulator();
@@ -414,8 +401,8 @@
 }
 
 void EmulatedVehicleHal::initStaticConfig() {
-    for (auto&& it = std::begin(kVehicleProperties); it != std::end(kVehicleProperties); ++it) {
-        const auto& cfg = it->config;
+    auto configs = mVehicleClient->getAllPropertyConfig();
+    for (auto&& cfg : configs) {
         VehiclePropertyStore::TokenFunction tokenFunction = nullptr;
 
         switch (cfg.prop) {
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h
index 5c67641..7871c7b 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.h
@@ -29,10 +29,10 @@
 #include <vhal_v2_0/VehicleHal.h>
 #include "vhal_v2_0/VehiclePropertyStore.h"
 
-#include "DefaultConfig.h"
 #include "EmulatedUserHal.h"
 #include "EmulatedVehicleConnector.h"
 #include "GeneratorHub.h"
+#include "PropertyUtils.h"
 #include "VehicleEmulator.h"
 
 namespace android {
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalClient.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalClient.h
index 6559e2a..81dfca1 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalClient.h
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleHalClient.h
@@ -27,6 +27,10 @@
     // Type of callback function for handling the new property values
     using PropertyCallBackType = std::function<void(const VehiclePropValue&, bool updateStatus)>;
 
+    // The server will call sendAllValuesToClient, onPropertyValue will be called when values are
+    // received.
+    virtual void triggerSendAllValues() = 0;
+
     // Method from IVehicleClient
     void onPropertyValue(const VehiclePropValue& value, bool updateStatus) override;
 
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 1e46897..57dd7d4 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
@@ -82,6 +82,14 @@
     }
 }
 
+void VehicleHalServer::sendAllValuesToClient() {
+    constexpr bool update_status = true;
+    auto values = mServerSidePropStore.readAllValues();
+    for (const auto& value : values) {
+        onPropertyValueFromCar(value, update_status);
+    }
+}
+
 GeneratorHub* VehicleHalServer::getGenerator() {
     return &mGeneratorHub;
 }
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 2ad75e3..be88cd9 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
@@ -31,6 +31,8 @@
   public:
     VehicleHalServer();
 
+    void sendAllValuesToClient();
+
     // Methods from IVehicleServer
 
     std::vector<VehiclePropConfig> onGetAllPropertyConfig() const override;