blob: 8ca0023730d5df9be7ca20ea2d371147d43c79e1 [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
17#include <stdint.h>
18
19#include <sys/mman.h>
20#include <binder/Parcelable.h>
21
Phil Burk5ed503c2017-02-01 09:38:15 -080022#include <aaudio/AAudioDefinitions.h>
Phil Burk204a1632017-01-03 17:23:43 -080023
24#include "binding/SharedMemoryParcelable.h"
25#include "binding/SharedRegionParcelable.h"
26
27using android::NO_ERROR;
28using android::status_t;
29using android::Parcel;
30using android::Parcelable;
31
Phil Burk5ed503c2017-02-01 09:38:15 -080032using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080033
34SharedRegionParcelable::SharedRegionParcelable() {}
35SharedRegionParcelable::~SharedRegionParcelable() {}
36
37void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
38 int32_t offsetInBytes,
39 int32_t sizeInBytes) {
40 mSharedMemoryIndex = sharedMemoryIndex;
41 mOffsetInBytes = offsetInBytes;
42 mSizeInBytes = sizeInBytes;
43}
44
45status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
46 parcel->writeInt32(mSizeInBytes);
47 if (mSizeInBytes > 0) {
48 parcel->writeInt32(mSharedMemoryIndex);
49 parcel->writeInt32(mOffsetInBytes);
50 }
51 return NO_ERROR; // TODO check for errors above
52}
53
54status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
55 parcel->readInt32(&mSizeInBytes);
56 if (mSizeInBytes > 0) {
57 parcel->readInt32(&mSharedMemoryIndex);
58 parcel->readInt32(&mOffsetInBytes);
59 }
60 return NO_ERROR; // TODO check for errors above
61}
62
Phil Burk5ed503c2017-02-01 09:38:15 -080063aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
Phil Burk204a1632017-01-03 17:23:43 -080064 void **regionAddressPtr) {
65 if (mSizeInBytes == 0) {
66 *regionAddressPtr = nullptr;
Phil Burk5ed503c2017-02-01 09:38:15 -080067 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080068 }
69 if (mSharedMemoryIndex < 0) {
70 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080071 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080072 }
73 SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
74 return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
75}
76
Phil Burk5ed503c2017-02-01 09:38:15 -080077aaudio_result_t SharedRegionParcelable::validate() {
Phil Burk3df348f2017-02-08 11:41:55 -080078 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
Phil Burk204a1632017-01-03 17:23:43 -080079 ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080080 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080081 }
82 if (mSizeInBytes > 0) {
Phil Burk3df348f2017-02-08 11:41:55 -080083 if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
Phil Burk204a1632017-01-03 17:23:43 -080084 ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes);
Phil Burk3df348f2017-02-08 11:41:55 -080085 return AAUDIO_ERROR_OUT_OF_RANGE;
Phil Burk204a1632017-01-03 17:23:43 -080086 }
87 if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
88 ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
Phil Burk5ed503c2017-02-01 09:38:15 -080089 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -080090 }
91 }
Phil Burk5ed503c2017-02-01 09:38:15 -080092 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -080093}
94
95void SharedRegionParcelable::dump() {
96 ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes);
97 if (mSizeInBytes > 0) {
98 ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex);
99 ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes);
100 }
101}