Move the virtualized vehicle hal code into the trout tree

AAOS virtualization work is happening under the trout tree, and
we should be building reference HAL implementations for the
virtualized deployment under that tree

Bug: 148816426
Test: build
Change-Id: Ie5cf5ef91b7b45424ceb00b071472556c8d1462e
diff --git a/aosp_trout_arm64.mk b/aosp_trout_arm64.mk
index f91e4de..da0d54d 100644
--- a/aosp_trout_arm64.mk
+++ b/aosp_trout_arm64.mk
@@ -15,7 +15,7 @@
 #
 
 # Vehicle HAL
-LOCAL_VHAL_PRODUCT_PACKAGE := android.hardware.automotive.vehicle@2.0-virtualization-service
+LOCAL_VHAL_PRODUCT_PACKAGE := android.hardware.automotive.vehicle@2.0-virtualization-service android.hardware.automotive.vehicle@2.0-virtualization-grpc-server
 PRODUCT_PROPERTY_OVERRIDES += \
 	ro.vendor.vehiclehal.server.cid=3 \
 	ro.vendor.vehiclehal.server.port=9210 \
diff --git a/hal/vehicle/2.0/Android.bp b/hal/vehicle/2.0/Android.bp
new file mode 100644
index 0000000..356d931
--- /dev/null
+++ b/hal/vehicle/2.0/Android.bp
@@ -0,0 +1,81 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//       http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library_static {
+    name: "android.hardware.automotive.vehicle@2.0-virtualization-utils",
+    vendor: true,
+    defaults: ["vhal_v2_0_defaults"],
+    srcs: [
+        "Utils.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+    ],
+}
+
+cc_binary {
+    name: "android.hardware.automotive.vehicle@2.0-virtualization-service",
+    defaults: ["vhal_v2_0_defaults"],
+    init_rc: ["android.hardware.automotive.vehicle@2.0-virtualization-service.rc"],
+    vendor: true,
+    relative_install_path: "hw",
+    srcs: [
+        "GrpcVehicleClient.cpp",
+        "VirtualizedVehicleService.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libjsoncpp",
+        "libprotobuf-cpp-full",
+        "libgrpc++",
+    ],
+    static_libs: [
+        "android.hardware.automotive.vehicle@2.0-manager-lib",
+        "android.hardware.automotive.vehicle@2.0-default-impl-lib",
+        "android.hardware.automotive.vehicle@2.0-grpc",
+        "android.hardware.automotive.vehicle@2.0-virtualization-utils",
+        "libqemu_pipe",
+    ],
+    cflags: [
+        "-Wno-unused-parameter",
+    ],
+}
+
+cc_binary {
+    name: "android.hardware.automotive.vehicle@2.0-virtualization-grpc-server",
+    init_rc: ["android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc"],
+    defaults: ["vhal_v2_0_defaults"],
+    vendor: true,
+    relative_install_path: "hw",
+    srcs: [
+        "GrpcVehicleServer.cpp",
+        "VirtualizationGrpcServer.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+        "libjsoncpp",
+        "libprotobuf-cpp-full",
+        "libgrpc++",
+    ],
+    static_libs: [
+        "android.hardware.automotive.vehicle@2.0-manager-lib",
+        "android.hardware.automotive.vehicle@2.0-default-impl-lib",
+        "android.hardware.automotive.vehicle@2.0-grpc",
+        "android.hardware.automotive.vehicle@2.0-virtualization-utils",
+    ],
+    cflags: [
+        "-Wno-unused-parameter",
+    ],
+}
diff --git a/hal/vehicle/2.0/GrpcVehicleClient.cpp b/hal/vehicle/2.0/GrpcVehicleClient.cpp
new file mode 100644
index 0000000..e329c5b
--- /dev/null
+++ b/hal/vehicle/2.0/GrpcVehicleClient.cpp
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "GrpcVehicleClient.h"
+
+#include <condition_variable>
+#include <mutex>
+
+#include <android-base/logging.h>
+#include <grpc++/grpc++.h>
+
+#include "VehicleServer.grpc.pb.h"
+#include "VehicleServer.pb.h"
+#include "vhal_v2_0/ProtoMessageConverter.h"
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+static std::shared_ptr<::grpc::ChannelCredentials> getChannelCredentials() {
+    // TODO(chenhaosjtuacm): get secured credentials here
+    return ::grpc::InsecureChannelCredentials();
+}
+
+class GrpcVehicleClientImpl : public EmulatedVehicleClient {
+  public:
+    GrpcVehicleClientImpl(const std::string& addr)
+        : mServiceAddr(addr),
+          mGrpcChannel(::grpc::CreateChannel(mServiceAddr, getChannelCredentials())),
+          mGrpcStub(vhal_proto::VehicleServer::NewStub(mGrpcChannel)) {
+        StartValuePollingThread();
+    }
+
+    ~GrpcVehicleClientImpl() {
+        mShuttingDownFlag.store(true);
+        mShutdownCV.notify_all();
+        if (mPollingThread.joinable()) {
+            mPollingThread.join();
+        }
+    }
+
+    // methods from IVehicleClient
+
+    std::vector<VehiclePropConfig> getAllPropertyConfig() const override;
+
+    StatusCode setProperty(const VehiclePropValue& value, bool updateStatus) override;
+
+  private:
+    void StartValuePollingThread();
+
+    // private data members
+
+    std::string mServiceAddr;
+    std::shared_ptr<::grpc::Channel> mGrpcChannel;
+    std::unique_ptr<vhal_proto::VehicleServer::Stub> mGrpcStub;
+    std::thread mPollingThread;
+
+    std::mutex mShutdownMutex;
+    std::condition_variable mShutdownCV;
+    std::atomic<bool> mShuttingDownFlag{false};
+};
+
+std::unique_ptr<EmulatedVehicleClient> makeGrpcVehicleClient(const std::string& addr) {
+    return std::make_unique<GrpcVehicleClientImpl>(addr);
+}
+
+std::vector<VehiclePropConfig> GrpcVehicleClientImpl::getAllPropertyConfig() const {
+    std::vector<VehiclePropConfig> configs;
+    ::grpc::ClientContext context;
+    auto config_stream = mGrpcStub->GetAllPropertyConfig(&context, ::google::protobuf::Empty());
+    vhal_proto::VehiclePropConfig protoConfig;
+    while (config_stream->Read(&protoConfig)) {
+        VehiclePropConfig config;
+        proto_msg_converter::fromProto(&config, protoConfig);
+        configs.emplace_back(std::move(config));
+    }
+    auto grpc_status = config_stream->Finish();
+    if (!grpc_status.ok()) {
+        LOG(ERROR) << __func__
+                   << ": GRPC GetAllPropertyConfig Failed: " << grpc_status.error_message();
+        configs.clear();
+    }
+
+    return configs;
+}
+
+StatusCode GrpcVehicleClientImpl::setProperty(const VehiclePropValue& value, bool updateStatus) {
+    ::grpc::ClientContext context;
+    vhal_proto::WrappedVehiclePropValue wrappedProtoValue;
+    vhal_proto::VehicleHalCallStatus vhal_status;
+    proto_msg_converter::toProto(wrappedProtoValue.mutable_value(), value);
+    wrappedProtoValue.set_update_status(updateStatus);
+
+    auto grpc_status = mGrpcStub->SetProperty(&context, wrappedProtoValue, &vhal_status);
+    if (!grpc_status.ok()) {
+        LOG(ERROR) << __func__ << ": GRPC SetProperty Failed: " << grpc_status.error_message();
+        return StatusCode::INTERNAL_ERROR;
+    }
+
+    return static_cast<StatusCode>(vhal_status.status_code());
+}
+
+void GrpcVehicleClientImpl::StartValuePollingThread() {
+    mPollingThread = std::thread([this]() {
+        while (!mShuttingDownFlag.load()) {
+            ::grpc::ClientContext context;
+
+            std::atomic<bool> rpc_ok{true};
+            std::thread shuttingdown_watcher([this, &rpc_ok, &context]() {
+                std::unique_lock<std::mutex> shutdownLock(mShutdownMutex);
+                mShutdownCV.wait(shutdownLock, [this, &rpc_ok]() {
+                    return !rpc_ok.load() || mShuttingDownFlag.load();
+                });
+                context.TryCancel();
+            });
+
+            auto value_stream =
+                    mGrpcStub->StartPropertyValuesStream(&context, ::google::protobuf::Empty());
+            vhal_proto::WrappedVehiclePropValue wrappedProtoValue;
+            while (!mShuttingDownFlag.load() && value_stream->Read(&wrappedProtoValue)) {
+                VehiclePropValue value;
+                proto_msg_converter::fromProto(&value, wrappedProtoValue.value());
+                onPropertyValue(value, wrappedProtoValue.update_status());
+            }
+
+            rpc_ok.store(false);
+            mShutdownCV.notify_all();
+            shuttingdown_watcher.join();
+
+            auto grpc_status = value_stream->Finish();
+            // never reach here until connection lost
+            LOG(ERROR) << __func__
+                       << ": GRPC Value Streaming Failed: " << grpc_status.error_message();
+
+            // try to reconnect
+        }
+    });
+}
+
+}  // namespace impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
diff --git a/hal/vehicle/2.0/GrpcVehicleClient.h b/hal/vehicle/2.0/GrpcVehicleClient.h
new file mode 100644
index 0000000..14eae7f
--- /dev/null
+++ b/hal/vehicle/2.0/GrpcVehicleClient.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleClient_H_
+#define android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleClient_H_
+
+#include "vhal_v2_0/EmulatedVehicleConnector.h"
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+std::unique_ptr<EmulatedVehicleClient> makeGrpcVehicleClient(const std::string& addr);
+
+}  // namespace impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
+
+#endif  // android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleClient_H_
diff --git a/hal/vehicle/2.0/GrpcVehicleServer.cpp b/hal/vehicle/2.0/GrpcVehicleServer.cpp
new file mode 100644
index 0000000..e30b3be
--- /dev/null
+++ b/hal/vehicle/2.0/GrpcVehicleServer.cpp
@@ -0,0 +1,229 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "GrpcVehicleServer.h"
+
+#include <condition_variable>
+#include <mutex>
+#include <shared_mutex>
+
+#include <android-base/logging.h>
+#include <grpc++/grpc++.h>
+
+#include "VehicleServer.grpc.pb.h"
+#include "VehicleServer.pb.h"
+#include "vhal_v2_0/ProtoMessageConverter.h"
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+class GrpcVehicleServerImpl : public GrpcVehicleServer, public vhal_proto::VehicleServer::Service {
+  public:
+    GrpcVehicleServerImpl(const std::string& addr) : mServiceAddr(addr) {
+        setValuePool(&mValueObjectPool);
+    }
+
+    // method from GrpcVehicleServer
+    void Start() override;
+
+    // method from IVehicleServer
+    void onPropertyValueFromCar(const VehiclePropValue& value, bool updateStatus) override;
+
+    // methods from vhal_proto::VehicleServer::Service
+
+    ::grpc::Status GetAllPropertyConfig(
+            ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
+            ::grpc::ServerWriter<vhal_proto::VehiclePropConfig>* stream) override;
+
+    ::grpc::Status SetProperty(::grpc::ServerContext* context,
+                               const vhal_proto::WrappedVehiclePropValue* wrappedPropValue,
+                               vhal_proto::VehicleHalCallStatus* status) override;
+
+    ::grpc::Status StartPropertyValuesStream(
+            ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
+            ::grpc::ServerWriter<vhal_proto::WrappedVehiclePropValue>* stream) override;
+
+  private:
+    // We keep long-lasting connection for streaming the prop values.
+    // For us, each connection can be represented as a function to send the new value, and
+    // an ID to identify this connection
+    struct ConnectionDescriptor {
+        using ValueWriterType = std::function<bool(const vhal_proto::WrappedVehiclePropValue&)>;
+
+        ConnectionDescriptor(ValueWriterType&& value_writer)
+            : mValueWriter(std::move(value_writer)),
+              mConnectionID(CONNECTION_ID_COUNTER.fetch_add(1)) {}
+
+        ConnectionDescriptor(const ConnectionDescriptor&) = delete;
+
+        ConnectionDescriptor& operator=(const ConnectionDescriptor&) = delete;
+
+        // This move constructor is NOT THREAD-SAFE, which means it cannot be moved
+        // while using. Since the connection descriptors are pretected by mConnectionMutex
+        // then we are fine here
+        ConnectionDescriptor(ConnectionDescriptor&& cd)
+            : mValueWriter(std::move(cd.mValueWriter)),
+              mConnectionID(cd.mConnectionID),
+              mIsAlive(cd.mIsAlive.load()) {
+            cd.mIsAlive.store(false);
+        }
+
+        ValueWriterType mValueWriter;
+        uint64_t mConnectionID;
+        std::atomic<bool> mIsAlive{true};
+
+        static std::atomic<uint64_t> CONNECTION_ID_COUNTER;
+    };
+
+    std::string mServiceAddr;
+    VehiclePropValuePool mValueObjectPool;
+    mutable std::shared_mutex mConnectionMutex;
+    mutable std::shared_mutex mWriterMutex;
+    std::list<ConnectionDescriptor> mValueStreamingConnections;
+};
+
+std::atomic<uint64_t> GrpcVehicleServerImpl::ConnectionDescriptor::CONNECTION_ID_COUNTER = 0;
+
+static std::shared_ptr<::grpc::ServerCredentials> getServerCredentials() {
+    // TODO(chenhaosjtuacm): get secured credentials here
+    return ::grpc::InsecureServerCredentials();
+}
+
+GrpcVehicleServerPtr makeGrpcVehicleServer(const std::string& addr) {
+    return std::make_unique<GrpcVehicleServerImpl>(addr);
+}
+
+void GrpcVehicleServerImpl::Start() {
+    ::grpc::ServerBuilder builder;
+    builder.RegisterService(this);
+    builder.AddListeningPort(mServiceAddr, getServerCredentials());
+    std::unique_ptr<::grpc::Server> server(builder.BuildAndStart());
+
+    server->Wait();
+}
+
+void GrpcVehicleServerImpl::onPropertyValueFromCar(const VehiclePropValue& value,
+                                                   bool updateStatus) {
+    vhal_proto::WrappedVehiclePropValue wrappedPropValue;
+    proto_msg_converter::toProto(wrappedPropValue.mutable_value(), value);
+    wrappedPropValue.set_update_status(updateStatus);
+    std::shared_lock read_lock(mConnectionMutex);
+
+    bool has_terminated_connections = 0;
+
+    for (auto& connection : mValueStreamingConnections) {
+        auto writeOK = connection.mValueWriter(wrappedPropValue);
+        if (!writeOK) {
+            LOG(ERROR) << __func__ << ": Server Write failed, connection lost. ID: "
+                       << connection.mConnectionID;
+            has_terminated_connections = true;
+            connection.mIsAlive.store(false);
+        }
+    }
+
+    if (!has_terminated_connections) {
+        return;
+    }
+
+    read_lock.unlock();
+
+    std::unique_lock write_lock(mConnectionMutex);
+
+    for (auto itr = mValueStreamingConnections.begin(); itr != mValueStreamingConnections.end();) {
+        if (!itr->mIsAlive.load()) {
+            itr = mValueStreamingConnections.erase(itr);
+        } else {
+            ++itr;
+        }
+    }
+}
+
+::grpc::Status GrpcVehicleServerImpl::GetAllPropertyConfig(
+        ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
+        ::grpc::ServerWriter<vhal_proto::VehiclePropConfig>* stream) {
+    auto configs = onGetAllPropertyConfig();
+    for (auto& config : configs) {
+        vhal_proto::VehiclePropConfig protoConfig;
+        proto_msg_converter::toProto(&protoConfig, config);
+        if (!stream->Write(protoConfig)) {
+            return ::grpc::Status(::grpc::StatusCode::ABORTED, "Connection lost.");
+        }
+    }
+
+    return ::grpc::Status::OK;
+}
+
+::grpc::Status GrpcVehicleServerImpl::SetProperty(
+        ::grpc::ServerContext* context, const vhal_proto::WrappedVehiclePropValue* wrappedPropValue,
+        vhal_proto::VehicleHalCallStatus* status) {
+    VehiclePropValue value;
+    proto_msg_converter::fromProto(&value, wrappedPropValue->value());
+
+    auto set_status = static_cast<int32_t>(onSetProperty(value, wrappedPropValue->update_status()));
+    if (!vhal_proto::VehicleHalStatusCode_IsValid(set_status)) {
+        return ::grpc::Status(::grpc::StatusCode::INTERNAL, "Unknown status code");
+    }
+
+    status->set_status_code(static_cast<vhal_proto::VehicleHalStatusCode>(set_status));
+
+    return ::grpc::Status::OK;
+}
+
+::grpc::Status GrpcVehicleServerImpl::StartPropertyValuesStream(
+        ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
+        ::grpc::ServerWriter<vhal_proto::WrappedVehiclePropValue>* stream) {
+    std::mutex terminateMutex;
+    std::condition_variable terminateCV;
+    std::unique_lock<std::mutex> terminateLock(terminateMutex);
+    bool terminated{false};
+
+    auto callBack = [stream, &terminateMutex, &terminateCV, &terminated,
+                     this](const vhal_proto::WrappedVehiclePropValue& value) {
+        std::unique_lock lock(mWriterMutex);
+        if (!stream->Write(value)) {
+            std::unique_lock<std::mutex> terminateLock(terminateMutex);
+            terminated = true;
+            terminateLock.unlock();
+            terminateCV.notify_all();
+            return false;
+        }
+        return true;
+    };
+
+    // Register connection
+    std::unique_lock lock(mConnectionMutex);
+    auto& conn = mValueStreamingConnections.emplace_back(std::move(callBack));
+    lock.unlock();
+
+    // Never stop until connection lost
+    terminateCV.wait(terminateLock, [&terminated]() { return terminated; });
+
+    LOG(ERROR) << __func__ << ": Stream lost, ID : " << conn.mConnectionID;
+
+    return ::grpc::Status(::grpc::StatusCode::ABORTED, "Connection lost.");
+}
+
+}  // namespace impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
diff --git a/hal/vehicle/2.0/GrpcVehicleServer.h b/hal/vehicle/2.0/GrpcVehicleServer.h
new file mode 100644
index 0000000..32f4eb2
--- /dev/null
+++ b/hal/vehicle/2.0/GrpcVehicleServer.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleServer_H_
+#define android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleServer_H_
+
+#include "vhal_v2_0/EmulatedVehicleConnector.h"
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+// Connect to the Vehicle Client via GRPC
+class GrpcVehicleServer : public EmulatedVehicleServer {
+  public:
+    // Start listening incoming calls, should never return if working normally
+    virtual void Start() = 0;
+};
+
+using GrpcVehicleServerPtr = std::unique_ptr<GrpcVehicleServer>;
+
+GrpcVehicleServerPtr makeGrpcVehicleServer(const std::string& addr);
+
+}  // namespace impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
+
+#endif  // android_hardware_automotive_vehicle_V2_0_impl_virtialization_GrpcVehicleServer_H_
diff --git a/hal/vehicle/2.0/Utils.cpp b/hal/vehicle/2.0/Utils.cpp
new file mode 100644
index 0000000..184d8a4
--- /dev/null
+++ b/hal/vehicle/2.0/Utils.cpp
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Utils.h"
+
+#include <cutils/properties.h>
+
+#include <getopt.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sstream>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+namespace impl {
+
+std::string VsockServerInfo::toUri() {
+    std::stringstream uri_stream;
+    uri_stream << "vsock:" << serverCid << ":" << serverPort;
+    return uri_stream.str();
+}
+
+static std::optional<unsigned> parseUnsignedIntFromString(const char* optarg, const char* name) {
+    auto v = strtoul(optarg, nullptr, 0);
+    if (((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT_MAX)) {
+        LOG(WARNING) << name << " value is out of range: " << optarg;
+    } else if (v != 0) {
+        return v;
+    } else {
+        LOG(WARNING) << name << " value is invalid or missing: " << optarg;
+    }
+
+    return std::nullopt;
+}
+
+static std::optional<unsigned> getNumberFromProperty(const char* key) {
+    auto value = property_get_int64(key, -1);
+    if ((value <= 0) || (value > UINT_MAX)) {
+        LOG(WARNING) << key << " is missing or out of bounds";
+        return std::nullopt;
+    }
+
+    return static_cast<unsigned int>(value);
+};
+
+std::optional<VsockServerInfo> VsockServerInfo::fromCommandLine(int argc, char* argv[]) {
+    std::optional<unsigned int> cid;
+    std::optional<unsigned int> port;
+
+    // unique values to identify the options
+    constexpr int OPT_VHAL_SERVER_CID = 1001;
+    constexpr int OPT_VHAL_SERVER_PORT_NUMBER = 1002;
+
+    struct option longOptions[] = {
+            {"server_cid", 1, 0, OPT_VHAL_SERVER_CID},
+            {"server_port", 1, 0, OPT_VHAL_SERVER_PORT_NUMBER},
+            {},
+    };
+
+    int optValue;
+    while ((optValue = getopt_long_only(argc, argv, ":", longOptions, 0)) != -1) {
+        switch (optValue) {
+            case OPT_VHAL_SERVER_CID:
+                cid = parseUnsignedIntFromString(optarg, "cid");
+                break;
+            case OPT_VHAL_SERVER_PORT_NUMBER:
+                port = parseUnsignedIntFromString(optarg, "port");
+                break;
+            default:
+                // ignore other options
+                break;
+        }
+    }
+
+    if (cid && port) {
+        return VsockServerInfo{*cid, *port};
+    }
+    return std::nullopt;
+}
+
+std::optional<VsockServerInfo> VsockServerInfo::fromRoPropertyStore() {
+    constexpr const char* VHAL_SERVER_CID_PROPERTY_KEY = "ro.vendor.vehiclehal.server.cid";
+    constexpr const char* VHAL_SERVER_PORT_PROPERTY_KEY = "ro.vendor.vehiclehal.server.port";
+
+    const auto cid = getNumberFromProperty(VHAL_SERVER_CID_PROPERTY_KEY);
+    const auto port = getNumberFromProperty(VHAL_SERVER_PORT_PROPERTY_KEY);
+
+    if (cid && port) {
+        return VsockServerInfo{*cid, *port};
+    }
+    return std::nullopt;
+}
+
+}  // namespace impl
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
diff --git a/hal/vehicle/2.0/Utils.h b/hal/vehicle/2.0/Utils.h
new file mode 100644
index 0000000..8a8bce7
--- /dev/null
+++ b/hal/vehicle/2.0/Utils.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
+#define android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
+
+#include <optional>
+#include <string>
+
+#include <android-base/logging.h>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace V2_0 {
+namespace impl {
+
+struct VsockServerInfo {
+    unsigned int serverCid{0};
+    unsigned int serverPort{0};
+
+    static std::optional<VsockServerInfo> fromCommandLine(int argc, char* argv[]);
+    static std::optional<VsockServerInfo> fromRoPropertyStore();
+
+    std::string toUri();
+};
+
+}  // namespace impl
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace automotive
+}  // namespace hardware
+}  // namespace android
+
+#endif  // android_hardware_automotive_vehicle_V2_0_impl_virtualization_Utils_H_
diff --git a/hal/vehicle/2.0/VirtualizationGrpcServer.cpp b/hal/vehicle/2.0/VirtualizationGrpcServer.cpp
new file mode 100644
index 0000000..46d3419
--- /dev/null
+++ b/hal/vehicle/2.0/VirtualizationGrpcServer.cpp
@@ -0,0 +1,15 @@
+#include <android-base/logging.h>
+
+#include "GrpcVehicleServer.h"
+#include "Utils.h"
+
+int main(int argc, char* argv[]) {
+    namespace vhal_impl = android::hardware::automotive::vehicle::V2_0::impl;
+
+    auto serverInfo = vhal_impl::VsockServerInfo::fromCommandLine(argc, argv);
+    CHECK(serverInfo.has_value()) << "Invalid server CID/port combination";
+
+    auto server = vhal_impl::makeGrpcVehicleServer(serverInfo->toUri());
+    server->Start();
+    return 0;
+}
diff --git a/hal/vehicle/2.0/VirtualizedVehicleService.cpp b/hal/vehicle/2.0/VirtualizedVehicleService.cpp
new file mode 100644
index 0000000..204f253
--- /dev/null
+++ b/hal/vehicle/2.0/VirtualizedVehicleService.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android-base/logging.h>
+#include <hidl/HidlTransportSupport.h>
+
+#include <vhal_v2_0/EmulatedVehicleConnector.h>
+#include <vhal_v2_0/EmulatedVehicleHal.h>
+#include <vhal_v2_0/VehicleHalManager.h>
+
+#include "Utils.h"
+#include "GrpcVehicleClient.h"
+
+using namespace android;
+using namespace android::hardware;
+using namespace android::hardware::automotive::vehicle::V2_0;
+
+int main(int argc, char* argv[]) {
+    namespace vhal_impl = android::hardware::automotive::vehicle::V2_0::impl;
+
+    auto serverInfo = vhal_impl::VsockServerInfo::fromRoPropertyStore();
+    CHECK(serverInfo.has_value()) << "Invalid server CID/port combination";
+
+    auto store = std::make_unique<VehiclePropertyStore>();
+    auto connector = impl::makeGrpcVehicleClient(serverInfo->toUri());
+    auto hal = std::make_unique<impl::EmulatedVehicleHal>(store.get(), connector.get());
+    auto emulator = std::make_unique<impl::VehicleEmulator>(hal.get());
+    auto service = std::make_unique<VehicleHalManager>(hal.get());
+
+    configureRpcThreadpool(4, true /* callerWillJoin */);
+
+    LOG(INFO) << "Registering as service...";
+    status_t status = service->registerAsService();
+
+    if (status != OK) {
+        LOG(ERROR) << "Unable to register vehicle service (" << status << ")";
+        return 1;
+    }
+
+    LOG(INFO) << "Ready";
+    joinRpcThreadpool();
+
+    return 1;
+}
diff --git a/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc b/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc
new file mode 100644
index 0000000..1101b08
--- /dev/null
+++ b/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc
@@ -0,0 +1,10 @@
+# It is an interim state to run GRPC server as an Android service.
+# Eventually it will run outside of Android (e.g., AGL),
+# so the command line arguments are expected, though not conventionally used in Android
+service vendor.vehicle-hal-2.0-server \
+        /vendor/bin/hw/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server \
+        -server_cid ${ro.vendor.vehiclehal.server.cid:-pleaseconfigurethis} \
+        -server_port ${ro.vendor.vehiclehal.server.port:-pleaseconfigurethis}
+    class hal
+    user vehicle_network
+    group system inet
diff --git a/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-service.rc b/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-service.rc
new file mode 100644
index 0000000..234de59
--- /dev/null
+++ b/hal/vehicle/2.0/android.hardware.automotive.vehicle@2.0-virtualization-service.rc
@@ -0,0 +1,4 @@
+service vendor.vehicle-hal-2.0 /vendor/bin/hw/android.hardware.automotive.vehicle@2.0-virtualization-service
+    class hal
+    user vehicle_network
+    group system inet