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