• 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 
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.Slog;
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     static final 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         Slog.d(MY_TAG, getClass().getSimpleName() + ".onPropertySetError(): property=" + property
94                 + ", area=" + area + " , errorCode = " + errorCode);
95     }
96 
97     /**
98      * Dumps HAL service related info to the writer passed as parameter.
99      */
dump(PrintWriter writer)100     public abstract void dump(PrintWriter writer);
101 
102     /**
103      * Helper class that maintains bi-directional mapping between manager's property
104      * Id (public or system API) and vehicle HAL property Id.
105      *
106      * <p>This class is supposed to be immutable. Use {@link #create(int[])} factory method to
107      * instantiate this class.
108      */
109     static class ManagerToHalPropIdMap {
110         private final BidirectionalSparseIntArray mMap;
111 
112         /**
113          * Creates {@link ManagerToHalPropIdMap} for provided [manager prop Id, hal prop Id] pairs.
114          *
115          * <p> The input array should have an odd number of elements.
116          */
create(int... mgrToHalPropIds)117         static ManagerToHalPropIdMap create(int... mgrToHalPropIds) {
118             return new ManagerToHalPropIdMap(BidirectionalSparseIntArray.create(mgrToHalPropIds));
119         }
120 
ManagerToHalPropIdMap(BidirectionalSparseIntArray map)121         private ManagerToHalPropIdMap(BidirectionalSparseIntArray map) {
122             mMap = map;
123         }
124 
getHalPropId(int managerPropId)125         int getHalPropId(int managerPropId) {
126             return mMap.getValue(managerPropId, NOT_SUPPORTED_PROPERTY);
127         }
128 
getManagerPropId(int halPropId)129         int getManagerPropId(int halPropId) {
130             return mMap.getKey(halPropId, NOT_SUPPORTED_PROPERTY);
131         }
132     }
133 }
134