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