blob: ed9279ab05fc4d8f3797209579d3053f1c852c21 [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
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080052template <typename T, 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 Valsaraju9ec57302016-12-14 09:00:31 -080059 size_t messageSize, bool configureEventFlag = false);
Andreas Huber0a451282016-08-30 11:27:24 -070060
Hridya Valsaraju0e326a02016-12-21 10:52:29 -080061 MQDescriptor();
Andreas Huber0a451282016-08-30 11:27:24 -070062 ~MQDescriptor();
63
64 explicit MQDescriptor(const MQDescriptor &other);
65 MQDescriptor &operator=(const MQDescriptor &other) = delete;
66
67 size_t getSize() const;
68
69 size_t getQuantum() const;
70
71 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070072
Andreas Huber0a451282016-08-30 11:27:24 -070073 bool isHandleValid() const { return mHandle != nullptr; }
74 size_t countGrantors() const { return mGrantors.size(); }
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -070075 std::vector<GrantorDescriptor> getGrantors() const;
76 const sp<NativeHandle> getNativeHandle() const;
Yifan Hong089ae132016-11-11 11:32:52 -080077
78 inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const {
79 return mGrantors;
80 }
81
82 inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() {
83 return mGrantors;
84 }
85
86 inline const ::native_handle_t *handle() const {
87 return mHandle;
88 }
89
90 inline ::native_handle_t *handle() {
91 return mHandle;
92 }
93
94 static const size_t kOffsetOfGrantors;
95 static const size_t kOffsetOfHandle;
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080096 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS };
97
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070098 /*
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080099 * There should at least be GrantorDescriptors for the read counter, write
100 * counter and data buffer. A GrantorDescriptor for an EventFlag word is
101 * not required if there is no need for blocking FMQ operations.
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -0700102 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800103 static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1;
104
105 /*
106 * Minimum number of GrantorDescriptors required if EventFlag support is
107 * needed for blocking FMQ operations.
108 */
109 static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
Andreas Huber0a451282016-08-30 11:27:24 -0700110private:
111 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju44843442016-12-09 13:38:55 -0800112 ::android::hardware::details::hidl_pointer<native_handle_t> mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -0700113 uint32_t mQuantum;
114 uint32_t mFlags;
115};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700116
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800117template<typename T, MQFlavor flavor>
118const size_t MQDescriptor<T, flavor>::kOffsetOfGrantors = offsetof(MQDescriptor, mGrantors);
Yifan Hong089ae132016-11-11 11:32:52 -0800119
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800120template<typename T, MQFlavor flavor>
121const size_t MQDescriptor<T, flavor>::kOffsetOfHandle = offsetof(MQDescriptor, mHandle);
Yifan Hong089ae132016-11-11 11:32:52 -0800122
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700123/*
124 * MQDescriptorSync will describe the wait-free synchronized
125 * flavor of FMQ.
126 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800127template<typename T>
128using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700129
Hridya Valsaraju56575782016-10-13 21:44:55 -0700130/*
131 * MQDescriptorUnsync will describe the unsynchronized write
132 * flavor of FMQ.
133 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800134template<typename T>
135using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>;
Hridya Valsaraju56575782016-10-13 21:44:55 -0700136
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800137template<typename T, MQFlavor flavor>
138MQDescriptor<T, flavor>::MQDescriptor(
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700139 const std::vector<GrantorDescriptor>& grantors,
140 native_handle_t* nhandle,
141 size_t size)
142 : mHandle(nhandle),
143 mQuantum(size),
144 mFlags(flavor) {
145 mGrantors.resize(grantors.size());
146 for (size_t i = 0; i < grantors.size(); ++i) {
147 mGrantors[i] = grantors[i];
148 }
149}
150
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800151template<typename T, MQFlavor flavor>
152MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle,
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800153 size_t messageSize, bool configureEventFlag)
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700154 : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) {
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800155 /*
156 * If configureEventFlag is true, allocate an additional spot in mGrantor
157 * for containing the fd and offset for mmapping the EventFlag word.
158 */
159 mGrantors.resize(configureEventFlag? kMinGrantorCountForEvFlagSupport : kMinGrantorCount);
160
161 size_t memSize[] = {
162 sizeof(RingBufferPosition), /* memory to be allocated for read pointer counter */
163 sizeof(RingBufferPosition), /* memory to be allocated for write pointer counter */
164 bufferSize, /* memory to be allocated for data buffer */
165 sizeof(std::atomic<uint32_t>)/* memory to be allocated for EventFlag word */
166 };
167
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700168 /*
169 * Create a default grantor descriptor for read, write pointers and
170 * the data buffer. fdIndex parameter is set to 0 by default and
171 * the offset for each grantor is contiguous.
172 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800173 for (size_t grantorPos = 0, offset = 0;
174 grantorPos < mGrantors.size();
175 offset += memSize[grantorPos++]) {
176 mGrantors[grantorPos] = {
177 0 /* grantor flags */,
178 0 /* fdIndex */,
179 static_cast<uint32_t>(offset),
180 memSize[grantorPos]
181 };
182 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700183}
184
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800185template<typename T, MQFlavor flavor>
186MQDescriptor<T, flavor>::MQDescriptor(const MQDescriptor<T, flavor> &other)
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700187 : mGrantors(other.mGrantors),
188 mHandle(nullptr),
189 mQuantum(other.mQuantum),
190 mFlags(other.mFlags) {
191 if (other.mHandle != nullptr) {
192 mHandle = native_handle_create(
193 other.mHandle->numFds, other.mHandle->numInts);
194
195 for (int i = 0; i < other.mHandle->numFds; ++i) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800196 mHandle->data[i] = dup(other.mHandle->data[i]);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700197 }
198
Hridya Valsaraju44843442016-12-09 13:38:55 -0800199 memcpy(&mHandle->data[other.mHandle->numFds],
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700200 &other.mHandle->data[other.mHandle->numFds],
201 other.mHandle->numInts * sizeof(int));
202 }
203}
204
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800205template<typename T, MQFlavor flavor>
206MQDescriptor<T, flavor>::MQDescriptor() : MQDescriptor(
Hridya Valsaraju0e326a02016-12-21 10:52:29 -0800207 std::vector<android::hardware::GrantorDescriptor>(),
208 nullptr /* nHandle */,
209 0 /* size */) {}
210
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800211template<typename T, MQFlavor flavor>
212MQDescriptor<T, flavor>::~MQDescriptor() {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700213 if (mHandle != nullptr) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800214 native_handle_close(mHandle);
215 native_handle_delete(mHandle);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700216 }
217}
218
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800219template<typename T, MQFlavor flavor>
220size_t MQDescriptor<T, flavor>::getSize() const {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700221 return mGrantors[DATAPTRPOS].extent;
222}
223
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800224template<typename T, MQFlavor flavor>
225size_t MQDescriptor<T, flavor>::getQuantum() const { return mQuantum; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700226
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800227template<typename T, MQFlavor flavor>
228int32_t MQDescriptor<T, flavor>::getFlags() const { return mFlags; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700229
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800230template<typename T, MQFlavor flavor>
231std::vector<GrantorDescriptor> MQDescriptor<T, flavor>::getGrantors() const {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700232 size_t grantor_count = mGrantors.size();
233 std::vector<GrantorDescriptor> grantors(grantor_count);
234 for (size_t i = 0; i < grantor_count; i++) {
235 grantors[i] = mGrantors[i];
236 }
237 return grantors;
238}
239
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800240template<typename T, MQFlavor flavor>
241const sp<NativeHandle> MQDescriptor<T, flavor>::getNativeHandle() const {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700242 /*
243 * Create an sp<NativeHandle> from mHandle.
244 */
245 return NativeHandle::create(mHandle, false /* ownsHandle */);
246}
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700247} // namespace hardware
248} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700249
250#endif // FMSGQ_DESCRIPTOR_H