Hao Chen | 32d4670 | 2023-04-10 15:59:50 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "IVehicleHardware.h" |
| 20 | |
| 21 | #include "VehicleServer.grpc.pb.h" |
| 22 | #include "VehicleServer.pb.h" |
| 23 | |
| 24 | #include <grpc++/grpc++.h> |
| 25 | |
| 26 | #include <atomic> |
| 27 | #include <chrono> |
| 28 | #include <cstdint> |
| 29 | #include <functional> |
| 30 | #include <memory> |
| 31 | #include <shared_mutex> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace android::hardware::automotive::vehicle::virtualization { |
| 36 | |
| 37 | namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle; |
| 38 | |
| 39 | // Connect other GRPC vehicle hardware(s) to the underlying vehicle hardware. |
| 40 | class GrpcVehicleProxyServer : public proto::VehicleServer::Service { |
| 41 | public: |
| 42 | GrpcVehicleProxyServer(std::string serverAddr, std::unique_ptr<IVehicleHardware>&& hardware); |
| 43 | |
| 44 | ::grpc::Status GetAllPropertyConfig( |
| 45 | ::grpc::ServerContext* context, const ::google::protobuf::Empty* request, |
| 46 | ::grpc::ServerWriter<proto::VehiclePropConfig>* stream) override; |
| 47 | |
| 48 | ::grpc::Status SetValues(::grpc::ServerContext* context, |
| 49 | const proto::VehiclePropValueRequests* requests, |
| 50 | proto::SetValueResults* results) override; |
| 51 | |
| 52 | ::grpc::Status GetValues(::grpc::ServerContext* context, |
| 53 | const proto::VehiclePropValueRequests* requests, |
| 54 | proto::GetValueResults* results) override; |
| 55 | |
| 56 | ::grpc::Status StartPropertyValuesStream( |
| 57 | ::grpc::ServerContext* context, const ::google::protobuf::Empty* request, |
| 58 | ::grpc::ServerWriter<proto::VehiclePropValues>* stream) override; |
| 59 | |
| 60 | GrpcVehicleProxyServer& Start(); |
| 61 | |
| 62 | GrpcVehicleProxyServer& Shutdown(); |
| 63 | |
| 64 | void Wait(); |
| 65 | |
| 66 | private: |
| 67 | void OnVehiclePropChange(const std::vector<aidlvhal::VehiclePropValue>& values); |
| 68 | |
| 69 | // We keep long-lasting connection for streaming the prop values. |
| 70 | struct ConnectionDescriptor { |
| 71 | explicit ConnectionDescriptor(::grpc::ServerWriter<proto::VehiclePropValues>* stream) |
| 72 | : mStream(stream), |
| 73 | mConnectionID(connection_id_counter_.fetch_add(1) + 1), |
| 74 | mMtx(std::make_unique<std::mutex>()), |
| 75 | mCV(std::make_unique<std::condition_variable>()) {} |
| 76 | |
| 77 | ConnectionDescriptor(const ConnectionDescriptor&) = delete; |
| 78 | ConnectionDescriptor(ConnectionDescriptor&& cd) = default; |
| 79 | ConnectionDescriptor& operator=(const ConnectionDescriptor&) = delete; |
| 80 | ConnectionDescriptor& operator=(ConnectionDescriptor&& cd) = default; |
| 81 | |
| 82 | ~ConnectionDescriptor(); |
| 83 | |
| 84 | uint64_t ID() const { return mConnectionID; } |
| 85 | |
| 86 | bool Write(const proto::VehiclePropValues& values); |
| 87 | |
| 88 | void Wait(); |
| 89 | |
| 90 | void Shutdown(); |
| 91 | |
| 92 | private: |
| 93 | ::grpc::ServerWriter<proto::VehiclePropValues>* mStream; |
| 94 | uint64_t mConnectionID{0}; |
| 95 | std::unique_ptr<std::mutex> mMtx; |
| 96 | std::unique_ptr<std::condition_variable> mCV; |
| 97 | bool mShutdownFlag{false}; |
| 98 | |
| 99 | static std::atomic<uint64_t> connection_id_counter_; |
| 100 | }; |
| 101 | |
| 102 | std::string mServiceAddr; |
| 103 | std::unique_ptr<::grpc::Server> mServer{nullptr}; |
| 104 | std::unique_ptr<IVehicleHardware> mHardware; |
| 105 | |
| 106 | std::shared_mutex mConnectionMutex; |
| 107 | std::vector<std::shared_ptr<ConnectionDescriptor>> mValueStreamingConnections; |
| 108 | |
| 109 | static constexpr auto kHardwareOpTimeout = std::chrono::seconds(1); |
| 110 | }; |
| 111 | |
| 112 | } // namespace android::hardware::automotive::vehicle::virtualization |