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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "android_os_HwBinder" |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "android_os_HwBinder.h" |
| 22 | |
| 23 | #include "android_os_HwParcel.h" |
| 24 | #include "android_os_HwRemoteBinder.h" |
| 25 | |
Steven Moreland | afe95a9 | 2017-05-23 12:45:16 -0700 | [diff] [blame] | 26 | #include <cstring> |
| 27 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 28 | #include <nativehelper/JNIHelp.h> |
Steven Moreland | a813686 | 2016-10-24 13:44:41 -0700 | [diff] [blame] | 29 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Yifan Hong | bae1b55 | 2016-12-01 10:25:05 -0800 | [diff] [blame] | 30 | #include <android/hidl/base/1.0/IBase.h> |
Yifan Hong | 90b6a37 | 2017-01-09 17:58:33 -0800 | [diff] [blame] | 31 | #include <android/hidl/base/1.0/BpHwBase.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 32 | #include <android_runtime/AndroidRuntime.h> |
Steven Moreland | a813686 | 2016-10-24 13:44:41 -0700 | [diff] [blame] | 33 | #include <hidl/ServiceManagement.h> |
Martijn Coenen | aa2c32f | 2016-09-01 01:37:05 +0200 | [diff] [blame] | 34 | #include <hidl/Status.h> |
Martijn Coenen | 1298711 | 2016-12-08 10:48:19 +0100 | [diff] [blame] | 35 | #include <hidl/HidlTransportSupport.h> |
Steven Moreland | 64d7d94 | 2018-07-31 16:34:24 -0700 | [diff] [blame] | 36 | #include <hwbinder/IPCThreadState.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 37 | #include <hwbinder/ProcessState.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 38 | #include <nativehelper/ScopedLocalRef.h> |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 39 | #include <nativehelper/ScopedUtfChars.h> |
Yifan Hong | 0382be2 | 2017-02-06 12:43:08 -0800 | [diff] [blame] | 40 | #include <vintf/parse_string.h> |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 41 | #include <utils/misc.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 42 | |
| 43 | #include "core_jni_helpers.h" |
| 44 | |
| 45 | using android::AndroidRuntime; |
Steven Moreland | f3c5349 | 2016-11-03 15:17:04 -0700 | [diff] [blame] | 46 | using android::hardware::hidl_vec; |
| 47 | using android::hardware::hidl_string; |
Timur Iskhakov | b6a6283 | 2017-07-10 10:08:38 -0700 | [diff] [blame] | 48 | using android::hardware::IPCThreadState; |
| 49 | using android::hardware::ProcessState; |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 50 | template<typename T> |
| 51 | using Return = android::hardware::Return<T>; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 52 | |
| 53 | #define PACKAGE_PATH "android/os" |
| 54 | #define CLASS_NAME "HwBinder" |
| 55 | #define CLASS_PATH PACKAGE_PATH "/" CLASS_NAME |
| 56 | |
| 57 | namespace android { |
| 58 | |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 59 | static jclass gErrorClass; |
| 60 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 61 | static struct fields_t { |
| 62 | jfieldID contextID; |
| 63 | jmethodID onTransactID; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 64 | } gFields; |
| 65 | |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 66 | struct JHwBinderHolder : public RefBase { |
| 67 | JHwBinderHolder() {} |
| 68 | |
| 69 | sp<JHwBinder> get(JNIEnv *env, jobject obj) { |
| 70 | Mutex::Autolock autoLock(mLock); |
| 71 | |
| 72 | sp<JHwBinder> binder = mBinder.promote(); |
| 73 | |
| 74 | if (binder == NULL) { |
| 75 | binder = new JHwBinder(env, obj); |
| 76 | mBinder = binder; |
| 77 | } |
| 78 | |
| 79 | return binder; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | Mutex mLock; |
| 84 | wp<JHwBinder> mBinder; |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(JHwBinderHolder); |
| 87 | }; |
| 88 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 89 | // static |
| 90 | void JHwBinder::InitClass(JNIEnv *env) { |
| 91 | ScopedLocalRef<jclass> clazz( |
| 92 | env, FindClassOrDie(env, CLASS_PATH)); |
| 93 | |
| 94 | gFields.contextID = |
| 95 | GetFieldIDOrDie(env, clazz.get(), "mNativeContext", "J"); |
| 96 | |
| 97 | gFields.onTransactID = |
| 98 | GetMethodIDOrDie( |
| 99 | env, |
| 100 | clazz.get(), |
| 101 | "onTransact", |
| 102 | "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V"); |
| 103 | } |
| 104 | |
| 105 | // static |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 106 | sp<JHwBinderHolder> JHwBinder::SetNativeContext( |
| 107 | JNIEnv *env, jobject thiz, const sp<JHwBinderHolder> &context) { |
| 108 | sp<JHwBinderHolder> old = |
| 109 | (JHwBinderHolder *)env->GetLongField(thiz, gFields.contextID); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 110 | |
| 111 | if (context != NULL) { |
| 112 | context->incStrong(NULL /* id */); |
| 113 | } |
| 114 | |
| 115 | if (old != NULL) { |
| 116 | old->decStrong(NULL /* id */); |
| 117 | } |
| 118 | |
| 119 | env->SetLongField(thiz, gFields.contextID, (long)context.get()); |
| 120 | |
| 121 | return old; |
| 122 | } |
| 123 | |
| 124 | // static |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 125 | sp<JHwBinder> JHwBinder::GetNativeBinder( |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 126 | JNIEnv *env, jobject thiz) { |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 127 | JHwBinderHolder *holder = |
| 128 | reinterpret_cast<JHwBinderHolder *>( |
| 129 | env->GetLongField(thiz, gFields.contextID)); |
| 130 | |
| 131 | return holder->get(env, thiz); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | JHwBinder::JHwBinder(JNIEnv *env, jobject thiz) { |
| 135 | jclass clazz = env->GetObjectClass(thiz); |
| 136 | CHECK(clazz != NULL); |
| 137 | |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 138 | mObject = env->NewGlobalRef(thiz); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | JHwBinder::~JHwBinder() { |
| 142 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 143 | |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 144 | env->DeleteGlobalRef(mObject); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 145 | mObject = NULL; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | status_t JHwBinder::onTransact( |
| 149 | uint32_t code, |
| 150 | const hardware::Parcel &data, |
| 151 | hardware::Parcel *reply, |
| 152 | uint32_t flags, |
| 153 | TransactCallback callback) { |
| 154 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Martijn Coenen | 60bf84a | 2017-02-08 10:22:28 +0100 | [diff] [blame] | 155 | bool isOneway = (flags & TF_ONE_WAY) != 0; |
| 156 | ScopedLocalRef<jobject> replyObj(env, nullptr); |
| 157 | sp<JHwParcel> replyContext = nullptr; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 158 | |
| 159 | ScopedLocalRef<jobject> requestObj(env, JHwParcel::NewObject(env)); |
| 160 | JHwParcel::GetNativeContext(env, requestObj.get())->setParcel( |
| 161 | const_cast<hardware::Parcel *>(&data), false /* assumeOwnership */); |
| 162 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 163 | |
Martijn Coenen | 60bf84a | 2017-02-08 10:22:28 +0100 | [diff] [blame] | 164 | if (!isOneway) { |
| 165 | replyObj.reset(JHwParcel::NewObject(env)); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 166 | |
Martijn Coenen | 60bf84a | 2017-02-08 10:22:28 +0100 | [diff] [blame] | 167 | replyContext = JHwParcel::GetNativeContext(env, replyObj.get()); |
| 168 | |
| 169 | replyContext->setParcel(reply, false /* assumeOwnership */); |
| 170 | replyContext->setTransactCallback(callback); |
| 171 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 172 | |
| 173 | env->CallVoidMethod( |
| 174 | mObject, |
| 175 | gFields.onTransactID, |
| 176 | code, |
| 177 | requestObj.get(), |
| 178 | replyObj.get(), |
| 179 | flags); |
| 180 | |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 181 | if (env->ExceptionCheck()) { |
| 182 | jthrowable excep = env->ExceptionOccurred(); |
| 183 | env->ExceptionDescribe(); |
Aurimas Liutikas | f0aa0f1 | 2017-02-10 14:52:59 -0800 | [diff] [blame] | 184 | env->ExceptionClear(); |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 185 | |
Aurimas Liutikas | f0aa0f1 | 2017-02-10 14:52:59 -0800 | [diff] [blame] | 186 | // It is illegal to call IsInstanceOf if there is a pending exception. |
| 187 | // Attempting to do so results in a JniAbort which crashes the entire process. |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 188 | if (env->IsInstanceOf(excep, gErrorClass)) { |
| 189 | /* It's an error */ |
| 190 | LOG(ERROR) << "Forcefully exiting"; |
| 191 | exit(1); |
| 192 | } else { |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 193 | LOG(ERROR) << "Uncaught exception!"; |
| 194 | } |
| 195 | |
| 196 | env->DeleteLocalRef(excep); |
| 197 | } |
| 198 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 199 | status_t err = OK; |
| 200 | |
Martijn Coenen | 60bf84a | 2017-02-08 10:22:28 +0100 | [diff] [blame] | 201 | if (!isOneway) { |
| 202 | if (!replyContext->wasSent()) { |
| 203 | // The implementation never finished the transaction. |
| 204 | err = UNKNOWN_ERROR; // XXX special error code instead? |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 205 | |
Martijn Coenen | 60bf84a | 2017-02-08 10:22:28 +0100 | [diff] [blame] | 206 | reply->setDataPosition(0 /* pos */); |
| 207 | } |
| 208 | |
| 209 | // Release all temporary storage now that scatter-gather data |
| 210 | // has been consolidated, either by calling the TransactCallback, |
| 211 | // if wasSent() == true or clearing the reply parcel (setDataOffset above). |
| 212 | replyContext->getStorage()->release(env); |
| 213 | |
| 214 | // We cannot permanently pass ownership of "data" and "reply" over to their |
| 215 | // Java object wrappers (we don't own them ourselves). |
| 216 | replyContext->setParcel( |
| 217 | NULL /* parcel */, false /* assumeOwnership */); |
| 218 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 221 | JHwParcel::GetNativeContext(env, requestObj.get())->setParcel( |
| 222 | NULL /* parcel */, false /* assumeOwnership */); |
| 223 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 224 | return err; |
| 225 | } |
| 226 | |
Steven Moreland | 47942d9 | 2019-04-08 19:22:11 -0700 | [diff] [blame] | 227 | bool validateCanUseHwBinder(const sp<hardware::IBinder>& binder) { |
| 228 | if (binder != nullptr && binder->localBinder() != nullptr) { |
| 229 | // untested/unsupported/inefficient |
| 230 | // see b/129150021, doesn't work with scatter-gather |
| 231 | // |
| 232 | // explicitly disabling until it is supported |
| 233 | // (note, even if this is fixed to work with scatter gather, we would also need |
| 234 | // to convert this to the Java object rather than re-wrapping with a proxy) |
| 235 | LOG(ERROR) << "Local Java Binder not supported."; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 242 | } // namespace android |
| 243 | |
| 244 | //////////////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | using namespace android; |
| 247 | |
| 248 | static void releaseNativeContext(void *nativeContext) { |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 249 | sp<JHwBinderHolder> context = static_cast<JHwBinderHolder *>(nativeContext); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 250 | |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 251 | if (context != NULL) { |
| 252 | context->decStrong(NULL /* id */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | static jlong JHwBinder_native_init(JNIEnv *env) { |
| 257 | JHwBinder::InitClass(env); |
| 258 | |
| 259 | return reinterpret_cast<jlong>(&releaseNativeContext); |
| 260 | } |
| 261 | |
| 262 | static void JHwBinder_native_setup(JNIEnv *env, jobject thiz) { |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 263 | sp<JHwBinderHolder> context = new JHwBinderHolder; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 264 | JHwBinder::SetNativeContext(env, thiz, context); |
| 265 | } |
| 266 | |
| 267 | static void JHwBinder_native_transact( |
| 268 | JNIEnv * /* env */, |
| 269 | jobject /* thiz */, |
| 270 | jint /* code */, |
| 271 | jobject /* requestObj */, |
| 272 | jobject /* replyObj */, |
| 273 | jint /* flags */) { |
| 274 | CHECK(!"Should not be here"); |
| 275 | } |
| 276 | |
| 277 | static void JHwBinder_native_registerService( |
Andreas Huber | 35eb799 | 2016-10-25 13:29:30 -0700 | [diff] [blame] | 278 | JNIEnv *env, |
| 279 | jobject thiz, |
Steven Moreland | f3c5349 | 2016-11-03 15:17:04 -0700 | [diff] [blame] | 280 | jstring serviceNameObj) { |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 281 | ScopedUtfChars str(env, serviceNameObj); |
| 282 | if (str.c_str() == nullptr) { |
| 283 | return; // NPE will be pending. |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 286 | sp<hardware::IBinder> binder = JHwBinder::GetNativeBinder(env, thiz); |
Steven Moreland | 520d12c | 2016-12-15 15:50:17 -0800 | [diff] [blame] | 287 | |
Martijn Coenen | 1298711 | 2016-12-08 10:48:19 +0100 | [diff] [blame] | 288 | /* TODO(b/33440494) this is not right */ |
Yifan Hong | 90b6a37 | 2017-01-09 17:58:33 -0800 | [diff] [blame] | 289 | sp<hidl::base::V1_0::IBase> base = new hidl::base::V1_0::BpHwBase(binder); |
Steven Moreland | 520d12c | 2016-12-15 15:50:17 -0800 | [diff] [blame] | 290 | |
| 291 | auto manager = hardware::defaultServiceManager(); |
| 292 | |
| 293 | if (manager == nullptr) { |
| 294 | LOG(ERROR) << "Could not get hwservicemanager."; |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 295 | signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */); |
Steven Moreland | 520d12c | 2016-12-15 15:50:17 -0800 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 299 | Return<bool> ret = manager->add(str.c_str(), base); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 300 | |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 301 | bool ok = ret.isOk() && ret; |
| 302 | |
Steven Moreland | a813686 | 2016-10-24 13:44:41 -0700 | [diff] [blame] | 303 | if (ok) { |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 304 | LOG(INFO) << "HwBinder: Starting thread pool for " << str.c_str(); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 305 | ::android::hardware::ProcessState::self()->startThreadPool(); |
| 306 | } |
| 307 | |
Steven Moreland | c0631d0 | 2017-01-04 10:37:59 -0800 | [diff] [blame] | 308 | signalExceptionForError(env, (ok ? OK : UNKNOWN_ERROR), true /* canThrowRemoteException */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static jobject JHwBinder_native_getService( |
Andreas Huber | 35eb799 | 2016-10-25 13:29:30 -0700 | [diff] [blame] | 312 | JNIEnv *env, |
| 313 | jclass /* clazzObj */, |
Steven Moreland | f3c5349 | 2016-11-03 15:17:04 -0700 | [diff] [blame] | 314 | jstring ifaceNameObj, |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 315 | jstring serviceNameObj, |
| 316 | jboolean retry) { |
Steven Moreland | f3c5349 | 2016-11-03 15:17:04 -0700 | [diff] [blame] | 317 | |
Steven Moreland | f8202e4 | 2017-04-06 09:28:32 -0700 | [diff] [blame] | 318 | using ::android::hidl::base::V1_0::IBase; |
Steven Moreland | c4bfdef | 2017-10-18 23:19:43 -0700 | [diff] [blame] | 319 | using ::android::hardware::details::getRawServiceInternal; |
Yifan Hong | 0382be2 | 2017-02-06 12:43:08 -0800 | [diff] [blame] | 320 | |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 321 | std::string ifaceName; |
| 322 | { |
| 323 | ScopedUtfChars str(env, ifaceNameObj); |
| 324 | if (str.c_str() == nullptr) { |
| 325 | return nullptr; // NPE will be pending. |
| 326 | } |
| 327 | ifaceName = str.c_str(); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Andreas Gampe | 63c0193 | 2017-10-25 13:03:24 -0700 | [diff] [blame] | 330 | std::string serviceName; |
| 331 | { |
| 332 | ScopedUtfChars str(env, serviceNameObj); |
| 333 | if (str.c_str() == nullptr) { |
| 334 | return nullptr; // NPE will be pending. |
| 335 | } |
| 336 | serviceName = str.c_str(); |
Andreas Huber | 35eb799 | 2016-10-25 13:29:30 -0700 | [diff] [blame] | 337 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 338 | |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 339 | sp<IBase> ret = getRawServiceInternal(ifaceName, serviceName, retry /* retry */, false /* getStub */); |
Steven Moreland | 72be156 | 2017-07-21 19:29:54 +0000 | [diff] [blame] | 340 | sp<hardware::IBinder> service = hardware::toBinder<hidl::base::V1_0::IBase>(ret); |
Steven Moreland | 2f379ca | 2017-02-01 09:58:00 -0800 | [diff] [blame] | 341 | |
Steven Moreland | 47942d9 | 2019-04-08 19:22:11 -0700 | [diff] [blame] | 342 | if (service == nullptr || !validateCanUseHwBinder(service)) { |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 343 | signalExceptionForError(env, NAME_NOT_FOUND); |
Steven Moreland | 47942d9 | 2019-04-08 19:22:11 -0700 | [diff] [blame] | 344 | return nullptr; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Steven Moreland | 0ae1e63 | 2019-01-10 12:16:03 -0800 | [diff] [blame] | 347 | LOG(INFO) << "HwBinder: Starting thread pool for getting: " << ifaceName << "/" << serviceName; |
Andreas Huber | 781c083 | 2016-10-05 11:10:26 -0700 | [diff] [blame] | 348 | ::android::hardware::ProcessState::self()->startThreadPool(); |
| 349 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 350 | return JHwRemoteBinder::NewObject(env, service); |
| 351 | } |
| 352 | |
Yifan Hong | 029b08c | 2017-10-25 13:19:22 -0700 | [diff] [blame] | 353 | void JHwBinder_native_configureRpcThreadpool(JNIEnv *, jclass, |
| 354 | jlong maxThreads, jboolean callerWillJoin) { |
Timur Iskhakov | b6a6283 | 2017-07-10 10:08:38 -0700 | [diff] [blame] | 355 | CHECK(maxThreads > 0); |
| 356 | ProcessState::self()->setThreadPoolConfiguration(maxThreads, callerWillJoin /*callerJoinsPool*/); |
| 357 | } |
| 358 | |
| 359 | void JHwBinder_native_joinRpcThreadpool() { |
| 360 | IPCThreadState::self()->joinThreadPool(); |
| 361 | } |
| 362 | |
Yifan Hong | 029b08c | 2017-10-25 13:19:22 -0700 | [diff] [blame] | 363 | static void JHwBinder_report_sysprop_change(JNIEnv * /*env*/, jclass /*clazz*/) |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 364 | { |
| 365 | report_sysprop_change(); |
| 366 | } |
| 367 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 368 | static JNINativeMethod gMethods[] = { |
| 369 | { "native_init", "()J", (void *)JHwBinder_native_init }, |
| 370 | { "native_setup", "()V", (void *)JHwBinder_native_setup }, |
| 371 | |
| 372 | { "transact", |
| 373 | "(IL" PACKAGE_PATH "/HwParcel;L" PACKAGE_PATH "/HwParcel;I)V", |
| 374 | (void *)JHwBinder_native_transact }, |
| 375 | |
Martijn Coenen | 85d12da | 2017-03-06 13:07:53 +0100 | [diff] [blame] | 376 | { "registerService", "(Ljava/lang/String;)V", |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 377 | (void *)JHwBinder_native_registerService }, |
| 378 | |
Steven Moreland | ec5eb7e | 2017-10-25 04:50:46 +0000 | [diff] [blame] | 379 | { "getService", "(Ljava/lang/String;Ljava/lang/String;Z)L" PACKAGE_PATH "/IHwBinder;", |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 380 | (void *)JHwBinder_native_getService }, |
Timur Iskhakov | b6a6283 | 2017-07-10 10:08:38 -0700 | [diff] [blame] | 381 | |
| 382 | { "configureRpcThreadpool", "(JZ)V", |
| 383 | (void *)JHwBinder_native_configureRpcThreadpool }, |
| 384 | |
| 385 | { "joinRpcThreadpool", "()V", |
| 386 | (void *)JHwBinder_native_joinRpcThreadpool }, |
Sundong Ahn | 28cc6e8 | 2017-07-13 16:34:01 +0900 | [diff] [blame] | 387 | |
| 388 | { "native_report_sysprop_change", "()V", |
| 389 | (void *)JHwBinder_report_sysprop_change }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | namespace android { |
| 393 | |
| 394 | int register_android_os_HwBinder(JNIEnv *env) { |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 395 | jclass errorClass = FindClassOrDie(env, "java/lang/Error"); |
| 396 | gErrorClass = MakeGlobalRefOrDie(env, errorClass); |
| 397 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 398 | return RegisterMethodsOrDie(env, CLASS_PATH, gMethods, NELEM(gMethods)); |
| 399 | } |
| 400 | |
| 401 | } // namespace android |