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 | #include "GRPCVehicleProxyServer.h" |
| 18 | |
| 19 | #include "ProtoMessageConverter.h" |
| 20 | |
| 21 | #include <grpc++/grpc++.h> |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <condition_variable> |
| 27 | #include <mutex> |
| 28 | #include <unordered_set> |
| 29 | #include <utility> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace android::hardware::automotive::vehicle::virtualization { |
| 33 | |
| 34 | std::atomic<uint64_t> GrpcVehicleProxyServer::ConnectionDescriptor::connection_id_counter_{0}; |
| 35 | |
| 36 | static std::shared_ptr<::grpc::ServerCredentials> getServerCredentials() { |
| 37 | // TODO(chenhaosjtuacm): get secured credentials here |
| 38 | return ::grpc::InsecureServerCredentials(); |
| 39 | } |
| 40 | |
| 41 | GrpcVehicleProxyServer::GrpcVehicleProxyServer(std::string serverAddr, |
| 42 | std::unique_ptr<IVehicleHardware>&& hardware) |
| 43 | : mServiceAddr(std::move(serverAddr)), mHardware(std::move(hardware)) { |
| 44 | mHardware->registerOnPropertyChangeEvent( |
| 45 | std::make_unique<const IVehicleHardware::PropertyChangeCallback>( |
| 46 | [this](std::vector<aidlvhal::VehiclePropValue> values) { |
| 47 | OnVehiclePropChange(values); |
| 48 | })); |
| 49 | } |
| 50 | |
| 51 | ::grpc::Status GrpcVehicleProxyServer::GetAllPropertyConfig( |
| 52 | ::grpc::ServerContext* context, const ::google::protobuf::Empty* request, |
| 53 | ::grpc::ServerWriter<proto::VehiclePropConfig>* stream) { |
| 54 | for (const auto& config : mHardware->getAllPropertyConfigs()) { |
| 55 | proto::VehiclePropConfig protoConfig; |
| 56 | proto_msg_converter::aidlToProto(config, &protoConfig); |
| 57 | if (!stream->Write(protoConfig)) { |
| 58 | return ::grpc::Status(::grpc::StatusCode::ABORTED, "Connection lost."); |
| 59 | } |
| 60 | } |
| 61 | return ::grpc::Status::OK; |
| 62 | } |
| 63 | |
| 64 | ::grpc::Status GrpcVehicleProxyServer::SetValues(::grpc::ServerContext* context, |
| 65 | const proto::VehiclePropValueRequests* requests, |
| 66 | proto::SetValueResults* results) { |
| 67 | std::vector<aidlvhal::SetValueRequest> aidlRequests; |
| 68 | for (const auto& protoRequest : requests->requests()) { |
| 69 | auto& aidlRequest = aidlRequests.emplace_back(); |
| 70 | aidlRequest.requestId = protoRequest.request_id(); |
| 71 | proto_msg_converter::protoToAidl(protoRequest.value(), &aidlRequest.value); |
| 72 | } |
| 73 | auto waitMtx = std::make_shared<std::mutex>(); |
| 74 | auto waitCV = std::make_shared<std::condition_variable>(); |
| 75 | auto complete = std::make_shared<bool>(false); |
| 76 | auto tmpResults = std::make_shared<proto::SetValueResults>(); |
| 77 | auto aidlStatus = mHardware->setValues( |
| 78 | std::make_shared<const IVehicleHardware::SetValuesCallback>( |
| 79 | [waitMtx, waitCV, complete, |
| 80 | tmpResults](std::vector<aidlvhal::SetValueResult> setValueResults) { |
| 81 | for (const auto& aidlResult : setValueResults) { |
| 82 | auto& protoResult = *tmpResults->add_results(); |
| 83 | protoResult.set_request_id(aidlResult.requestId); |
| 84 | protoResult.set_status( |
| 85 | static_cast<proto::StatusCode>(aidlResult.status)); |
| 86 | } |
| 87 | { |
| 88 | std::lock_guard lck(*waitMtx); |
| 89 | *complete = true; |
| 90 | } |
| 91 | waitCV->notify_all(); |
| 92 | }), |
| 93 | aidlRequests); |
| 94 | if (aidlStatus != aidlvhal::StatusCode::OK) { |
| 95 | return ::grpc::Status(::grpc::StatusCode::INTERNAL, |
| 96 | "The underlying hardware fails to set values, VHAL status: " + |
| 97 | toString(aidlStatus)); |
| 98 | } |
| 99 | std::unique_lock lck(*waitMtx); |
| 100 | bool success = waitCV->wait_for(lck, kHardwareOpTimeout, [complete] { return *complete; }); |
| 101 | if (!success) { |
| 102 | return ::grpc::Status(::grpc::StatusCode::INTERNAL, |
| 103 | "The underlying hardware set values timeout."); |
| 104 | } |
| 105 | *results = std::move(*tmpResults); |
| 106 | return ::grpc::Status::OK; |
| 107 | } |
| 108 | |
| 109 | ::grpc::Status GrpcVehicleProxyServer::GetValues(::grpc::ServerContext* context, |
| 110 | const proto::VehiclePropValueRequests* requests, |
| 111 | proto::GetValueResults* results) { |
| 112 | std::vector<aidlvhal::GetValueRequest> aidlRequests; |
| 113 | for (const auto& protoRequest : requests->requests()) { |
| 114 | auto& aidlRequest = aidlRequests.emplace_back(); |
| 115 | aidlRequest.requestId = protoRequest.request_id(); |
| 116 | proto_msg_converter::protoToAidl(protoRequest.value(), &aidlRequest.prop); |
| 117 | } |
| 118 | auto waitMtx = std::make_shared<std::mutex>(); |
| 119 | auto waitCV = std::make_shared<std::condition_variable>(); |
| 120 | auto complete = std::make_shared<bool>(false); |
| 121 | auto tmpResults = std::make_shared<proto::GetValueResults>(); |
| 122 | auto aidlStatus = mHardware->getValues( |
| 123 | std::make_shared<const IVehicleHardware::GetValuesCallback>( |
| 124 | [waitMtx, waitCV, complete, |
| 125 | tmpResults](std::vector<aidlvhal::GetValueResult> getValueResults) { |
| 126 | for (const auto& aidlResult : getValueResults) { |
| 127 | auto& protoResult = *tmpResults->add_results(); |
| 128 | protoResult.set_request_id(aidlResult.requestId); |
| 129 | protoResult.set_status( |
| 130 | static_cast<proto::StatusCode>(aidlResult.status)); |
| 131 | if (aidlResult.prop) { |
| 132 | auto* valuePtr = protoResult.mutable_value(); |
| 133 | proto_msg_converter::aidlToProto(*aidlResult.prop, valuePtr); |
| 134 | } |
| 135 | } |
| 136 | { |
| 137 | std::lock_guard lck(*waitMtx); |
| 138 | *complete = true; |
| 139 | } |
| 140 | waitCV->notify_all(); |
| 141 | }), |
| 142 | aidlRequests); |
| 143 | if (aidlStatus != aidlvhal::StatusCode::OK) { |
| 144 | return ::grpc::Status(::grpc::StatusCode::INTERNAL, |
| 145 | "The underlying hardware fails to get values, VHAL status: " + |
| 146 | toString(aidlStatus)); |
| 147 | } |
| 148 | std::unique_lock lck(*waitMtx); |
| 149 | bool success = waitCV->wait_for(lck, kHardwareOpTimeout, [complete] { return *complete; }); |
| 150 | if (!success) { |
| 151 | return ::grpc::Status(::grpc::StatusCode::INTERNAL, |
| 152 | "The underlying hardware get values timeout."); |
| 153 | } |
| 154 | *results = std::move(*tmpResults); |
| 155 | return ::grpc::Status::OK; |
| 156 | } |
| 157 | |
| 158 | ::grpc::Status GrpcVehicleProxyServer::StartPropertyValuesStream( |
| 159 | ::grpc::ServerContext* context, const ::google::protobuf::Empty* request, |
| 160 | ::grpc::ServerWriter<proto::VehiclePropValues>* stream) { |
| 161 | auto conn = std::make_shared<ConnectionDescriptor>(stream); |
| 162 | { |
| 163 | std::lock_guard lck(mConnectionMutex); |
| 164 | mValueStreamingConnections.push_back(conn); |
| 165 | } |
| 166 | conn->Wait(); |
| 167 | LOG(ERROR) << __func__ << ": Stream lost, ID : " << conn->ID(); |
| 168 | return ::grpc::Status(::grpc::StatusCode::ABORTED, "Connection lost."); |
| 169 | } |
| 170 | |
| 171 | void GrpcVehicleProxyServer::OnVehiclePropChange( |
| 172 | const std::vector<aidlvhal::VehiclePropValue>& values) { |
| 173 | std::unordered_set<uint64_t> brokenConn; |
| 174 | proto::VehiclePropValues protoValues; |
| 175 | for (const auto& value : values) { |
| 176 | auto* protoValuePtr = protoValues.add_values(); |
| 177 | proto_msg_converter::aidlToProto(value, protoValuePtr); |
| 178 | } |
| 179 | { |
| 180 | std::shared_lock read_lock(mConnectionMutex); |
| 181 | for (auto& connection : mValueStreamingConnections) { |
| 182 | auto writeOK = connection->Write(protoValues); |
| 183 | if (!writeOK) { |
| 184 | LOG(ERROR) << __func__ |
| 185 | << ": Server Write failed, connection lost. ID: " << connection->ID(); |
| 186 | brokenConn.insert(connection->ID()); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | if (brokenConn.empty()) { |
| 191 | return; |
| 192 | } |
| 193 | std::unique_lock write_lock(mConnectionMutex); |
| 194 | mValueStreamingConnections.erase( |
| 195 | std::remove_if(mValueStreamingConnections.begin(), mValueStreamingConnections.end(), |
| 196 | [&brokenConn](const auto& conn) { |
| 197 | return brokenConn.find(conn->ID()) != brokenConn.end(); |
| 198 | }), |
| 199 | mValueStreamingConnections.end()); |
| 200 | } |
| 201 | |
| 202 | GrpcVehicleProxyServer& GrpcVehicleProxyServer::Start() { |
| 203 | if (mServer) { |
| 204 | LOG(WARNING) << __func__ << ": GrpcVehicleProxyServer has already started."; |
| 205 | return *this; |
| 206 | } |
| 207 | ::grpc::ServerBuilder builder; |
| 208 | builder.RegisterService(this); |
| 209 | builder.AddListeningPort(mServiceAddr, getServerCredentials()); |
| 210 | mServer = builder.BuildAndStart(); |
| 211 | CHECK(mServer) << __func__ << ": failed to create the GRPC server, " |
| 212 | << "please make sure the configuration and permissions are correct"; |
| 213 | return *this; |
| 214 | } |
| 215 | |
| 216 | GrpcVehicleProxyServer& GrpcVehicleProxyServer::Shutdown() { |
| 217 | std::shared_lock read_lock(mConnectionMutex); |
| 218 | for (auto& conn : mValueStreamingConnections) { |
| 219 | conn->Shutdown(); |
| 220 | } |
| 221 | if (mServer) { |
| 222 | mServer->Shutdown(); |
| 223 | } |
| 224 | return *this; |
| 225 | } |
| 226 | |
| 227 | void GrpcVehicleProxyServer::Wait() { |
| 228 | if (mServer) { |
| 229 | mServer->Wait(); |
| 230 | } |
| 231 | mServer.reset(); |
| 232 | } |
| 233 | |
| 234 | GrpcVehicleProxyServer::ConnectionDescriptor::~ConnectionDescriptor() { |
| 235 | Shutdown(); |
| 236 | } |
| 237 | |
| 238 | bool GrpcVehicleProxyServer::ConnectionDescriptor::Write(const proto::VehiclePropValues& values) { |
| 239 | if (!mStream) { |
| 240 | LOG(ERROR) << __func__ << ": Empty stream. ID: " << ID(); |
| 241 | Shutdown(); |
| 242 | return false; |
| 243 | } |
| 244 | { |
| 245 | std::lock_guard lck(*mMtx); |
| 246 | if (!mShutdownFlag && mStream->Write(values)) { |
| 247 | return true; |
| 248 | } else { |
| 249 | LOG(ERROR) << __func__ << ": Server Write failed, connection lost. ID: " << ID(); |
| 250 | } |
| 251 | } |
| 252 | Shutdown(); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | void GrpcVehicleProxyServer::ConnectionDescriptor::Wait() { |
| 257 | std::unique_lock lck(*mMtx); |
| 258 | mCV->wait(lck, [this] { return mShutdownFlag; }); |
| 259 | } |
| 260 | |
| 261 | void GrpcVehicleProxyServer::ConnectionDescriptor::Shutdown() { |
| 262 | { |
| 263 | std::lock_guard lck(*mMtx); |
| 264 | mShutdownFlag = true; |
| 265 | } |
| 266 | mCV->notify_all(); |
| 267 | } |
| 268 | |
| 269 | } // namespace android::hardware::automotive::vehicle::virtualization |