blob: a3cfeb2d55d34d0b9f05f8cc91ca3cbff1d8f63b [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#ifndef _FMSGQ_DESCRIPTOR_H
18#define _FMSGQ_DESCRIPTOR_H
19
20#include <android-base/macros.h>
21#include <cutils/native_handle.h>
22#include <hidl/HidlSupport.h>
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070023#include <utils/NativeHandle.h>
Andreas Huber0a451282016-08-30 11:27:24 -070024
25namespace android {
26namespace hardware {
27
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070028typedef uint64_t RingBufferPosition;
29
Andreas Huber0a451282016-08-30 11:27:24 -070030struct GrantorDescriptor {
31 uint32_t flags;
32 uint32_t fdIndex;
33 uint32_t offset;
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070034 size_t extent;
Andreas Huber0a451282016-08-30 11:27:24 -070035};
36
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070037enum MQFlavor : uint32_t {
38 /*
39 * kSynchronizedReadWrite represents the wait-free synchronized flavor of the
40 * FMQ. It is intended to be have a single reader and single writer.
41 * Attempts to overflow/underflow returns a failure.
42 */
43 kSynchronizedReadWrite = 0x01
44};
45
46template <MQFlavor flavor>
Andreas Huber0a451282016-08-30 11:27:24 -070047struct MQDescriptor {
48 MQDescriptor(
49 const std::vector<GrantorDescriptor>& grantors,
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070050 native_handle_t* nHandle, size_t size);
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070051
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070052 MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070053 size_t messageSize);
Andreas Huber0a451282016-08-30 11:27:24 -070054
55 ~MQDescriptor();
56
57 explicit MQDescriptor(const MQDescriptor &other);
58 MQDescriptor &operator=(const MQDescriptor &other) = delete;
59
60 size_t getSize() const;
61
62 size_t getQuantum() const;
63
64 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070065
66 ::android::status_t readEmbeddedFromParcel(
67 const ::android::hardware::Parcel &parcel,
68 size_t parentHandle,
69 size_t parentOffset);
70
71 ::android::status_t writeEmbeddedToParcel(
72 ::android::hardware::Parcel *parcel,
73 size_t parentHandle,
74 size_t parentOffset) const;
75
76 bool isHandleValid() const { return mHandle != nullptr; }
77 size_t countGrantors() const { return mGrantors.size(); }
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070078 std::vector<GrantorDescriptor> getGrantors() const;
79 const sp<NativeHandle> getNativeHandle() const;
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070080 /*
81 * There should atleast be GrantorDescriptors for the read counter, write
82 * counter and data buffer.
83 */
84 static constexpr int32_t kMinGrantorCount = 3;
85 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS };
Andreas Huber0a451282016-08-30 11:27:24 -070086private:
87 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070088 ::native_handle_t *mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -070089 uint32_t mQuantum;
90 uint32_t mFlags;
91};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070092
93/*
94 * MQDescriptorSync will describe the wait-free synchronized
95 * flavor of FMQ.
96 */
97using MQDescriptorSync = MQDescriptor<kSynchronizedReadWrite>;
98
99template<MQFlavor flavor>
100MQDescriptor<flavor>::MQDescriptor(
101 const std::vector<GrantorDescriptor>& grantors,
102 native_handle_t* nhandle,
103 size_t size)
104 : mHandle(nhandle),
105 mQuantum(size),
106 mFlags(flavor) {
107 mGrantors.resize(grantors.size());
108 for (size_t i = 0; i < grantors.size(); ++i) {
109 mGrantors[i] = grantors[i];
110 }
111}
112
113template<MQFlavor flavor>
114MQDescriptor<flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
115 size_t messageSize)
116 : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
117 mGrantors.resize(kMinGrantorCount);
118 /*
119 * Create a default grantor descriptor for read, write pointers and
120 * the data buffer. fdIndex parameter is set to 0 by default and
121 * the offset for each grantor is contiguous.
122 */
123 mGrantors[READPTRPOS] = {
124 0 /* grantor flags */, 0 /* fdIndex */, 0 /* offset */,
125 sizeof(RingBufferPosition) /* extent */
126 };
127
128 mGrantors[WRITEPTRPOS] = {
129 0 /* grantor flags */,
130 0 /* fdIndex */,
131 sizeof(RingBufferPosition) /* offset */,
132 sizeof(RingBufferPosition) /* extent */
133 };
134 mGrantors[DATAPTRPOS] = {
135 0 /* grantor flags */, 0 /* fdIndex */,
136 2 * sizeof(RingBufferPosition) /* offset */, bufferSize /* extent */
137 };
138}
139
140template<MQFlavor flavor>
141MQDescriptor<flavor>::MQDescriptor(const MQDescriptor<flavor> &other)
142 : mGrantors(other.mGrantors),
143 mHandle(nullptr),
144 mQuantum(other.mQuantum),
145 mFlags(other.mFlags) {
146 if (other.mHandle != nullptr) {
147 mHandle = native_handle_create(
148 other.mHandle->numFds, other.mHandle->numInts);
149
150 for (int i = 0; i < other.mHandle->numFds; ++i) {
151 const_cast<native_handle_t *>(mHandle)->data[i] =
152 dup(other.mHandle->data[i]);
153 }
154
155 memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds],
156 &other.mHandle->data[other.mHandle->numFds],
157 other.mHandle->numInts * sizeof(int));
158 }
159}
160
161template<MQFlavor flavor>
162MQDescriptor<flavor>::~MQDescriptor() {
163 if (mHandle != nullptr) {
164 native_handle_close(const_cast<native_handle_t *>(mHandle));
165 native_handle_delete(const_cast<native_handle_t *>(mHandle));
166 }
167}
168
169template<MQFlavor flavor>
170::android::status_t MQDescriptor<flavor>::readEmbeddedFromParcel(
171 const Parcel &parcel,
172 size_t parentHandle,
173 size_t parentOffset) {
174 ::android::status_t _hidl_err = ::android::OK;
175
176 size_t _hidl_grantors_child;
177
178 _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>(
179 &mGrantors)->readEmbeddedFromParcel(
180 parcel,
181 parentHandle,
182 parentOffset + offsetof(MQDescriptor, mGrantors),
183 &_hidl_grantors_child);
184
185 if (_hidl_err != ::android::OK) { return _hidl_err; }
186
187 const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle(
188 parentHandle,
189 parentOffset + offsetof(MQDescriptor, mHandle));
190
191 if (_hidl_mq_handle_ptr == nullptr) {
192 _hidl_err = ::android::UNKNOWN_ERROR;
193 return _hidl_err;
194 }
195
196 return _hidl_err;
197}
198
199template<MQFlavor flavor>
200::android::status_t MQDescriptor<flavor>::writeEmbeddedToParcel(
201 Parcel *parcel,
202 size_t parentHandle,
203 size_t parentOffset) const {
204 ::android::status_t _hidl_err = ::android::OK;
205
206 size_t _hidl_grantors_child;
207
208 _hidl_err = mGrantors.writeEmbeddedToParcel(
209 parcel,
210 parentHandle,
211 parentOffset + offsetof(MQDescriptor, mGrantors),
212 &_hidl_grantors_child);
213
214 if (_hidl_err != ::android::OK) { return _hidl_err; }
215
216 _hidl_err = parcel->writeEmbeddedNativeHandle(
217 mHandle,
218 parentHandle,
219 parentOffset + offsetof(MQDescriptor, mHandle));
220
221 if (_hidl_err != ::android::OK) { return _hidl_err; }
222
223 return _hidl_err;
224}
225
226template<MQFlavor flavor>
227size_t MQDescriptor<flavor>::getSize() const {
228 return mGrantors[DATAPTRPOS].extent;
229}
230
231template<MQFlavor flavor>
232size_t MQDescriptor<flavor>::getQuantum() const { return mQuantum; }
233
234template<MQFlavor flavor>
235int32_t MQDescriptor<flavor>::getFlags() const { return mFlags; }
236
237template<MQFlavor flavor>
238std::vector<GrantorDescriptor> MQDescriptor<flavor>::getGrantors() const {
239 size_t grantor_count = mGrantors.size();
240 std::vector<GrantorDescriptor> grantors(grantor_count);
241 for (size_t i = 0; i < grantor_count; i++) {
242 grantors[i] = mGrantors[i];
243 }
244 return grantors;
245}
246
247template<MQFlavor flavor>
248const sp<NativeHandle> MQDescriptor<flavor>::getNativeHandle() const {
249 /*
250 * Create an sp<NativeHandle> from mHandle.
251 */
252 return NativeHandle::create(mHandle, false /* ownsHandle */);
253}
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700254} // namespace hardware
255} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700256
257#endif // FMSGQ_DESCRIPTOR_H