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 <binder/Parcel.h>
24 #include <binder/Parcelable.h>
25 #include <utility/AAudioUtilities.h>
26
27 #include "binding/AAudioServiceDefinitions.h"
28 #include "binding/RingBufferParcelable.h"
29 #include "binding/AudioEndpointParcelable.h"
30
31 using android::base::unique_fd;
32 using android::NO_ERROR;
33 using android::status_t;
34 using android::Parcel;
35 using android::Parcelable;
36
37 using namespace aaudio;
38
39 /**
40 * Container for information about the message queues plus
41 * general stream information needed by AAudio clients.
42 * It contains no addresses, just sizes, offsets and file descriptors for
43 * shared memory that can be passed through Binder.
44 */
AudioEndpointParcelable()45 AudioEndpointParcelable::AudioEndpointParcelable() {}
46
~AudioEndpointParcelable()47 AudioEndpointParcelable::~AudioEndpointParcelable() {}
48
49 /**
50 * Add the file descriptor to the table.
51 * @return index in table or negative error
52 */
addFileDescriptor(const unique_fd & fd,int32_t sizeInBytes)53 int32_t AudioEndpointParcelable::addFileDescriptor(const unique_fd& fd,
54 int32_t sizeInBytes) {
55 if (mNumSharedMemories >= MAX_SHARED_MEMORIES) {
56 return AAUDIO_ERROR_OUT_OF_RANGE;
57 }
58 int32_t index = mNumSharedMemories++;
59 mSharedMemories[index].setup(fd, sizeInBytes);
60 return index;
61 }
62
63 /**
64 * The read and write must be symmetric.
65 */
writeToParcel(Parcel * parcel) const66 status_t AudioEndpointParcelable::writeToParcel(Parcel* parcel) const {
67 parcel->writeInt32(mNumSharedMemories);
68 for (int i = 0; i < mNumSharedMemories; i++) {
69 mSharedMemories[i].writeToParcel(parcel);
70 }
71 mUpMessageQueueParcelable.writeToParcel(parcel);
72 mDownMessageQueueParcelable.writeToParcel(parcel);
73 mUpDataQueueParcelable.writeToParcel(parcel);
74 mDownDataQueueParcelable.writeToParcel(parcel);
75 return NO_ERROR; // TODO check for errors above
76 }
77
readFromParcel(const Parcel * parcel)78 status_t AudioEndpointParcelable::readFromParcel(const Parcel* parcel) {
79 parcel->readInt32(&mNumSharedMemories);
80 for (int i = 0; i < mNumSharedMemories; i++) {
81 mSharedMemories[i].readFromParcel(parcel);
82 }
83 mUpMessageQueueParcelable.readFromParcel(parcel);
84 mDownMessageQueueParcelable.readFromParcel(parcel);
85 mUpDataQueueParcelable.readFromParcel(parcel);
86 mDownDataQueueParcelable.readFromParcel(parcel);
87 return NO_ERROR; // TODO check for errors above
88 }
89
resolve(EndpointDescriptor * descriptor)90 aaudio_result_t AudioEndpointParcelable::resolve(EndpointDescriptor *descriptor) {
91 aaudio_result_t result = mUpMessageQueueParcelable.resolve(mSharedMemories,
92 &descriptor->upMessageQueueDescriptor);
93 if (result != AAUDIO_OK) return result;
94 result = mDownMessageQueueParcelable.resolve(mSharedMemories,
95 &descriptor->downMessageQueueDescriptor);
96 if (result != AAUDIO_OK) return result;
97
98 result = mDownDataQueueParcelable.resolve(mSharedMemories,
99 &descriptor->dataQueueDescriptor);
100 return result;
101 }
102
close()103 aaudio_result_t AudioEndpointParcelable::close() {
104 int err = 0;
105 for (int i = 0; i < mNumSharedMemories; i++) {
106 int lastErr = mSharedMemories[i].close();
107 if (lastErr < 0) err = lastErr;
108 }
109 return AAudioConvert_androidToAAudioResult(err);
110 }
111
validate()112 aaudio_result_t AudioEndpointParcelable::validate() {
113 aaudio_result_t result;
114 if (mNumSharedMemories < 0 || mNumSharedMemories >= MAX_SHARED_MEMORIES) {
115 ALOGE("AudioEndpointParcelable invalid mNumSharedMemories = %d", mNumSharedMemories);
116 return AAUDIO_ERROR_INTERNAL;
117 }
118 for (int i = 0; i < mNumSharedMemories; i++) {
119 result = mSharedMemories[i].validate();
120 if (result != AAUDIO_OK) {
121 ALOGE("AudioEndpointParcelable invalid mSharedMemories[%d] = %d", i, result);
122 return result;
123 }
124 }
125 if ((result = mUpMessageQueueParcelable.validate()) != AAUDIO_OK) {
126 ALOGE("AudioEndpointParcelable invalid mUpMessageQueueParcelable = %d", result);
127 return result;
128 }
129 if ((result = mDownMessageQueueParcelable.validate()) != AAUDIO_OK) {
130 ALOGE("AudioEndpointParcelable invalid mDownMessageQueueParcelable = %d", result);
131 return result;
132 }
133 if ((result = mUpDataQueueParcelable.validate()) != AAUDIO_OK) {
134 ALOGE("AudioEndpointParcelable invalid mUpDataQueueParcelable = %d", result);
135 return result;
136 }
137 if ((result = mDownDataQueueParcelable.validate()) != AAUDIO_OK) {
138 ALOGE("AudioEndpointParcelable invalid mDownDataQueueParcelable = %d", result);
139 return result;
140 }
141 return AAUDIO_OK;
142 }
143
dump()144 void AudioEndpointParcelable::dump() {
145 ALOGD("AudioEndpointParcelable ======================================= BEGIN");
146 ALOGD("AudioEndpointParcelable mNumSharedMemories = %d", mNumSharedMemories);
147 for (int i = 0; i < mNumSharedMemories; i++) {
148 mSharedMemories[i].dump();
149 }
150 ALOGD("AudioEndpointParcelable mUpMessageQueueParcelable =========");
151 mUpMessageQueueParcelable.dump();
152 ALOGD("AudioEndpointParcelable mDownMessageQueueParcelable =======");
153 mDownMessageQueueParcelable.dump();
154 ALOGD("AudioEndpointParcelable mUpDataQueueParcelable ============");
155 mUpDataQueueParcelable.dump();
156 ALOGD("AudioEndpointParcelable mDownDataQueueParcelable ==========");
157 mDownDataQueueParcelable.dump();
158 ALOGD("AudioEndpointParcelable ======================================= END");
159 }
160
161