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