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 import static java.lang.Integer.toHexString;
20 
21 import android.car.VehicleAreaType;
22 import android.car.VehicleZoneUtil;
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         VehiclePropValue.RawValue v = halValue.value;
48 
49         if (Boolean.class == clazz) {
50             return new CarPropertyValue<>(propertyId, areaId, v.int32Values.get(0) == 1);
51         } else if (String.class == clazz) {
52             return new CarPropertyValue<>(propertyId, areaId, v.stringValue);
53         } else if (Long.class == clazz) {
54             return new CarPropertyValue<>(propertyId, areaId, v.int64Values.get(0));
55         } else if (byte[].class == clazz) {
56             byte[] halData = toByteArray(v.bytes);
57             return new CarPropertyValue<>(propertyId, areaId, halData);
58         } else /* All list properties */ {
59             Object[] values = getRawValueList(clazz, v).toArray();
60             return new CarPropertyValue<>(propertyId, areaId,
61                     values.length == 1 ? values[0] : values);
62         }
63     }
64 
65     /** Converts {@link CarPropertyValue} to {@link VehiclePropValue} */
toVehiclePropValue(CarPropertyValue carProp, int halPropId)66     static VehiclePropValue toVehiclePropValue(CarPropertyValue carProp, int halPropId) {
67         VehiclePropValue vehicleProp = new VehiclePropValue();
68         vehicleProp.prop = halPropId;
69         vehicleProp.areaId = carProp.getAreaId();
70         VehiclePropValue.RawValue v = vehicleProp.value;
71 
72         Object o = carProp.getValue();
73 
74         if (o instanceof Boolean) {
75             v.int32Values.add(((Boolean )o) ? 1 : 0);
76         } else if (o instanceof Integer) {
77             v.int32Values.add((Integer) o);
78         } else if (o instanceof Float) {
79             v.floatValues.add((Float) o);
80         } else if (o instanceof Integer[]) {
81             Collections.addAll(v.int32Values, (Integer[]) o);
82         } else if (o instanceof Float[]) {
83             Collections.addAll(v.floatValues, (Float[]) o);
84         } else if (o instanceof String) {
85             v.stringValue = (String) o;
86         } else if (o instanceof byte[]) {
87             for (byte b : (byte[]) o) {
88                 v.bytes.add(b);
89             }
90         } else {
91             throw new IllegalArgumentException("Unexpected type in: " + carProp);
92         }
93 
94         return vehicleProp;
95     }
96 
97     /**
98      * Converts {@link VehiclePropConfig} to {@link CarPropertyConfig}.
99      */
toCarPropertyConfig(VehiclePropConfig p, int propertyId)100     static CarPropertyConfig<?> toCarPropertyConfig(VehiclePropConfig p, int propertyId) {
101         int[] areas = VehicleZoneUtil.listAllZones(p.supportedAreas);
102 
103         int areaType = getVehicleAreaType(p.prop & VehicleArea.MASK);
104 
105         Class<?> clazz = getJavaClass(p.prop & VehiclePropertyType.MASK);
106         if (p.areaConfigs.isEmpty()) {
107             return CarPropertyConfig
108                     .newBuilder(clazz, propertyId, areaType, /* capacity */ 1)
109                     .addAreas(areas)
110                     .build();
111         } else {
112             CarPropertyConfig.Builder builder = CarPropertyConfig
113                     .newBuilder(clazz, propertyId, areaType, /* capacity */  p.areaConfigs.size());
114 
115             for (VehicleAreaConfig area : p.areaConfigs) {
116                 if (classMatched(Integer.class, clazz)) {
117                     builder.addAreaConfig(area.areaId, area.minInt32Value, area.maxInt32Value);
118                 } else if (classMatched(Float.class, clazz)) {
119                     builder.addAreaConfig(area.areaId, area.minFloatValue, area.maxFloatValue);
120                 } else if (classMatched(Long.class, clazz)) {
121                     builder.addAreaConfig(area.areaId, area.minInt64Value, area.maxInt64Value);
122                 } else {
123                     throw new IllegalArgumentException("Unexpected type: " + clazz);
124                 }
125             }
126             return builder.build();
127         }
128     }
129 
getVehicleAreaType(int halArea)130     private static @VehicleAreaType.VehicleAreaTypeValue int getVehicleAreaType(int halArea) {
131         switch (halArea) {
132             case VehicleArea.GLOBAL:
133                 return VehicleAreaType.VEHICLE_AREA_TYPE_NONE;
134             case VehicleArea.ZONE:
135                 return VehicleAreaType.VEHICLE_AREA_TYPE_ZONE;
136             case VehicleArea.SEAT:
137                 return VehicleAreaType.VEHICLE_AREA_TYPE_SEAT;
138             case VehicleArea.DOOR:
139                 return VehicleAreaType.VEHICLE_AREA_TYPE_DOOR;
140             case VehicleArea.WINDOW:
141                 return VehicleAreaType.VEHICLE_AREA_TYPE_WINDOW;
142             case VehicleArea.MIRROR:
143                 return VehicleAreaType.VEHICLE_AREA_TYPE_MIRROR;
144             default:
145                 throw new RuntimeException("Unsupported area type " + halArea);
146         }
147     }
148 
getJavaClass(int halType)149     private static Class<?> getJavaClass(int halType) {
150         switch (halType) {
151             case VehiclePropertyType.BOOLEAN:
152                 return Boolean.class;
153             case VehiclePropertyType.FLOAT:
154                 return Float.class;
155             case VehiclePropertyType.INT32:
156                 return Integer.class;
157             case VehiclePropertyType.INT32_VEC:
158                 return Integer[].class;
159             case VehiclePropertyType.FLOAT_VEC:
160                 return Float[].class;
161             case VehiclePropertyType.STRING:
162                 return String.class;
163             case VehiclePropertyType.BYTES:
164                 return byte[].class;
165             case VehiclePropertyType.COMPLEX:
166                 return Object.class;
167             default:
168                 throw new IllegalArgumentException("Unexpected type: " + toHexString(halType));
169         }
170     }
171 
getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value)172     private static List getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value) {
173         if (classMatched(Float.class, clazz)) {
174             return value.floatValues;
175         } else if (classMatched(Integer.class, clazz)) {
176             return value.int32Values;
177         } else {
178             throw new IllegalArgumentException("Unexpected type: " + clazz);
179         }
180     }
181 
classMatched(Class<?> class1, Class<?> class2)182     private static boolean classMatched(Class<?> class1, Class<?> class2) {
183         return class1 == class2 || class1.getComponentType() == class2;
184     }
185 }
186