SharedMemoryParcelable.cpp revision 478d5df3a0e1693669f5809176d5c7f6e8c9aa88
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 != MMAP_UNRESOLVED_ADDRESS) { 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 = MMAP_UNRESOLVED_ADDRESS; 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 == MMAP_UNRESOLVED_ADDRESS) { 103 mResolvedAddress = (uint8_t *) mmap(0, mSizeInBytes, PROT_READ|PROT_WRITE, 104 MAP_SHARED, mFd, 0); 105 if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) { 106 ALOGE("SharedMemoryParcelable mmap failed for fd = %d, errno = %s", 107 mFd, strerror(errno)); 108 return AAUDIO_ERROR_INTERNAL; 109 } 110 } 111 *regionAddressPtr = mResolvedAddress + offsetInBytes; 112 ALOGV("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 113 ALOGV("SharedMemoryParcelable offset by %d, *regionAddressPtr = %p", 114 offsetInBytes, *regionAddressPtr); 115 return AAUDIO_OK; 116} 117 118int32_t SharedMemoryParcelable::getSizeInBytes() { 119 return mSizeInBytes; 120} 121 122aaudio_result_t SharedMemoryParcelable::validate() { 123 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { 124 ALOGE("SharedMemoryParcelable invalid mSizeInBytes = %d", mSizeInBytes); 125 return AAUDIO_ERROR_OUT_OF_RANGE; 126 } 127 if (mSizeInBytes > 0) { 128 if (mFd == -1) { 129 ALOGE("SharedMemoryParcelable uninitialized mFd = %d", mFd); 130 return AAUDIO_ERROR_INTERNAL; 131 } 132 } 133 return AAUDIO_OK; 134} 135 136void SharedMemoryParcelable::dump() { 137 ALOGD("SharedMemoryParcelable mFd = %d", mFd); 138 ALOGD("SharedMemoryParcelable mSizeInBytes = %d", mSizeInBytes); 139 ALOGD("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 140} 141