blob: d6205129e067d8c3bb0fc8727af4fed54416ad2d [file] [log] [blame]
Andreas Huber0a451282016-08-30 11:27:24 -07001/*
2 * Copyright (C) 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 "MQDescriptor"
18#include <android-base/logging.h>
19
20#include <hidl/MQDescriptor.h>
21
22#include <cutils/native_handle.h>
23#include <sys/mman.h>
24
25namespace android {
26namespace hardware {
27
28MQDescriptor::MQDescriptor(
29 const std::vector<GrantorDescriptor>& grantors,
30 native_handle_t* nhandle,
31 int32_t flags,
32 size_t size)
33 : mHandle(nhandle),
34 mQuantum(size),
35 mFlags(flags) {
36 mGrantors.resize(grantors.size());
37 for (size_t i = 0; i < grantors.size(); ++i) {
38 mGrantors[i] = grantors[i];
39 }
40}
41
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070042MQDescriptor::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
43 int32_t flags, size_t messageSize)
44 : mHandle(nHandle), mQuantum(messageSize), mFlags(flags) {
45 mGrantors.resize(kMinGrantorCount);
46 /*
47 * Create a default grantor descriptor for read, write pointers and
48 * the data buffer. fdIndex parameter is set to 0 by default and
49 * the offset for each grantor is contiguous.
50 */
51 mGrantors[READPTRPOS] = {
52 0 /* grantor flags */, 0 /* fdIndex */, 0 /* offset */,
53 sizeof(RingBufferPosition) /* extent */
54 };
55
56 mGrantors[WRITEPTRPOS] = {
57 0 /* grantor flags */,
58 0 /* fdIndex */,
59 sizeof(RingBufferPosition) /* offset */,
60 sizeof(RingBufferPosition) /* extent */
61 };
62 mGrantors[DATAPTRPOS] = {
63 0 /* grantor flags */, 0 /* fdIndex */,
64 2 * sizeof(RingBufferPosition) /* offset */, bufferSize /* extent */
65 };
66}
67
Andreas Huber0a451282016-08-30 11:27:24 -070068MQDescriptor::MQDescriptor(const MQDescriptor &other)
69 : mGrantors(other.mGrantors),
70 mHandle(nullptr),
71 mQuantum(other.mQuantum),
72 mFlags(other.mFlags) {
73 if (other.mHandle != nullptr) {
74 mHandle = native_handle_create(
75 other.mHandle->numFds, other.mHandle->numInts);
76
77 for (int i = 0; i < other.mHandle->numFds; ++i) {
78 const_cast<native_handle_t *>(mHandle)->data[i] =
79 dup(other.mHandle->data[i]);
80 }
81
82 memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds],
83 &other.mHandle->data[other.mHandle->numFds],
84 other.mHandle->numInts * sizeof(int));
85 }
86}
87
88MQDescriptor::~MQDescriptor() {
89 if (mHandle != nullptr) {
90 native_handle_close(const_cast<native_handle_t *>(mHandle));
91 native_handle_delete(const_cast<native_handle_t *>(mHandle));
92 }
93}
94
Andreas Huber0a451282016-08-30 11:27:24 -070095::android::status_t MQDescriptor::readEmbeddedFromParcel(
96 const Parcel &parcel,
97 size_t parentHandle,
98 size_t parentOffset) {
99 ::android::status_t _hidl_err = ::android::OK;
100
101 size_t _hidl_grantors_child;
102
103 _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>(
104 &mGrantors)->readEmbeddedFromParcel(
105 parcel,
106 parentHandle,
107 parentOffset + offsetof(MQDescriptor, mGrantors),
108 &_hidl_grantors_child);
109
110 if (_hidl_err != ::android::OK) { return _hidl_err; }
111
112 const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle(
113 parentHandle,
114 parentOffset + offsetof(MQDescriptor, mHandle));
115
116 if (_hidl_mq_handle_ptr == nullptr) {
117 _hidl_err = ::android::UNKNOWN_ERROR;
118 return _hidl_err;
119 }
120
121_hidl_error:
122 return _hidl_err;
123}
124
125::android::status_t MQDescriptor::writeEmbeddedToParcel(
126 Parcel *parcel,
127 size_t parentHandle,
128 size_t parentOffset) const {
129 ::android::status_t _hidl_err = ::android::OK;
130
131 size_t _hidl_grantors_child;
132
133 _hidl_err = mGrantors.writeEmbeddedToParcel(
134 parcel,
135 parentHandle,
136 parentOffset + offsetof(MQDescriptor, mGrantors),
137 &_hidl_grantors_child);
138
139 if (_hidl_err != ::android::OK) { return _hidl_err; }
140
141 _hidl_err = parcel->writeEmbeddedNativeHandle(
142 mHandle,
143 parentHandle,
144 parentOffset + offsetof(MQDescriptor, mHandle));
145
146 if (_hidl_err != ::android::OK) { return _hidl_err; }
147
148_hidl_error:
149 return _hidl_err;
150}
151
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700152size_t MQDescriptor::getSize() const {
153 return mGrantors[DATAPTRPOS].extent;
154}
155
156size_t MQDescriptor::getQuantum() const { return mQuantum; }
157
158int32_t MQDescriptor::getFlags() const { return mFlags; }
159
160std::vector<GrantorDescriptor> MQDescriptor::getGrantors() const {
161 size_t grantor_count = mGrantors.size();
162 std::vector<GrantorDescriptor> grantors(grantor_count);
163 for (size_t i = 0; i < grantor_count; i++) {
164 grantors[i] = mGrantors[i];
165 }
166 return grantors;
167}
168
169const sp<NativeHandle> MQDescriptor::getNativeHandle() const {
170 /*
171 * Create an sp<NativeHandle> from mHandle.
172 */
173 return NativeHandle::create(mHandle, false /* ownsHandle */);
174}
175
Andreas Huber0a451282016-08-30 11:27:24 -0700176} // namespace hardware
177} // namespace android
178