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.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
22 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
23 import android.util.Log;
24 
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.Collection;
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 {
36 
37     private static final String MY_TAG = HalServiceBase.class.getSimpleName();
38 
39     /** For dispatching events. Kept here to avoid alloc every time */
40     private final ArrayList<VehiclePropValue> mDispatchList = new ArrayList<>(1);
41 
42     final static int NOT_SUPPORTED_PROPERTY = -1;
43 
getDispatchList()44     public List<VehiclePropValue> getDispatchList() {
45         return mDispatchList;
46     }
47 
48     /** initialize */
init()49     public abstract void init();
50 
51     /** release and stop operation */
release()52     public abstract void release();
53 
54     /**
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.
57      */
58     @NonNull
getAllSupportedProperties()59     public abstract int[] getAllSupportedProperties();
60 
61     /**
62      * Checks if given {@code propId} is supported.
63      */
isSupportedProperty(int propId)64     public boolean isSupportedProperty(int propId) {
65         for (int supported: getAllSupportedProperties()) {
66             if (propId == supported) {
67                 return true;
68             }
69         }
70         return false;
71     }
72 
73     /**
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      */
takeProperties(@onNull Collection<VehiclePropConfig> properties)82     public abstract void takeProperties(@NonNull Collection<VehiclePropConfig> properties);
83 
84     /**
85      * Handles property changes from HAL.
86      */
onHalEvents(List<VehiclePropValue> values)87     public abstract void onHalEvents(List<VehiclePropValue> values);
88 
89     /**
90      * Handles errors and pass error codes  when setting properties.
91      */
onPropertySetError(int property, int area, int errorCode)92     public void onPropertySetError(int property, int area, int errorCode) {
93         Log.d(MY_TAG, getClass().getSimpleName() + ".onPropertySetError(): property=" + property
94                 + ", area=" + area + " , errorCode = " + errorCode);
95     }
96 
dump(PrintWriter writer)97     public abstract void dump(PrintWriter writer);
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          */
create(int... mgrToHalPropIds)114         static ManagerToHalPropIdMap create(int... mgrToHalPropIds) {
115             return new ManagerToHalPropIdMap(BidirectionalSparseIntArray.create(mgrToHalPropIds));
116         }
117 
ManagerToHalPropIdMap(BidirectionalSparseIntArray map)118         private ManagerToHalPropIdMap(BidirectionalSparseIntArray map) {
119             mMap = map;
120         }
121 
getHalPropId(int managerPropId)122         int getHalPropId(int managerPropId) {
123             return mMap.getValue(managerPropId, NOT_SUPPORTED_PROPERTY);
124         }
125 
getManagerPropId(int halPropId)126         int getManagerPropId(int halPropId) {
127             return mMap.getKey(halPropId, NOT_SUPPORTED_PROPERTY);
128         }
129     }
130 }
131