SharedRegionParcelable.cpp revision 3df348fbaca567ca891503213ff8c344a1ea2e05
1204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk/*
2204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Copyright 2016 The Android Open Source Project
3204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
4204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * you may not use this file except in compliance with the License.
6204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * You may obtain a copy of the License at
7204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
8204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
10204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Unless required by applicable law or agreed to in writing, software
11204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * See the License for the specific language governing permissions and
14204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * limitations under the License.
15204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk */
16204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
17204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <stdint.h>
18204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
19204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <sys/mman.h>
20204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <binder/Parcelable.h>
21204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudioDefinitions.h>
23204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
24204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "binding/SharedMemoryParcelable.h"
25204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "binding/SharedRegionParcelable.h"
26204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::NO_ERROR;
28204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::status_t;
29204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcel;
30204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcelable;
31204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing namespace aaudio;
33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
34204a163c86f357a878873fe7d4c4164f3d55c9b6Phil BurkSharedRegionParcelable::SharedRegionParcelable() {}
35204a163c86f357a878873fe7d4c4164f3d55c9b6Phil BurkSharedRegionParcelable::~SharedRegionParcelable() {}
36204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
37204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkvoid SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
38204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                   int32_t offsetInBytes,
39204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                   int32_t sizeInBytes) {
40204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mSharedMemoryIndex = sharedMemoryIndex;
41204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mOffsetInBytes = offsetInBytes;
42204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mSizeInBytes = sizeInBytes;
43204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
44204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
45204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkstatus_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
46204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    parcel->writeInt32(mSizeInBytes);
47204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
48204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->writeInt32(mSharedMemoryIndex);
49204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->writeInt32(mOffsetInBytes);
50204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
51204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return NO_ERROR; // TODO check for errors above
52204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
53204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
54204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkstatus_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
55204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    parcel->readInt32(&mSizeInBytes);
56204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
57204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->readInt32(&mSharedMemoryIndex);
58204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->readInt32(&mOffsetInBytes);
59204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
60204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return NO_ERROR; // TODO check for errors above
61204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
62204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
64204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                              void **regionAddressPtr) {
65204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes == 0) {
66204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        *regionAddressPtr = nullptr;
675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
68204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
69204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSharedMemoryIndex < 0) {
70204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
72204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
73204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
74204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
75204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
76204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t SharedRegionParcelable::validate() {
783df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
79204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes);
803df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        return AAUDIO_ERROR_OUT_OF_RANGE;
81204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
82204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
833df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
84204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk            ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes);
853df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk            return AAUDIO_ERROR_OUT_OF_RANGE;
86204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        }
87204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
88204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk            ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
895ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk            return AAUDIO_ERROR_INTERNAL;
90204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        }
91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
925ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return AAUDIO_OK;
93204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
94204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
95204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkvoid SharedRegionParcelable::dump() {
96204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes);
97204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
98204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex);
99204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes);
100204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
101204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
102