1 /* 2 * Copyright (C) 2022 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.rkpdapp.interfaces; 18 19 import android.annotation.TestApi; 20 import android.hardware.security.keymint.IRemotelyProvisionedComponent; 21 import android.os.ServiceManager; 22 23 import java.util.Arrays; 24 25 /** 26 * Provides convenience methods for interfacing with ServiceManager class and its static functions. 27 */ 28 public class ServiceManagerInterface { 29 private static final String TAG = "RkpdSvcManagerInterface"; 30 private static SystemInterface[] sInstances; 31 ServiceManagerInterface()32 private ServiceManagerInterface() { 33 } 34 createSystemInterface(String serviceName)35 private static SystemInterface createSystemInterface(String serviceName) { 36 IRemotelyProvisionedComponent binder = IRemotelyProvisionedComponent.Stub.asInterface( 37 ServiceManager.waitForDeclaredService(serviceName)); 38 if (binder == null) { 39 throw new IllegalArgumentException("Cannot find any implementation for " + serviceName); 40 } 41 return new SystemInterface(binder, serviceName); 42 } 43 44 /** 45 * Gets all the instances on this device for IRemotelyProvisionedComponent as an array. The 46 * returned values each contain a binder for interacting with the instance. 47 * 48 * For testing purposes, the instances may be overridden by setInstances 49 */ getAllInstances()50 public static SystemInterface[] getAllInstances() { 51 if (sInstances != null) { 52 return sInstances; 53 } 54 55 String irpcInterface = IRemotelyProvisionedComponent.DESCRIPTOR; 56 return Arrays.stream(ServiceManager.getDeclaredInstances(irpcInterface)) 57 .map(x -> createSystemInterface(irpcInterface + "/" + x)) 58 .toArray(SystemInterface[]::new); 59 } 60 61 /** 62 * Get a specific system interface instance for a given IRemotelyProvisionedComponent. 63 * If the given serviceName does not map to a known IRemotelyProvisionedComponent, this 64 * method throws IllegalArgumentException. 65 * 66 * For testing purposes, the instances may be overridden by setInstances. 67 */ getInstance(String serviceName)68 public static SystemInterface getInstance(String serviceName) { 69 if (sInstances != null) { 70 for (SystemInterface i : sInstances) { 71 if (i.getServiceName().equals(serviceName)) { 72 return i; 73 } 74 } 75 throw new IllegalArgumentException("Cannot find any implementation for " + serviceName); 76 } 77 78 return createSystemInterface(serviceName); 79 } 80 81 @TestApi setInstances(SystemInterface[] instances)82 public static void setInstances(SystemInterface[] instances) { 83 sInstances = instances; 84 } 85 } 86