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 17package com.android.car.vehiclehal.test; 18 19import android.annotation.Nullable; 20import android.hardware.automotive.vehicle.V2_0.IVehicle; 21import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; 22import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 23import android.os.RemoteException; 24import android.util.Log; 25 26import com.android.car.vehiclehal.VehiclePropValueBuilder; 27 28import java.util.NoSuchElementException; 29import java.util.Objects; 30 31final class Utils { 32 private Utils() {} 33 34 private static final String TAG = concatTag(Utils.class); 35 36 static String concatTag(Class clazz) { 37 return "VehicleHalTest." + clazz.getSimpleName(); 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( 47 IVehicle vehicle, 48 VehiclePropValue request, 49 java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) { 50 vehicle = Objects.requireNonNull(vehicle); 51 request = Objects.requireNonNull(request); 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) { 62 Log.w(TAG, "attempt to read VHAL property " + request + " caused RemoteException: ", e); 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) { 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) { 79 VehiclePropValue request = 80 VehiclePropValueBuilder.newBuilder(propertyId).setAreaId(areaId).build(); 81 return readVhalProperty(vehicle, request, f); 82 } 83 84 @Nullable 85 static IVehicle getVehicle() throws RemoteException { 86 IVehicle service; 87 try { 88 service = android.hardware.automotive.vehicle.V2_0.IVehicle.getService(); 89 } catch (NoSuchElementException ex) { 90 Log.d(TAG, "Couldn't connect to vehicle@2.1, connecting to vehicle@2.0..."); 91 service = IVehicle.getService(); 92 } 93 Log.d(TAG, "Connected to IVehicle service: " + service); 94 return service; 95 } 96} 97