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 19 import static android.os.SystemClock.elapsedRealtime; 20 21 import android.annotation.Nullable; 22 import android.car.hardware.CarPropertyValue; 23 import android.hardware.automotive.vehicle.V2_0.IVehicle; 24 import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; 25 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 26 import android.os.RemoteException; 27 import android.util.Log; 28 29 import com.android.car.vehiclehal.VehiclePropValueBuilder; 30 31 import java.util.NoSuchElementException; 32 import java.util.Objects; 33 34 final class Utils { Utils()35 private Utils() {} 36 37 private static final String TAG = concatTag(Utils.class); 38 concatTag(Class clazz)39 static String concatTag(Class clazz) { 40 return "VehicleHalTest." + clazz.getSimpleName(); 41 } 42 isVhalPropertyAvailable(IVehicle vehicle, int prop)43 static boolean isVhalPropertyAvailable(IVehicle vehicle, int prop) throws RemoteException { 44 return vehicle.getAllPropConfigs() 45 .stream() 46 .anyMatch((VehiclePropConfig config) -> config.prop == prop); 47 } 48 readVhalProperty( IVehicle vehicle, VehiclePropValue request, java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f)49 static VehiclePropValue readVhalProperty( 50 IVehicle vehicle, 51 VehiclePropValue request, 52 java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { 53 Objects.requireNonNull(vehicle); 54 Objects.requireNonNull(request); 55 VehiclePropValue vpv[] = new VehiclePropValue[] {null}; 56 try { 57 vehicle.get( 58 request, 59 (int status, VehiclePropValue propValue) -> { 60 if (f.apply(status, propValue)) { 61 vpv[0] = propValue; 62 } 63 }); 64 } catch (RemoteException e) { 65 Log.w(TAG, "attempt to read VHAL property " + request + " caused RemoteException: ", e); 66 } 67 return vpv[0]; 68 } 69 readVhalProperty( IVehicle vehicle, int propertyId, java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f)70 static VehiclePropValue readVhalProperty( 71 IVehicle vehicle, 72 int propertyId, 73 java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { 74 return readVhalProperty(vehicle, propertyId, 0, f); 75 } 76 readVhalProperty( IVehicle vehicle, int propertyId, int areaId, java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f)77 static VehiclePropValue readVhalProperty( 78 IVehicle vehicle, 79 int propertyId, 80 int areaId, 81 java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { 82 VehiclePropValue request = 83 VehiclePropValueBuilder.newBuilder(propertyId).setAreaId(areaId).build(); 84 return readVhalProperty(vehicle, request, f); 85 } 86 87 @Nullable getVehicle()88 private static IVehicle getVehicle() { 89 try { 90 return IVehicle.getService(); 91 } catch (NoSuchElementException ex) { 92 Log.e(TAG, "IVehicle service not registered yet", ex); 93 } catch (RemoteException e) { 94 Log.e(TAG, "Failed to get IVehicle Service ", e); 95 } 96 Log.d(TAG, "Failed to connect to IVehicle service"); 97 return null; 98 } 99 getVehicleWithTimeout(long waitMilliseconds)100 static IVehicle getVehicleWithTimeout(long waitMilliseconds) { 101 IVehicle vehicle = getVehicle(); 102 long endTime = elapsedRealtime() + waitMilliseconds; 103 while (vehicle == null && endTime > elapsedRealtime()) { 104 try { 105 Thread.sleep(100); 106 } catch (InterruptedException e) { 107 throw new RuntimeException("Sleep was interrupted", e); 108 } 109 vehicle = getVehicle(); 110 } 111 112 return vehicle; 113 } 114 115 /** 116 * Check the equality of two VehiclePropValue object ignoring timestamp and status. 117 * 118 * @param value1 119 * @param value2 120 * @return true if equal 121 */ areCarPropertyValuesEqual(final CarPropertyValue value1, final CarPropertyValue value2)122 static boolean areCarPropertyValuesEqual(final CarPropertyValue value1, 123 final CarPropertyValue value2) { 124 return value1 == value2 125 || value1 != null 126 && value2 != null 127 && value1.getPropertyId() == value2.getPropertyId() 128 && value1.getAreaId() == value2.getAreaId() 129 && value1.getValue().equals(value2.getValue()); 130 } 131 } 132