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