Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "SharedRegionParcelable" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <sys/mman.h> |
| 24 | #include <binder/Parcelable.h> |
| 25 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 26 | #include <aaudio/AAudio.h> |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 27 | #include <utility/AAudioUtilities.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 | |
| 29 | #include "binding/SharedMemoryParcelable.h" |
| 30 | #include "binding/SharedRegionParcelable.h" |
| 31 | |
| 32 | using android::NO_ERROR; |
| 33 | using android::status_t; |
| 34 | using android::Parcel; |
| 35 | using android::Parcelable; |
| 36 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 37 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 38 | |
| 39 | SharedRegionParcelable::SharedRegionParcelable() {} |
| 40 | SharedRegionParcelable::~SharedRegionParcelable() {} |
| 41 | |
| 42 | void SharedRegionParcelable::setup(int32_t sharedMemoryIndex, |
| 43 | int32_t offsetInBytes, |
| 44 | int32_t sizeInBytes) { |
| 45 | mSharedMemoryIndex = sharedMemoryIndex; |
| 46 | mOffsetInBytes = offsetInBytes; |
| 47 | mSizeInBytes = sizeInBytes; |
| 48 | } |
| 49 | |
| 50 | status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 51 | status_t status = AAudioConvert_aaudioToAndroidStatus(validate()); |
| 52 | if (status != NO_ERROR) goto error; |
| 53 | |
| 54 | status = parcel->writeInt32(mSizeInBytes); |
| 55 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 56 | if (mSizeInBytes > 0) { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 57 | status = parcel->writeInt32(mSharedMemoryIndex); |
| 58 | if (status != NO_ERROR) goto error; |
| 59 | status = parcel->writeInt32(mOffsetInBytes); |
| 60 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 61 | } |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 62 | return NO_ERROR; |
| 63 | |
| 64 | error: |
| 65 | ALOGE("%s returning %d", __func__, status); |
| 66 | return status; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 70 | status_t status = parcel->readInt32(&mSizeInBytes); |
| 71 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | if (mSizeInBytes > 0) { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 73 | status = parcel->readInt32(&mSharedMemoryIndex); |
| 74 | if (status != NO_ERROR) goto error; |
| 75 | status = parcel->readInt32(&mOffsetInBytes); |
| 76 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 77 | } |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 78 | return AAudioConvert_aaudioToAndroidStatus(validate()); |
| 79 | |
| 80 | error: |
| 81 | ALOGE("%s returning %d", __func__, status); |
| 82 | return status; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 85 | aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 86 | void **regionAddressPtr) { |
| 87 | if (mSizeInBytes == 0) { |
| 88 | *regionAddressPtr = nullptr; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 89 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 90 | } |
| 91 | if (mSharedMemoryIndex < 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 92 | ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 93 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 94 | } |
| 95 | SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex]; |
| 96 | return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr); |
| 97 | } |
| 98 | |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 99 | aaudio_result_t SharedRegionParcelable::validate() const { |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 100 | if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 101 | ALOGE("invalid mSizeInBytes = %d", mSizeInBytes); |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 102 | return AAUDIO_ERROR_OUT_OF_RANGE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 103 | } |
| 104 | if (mSizeInBytes > 0) { |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 105 | if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 106 | ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes); |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 107 | return AAUDIO_ERROR_OUT_OF_RANGE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 108 | } |
| 109 | if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 110 | ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 111 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 114 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void SharedRegionParcelable::dump() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 118 | ALOGD("mSizeInBytes = %d -----", mSizeInBytes); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 119 | if (mSizeInBytes > 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 120 | ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex); |
| 121 | ALOGD("mOffsetInBytes = %d", mOffsetInBytes); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 | } |
| 123 | } |