• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef android_hardware_automotive_vehicle_aidl_impl_hardware_include_IVehicleHardware_H_
18 #define android_hardware_automotive_vehicle_aidl_impl_hardware_include_IVehicleHardware_H_
19 
20 #include <VehicleHalTypes.h>
21 
22 #include <memory>
23 #include <vector>
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace vehicle {
29 
30 // A structure used to return dumped information.
31 struct DumpResult {
32     // If callerShouldDumpState is true, caller would print the information in buffer and
33     // continue to dump its state, otherwise would just dump the buffer and skip its own
34     // dumping logic.
35     bool callerShouldDumpState;
36     // The dumped information for the caller to print.
37     std::string buffer;
38     // To pass if DefaultVehicleHal should refresh the property configs
39     bool refreshPropertyConfigs = false;
40 };
41 
42 // A structure to represent a set value error event reported from vehicle.
43 struct SetValueErrorEvent {
44     aidl::android::hardware::automotive::vehicle::StatusCode errorCode;
45     int32_t propId;
46     int32_t areaId;
47 };
48 
49 // An abstract interface to access vehicle hardware.
50 // For virtualized VHAL, GrpcVehicleHardware would communicate with a VehicleHardware
51 // implementation in another VM through GRPC. For non-virtualzied VHAL, VHAL directly communicates
52 // with a VehicleHardware through this interface.
53 class IVehicleHardware {
54   public:
55     using SetValuesCallback = std::function<void(
56             std::vector<aidl::android::hardware::automotive::vehicle::SetValueResult>)>;
57     using GetValuesCallback = std::function<void(
58             std::vector<aidl::android::hardware::automotive::vehicle::GetValueResult>)>;
59     using PropertyChangeCallback = std::function<void(
60             std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropValue>)>;
61     using PropertySetErrorCallback = std::function<void(std::vector<SetValueErrorEvent>)>;
62 
63     virtual ~IVehicleHardware() = default;
64 
65     // Get all the property configs.
66     virtual std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>
67     getAllPropertyConfigs() const = 0;
68 
69     // Set property values asynchronously. Server could return before the property set requests
70     // are sent to vehicle bus or before property set confirmation is received. The callback is
71     // safe to be called after the function returns and is safe to be called in a different thread.
72     virtual aidl::android::hardware::automotive::vehicle::StatusCode setValues(
73             std::shared_ptr<const SetValuesCallback> callback,
74             const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>&
75                     requests) = 0;
76 
77     // Get property values asynchronously. Server could return before the property values are ready.
78     // The callback is safe to be called after the function returns and is safe to be called in a
79     // different thread.
80     virtual aidl::android::hardware::automotive::vehicle::StatusCode getValues(
81             std::shared_ptr<const GetValuesCallback> callback,
82             const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&
83                     requests) const = 0;
84 
85     // Update the sampling rate for the specified property and the specified areaId (0 for global
86     // property) if server supports it. The property must be a continuous property.
87     // {@code sampleRate} means that for this specific property, the server must generate at least
88     // this many OnPropertyChange events per seconds.
89     // A sampleRate of 0 means the property is no longer subscribed and server does not need to
90     // generate any onPropertyEvent for this property.
91     // This would be called if sample rate is updated for a subscriber, a new subscriber is added
92     // or an existing subscriber is removed. For example:
93     // 1. We have no subscriber for speed.
94     // 2. A new subscriber is subscribing speed for 10 times/s, updsateSampleRate would be called
95     //    with sampleRate as 10. The impl is now polling vehicle speed from bus 10 times/s.
96     // 3. A new subscriber is subscribing speed for 5 times/s, because it is less than 10
97     //    times/sec, updateSampleRate would not be called.
98     // 4. The initial subscriber is removed, updateSampleRate would be called with sampleRate as
99     //    5, because now it only needs to report event 5times/sec. The impl can now poll vehicle
100     //    speed 5 times/s. If the impl is still polling at 10 times/s, that is okay as long as
101     //    the polling rate is larger than 5times/s. DefaultVehicleHal would ignore the additional
102     //    events.
103     // 5. The second subscriber is removed, updateSampleRate would be called with sampleRate as 0.
104     //    The impl can optionally disable the polling for vehicle speed.
105     //
106     // If the impl is always polling at {@code maxSampleRate} as specified in config, then this
107     // function can be a no-op.
updateSampleRate(int32_t propId,int32_t areaId,float sampleRate)108     virtual aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate(
109             [[maybe_unused]] int32_t propId, [[maybe_unused]] int32_t areaId,
110             [[maybe_unused]] float sampleRate) {
111         return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
112     }
113 
114     // Dump debug information in the server.
115     virtual DumpResult dump(const std::vector<std::string>& options) = 0;
116 
117     // Check whether the system is healthy, return {@code StatusCode::OK} for healthy.
118     virtual aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() = 0;
119 
120     // Register a callback that would be called when there is a property change event from vehicle.
121     // Must only be called once during initialization.
122     virtual void registerOnPropertyChangeEvent(
123             std::unique_ptr<const PropertyChangeCallback> callback) = 0;
124 
125     // Register a callback that would be called when there is a property set error event from
126     // vehicle. Must only be called once during initialization.
127     virtual void registerOnPropertySetErrorEvent(
128             std::unique_ptr<const PropertySetErrorCallback> callback) = 0;
129 };
130 
131 }  // namespace vehicle
132 }  // namespace automotive
133 }  // namespace hardware
134 }  // namespace android
135 
136 #endif  // android_hardware_automotive_vehicle_aidl_impl_hardware_include_IVehicleHardware_H_
137