Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.vehiclehal.test; |
| 18 | |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 19 | import android.car.hardware.CarPropertyValue; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 20 | import android.hardware.automotive.vehicle.V2_0.IVehicle; |
| 21 | import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; |
| 22 | import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 25 | |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 26 | import com.android.car.vehiclehal.VehiclePropValueBuilder; |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.NoSuchElementException; |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
| 31 | final class Utils { |
| 32 | private Utils() {} |
| 33 | |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 34 | private static final String TAG = concatTag(Utils.class); |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 35 | |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 36 | static String concatTag(Class clazz) { |
| 37 | return "VehicleHalTest." + clazz.getSimpleName(); |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static boolean isVhalPropertyAvailable(IVehicle vehicle, int prop) throws RemoteException { |
| 41 | return vehicle.getAllPropConfigs() |
| 42 | .stream() |
| 43 | .anyMatch((VehiclePropConfig config) -> config.prop == prop); |
| 44 | } |
| 45 | |
| 46 | static VehiclePropValue readVhalProperty( |
Enrico Granata | 15c4ec4 | 2017-02-24 14:13:05 -0800 | [diff] [blame] | 47 | IVehicle vehicle, |
| 48 | VehiclePropValue request, |
| 49 | java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { |
Andreas Gampe | 30450c9 | 2018-01-26 09:22:50 -0800 | [diff] [blame] | 50 | Objects.requireNonNull(vehicle); |
| 51 | Objects.requireNonNull(request); |
Enrico Granata | 15c4ec4 | 2017-02-24 14:13:05 -0800 | [diff] [blame] | 52 | VehiclePropValue vpv[] = new VehiclePropValue[] {null}; |
| 53 | try { |
| 54 | vehicle.get( |
| 55 | request, |
| 56 | (int status, VehiclePropValue propValue) -> { |
| 57 | if (f.apply(status, propValue)) { |
| 58 | vpv[0] = propValue; |
| 59 | } |
| 60 | }); |
| 61 | } catch (RemoteException e) { |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 62 | Log.w(TAG, "attempt to read VHAL property " + request + " caused RemoteException: ", e); |
Enrico Granata | 15c4ec4 | 2017-02-24 14:13:05 -0800 | [diff] [blame] | 63 | } |
| 64 | return vpv[0]; |
| 65 | } |
| 66 | |
| 67 | static VehiclePropValue readVhalProperty( |
| 68 | IVehicle vehicle, |
| 69 | int propertyId, |
| 70 | java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 71 | return readVhalProperty(vehicle, propertyId, 0, f); |
| 72 | } |
| 73 | |
| 74 | static VehiclePropValue readVhalProperty( |
| 75 | IVehicle vehicle, |
| 76 | int propertyId, |
| 77 | int areaId, |
| 78 | java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 79 | VehiclePropValue request = |
Enrico Granata | 15c4ec4 | 2017-02-24 14:13:05 -0800 | [diff] [blame] | 80 | VehiclePropValueBuilder.newBuilder(propertyId).setAreaId(areaId).build(); |
| 81 | return readVhalProperty(vehicle, request, f); |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 82 | } |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 83 | |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 84 | static IVehicle getVehicle() throws RemoteException { |
| 85 | IVehicle service; |
| 86 | try { |
Keun-young Park | 19ca132 | 2017-08-25 11:35:38 -0700 | [diff] [blame] | 87 | service = IVehicle.getService(); |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 88 | } catch (NoSuchElementException ex) { |
Keun-young Park | 00e2dc8 | 2017-08-25 11:51:15 -0700 | [diff] [blame] | 89 | throw new RuntimeException("Couldn't connect to vehicle@2.0", ex); |
Pavel Maltsev | 82c20cd | 2017-04-11 12:38:17 -0700 | [diff] [blame] | 90 | } |
| 91 | Log.d(TAG, "Connected to IVehicle service: " + service); |
| 92 | return service; |
| 93 | } |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Check the equality of two VehiclePropValue object ignoring timestamp and status. |
| 97 | * |
| 98 | * @param value1 |
| 99 | * @param value2 |
| 100 | * @return true if equal |
| 101 | */ |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 102 | static boolean areCarPropertyValuesEqual(final CarPropertyValue value1, |
| 103 | final CarPropertyValue value2) { |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 104 | return value1 == value2 |
| 105 | || value1 != null |
| 106 | && value2 != null |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 107 | && value1.getPropertyId() == value2.getPropertyId() |
| 108 | && value1.getAreaId() == value2.getAreaId() |
| 109 | && value1.getValue().equals(value2.getValue()); |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 110 | } |
Enrico Granata | e190f57 | 2017-01-13 17:56:50 -0800 | [diff] [blame] | 111 | } |