• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include "VehicleServer.grpc.pb.h"
25 #include "VehicleServer.pb.h"
26 
27 #include <grpc++/grpc++.h>
28 
29 #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 
38 namespace android::hardware::automotive::vehicle::virtualization {
39 
40 namespace aidlvhal = ::aidl::android::hardware::automotive::vehicle;
41 
42 class GRPCVehicleHardware : public IVehicleHardware {
43   public:
44     explicit GRPCVehicleHardware(std::string service_addr);
45 
46     // Only used for unit testing.
47     explicit GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub);
48 
49     ~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 
86     aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override;
87 
88     aidlvhal::StatusCode unsubscribe(int32_t propId, int32_t areaId) override;
89 
90     bool waitForConnected(std::chrono::milliseconds waitTime);
91 
92   protected:
93     std::shared_mutex mCallbackMutex;
94     std::unique_ptr<const PropertyChangeCallback> mOnPropChange;
95 
96   private:
97     void ValuePollingLoop();
98 
99     std::string mServiceAddr;
100     std::shared_ptr<::grpc::Channel> mGrpcChannel;
101     std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub;
102     std::thread mValuePollingThread;
103 
104     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
112