blob: 6e43749d42365c94ed7571d9b1c4c9800846dab2 [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 */
Hridya Valsaraju56575782016-10-13 21:44:55 -070043 kSynchronizedReadWrite = 0x01,
44 /*
45 * kUnsynchronizedWrite represents the flavor of FMQ where writes always
46 * succeed. This flavor allows one writer and many readers. A read operation
47 * can detect an overwrite and reset the read counter.
48 */
49 kUnsynchronizedWrite = 0x02
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070050};
51
52template <MQFlavor flavor>
Andreas Huber0a451282016-08-30 11:27:24 -070053struct MQDescriptor {
54 MQDescriptor(
55 const std::vector<GrantorDescriptor>& grantors,
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070056 native_handle_t* nHandle, size_t size);
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070057
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070058 MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070059 size_t messageSize);
Andreas Huber0a451282016-08-30 11:27:24 -070060
61 ~MQDescriptor();
62
63 explicit MQDescriptor(const MQDescriptor &other);
64 MQDescriptor &operator=(const MQDescriptor &other) = delete;
65
66 size_t getSize() const;
67
68 size_t getQuantum() const;
69
70 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070071
72 ::android::status_t readEmbeddedFromParcel(
73 const ::android::hardware::Parcel &parcel,
74 size_t parentHandle,
75 size_t parentOffset);
76
77 ::android::status_t writeEmbeddedToParcel(
78 ::android::hardware::Parcel *parcel,
79 size_t parentHandle,
80 size_t parentOffset) const;
81
82 bool isHandleValid() const { return mHandle != nullptr; }
83 size_t countGrantors() const { return mGrantors.size(); }
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070084 std::vector<GrantorDescriptor> getGrantors() const;
85 const sp<NativeHandle> getNativeHandle() const;
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070086 /*
87 * There should atleast be GrantorDescriptors for the read counter, write
88 * counter and data buffer.
89 */
90 static constexpr int32_t kMinGrantorCount = 3;
91 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS };
Andreas Huber0a451282016-08-30 11:27:24 -070092private:
93 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070094 ::native_handle_t *mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -070095 uint32_t mQuantum;
96 uint32_t mFlags;
97};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070098
99/*
100 * MQDescriptorSync will describe the wait-free synchronized
101 * flavor of FMQ.
102 */
103using MQDescriptorSync = MQDescriptor<kSynchronizedReadWrite>;
104
Hridya Valsaraju56575782016-10-13 21:44:55 -0700105/*
106 * MQDescriptorUnsync will describe the unsynchronized write
107 * flavor of FMQ.
108 */
109using MQDescriptorUnsync = MQDescriptor<kUnsynchronizedWrite>;
110
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700111template<MQFlavor flavor>
112MQDescriptor<flavor>::MQDescriptor(
113 const std::vector<GrantorDescriptor>& grantors,
114 native_handle_t* nhandle,
115 size_t size)
116 : mHandle(nhandle),
117 mQuantum(size),
118 mFlags(flavor) {
119 mGrantors.resize(grantors.size());
120 for (size_t i = 0; i < grantors.size(); ++i) {
121 mGrantors[i] = grantors[i];
122 }
123}
124
125template<MQFlavor flavor>
126MQDescriptor<flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
127 size_t messageSize)
128 : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
129 mGrantors.resize(kMinGrantorCount);
130 /*
131 * Create a default grantor descriptor for read, write pointers and
132 * the data buffer. fdIndex parameter is set to 0 by default and
133 * the offset for each grantor is contiguous.
134 */
135 mGrantors[READPTRPOS] = {
136 0 /* grantor flags */, 0 /* fdIndex */, 0 /* offset */,
137 sizeof(RingBufferPosition) /* extent */
138 };
139
140 mGrantors[WRITEPTRPOS] = {
141 0 /* grantor flags */,
142 0 /* fdIndex */,
143 sizeof(RingBufferPosition) /* offset */,
144 sizeof(RingBufferPosition) /* extent */
145 };
146 mGrantors[DATAPTRPOS] = {
147 0 /* grantor flags */, 0 /* fdIndex */,
148 2 * sizeof(RingBufferPosition) /* offset */, bufferSize /* extent */
149 };
150}
151
152template<MQFlavor flavor>
153MQDescriptor<flavor>::MQDescriptor(const MQDescriptor<flavor> &other)
154 : mGrantors(other.mGrantors),
155 mHandle(nullptr),
156 mQuantum(other.mQuantum),
157 mFlags(other.mFlags) {
158 if (other.mHandle != nullptr) {
159 mHandle = native_handle_create(
160 other.mHandle->numFds, other.mHandle->numInts);
161
162 for (int i = 0; i < other.mHandle->numFds; ++i) {
163 const_cast<native_handle_t *>(mHandle)->data[i] =
164 dup(other.mHandle->data[i]);
165 }
166
167 memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds],
168 &other.mHandle->data[other.mHandle->numFds],
169 other.mHandle->numInts * sizeof(int));
170 }
171}
172
173template<MQFlavor flavor>
174MQDescriptor<flavor>::~MQDescriptor() {
175 if (mHandle != nullptr) {
176 native_handle_close(const_cast<native_handle_t *>(mHandle));
177 native_handle_delete(const_cast<native_handle_t *>(mHandle));
178 }
179}
180
181template<MQFlavor flavor>
182::android::status_t MQDescriptor<flavor>::readEmbeddedFromParcel(
183 const Parcel &parcel,
184 size_t parentHandle,
185 size_t parentOffset) {
186 ::android::status_t _hidl_err = ::android::OK;
187
188 size_t _hidl_grantors_child;
189
190 _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>(
191 &mGrantors)->readEmbeddedFromParcel(
192 parcel,
193 parentHandle,
194 parentOffset + offsetof(MQDescriptor, mGrantors),
195 &_hidl_grantors_child);
196
197 if (_hidl_err != ::android::OK) { return _hidl_err; }
198
199 const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle(
200 parentHandle,
201 parentOffset + offsetof(MQDescriptor, mHandle));
202
203 if (_hidl_mq_handle_ptr == nullptr) {
204 _hidl_err = ::android::UNKNOWN_ERROR;
205 return _hidl_err;
206 }
207
208 return _hidl_err;
209}
210
211template<MQFlavor flavor>
212::android::status_t MQDescriptor<flavor>::writeEmbeddedToParcel(
213 Parcel *parcel,
214 size_t parentHandle,
215 size_t parentOffset) const {
216 ::android::status_t _hidl_err = ::android::OK;
217
218 size_t _hidl_grantors_child;
219
220 _hidl_err = mGrantors.writeEmbeddedToParcel(
221 parcel,
222 parentHandle,
223 parentOffset + offsetof(MQDescriptor, mGrantors),
224 &_hidl_grantors_child);
225
226 if (_hidl_err != ::android::OK) { return _hidl_err; }
227
228 _hidl_err = parcel->writeEmbeddedNativeHandle(
229 mHandle,
230 parentHandle,
231 parentOffset + offsetof(MQDescriptor, mHandle));
232
233 if (_hidl_err != ::android::OK) { return _hidl_err; }
234
235 return _hidl_err;
236}
237
238template<MQFlavor flavor>
239size_t MQDescriptor<flavor>::getSize() const {
240 return mGrantors[DATAPTRPOS].extent;
241}
242
243template<MQFlavor flavor>
244size_t MQDescriptor<flavor>::getQuantum() const { return mQuantum; }
245
246template<MQFlavor flavor>
247int32_t MQDescriptor<flavor>::getFlags() const { return mFlags; }
248
249template<MQFlavor flavor>
250std::vector<GrantorDescriptor> MQDescriptor<flavor>::getGrantors() const {
251 size_t grantor_count = mGrantors.size();
252 std::vector<GrantorDescriptor> grantors(grantor_count);
253 for (size_t i = 0; i < grantor_count; i++) {
254 grantors[i] = mGrantors[i];
255 }
256 return grantors;
257}
258
259template<MQFlavor flavor>
260const sp<NativeHandle> MQDescriptor<flavor>::getNativeHandle() const {
261 /*
262 * Create an sp<NativeHandle> from mHandle.
263 */
264 return NativeHandle::create(mHandle, false /* ownsHandle */);
265}
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700266} // namespace hardware
267} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700268
269#endif // FMSGQ_DESCRIPTOR_H