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      * Implementers should consider keeping this method package private to avoid exposing the
61      * internal array.
62      */
63     @NonNull
getAllSupportedProperties()64     abstract int[] getAllSupportedProperties();
65 
66     /**
67      * Checks if given {@code propId} is supported.
68      */
isSupportedProperty(int propId)69     public boolean isSupportedProperty(int propId) {
70         for (int supported: getAllSupportedProperties()) {
71             if (propId == supported) {
72                 return true;
73             }
74         }
75         return false;
76     }
77 
78     /**
79      * Takes the passed properties. Passed properties are a subset of properties returned from
80      * {@link #getAllSupportedProperties()} and are supported in the current device.
81      *
82      * @param properties properties that are available in this device. This is guaranteed to be
83      *                   supported by the HalService as the list is filtered with
84      *                   {@link #getAllSupportedProperties()} or {@link #isSupportedProperty(int)}.
85      *                   It can be empty if no property is available.
86      */
87     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
takeProperties(@onNull Collection<HalPropConfig> properties)88     public void takeProperties(@NonNull Collection<HalPropConfig> properties) {
89     }
90 
91     /**
92      * Handles property changes from HAL.
93      */
94     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
onHalEvents(List<HalPropValue> values)95     public void onHalEvents(List<HalPropValue> values) {
96     }
97 
98     /**
99      * Handles errors and pass error codes  when setting properties.
100      */
101     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
onPropertySetError(ArrayList<VehiclePropError> errors)102     public void onPropertySetError(ArrayList<VehiclePropError> errors) {
103         for (int i = 0; i < errors.size(); i++) {
104             VehiclePropError error = errors.get(i);
105             Slogf.d(MY_TAG, getClass().getSimpleName() + ".onPropertySetError(): property="
106                     + error.propId + ", area=" + error.areaId + " , errorCode = "
107                     + error.errorCode);
108         }
109     }
110 
111     /**
112      * Dumps HAL service related info to the writer passed as parameter.
113      */
dump(PrintWriter writer)114     public abstract void dump(PrintWriter writer);
115 
116     /**
117      * Helper class that maintains bi-directional mapping between manager's property
118      * Id (public or system API) and vehicle HAL property Id.
119      *
120      * <p>This class is supposed to be immutable. Use {@link #create(int[])} factory method to
121      * instantiate this class.
122      */
123     static class ManagerToHalPropIdMap {
124         private final BidirectionalSparseIntArray mMap;
125 
126         /**
127          * Creates {@link ManagerToHalPropIdMap} for provided [manager prop Id, hal prop Id] pairs.
128          *
129          * <p> The input array should have an odd number of elements.
130          */
create(int... mgrToHalPropIds)131         static ManagerToHalPropIdMap create(int... mgrToHalPropIds) {
132             return new ManagerToHalPropIdMap(BidirectionalSparseIntArray.create(mgrToHalPropIds));
133         }
134 
ManagerToHalPropIdMap(BidirectionalSparseIntArray map)135         private ManagerToHalPropIdMap(BidirectionalSparseIntArray map) {
136             mMap = map;
137         }
138 
getHalPropId(int managerPropId)139         int getHalPropId(int managerPropId) {
140             return mMap.getValue(managerPropId, NOT_SUPPORTED_PROPERTY);
141         }
142 
getManagerPropId(int halPropId)143         int getManagerPropId(int halPropId) {
144             return mMap.getKey(halPropId, NOT_SUPPORTED_PROPERTY);
145         }
146     }
147 }
148