blob: ddd620ea2ea153009ffede5dd7d0ab222fdf7811 [file] [log] [blame]
Hao Chen6cb86892023-04-10 15:21:59 -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#include <VehicleHalTypes.h>
21#include <VehicleUtils.h>
22#include <android-base/result.h>
23
24#include "VehicleServer.grpc.pb.h"
25#include "VehicleServer.pb.h"
26
Krzysztof KosiƄskic6081872023-08-19 02:07:47 +000027#include <grpc++/grpc++.h>
28
Hao Chen6cb86892023-04-10 15:21:59 -070029#include <atomic>
30#include <chrono>
31#include <condition_variable>
32#include <memory>
33#include <shared_mutex>
34#include <string>
35#include <thread>
36#include <vector>
37
38namespace android::hardware::automotive::vehicle::virtualization {
39
40namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle;
41
42class GRPCVehicleHardware : public IVehicleHardware {
43 public:
44 explicit GRPCVehicleHardware(std::string service_addr);
45
46 ~GRPCVehicleHardware();
47
48 // Get all the property configs.
49 std::vector<aidlvhal::VehiclePropConfig> getAllPropertyConfigs() const override;
50
51 // Set property values asynchronously. Server could return before the property set requests
52 // are sent to vehicle bus or before property set confirmation is received. The callback is
53 // safe to be called after the function returns and is safe to be called in a different thread.
54 aidlvhal::StatusCode setValues(std::shared_ptr<const SetValuesCallback> callback,
55 const std::vector<aidlvhal::SetValueRequest>& requests) override;
56
57 // Get property values asynchronously. Server could return before the property values are ready.
58 // The callback is safe to be called after the function returns and is safe to be called in a
59 // different thread.
60 aidlvhal::StatusCode getValues(
61 std::shared_ptr<const GetValuesCallback> callback,
62 const std::vector<aidlvhal::GetValueRequest>& requests) const override;
63
64 // Dump debug information in the server.
65 DumpResult dump(const std::vector<std::string>& options) override;
66
67 // Check whether the system is healthy, return {@code StatusCode::OK} for healthy.
68 aidlvhal::StatusCode checkHealth() override;
69
70 // Register a callback that would be called when there is a property change event from vehicle.
71 void registerOnPropertyChangeEvent(
72 std::unique_ptr<const PropertyChangeCallback> callback) override;
73
74 // Register a callback that would be called when there is a property set error event from
75 // vehicle.
76 void registerOnPropertySetErrorEvent(
77 std::unique_ptr<const PropertySetErrorCallback> callback) override;
78
79 // Update the sample rate for the [propId, areaId] pair.
80 aidlvhal::StatusCode updateSampleRate(int32_t propId, int32_t areaId,
81 float sampleRate) override;
82
83 bool waitForConnected(std::chrono::milliseconds waitTime);
84
Aaqib Ismail514c23c2023-09-19 17:55:01 -070085 protected:
86 std::shared_mutex mCallbackMutex;
87 std::unique_ptr<const PropertyChangeCallback> mOnPropChange;
88
Hao Chen6cb86892023-04-10 15:21:59 -070089 private:
90 void ValuePollingLoop();
91
92 std::string mServiceAddr;
93 std::shared_ptr<::grpc::Channel> mGrpcChannel;
94 std::unique_ptr<proto::VehicleServer::Stub> mGrpcStub;
95 std::thread mValuePollingThread;
96
Hao Chen6cb86892023-04-10 15:21:59 -070097 std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
98
99 std::mutex mShutdownMutex;
100 std::condition_variable mShutdownCV;
101 std::atomic<bool> mShuttingDownFlag{false};
102};
103
104} // namespace android::hardware::automotive::vehicle::virtualization