1 /* 2 * Copyright (C) 2016 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 android.os; 18 19 import android.annotation.SystemApi; 20 import android.compat.annotation.UnsupportedAppUsage; 21 22 import libcore.util.NativeAllocationRegistry; 23 24 import java.util.NoSuchElementException; 25 26 /** @hide */ 27 @SystemApi 28 public abstract class HwBinder implements IHwBinder { 29 private static final String TAG = "HwBinder"; 30 31 private static final NativeAllocationRegistry sNativeRegistry; 32 33 /** 34 * Create and initialize a HwBinder object and the native objects 35 * used to allow this to participate in hwbinder transactions. 36 */ HwBinder()37 public HwBinder() { 38 native_setup(); 39 40 sNativeRegistry.registerNativeAllocation( 41 this, 42 mNativeContext); 43 } 44 45 @Override transact( int code, HwParcel request, HwParcel reply, int flags)46 public final native void transact( 47 int code, HwParcel request, HwParcel reply, int flags) 48 throws RemoteException; 49 50 /** 51 * Process a hwbinder transaction. 52 * 53 * @param code interface specific code for interface. 54 * @param request parceled transaction 55 * @param reply object to parcel reply into 56 * @param flags transaction flags to be chosen by wire protocol 57 */ onTransact( int code, HwParcel request, HwParcel reply, int flags)58 public abstract void onTransact( 59 int code, HwParcel request, HwParcel reply, int flags) 60 throws RemoteException; 61 62 /** 63 * Registers this service with the hwservicemanager. 64 * 65 * @param serviceName instance name of the service 66 */ registerService(String serviceName)67 public native final void registerService(String serviceName) 68 throws RemoteException; 69 70 /** 71 * Returns the specified service from the hwservicemanager. Does not retry. 72 * 73 * @param iface fully-qualified interface name for example foo.bar@1.3::IBaz 74 * @param serviceName the instance name of the service for example default. 75 * @throws NoSuchElementException when the service is unavailable 76 */ getService( String iface, String serviceName)77 public static final IHwBinder getService( 78 String iface, 79 String serviceName) 80 throws RemoteException, NoSuchElementException { 81 return getService(iface, serviceName, false /* retry */); 82 } 83 /** 84 * Returns the specified service from the hwservicemanager. 85 * @param iface fully-qualified interface name for example foo.bar@1.3::IBaz 86 * @param serviceName the instance name of the service for example default. 87 * @param retry whether to wait for the service to start if it's not already started 88 * @throws NoSuchElementException when the service is unavailable 89 */ getService( String iface, String serviceName, boolean retry)90 public static native final IHwBinder getService( 91 String iface, 92 String serviceName, 93 boolean retry) 94 throws RemoteException, NoSuchElementException; 95 96 /** 97 * This allows getService to bypass the VINTF manifest for testing only. 98 * 99 * Disabled on user builds. 100 * @hide 101 */ setTrebleTestingOverride( boolean testingOverride)102 public static native final void setTrebleTestingOverride( 103 boolean testingOverride); 104 105 /** 106 * Configures how many threads the process-wide hwbinder threadpool 107 * has to process incoming requests. 108 * 109 * @param maxThreads total number of threads to create (includes this thread if 110 * callerWillJoin is true) 111 * @param callerWillJoin whether joinRpcThreadpool will be called in advance 112 */ configureRpcThreadpool( long maxThreads, boolean callerWillJoin)113 public static native final void configureRpcThreadpool( 114 long maxThreads, boolean callerWillJoin); 115 116 /** 117 * Current thread will join hwbinder threadpool and process 118 * commands in the pool. Should be called after configuring 119 * a threadpool with callerWillJoin true and then registering 120 * the provided service if this thread doesn't need to do 121 * anything else. 122 */ joinRpcThreadpool()123 public static native final void joinRpcThreadpool(); 124 125 // Returns address of the "freeFunction". native_init()126 private static native final long native_init(); 127 native_setup()128 private native final void native_setup(); 129 130 static { 131 long freeFunction = native_init(); 132 133 sNativeRegistry = new NativeAllocationRegistry( 134 HwBinder.class.getClassLoader(), 135 freeFunction, 136 128 /* size */); 137 } 138 139 private long mNativeContext; 140 native_report_sysprop_change()141 private static native void native_report_sysprop_change(); 142 143 /** 144 * Enable instrumentation if available. 145 * 146 * On a non-user build, this method: 147 * - tries to enable atracing (if enabled) 148 * - tries to enable coverage dumps (if running in VTS) 149 * - tries to enable record and replay (if running in VTS) 150 */ enableInstrumentation()151 public static void enableInstrumentation() { 152 native_report_sysprop_change(); 153 } 154 155 /** 156 * Notifies listeners that a system property has changed 157 * 158 * TODO(b/72480743): remove this method 159 * 160 * @hide 161 */ 162 @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553) reportSyspropChanged()163 public static void reportSyspropChanged() { 164 native_report_sysprop_change(); 165 } 166 } 167