• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::status_t;
33 
34 using namespace aaudio;
35 
SharedRegionParcelable(const SharedRegion & parcelable)36 SharedRegionParcelable::SharedRegionParcelable(const SharedRegion& parcelable)
37         : mSharedMemoryIndex(parcelable.sharedMemoryIndex),
38           mOffsetInBytes(parcelable.offsetInBytes),
39           mSizeInBytes(parcelable.sizeInBytes) {}
40 
parcelable() const41 SharedRegion SharedRegionParcelable::parcelable() const {
42     SharedRegion result;
43     result.sharedMemoryIndex = mSharedMemoryIndex;
44     result.offsetInBytes = mOffsetInBytes;
45     result.sizeInBytes = mSizeInBytes;
46     return result;
47 }
48 
setup(int32_t sharedMemoryIndex,int32_t offsetInBytes,int32_t sizeInBytes)49 void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
50                                    int32_t offsetInBytes,
51                                    int32_t sizeInBytes) {
52     mSharedMemoryIndex = sharedMemoryIndex;
53     mOffsetInBytes = offsetInBytes;
54     mSizeInBytes = sizeInBytes;
55 }
56 
resolve(SharedMemoryParcelable * memoryParcels,void ** regionAddressPtr)57 aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
58                                               void **regionAddressPtr) {
59     if (mSizeInBytes == 0) {
60         *regionAddressPtr = nullptr;
61         return AAUDIO_OK;
62     }
63     if (mSharedMemoryIndex < 0) {
64         ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
65         return AAUDIO_ERROR_INTERNAL;
66     }
67     SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
68     return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
69 }
70 
validate() const71 aaudio_result_t SharedRegionParcelable::validate() const {
72     if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
73         ALOGE("invalid mSizeInBytes = %d", mSizeInBytes);
74         return AAUDIO_ERROR_OUT_OF_RANGE;
75     }
76     if (mSizeInBytes > 0) {
77         if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
78             ALOGE("invalid mOffsetInBytes = %d", mOffsetInBytes);
79             return AAUDIO_ERROR_OUT_OF_RANGE;
80         }
81         if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
82             ALOGE("invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
83             return AAUDIO_ERROR_INTERNAL;
84         }
85     }
86     return AAUDIO_OK;
87 }
88 
dump()89 void SharedRegionParcelable::dump() {
90     ALOGD("mSizeInBytes = %d -----", mSizeInBytes);
91     if (mSizeInBytes > 0) {
92         ALOGD("mSharedMemoryIndex = %d", mSharedMemoryIndex);
93         ALOGD("mOffsetInBytes = %d", mOffsetInBytes);
94     }
95 }
96