blob: 9e3e83e14a48797429e7299aa5ebc26878412b76 [file] [log] [blame]
Andreas Huberdab5fc62016-08-15 09:25:02 -07001/*
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
17package android.os;
18
Steven Morelande8e63aa2018-01-10 15:45:36 -080019import android.annotation.SystemApi;
Steven Morelandcbba4c72019-01-11 10:19:51 -080020import android.annotation.TestApi;
Steven Morelande8e63aa2018-01-10 15:45:36 -080021
Andreas Huberdab5fc62016-08-15 09:25:02 -070022import libcore.util.NativeAllocationRegistry;
23
Steven Morelandec5eb7e2017-10-25 04:50:46 +000024import java.util.NoSuchElementException;
25
Andreas Huberdab5fc62016-08-15 09:25:02 -070026/** @hide */
Steven Morelande8e63aa2018-01-10 15:45:36 -080027@SystemApi
Steven Morelandcbba4c72019-01-11 10:19:51 -080028@TestApi
Andreas Huberdab5fc62016-08-15 09:25:02 -070029public abstract class HwBinder implements IHwBinder {
30 private static final String TAG = "HwBinder";
31
32 private static final NativeAllocationRegistry sNativeRegistry;
33
Steven Moreland4c0d6b62018-02-07 10:07:03 -080034 /**
35 * Create and initialize a HwBinder object and the native objects
36 * used to allow this to participate in hwbinder transactions.
Steven Moreland4c0d6b62018-02-07 10:07:03 -080037 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070038 public HwBinder() {
39 native_setup();
40
41 sNativeRegistry.registerNativeAllocation(
42 this,
43 mNativeContext);
44 }
45
Steven Morelande62b1f32016-12-20 15:55:48 -080046 @Override
Andreas Huberdab5fc62016-08-15 09:25:02 -070047 public final native void transact(
Steven Morelande62b1f32016-12-20 15:55:48 -080048 int code, HwParcel request, HwParcel reply, int flags)
49 throws RemoteException;
Andreas Huberdab5fc62016-08-15 09:25:02 -070050
Steven Moreland4c0d6b62018-02-07 10:07:03 -080051 /**
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
Steven Moreland4c0d6b62018-02-07 10:07:03 -080058 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070059 public abstract void onTransact(
Steven Morelande62b1f32016-12-20 15:55:48 -080060 int code, HwParcel request, HwParcel reply, int flags)
61 throws RemoteException;
Andreas Huberdab5fc62016-08-15 09:25:02 -070062
Steven Moreland4c0d6b62018-02-07 10:07:03 -080063 /**
64 * Registers this service with the hwservicemanager.
65 *
66 * @param serviceName instance name of the service
Steven Moreland4c0d6b62018-02-07 10:07:03 -080067 */
Martijn Coenen85d12da2017-03-06 13:07:53 +010068 public native final void registerService(String serviceName)
Steven Morelandc0631d02017-01-04 10:37:59 -080069 throws RemoteException;
Andreas Huber35eb7992016-10-25 13:29:30 -070070
Steven Morelandadcb8962018-01-25 10:24:07 -080071 /**
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
Steven Morelandadcb8962018-01-25 10:24:07 -080077 */
Steven Morelandec5eb7e2017-10-25 04:50:46 +000078 public static final IHwBinder getService(
Steven Morelandf3c53492016-11-03 15:17:04 -070079 String iface,
Steven Morelandcc9e67c2017-10-25 04:46:26 +000080 String serviceName)
Steven Morelandec5eb7e2017-10-25 04:50:46 +000081 throws RemoteException, NoSuchElementException {
82 return getService(iface, serviceName, false /* retry */);
83 }
Steven Morelandadcb8962018-01-25 10:24:07 -080084 /**
85 * Returns the specified service from the hwservicemanager.
86 * @param iface fully-qualified interface name for example foo.bar@1.3::IBaz
87 * @param serviceName the instance name of the service for example default.
88 * @param retry whether to wait for the service to start if it's not already started
89 * @throws NoSuchElementException when the service is unavailable
Steven Morelandadcb8962018-01-25 10:24:07 -080090 */
Steven Morelandec5eb7e2017-10-25 04:50:46 +000091 public static native final IHwBinder getService(
92 String iface,
93 String serviceName,
94 boolean retry)
Steven Morelandc0631d02017-01-04 10:37:59 -080095 throws RemoteException, NoSuchElementException;
Andreas Huberdab5fc62016-08-15 09:25:02 -070096
Steven Morelande8e63aa2018-01-10 15:45:36 -080097 /**
98 * Configures how many threads the process-wide hwbinder threadpool
99 * has to process incoming requests.
100 *
Steven Moreland739811a2018-01-30 10:11:40 -0800101 * @param maxThreads total number of threads to create (includes this thread if
102 * callerWillJoin is true)
103 * @param callerWillJoin whether joinRpcThreadpool will be called in advance
Steven Morelande8e63aa2018-01-10 15:45:36 -0800104 */
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700105 public static native final void configureRpcThreadpool(
106 long maxThreads, boolean callerWillJoin);
107
Steven Morelande8e63aa2018-01-10 15:45:36 -0800108 /**
109 * Current thread will join hwbinder threadpool and process
110 * commands in the pool. Should be called after configuring
111 * a threadpool with callerWillJoin true and then registering
112 * the provided service if this thread doesn't need to do
113 * anything else.
Steven Morelande8e63aa2018-01-10 15:45:36 -0800114 */
Timur Iskhakovb6a62832017-07-10 10:08:38 -0700115 public static native final void joinRpcThreadpool();
116
Andreas Huberdab5fc62016-08-15 09:25:02 -0700117 // Returns address of the "freeFunction".
118 private static native final long native_init();
119
120 private native final void native_setup();
121
122 static {
123 long freeFunction = native_init();
124
125 sNativeRegistry = new NativeAllocationRegistry(
126 HwBinder.class.getClassLoader(),
127 freeFunction,
128 128 /* size */);
129 }
130
131 private long mNativeContext;
Sundong Ahn28cc6e82017-07-13 16:34:01 +0900132
133 private static native void native_report_sysprop_change();
134
135 /**
Steven Moreland36be1912018-01-25 15:42:02 -0800136 * Enable instrumentation if available.
Steven Moreland739811a2018-01-30 10:11:40 -0800137 *
138 * On a non-user build, this method:
139 * - tries to enable atracing (if enabled)
140 * - tries to enable coverage dumps (if running in VTS)
141 * - tries to enable record and replay (if running in VTS)
Steven Moreland36be1912018-01-25 15:42:02 -0800142 */
143 public static void enableInstrumentation() {
144 native_report_sysprop_change();
145 }
146
147 /**
Sundong Ahn28cc6e82017-07-13 16:34:01 +0900148 * Notifies listeners that a system property has changed
Steven Moreland36be1912018-01-25 15:42:02 -0800149 *
150 * TODO(b/72480743): remove this method
151 *
Steven Morelande8e63aa2018-01-10 15:45:36 -0800152 * @hide
Sundong Ahn28cc6e82017-07-13 16:34:01 +0900153 */
154 public static void reportSyspropChanged() {
155 native_report_sysprop_change();
156 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700157}