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