Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Shan | abd92c1 | 2024-07-03 16:13:18 -0700 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 24 | |
| 25 | #include "VehicleServer.grpc.pb.h" |
| 26 | #include "VehicleServer.pb.h" |
| 27 | |
Krzysztof KosiĆski | c608187 | 2023-08-19 02:07:47 +0000 | [diff] [blame] | 28 | #include <grpc++/grpc++.h> |
| 29 | |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 30 | #include <atomic> |
| 31 | #include <chrono> |
| 32 | #include <condition_variable> |
| 33 | #include <memory> |
| 34 | #include <shared_mutex> |
| 35 | #include <string> |
| 36 | #include <thread> |
Yu Shan | abd92c1 | 2024-07-03 16:13:18 -0700 | [diff] [blame] | 37 | #include <unordered_map> |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | namespace android::hardware::automotive::vehicle::virtualization { |
| 41 | |
| 42 | namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle; |
| 43 | |
| 44 | class 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 Shan | 96b97eb | 2024-07-12 14:59:04 -0700 | [diff] [blame^] | 53 | // 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 Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 57 | // 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 Shan | 5c846f7 | 2024-05-16 15:39:51 -0700 | [diff] [blame] | 89 | aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override; |
| 90 | |
Yu Shan | 2c37c11 | 2024-05-20 17:27:19 -0700 | [diff] [blame] | 91 | aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override; |
| 92 | |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 93 | bool waitForConnected(std::chrono::milliseconds waitTime); |
| 94 | |
Aaqib Ismail | 514c23c | 2023-09-19 17:55:01 -0700 | [diff] [blame] | 95 | protected: |
| 96 | std::shared_mutex mCallbackMutex; |
| 97 | std::unique_ptr<const PropertyChangeCallback> mOnPropChange; |
| 98 | |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 99 | private: |
Yu Shan | abd92c1 | 2024-07-03 16:13:18 -0700 | [diff] [blame] | 100 | friend class GRPCVehicleHardwareUnitTest; |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 101 | |
| 102 | std::string mServiceAddr; |
| 103 | std::shared_ptr<::grpc::Channel> mGrpcChannel; |
Yu Shan | 5c846f7 | 2024-05-16 15:39:51 -0700 | [diff] [blame] | 104 | std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub; |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 105 | std::thread mValuePollingThread; |
| 106 | |
Hao Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 107 | std::unique_ptr<const PropertySetErrorCallback> mOnSetErr; |
| 108 | |
| 109 | std::mutex mShutdownMutex; |
| 110 | std::condition_variable mShutdownCV; |
| 111 | std::atomic<bool> mShuttingDownFlag{false}; |
Yu Shan | abd92c1 | 2024-07-03 16:13:18 -0700 | [diff] [blame] | 112 | |
| 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 Chen | 6cb8689 | 2023-04-10 15:21:59 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // namespace android::hardware::automotive::vehicle::virtualization |