Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 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 | |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Steven Moreland | cbba4c7 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 20 | import android.annotation.TestApi; |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 22 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 23 | import libcore.util.NativeAllocationRegistry; |
| 24 | |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 25 | import java.util.NoSuchElementException; |
| 26 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 27 | /** @hide */ |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 28 | @SystemApi |
Steven Moreland | cbba4c7 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 29 | @TestApi |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 30 | public abstract class HwBinder implements IHwBinder { |
| 31 | private static final String TAG = "HwBinder"; |
| 32 | |
| 33 | private static final NativeAllocationRegistry sNativeRegistry; |
| 34 | |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Create and initialize a HwBinder object and the native objects |
| 37 | * used to allow this to participate in hwbinder transactions. |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 38 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 39 | public HwBinder() { |
| 40 | native_setup(); |
| 41 | |
| 42 | sNativeRegistry.registerNativeAllocation( |
| 43 | this, |
| 44 | mNativeContext); |
| 45 | } |
| 46 | |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 47 | @Override |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 48 | public final native void transact( |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 49 | int code, HwParcel request, HwParcel reply, int flags) |
| 50 | throws RemoteException; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 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 |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 59 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 60 | public abstract void onTransact( |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 61 | int code, HwParcel request, HwParcel reply, int flags) |
| 62 | throws RemoteException; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 63 | |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Registers this service with the hwservicemanager. |
| 66 | * |
| 67 | * @param serviceName instance name of the service |
Steven Moreland | 4c0d6b6 | 2018-02-07 10:07:03 -0800 | [diff] [blame] | 68 | */ |
Martijn Coenen | 85d12da | 2017-03-06 13:07:53 +0100 | [diff] [blame] | 69 | public native final void registerService(String serviceName) |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 70 | throws RemoteException; |
Andreas Huber | 35eb799 | 2016-10-25 13:29:30 -0700 | [diff] [blame] | 71 | |
Steven Moreland | adcb896 | 2018-01-25 10:24:07 -0800 | [diff] [blame] | 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 |
Steven Moreland | adcb896 | 2018-01-25 10:24:07 -0800 | [diff] [blame] | 78 | */ |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 79 | public static final IHwBinder getService( |
Steven Moreland | f3c5349 | 2016-11-03 15:17:04 -0700 | [diff] [blame] | 80 | String iface, |
Steven Moreland | cc9e67c | 2017-10-25 04:46:26 +0000 | [diff] [blame] | 81 | String serviceName) |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 82 | throws RemoteException, NoSuchElementException { |
| 83 | return getService(iface, serviceName, false /* retry */); |
| 84 | } |
Steven Moreland | adcb896 | 2018-01-25 10:24:07 -0800 | [diff] [blame] | 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 |
Steven Moreland | adcb896 | 2018-01-25 10:24:07 -0800 | [diff] [blame] | 91 | */ |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 92 | public static native final IHwBinder getService( |
| 93 | String iface, |
| 94 | String serviceName, |
| 95 | boolean retry) |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 96 | throws RemoteException, NoSuchElementException; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 97 | |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Configures how many threads the process-wide hwbinder threadpool |
| 100 | * has to process incoming requests. |
| 101 | * |
Steven Moreland | 739811a | 2018-01-30 10:11:40 -0800 | [diff] [blame] | 102 | * @param maxThreads total number of threads to create (includes this thread if |
| 103 | * callerWillJoin is true) |
| 104 | * @param callerWillJoin whether joinRpcThreadpool will be called in advance |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 105 | */ |
Timur Iskhakov | b6a6283 | 2017-07-10 10:08:38 -0700 | [diff] [blame] | 106 | public static native final void configureRpcThreadpool( |
| 107 | long maxThreads, boolean callerWillJoin); |
| 108 | |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Current thread will join hwbinder threadpool and process |
| 111 | * commands in the pool. Should be called after configuring |
| 112 | * a threadpool with callerWillJoin true and then registering |
| 113 | * the provided service if this thread doesn't need to do |
| 114 | * anything else. |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 115 | */ |
Timur Iskhakov | b6a6283 | 2017-07-10 10:08:38 -0700 | [diff] [blame] | 116 | public static native final void joinRpcThreadpool(); |
| 117 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 118 | // Returns address of the "freeFunction". |
| 119 | private static native final long native_init(); |
| 120 | |
| 121 | private native final void native_setup(); |
| 122 | |
| 123 | static { |
| 124 | long freeFunction = native_init(); |
| 125 | |
| 126 | sNativeRegistry = new NativeAllocationRegistry( |
| 127 | HwBinder.class.getClassLoader(), |
| 128 | freeFunction, |
| 129 | 128 /* size */); |
| 130 | } |
| 131 | |
| 132 | private long mNativeContext; |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 133 | |
| 134 | private static native void native_report_sysprop_change(); |
| 135 | |
| 136 | /** |
Steven Moreland | 36be191 | 2018-01-25 15:42:02 -0800 | [diff] [blame] | 137 | * Enable instrumentation if available. |
Steven Moreland | 739811a | 2018-01-30 10:11:40 -0800 | [diff] [blame] | 138 | * |
| 139 | * On a non-user build, this method: |
| 140 | * - tries to enable atracing (if enabled) |
| 141 | * - tries to enable coverage dumps (if running in VTS) |
| 142 | * - tries to enable record and replay (if running in VTS) |
Steven Moreland | 36be191 | 2018-01-25 15:42:02 -0800 | [diff] [blame] | 143 | */ |
| 144 | public static void enableInstrumentation() { |
| 145 | native_report_sysprop_change(); |
| 146 | } |
| 147 | |
| 148 | /** |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 149 | * Notifies listeners that a system property has changed |
Steven Moreland | 36be191 | 2018-01-25 15:42:02 -0800 | [diff] [blame] | 150 | * |
| 151 | * TODO(b/72480743): remove this method |
| 152 | * |
Steven Moreland | e8e63aa | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 153 | * @hide |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 154 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 155 | @UnsupportedAppUsage |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 156 | public static void reportSyspropChanged() { |
| 157 | native_report_sysprop_change(); |
| 158 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 159 | } |