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
17c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#define LOG_TAG "AAudio"
18c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk//#define LOG_NDEBUG 0
19c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include <utils/Log.h>
20c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
21204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <stdint.h>
22204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
23204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <sys/mman.h>
24204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <binder/Parcelable.h>
25204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
26a4eb0d86a29be2763be5fac51727858d5095794bPhil Burk#include <aaudio/AAudio.h>
27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
28204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "binding/SharedMemoryParcelable.h"
29204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include "binding/SharedRegionParcelable.h"
30204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
31204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::NO_ERROR;
32204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::status_t;
33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcel;
34204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcelable;
35204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing namespace aaudio;
37204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
38204a163c86f357a878873fe7d4c4164f3d55c9b6Phil BurkSharedRegionParcelable::SharedRegionParcelable() {}
39204a163c86f357a878873fe7d4c4164f3d55c9b6Phil BurkSharedRegionParcelable::~SharedRegionParcelable() {}
40204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
41204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkvoid SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
42204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                   int32_t offsetInBytes,
43204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                   int32_t sizeInBytes) {
44204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mSharedMemoryIndex = sharedMemoryIndex;
45204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mOffsetInBytes = offsetInBytes;
46204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    mSizeInBytes = sizeInBytes;
47204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
48204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
49204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkstatus_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
50204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    parcel->writeInt32(mSizeInBytes);
51204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
52204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->writeInt32(mSharedMemoryIndex);
53204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->writeInt32(mOffsetInBytes);
54204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
55204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return NO_ERROR; // TODO check for errors above
56204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
57204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
58204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkstatus_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
59204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    parcel->readInt32(&mSizeInBytes);
60204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
61204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->readInt32(&mSharedMemoryIndex);
62204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        parcel->readInt32(&mOffsetInBytes);
63204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
64204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return NO_ERROR; // TODO check for errors above
65204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
66204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
68204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk                                              void **regionAddressPtr) {
69204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes == 0) {
70204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        *regionAddressPtr = nullptr;
715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
72204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
73204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSharedMemoryIndex < 0) {
74204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
76204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
77204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
78204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
79204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
80204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
815ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t SharedRegionParcelable::validate() {
823df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
83204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes);
843df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        return AAUDIO_ERROR_OUT_OF_RANGE;
85204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
86204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
873df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
88204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk            ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes);
893df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk            return AAUDIO_ERROR_OUT_OF_RANGE;
90204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        }
91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
92204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk            ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk            return AAUDIO_ERROR_INTERNAL;
94204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        }
95204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return AAUDIO_OK;
97204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
98204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
99204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkvoid SharedRegionParcelable::dump() {
100204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes);
101204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSizeInBytes > 0) {
102204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex);
103204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes);
104204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
105204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
106