| 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 | |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 32 | using android::status_t; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | using namespace aaudio; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 35 | |
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 36 | SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable) |
| 37 | : mSharedMemoryIndex(parcelable.sharedMemoryIndex), |
| 38 | mOffsetInBytes(parcelable.offsetInBytes), |
| 39 | mSizeInBytes(parcelable.sizeInBytes) {} |
| 40 | |
| 41 | SharedRegion SharedRegionParcelable::parcelable() const { |
| 42 | SharedRegion result; |
| 43 | result.sharedMemoryIndex = mSharedMemoryIndex; |
| 44 | result.offsetInBytes = mOffsetInBytes; |
| 45 | result.sizeInBytes = mSizeInBytes; |
| 46 | return result; |
| 47 | } |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 48 | |
| jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame^] | 49 | void SharedRegionParcelable::setup(MemoryInfoTuple memoryInfoTuple) { |
| 50 | mSharedMemoryIndex = std::get<MEMORY_INDEX>(memoryInfoTuple); |
| 51 | mOffsetInBytes = std::get<OFFSET>(memoryInfoTuple); |
| 52 | mSizeInBytes = std::get<SIZE>(memoryInfoTuple); |
| 53 | } |
| 54 | |
| 55 | SharedRegionParcelable::MemoryInfoTuple SharedRegionParcelable::getMemoryInfo( |
| 56 | const std::map<int32_t, int32_t>* memoryIndexMap) const { |
| 57 | return {memoryIndexMap == nullptr ? mSharedMemoryIndex : memoryIndexMap->at(mSharedMemoryIndex), |
| 58 | mOffsetInBytes, |
| 59 | mSizeInBytes}; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 62 | aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels, |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 63 | void **regionAddressPtr) { |
| 64 | if (mSizeInBytes == 0) { |
| 65 | *regionAddressPtr = nullptr; |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 66 | return AAUDIO_OK; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 67 | } |
| 68 | if (mSharedMemoryIndex < 0) { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 69 | ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 70 | return AAUDIO_ERROR_INTERNAL; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 71 | } |
| 72 | SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex]; |
| 73 | return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr); |
| 74 | } |
| 75 | |
| Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 76 | aaudio_result_t SharedRegionParcelable::validate() const { |
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 77 | if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 78 | ALOGE("invalid mSizeInBytes = %d", mSizeInBytes); |
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 79 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 80 | } |
| 81 | if (mSizeInBytes > 0) { |
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 82 | if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 83 | ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes); |
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 84 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 85 | } |
| 86 | if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 87 | ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 88 | return AAUDIO_ERROR_INTERNAL; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 91 | return AAUDIO_OK; |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void SharedRegionParcelable::dump() { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 95 | ALOGD("mSizeInBytes = %d -----", mSizeInBytes); |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 96 | if (mSizeInBytes > 0) { |
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 97 | ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex); |
| 98 | ALOGD("mOffsetInBytes = %d", mOffsetInBytes); |
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 99 | } |
| 100 | } |