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