• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.car.hal;
17 
18 import static com.android.car.CarServiceUtils.toByteArray;
19 
20 import static java.lang.Integer.toHexString;
21 
22 import android.car.VehicleAreaType;
23 import android.car.hardware.CarPropertyConfig;
24 import android.car.hardware.CarPropertyValue;
25 import android.hardware.automotive.vehicle.V2_0.VehicleArea;
26 import android.hardware.automotive.vehicle.V2_0.VehicleAreaConfig;
27 import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
28 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
29 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyType;
30 
31 import java.util.Collections;
32 import java.util.List;
33 
34 /**
35  * Utility functions to work with {@link CarPropertyConfig} and {@link CarPropertyValue}
36  */
37 /*package*/ final class CarPropertyUtils {
38 
39     /* Utility class has no public constructor */
CarPropertyUtils()40     private CarPropertyUtils() {}
41 
42     /** Converts {@link VehiclePropValue} to {@link CarPropertyValue} */
toCarPropertyValue( VehiclePropValue halValue, int propertyId)43     static CarPropertyValue<?> toCarPropertyValue(
44             VehiclePropValue halValue, int propertyId) {
45         Class<?> clazz = getJavaClass(halValue.prop & VehiclePropertyType.MASK);
46         int areaId = halValue.areaId;
47         int status = halValue.status;
48         long timestamp = halValue.timestamp;
49         VehiclePropValue.RawValue v = halValue.value;
50 
51         if (Boolean.class == clazz) {
52             return new CarPropertyValue<>(propertyId, areaId, status, timestamp,
53                                           v.int32Values.get(0) == 1);
54         } else if (Boolean[].class == clazz) {
55             Boolean[] values = new Boolean[v.int32Values.size()];
56             for (int i = 0; i < values.length; i++) {
57                 values[i] = v.int32Values.get(i) == 1;
58             }
59             return new CarPropertyValue<>(propertyId, areaId, status, timestamp, values);
60         } else if (String.class == clazz) {
61             return new CarPropertyValue<>(propertyId, areaId, status, timestamp, v.stringValue);
62         } else if (byte[].class == clazz) {
63             byte[] halData = toByteArray(v.bytes);
64             return new CarPropertyValue<>(propertyId, areaId, status, timestamp, halData);
65         } else if (Long[].class == clazz) {
66             Long[] values = new Long[v.int64Values.size()];
67             for (int i = 0; i < values.length; i++) {
68                 values[i] = v.int64Values.get(i);
69             }
70             return new CarPropertyValue<>(propertyId, areaId, status, timestamp, values);
71         } else /* All list properties */ {
72             Object[] values = getRawValueList(clazz, v).toArray();
73             return new CarPropertyValue<>(propertyId, areaId, status, timestamp,
74                     values.length == 1 ? values[0] : values);
75         }
76     }
77 
78     /** Converts {@link CarPropertyValue} to {@link VehiclePropValue} */
toVehiclePropValue(CarPropertyValue carProp, int halPropId)79     static VehiclePropValue toVehiclePropValue(CarPropertyValue carProp, int halPropId) {
80         VehiclePropValue vehicleProp = new VehiclePropValue();
81         vehicleProp.prop = halPropId;
82         vehicleProp.areaId = carProp.getAreaId();
83         VehiclePropValue.RawValue v = vehicleProp.value;
84 
85         Object o = carProp.getValue();
86 
87         if (o instanceof Boolean) {
88             v.int32Values.add(((Boolean) o) ? 1 : 0);
89         } else if (o instanceof Boolean[]) {
90             for (Boolean b : (Boolean[]) o) {
91                 v.int32Values.add(((Boolean) o) ? 1 : 0);
92             }
93         } else if (o instanceof Integer) {
94             v.int32Values.add((Integer) o);
95         } else if (o instanceof Integer[]) {
96             Collections.addAll(v.int32Values, (Integer[]) o);
97         } else if (o instanceof Float) {
98             v.floatValues.add((Float) o);
99         } else if (o instanceof Float[]) {
100             Collections.addAll(v.floatValues, (Float[]) o);
101         } else if (o instanceof Long) {
102             v.int64Values.add((Long) o);
103         } else if (o instanceof Long[]) {
104             Collections.addAll(v.int64Values, (Long[]) o);
105         } else if (o instanceof String) {
106             v.stringValue = (String) o;
107         } else if (o instanceof byte[]) {
108             for (byte b : (byte[]) o) {
109                 v.bytes.add(b);
110             }
111         } else {
112             throw new IllegalArgumentException("Unexpected type in: " + carProp);
113         }
114 
115         return vehicleProp;
116     }
117 
118     /**
119      * Converts {@link VehiclePropConfig} to {@link CarPropertyConfig}.
120      */
toCarPropertyConfig(VehiclePropConfig p, int propertyId)121     static CarPropertyConfig<?> toCarPropertyConfig(VehiclePropConfig p, int propertyId) {
122         int areaType = getVehicleAreaType(p.prop & VehicleArea.MASK);
123         // Create list of areaIds for this property
124         int[] areas = new int[p.areaConfigs.size()];
125         for (int i=0; i<p.areaConfigs.size(); i++) {
126             areas[i] = p.areaConfigs.get(i).areaId;
127         }
128 
129         Class<?> clazz = getJavaClass(p.prop & VehiclePropertyType.MASK);
130         if (p.areaConfigs.isEmpty()) {
131             return CarPropertyConfig
132                     .newBuilder(clazz, propertyId, areaType, /* capacity */ 1)
133                     .addAreas(areas)
134                     .setAccess(p.access)
135                     .setChangeMode(p.changeMode)
136                     .setConfigArray(p.configArray)
137                     .setConfigString(p.configString)
138                     .setMaxSampleRate(p.maxSampleRate)
139                     .setMinSampleRate(p.minSampleRate)
140                     .build();
141         } else {
142             CarPropertyConfig.Builder builder = CarPropertyConfig
143                     .newBuilder(clazz, propertyId, areaType, /* capacity */ p.areaConfigs.size())
144                     .setAccess(p.access)
145                     .setChangeMode(p.changeMode)
146                     .setConfigArray(p.configArray)
147                     .setConfigString(p.configString)
148                     .setMaxSampleRate(p.maxSampleRate)
149                     .setMinSampleRate(p.minSampleRate);
150 
151             for (VehicleAreaConfig area : p.areaConfigs) {
152                 if (classMatched(Integer.class, clazz)) {
153                     builder.addAreaConfig(area.areaId, area.minInt32Value, area.maxInt32Value);
154                 } else if (classMatched(Float.class, clazz)) {
155                     builder.addAreaConfig(area.areaId, area.minFloatValue, area.maxFloatValue);
156                 } else if (classMatched(Long.class, clazz)) {
157                     builder.addAreaConfig(area.areaId, area.minInt64Value, area.maxInt64Value);
158                 } else if (classMatched(Boolean.class, clazz) ||
159                            classMatched(Float[].class, clazz) ||
160                            classMatched(Integer[].class, clazz) ||
161                            classMatched(Long[].class, clazz) ||
162                            classMatched(String.class, clazz) ||
163                            classMatched(byte[].class, clazz) ||
164                            classMatched(Object.class, clazz)) {
165                     // These property types do not have min/max values
166                     builder.addArea(area.areaId);
167                 } else {
168                     throw new IllegalArgumentException("Unexpected type: " + clazz);
169                 }
170             }
171 
172             return builder.build();
173         }
174     }
175 
getVehicleAreaType(int halArea)176     private static @VehicleAreaType.VehicleAreaTypeValue int getVehicleAreaType(int halArea) {
177         switch (halArea) {
178             case VehicleArea.GLOBAL:
179                 return VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
180             case VehicleArea.SEAT:
181                 return VehicleAreaType.VEHICLE_AREA_TYPE_SEAT;
182             case VehicleArea.DOOR:
183                 return VehicleAreaType.VEHICLE_AREA_TYPE_DOOR;
184             case VehicleArea.WINDOW:
185                 return VehicleAreaType.VEHICLE_AREA_TYPE_WINDOW;
186             case VehicleArea.MIRROR:
187                 return VehicleAreaType.VEHICLE_AREA_TYPE_MIRROR;
188             case VehicleArea.WHEEL:
189                 return VehicleAreaType.VEHICLE_AREA_TYPE_WHEEL;
190             default:
191                 throw new RuntimeException("Unsupported area type " + halArea);
192         }
193     }
194 
getJavaClass(int halType)195     private static Class<?> getJavaClass(int halType) {
196         switch (halType) {
197             case VehiclePropertyType.BOOLEAN:
198                 return Boolean.class;
199             case VehiclePropertyType.FLOAT:
200                 return Float.class;
201             case VehiclePropertyType.INT32:
202                 return Integer.class;
203             case VehiclePropertyType.INT64:
204                 return Long.class;
205             case VehiclePropertyType.FLOAT_VEC:
206                 return Float[].class;
207             case VehiclePropertyType.INT32_VEC:
208                 return Integer[].class;
209             case VehiclePropertyType.INT64_VEC:
210                 return Long[].class;
211             case VehiclePropertyType.STRING:
212                 return String.class;
213             case VehiclePropertyType.BYTES:
214                 return byte[].class;
215             case VehiclePropertyType.MIXED:
216                 return Object.class;
217             default:
218                 throw new IllegalArgumentException("Unexpected type: " + toHexString(halType));
219         }
220     }
221 
getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value)222     private static List getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value) {
223         if (classMatched(Float.class, clazz) || classMatched(Float[].class, clazz)) {
224             return value.floatValues;
225         } else if (classMatched(Integer.class, clazz) || classMatched(Integer[].class, clazz)) {
226             return value.int32Values;
227         } else if (classMatched(Long.class, clazz) || classMatched(Long[].class, clazz)) {
228             return value.int64Values;
229         } else {
230             throw new IllegalArgumentException("Unexpected type: " + clazz);
231         }
232     }
233 
classMatched(Class<?> class1, Class<?> class2)234     private static boolean classMatched(Class<?> class1, Class<?> class2) {
235         return class1 == class2 || class1.getComponentType() == class2;
236     }
237 }
238