blob: c776116dbc16516b980c3fbcecdf3eb0ab8933e1 [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 Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "SharedRegionParcelable"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#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 Burka5891f42018-03-12 17:21:11 -070027#include <utility/AAudioUtilities.h>
Phil Burk204a1632017-01-03 17:23:43 -080028
29#include "binding/SharedMemoryParcelable.h"
30#include "binding/SharedRegionParcelable.h"
31
32using android::NO_ERROR;
33using android::status_t;
34using android::Parcel;
35using android::Parcelable;
36
Phil Burk5ed503c2017-02-01 09:38:15 -080037using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080038
39SharedRegionParcelable::SharedRegionParcelable() {}
40SharedRegionParcelable::~SharedRegionParcelable() {}
41
42void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
43 int32_t offsetInBytes,
44 int32_t sizeInBytes) {
45 mSharedMemoryIndex = sharedMemoryIndex;
46 mOffsetInBytes = offsetInBytes;
47 mSizeInBytes = sizeInBytes;
48}
49
50status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
Phil Burka5891f42018-03-12 17:21:11 -070051 status_t status = AAudioConvert_aaudioToAndroidStatus(validate());
52 if (status != NO_ERROR) goto error;
53
54 status = parcel->writeInt32(mSizeInBytes);
55 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080056 if (mSizeInBytes > 0) {
Phil Burka5891f42018-03-12 17:21:11 -070057 status = parcel->writeInt32(mSharedMemoryIndex);
58 if (status != NO_ERROR) goto error;
59 status = parcel->writeInt32(mOffsetInBytes);
60 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080061 }
Phil Burka5891f42018-03-12 17:21:11 -070062 return NO_ERROR;
63
64error:
65 ALOGE("%s returning %d", __func__, status);
66 return status;
Phil Burk204a1632017-01-03 17:23:43 -080067}
68
69status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
Phil Burka5891f42018-03-12 17:21:11 -070070 status_t status = parcel->readInt32(&mSizeInBytes);
71 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080072 if (mSizeInBytes > 0) {
Phil Burka5891f42018-03-12 17:21:11 -070073 status = parcel->readInt32(&mSharedMemoryIndex);
74 if (status != NO_ERROR) goto error;
75 status = parcel->readInt32(&mOffsetInBytes);
76 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080077 }
Phil Burka5891f42018-03-12 17:21:11 -070078 return AAudioConvert_aaudioToAndroidStatus(validate());
79
80error:
81 ALOGE("%s returning %d", __func__, status);
82 return status;
Phil Burk204a1632017-01-03 17:23:43 -080083}
84
Phil Burk5ed503c2017-02-01 09:38:15 -080085aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080086 void **regionAddressPtr) {
87 if (mSizeInBytes == 0) {
88 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080089 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080090 }
91 if (mSharedMemoryIndex < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070092 ALOGE("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 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
96 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
97}
98
Phil Burka5891f42018-03-12 17:21:11 -070099aaudio_result_t SharedRegionParcelable::validate() const {
Phil Burk3df348f2017-02-08 11:41:55 -0800100 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700101 ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -0800102 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -0800103 }
104 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -0800105 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700106 ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -0800107 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -0800108 }
109 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700110 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -0800111 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800112 }
113 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800114 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800115}
116
117void SharedRegionParcelable::dump() {
Phil Burkfbf031e2017-10-12 15:58:31 -0700118 ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
Phil Burk204a1632017-01-03 17:23:43 -0800119 if (mSizeInBytes > 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700120 ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
121 ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk204a1632017-01-03 17:23:43 -0800122 }
123}