blob: 56b99c07874247789084869c3c806f516b563c3a [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
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070039SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable)
40 : mSharedMemoryIndex(parcelable.sharedMemoryIndex),
41 mOffsetInBytes(parcelable.offsetInBytes),
42 mSizeInBytes(parcelable.sizeInBytes) {}
43
44SharedRegion SharedRegionParcelable::parcelable() const {
45 SharedRegion result;
46 result.sharedMemoryIndex = mSharedMemoryIndex;
47 result.offsetInBytes = mOffsetInBytes;
48 result.sizeInBytes = mSizeInBytes;
49 return result;
50}
Phil Burk204a1632017-01-03 17:23:43 -080051
52void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
53 int32_t offsetInBytes,
54 int32_t sizeInBytes) {
55 mSharedMemoryIndex = sharedMemoryIndex;
56 mOffsetInBytes = offsetInBytes;
57 mSizeInBytes = sizeInBytes;
58}
59
Phil Burk5ed503c2017-02-01 09:38:15 -080060aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080061 void **regionAddressPtr) {
62 if (mSizeInBytes == 0) {
63 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080064 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080065 }
66 if (mSharedMemoryIndex < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070067 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080068 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080069 }
70 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
71 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
72}
73
Phil Burka5891f42018-03-12 17:21:11 -070074aaudio_result_t SharedRegionParcelable::validate() const {
Phil Burk3df348f2017-02-08 11:41:55 -080075 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070076 ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080077 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080078 }
79 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -080080 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070081 ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080082 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080083 }
84 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070085 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080086 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080087 }
88 }
Phil Burk5ed503c2017-02-01 09:38:15 -080089 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080090}
91
92void SharedRegionParcelable::dump() {
Phil Burkfbf031e2017-10-12 15:58:31 -070093 ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080094 if (mSizeInBytes > 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070095 ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
96 ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080097 }
98}