Home | History | Annotate | Download | only in binding
      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 "AAudio"
     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 
     28 #include "binding/SharedMemoryParcelable.h"
     29 #include "binding/SharedRegionParcelable.h"
     30 
     31 using android::NO_ERROR;
     32 using android::status_t;
     33 using android::Parcel;
     34 using android::Parcelable;
     35 
     36 using namespace aaudio;
     37 
     38 SharedRegionParcelable::SharedRegionParcelable() {}
     39 SharedRegionParcelable::~SharedRegionParcelable() {}
     40 
     41 void SharedRegionParcelable::setup(int32_t sharedMemoryIndex,
     42                                    int32_t offsetInBytes,
     43                                    int32_t sizeInBytes) {
     44     mSharedMemoryIndex = sharedMemoryIndex;
     45     mOffsetInBytes = offsetInBytes;
     46     mSizeInBytes = sizeInBytes;
     47 }
     48 
     49 status_t SharedRegionParcelable::writeToParcel(Parcel* parcel) const {
     50     parcel->writeInt32(mSizeInBytes);
     51     if (mSizeInBytes > 0) {
     52         parcel->writeInt32(mSharedMemoryIndex);
     53         parcel->writeInt32(mOffsetInBytes);
     54     }
     55     return NO_ERROR; // TODO check for errors above
     56 }
     57 
     58 status_t SharedRegionParcelable::readFromParcel(const Parcel* parcel) {
     59     parcel->readInt32(&mSizeInBytes);
     60     if (mSizeInBytes > 0) {
     61         parcel->readInt32(&mSharedMemoryIndex);
     62         parcel->readInt32(&mOffsetInBytes);
     63     }
     64     return NO_ERROR; // TODO check for errors above
     65 }
     66 
     67 aaudio_result_t SharedRegionParcelable::resolve(SharedMemoryParcelable *memoryParcels,
     68                                               void **regionAddressPtr) {
     69     if (mSizeInBytes == 0) {
     70         *regionAddressPtr = nullptr;
     71         return AAUDIO_OK;
     72     }
     73     if (mSharedMemoryIndex < 0) {
     74         ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
     75         return AAUDIO_ERROR_INTERNAL;
     76     }
     77     SharedMemoryParcelable *memoryParcel = &memoryParcels[mSharedMemoryIndex];
     78     return memoryParcel->resolve(mOffsetInBytes, mSizeInBytes, regionAddressPtr);
     79 }
     80 
     81 aaudio_result_t SharedRegionParcelable::validate() {
     82     if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
     83         ALOGE("SharedRegionParcelable invalid mSizeInBytes = %d", mSizeInBytes);
     84         return AAUDIO_ERROR_OUT_OF_RANGE;
     85     }
     86     if (mSizeInBytes > 0) {
     87         if (mOffsetInBytes < 0 || mOffsetInBytes >= MAX_MMAP_OFFSET_BYTES) {
     88             ALOGE("SharedRegionParcelable invalid mOffsetInBytes = %d", mOffsetInBytes);
     89             return AAUDIO_ERROR_OUT_OF_RANGE;
     90         }
     91         if (mSharedMemoryIndex < 0 || mSharedMemoryIndex >= MAX_SHARED_MEMORIES) {
     92             ALOGE("SharedRegionParcelable invalid mSharedMemoryIndex = %d", mSharedMemoryIndex);
     93             return AAUDIO_ERROR_INTERNAL;
     94         }
     95     }
     96     return AAUDIO_OK;
     97 }
     98 
     99 void SharedRegionParcelable::dump() {
    100     ALOGD("SharedRegionParcelable mSizeInBytes = %d -----", mSizeInBytes);
    101     if (mSizeInBytes > 0) {
    102         ALOGD("SharedRegionParcelable mSharedMemoryIndex = %d", mSharedMemoryIndex);
    103         ALOGD("SharedRegionParcelable mOffsetInBytes = %d", mOffsetInBytes);
    104     }
    105 }
    106