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