blob: 5ffb531c7a23f908bc5c32cbddbf7b50f5c8574a [file] [log] [blame]
Hao Chen32d46702023-04-10 15:59:50 -07001/*
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
35namespace android::hardware::automotive::vehicle::virtualization {
36
37namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle;
38
39// Connect other GRPC vehicle hardware(s) to the underlying vehicle hardware.
40class GrpcVehicleProxyServer : public proto::VehicleServer::Service {
41 public:
42 GrpcVehicleProxyServer(std::string serverAddr, std::unique_ptr<IVehicleHardware>&& hardware);
43
Yu Shanb02b7722024-06-18 17:38:11 -070044 GrpcVehicleProxyServer(std::vector<std::string> serverAddrs,
45 std::unique_ptr<IVehicleHardware>&& hardware);
46
Hao Chen32d46702023-04-10 15:59:50 -070047 ::grpc::Status GetAllPropertyConfig(
48 ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
49 ::grpc::ServerWriter<proto::VehiclePropConfig>* stream) override;
50
51 ::grpc::Status SetValues(::grpc::ServerContext* context,
52 const proto::VehiclePropValueRequests* requests,
53 proto::SetValueResults* results) override;
54
55 ::grpc::Status GetValues(::grpc::ServerContext* context,
56 const proto::VehiclePropValueRequests* requests,
57 proto::GetValueResults* results) override;
58
Hao Chena810fb22023-04-11 15:27:44 -070059 ::grpc::Status UpdateSampleRate(::grpc::ServerContext* context,
60 const proto::UpdateSampleRateRequest* request,
61 proto::VehicleHalCallStatus* status) override;
62
Yu Shan5c846f72024-05-16 15:39:51 -070063 ::grpc::Status Subscribe(::grpc::ServerContext* context, const proto::SubscribeRequest* request,
64 proto::VehicleHalCallStatus* status) override;
65
Yu Shan2c37c112024-05-20 17:27:19 -070066 ::grpc::Status Unsubscribe(::grpc::ServerContext* context,
67 const proto::UnsubscribeRequest* request,
68 proto::VehicleHalCallStatus* status) override;
69
Hao Chena810fb22023-04-11 15:27:44 -070070 ::grpc::Status CheckHealth(::grpc::ServerContext* context, const ::google::protobuf::Empty*,
71 proto::VehicleHalCallStatus* status) override;
72
73 ::grpc::Status Dump(::grpc::ServerContext* context, const proto::DumpOptions* options,
74 proto::DumpResult* result) override;
75
Hao Chen32d46702023-04-10 15:59:50 -070076 ::grpc::Status StartPropertyValuesStream(
77 ::grpc::ServerContext* context, const ::google::protobuf::Empty* request,
78 ::grpc::ServerWriter<proto::VehiclePropValues>* stream) override;
79
80 GrpcVehicleProxyServer& Start();
81
82 GrpcVehicleProxyServer& Shutdown();
83
84 void Wait();
85
86 private:
87 void OnVehiclePropChange(const std::vector<aidlvhal::VehiclePropValue>& values);
88
89 // We keep long-lasting connection for streaming the prop values.
90 struct ConnectionDescriptor {
91 explicit ConnectionDescriptor(::grpc::ServerWriter<proto::VehiclePropValues>* stream)
92 : mStream(stream),
93 mConnectionID(connection_id_counter_.fetch_add(1) + 1),
94 mMtx(std::make_unique<std::mutex>()),
95 mCV(std::make_unique<std::condition_variable>()) {}
96
97 ConnectionDescriptor(const ConnectionDescriptor&) = delete;
98 ConnectionDescriptor(ConnectionDescriptor&& cd) = default;
99 ConnectionDescriptor& operator=(const ConnectionDescriptor&) = delete;
100 ConnectionDescriptor& operator=(ConnectionDescriptor&& cd) = default;
101
102 ~ConnectionDescriptor();
103
104 uint64_t ID() const { return mConnectionID; }
105
106 bool Write(const proto::VehiclePropValues& values);
107
108 void Wait();
109
110 void Shutdown();
111
112 private:
113 ::grpc::ServerWriter<proto::VehiclePropValues>* mStream;
114 uint64_t mConnectionID{0};
115 std::unique_ptr<std::mutex> mMtx;
116 std::unique_ptr<std::condition_variable> mCV;
117 bool mShutdownFlag{false};
118
119 static std::atomic<uint64_t> connection_id_counter_;
120 };
121
Yu Shanb02b7722024-06-18 17:38:11 -0700122 std::vector<std::string> mServiceAddrs;
Hao Chen32d46702023-04-10 15:59:50 -0700123 std::unique_ptr<::grpc::Server> mServer{nullptr};
124 std::unique_ptr<IVehicleHardware> mHardware;
125
126 std::shared_mutex mConnectionMutex;
127 std::vector<std::shared_ptr<ConnectionDescriptor>> mValueStreamingConnections;
128
129 static constexpr auto kHardwareOpTimeout = std::chrono::seconds(1);
130};
131
132} // namespace android::hardware::automotive::vehicle::virtualization