blob: 9750f621e9f3827f08ee64f944d9956c78f27644 [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
Yu Shan5c846f72024-05-16 15:39:51 -070046 // Only used for unit testing.
47 explicit GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub);
48
Hao Chen6cb86892023-04-10 15:21:59 -070049 ~GRPCVehicleHardware();
50
51 // Get all the property configs.
52 std::vector<aidlvhal::VehiclePropConfig> getAllPropertyConfigs() const override;
53
54 // Set property values asynchronously. Server could return before the property set requests
55 // are sent to vehicle bus or before property set confirmation is received. The callback is
56 // safe to be called after the function returns and is safe to be called in a different thread.
57 aidlvhal::StatusCode setValues(std::shared_ptr<const SetValuesCallback> callback,
58 const std::vector<aidlvhal::SetValueRequest>& requests) override;
59
60 // Get property values asynchronously. Server could return before the property values are ready.
61 // The callback is safe to be called after the function returns and is safe to be called in a
62 // different thread.
63 aidlvhal::StatusCode getValues(
64 std::shared_ptr<const GetValuesCallback> callback,
65 const std::vector<aidlvhal::GetValueRequest>& requests) const override;
66
67 // Dump debug information in the server.
68 DumpResult dump(const std::vector<std::string>& options) override;
69
70 // Check whether the system is healthy, return {@code StatusCode::OK} for healthy.
71 aidlvhal::StatusCode checkHealth() override;
72
73 // Register a callback that would be called when there is a property change event from vehicle.
74 void registerOnPropertyChangeEvent(
75 std::unique_ptr<const PropertyChangeCallback> callback) override;
76
77 // Register a callback that would be called when there is a property set error event from
78 // vehicle.
79 void registerOnPropertySetErrorEvent(
80 std::unique_ptr<const PropertySetErrorCallback> callback) override;
81
82 // Update the sample rate for the [propId, areaId] pair.
83 aidlvhal::StatusCode updateSampleRate(int32_t propId, int32_t areaId,
84 float sampleRate) override;
85
Yu Shan5c846f72024-05-16 15:39:51 -070086 aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override;
87
Yu Shan2c37c112024-05-20 17:27:19 -070088 aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override;
89
Hao Chen6cb86892023-04-10 15:21:59 -070090 bool waitForConnected(std::chrono::milliseconds waitTime);
91
Aaqib Ismail514c23c2023-09-19 17:55:01 -070092 protected:
93 std::shared_mutex mCallbackMutex;
94 std::unique_ptr<const PropertyChangeCallback> mOnPropChange;
95
Hao Chen6cb86892023-04-10 15:21:59 -070096 private:
97 void ValuePollingLoop();
98
99 std::string mServiceAddr;
100 std::shared_ptr<::grpc::Channel> mGrpcChannel;
Yu Shan5c846f72024-05-16 15:39:51 -0700101 std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub;
Hao Chen6cb86892023-04-10 15:21:59 -0700102 std::thread mValuePollingThread;
103
Hao Chen6cb86892023-04-10 15:21:59 -0700104 std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
105
106 std::mutex mShutdownMutex;
107 std::condition_variable mShutdownCV;
108 std::atomic<bool> mShuttingDownFlag{false};
109};
110
111} // namespace android::hardware::automotive::vehicle::virtualization