Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [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 | #ifndef ANDROID_HIDL_BINDER_SUPPORT_H |
| 18 | #define ANDROID_HIDL_BINDER_SUPPORT_H |
| 19 | |
Yifan Hong | 777bef9 | 2017-02-01 15:50:36 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <android/hidl/base/1.0/BnHwBase.h> |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 23 | #include <android/hidl/base/1.0/IBase.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 24 | #include <hidl/HidlSupport.h> |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 25 | #include <hidl/HidlTransportUtils.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 26 | #include <hidl/MQDescriptor.h> |
| 27 | #include <hwbinder/IBinder.h> |
| 28 | #include <hwbinder/Parcel.h> |
Steven Moreland | b917318 | 2018-08-08 20:06:08 +0000 | [diff] [blame] | 29 | #include <log/log.h> // TODO(b/65843592): remove. Too many users depending on this transitively. |
| 30 | |
Steven Moreland | e783834 | 2019-05-30 11:29:16 -0700 | [diff] [blame] | 31 | // Defines functions for hidl_string, Status, hidl_vec, MQDescriptor, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 32 | // etc. to interact with Parcel. |
| 33 | |
| 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 37 | // hidl_binder_death_recipient wraps a transport-independent |
| 38 | // hidl_death_recipient, and implements the binder-specific |
| 39 | // DeathRecipient interface. |
| 40 | struct hidl_binder_death_recipient : IBinder::DeathRecipient { |
| 41 | hidl_binder_death_recipient(const sp<hidl_death_recipient> &recipient, |
Steven Moreland | 108d09d | 2017-05-05 16:15:38 -0700 | [diff] [blame] | 42 | uint64_t cookie, const sp<::android::hidl::base::V1_0::IBase> &base); |
| 43 | virtual void binderDied(const wp<IBinder>& /*who*/); |
| 44 | wp<hidl_death_recipient> getRecipient(); |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 45 | private: |
| 46 | wp<hidl_death_recipient> mRecipient; |
| 47 | uint64_t mCookie; |
| 48 | wp<::android::hidl::base::V1_0::IBase> mBase; |
| 49 | }; |
| 50 | |
Nirav Atre | 564a8d2 | 2018-07-26 18:29:12 -0700 | [diff] [blame] | 51 | // ---------------------- hidl_handle |
| 52 | |
| 53 | status_t readEmbeddedFromParcel(const hidl_handle &handle, |
| 54 | const Parcel &parcel, size_t parentHandle, size_t parentOffset); |
| 55 | |
| 56 | status_t writeEmbeddedToParcel(const hidl_handle &handle, |
| 57 | Parcel *parcel, size_t parentHandle, size_t parentOffset); |
| 58 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 59 | // ---------------------- hidl_memory |
| 60 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 61 | status_t readEmbeddedFromParcel(const hidl_memory &memory, |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 62 | const Parcel &parcel, size_t parentHandle, size_t parentOffset); |
| 63 | |
| 64 | status_t writeEmbeddedToParcel(const hidl_memory &memory, |
| 65 | Parcel *parcel, size_t parentHandle, size_t parentOffset); |
| 66 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 67 | // ---------------------- hidl_string |
| 68 | |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 69 | status_t readEmbeddedFromParcel(const hidl_string &string, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 70 | const Parcel &parcel, size_t parentHandle, size_t parentOffset); |
| 71 | |
| 72 | status_t writeEmbeddedToParcel(const hidl_string &string, |
| 73 | Parcel *parcel, size_t parentHandle, size_t parentOffset); |
| 74 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 75 | // ---------------------- Status |
| 76 | |
| 77 | // Bear in mind that if the client or service is a Java endpoint, this |
| 78 | // is not the logic which will provide/interpret the data here. |
| 79 | status_t readFromParcel(Status *status, const Parcel& parcel); |
| 80 | status_t writeToParcel(const Status &status, Parcel* parcel); |
| 81 | |
| 82 | // ---------------------- hidl_vec |
| 83 | |
| 84 | template<typename T> |
| 85 | status_t readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 86 | const hidl_vec<T> &vec, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 87 | const Parcel &parcel, |
| 88 | size_t parentHandle, |
| 89 | size_t parentOffset, |
| 90 | size_t *handle) { |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 91 | const void *out; |
Martijn Coenen | 6091d18 | 2017-01-13 00:18:54 +0100 | [diff] [blame] | 92 | return parcel.readNullableEmbeddedBuffer( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 93 | vec.size() * sizeof(T), |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 94 | handle, |
| 95 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 96 | parentOffset + hidl_vec<T>::kOffsetOfBuffer, |
| 97 | &out); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template<typename T> |
| 101 | status_t writeEmbeddedToParcel( |
| 102 | const hidl_vec<T> &vec, |
| 103 | Parcel *parcel, |
| 104 | size_t parentHandle, |
| 105 | size_t parentOffset, |
| 106 | size_t *handle) { |
| 107 | return parcel->writeEmbeddedBuffer( |
| 108 | vec.data(), |
| 109 | sizeof(T) * vec.size(), |
| 110 | handle, |
| 111 | parentHandle, |
| 112 | parentOffset + hidl_vec<T>::kOffsetOfBuffer); |
| 113 | } |
| 114 | |
| 115 | template<typename T> |
| 116 | status_t findInParcel(const hidl_vec<T> &vec, const Parcel &parcel, size_t *handle) { |
| 117 | return parcel.quickFindBuffer(vec.data(), handle); |
| 118 | } |
| 119 | |
| 120 | // ---------------------- MQDescriptor |
| 121 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 122 | template<typename T, MQFlavor flavor> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 123 | ::android::status_t readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 124 | MQDescriptor<T, flavor> &obj, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 125 | const ::android::hardware::Parcel &parcel, |
| 126 | size_t parentHandle, |
| 127 | size_t parentOffset) { |
| 128 | ::android::status_t _hidl_err = ::android::OK; |
| 129 | |
| 130 | size_t _hidl_grantors_child; |
| 131 | |
| 132 | _hidl_err = ::android::hardware::readEmbeddedFromParcel( |
Martijn Coenen | 9d3eb35 | 2017-04-18 20:28:03 -0700 | [diff] [blame] | 133 | obj.grantors(), |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 134 | parcel, |
| 135 | parentHandle, |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 136 | parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 137 | &_hidl_grantors_child); |
| 138 | |
| 139 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 140 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 141 | const native_handle_t *_hidl_mq_handle_ptr; |
Hridya Valsaraju | f6fa2a9 | 2017-03-08 15:20:07 -0800 | [diff] [blame] | 142 | _hidl_err = parcel.readNullableEmbeddedNativeHandle( |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 143 | parentHandle, |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 144 | parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle, |
| 145 | &_hidl_mq_handle_ptr); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 146 | |
Steven Moreland | 1f535a2 | 2017-01-06 19:25:49 -0800 | [diff] [blame] | 147 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 148 | |
| 149 | return _hidl_err; |
| 150 | } |
| 151 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 152 | template<typename T, MQFlavor flavor> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 153 | ::android::status_t writeEmbeddedToParcel( |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 154 | const MQDescriptor<T, flavor> &obj, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 155 | ::android::hardware::Parcel *parcel, |
| 156 | size_t parentHandle, |
| 157 | size_t parentOffset) { |
| 158 | ::android::status_t _hidl_err = ::android::OK; |
| 159 | |
| 160 | size_t _hidl_grantors_child; |
| 161 | |
| 162 | _hidl_err = ::android::hardware::writeEmbeddedToParcel( |
| 163 | obj.grantors(), |
| 164 | parcel, |
| 165 | parentHandle, |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 166 | parentOffset + MQDescriptor<T, flavor>::kOffsetOfGrantors, |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 167 | &_hidl_grantors_child); |
| 168 | |
| 169 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 170 | |
| 171 | _hidl_err = parcel->writeEmbeddedNativeHandle( |
| 172 | obj.handle(), |
| 173 | parentHandle, |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 174 | parentOffset + MQDescriptor<T, flavor>::kOffsetOfHandle); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 175 | |
| 176 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 177 | |
| 178 | return _hidl_err; |
| 179 | } |
| 180 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 181 | // ---------------------- support for casting interfaces |
| 182 | |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 183 | // Constructs a binder for this interface and caches it. If it has already been created |
Steven Moreland | 986fa09 | 2018-09-27 10:38:03 -0700 | [diff] [blame] | 184 | // then it returns it. |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 185 | sp<IBinder> getOrCreateCachedBinder(::android::hidl::base::V1_0::IBase* ifacePtr); |
| 186 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 187 | // Construct a smallest possible binder from the given interface. |
| 188 | // If it is remote, then its remote() will be retrieved. |
| 189 | // Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType |
| 190 | // and iface is of class IChild. BnChild will be used to wrapped the given iface. |
| 191 | // Return nullptr if iface is null or any failure. |
Steven Moreland | 5f82d3e | 2017-07-27 13:57:56 -0700 | [diff] [blame] | 192 | template <typename IType, |
| 193 | typename = std::enable_if_t<std::is_same<details::i_tag, typename IType::_hidl_tag>::value>> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 194 | sp<IBinder> toBinder(sp<IType> iface) { |
| 195 | IType *ifacePtr = iface.get(); |
Steven Moreland | 986fa09 | 2018-09-27 10:38:03 -0700 | [diff] [blame] | 196 | return getOrCreateCachedBinder(ifacePtr); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 199 | template <typename IType, typename ProxyType, typename StubType> |
| 200 | sp<IType> fromBinder(const sp<IBinder>& binderIface) { |
| 201 | using ::android::hidl::base::V1_0::IBase; |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 202 | using ::android::hidl::base::V1_0::BnHwBase; |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 203 | |
| 204 | if (binderIface.get() == nullptr) { |
| 205 | return nullptr; |
| 206 | } |
Steven Moreland | de0f413 | 2019-05-20 11:05:36 -0700 | [diff] [blame] | 207 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 208 | if (binderIface->localBinder() == nullptr) { |
| 209 | return new ProxyType(binderIface); |
| 210 | } |
Steven Moreland | de0f413 | 2019-05-20 11:05:36 -0700 | [diff] [blame] | 211 | |
| 212 | // Ensure that IBinder is BnHwBase (not JHwBinder, for instance) |
| 213 | if (!binderIface->checkSubclass(IBase::descriptor)) { |
| 214 | return new ProxyType(binderIface); |
| 215 | } |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 216 | sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl(); |
Steven Moreland | de0f413 | 2019-05-20 11:05:36 -0700 | [diff] [blame] | 217 | |
Yifan Hong | 2ac326f | 2017-02-22 18:13:06 -0800 | [diff] [blame] | 218 | if (details::canCastInterface(base.get(), IType::descriptor)) { |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 219 | StubType* stub = static_cast<StubType*>(binderIface.get()); |
| 220 | return stub->getImpl(); |
| 221 | } else { |
| 222 | return nullptr; |
| 223 | } |
| 224 | } |
| 225 | |
Steven Moreland | 6cf8fa2 | 2017-02-21 13:38:17 -0800 | [diff] [blame] | 226 | void configureBinderRpcThreadpool(size_t maxThreads, bool callerWillJoin); |
| 227 | void joinBinderRpcThreadpool(); |
Martijn Coenen | 3f5ac4c | 2017-11-27 15:09:28 -0800 | [diff] [blame] | 228 | int setupBinderPolling(); |
| 229 | status_t handleBinderPoll(); |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 230 | |
Steven Moreland | d40af8e | 2018-05-01 16:33:21 -0700 | [diff] [blame] | 231 | void addPostCommandTask(const std::function<void(void)> task); |
| 232 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 233 | } // namespace hardware |
| 234 | } // namespace android |
| 235 | |
| 236 | |
| 237 | #endif // ANDROID_HIDL_BINDER_SUPPORT_H |