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 init(VehiclePropValuePool * valueObjectPool,const HalEventFunction & onHalEvent,const HalErrorFunction & onHalError)73 void init( 74 VehiclePropValuePool* valueObjectPool, 75 const HalEventFunction& onHalEvent, 76 const HalErrorFunction& onHalError) { 77 mValuePool = valueObjectPool; 78 mOnHalEvent = onHalEvent; 79 mOnHalPropertySetError = onHalError; 80 81 onCreate(); 82 } 83 getValuePool()84 VehiclePropValuePool* getValuePool() { 85 return mValuePool; 86 } 87 protected: 88 /* Propagates property change events to vehicle HAL clients. */ doHalEvent(VehiclePropValuePtr v)89 void doHalEvent(VehiclePropValuePtr v) { 90 mOnHalEvent(std::move(v)); 91 } 92 93 /* Propagates error during set operation to the vehicle HAL clients. */ doHalPropertySetError(StatusCode errorCode,int32_t propId,int32_t areaId)94 void doHalPropertySetError(StatusCode errorCode, 95 int32_t propId, 96 int32_t areaId) { 97 mOnHalPropertySetError(errorCode, propId, areaId); 98 } 99 100 private: 101 HalEventFunction mOnHalEvent; 102 HalErrorFunction mOnHalPropertySetError; 103 VehiclePropValuePool* mValuePool; 104 }; 105 106 } // namespace V2_0 107 } // namespace vehicle 108 } // namespace automotive 109 } // namespace hardware 110 } // namespace android 111 112 #endif //android_hardware_automotive_vehicle_V2_0_VehicleHal_H_ 113