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_HwParcel" |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "android_os_HwParcel.h" |
| 22 | |
| 23 | #include "android_os_HwBinder.h" |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 24 | #include "android_os_HwBlob.h" |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 25 | #include "android_os_NativeHandle.h" |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 26 | #include "android_os_HwRemoteBinder.h" |
| 27 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 28 | #include <nativehelper/JNIHelp.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 29 | #include <android_runtime/AndroidRuntime.h> |
Yifan Hong | ddf31f6 | 2016-11-15 16:02:40 -0800 | [diff] [blame] | 30 | #include <hidl/HidlTransportSupport.h> |
Martijn Coenen | aa2c32f | 2016-09-01 01:37:05 +0200 | [diff] [blame] | 31 | #include <hidl/Status.h> |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 32 | #include <nativehelper/ScopedLocalRef.h> |
| 33 | |
| 34 | #include "core_jni_helpers.h" |
| 35 | |
| 36 | using android::AndroidRuntime; |
| 37 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 38 | using ::android::hardware::hidl_handle; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 39 | using ::android::hardware::hidl_string; |
| 40 | using ::android::hardware::hidl_vec; |
| 41 | |
| 42 | #define PACKAGE_PATH "android/os" |
| 43 | #define CLASS_NAME "HwParcel" |
| 44 | #define CLASS_PATH PACKAGE_PATH "/" CLASS_NAME |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | static struct fields_t { |
| 49 | jfieldID contextID; |
| 50 | jmethodID constructID; |
| 51 | |
| 52 | } gFields; |
| 53 | |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 54 | void signalExceptionForError(JNIEnv *env, status_t err, bool canThrowRemoteException) { |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 55 | switch (err) { |
| 56 | case OK: |
| 57 | break; |
| 58 | |
| 59 | case NO_MEMORY: |
| 60 | { |
| 61 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | case INVALID_OPERATION: |
| 66 | { |
| 67 | jniThrowException( |
| 68 | env, "java/lang/UnsupportedOperationException", NULL); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | case BAD_VALUE: |
| 73 | { |
| 74 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 75 | break; |
| 76 | } |
| 77 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 78 | case -ERANGE: |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 79 | case BAD_INDEX: |
| 80 | { |
| 81 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | case BAD_TYPE: |
| 86 | { |
| 87 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | case NAME_NOT_FOUND: |
| 92 | { |
| 93 | jniThrowException(env, "java/util/NoSuchElementException", NULL); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | case PERMISSION_DENIED: |
| 98 | { |
| 99 | jniThrowException(env, "java/lang/SecurityException", NULL); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | case NO_INIT: |
| 104 | { |
| 105 | jniThrowException( |
| 106 | env, "java/lang/RuntimeException", "Not initialized"); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | case ALREADY_EXISTS: |
| 111 | { |
| 112 | jniThrowException( |
| 113 | env, "java/lang/RuntimeException", "Item already exists"); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | default: |
| 118 | { |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 119 | std::stringstream ss; |
| 120 | ss << "HwBinder Error: (" << err << ")"; |
| 121 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 122 | jniThrowException( |
Steven Moreland | e62b1f3 | 2016-12-20 15:55:48 -0800 | [diff] [blame] | 123 | env, |
| 124 | canThrowRemoteException ? "android/os/RemoteException" : "java/lang/RuntimeException", |
| 125 | ss.str().c_str()); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 126 | |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // static |
| 133 | void JHwParcel::InitClass(JNIEnv *env) { |
| 134 | ScopedLocalRef<jclass> clazz( |
| 135 | env, FindClassOrDie(env, CLASS_PATH)); |
| 136 | |
| 137 | gFields.contextID = |
| 138 | GetFieldIDOrDie(env, clazz.get(), "mNativeContext", "J"); |
| 139 | |
| 140 | gFields.constructID = GetMethodIDOrDie(env, clazz.get(), "<init>", "(Z)V"); |
| 141 | } |
| 142 | |
| 143 | // static |
| 144 | sp<JHwParcel> JHwParcel::SetNativeContext( |
| 145 | JNIEnv *env, jobject thiz, const sp<JHwParcel> &context) { |
| 146 | sp<JHwParcel> old = (JHwParcel *)env->GetLongField(thiz, gFields.contextID); |
| 147 | |
| 148 | if (context != NULL) { |
| 149 | context->incStrong(NULL /* id */); |
| 150 | } |
| 151 | |
| 152 | if (old != NULL) { |
| 153 | old->decStrong(NULL /* id */); |
| 154 | } |
| 155 | |
| 156 | env->SetLongField(thiz, gFields.contextID, (long)context.get()); |
| 157 | |
| 158 | return old; |
| 159 | } |
| 160 | |
| 161 | // static |
| 162 | sp<JHwParcel> JHwParcel::GetNativeContext(JNIEnv *env, jobject thiz) { |
| 163 | return (JHwParcel *)env->GetLongField(thiz, gFields.contextID); |
| 164 | } |
| 165 | |
| 166 | JHwParcel::JHwParcel(JNIEnv *env, jobject thiz) |
| 167 | : mParcel(NULL), |
| 168 | mOwnsParcel(false), |
| 169 | mTransactCallback(nullptr), |
| 170 | mWasSent(false) { |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | JHwParcel::~JHwParcel() { |
| 174 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 175 | |
| 176 | mStorage.release(env); |
| 177 | |
| 178 | setParcel(NULL, false /* assumeOwnership */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | hardware::Parcel *JHwParcel::getParcel() { |
| 182 | return mParcel; |
| 183 | } |
| 184 | |
| 185 | EphemeralStorage *JHwParcel::getStorage() { |
| 186 | return &mStorage; |
| 187 | } |
| 188 | |
| 189 | void JHwParcel::setParcel(hardware::Parcel *parcel, bool assumeOwnership) { |
| 190 | if (mParcel && mOwnsParcel) { |
| 191 | delete mParcel; |
| 192 | } |
| 193 | |
| 194 | mParcel = parcel; |
| 195 | mOwnsParcel = assumeOwnership; |
| 196 | } |
| 197 | |
| 198 | // static |
| 199 | jobject JHwParcel::NewObject(JNIEnv *env) { |
| 200 | ScopedLocalRef<jclass> clazz(env, FindClassOrDie(env, CLASS_PATH)); |
| 201 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 202 | jmethodID constructID = |
| 203 | GetMethodIDOrDie(env, clazz.get(), "<init>", "(Z)V"); |
| 204 | |
| 205 | return env->NewObject(clazz.get(), constructID, false /* allocate */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void JHwParcel::setTransactCallback( |
| 209 | ::android::hardware::IBinder::TransactCallback cb) { |
| 210 | mTransactCallback = cb; |
| 211 | } |
| 212 | |
| 213 | void JHwParcel::send() { |
| 214 | CHECK(mTransactCallback != nullptr); |
| 215 | CHECK(mParcel != nullptr); |
| 216 | |
| 217 | mTransactCallback(*mParcel); |
| 218 | mTransactCallback = nullptr; |
| 219 | |
| 220 | mWasSent = true; |
| 221 | } |
| 222 | |
| 223 | bool JHwParcel::wasSent() const { |
| 224 | return mWasSent; |
| 225 | } |
| 226 | |
| 227 | } // namespace android |
| 228 | |
| 229 | //////////////////////////////////////////////////////////////////////////////// |
| 230 | |
| 231 | using namespace android; |
| 232 | |
| 233 | static void releaseNativeContext(void *nativeContext) { |
| 234 | sp<JHwParcel> parcel = (JHwParcel *)nativeContext; |
| 235 | |
| 236 | if (parcel != NULL) { |
| 237 | parcel->decStrong(NULL /* id */); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static jlong JHwParcel_native_init(JNIEnv *env) { |
| 242 | JHwParcel::InitClass(env); |
| 243 | |
| 244 | return reinterpret_cast<jlong>(&releaseNativeContext); |
| 245 | } |
| 246 | |
| 247 | static void JHwParcel_native_setup( |
| 248 | JNIEnv *env, jobject thiz, jboolean allocate) { |
| 249 | sp<JHwParcel> context = new JHwParcel(env, thiz); |
| 250 | |
| 251 | if (allocate) { |
| 252 | context->setParcel(new hardware::Parcel, true /* assumeOwnership */); |
| 253 | } |
| 254 | |
| 255 | JHwParcel::SetNativeContext(env, thiz, context); |
| 256 | } |
| 257 | |
| 258 | static void JHwParcel_native_writeInterfaceToken( |
| 259 | JNIEnv *env, jobject thiz, jstring interfaceNameObj) { |
| 260 | if (interfaceNameObj == NULL) { |
| 261 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | const jchar *interfaceName = env->GetStringCritical(interfaceNameObj, NULL); |
| 266 | if (interfaceName) { |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 267 | String8 nameCopy = String8(String16( |
Andreas Huber | 9f02686 | 2016-11-03 15:46:04 -0700 | [diff] [blame] | 268 | reinterpret_cast<const char16_t *>(interfaceName), |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 269 | env->GetStringLength(interfaceNameObj))); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 270 | |
| 271 | env->ReleaseStringCritical(interfaceNameObj, interfaceName); |
| 272 | interfaceName = NULL; |
| 273 | |
Andreas Huber | 9f02686 | 2016-11-03 15:46:04 -0700 | [diff] [blame] | 274 | hardware::Parcel *parcel = |
| 275 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 276 | |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 277 | status_t err = parcel->writeInterfaceToken(nameCopy.string()); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 278 | signalExceptionForError(env, err); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static void JHwParcel_native_enforceInterface( |
| 283 | JNIEnv *env, jobject thiz, jstring interfaceNameObj) { |
| 284 | // XXX original binder Parcel enforceInterface implementation does some |
| 285 | // mysterious things regarding strictModePolicy(), figure out if we need |
| 286 | // that here as well. |
| 287 | if (interfaceNameObj == NULL) { |
| 288 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | const jchar *interfaceName = env->GetStringCritical(interfaceNameObj, NULL); |
| 293 | if (interfaceName) { |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 294 | String8 interfaceNameCopy = String8(String16( |
Andreas Huber | 9f02686 | 2016-11-03 15:46:04 -0700 | [diff] [blame] | 295 | reinterpret_cast<const char16_t *>(interfaceName), |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 296 | env->GetStringLength(interfaceNameObj))); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 297 | |
| 298 | env->ReleaseStringCritical(interfaceNameObj, interfaceName); |
| 299 | interfaceName = NULL; |
| 300 | |
Andreas Huber | 9f02686 | 2016-11-03 15:46:04 -0700 | [diff] [blame] | 301 | hardware::Parcel *parcel = |
| 302 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 303 | |
Steven Moreland | ab2fca2 | 2016-11-11 12:33:41 -0800 | [diff] [blame] | 304 | bool valid = parcel->enforceInterface(interfaceNameCopy.string()); |
Andreas Huber | 9f02686 | 2016-11-03 15:46:04 -0700 | [diff] [blame] | 305 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 306 | if (!valid) { |
| 307 | jniThrowException( |
| 308 | env, |
| 309 | "java/lang/SecurityException", |
| 310 | "HWBinder invocation to an incorrect interface"); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | #define DEFINE_PARCEL_WRITER(Suffix,Type) \ |
| 316 | static void JHwParcel_native_write ## Suffix( \ |
| 317 | JNIEnv *env, jobject thiz, Type val) { \ |
| 318 | hardware::Parcel *parcel = \ |
| 319 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); \ |
| 320 | \ |
| 321 | status_t err = parcel->write ## Suffix(val); \ |
| 322 | signalExceptionForError(env, err); \ |
| 323 | } |
| 324 | |
| 325 | #define DEFINE_PARCEL_READER(Suffix,Type) \ |
| 326 | static Type JHwParcel_native_read ## Suffix( \ |
| 327 | JNIEnv *env, jobject thiz) { \ |
| 328 | hardware::Parcel *parcel = \ |
| 329 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); \ |
| 330 | \ |
| 331 | Type val; \ |
| 332 | status_t err = parcel->read ## Suffix(&val); \ |
| 333 | signalExceptionForError(env, err); \ |
| 334 | \ |
| 335 | return val; \ |
| 336 | } |
| 337 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 338 | DEFINE_PARCEL_WRITER(Bool,jboolean) |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 339 | DEFINE_PARCEL_WRITER(Int8,jbyte) |
| 340 | DEFINE_PARCEL_WRITER(Int16,jshort) |
| 341 | DEFINE_PARCEL_WRITER(Int32,jint) |
| 342 | DEFINE_PARCEL_WRITER(Int64,jlong) |
| 343 | DEFINE_PARCEL_WRITER(Float,jfloat) |
| 344 | DEFINE_PARCEL_WRITER(Double,jdouble) |
| 345 | |
| 346 | DEFINE_PARCEL_READER(Int8,jbyte) |
| 347 | DEFINE_PARCEL_READER(Int16,jshort) |
| 348 | DEFINE_PARCEL_READER(Int32,jint) |
| 349 | DEFINE_PARCEL_READER(Int64,jlong) |
| 350 | DEFINE_PARCEL_READER(Float,jfloat) |
| 351 | DEFINE_PARCEL_READER(Double,jdouble) |
| 352 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 353 | static jboolean JHwParcel_native_readBool(JNIEnv *env, jobject thiz) { |
| 354 | hardware::Parcel *parcel = |
| 355 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 356 | |
| 357 | bool val; |
| 358 | status_t err = parcel->readBool(&val); |
| 359 | signalExceptionForError(env, err); |
| 360 | |
| 361 | return (jboolean)val; |
| 362 | } |
| 363 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 364 | static void JHwParcel_native_writeStatus( |
| 365 | JNIEnv *env, jobject thiz, jint statusCode) { |
| 366 | using hardware::Status; |
| 367 | |
| 368 | Status status; |
| 369 | switch (statusCode) { |
| 370 | case 0: // kStatusSuccess |
| 371 | status = Status::ok(); |
| 372 | break; |
| 373 | case -1: // kStatusError |
| 374 | status = Status::fromStatusT(UNKNOWN_ERROR); |
| 375 | break; |
| 376 | default: |
| 377 | CHECK(!"Should not be here"); |
| 378 | } |
| 379 | |
| 380 | hardware::Parcel *parcel = |
| 381 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 382 | |
Yifan Hong | ddf31f6 | 2016-11-15 16:02:40 -0800 | [diff] [blame] | 383 | status_t err = ::android::hardware::writeToParcel(status, parcel); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 384 | signalExceptionForError(env, err); |
| 385 | } |
| 386 | |
| 387 | static void JHwParcel_native_verifySuccess(JNIEnv *env, jobject thiz) { |
| 388 | using hardware::Status; |
| 389 | |
| 390 | hardware::Parcel *parcel = |
| 391 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 392 | |
| 393 | Status status; |
Yifan Hong | ddf31f6 | 2016-11-15 16:02:40 -0800 | [diff] [blame] | 394 | status_t err = ::android::hardware::readFromParcel(&status, *parcel); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 395 | signalExceptionForError(env, err); |
Steven Moreland | 2ef0cbd | 2018-01-17 13:26:35 -0800 | [diff] [blame] | 396 | |
| 397 | if (!status.isOk()) { |
| 398 | signalExceptionForError(env, UNKNOWN_ERROR, true /* canThrowRemoteException */); |
| 399 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Martijn Coenen | 3d726d1 | 2017-03-16 18:46:42 +0100 | [diff] [blame] | 402 | static void JHwParcel_native_release( |
| 403 | JNIEnv *env, jobject thiz) { |
| 404 | JHwParcel::GetNativeContext(env, thiz)->setParcel(NULL, false /* assumeOwnership */); |
| 405 | } |
| 406 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 407 | static void JHwParcel_native_releaseTemporaryStorage( |
| 408 | JNIEnv *env, jobject thiz) { |
| 409 | JHwParcel::GetNativeContext(env, thiz)->getStorage()->release(env); |
| 410 | } |
| 411 | |
| 412 | static void JHwParcel_native_send(JNIEnv *env, jobject thiz) { |
| 413 | JHwParcel::GetNativeContext(env, thiz)->send(); |
| 414 | } |
| 415 | |
| 416 | static void JHwParcel_native_writeString( |
| 417 | JNIEnv *env, jobject thiz, jstring valObj) { |
| 418 | if (valObj == NULL) { |
| 419 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); |
| 424 | |
| 425 | const hidl_string *s = |
| 426 | impl->getStorage()->allocTemporaryString(env, valObj); |
| 427 | |
| 428 | hardware::Parcel *parcel = impl->getParcel(); |
| 429 | |
| 430 | size_t parentHandle; |
| 431 | status_t err = parcel->writeBuffer(s, sizeof(*s), &parentHandle); |
| 432 | |
| 433 | if (err == OK) { |
Yifan Hong | 3bb6578 | 2016-11-11 17:20:54 -0800 | [diff] [blame] | 434 | err = ::android::hardware::writeEmbeddedToParcel( |
| 435 | *s, parcel, parentHandle, 0 /* parentOffset */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | signalExceptionForError(env, err); |
| 439 | } |
| 440 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 441 | static void JHwParcel_native_writeNativeHandle(JNIEnv *env, jobject thiz, jobject valObj) { |
| 442 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); |
| 443 | |
| 444 | EphemeralStorage *storage = impl->getStorage(); |
| 445 | native_handle_t *handle = JNativeHandle::MakeCppNativeHandle(env, valObj, storage); |
| 446 | |
| 447 | hardware::Parcel *parcel = impl->getParcel(); |
| 448 | status_t err = parcel->writeNativeHandleNoDup(handle); |
| 449 | |
| 450 | signalExceptionForError(env, err); |
| 451 | } |
| 452 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 453 | #define DEFINE_PARCEL_VECTOR_WRITER(Suffix,Type) \ |
| 454 | static void JHwParcel_native_write ## Suffix ## Vector( \ |
| 455 | JNIEnv *env, jobject thiz, Type ## Array valObj) { \ |
| 456 | if (valObj == NULL) { \ |
| 457 | jniThrowException(env, "java/lang/NullPointerException", NULL); \ |
| 458 | return; \ |
| 459 | } \ |
| 460 | \ |
| 461 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); \ |
| 462 | \ |
| 463 | const hidl_vec<Type> *vec = \ |
| 464 | impl->getStorage()->allocTemporary ## Suffix ## Vector(env, valObj); \ |
| 465 | \ |
| 466 | hardware::Parcel *parcel = impl->getParcel(); \ |
| 467 | \ |
| 468 | size_t parentHandle; \ |
| 469 | status_t err = parcel->writeBuffer(vec, sizeof(*vec), &parentHandle); \ |
| 470 | \ |
| 471 | if (err == OK) { \ |
| 472 | size_t childHandle; \ |
| 473 | \ |
Yifan Hong | 3bb6578 | 2016-11-11 17:20:54 -0800 | [diff] [blame] | 474 | err = ::android::hardware::writeEmbeddedToParcel( \ |
| 475 | *vec, \ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 476 | parcel, \ |
| 477 | parentHandle, \ |
| 478 | 0 /* parentOffset */, \ |
| 479 | &childHandle); \ |
| 480 | } \ |
| 481 | \ |
| 482 | signalExceptionForError(env, err); \ |
| 483 | } |
| 484 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 485 | DEFINE_PARCEL_VECTOR_WRITER(Int8,jbyte) |
| 486 | DEFINE_PARCEL_VECTOR_WRITER(Int16,jshort) |
| 487 | DEFINE_PARCEL_VECTOR_WRITER(Int32,jint) |
| 488 | DEFINE_PARCEL_VECTOR_WRITER(Int64,jlong) |
| 489 | DEFINE_PARCEL_VECTOR_WRITER(Float,jfloat) |
| 490 | DEFINE_PARCEL_VECTOR_WRITER(Double,jdouble) |
| 491 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 492 | static void JHwParcel_native_writeBoolVector( |
| 493 | JNIEnv *env, jobject thiz, jbooleanArray valObj) { |
| 494 | if (valObj == NULL) { |
| 495 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); |
| 500 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 501 | void *vecPtr = |
| 502 | impl->getStorage()->allocTemporaryStorage(sizeof(hidl_vec<bool>)); |
| 503 | |
| 504 | hidl_vec<bool> *vec = new (vecPtr) hidl_vec<bool>; |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 505 | |
| 506 | jsize len = env->GetArrayLength(valObj); |
| 507 | |
| 508 | jboolean *src = env->GetBooleanArrayElements(valObj, nullptr); |
| 509 | |
| 510 | bool *dst = |
| 511 | (bool *)impl->getStorage()->allocTemporaryStorage(len * sizeof(bool)); |
| 512 | |
| 513 | for (jsize i = 0; i < len; ++i) { |
| 514 | dst[i] = src[i]; |
| 515 | } |
| 516 | |
| 517 | env->ReleaseBooleanArrayElements(valObj, src, 0 /* mode */); |
| 518 | src = nullptr; |
| 519 | |
| 520 | vec->setToExternal(dst, len); |
| 521 | |
| 522 | hardware::Parcel *parcel = impl->getParcel(); |
| 523 | |
| 524 | size_t parentHandle; |
| 525 | status_t err = parcel->writeBuffer(vec, sizeof(*vec), &parentHandle); |
| 526 | |
| 527 | if (err == OK) { |
| 528 | size_t childHandle; |
| 529 | |
Yifan Hong | 3bb6578 | 2016-11-11 17:20:54 -0800 | [diff] [blame] | 530 | err = ::android::hardware::writeEmbeddedToParcel( |
| 531 | *vec, |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 532 | parcel, |
| 533 | parentHandle, |
| 534 | 0 /* parentOffset */, |
| 535 | &childHandle); |
| 536 | } |
| 537 | |
| 538 | signalExceptionForError(env, err); |
| 539 | } |
| 540 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 541 | template<typename T> |
| 542 | static void WriteHidlVector(JNIEnv *env, jobject thiz, const hidl_vec<T> &vec) { |
| 543 | hardware::Parcel *parcel = JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 544 | |
| 545 | size_t parentHandle; |
| 546 | status_t err = parcel->writeBuffer(&vec, sizeof(vec), &parentHandle); |
| 547 | |
| 548 | if (err == OK) { |
| 549 | size_t childHandle; |
| 550 | err = ::android::hardware::writeEmbeddedToParcel( |
| 551 | vec, |
| 552 | parcel, |
| 553 | parentHandle, |
| 554 | 0 /* parentOffset */, |
| 555 | &childHandle); |
| 556 | |
| 557 | for (size_t i = 0; (err == OK) && (i < vec.size()); ++i) { |
| 558 | err = ::android::hardware::writeEmbeddedToParcel( |
| 559 | vec[i], |
| 560 | parcel, |
| 561 | childHandle, |
| 562 | i * sizeof(T)); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | signalExceptionForError(env, err); |
| 567 | } |
| 568 | |
| 569 | static void JHwParcel_native_writeStringVector( |
| 570 | JNIEnv *env, jobject thiz, jobjectArray arrayObj) { |
| 571 | if (arrayObj == nullptr) { |
| 572 | jniThrowException(env, "java/lang/NullPointerException", nullptr); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); |
| 577 | EphemeralStorage *storage = impl->getStorage(); |
| 578 | |
| 579 | void *vecPtr = storage->allocTemporaryStorage(sizeof(hidl_vec<hidl_string>)); |
| 580 | hidl_vec<hidl_string> *vec = new (vecPtr) hidl_vec<hidl_string>(); |
| 581 | |
| 582 | jsize len = env->GetArrayLength(arrayObj); |
| 583 | hidl_string *strings = storage->allocStringArray(len); |
| 584 | vec->setToExternal(strings, len, false /* shouldOwn */); |
| 585 | |
| 586 | for (jsize i = 0; i < len; ++i) { |
| 587 | ScopedLocalRef<jstring> stringObj(env, (jstring) env->GetObjectArrayElement(arrayObj, i)); |
| 588 | |
| 589 | const hidl_string *s = storage->allocTemporaryString(env, stringObj.get()); |
| 590 | strings[i].setToExternal(s->c_str(), s->size()); |
| 591 | } |
| 592 | |
| 593 | WriteHidlVector(env, thiz, *vec); |
| 594 | } |
| 595 | |
| 596 | static void JHwParcel_native_writeNativeHandleVector( |
| 597 | JNIEnv *env, jobject thiz, jobjectArray jHandleArray) { |
| 598 | if (jHandleArray == nullptr) { |
| 599 | jniThrowException(env, "java/lang/NullPointerException", nullptr); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | sp<JHwParcel> impl = JHwParcel::GetNativeContext(env, thiz); |
| 604 | EphemeralStorage *storage = impl->getStorage(); |
| 605 | |
| 606 | void *vecPtr = storage->allocTemporaryStorage(sizeof(hidl_vec<hidl_handle>)); |
| 607 | hidl_vec<hidl_handle> *vec = new (vecPtr) hidl_vec<hidl_handle>(); |
| 608 | |
| 609 | jsize len = env->GetArrayLength(jHandleArray); |
| 610 | hidl_handle *handles = static_cast<hidl_handle *>( |
| 611 | storage->allocTemporaryStorage(len * sizeof(hidl_handle))); |
| 612 | |
| 613 | vec->setToExternal(handles, len, false /* shouldOwn */); |
| 614 | for (jsize i = 0; i < len; i++) { |
| 615 | ScopedLocalRef<jobject> jHandle(env, env->GetObjectArrayElement(jHandleArray, i)); |
| 616 | |
| 617 | native_handle_t* handle = JNativeHandle::MakeCppNativeHandle(env, jHandle.get(), storage); |
| 618 | |
| 619 | new (&(handles[i])) hidl_handle(); |
| 620 | handles[i].setTo(handle, false /* shouldOwn */); |
| 621 | } |
| 622 | |
| 623 | WriteHidlVector(env, thiz, *vec); |
| 624 | } |
| 625 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 626 | static void JHwParcel_native_writeStrongBinder( |
| 627 | JNIEnv *env, jobject thiz, jobject binderObj) { |
| 628 | sp<hardware::IBinder> binder; |
| 629 | if (binderObj != NULL) { |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 630 | ScopedLocalRef<jclass> hwBinderKlass(env, FindClassOrDie(env, PACKAGE_PATH "/HwBinder")); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 631 | |
| 632 | ScopedLocalRef<jclass> hwRemoteBinderKlass( |
| 633 | env, FindClassOrDie(env, PACKAGE_PATH "/HwRemoteBinder")); |
| 634 | |
| 635 | if (env->IsInstanceOf(binderObj, hwBinderKlass.get())) { |
Andreas Huber | 56bdb8a | 2017-06-16 14:56:12 -0700 | [diff] [blame] | 636 | binder = JHwBinder::GetNativeBinder(env, binderObj); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 637 | } else if (env->IsInstanceOf(binderObj, hwRemoteBinderKlass.get())) { |
| 638 | binder = JHwRemoteBinder::GetNativeContext( |
| 639 | env, binderObj)->getBinder(); |
| 640 | } else { |
| 641 | signalExceptionForError(env, INVALID_OPERATION); |
| 642 | return; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | hardware::Parcel *parcel = |
| 647 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 648 | |
| 649 | status_t err = parcel->writeStrongBinder(binder); |
| 650 | signalExceptionForError(env, err); |
| 651 | } |
| 652 | |
| 653 | static jstring MakeStringObjFromHidlString(JNIEnv *env, const hidl_string &s) { |
| 654 | String16 utf16String(s.c_str(), s.size()); |
| 655 | |
| 656 | return env->NewString( |
| 657 | reinterpret_cast<const jchar *>(utf16String.string()), |
| 658 | utf16String.size()); |
| 659 | } |
| 660 | |
| 661 | static jstring JHwParcel_native_readString(JNIEnv *env, jobject thiz) { |
| 662 | hardware::Parcel *parcel = |
| 663 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 664 | |
| 665 | size_t parentHandle; |
| 666 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 667 | const hidl_string *s; |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 668 | status_t err = parcel->readBuffer(sizeof(*s), &parentHandle, |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 669 | reinterpret_cast<const void**>(&s)); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 670 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 671 | if (err != OK) { |
| 672 | signalExceptionForError(env, err); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 673 | return NULL; |
| 674 | } |
| 675 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 676 | err = ::android::hardware::readEmbeddedFromParcel( |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 677 | const_cast<hidl_string &>(*s), |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 678 | *parcel, parentHandle, 0 /* parentOffset */); |
| 679 | |
| 680 | if (err != OK) { |
| 681 | signalExceptionForError(env, err); |
| 682 | return NULL; |
| 683 | } |
| 684 | |
| 685 | return MakeStringObjFromHidlString(env, *s); |
| 686 | } |
| 687 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 688 | static jobject ReadNativeHandle(JNIEnv *env, jobject thiz, jboolean embedded, |
| 689 | jlong parentHandle, jlong offset) { |
| 690 | hardware::Parcel *parcel = |
| 691 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 692 | |
| 693 | const native_handle_t *handle = nullptr; |
| 694 | status_t err = OK; |
| 695 | |
| 696 | if (embedded) { |
| 697 | err = parcel->readNullableEmbeddedNativeHandle(parentHandle, offset, &handle); |
| 698 | } else { |
| 699 | err = parcel->readNullableNativeHandleNoDup(&handle); |
| 700 | } |
| 701 | |
| 702 | if (err != OK) { |
| 703 | signalExceptionForError(env, err); |
| 704 | return nullptr; |
| 705 | } |
| 706 | |
| 707 | return JNativeHandle::MakeJavaNativeHandleObj(env, handle); |
| 708 | } |
| 709 | |
| 710 | static jobject JHwParcel_native_readNativeHandle(JNIEnv *env, jobject thiz) { |
| 711 | return ReadNativeHandle(env, thiz, false /*embedded*/, 0L /*parentHandle*/, 0L /*offset*/); |
| 712 | } |
| 713 | |
| 714 | static jobject JHwParcel_native_readEmbeddedNativeHandle( |
| 715 | JNIEnv *env, jobject thiz, jlong parentHandle, jlong offset) { |
| 716 | return ReadNativeHandle(env, thiz, true /*embedded*/, parentHandle, offset); |
| 717 | } |
| 718 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 719 | #define DEFINE_PARCEL_VECTOR_READER(Suffix,Type,NewType) \ |
| 720 | static Type ## Array JHwParcel_native_read ## Suffix ## Vector( \ |
| 721 | JNIEnv *env, jobject thiz) { \ |
| 722 | hardware::Parcel *parcel = \ |
| 723 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); \ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 724 | size_t parentHandle; \ |
| 725 | \ |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 726 | const hidl_vec<Type> *vec; \ |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 727 | status_t err = parcel->readBuffer(sizeof(*vec), &parentHandle, \ |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 728 | reinterpret_cast<const void**>(&vec)); \ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 729 | \ |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 730 | if (err != OK) { \ |
| 731 | signalExceptionForError(env, err); \ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 732 | return NULL; \ |
| 733 | } \ |
| 734 | \ |
| 735 | size_t childHandle; \ |
| 736 | \ |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 737 | err = ::android::hardware::readEmbeddedFromParcel( \ |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 738 | const_cast<hidl_vec<Type> &>(*vec), \ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 739 | *parcel, \ |
| 740 | parentHandle, \ |
| 741 | 0 /* parentOffset */, \ |
| 742 | &childHandle); \ |
| 743 | \ |
| 744 | if (err != OK) { \ |
| 745 | signalExceptionForError(env, err); \ |
| 746 | return NULL; \ |
| 747 | } \ |
| 748 | \ |
| 749 | Type ## Array valObj = env->New ## NewType ## Array(vec->size()); \ |
| 750 | env->Set ## NewType ## ArrayRegion(valObj, 0, vec->size(), &(*vec)[0]); \ |
| 751 | \ |
| 752 | return valObj; \ |
| 753 | } |
| 754 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 755 | DEFINE_PARCEL_VECTOR_READER(Int8,jbyte,Byte) |
| 756 | DEFINE_PARCEL_VECTOR_READER(Int16,jshort,Short) |
| 757 | DEFINE_PARCEL_VECTOR_READER(Int32,jint,Int) |
| 758 | DEFINE_PARCEL_VECTOR_READER(Int64,jlong,Long) |
| 759 | DEFINE_PARCEL_VECTOR_READER(Float,jfloat,Float) |
| 760 | DEFINE_PARCEL_VECTOR_READER(Double,jdouble,Double) |
| 761 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 762 | static jbooleanArray JHwParcel_native_readBoolVector(JNIEnv *env, jobject thiz) { |
| 763 | hardware::Parcel *parcel = JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 764 | |
| 765 | size_t parentHandle; |
| 766 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 767 | const hidl_vec<bool> *vec; |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 768 | status_t err = parcel->readBuffer(sizeof(*vec), &parentHandle, |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 769 | reinterpret_cast<const void**>(&vec)); |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 770 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 771 | if (err != OK) { |
| 772 | signalExceptionForError(env, err); |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | size_t childHandle; |
| 777 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 778 | err = ::android::hardware::readEmbeddedFromParcel( |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 779 | const_cast<hidl_vec<bool> &>(*vec), |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 780 | *parcel, |
| 781 | parentHandle, |
| 782 | 0 /* parentOffset */, |
| 783 | &childHandle); |
| 784 | |
| 785 | if (err != OK) { |
| 786 | signalExceptionForError(env, err); |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | jbooleanArray valObj = env->NewBooleanArray(vec->size()); |
| 791 | |
| 792 | for (size_t i = 0; i < vec->size(); ++i) { |
| 793 | jboolean x = (*vec)[i]; |
| 794 | env->SetBooleanArrayRegion(valObj, i, 1, &x); |
| 795 | } |
| 796 | |
| 797 | return valObj; |
| 798 | } |
| 799 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 800 | static jobjectArray MakeStringArray( |
| 801 | JNIEnv *env, const hidl_string *array, size_t size) { |
| 802 | ScopedLocalRef<jclass> stringKlass( |
| 803 | env, |
| 804 | env->FindClass("java/lang/String")); |
| 805 | |
| 806 | // XXX Why can't I use ScopedLocalRef<> for the arrayObj and the stringObjs? |
| 807 | |
| 808 | jobjectArray arrayObj = env->NewObjectArray(size, stringKlass.get(), NULL); |
| 809 | |
| 810 | for (size_t i = 0; i < size; ++i) { |
| 811 | jstring stringObj = MakeStringObjFromHidlString(env, array[i]); |
| 812 | |
| 813 | env->SetObjectArrayElement( |
| 814 | arrayObj, |
| 815 | i, |
| 816 | stringObj); |
| 817 | } |
| 818 | |
| 819 | return arrayObj; |
| 820 | } |
| 821 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 822 | template<typename T> |
| 823 | static const hidl_vec<T> *ReadHidlVector(JNIEnv *env, jobject thiz) { |
| 824 | const hidl_vec<T> *vec; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 825 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 826 | hardware::Parcel *parcel = JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 827 | |
| 828 | size_t parentHandle; |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 829 | status_t err = parcel->readBuffer(sizeof(hidl_vec<T>), |
| 830 | &parentHandle, reinterpret_cast<const void **>(&vec)); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 831 | |
| 832 | if (err == OK) { |
| 833 | size_t childHandle; |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 834 | err = ::android::hardware::readEmbeddedFromParcel( |
| 835 | const_cast<hidl_vec<T> &>(*vec), |
| 836 | *parcel, parentHandle, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 837 | 0 /* parentOffset */, |
| 838 | &childHandle); |
| 839 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 840 | for (size_t i = 0; (err == OK) && (i < vec->size()); i++) { |
| 841 | err = android::hardware::readEmbeddedFromParcel( |
| 842 | const_cast<T &>((*vec)[i]), |
| 843 | *parcel, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 844 | childHandle, |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 845 | i * sizeof(T) /* parentOffset */); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 849 | if (err != OK) { |
| 850 | signalExceptionForError(env, err); |
| 851 | return nullptr; |
| 852 | } |
| 853 | |
| 854 | return vec; |
| 855 | } |
| 856 | |
| 857 | static jobjectArray JHwParcel_native_readStringVector( |
| 858 | JNIEnv *env, jobject thiz) { |
| 859 | const hidl_vec<hidl_string> *vec = ReadHidlVector<hidl_string>(env, thiz); |
| 860 | return MakeStringArray(env, &(*vec)[0], vec->size()); |
| 861 | } |
| 862 | |
| 863 | static jobjectArray JHwParcel_native_readNativeHandleVector( |
| 864 | JNIEnv *env, jobject thiz) { |
| 865 | const hidl_vec<hidl_handle> *vec = ReadHidlVector<hidl_handle>(env, thiz); |
| 866 | |
| 867 | jsize length = vec->size(); |
| 868 | jobjectArray objArray = JNativeHandle::AllocJavaNativeHandleObjArray( |
| 869 | env, length); |
| 870 | |
| 871 | for (jsize i = 0; i < length; i++) { |
| 872 | jobject jHandle = JNativeHandle::MakeJavaNativeHandleObj(env, (*vec)[i].getNativeHandle()); |
| 873 | |
| 874 | env->SetObjectArrayElement(objArray, i, jHandle); |
| 875 | } |
| 876 | |
| 877 | return objArray; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static jobject JHwParcel_native_readStrongBinder(JNIEnv *env, jobject thiz) { |
| 881 | hardware::Parcel *parcel = |
| 882 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 883 | |
| 884 | sp<hardware::IBinder> binder = parcel->readStrongBinder(); |
| 885 | |
Steven Moreland | 47942d9 | 2019-04-08 19:22:11 -0700 | [diff] [blame^] | 886 | if (binder == nullptr) { |
| 887 | return nullptr; |
| 888 | } |
| 889 | |
| 890 | if (!validateCanUseHwBinder(binder)) { |
| 891 | return nullptr; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return JHwRemoteBinder::NewObject(env, binder); |
| 895 | } |
| 896 | |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 897 | static jobject JHwParcel_native_readBuffer(JNIEnv *env, jobject thiz, |
| 898 | jlong expectedSize) { |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 899 | hardware::Parcel *parcel = |
| 900 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 901 | |
| 902 | size_t handle; |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 903 | const void *ptr; |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 904 | |
| 905 | if (expectedSize < 0) { |
| 906 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 907 | return nullptr; |
| 908 | } |
| 909 | |
| 910 | status_t status = parcel->readBuffer(expectedSize, &handle, &ptr); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 911 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 912 | if (status != OK) { |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 913 | jniThrowException(env, "java/util/NoSuchElementException", NULL); |
| 914 | return nullptr; |
| 915 | } |
| 916 | |
| 917 | return JHwBlob::NewObject(env, ptr, handle); |
| 918 | } |
| 919 | |
| 920 | static jobject JHwParcel_native_readEmbeddedBuffer( |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 921 | JNIEnv *env, jobject thiz, jlong expectedSize, |
| 922 | jlong parentHandle, jlong offset, jboolean nullable) { |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 923 | hardware::Parcel *parcel = |
| 924 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 925 | |
| 926 | size_t childHandle; |
| 927 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 928 | const void *ptr; |
| 929 | status_t status = |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 930 | parcel->readNullableEmbeddedBuffer(expectedSize, |
| 931 | &childHandle, parentHandle, offset, &ptr); |
| 932 | |
| 933 | if (expectedSize < 0) { |
| 934 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 935 | return nullptr; |
| 936 | } |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 937 | |
Martijn Coenen | 24c8227 | 2017-01-11 12:35:56 +0100 | [diff] [blame] | 938 | if (status != OK) { |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 939 | jniThrowException(env, "java/util/NoSuchElementException", NULL); |
| 940 | return 0; |
Martijn Coenen | 14fae06 | 2017-01-13 11:08:51 +0100 | [diff] [blame] | 941 | } else if (status == OK && !nullable && ptr == nullptr) { |
| 942 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 943 | return 0; |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | return JHwBlob::NewObject(env, ptr, childHandle); |
| 947 | } |
| 948 | |
| 949 | static void JHwParcel_native_writeBuffer( |
| 950 | JNIEnv *env, jobject thiz, jobject blobObj) { |
| 951 | if (blobObj == nullptr) { |
| 952 | jniThrowException(env, "java/lang/NullPointerException", NULL); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | hardware::Parcel *parcel = |
| 957 | JHwParcel::GetNativeContext(env, thiz)->getParcel(); |
| 958 | |
| 959 | sp<JHwBlob> blob = JHwBlob::GetNativeContext(env, blobObj); |
| 960 | status_t err = blob->writeToParcel(parcel); |
| 961 | |
| 962 | if (err != OK) { |
| 963 | signalExceptionForError(env, err); |
| 964 | } |
| 965 | } |
| 966 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 967 | static JNINativeMethod gMethods[] = { |
| 968 | { "native_init", "()J", (void *)JHwParcel_native_init }, |
| 969 | { "native_setup", "(Z)V", (void *)JHwParcel_native_setup }, |
| 970 | |
| 971 | { "writeInterfaceToken", "(Ljava/lang/String;)V", |
| 972 | (void *)JHwParcel_native_writeInterfaceToken }, |
| 973 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 974 | { "writeBool", "(Z)V", (void *)JHwParcel_native_writeBool }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 975 | { "writeInt8", "(B)V", (void *)JHwParcel_native_writeInt8 }, |
| 976 | { "writeInt16", "(S)V", (void *)JHwParcel_native_writeInt16 }, |
| 977 | { "writeInt32", "(I)V", (void *)JHwParcel_native_writeInt32 }, |
| 978 | { "writeInt64", "(J)V", (void *)JHwParcel_native_writeInt64 }, |
| 979 | { "writeFloat", "(F)V", (void *)JHwParcel_native_writeFloat }, |
| 980 | { "writeDouble", "(D)V", (void *)JHwParcel_native_writeDouble }, |
| 981 | |
| 982 | { "writeString", "(Ljava/lang/String;)V", |
| 983 | (void *)JHwParcel_native_writeString }, |
| 984 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 985 | { "writeNativeHandle", "(L" PACKAGE_PATH "/NativeHandle;)V", |
| 986 | (void *)JHwParcel_native_writeNativeHandle }, |
| 987 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 988 | { "writeBoolVector", "([Z)V", (void *)JHwParcel_native_writeBoolVector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 989 | { "writeInt8Vector", "([B)V", (void *)JHwParcel_native_writeInt8Vector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 990 | { "writeInt16Vector", "([S)V", (void *)JHwParcel_native_writeInt16Vector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 991 | { "writeInt32Vector", "([I)V", (void *)JHwParcel_native_writeInt32Vector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 992 | { "writeInt64Vector", "([J)V", (void *)JHwParcel_native_writeInt64Vector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 993 | { "writeFloatVector", "([F)V", (void *)JHwParcel_native_writeFloatVector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 994 | |
| 995 | { "writeDoubleVector", "([D)V", |
| 996 | (void *)JHwParcel_native_writeDoubleVector }, |
| 997 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 998 | { "writeStringVector", "([Ljava/lang/String;)V", |
| 999 | (void *)JHwParcel_native_writeStringVector }, |
| 1000 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 1001 | { "writeNativeHandleVector", "([L" PACKAGE_PATH "/NativeHandle;)V", |
| 1002 | (void *)JHwParcel_native_writeNativeHandleVector }, |
| 1003 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1004 | { "writeStrongBinder", "(L" PACKAGE_PATH "/IHwBinder;)V", |
| 1005 | (void *)JHwParcel_native_writeStrongBinder }, |
| 1006 | |
| 1007 | { "enforceInterface", "(Ljava/lang/String;)V", |
| 1008 | (void *)JHwParcel_native_enforceInterface }, |
| 1009 | |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 1010 | { "readBool", "()Z", (void *)JHwParcel_native_readBool }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1011 | { "readInt8", "()B", (void *)JHwParcel_native_readInt8 }, |
| 1012 | { "readInt16", "()S", (void *)JHwParcel_native_readInt16 }, |
| 1013 | { "readInt32", "()I", (void *)JHwParcel_native_readInt32 }, |
| 1014 | { "readInt64", "()J", (void *)JHwParcel_native_readInt64 }, |
| 1015 | { "readFloat", "()F", (void *)JHwParcel_native_readFloat }, |
| 1016 | { "readDouble", "()D", (void *)JHwParcel_native_readDouble }, |
| 1017 | |
| 1018 | { "readString", "()Ljava/lang/String;", |
| 1019 | (void *)JHwParcel_native_readString }, |
| 1020 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 1021 | { "readNativeHandle", "()L" PACKAGE_PATH "/NativeHandle;", |
| 1022 | (void *)JHwParcel_native_readNativeHandle }, |
| 1023 | |
| 1024 | { "readEmbeddedNativeHandle", "(JJ)L" PACKAGE_PATH "/NativeHandle;", |
| 1025 | (void *)JHwParcel_native_readEmbeddedNativeHandle }, |
| 1026 | |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 1027 | { "readBoolVectorAsArray", "()[Z", |
| 1028 | (void *)JHwParcel_native_readBoolVector }, |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1029 | |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 1030 | { "readInt8VectorAsArray", "()[B", |
| 1031 | (void *)JHwParcel_native_readInt8Vector }, |
| 1032 | |
| 1033 | { "readInt16VectorAsArray", "()[S", |
| 1034 | (void *)JHwParcel_native_readInt16Vector }, |
| 1035 | |
| 1036 | { "readInt32VectorAsArray", "()[I", |
| 1037 | (void *)JHwParcel_native_readInt32Vector }, |
| 1038 | |
| 1039 | { "readInt64VectorAsArray", "()[J", |
| 1040 | (void *)JHwParcel_native_readInt64Vector }, |
| 1041 | |
| 1042 | { "readFloatVectorAsArray", "()[F", |
| 1043 | (void *)JHwParcel_native_readFloatVector }, |
| 1044 | |
| 1045 | { "readDoubleVectorAsArray", "()[D", |
| 1046 | (void *)JHwParcel_native_readDoubleVector }, |
| 1047 | |
| 1048 | { "readStringVectorAsArray", "()[Ljava/lang/String;", |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1049 | (void *)JHwParcel_native_readStringVector }, |
| 1050 | |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 1051 | { "readNativeHandleAsArray", "()[L" PACKAGE_PATH "/NativeHandle;", |
| 1052 | (void *)JHwParcel_native_readNativeHandleVector }, |
| 1053 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1054 | { "readStrongBinder", "()L" PACKAGE_PATH "/IHwBinder;", |
| 1055 | (void *)JHwParcel_native_readStrongBinder }, |
| 1056 | |
| 1057 | { "writeStatus", "(I)V", (void *)JHwParcel_native_writeStatus }, |
| 1058 | |
| 1059 | { "verifySuccess", "()V", (void *)JHwParcel_native_verifySuccess }, |
| 1060 | |
| 1061 | { "releaseTemporaryStorage", "()V", |
| 1062 | (void *)JHwParcel_native_releaseTemporaryStorage }, |
| 1063 | |
| 1064 | { "send", "()V", (void *)JHwParcel_native_send }, |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 1065 | |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 1066 | { "readBuffer", "(J)L" PACKAGE_PATH "/HwBlob;", |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 1067 | (void *)JHwParcel_native_readBuffer }, |
| 1068 | |
Martijn Coenen | 7c00639 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 1069 | { "readEmbeddedBuffer", "(JJJZ)L" PACKAGE_PATH "/HwBlob;", |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 1070 | (void *)JHwParcel_native_readEmbeddedBuffer }, |
| 1071 | |
| 1072 | { "writeBuffer", "(L" PACKAGE_PATH "/HwBlob;)V", |
| 1073 | (void *)JHwParcel_native_writeBuffer }, |
Martijn Coenen | 3d726d1 | 2017-03-16 18:46:42 +0100 | [diff] [blame] | 1074 | |
| 1075 | { "release", "()V", |
| 1076 | (void *)JHwParcel_native_release }, |
| 1077 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1078 | }; |
| 1079 | |
| 1080 | namespace android { |
| 1081 | |
| 1082 | int register_android_os_HwParcel(JNIEnv *env) { |
| 1083 | return RegisterMethodsOrDie(env, CLASS_PATH, gMethods, NELEM(gMethods)); |
| 1084 | } |
| 1085 | |
| 1086 | } // namespace android |