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