SharedRegionParcelable.cpp revision f53e613b3dedab3ecada2c93d8846233c442d129
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 22#include <aaudio/AAudioDefinitions.h> 23 24#include "binding/SharedMemoryParcelable.h" 25#include "binding/SharedRegionParcelable.h" 26 27using android::NO_ERROR; 28using android::status_t; 29using android::Parcel; 30using android::Parcelable; 31 32using namespace aaudio; 33 34SharedRegionParcelable::SharedRegionParcelable() {} 35SharedRegionParcelable::~SharedRegionParcelable() {} 36 37void SharedRegionParcelable::setup(int32_t sharedMemoryIndex, 38 int32_t offsetInBytes, 39 int32_t sizeInBytes) { 40 mSharedMemoryIndex = sharedMemoryIndex; 41 mOffsetInBytes = offsetInBytes; 42 mSizeInBytes = sizeInBytes; 43} 44 45status_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 54status_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 63aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels, 64 void **regionAddressPtr) { 65 if (mSizeInBytes == 0) { 66 *regionAddressPtr = nullptr; 67 return AAUDIO_OK; 68 } 69 if (mSharedMemoryIndex < 0) { 70 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); 71 return AAUDIO_ERROR_INTERNAL; 72 } 73 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex]; 74 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr); 75} 76 77aaudio_result_t SharedRegionParcelable::validate() { 78 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { 79 ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes); 80 return AAUDIO_ERROR_OUT_OF_RANGE; 81 } 82 if (mSizeInBytes > 0) { 83 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) { 84 ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes); 85 return AAUDIO_ERROR_OUT_OF_RANGE; 86 } 87 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) { 88 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex); 89 return AAUDIO_ERROR_INTERNAL; 90 } 91 } 92 return AAUDIO_OK; 93} 94 95void 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} 102