blob: 00cde2899c9539d725402b31b34e0a488bec2747 [file] [log] [blame]
Steve Paikfb5f6232016-03-28 10:31:25 -07001/*
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 */
16package com.android.car.hal;
17
Pavel Maltsev0d07c762016-11-03 16:40:15 -070018import static com.android.car.CarServiceUtils.toByteArray;
Steve Paikfb5f6232016-03-28 10:31:25 -070019import static java.lang.Integer.toHexString;
20
Steve Paikfb5f6232016-03-28 10:31:25 -070021import android.car.VehicleAreaType;
Pavel Maltsev1bfbaef2016-07-25 14:23:51 -070022import android.car.VehicleZoneUtil;
Steve Paikfb5f6232016-03-28 10:31:25 -070023import android.car.hardware.CarPropertyConfig;
24import android.car.hardware.CarPropertyValue;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070025import android.hardware.vehicle.V2_0.VehicleArea;
26import android.hardware.vehicle.V2_0.VehicleAreaConfig;
27import android.hardware.vehicle.V2_0.VehiclePropConfig;
28import android.hardware.vehicle.V2_0.VehiclePropValue;
29import android.hardware.vehicle.V2_0.VehiclePropertyType;
Steve Paikfb5f6232016-03-28 10:31:25 -070030
Pavel Maltsev0d07c762016-11-03 16:40:15 -070031import java.util.Collections;
Steve Paikfb5f6232016-03-28 10:31:25 -070032import 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 Maltsev0d07c762016-11-03 16:40:15 -070045 Class<?> clazz = getJavaClass(halValue.prop & VehiclePropertyType.MASK);
46 int areaId = halValue.areaId;
47 VehiclePropValue.RawValue v = halValue.value;
48
Steve Paikfb5f6232016-03-28 10:31:25 -070049 if (Boolean.class == clazz) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070050 return new CarPropertyValue<>(propertyId, areaId, v.int32Values.get(0) == 1);
Steve Paikfb5f6232016-03-28 10:31:25 -070051 } else if (String.class == clazz) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070052 return new CarPropertyValue<>(propertyId, areaId, v.stringValue);
Steve Paikfb5f6232016-03-28 10:31:25 -070053 } else if (Long.class == clazz) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070054 return new CarPropertyValue<>(propertyId, areaId, v.int64Values.get(0));
Pavel Maltsev437ab412016-08-15 15:41:06 -070055 } else if (byte[].class == clazz) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070056 byte[] halData = toByteArray(v.bytes);
Pavel Maltsev437ab412016-08-15 15:41:06 -070057 return new CarPropertyValue<>(propertyId, areaId, halData);
Steve Paikfb5f6232016-03-28 10:31:25 -070058 } else /* All list properties */ {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070059 Object[] values = getRawValueList(clazz, v).toArray();
Steve Paikfb5f6232016-03-28 10:31:25 -070060 return new CarPropertyValue<>(propertyId, areaId,
61 values.length == 1 ? values[0] : values);
62 }
63 }
64
65 /** Converts {@link CarPropertyValue} to {@link VehiclePropValue} */
Pavel Maltsev0d07c762016-11-03 16:40:15 -070066 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 Paikfb5f6232016-03-28 10:31:25 -070071
Pavel Maltsev0d07c762016-11-03 16:40:15 -070072 Object o = carProp.getValue();
Steve Paikfb5f6232016-03-28 10:31:25 -070073
Steve Paikfb5f6232016-03-28 10:31:25 -070074 if (o instanceof Boolean) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070075 v.int32Values.add(((Boolean )o) ? 1 : 0);
Steve Paikfb5f6232016-03-28 10:31:25 -070076 } else if (o instanceof Integer) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070077 v.int32Values.add((Integer) o);
Steve Paikfb5f6232016-03-28 10:31:25 -070078 } else if (o instanceof Float) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070079 v.floatValues.add((Float) o);
Steve Paikfb5f6232016-03-28 10:31:25 -070080 } else if (o instanceof Integer[]) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070081 Collections.addAll(v.int32Values, (Integer[]) o);
Steve Paikfb5f6232016-03-28 10:31:25 -070082 } else if (o instanceof Float[]) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070083 Collections.addAll(v.floatValues, (Float[]) o);
Steve Paikfb5f6232016-03-28 10:31:25 -070084 } else if (o instanceof String) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070085 v.stringValue = (String) o;
Pavel Maltsev437ab412016-08-15 15:41:06 -070086 } else if (o instanceof byte[]) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070087 for (byte b : (byte[]) o) {
88 v.bytes.add(b);
89 }
Steve Paikfb5f6232016-03-28 10:31:25 -070090 } else {
Pavel Maltsev0d07c762016-11-03 16:40:15 -070091 throw new IllegalArgumentException("Unexpected type in: " + carProp);
Steve Paikfb5f6232016-03-28 10:31:25 -070092 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -070093
94 return vehicleProp;
Steve Paikfb5f6232016-03-28 10:31:25 -070095 }
96
97 /**
98 * Converts {@link VehiclePropConfig} to {@link CarPropertyConfig}.
99 */
100 static CarPropertyConfig<?> toCarPropertyConfig(VehiclePropConfig p, int propertyId) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700101 int[] areas = VehicleZoneUtil.listAllZones(p.supportedAreas);
Steve Paikfb5f6232016-03-28 10:31:25 -0700102
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700103 int areaType = getVehicleAreaType(p.prop & VehicleArea.MASK);
Steve Paikfb5f6232016-03-28 10:31:25 -0700104
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700105 Class<?> clazz = getJavaClass(p.prop & VehiclePropertyType.MASK);
Pavel Maltsev437ab412016-08-15 15:41:06 -0700106 if (clazz == Boolean.class || clazz == byte[].class || clazz == String.class) {
Steve Paikfb5f6232016-03-28 10:31:25 -0700107 return CarPropertyConfig
108 .newBuilder(clazz, propertyId, areaType, /* capacity */ 1)
109 .addAreas(areas)
110 .build();
111 } else {
Steve Paikfb5f6232016-03-28 10:31:25 -0700112 CarPropertyConfig.Builder builder = CarPropertyConfig
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700113 .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 Paikfb5f6232016-03-28 10:31:25 -0700125 }
126 return builder.build();
127 }
128 }
129
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700130 private static @VehicleAreaType.VehicleAreaTypeValue int getVehicleAreaType(int halArea) {
131 switch (halArea) {
132 case VehicleArea.GLOBAL:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700133 return VehicleAreaType.VEHICLE_AREA_TYPE_NONE;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700134 case VehicleArea.ZONE:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700135 return VehicleAreaType.VEHICLE_AREA_TYPE_ZONE;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700136 case VehicleArea.SEAT:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700137 return VehicleAreaType.VEHICLE_AREA_TYPE_SEAT;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700138 case VehicleArea.DOOR:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700139 return VehicleAreaType.VEHICLE_AREA_TYPE_DOOR;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700140 case VehicleArea.WINDOW:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700141 return VehicleAreaType.VEHICLE_AREA_TYPE_WINDOW;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700142 case VehicleArea.MIRROR:
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700143 return VehicleAreaType.VEHICLE_AREA_TYPE_MIRROR;
144 default:
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700145 throw new RuntimeException("Unsupported area type " + halArea);
Vitalii Tomkiv5f537ee2016-10-11 14:26:44 -0700146 }
147 }
148
Steve Paikfb5f6232016-03-28 10:31:25 -0700149 private static Class<?> getJavaClass(int halType) {
150 switch (halType) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700151 case VehiclePropertyType.BOOLEAN:
Steve Paikfb5f6232016-03-28 10:31:25 -0700152 return Boolean.class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700153 case VehiclePropertyType.FLOAT:
Steve Paikfb5f6232016-03-28 10:31:25 -0700154 return Float.class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700155 case VehiclePropertyType.INT32:
Steve Paikfb5f6232016-03-28 10:31:25 -0700156 return Integer.class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700157 case VehiclePropertyType.INT32_VEC:
Steve Paikfb5f6232016-03-28 10:31:25 -0700158 return Integer[].class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700159 case VehiclePropertyType.FLOAT_VEC:
Steve Paikfb5f6232016-03-28 10:31:25 -0700160 return Float[].class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700161 case VehiclePropertyType.STRING:
Steve Paikfb5f6232016-03-28 10:31:25 -0700162 return String.class;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700163 case VehiclePropertyType.BYTES:
Pavel Maltsev437ab412016-08-15 15:41:06 -0700164 return byte[].class;
Steve Paikfb5f6232016-03-28 10:31:25 -0700165 default:
166 throw new IllegalArgumentException("Unexpected type: " + toHexString(halType));
167 }
168 }
169
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700170 private static List getRawValueList(Class<?> clazz, VehiclePropValue.RawValue value) {
Steve Paikfb5f6232016-03-28 10:31:25 -0700171 if (classMatched(Float.class, clazz)) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700172 return value.floatValues;
Steve Paikfb5f6232016-03-28 10:31:25 -0700173 } else if (classMatched(Integer.class, clazz)) {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700174 return value.int32Values;
Steve Paikfb5f6232016-03-28 10:31:25 -0700175 } 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}