blob: 7381dcb961ba6a2e8b9730340c09d6dd25144157 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
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 Burkc0c70e32017-02-09 13:18:38 -080017#define LOG_TAG "AAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk204a1632017-01-03 17:23:43 -080021#include <stdint.h>
22
23#include <sys/mman.h>
24#include <binder/Parcelable.h>
25
Phil Burka4eb0d82017-04-12 15:44:06 -070026#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080027
28#include "binding/SharedMemoryParcelable.h"
29#include "binding/SharedRegionParcelable.h"
30
31using android::NO_ERROR;
32using android::status_t;
33using android::Parcel;
34using android::Parcelable;
35
Phil Burk5ed503c2017-02-01 09:38:15 -080036using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080037
38SharedRegionParcelable::SharedRegionParcelable() {}
39SharedRegionParcelable::~SharedRegionParcelable() {}
40
41void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
42 int32_t offsetInBytes,
43 int32_t sizeInBytes) {
44 mSharedMemoryIndex = sharedMemoryIndex;
45 mOffsetInBytes = offsetInBytes;
46 mSizeInBytes = sizeInBytes;
47}
48
49status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
50 parcel->writeInt32(mSizeInBytes);
51 if (mSizeInBytes > 0) {
52 parcel->writeInt32(mSharedMemoryIndex);
53 parcel->writeInt32(mOffsetInBytes);
54 }
55 return NO_ERROR; // TODO check for errors above
56}
57
58status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
59 parcel->readInt32(&mSizeInBytes);
60 if (mSizeInBytes > 0) {
61 parcel->readInt32(&mSharedMemoryIndex);
62 parcel->readInt32(&mOffsetInBytes);
63 }
64 return NO_ERROR; // TODO check for errors above
65}
66
Phil Burk5ed503c2017-02-01 09:38:15 -080067aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080068 void **regionAddressPtr) {
69 if (mSizeInBytes == 0) {
70 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080071 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080072 }
73 if (mSharedMemoryIndex < 0) {
74 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080075 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080076 }
77 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
78 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
79}
80
Phil Burk5ed503c2017-02-01 09:38:15 -080081aaudio_result_t SharedRegionParcelable::validate() {
Phil Burk3df348f2017-02-08 11:41:55 -080082 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burk204a1632017-01-03 17:23:43 -080083 ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080084 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080085 }
86 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -080087 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burk204a1632017-01-03 17:23:43 -080088 ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080089 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080090 }
91 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
92 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080093 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080094 }
95 }
Phil Burk5ed503c2017-02-01 09:38:15 -080096 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080097}
98
99void SharedRegionParcelable::dump() {
100 ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes);
101 if (mSizeInBytes > 0) {
102 ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex);
103 ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes);
104 }
105}