xref: /aosp_15_r20/system/libhwbinder/include/hwbinder/IBinder.h (revision 77b80299c8bdfeca3ae6d0ce27ae1ad3db289be3)
1*77b80299SAndroid Build Coastguard Worker /*
2*77b80299SAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*77b80299SAndroid Build Coastguard Worker  *
4*77b80299SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*77b80299SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*77b80299SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*77b80299SAndroid Build Coastguard Worker  *
8*77b80299SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*77b80299SAndroid Build Coastguard Worker  *
10*77b80299SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*77b80299SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*77b80299SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*77b80299SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*77b80299SAndroid Build Coastguard Worker  * limitations under the License.
15*77b80299SAndroid Build Coastguard Worker  */
16*77b80299SAndroid Build Coastguard Worker 
17*77b80299SAndroid Build Coastguard Worker #ifndef ANDROID_HARDWARE_IBINDER_H
18*77b80299SAndroid Build Coastguard Worker #define ANDROID_HARDWARE_IBINDER_H
19*77b80299SAndroid Build Coastguard Worker 
20*77b80299SAndroid Build Coastguard Worker #include <functional>
21*77b80299SAndroid Build Coastguard Worker 
22*77b80299SAndroid Build Coastguard Worker #include <utils/Errors.h>
23*77b80299SAndroid Build Coastguard Worker #include <utils/RefBase.h>
24*77b80299SAndroid Build Coastguard Worker #include <utils/String16.h>
25*77b80299SAndroid Build Coastguard Worker 
26*77b80299SAndroid Build Coastguard Worker // WARNING: this code is part of libhwbinder, a fork of libbinder. Generally,
27*77b80299SAndroid Build Coastguard Worker // this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific
28*77b80299SAndroid Build Coastguard Worker // code should not try to use these things.
29*77b80299SAndroid Build Coastguard Worker 
30*77b80299SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
31*77b80299SAndroid Build Coastguard Worker namespace android {
32*77b80299SAndroid Build Coastguard Worker namespace hardware {
33*77b80299SAndroid Build Coastguard Worker 
34*77b80299SAndroid Build Coastguard Worker class BHwBinder;
35*77b80299SAndroid Build Coastguard Worker class BpHwBinder;
36*77b80299SAndroid Build Coastguard Worker class IInterface;
37*77b80299SAndroid Build Coastguard Worker class Parcel;
38*77b80299SAndroid Build Coastguard Worker 
39*77b80299SAndroid Build Coastguard Worker /**
40*77b80299SAndroid Build Coastguard Worker  * Base class and low-level protocol for a remotable object.
41*77b80299SAndroid Build Coastguard Worker  * You can derive from this class to create an object for which other
42*77b80299SAndroid Build Coastguard Worker  * processes can hold references to it.  Communication between processes
43*77b80299SAndroid Build Coastguard Worker  * (method calls, property get and set) is down through a low-level
44*77b80299SAndroid Build Coastguard Worker  * protocol implemented on top of the transact() API.
45*77b80299SAndroid Build Coastguard Worker  */
46*77b80299SAndroid Build Coastguard Worker class IBinder : public virtual RefBase
47*77b80299SAndroid Build Coastguard Worker {
48*77b80299SAndroid Build Coastguard Worker public:
49*77b80299SAndroid Build Coastguard Worker     using TransactCallback = std::function<void(Parcel&)>;
50*77b80299SAndroid Build Coastguard Worker 
51*77b80299SAndroid Build Coastguard Worker     enum {
52*77b80299SAndroid Build Coastguard Worker         /* It is very important that these values NEVER change. These values
53*77b80299SAndroid Build Coastguard Worker          * must remain unchanged over the lifetime of android. This is
54*77b80299SAndroid Build Coastguard Worker          * because the framework on a device will be updated independently of
55*77b80299SAndroid Build Coastguard Worker          * the hals on a device. If the hals are compiled with one set of
56*77b80299SAndroid Build Coastguard Worker          * transaction values, and the framework with another, then the
57*77b80299SAndroid Build Coastguard Worker          * interface between them will be destroyed, and the device will not
58*77b80299SAndroid Build Coastguard Worker          * work.
59*77b80299SAndroid Build Coastguard Worker          */
60*77b80299SAndroid Build Coastguard Worker         /////////////////// User defined transactions
61*77b80299SAndroid Build Coastguard Worker         FIRST_CALL_TRANSACTION  = 0x00000001,
62*77b80299SAndroid Build Coastguard Worker         LAST_CALL_TRANSACTION   = 0x0effffff,
63*77b80299SAndroid Build Coastguard Worker         /////////////////// HIDL reserved
64*77b80299SAndroid Build Coastguard Worker #define B_PACK_CHARS_USER(c1, c2, c3, c4) \
65*77b80299SAndroid Build Coastguard Worker          ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
66*77b80299SAndroid Build Coastguard Worker         FIRST_HIDL_TRANSACTION  = 0x0f000000,
67*77b80299SAndroid Build Coastguard Worker         HIDL_PING_TRANSACTION                     = B_PACK_CHARS_USER(0x0f, 'P', 'N', 'G'),
68*77b80299SAndroid Build Coastguard Worker         HIDL_DESCRIPTOR_CHAIN_TRANSACTION         = B_PACK_CHARS_USER(0x0f, 'C', 'H', 'N'),
69*77b80299SAndroid Build Coastguard Worker         HIDL_GET_DESCRIPTOR_TRANSACTION           = B_PACK_CHARS_USER(0x0f, 'D', 'S', 'C'),
70*77b80299SAndroid Build Coastguard Worker         HIDL_SYSPROPS_CHANGED_TRANSACTION         = B_PACK_CHARS_USER(0x0f, 'S', 'Y', 'S'),
71*77b80299SAndroid Build Coastguard Worker         HIDL_LINK_TO_DEATH_TRANSACTION            = B_PACK_CHARS_USER(0x0f, 'L', 'T', 'D'),
72*77b80299SAndroid Build Coastguard Worker         HIDL_UNLINK_TO_DEATH_TRANSACTION          = B_PACK_CHARS_USER(0x0f, 'U', 'T', 'D'),
73*77b80299SAndroid Build Coastguard Worker         HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION  = B_PACK_CHARS_USER(0x0f, 'I', 'N', 'T'),
74*77b80299SAndroid Build Coastguard Worker         HIDL_GET_REF_INFO_TRANSACTION             = B_PACK_CHARS_USER(0x0f, 'R', 'E', 'F'),
75*77b80299SAndroid Build Coastguard Worker         HIDL_DEBUG_TRANSACTION                    = B_PACK_CHARS_USER(0x0f, 'D', 'B', 'G'),
76*77b80299SAndroid Build Coastguard Worker         HIDL_HASH_CHAIN_TRANSACTION               = B_PACK_CHARS_USER(0x0f, 'H', 'S', 'H'),
77*77b80299SAndroid Build Coastguard Worker #undef B_PACK_CHARS_USER
78*77b80299SAndroid Build Coastguard Worker         LAST_HIDL_TRANSACTION   = 0x0fffffff,
79*77b80299SAndroid Build Coastguard Worker 
80*77b80299SAndroid Build Coastguard Worker         // Corresponds to TF_ONE_WAY -- an asynchronous call.
81*77b80299SAndroid Build Coastguard Worker         FLAG_ONEWAY             = 0x00000001,
82*77b80299SAndroid Build Coastguard Worker 
83*77b80299SAndroid Build Coastguard Worker         // Corresponds to TF_CLEAR_BUF -- clear transaction buffers after call
84*77b80299SAndroid Build Coastguard Worker         // is made
85*77b80299SAndroid Build Coastguard Worker         FLAG_CLEAR_BUF          = 0x00000020,
86*77b80299SAndroid Build Coastguard Worker     };
87*77b80299SAndroid Build Coastguard Worker 
88*77b80299SAndroid Build Coastguard Worker                           IBinder();
89*77b80299SAndroid Build Coastguard Worker 
90*77b80299SAndroid Build Coastguard Worker     virtual status_t        transact(   uint32_t code,
91*77b80299SAndroid Build Coastguard Worker                                         const Parcel& data,
92*77b80299SAndroid Build Coastguard Worker                                         Parcel* reply,
93*77b80299SAndroid Build Coastguard Worker                                         uint32_t flags = 0,
94*77b80299SAndroid Build Coastguard Worker                                         TransactCallback callback = nullptr) = 0;
95*77b80299SAndroid Build Coastguard Worker 
96*77b80299SAndroid Build Coastguard Worker     class DeathRecipient : public virtual RefBase
97*77b80299SAndroid Build Coastguard Worker     {
98*77b80299SAndroid Build Coastguard Worker     public:
99*77b80299SAndroid Build Coastguard Worker         virtual void binderDied(const wp<IBinder>& who) = 0;
100*77b80299SAndroid Build Coastguard Worker     };
101*77b80299SAndroid Build Coastguard Worker 
102*77b80299SAndroid Build Coastguard Worker     /**
103*77b80299SAndroid Build Coastguard Worker      * Register the @a recipient for a notification if this binder
104*77b80299SAndroid Build Coastguard Worker      * goes away.  If this binder object unexpectedly goes away
105*77b80299SAndroid Build Coastguard Worker      * (typically because its hosting process has been killed),
106*77b80299SAndroid Build Coastguard Worker      * then DeathRecipient::binderDied() will be called with a reference
107*77b80299SAndroid Build Coastguard Worker      * to this.
108*77b80299SAndroid Build Coastguard Worker      *
109*77b80299SAndroid Build Coastguard Worker      * The @a cookie is optional -- if non-NULL, it should be a
110*77b80299SAndroid Build Coastguard Worker      * memory address that you own (that is, you know it is unique).
111*77b80299SAndroid Build Coastguard Worker      *
112*77b80299SAndroid Build Coastguard Worker      * @note You will only receive death notifications for remote binders,
113*77b80299SAndroid Build Coastguard Worker      * as local binders by definition can't die without you dying as well.
114*77b80299SAndroid Build Coastguard Worker      * Trying to use this function on a local binder will result in an
115*77b80299SAndroid Build Coastguard Worker      * INVALID_OPERATION code being returned and nothing happening.
116*77b80299SAndroid Build Coastguard Worker      *
117*77b80299SAndroid Build Coastguard Worker      * @note This link always holds a weak reference to its recipient.
118*77b80299SAndroid Build Coastguard Worker      *
119*77b80299SAndroid Build Coastguard Worker      * @note You will only receive a weak reference to the dead
120*77b80299SAndroid Build Coastguard Worker      * binder.  You should not try to promote this to a strong reference.
121*77b80299SAndroid Build Coastguard Worker      * (Nor should you need to, as there is nothing useful you can
122*77b80299SAndroid Build Coastguard Worker      * directly do with it now that it has passed on.)
123*77b80299SAndroid Build Coastguard Worker      */
124*77b80299SAndroid Build Coastguard Worker     virtual status_t        linkToDeath(const sp<DeathRecipient>& recipient,
125*77b80299SAndroid Build Coastguard Worker                                         void* cookie = nullptr,
126*77b80299SAndroid Build Coastguard Worker                                         uint32_t flags = 0) = 0;
127*77b80299SAndroid Build Coastguard Worker 
128*77b80299SAndroid Build Coastguard Worker     /**
129*77b80299SAndroid Build Coastguard Worker      * Remove a previously registered death notification.
130*77b80299SAndroid Build Coastguard Worker      * The @a recipient will no longer be called if this object
131*77b80299SAndroid Build Coastguard Worker      * dies.  The @a cookie is optional.  If non-NULL, you can
132*77b80299SAndroid Build Coastguard Worker      * supply a NULL @a recipient, and the recipient previously
133*77b80299SAndroid Build Coastguard Worker      * added with that cookie will be unlinked.
134*77b80299SAndroid Build Coastguard Worker      */
135*77b80299SAndroid Build Coastguard Worker     virtual status_t        unlinkToDeath(  const wp<DeathRecipient>& recipient,
136*77b80299SAndroid Build Coastguard Worker                                             void* cookie = nullptr,
137*77b80299SAndroid Build Coastguard Worker                                             uint32_t flags = 0,
138*77b80299SAndroid Build Coastguard Worker                                             wp<DeathRecipient>* outRecipient = nullptr) = 0;
139*77b80299SAndroid Build Coastguard Worker 
140*77b80299SAndroid Build Coastguard Worker     virtual bool            checkSubclass(const void* subclassID) const;
141*77b80299SAndroid Build Coastguard Worker 
142*77b80299SAndroid Build Coastguard Worker     typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
143*77b80299SAndroid Build Coastguard Worker 
144*77b80299SAndroid Build Coastguard Worker     /**
145*77b80299SAndroid Build Coastguard Worker      * This object is attached for the lifetime of this binder object. When
146*77b80299SAndroid Build Coastguard Worker      * this binder object is destructed, the cleanup function of all attached
147*77b80299SAndroid Build Coastguard Worker      * objects are invoked with their respective objectID, object, and
148*77b80299SAndroid Build Coastguard Worker      * cleanupCookie. Access to these APIs can be made from multiple threads,
149*77b80299SAndroid Build Coastguard Worker      * but calls from different threads are allowed to be interleaved.
150*77b80299SAndroid Build Coastguard Worker      */
151*77b80299SAndroid Build Coastguard Worker     virtual void            attachObject(   const void* objectID,
152*77b80299SAndroid Build Coastguard Worker                                             void* object,
153*77b80299SAndroid Build Coastguard Worker                                             void* cleanupCookie,
154*77b80299SAndroid Build Coastguard Worker                                             object_cleanup_func func) = 0;
155*77b80299SAndroid Build Coastguard Worker     /**
156*77b80299SAndroid Build Coastguard Worker      * Returns object attached with attachObject.
157*77b80299SAndroid Build Coastguard Worker      */
158*77b80299SAndroid Build Coastguard Worker     virtual void*           findObject(const void* objectID) const = 0;
159*77b80299SAndroid Build Coastguard Worker     /**
160*77b80299SAndroid Build Coastguard Worker      * WARNING: this API does not call the cleanup function for legacy reasons.
161*77b80299SAndroid Build Coastguard Worker      * It also does not return void* for legacy reasons. If you need to detach
162*77b80299SAndroid Build Coastguard Worker      * an object and destroy it, there are two options:
163*77b80299SAndroid Build Coastguard Worker      * - if you can, don't call detachObject and instead wait for the destructor
164*77b80299SAndroid Build Coastguard Worker      *   to clean it up.
165*77b80299SAndroid Build Coastguard Worker      * - manually retrieve and destruct the object (if multiple of your threads
166*77b80299SAndroid Build Coastguard Worker      *   are accessing these APIs, you must guarantee that attachObject isn't
167*77b80299SAndroid Build Coastguard Worker      *   called after findObject and before detachObject is called).
168*77b80299SAndroid Build Coastguard Worker      */
169*77b80299SAndroid Build Coastguard Worker     virtual void            detachObject(const void* objectID) = 0;
170*77b80299SAndroid Build Coastguard Worker 
171*77b80299SAndroid Build Coastguard Worker     virtual BHwBinder*        localBinder();
172*77b80299SAndroid Build Coastguard Worker     virtual BpHwBinder*       remoteBinder();
173*77b80299SAndroid Build Coastguard Worker 
174*77b80299SAndroid Build Coastguard Worker protected:
175*77b80299SAndroid Build Coastguard Worker     virtual          ~IBinder();
176*77b80299SAndroid Build Coastguard Worker 
177*77b80299SAndroid Build Coastguard Worker private:
178*77b80299SAndroid Build Coastguard Worker };
179*77b80299SAndroid Build Coastguard Worker 
180*77b80299SAndroid Build Coastguard Worker } // namespace hardware
181*77b80299SAndroid Build Coastguard Worker } // namespace android
182*77b80299SAndroid Build Coastguard Worker 
183*77b80299SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
184*77b80299SAndroid Build Coastguard Worker 
185*77b80299SAndroid Build Coastguard Worker #endif // ANDROID_HARDWARE_IBINDER_H
186