• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.car.VehicleAreaType;
20 import android.car.hardware.CarPropertyConfig;
21 import android.hardware.automotive.vehicle.VehicleArea;
22 import android.hardware.automotive.vehicle.VehiclePropertyType;
23 
24 import java.util.ArrayList;
25 
26 /**
27  * HalPropConfig represents a vehicle property config.
28  */
29 public abstract class HalPropConfig {
30 
31     private static final int[] DEFAULT_AREA_IDS = {VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL};
32 
33     /**
34      * Get the property ID.
35      */
getPropId()36     public abstract int getPropId();
37 
38     /**
39      * Get the access mode.
40      */
getAccess()41     public abstract int getAccess();
42 
43     /**
44      * Get the change mode.
45      */
getChangeMode()46     public abstract int getChangeMode();
47 
48     /**
49      * Get the area configs.
50      */
getAreaConfigs()51     public abstract HalAreaConfig[] getAreaConfigs();
52 
53     /**
54      * Get the config array.
55      */
getConfigArray()56     public abstract int[] getConfigArray();
57 
58     /**
59      * Get the config string.
60      */
getConfigString()61     public abstract String getConfigString();
62 
63     /**
64      * Get the min sample rate.
65      */
getMinSampleRate()66     public abstract float getMinSampleRate();
67 
68     /**
69      * Get the max sample rate.
70      */
getMaxSampleRate()71     public abstract float getMaxSampleRate();
72 
73     /**
74      * Converts to AIDL or HIDL VehiclePropConfig.
75      */
toVehiclePropConfig()76     public abstract Object toVehiclePropConfig();
77 
78     /**
79      * Converts {@link HalPropConfig} to {@link CarPropertyConfig}.
80      *
81      * @param mgrPropertyId The Property ID used by Car Property Manager, different from the
82      *      property ID used by VHAL.
83      */
toCarPropertyConfig(int mgrPropertyId)84     public CarPropertyConfig<?> toCarPropertyConfig(int mgrPropertyId) {
85         int propId = getPropId();
86         int areaType = getVehicleAreaType(propId & VehicleArea.MASK);
87 
88         Class<?> clazz = CarPropertyUtils.getJavaClass(propId & VehiclePropertyType.MASK);
89         float maxSampleRate = 0f;
90         float minSampleRate = 0f;
91         if (getChangeMode() != CarPropertyConfig.VEHICLE_PROPERTY_CHANGE_MODE_STATIC) {
92             maxSampleRate = getMaxSampleRate();
93             minSampleRate = getMinSampleRate();
94         }
95 
96         int[] configIntArray = getConfigArray();
97         ArrayList<Integer> configArray = new ArrayList<>(configIntArray.length);
98         for (int i = 0; i < configIntArray.length; i++) {
99             configArray.add(configIntArray[i]);
100         }
101         HalAreaConfig[] areaConfigs = getAreaConfigs();
102         if (areaConfigs.length == 0) {
103             return CarPropertyConfig
104                     .newBuilder(clazz, mgrPropertyId, areaType, /* capacity= */ 1)
105                     .addAreas(DEFAULT_AREA_IDS)
106                     .setAccess(getAccess())
107                     .setChangeMode(getChangeMode())
108                     .setConfigArray(configArray)
109                     .setConfigString(getConfigString())
110                     .setMaxSampleRate(maxSampleRate)
111                     .setMinSampleRate(minSampleRate)
112                     .build();
113         } else {
114             CarPropertyConfig.Builder builder = CarPropertyConfig
115                     .newBuilder(clazz, mgrPropertyId, areaType, /* capacity= */ areaConfigs.length)
116                     .setAccess(getAccess())
117                     .setChangeMode(getChangeMode())
118                     .setConfigArray(configArray)
119                     .setConfigString(getConfigString())
120                     .setMaxSampleRate(maxSampleRate)
121                     .setMinSampleRate(minSampleRate);
122 
123             for (HalAreaConfig area : areaConfigs) {
124                 int areaId = area.getAreaId();
125                 if (classMatched(Integer.class, clazz)) {
126                     builder.addAreaConfig(areaId, area.getMinInt32Value(), area.getMaxInt32Value());
127                 } else if (classMatched(Float.class, clazz)) {
128                     builder.addAreaConfig(areaId, area.getMinFloatValue(), area.getMaxFloatValue());
129                 } else if (classMatched(Long.class, clazz)) {
130                     builder.addAreaConfig(areaId, area.getMinInt64Value(), area.getMaxInt64Value());
131                 } else if (classMatched(Boolean.class, clazz)
132                         || classMatched(Float[].class, clazz)
133                         || classMatched(Integer[].class, clazz)
134                         || classMatched(Long[].class, clazz)
135                         || classMatched(String.class, clazz)
136                         || classMatched(byte[].class, clazz)
137                         || classMatched(Object[].class, clazz)) {
138                     // These property types do not have min/max values
139                     builder.addArea(areaId);
140                 } else {
141                     throw new IllegalArgumentException("Unexpected type: " + clazz);
142                 }
143             }
144             return builder.build();
145         }
146     }
147 
getVehicleAreaType(int halArea)148     private static @VehicleAreaType.VehicleAreaTypeValue int getVehicleAreaType(int halArea) {
149         switch (halArea) {
150             case VehicleArea.GLOBAL:
151                 return VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
152             case VehicleArea.SEAT:
153                 return VehicleAreaType.VEHICLE_AREA_TYPE_SEAT;
154             case VehicleArea.DOOR:
155                 return VehicleAreaType.VEHICLE_AREA_TYPE_DOOR;
156             case VehicleArea.WINDOW:
157                 return VehicleAreaType.VEHICLE_AREA_TYPE_WINDOW;
158             case VehicleArea.MIRROR:
159                 return VehicleAreaType.VEHICLE_AREA_TYPE_MIRROR;
160             case VehicleArea.WHEEL:
161                 return VehicleAreaType.VEHICLE_AREA_TYPE_WHEEL;
162             default:
163                 throw new RuntimeException("Unsupported area type " + halArea);
164         }
165     }
166 
classMatched(Class<?> class1, Class<?> class2)167     private static boolean classMatched(Class<?> class1, Class<?> class2) {
168         return class1 == class2 || class1.getComponentType() == class2;
169     }
170 
171 }
172