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