blob: 6fa109b33d5531559b5f2957b905e818c5bb9ef6 [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
Phil Burk204a1632017-01-03 17:23:43 -080032using android::status_t;
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080035
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070036SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable)
37 : mSharedMemoryIndex(parcelable.sharedMemoryIndex),
38 mOffsetInBytes(parcelable.offsetInBytes),
39 mSizeInBytes(parcelable.sizeInBytes) {}
40
41SharedRegion SharedRegionParcelable::parcelable() const {
42 SharedRegion result;
43 result.sharedMemoryIndex = mSharedMemoryIndex;
44 result.offsetInBytes = mOffsetInBytes;
45 result.sizeInBytes = mSizeInBytes;
46 return result;
47}
Phil Burk204a1632017-01-03 17:23:43 -080048
49void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
50 int32_t offsetInBytes,
51 int32_t sizeInBytes) {
52 mSharedMemoryIndex = sharedMemoryIndex;
53 mOffsetInBytes = offsetInBytes;
54 mSizeInBytes = sizeInBytes;
55}
56
Phil Burk5ed503c2017-02-01 09:38:15 -080057aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080058 void **regionAddressPtr) {
59 if (mSizeInBytes == 0) {
60 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080061 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080062 }
63 if (mSharedMemoryIndex < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070064 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080065 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080066 }
67 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
68 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
69}
70
Phil Burka5891f42018-03-12 17:21:11 -070071aaudio_result_t SharedRegionParcelable::validate() const {
Phil Burk3df348f2017-02-08 11:41:55 -080072 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070073 ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080074 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080075 }
76 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -080077 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070078 ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080079 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080080 }
81 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
Phil Burkfbf031e2017-10-12 15:58:31 -070082 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080083 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080084 }
85 }
Phil Burk5ed503c2017-02-01 09:38:15 -080086 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080087}
88
89void SharedRegionParcelable::dump() {
Phil Burkfbf031e2017-10-12 15:58:31 -070090 ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080091 if (mSizeInBytes > 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070092 ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
93 ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk204a1632017-01-03 17:23:43 -080094 }
95}