blob: 5e2e5bb52066481b9567479cbe1e99e3f0b4c5b8 [file] [log] [blame]
keunyoungcc449f72015-08-12 10:46:27 -07001/*
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
17package com.android.car.hal;
18
Pavel Maltsev0d07c762016-11-03 16:40:15 -070019
Keun young Park1fd33fe2019-12-19 18:25:14 -080020import android.annotation.NonNull;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080021import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
22import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
Felipe Lemea123f692020-02-10 09:50:38 -080023import android.util.Log;
keunyoungfe30ba02015-09-17 17:56:35 -070024
keunyoungcc449f72015-08-12 10:46:27 -070025import java.io.PrintWriter;
Keun young Park1fd33fe2019-12-19 18:25:14 -080026import java.util.ArrayList;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070027import java.util.Collection;
keunyoungcc449f72015-08-12 10:46:27 -070028import 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 */
35public abstract class HalServiceBase {
Felipe Lemea123f692020-02-10 09:50:38 -080036
37 private static final String MY_TAG = HalServiceBase.class.getSimpleName();
38
keunyoungfe30ba02015-09-17 17:56:35 -070039 /** For dispatching events. Kept here to avoid alloc every time */
Keun young Park1fd33fe2019-12-19 18:25:14 -080040 private final ArrayList<VehiclePropValue> mDispatchList = new ArrayList<>(1);
keunyoungfe30ba02015-09-17 17:56:35 -070041
Pavel Maltsev1e5a88b2016-12-15 17:51:29 -080042 final static int NOT_SUPPORTED_PROPERTY = -1;
43
keunyoungfe30ba02015-09-17 17:56:35 -070044 public List<VehiclePropValue> getDispatchList() {
45 return mDispatchList;
46 }
47
keunyoungcc449f72015-08-12 10:46:27 -070048 /** initialize */
49 public abstract void init();
50
51 /** release and stop operation */
52 public abstract void release();
53
54 /**
Keun young Park1fd33fe2019-12-19 18:25:14 -080055 * 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.
keunyoungcc449f72015-08-12 10:46:27 -070057 */
Keun young Park1fd33fe2019-12-19 18:25:14 -080058 @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 Maltsev0d07c762016-11-03 16:40:15 -070071 }
keunyoungcc449f72015-08-12 10:46:27 -070072
Felipe Lemea123f692020-02-10 09:50:38 -080073 /**
Keun young Park1fd33fe2019-12-19 18:25:14 -080074 * 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 Lemea123f692020-02-10 09:50:38 -080085 * Handles property changes from HAL.
86 */
87 public abstract void onHalEvents(List<VehiclePropValue> values);
keunyoungcc449f72015-08-12 10:46:27 -070088
Felipe Lemea123f692020-02-10 09:50:38 -080089 /**
Kai6e75d492020-02-20 16:11:40 -080090 * Handles errors and pass error codes when setting properties.
Felipe Lemea123f692020-02-10 09:50:38 -080091 */
Kai6e75d492020-02-20 16:11:40 -080092 public void onPropertySetError(int property, int area, int errorCode) {
Felipe Lemea123f692020-02-10 09:50:38 -080093 Log.d(MY_TAG, getClass().getSimpleName() + ".onPropertySetError(): property=" + property
Kai6e75d492020-02-20 16:11:40 -080094 + ", area=" + area + " , errorCode = " + errorCode);
Felipe Lemea123f692020-02-10 09:50:38 -080095 }
Pavel Maltsev2a8c56d2016-12-14 11:58:14 -080096
keunyoungcc449f72015-08-12 10:46:27 -070097 public abstract void dump(PrintWriter writer);
Pavel Maltsev1e5a88b2016-12-15 17:51:29 -080098
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 }
keunyoungcc449f72015-08-12 10:46:27 -0700130}