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 subscribed areas or sample 52 * rate. 53 * 54 * @param property to subscribe 55 * @param areas a bitwise vehicle areas or 0 for all supported areas 56 * @param sampleRate sample rate in Hz for properties that support sample 57 * rate, e.g. for properties with 58 * VehiclePropertyChangeMode::CONTINUOUS 59 */ 60 virtual StatusCode subscribe(int32_t property, 61 int32_t areas, 62 float sampleRate) = 0; 63 64 /** 65 * Unsubscribe from HAL events for given property 66 * 67 * @param property vehicle property to unsubscribe 68 */ 69 virtual StatusCode unsubscribe(int32_t property) = 0; 70 71 /** 72 * Override this method if you need to do one-time initialization. 73 */ onCreate()74 virtual void onCreate() {} 75 init(VehiclePropValuePool * valueObjectPool,const HalEventFunction & onHalEvent,const HalErrorFunction & onHalError)76 void init( 77 VehiclePropValuePool* valueObjectPool, 78 const HalEventFunction& onHalEvent, 79 const HalErrorFunction& onHalError) { 80 mValuePool = valueObjectPool; 81 mOnHalEvent = onHalEvent; 82 mOnHalPropertySetError = onHalError; 83 84 onCreate(); 85 } 86 getValuePool()87 VehiclePropValuePool* getValuePool() { 88 return mValuePool; 89 } 90 protected: 91 /* Propagates property change events to vehicle HAL clients. */ doHalEvent(VehiclePropValuePtr v)92 void doHalEvent(VehiclePropValuePtr v) { 93 mOnHalEvent(std::move(v)); 94 } 95 96 /* Propagates error during set operation to the vehicle HAL clients. */ doHalPropertySetError(StatusCode errorCode,int32_t propId,int32_t areaId)97 void doHalPropertySetError(StatusCode errorCode, 98 int32_t propId, 99 int32_t areaId) { 100 mOnHalPropertySetError(errorCode, propId, areaId); 101 } 102 103 private: 104 HalEventFunction mOnHalEvent; 105 HalErrorFunction mOnHalPropertySetError; 106 VehiclePropValuePool* mValuePool; 107 }; 108 109 } // namespace V2_0 110 } // namespace vehicle 111 } // namespace automotive 112 } // namespace hardware 113 } // namespace android 114 115 #endif //android_hardware_automotive_vehicle_V2_0_VehicleHal_H_ 116