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