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 "SharedRegionParcelable"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <stdint.h>
22
23 #include <sys/mman.h>
24 #include <binder/Parcelable.h>
25
26 #include <aaudio/AAudio.h>
27 #include <utility/AAudioUtilities.h>
28
29 #include "binding/SharedMemoryParcelable.h"
30 #include "binding/SharedRegionParcelable.h"
31
32 using android::NO_ERROR;
33 using android::status_t;
34 using android::Parcel;
35 using android::Parcelable;
36
37 using namespace aaudio;
38
SharedRegionParcelable(const SharedRegion & parcelable)39 SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable)
40 : mSharedMemoryIndex(parcelable.sharedMemoryIndex),
41 mOffsetInBytes(parcelable.offsetInBytes),
42 mSizeInBytes(parcelable.sizeInBytes) {}
43
parcelable() const44 SharedRegion SharedRegionParcelable::parcelable() const {
45 SharedRegion result;
46 result.sharedMemoryIndex = mSharedMemoryIndex;
47 result.offsetInBytes = mOffsetInBytes;
48 result.sizeInBytes = mSizeInBytes;
49 return result;
50 }
51
setup(int32_t sharedMemoryIndex,int32_t offsetInBytes,int32_t sizeInBytes)52 void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
53 int32_t offsetInBytes,
54 int32_t sizeInBytes) {
55 mSharedMemoryIndex = sharedMemoryIndex;
56 mOffsetInBytes = offsetInBytes;
57 mSizeInBytes = sizeInBytes;
58 }
59
resolve(SharedMemoryParcelable * memoryParcels,void ** regionAddressPtr)60 aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
61 void **regionAddressPtr) {
62 if (mSizeInBytes == 0) {
63 *regionAddressPtr = nullptr;
64 return AAUDIO_OK;
65 }
66 if (mSharedMemoryIndex < 0) {
67 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
68 return AAUDIO_ERROR_INTERNAL;
69 }
70 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
71 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
72 }
73
validate() const74 aaudio_result_t SharedRegionParcelable::validate() const {
75 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
76 ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
77 return AAUDIO_ERROR_OUT_OF_RANGE;
78 }
79 if (mSizeInBytes > 0) {
80 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
81 ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
82 return AAUDIO_ERROR_OUT_OF_RANGE;
83 }
84 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
85 ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
86 return AAUDIO_ERROR_INTERNAL;
87 }
88 }
89 return AAUDIO_OK;
90 }
91
dump()92 void SharedRegionParcelable::dump() {
93 ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
94 if (mSizeInBytes > 0) {
95 ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
96 ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
97 }
98 }
99