blob: 496b504897503ba168c2e199bc983ff406205c51 [file] [log] [blame]
Enrico Granatae190f572017-01-13 17:56:50 -08001/*
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
17package com.android.car.vehiclehal.test;
18
Chao Yanf2c109c2018-05-11 12:16:14 -070019import android.car.hardware.CarPropertyValue;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080020import android.hardware.automotive.vehicle.V2_0.IVehicle;
21import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
22import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
Enrico Granatae190f572017-01-13 17:56:50 -080023import android.os.RemoteException;
24import android.util.Log;
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070025
Enrico Granatae190f572017-01-13 17:56:50 -080026import com.android.car.vehiclehal.VehiclePropValueBuilder;
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070027
28import java.util.NoSuchElementException;
Enrico Granatae190f572017-01-13 17:56:50 -080029import java.util.Objects;
30
31final class Utils {
32 private Utils() {}
33
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070034 private static final String TAG = concatTag(Utils.class);
Enrico Granatae190f572017-01-13 17:56:50 -080035
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070036 static String concatTag(Class clazz) {
37 return "VehicleHalTest." + clazz.getSimpleName();
Enrico Granatae190f572017-01-13 17:56:50 -080038 }
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 Granata15c4ec42017-02-24 14:13:05 -080047 IVehicle vehicle,
48 VehiclePropValue request,
49 java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) {
Andreas Gampe30450c92018-01-26 09:22:50 -080050 Objects.requireNonNull(vehicle);
51 Objects.requireNonNull(request);
Enrico Granata15c4ec42017-02-24 14:13:05 -080052 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 Maltsev82c20cd2017-04-11 12:38:17 -070062 Log.w(TAG, "attempt to read VHAL property " + request + " caused RemoteException: ", e);
Enrico Granata15c4ec42017-02-24 14:13:05 -080063 }
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 Granatae190f572017-01-13 17:56:50 -080071 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 Granatae190f572017-01-13 17:56:50 -080079 VehiclePropValue request =
Enrico Granata15c4ec42017-02-24 14:13:05 -080080 VehiclePropValueBuilder.newBuilder(propertyId).setAreaId(areaId).build();
81 return readVhalProperty(vehicle, request, f);
Enrico Granatae190f572017-01-13 17:56:50 -080082 }
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070083
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070084 static IVehicle getVehicle() throws RemoteException {
85 IVehicle service;
86 try {
Keun-young Park19ca1322017-08-25 11:35:38 -070087 service = IVehicle.getService();
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070088 } catch (NoSuchElementException ex) {
Keun-young Park00e2dc82017-08-25 11:51:15 -070089 throw new RuntimeException("Couldn't connect to vehicle@2.0", ex);
Pavel Maltsev82c20cd2017-04-11 12:38:17 -070090 }
91 Log.d(TAG, "Connected to IVehicle service: " + service);
92 return service;
93 }
Chao Yanf2c109c2018-05-11 12:16:14 -070094
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 Yana9160682018-06-14 11:05:45 -0700102 static boolean areCarPropertyValuesEqual(final CarPropertyValue value1,
103 final CarPropertyValue value2) {
Chao Yanf2c109c2018-05-11 12:16:14 -0700104 return value1 == value2
105 || value1 != null
106 && value2 != null
Chao Yana9160682018-06-14 11:05:45 -0700107 && value1.getPropertyId() == value2.getPropertyId()
108 && value1.getAreaId() == value2.getAreaId()
109 && value1.getValue().equals(value2.getValue());
Chao Yanf2c109c2018-05-11 12:16:14 -0700110 }
Enrico Granatae190f572017-01-13 17:56:50 -0800111}