blob: 1edf6580aaf5bb770f9ebf1ca9b6f2f01e6360b3 [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>
Yu Shanabd92c12024-07-03 16:13:18 -070023#include <android-base/thread_annotations.h>
Hao Chen6cb86892023-04-10 15:21:59 -070024
25#include "VehicleServer.grpc.pb.h"
26#include "VehicleServer.pb.h"
27
Krzysztof KosiƄskic6081872023-08-19 02:07:47 +000028#include <grpc++/grpc++.h>
29
Hao Chen6cb86892023-04-10 15:21:59 -070030#include <atomic>
31#include <chrono>
32#include <condition_variable>
33#include <memory>
34#include <shared_mutex>
35#include <string>
36#include <thread>
Yu Shanabd92c12024-07-03 16:13:18 -070037#include <unordered_map>
Hao Chen6cb86892023-04-10 15:21:59 -070038#include <vector>
39
40namespace android::hardware::automotive::vehicle::virtualization {
41
42namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle;
43
44class GRPCVehicleHardware : public IVehicleHardware {
45 public:
46 explicit GRPCVehicleHardware(std::string service_addr);
47
48 ~GRPCVehicleHardware();
49
50 // Get all the property configs.
51 std::vector<aidlvhal::VehiclePropConfig> getAllPropertyConfigs() const override;
52
53 // Set property values asynchronously. Server could return before the property set requests
54 // are sent to vehicle bus or before property set confirmation is received. The callback is
55 // safe to be called after the function returns and is safe to be called in a different thread.
56 aidlvhal::StatusCode setValues(std::shared_ptr<const SetValuesCallback> callback,
57 const std::vector<aidlvhal::SetValueRequest>& requests) override;
58
59 // Get property values asynchronously. Server could return before the property values are ready.
60 // The callback is safe to be called after the function returns and is safe to be called in a
61 // different thread.
62 aidlvhal::StatusCode getValues(
63 std::shared_ptr<const GetValuesCallback> callback,
64 const std::vector<aidlvhal::GetValueRequest>& requests) const override;
65
66 // Dump debug information in the server.
67 DumpResult dump(const std::vector<std::string>& options) override;
68
69 // Check whether the system is healthy, return {@code StatusCode::OK} for healthy.
70 aidlvhal::StatusCode checkHealth() override;
71
72 // Register a callback that would be called when there is a property change event from vehicle.
73 void registerOnPropertyChangeEvent(
74 std::unique_ptr<const PropertyChangeCallback> callback) override;
75
76 // Register a callback that would be called when there is a property set error event from
77 // vehicle.
78 void registerOnPropertySetErrorEvent(
79 std::unique_ptr<const PropertySetErrorCallback> callback) override;
80
81 // Update the sample rate for the [propId, areaId] pair.
82 aidlvhal::StatusCode updateSampleRate(int32_t propId, int32_t areaId,
83 float sampleRate) override;
84
Yu Shan5c846f72024-05-16 15:39:51 -070085 aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override;
86
Yu Shan2c37c112024-05-20 17:27:19 -070087 aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override;
88
Hao Chen6cb86892023-04-10 15:21:59 -070089 bool waitForConnected(std::chrono::milliseconds waitTime);
90
Aaqib Ismail514c23c2023-09-19 17:55:01 -070091 protected:
92 std::shared_mutex mCallbackMutex;
93 std::unique_ptr<const PropertyChangeCallback> mOnPropChange;
94
Hao Chen6cb86892023-04-10 15:21:59 -070095 private:
Yu Shanabd92c12024-07-03 16:13:18 -070096 friend class GRPCVehicleHardwareUnitTest;
Hao Chen6cb86892023-04-10 15:21:59 -070097
98 std::string mServiceAddr;
99 std::shared_ptr<::grpc::Channel> mGrpcChannel;
Yu Shan5c846f72024-05-16 15:39:51 -0700100 std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub;
Hao Chen6cb86892023-04-10 15:21:59 -0700101 std::thread mValuePollingThread;
102
Hao Chen6cb86892023-04-10 15:21:59 -0700103 std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
104
105 std::mutex mShutdownMutex;
106 std::condition_variable mShutdownCV;
107 std::atomic<bool> mShuttingDownFlag{false};
Yu Shanabd92c12024-07-03 16:13:18 -0700108
109 mutable std::mutex mLatestUpdateTimestampsMutex;
110
111 // A map from [propId, areaId] to the latest timestamp this property is updated.
112 // The key is a tuple, the first element is the external timestamp (timestamp set by VHAL
113 // server), the second element is the Android timestamp (elapsedRealtimeNano).
114 mutable std::unordered_map<PropIdAreaId, std::pair<int64_t, int64_t>,
115 PropIdAreaIdHash> mLatestUpdateTimestamps
116 GUARDED_BY(mLatestUpdateTimestampsMutex);
117
118 // Only used for unit testing.
119 GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub,
120 bool startValuePollingLoop);
121
122 void ValuePollingLoop();
123 void pollValue();
124
125 aidlvhal::StatusCode getValuesWithRetry(const std::vector<aidlvhal::GetValueRequest>& requests,
126 std::vector<aidlvhal::GetValueResult>* results,
127 size_t retryCount) const;
128
129 // Check the external timestamp of propValue against the latest updated external timestamp, if
130 // this is an outdated value, return false. Otherwise, update the external timestamp to the
131 // Android timestamp and return true.
132 bool setAndroidTimestamp(aidlvhal::VehiclePropValue* propValue) const;
Hao Chen6cb86892023-04-10 15:21:59 -0700133};
134
135} // namespace android::hardware::automotive::vehicle::virtualization