1 /*
2  * Copyright (C) 2016 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_V2_0_VehicleHal_H
18 #define android_hardware_automotive_vehicle_V2_0_VehicleHal_H
19 
20 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
21 #include "VehicleObjectPool.h"
22 
23 namespace android {
24 namespace hardware {
25 namespace automotive {
26 namespace vehicle {
27 namespace V2_0 {
28 
29 /**
30  * This is a low-level vehicle hal interface that should be implemented by
31  * Vendor.
32  */
33 class VehicleHal {
34 public:
35     using VehiclePropValuePtr = recyclable_ptr<VehiclePropValue>;
36 
37     using HalEventFunction = std::function<void(VehiclePropValuePtr)>;
38     using HalErrorFunction = std::function<void(
39             StatusCode errorCode, int32_t property, int32_t areaId)>;
40 
~VehicleHal()41     virtual ~VehicleHal() {}
42 
43     virtual std::vector<VehiclePropConfig> listProperties() = 0;
44     virtual VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue,
45                                     StatusCode* outStatus) = 0;
46 
47     virtual StatusCode set(const VehiclePropValue& propValue) = 0;
48 
49     /**
50      * Subscribe to HAL property events. This method might be called multiple
51      * times for the same vehicle property to update sample rate.
52      *
53      * @param property to subscribe
54      * @param sampleRate sample rate in Hz for properties that support sample
55      *                   rate, e.g. for properties with
56      *                   VehiclePropertyChangeMode::CONTINUOUS
57      */
58     virtual StatusCode subscribe(int32_t property,
59                                  float sampleRate) = 0;
60 
61     /**
62      * Unsubscribe from HAL events for given property
63      *
64      * @param property vehicle property to unsubscribe
65      */
66     virtual StatusCode unsubscribe(int32_t property) = 0;
67 
68     /**
69      * Override this method if you need to do one-time initialization.
70      */
onCreate()71     virtual void onCreate() {}
72 
73     /**
74      * Dump method forwarded from HIDL's debug().
75      *
76      * By default it doesn't dump anything and let caller dump its properties, but it could be
77      * override to change the behavior. For example:
78      *
79      * - To augment caller's dump, it should dump its state and return true.
80      * - To not dump anything at all, it should just return false.
81      * - To provide custom dump (like dumping just specific state or executing a custom command),
82      *   it should check if options is not empty, handle the options accordingly, then return false.
83      *
84      * @param handle handle used to dump the contents.
85      * @param options options passed to dump.
86      *
87      * @return whether the caller should dump its state.
88      */
dump(const hidl_handle &,const hidl_vec<hidl_string> &)89     virtual bool dump(const hidl_handle& /* handle */, const hidl_vec<hidl_string>& /* options */) {
90         return true;
91     }
92 
init(VehiclePropValuePool * valueObjectPool,const HalEventFunction & onHalEvent,const HalErrorFunction & onHalError)93     void init(
94         VehiclePropValuePool* valueObjectPool,
95         const HalEventFunction& onHalEvent,
96         const HalErrorFunction& onHalError) {
97         mValuePool = valueObjectPool;
98         mOnHalEvent = onHalEvent;
99         mOnHalPropertySetError = onHalError;
100 
101         onCreate();
102     }
103 
getValuePool()104     VehiclePropValuePool* getValuePool() {
105         return mValuePool;
106     }
107 protected:
108     /* Propagates property change events to vehicle HAL clients. */
doHalEvent(VehiclePropValuePtr v)109     void doHalEvent(VehiclePropValuePtr v) {
110         mOnHalEvent(std::move(v));
111     }
112 
113     /* Propagates error during set operation to the vehicle HAL clients. */
doHalPropertySetError(StatusCode errorCode,int32_t propId,int32_t areaId)114     void doHalPropertySetError(StatusCode errorCode,
115                                int32_t propId,
116                                int32_t areaId) {
117         mOnHalPropertySetError(errorCode, propId, areaId);
118     }
119 
120 private:
121     HalEventFunction mOnHalEvent;
122     HalErrorFunction mOnHalPropertySetError;
123     VehiclePropValuePool* mValuePool;
124 };
125 
126 }  // namespace V2_0
127 }  // namespace vehicle
128 }  // namespace automotive
129 }  // namespace hardware
130 }  // namespace android
131 
132 #endif //android_hardware_automotive_vehicle_V2_0_VehicleHal_H_
133