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> 23 #include <android-base/thread_annotations.h> 24 25 #include "VehicleServer.grpc.pb.h" 26 #include "VehicleServer.pb.h" 27 28 #include <grpc++/grpc++.h> 29 30 #include <atomic> 31 #include <chrono> 32 #include <condition_variable> 33 #include <memory> 34 #include <shared_mutex> 35 #include <string> 36 #include <thread> 37 #include <unordered_map> 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 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 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 89 aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override; 90 91 aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override; 92 93 bool waitForConnected(std::chrono::milliseconds waitTime); 94 95 protected: 96 std::shared_mutex mCallbackMutex; 97 std::unique_ptr<const PropertyChangeCallback> mOnPropChange; 98 99 private: 100 friend class GRPCVehicleHardwareUnitTest; 101 102 std::string mServiceAddr; 103 std::shared_ptr<::grpc::Channel> mGrpcChannel; 104 std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub; 105 std::thread mValuePollingThread; 106 107 std::unique_ptr<const PropertySetErrorCallback> mOnSetErr; 108 109 std::mutex mShutdownMutex; 110 std::condition_variable mShutdownCV; 111 std::atomic<bool> mShuttingDownFlag{false}; 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>, PropIdAreaIdHash> 119 mLatestUpdateTimestamps GUARDED_BY(mLatestUpdateTimestampsMutex); 120 121 // Only used for unit testing. 122 GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub, 123 bool startValuePollingLoop); 124 125 void ValuePollingLoop(); 126 void pollValue(); 127 128 aidlvhal::StatusCode getValuesWithRetry(const std::vector<aidlvhal::GetValueRequest>& requests, 129 std::vector<aidlvhal::GetValueResult>* results, 130 size_t retryCount) const; 131 132 // Check the external timestamp of propValue against the latest updated external timestamp, if 133 // this is an outdated value, return false. Otherwise, update the external timestamp to the 134 // Android timestamp and return true. 135 bool setAndroidTimestamp(aidlvhal::VehiclePropValue* propValue) const; 136 }; 137 138 } // namespace android::hardware::automotive::vehicle::virtualization 139