blob: 15f473c0bd9f344adf3592e03854c52530a1f596 [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
Yu Shan96b97eb2024-07-12 14:59:04 -070053 // Get the config for the specified propId.
54 std::optional<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>
55 getPropertyConfig(int32_t propId) const override;
56
Hao Chen6cb86892023-04-10 15:21:59 -070057 // Set property values asynchronously. Server could return before the property set requests
58 // are sent to vehicle bus or before property set confirmation is received. The callback is
59 // safe to be called after the function returns and is safe to be called in a different thread.
60 aidlvhal::StatusCode setValues(std::shared_ptr<const SetValuesCallback> callback,
61 const std::vector<aidlvhal::SetValueRequest>& requests) override;
62
63 // Get property values asynchronously. Server could return before the property values are ready.
64 // The callback is safe to be called after the function returns and is safe to be called in a
65 // different thread.
66 aidlvhal::StatusCode getValues(
67 std::shared_ptr<const GetValuesCallback> callback,
68 const std::vector<aidlvhal::GetValueRequest>& requests) const override;
69
70 // Dump debug information in the server.
71 DumpResult dump(const std::vector<std::string>& options) override;
72
73 // Check whether the system is healthy, return {@code StatusCode::OK} for healthy.
74 aidlvhal::StatusCode checkHealth() override;
75
76 // Register a callback that would be called when there is a property change event from vehicle.
77 void registerOnPropertyChangeEvent(
78 std::unique_ptr<const PropertyChangeCallback> callback) override;
79
80 // Register a callback that would be called when there is a property set error event from
81 // vehicle.
82 void registerOnPropertySetErrorEvent(
83 std::unique_ptr<const PropertySetErrorCallback> callback) override;
84
85 // Update the sample rate for the [propId, areaId] pair.
86 aidlvhal::StatusCode updateSampleRate(int32_t propId, int32_t areaId,
87 float sampleRate) override;
88
Yu Shan5c846f72024-05-16 15:39:51 -070089 aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override;
90
Yu Shan2c37c112024-05-20 17:27:19 -070091 aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override;
92
Hao Chen6cb86892023-04-10 15:21:59 -070093 bool waitForConnected(std::chrono::milliseconds waitTime);
94
Aaqib Ismail514c23c2023-09-19 17:55:01 -070095 protected:
96 std::shared_mutex mCallbackMutex;
97 std::unique_ptr<const PropertyChangeCallback> mOnPropChange;
98
Hao Chen6cb86892023-04-10 15:21:59 -070099 private:
Yu Shanabd92c12024-07-03 16:13:18 -0700100 friend class GRPCVehicleHardwareUnitTest;
Hao Chen6cb86892023-04-10 15:21:59 -0700101
102 std::string mServiceAddr;
103 std::shared_ptr<::grpc::Channel> mGrpcChannel;
Yu Shan5c846f72024-05-16 15:39:51 -0700104 std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub;
Hao Chen6cb86892023-04-10 15:21:59 -0700105 std::thread mValuePollingThread;
106
Hao Chen6cb86892023-04-10 15:21:59 -0700107 std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
108
109 std::mutex mShutdownMutex;
110 std::condition_variable mShutdownCV;
111 std::atomic<bool> mShuttingDownFlag{false};
Yu Shanabd92c12024-07-03 16:13:18 -0700112
113 mutable std::mutex mLatestUpdateTimestampsMutex;
114
115 // A map from [propId, areaId] to the latest timestamp this property is updated.
116 // The key is a tuple, the first element is the external timestamp (timestamp set by VHAL
117 // server), the second element is the Android timestamp (elapsedRealtimeNano).
118 mutable std::unordered_map<PropIdAreaId, std::pair<int64_t, int64_t>,
119 PropIdAreaIdHash> mLatestUpdateTimestamps
120 GUARDED_BY(mLatestUpdateTimestampsMutex);
121
122 // Only used for unit testing.
123 GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub,
124 bool startValuePollingLoop);
125
126 void ValuePollingLoop();
127 void pollValue();
128
129 aidlvhal::StatusCode getValuesWithRetry(const std::vector<aidlvhal::GetValueRequest>& requests,
130 std::vector<aidlvhal::GetValueResult>* results,
131 size_t retryCount) const;
132
133 // Check the external timestamp of propValue against the latest updated external timestamp, if
134 // this is an outdated value, return false. Otherwise, update the external timestamp to the
135 // Android timestamp and return true.
136 bool setAndroidTimestamp(aidlvhal::VehiclePropValue* propValue) const;
Hao Chen6cb86892023-04-10 15:21:59 -0700137};
138
139} // namespace android::hardware::automotive::vehicle::virtualization