SharedMemoryParcelable.cpp revision 71f35bb687476694882a617ba4a810a0bb56fe23
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#define LOG_TAG "AAudio" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include <stdint.h> 22#include <stdio.h> 23 24#include <sys/mman.h> 25#include <aaudio/AAudio.h> 26 27#include <binder/Parcelable.h> 28#include <utility/AAudioUtilities.h> 29 30#include "binding/SharedMemoryParcelable.h" 31 32using android::NO_ERROR; 33using android::status_t; 34using android::Parcel; 35using android::Parcelable; 36 37using namespace aaudio; 38 39SharedMemoryParcelable::SharedMemoryParcelable() {} 40SharedMemoryParcelable::~SharedMemoryParcelable() {}; 41 42void SharedMemoryParcelable::setup(int fd, int32_t sizeInBytes) { 43 mFd = fd; 44 mSizeInBytes = sizeInBytes; 45 46} 47 48status_t SharedMemoryParcelable::writeToParcel(Parcel* parcel) const { 49 status_t status = parcel->writeInt32(mSizeInBytes); 50 if (status != NO_ERROR) return status; 51 if (mSizeInBytes > 0) { 52 status = parcel->writeDupFileDescriptor(mFd); 53 ALOGE_IF(status != NO_ERROR, "SharedMemoryParcelable writeDupFileDescriptor failed : %d", status); 54 } 55 return status; 56} 57 58status_t SharedMemoryParcelable::readFromParcel(const Parcel* parcel) { 59 status_t status = parcel->readInt32(&mSizeInBytes); 60 if (status != NO_ERROR) { 61 return status; 62 } 63 if (mSizeInBytes > 0) { 64 int originalFD = parcel->readFileDescriptor(); 65 mFd = fcntl(originalFD, F_DUPFD_CLOEXEC, 0); 66 if (mFd == -1) { 67 status = -errno; 68 ALOGE("SharedMemoryParcelable readFileDescriptor fcntl() failed : %d", status); 69 } 70 } 71 return status; 72} 73 74aaudio_result_t SharedMemoryParcelable::close() { 75 if (mResolvedAddress != nullptr) { 76 int err = munmap(mResolvedAddress, mSizeInBytes); 77 if (err < 0) { 78 ALOGE("SharedMemoryParcelable::close() munmap() failed %d", err); 79 return AAudioConvert_androidToAAudioResult(err); 80 } 81 mResolvedAddress = nullptr; 82 } 83 if (mFd != -1) { 84 ::close(mFd); 85 mFd = -1; 86 } 87 return AAUDIO_OK; 88} 89 90aaudio_result_t SharedMemoryParcelable::resolve(int32_t offsetInBytes, int32_t sizeInBytes, 91 void **regionAddressPtr) { 92 93 if (offsetInBytes < 0) { 94 ALOGE("SharedMemoryParcelable illegal offsetInBytes = %d", offsetInBytes); 95 return AAUDIO_ERROR_OUT_OF_RANGE; 96 } else if ((offsetInBytes + sizeInBytes) > mSizeInBytes) { 97 ALOGE("SharedMemoryParcelable out of range, offsetInBytes = %d, " 98 "sizeInBytes = %d, mSizeInBytes = %d", 99 offsetInBytes, sizeInBytes, mSizeInBytes); 100 return AAUDIO_ERROR_OUT_OF_RANGE; 101 } 102 if (mResolvedAddress == nullptr) { 103 mResolvedAddress = (uint8_t *) mmap(0, mSizeInBytes, PROT_READ|PROT_WRITE, 104 MAP_SHARED, mFd, 0); 105 if (mResolvedAddress == nullptr) { 106 ALOGE("SharedMemoryParcelable mmap failed for fd = %d", mFd); 107 return AAUDIO_ERROR_INTERNAL; 108 } 109 } 110 *regionAddressPtr = mResolvedAddress + offsetInBytes; 111 ALOGV("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 112 ALOGV("SharedMemoryParcelable offset by %d, *regionAddressPtr = %p", 113 offsetInBytes, *regionAddressPtr); 114 return AAUDIO_OK; 115} 116 117int32_t SharedMemoryParcelable::getSizeInBytes() { 118 return mSizeInBytes; 119} 120 121aaudio_result_t SharedMemoryParcelable::validate() { 122 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { 123 ALOGE("SharedMemoryParcelable invalid mSizeInBytes = %d", mSizeInBytes); 124 return AAUDIO_ERROR_OUT_OF_RANGE; 125 } 126 if (mSizeInBytes > 0) { 127 if (mFd == -1) { 128 ALOGE("SharedMemoryParcelable uninitialized mFd = %d", mFd); 129 return AAUDIO_ERROR_INTERNAL; 130 } 131 } 132 return AAUDIO_OK; 133} 134 135void SharedMemoryParcelable::dump() { 136 ALOGD("SharedMemoryParcelable mFd = %d", mFd); 137 ALOGD("SharedMemoryParcelable mSizeInBytes = %d", mSizeInBytes); 138 ALOGD("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 139} 140