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 | |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <sys/mman.h> |
| 20 | #include <binder/Parcelable.h> |
| 21 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <aaudio/AAudioDefinitions.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
| 24 | #include "binding/SharedMemoryParcelable.h" |
| 25 | #include "binding/SharedRegionParcelable.h" |
| 26 | |
| 27 | using android::NO_ERROR; |
| 28 | using android::status_t; |
| 29 | using android::Parcel; |
| 30 | using android::Parcelable; |
| 31 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
| 34 | SharedRegionParcelable::SharedRegionParcelable() {} |
| 35 | SharedRegionParcelable::~SharedRegionParcelable() {} |
| 36 | |
| 37 | void SharedRegionParcelable::setup(int32_t sharedMemoryIndex, |
| 38 | int32_t offsetInBytes, |
| 39 | int32_t sizeInBytes) { |
| 40 | mSharedMemoryIndex = sharedMemoryIndex; |
| 41 | mOffsetInBytes = offsetInBytes; |
| 42 | mSizeInBytes = sizeInBytes; |
| 43 | } |
| 44 | |
| 45 | status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const { |
| 46 | parcel->writeInt32(mSizeInBytes); |
| 47 | if (mSizeInBytes > 0) { |
| 48 | parcel->writeInt32(mSharedMemoryIndex); |
| 49 | parcel->writeInt32(mOffsetInBytes); |
| 50 | } |
| 51 | return NO_ERROR; // TODO check for errors above |
| 52 | } |
| 53 | |
| 54 | status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) { |
| 55 | parcel->readInt32(&mSizeInBytes); |
| 56 | if (mSizeInBytes > 0) { |
| 57 | parcel->readInt32(&mSharedMemoryIndex); |
| 58 | parcel->readInt32(&mOffsetInBytes); |
| 59 | } |
| 60 | return NO_ERROR; // TODO check for errors above |
| 61 | } |
| 62 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 | aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 64 | void **regionAddressPtr) { |
| 65 | if (mSizeInBytes == 0) { |
| 66 | *regionAddressPtr = nullptr; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 68 | } |
| 69 | if (mSharedMemoryIndex < 0) { |
| 70 | ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 71 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | } |
| 73 | SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex]; |
| 74 | return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr); |
| 75 | } |
| 76 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 77 | aaudio_result_t SharedRegionParcelable::validate() { |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 78 | if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 79 | ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes); |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 80 | return AAUDIO_ERROR_OUT_OF_RANGE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 81 | } |
| 82 | if (mSizeInBytes > 0) { |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 83 | if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes); |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 85 | return AAUDIO_ERROR_OUT_OF_RANGE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 86 | } |
| 87 | if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) { |
| 88 | ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 89 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 92 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void SharedRegionParcelable::dump() { |
| 96 | ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes); |
| 97 | if (mSizeInBytes > 0) { |
| 98 | ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex); |
| 99 | ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes); |
| 100 | } |
| 101 | } |