1 /* 2 * Copyright (C) 2015 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 package com.android.car.hal; 18 19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 20 21 import android.annotation.NonNull; 22 import android.car.builtin.util.Slogf; 23 import android.hardware.automotive.vehicle.VehiclePropError; 24 25 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 26 import com.android.car.internal.property.PropIdAreaId; 27 28 import java.io.PrintWriter; 29 import java.util.ArrayList; 30 import java.util.Collection; 31 import java.util.List; 32 33 /** 34 * Common interface for all HAL service like sensor HAL. 35 * Each HAL service is connected with XyzService supporting XyzManager, 36 * and will translate HAL data into car api specific format. 37 */ 38 public abstract class HalServiceBase { 39 40 private static final String MY_TAG = HalServiceBase.class.getSimpleName(); 41 42 /** For dispatching events. Kept here to avoid alloc every time */ 43 private final ArrayList<HalPropValue> mDispatchList = new ArrayList<>(1); 44 45 static final int NOT_SUPPORTED_PROPERTY = -1; 46 getDispatchList()47 public List<HalPropValue> getDispatchList() { 48 return mDispatchList; 49 } 50 51 /** initialize */ init()52 public abstract void init(); 53 54 /** release and stop operation */ release()55 public abstract void release(); 56 57 /** 58 * Returns all property IDs this HalService can support. If return value is empty, 59 * {@link #isSupportedProperty(int)} is used to query support for each property. 60 * 61 * Implementers should consider keeping this method package private to avoid exposing the 62 * internal array. 63 */ 64 @NonNull getAllSupportedProperties()65 abstract int[] getAllSupportedProperties(); 66 67 /** 68 * Checks if given {@code propId} is supported. 69 */ isSupportedProperty(int propId)70 public boolean isSupportedProperty(int propId) { 71 for (int supported: getAllSupportedProperties()) { 72 if (propId == supported) { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 /** 80 * Takes the passed properties. Passed properties are a subset of properties returned from 81 * {@link #getAllSupportedProperties()} and are supported in the current device. 82 * 83 * @param properties properties that are available in this device. This is guaranteed to be 84 * supported by the HalService as the list is filtered with 85 * {@link #getAllSupportedProperties()} or {@link #isSupportedProperty(int)}. 86 * It can be empty if no property is available. 87 */ 88 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) takeProperties(@onNull Collection<HalPropConfig> properties)89 public void takeProperties(@NonNull Collection<HalPropConfig> properties) { 90 } 91 92 /** 93 * Handles property changes from HAL. 94 */ 95 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) onHalEvents(List<HalPropValue> values)96 public void onHalEvents(List<HalPropValue> values) { 97 } 98 99 /** 100 * Handles property min/max supported values change or supported values list change from HAL. 101 */ 102 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) onSupportedValuesChange(List<PropIdAreaId> propIdAreaIds)103 public void onSupportedValuesChange(List<PropIdAreaId> propIdAreaIds) { 104 } 105 106 /** 107 * Handles errors and pass error codes when setting properties. 108 */ 109 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) onPropertySetError(ArrayList<VehiclePropError> errors)110 public void onPropertySetError(ArrayList<VehiclePropError> errors) { 111 for (int i = 0; i < errors.size(); i++) { 112 VehiclePropError error = errors.get(i); 113 Slogf.d(MY_TAG, getClass().getSimpleName() + ".onPropertySetError(): property=" 114 + error.propId + ", area=" + error.areaId + " , errorCode = " 115 + error.errorCode); 116 } 117 } 118 119 /** 120 * Dumps HAL service related info to the writer passed as parameter. 121 */ dump(PrintWriter writer)122 public abstract void dump(PrintWriter writer); 123 124 /** 125 * Helper class that maintains bi-directional mapping between manager's property 126 * Id (public or system API) and vehicle HAL property Id. 127 * 128 * <p>This class is supposed to be immutable. Use {@link #create(int[])} factory method to 129 * instantiate this class. 130 */ 131 static class ManagerToHalPropIdMap { 132 private final BidirectionalSparseIntArray mMap; 133 134 /** 135 * Creates {@link ManagerToHalPropIdMap} for provided [manager prop Id, hal prop Id] pairs. 136 * 137 * <p> The input array should have an odd number of elements. 138 */ create(int... mgrToHalPropIds)139 static ManagerToHalPropIdMap create(int... mgrToHalPropIds) { 140 return new ManagerToHalPropIdMap(BidirectionalSparseIntArray.create(mgrToHalPropIds)); 141 } 142 ManagerToHalPropIdMap(BidirectionalSparseIntArray map)143 private ManagerToHalPropIdMap(BidirectionalSparseIntArray map) { 144 mMap = map; 145 } 146 getHalPropId(int managerPropId)147 int getHalPropId(int managerPropId) { 148 return mMap.getValue(managerPropId, NOT_SUPPORTED_PROPERTY); 149 } 150 getManagerPropId(int halPropId)151 int getManagerPropId(int halPropId) { 152 return mMap.getKey(halPropId, NOT_SUPPORTED_PROPERTY); 153 } 154 } 155 } 156