Set Timestamp By the Server when the client called 'set'

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

Bug: b/141493212

Change-Id: I6686a15d6e9fa483d9b361acfe88001b7497b937
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 89c5a4b..222fe5e 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
@@ -201,6 +201,36 @@
     switch (value.prop) {
         case kGenerateFakeDataControllingProperty:
             return handleGenerateFakeDataRequest(value);
+
+        // set the value from vehcile side, used in end to end test.
+        case kSetIntPropertyFromVehcileForTest: {
+            auto updatedPropValue = createVehiclePropValue(VehiclePropertyType::INT32, 1);
+            updatedPropValue->prop = value.value.int32Values[0];
+            updatedPropValue->value.int32Values[0] = value.value.int32Values[1];
+            updatedPropValue->timestamp = value.value.int64Values[0];
+            updatedPropValue->areaId = value.areaId;
+            onPropertyValueFromCar(*updatedPropValue, updateStatus);
+            return StatusCode::OK;
+        }
+        case kSetFloatPropertyFromVehcileForTest: {
+            auto updatedPropValue = createVehiclePropValue(VehiclePropertyType::FLOAT, 1);
+            updatedPropValue->prop = value.value.int32Values[0];
+            updatedPropValue->value.floatValues[0] = value.value.floatValues[0];
+            updatedPropValue->timestamp = value.value.int64Values[0];
+            updatedPropValue->areaId = value.areaId;
+            onPropertyValueFromCar(*updatedPropValue, updateStatus);
+            return StatusCode::OK;
+        }
+        case kSetBooleanPropertyFromVehcileForTest: {
+            auto updatedPropValue = createVehiclePropValue(VehiclePropertyType::BOOLEAN, 1);
+            updatedPropValue->prop = value.value.int32Values[1];
+            updatedPropValue->value.int32Values[0] = value.value.int32Values[0];
+            updatedPropValue->timestamp = value.value.int64Values[0];
+            updatedPropValue->areaId = value.areaId;
+            onPropertyValueFromCar(*updatedPropValue, updateStatus);
+            return StatusCode::OK;
+        }
+
         case AP_POWER_STATE_REPORT:
             switch (value.value.int32Values[0]) {
                 case toInt(VehicleApPowerStateReport::DEEP_SLEEP_EXIT):
@@ -237,7 +267,10 @@
 
     // In the real vhal, the value will be sent to Car ECU.
     // We just pretend it is done here and send back to HAL
-    onPropertyValueFromCar(value, updateStatus);
+    auto updatedPropValue = getValuePool()->obtain(value);
+    updatedPropValue->timestamp = elapsedRealtimeNano();
+
+    onPropertyValueFromCar(*updatedPropValue, updateStatus);
     return StatusCode::OK;
 }
 
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 2f52873..5c16bf7 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
@@ -134,36 +134,6 @@
 StatusCode EmulatedVehicleHal::set(const VehiclePropValue& propValue) {
     constexpr bool updateStatus = false;
 
-    // set the value from vehcile side, used in end to end test.
-    if (propValue.prop == kSetIntPropertyFromVehcileForTest) {
-        auto mockValue = createVehiclePropValue(VehiclePropertyType::INT32, 1);
-        mockValue->prop = propValue.value.int32Values[0];
-        mockValue->value.int32Values[0] = propValue.value.int32Values[1];
-        mockValue->timestamp = propValue.value.int64Values[0];
-        mockValue->areaId = propValue.areaId;
-        setPropertyFromVehicle(*mockValue);
-        return StatusCode::OK;
-    }
-
-    if (propValue.prop == kSetFloatPropertyFromVehcileForTest) {
-        auto mockValue = createVehiclePropValue(VehiclePropertyType::FLOAT, 1);
-        mockValue->prop = propValue.value.int32Values[0];
-        mockValue->value.floatValues[0] = propValue.value.floatValues[0];
-        mockValue->timestamp = propValue.value.int64Values[0];
-        mockValue->areaId = propValue.areaId;
-        setPropertyFromVehicle(*mockValue);
-        return StatusCode::OK;
-    }
-    if (propValue.prop == kSetBooleanPropertyFromVehcileForTest) {
-        auto mockValue = createVehiclePropValue(VehiclePropertyType::BOOLEAN, 1);
-        mockValue->prop = propValue.value.int32Values[1];
-        mockValue->value.int32Values[0] = propValue.value.int32Values[0];
-        mockValue->timestamp = propValue.value.int64Values[0];
-        mockValue->areaId = propValue.areaId;
-        setPropertyFromVehicle(*mockValue);
-        return StatusCode::OK;
-    }
-
     if (propValue.prop == kGenerateFakeDataControllingProperty) {
         // Send the generator controlling request to the server.
         // 'updateStatus' flag is only for the value sent by setProperty (propValue in this case)
@@ -217,11 +187,9 @@
      * After checking all conditions, such as the property is available, a real vhal will
      * sent the events to Car ECU to take actions.
      */
-    VehiclePropValuePtr updatedPropValue = getValuePool()->obtain(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, updateStatus);
+    auto setValueStatus = mVehicleClient->setProperty(propValue, updateStatus);
     if (setValueStatus != StatusCode::OK) {
         return setValueStatus;
     }
@@ -312,7 +280,6 @@
         }
 
         if (v.get()) {
-            v->timestamp = elapsedRealtimeNano();
             doHalEvent(std::move(v));
         }
     }