From 52d6e032af7399608a48ffa8a63207a2d27f3167 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Mon, 10 Feb 2020 17:42:53 -0800 Subject: [PATCH] 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: I90fd2bc4191f8076475e034c7feb07b2664e63c8 --- automotive/vehicle/2.0/default/Android.bp | 70 ------ .../2.0/default/VirtualizationGrpcServer.cpp | 15 -- .../2.0/default/VirtualizedVehicleService.cpp | 56 ----- ....vehicle@2.0-virtualization-grpc-server.rc | 10 - ...tive.vehicle@2.0-virtualization-service.rc | 4 - .../virtualization/GrpcVehicleClient.cpp | 162 ------------- .../virtualization/GrpcVehicleClient.h | 40 --- .../virtualization/GrpcVehicleServer.cpp | 229 ------------------ .../virtualization/GrpcVehicleServer.h | 49 ---- .../impl/vhal_v2_0/virtualization/Utils.cpp | 115 --------- .../impl/vhal_v2_0/virtualization/Utils.h | 49 ---- 11 files changed, 799 deletions(-) delete mode 100644 automotive/vehicle/2.0/default/VirtualizationGrpcServer.cpp delete mode 100644 automotive/vehicle/2.0/default/VirtualizedVehicleService.cpp delete mode 100644 automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc delete mode 100644 automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-service.rc delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.cpp delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.h delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.cpp delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.h delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.cpp delete mode 100644 automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.h diff --git a/automotive/vehicle/2.0/default/Android.bp b/automotive/vehicle/2.0/default/Android.bp index e325889517..8e579012cd 100644 --- a/automotive/vehicle/2.0/default/Android.bp +++ b/automotive/vehicle/2.0/default/Android.bp @@ -85,20 +85,6 @@ cc_library_static { ], } -// VHal virtualization utils -cc_library_static { - name: "android.hardware.automotive.vehicle@2.0-virtualization-utils", - vendor: true, - defaults: ["vhal_v2_0_defaults"], - srcs: [ - "impl/vhal_v2_0/virtualization/Utils.cpp", - ], - export_include_dirs: ["impl"], - shared_libs: [ - "libbase", - ], -} - cc_test { name: "android.hardware.automotive.vehicle@2.0-manager-unit-tests", vendor: true, @@ -153,59 +139,3 @@ cc_binary { "libqemu_pipe", ], } - -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: [ - "impl/vhal_v2_0/virtualization/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: [ - "impl/vhal_v2_0/virtualization/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/automotive/vehicle/2.0/default/VirtualizationGrpcServer.cpp b/automotive/vehicle/2.0/default/VirtualizationGrpcServer.cpp deleted file mode 100644 index fb02c58952..0000000000 --- a/automotive/vehicle/2.0/default/VirtualizationGrpcServer.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#include "vhal_v2_0/virtualization/GrpcVehicleServer.h" -#include "vhal_v2_0/virtualization/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/automotive/vehicle/2.0/default/VirtualizedVehicleService.cpp b/automotive/vehicle/2.0/default/VirtualizedVehicleService.cpp deleted file mode 100644 index 68813c9035..0000000000 --- a/automotive/vehicle/2.0/default/VirtualizedVehicleService.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 -#include - -#include -#include -#include -#include -#include - -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(); - auto connector = impl::makeGrpcVehicleClient(serverInfo->toUri()); - auto hal = std::make_unique(store.get(), connector.get()); - auto emulator = std::make_unique(hal.get()); - auto service = std::make_unique(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/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc b/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc deleted file mode 100644 index 1101b087eb..0000000000 --- a/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-grpc-server.rc +++ /dev/null @@ -1,10 +0,0 @@ -# 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/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-service.rc b/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-service.rc deleted file mode 100644 index 234de591cc..0000000000 --- a/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-virtualization-service.rc +++ /dev/null @@ -1,4 +0,0 @@ -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 diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.cpp deleted file mode 100644 index e329c5be01..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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 -#include - -#include -#include - -#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 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 mGrpcStub; - std::thread mPollingThread; - - std::mutex mShutdownMutex; - std::condition_variable mShutdownCV; - std::atomic mShuttingDownFlag{false}; -}; - -std::unique_ptr makeGrpcVehicleClient(const std::string& addr) { - return std::make_unique(addr); -} - -std::vector GrpcVehicleClientImpl::getAllPropertyConfig() const { - std::vector 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(vhal_status.status_code()); -} - -void GrpcVehicleClientImpl::StartValuePollingThread() { - mPollingThread = std::thread([this]() { - while (!mShuttingDownFlag.load()) { - ::grpc::ClientContext context; - - std::atomic rpc_ok{true}; - std::thread shuttingdown_watcher([this, &rpc_ok, &context]() { - std::unique_lock 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/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.h deleted file mode 100644 index 14eae7f057..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleClient.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 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/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.cpp deleted file mode 100644 index e30b3bec54..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* - * 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 -#include -#include - -#include -#include - -#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* 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* 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; - - 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 mIsAlive{true}; - - static std::atomic CONNECTION_ID_COUNTER; - }; - - std::string mServiceAddr; - VehiclePropValuePool mValueObjectPool; - mutable std::shared_mutex mConnectionMutex; - mutable std::shared_mutex mWriterMutex; - std::list mValueStreamingConnections; -}; - -std::atomic 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(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* 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(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(set_status)); - - return ::grpc::Status::OK; -} - -::grpc::Status GrpcVehicleServerImpl::StartPropertyValuesStream( - ::grpc::ServerContext* context, const ::google::protobuf::Empty* request, - ::grpc::ServerWriter* stream) { - std::mutex terminateMutex; - std::condition_variable terminateCV; - std::unique_lock 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 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/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.h deleted file mode 100644 index 32f4eb203a..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/GrpcVehicleServer.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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; - -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/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.cpp deleted file mode 100644 index 184d8a47f7..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 - -#include -#include -#include -#include - -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 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 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(value); -}; - -std::optional VsockServerInfo::fromCommandLine(int argc, char* argv[]) { - std::optional cid; - std::optional 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::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/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.h deleted file mode 100644 index 8a8bce7c7a..0000000000 --- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/virtualization/Utils.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 -#include - -#include - -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 fromCommandLine(int argc, char* argv[]); - static std::optional 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_