Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 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 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 18 | import static com.android.car.CarServiceUtils.toByteArray; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 19 | import static java.lang.Integer.toHexString; |
| 20 | |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 21 | import android.car.VehicleAreaType; |
Pavel Maltsev | 1bfbaef | 2016-07-25 14:23:51 -0700 | [diff] [blame] | 22 | import android.car.VehicleZoneUtil; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 23 | import android.car.hardware.CarPropertyConfig; |
| 24 | import android.car.hardware.CarPropertyValue; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 25 | import android.hardware.vehicle.V2_0.VehicleArea; |
| 26 | import android.hardware.vehicle.V2_0.VehicleAreaConfig; |
| 27 | import android.hardware.vehicle.V2_0.VehiclePropConfig; |
| 28 | import android.hardware.vehicle.V2_0.VehiclePropValue; |
| 29 | import android.hardware.vehicle.V2_0.VehiclePropertyType; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 30 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 31 | import java.util.Collections; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 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 */ |
| 40 | private CarPropertyUtils() {} |
| 41 | |
| 42 | /** Converts {@link VehiclePropValue} to {@link CarPropertyValue} */ |
| 43 | static CarPropertyValue<?> toCarPropertyValue( |
| 44 | VehiclePropValue halValue, int propertyId) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 45 | Class<?> clazz = getJavaClass(halValue.prop & VehiclePropertyType.MASK); |
| 46 | int areaId = halValue.areaId; |
| 47 | VehiclePropValue.RawValue v = halValue.value; |
| 48 | |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 49 | if (Boolean.class == clazz) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 50 | return new CarPropertyValue<>(propertyId, areaId, v.int32Values.get(0) == 1); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 51 | } else if (String.class == clazz) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 52 | return new CarPropertyValue<>(propertyId, areaId, v.stringValue); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 53 | } else if (Long.class == clazz) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 54 | return new CarPropertyValue<>(propertyId, areaId, v.int64Values.get(0)); |
Pavel Maltsev | 437ab41 | 2016-08-15 15:41:06 -0700 | [diff] [blame] | 55 | } else if (byte[].class == clazz) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 56 | byte[] halData = toByteArray(v.bytes); |
Pavel Maltsev | 437ab41 | 2016-08-15 15:41:06 -0700 | [diff] [blame] | 57 | return new CarPropertyValue<>(propertyId, areaId, halData); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 58 | } else /* All list properties */ { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 59 | Object[] values = getRawValueList(clazz, v).toArray(); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 60 | return new CarPropertyValue<>(propertyId, areaId, |
| 61 | values.length == 1 ? values[0] : values); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** Converts {@link CarPropertyValue} to {@link VehiclePropValue} */ |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 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; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 71 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 72 | Object o = carProp.getValue(); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 73 | |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 74 | if (o instanceof Boolean) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 75 | v.int32Values.add(((Boolean )o) ? 1 : 0); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 76 | } else if (o instanceof Integer) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 77 | v.int32Values.add((Integer) o); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 78 | } else if (o instanceof Float) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 79 | v.floatValues.add((Float) o); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 80 | } else if (o instanceof Integer[]) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 81 | Collections.addAll(v.int32Values, (Integer[]) o); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 82 | } else if (o instanceof Float[]) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 83 | Collections.addAll(v.floatValues, (Float[]) o); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 84 | } else if (o instanceof String) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 85 | v.stringValue = (String) o; |
Pavel Maltsev | 437ab41 | 2016-08-15 15:41:06 -0700 | [diff] [blame] | 86 | } else if (o instanceof byte[]) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 87 | for (byte b : (byte[]) o) { |
| 88 | v.bytes.add(b); |
| 89 | } |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 90 | } else { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 91 | throw new IllegalArgumentException("Unexpected type in: " + carProp); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 92 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 93 | |
| 94 | return vehicleProp; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Converts {@link VehiclePropConfig} to {@link CarPropertyConfig}. |
| 99 | */ |
| 100 | static CarPropertyConfig<?> toCarPropertyConfig(VehiclePropConfig p, int propertyId) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 101 | int[] areas = VehicleZoneUtil.listAllZones(p.supportedAreas); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 102 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 103 | int areaType = getVehicleAreaType(p.prop & VehicleArea.MASK); |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 104 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 105 | Class<?> clazz = getJavaClass(p.prop & VehiclePropertyType.MASK); |
Pavel Maltsev | 437ab41 | 2016-08-15 15:41:06 -0700 | [diff] [blame] | 106 | if (clazz == Boolean.class || clazz == byte[].class || clazz == String.class) { |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 107 | return CarPropertyConfig |
| 108 | .newBuilder(clazz, propertyId, areaType, /* capacity */ 1) |
| 109 | .addAreas(areas) |
| 110 | .build(); |
| 111 | } else { |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 112 | CarPropertyConfig.Builder builder = CarPropertyConfig |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 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 | } |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 125 | } |
| 126 | return builder.build(); |
| 127 | } |
| 128 | } |
| 129 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 130 | private static @VehicleAreaType.VehicleAreaTypeValue int getVehicleAreaType(int halArea) { |
| 131 | switch (halArea) { |
| 132 | case VehicleArea.GLOBAL: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 133 | return VehicleAreaType.VEHICLE_AREA_TYPE_NONE; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 134 | case VehicleArea.ZONE: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 135 | return VehicleAreaType.VEHICLE_AREA_TYPE_ZONE; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 136 | case VehicleArea.SEAT: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 137 | return VehicleAreaType.VEHICLE_AREA_TYPE_SEAT; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 138 | case VehicleArea.DOOR: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 139 | return VehicleAreaType.VEHICLE_AREA_TYPE_DOOR; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 140 | case VehicleArea.WINDOW: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 141 | return VehicleAreaType.VEHICLE_AREA_TYPE_WINDOW; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 142 | case VehicleArea.MIRROR: |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 143 | return VehicleAreaType.VEHICLE_AREA_TYPE_MIRROR; |
| 144 | default: |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 145 | throw new RuntimeException("Unsupported area type " + halArea); |
Vitalii Tomkiv | 5f537ee | 2016-10-11 14:26:44 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 149 | private static Class<?> getJavaClass(int halType) { |
| 150 | switch (halType) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 151 | case VehiclePropertyType.BOOLEAN: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 152 | return Boolean.class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 153 | case VehiclePropertyType.FLOAT: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 154 | return Float.class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 155 | case VehiclePropertyType.INT32: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 156 | return Integer.class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 157 | case VehiclePropertyType.INT32_VEC: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 158 | return Integer[].class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 159 | case VehiclePropertyType.FLOAT_VEC: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 160 | return Float[].class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 161 | case VehiclePropertyType.STRING: |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 162 | return String.class; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 163 | case VehiclePropertyType.BYTES: |
Pavel Maltsev | 437ab41 | 2016-08-15 15:41:06 -0700 | [diff] [blame] | 164 | return byte[].class; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 165 | default: |
| 166 | throw new IllegalArgumentException("Unexpected type: " + toHexString(halType)); |
| 167 | } |
| 168 | } |
| 169 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 170 | private static List getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value) { |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 171 | if (classMatched(Float.class, clazz)) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 172 | return value.floatValues; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 173 | } else if (classMatched(Integer.class, clazz)) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 174 | return value.int32Values; |
Steve Paik | fb5f623 | 2016-03-28 10:31:25 -0700 | [diff] [blame] | 175 | } else { |
| 176 | throw new IllegalArgumentException("Unexpected type: " + clazz); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private static boolean classMatched(Class<?> class1, Class<?> class2) { |
| 181 | return class1 == class2 || class1.getComponentType() == class2; |
| 182 | } |
| 183 | } |