blob: 6a0bc25771d01af6842b44cc175c45eba3fe55e3 [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
Steven Moreland141a4d32017-04-28 17:05:50 -070020#include <unistd.h>
21
Andreas Huber0a451282016-08-30 11:27:24 -070022#include <cutils/native_handle.h>
Devin Mooref9893e82020-07-08 08:17:39 -070023#include <fmq/MQDescriptorBase.h>
Hridya Valsarajub7370302017-03-08 14:39:23 -080024#include <hidl/HidlInternal.h>
Andreas Huber0a451282016-08-30 11:27:24 -070025#include <hidl/HidlSupport.h>
26
27namespace android {
28namespace hardware {
29
30struct GrantorDescriptor {
Hridya Valsaraju45eb91d2017-01-12 13:20:35 -080031 uint32_t flags __attribute__ ((aligned(4)));
32 uint32_t fdIndex __attribute__ ((aligned(4)));
33 uint32_t offset __attribute__ ((aligned(4)));
34 uint64_t extent __attribute__ ((aligned(8)));
Andreas Huber0a451282016-08-30 11:27:24 -070035};
36
Hridya Valsaraju45eb91d2017-01-12 13:20:35 -080037static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset");
38static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset");
39static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset");
40static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset");
41static_assert(sizeof(GrantorDescriptor) == 24, "wrong size");
42static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment");
43
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080044template <typename T, MQFlavor flavor>
Andreas Huber0a451282016-08-30 11:27:24 -070045struct MQDescriptor {
Steven Moreland4cee37c2018-10-09 15:54:27 -070046 // Takes ownership of handle
Andreas Huber0a451282016-08-30 11:27:24 -070047 MQDescriptor(
48 const std::vector<GrantorDescriptor>& grantors,
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070049 native_handle_t* nHandle, size_t size);
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070050
Steven Moreland4cee37c2018-10-09 15:54:27 -070051 // Takes ownership of handle
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070052 MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080053 size_t messageSize, bool configureEventFlag = false);
Andreas Huber0a451282016-08-30 11:27:24 -070054
Hridya Valsaraju0e326a02016-12-21 10:52:29 -080055 MQDescriptor();
Andreas Huber0a451282016-08-30 11:27:24 -070056 ~MQDescriptor();
57
Steven Moreland6d503152018-10-09 15:12:20 -070058 explicit MQDescriptor(const MQDescriptor& other) : MQDescriptor() { *this = other; }
59 MQDescriptor& operator=(const MQDescriptor& other);
Andreas Huber0a451282016-08-30 11:27:24 -070060
61 size_t getSize() const;
62
63 size_t getQuantum() const;
64
65 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070066
Andreas Huber0a451282016-08-30 11:27:24 -070067 bool isHandleValid() const { return mHandle != nullptr; }
68 size_t countGrantors() const { return mGrantors.size(); }
Yifan Hong089ae132016-11-11 11:32:52 -080069
70 inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const {
71 return mGrantors;
72 }
73
Devin Mooref9893e82020-07-08 08:17:39 -070074 // This should be removed if no one is using it. We shouldn't be returning
75 // a mutable reference if it's not necessary. TODO(b/162465295).
Yifan Hong089ae132016-11-11 11:32:52 -080076 inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() {
77 return mGrantors;
78 }
79
80 inline const ::native_handle_t *handle() const {
81 return mHandle;
82 }
83
84 inline ::native_handle_t *handle() {
85 return mHandle;
86 }
87
88 static const size_t kOffsetOfGrantors;
89 static const size_t kOffsetOfHandle;
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080090
Andreas Huber0a451282016-08-30 11:27:24 -070091private:
92 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju44843442016-12-09 13:38:55 -080093 ::android::hardware::details::hidl_pointer<native_handle_t> mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -070094 uint32_t mQuantum;
95 uint32_t mFlags;
96};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070097
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080098template<typename T, MQFlavor flavor>
99const size_t MQDescriptor<T, flavor>::kOffsetOfGrantors = offsetof(MQDescriptor, mGrantors);
Yifan Hong089ae132016-11-11 11:32:52 -0800100
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800101template<typename T, MQFlavor flavor>
102const size_t MQDescriptor<T, flavor>::kOffsetOfHandle = offsetof(MQDescriptor, mHandle);
Yifan Hong089ae132016-11-11 11:32:52 -0800103
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700104/*
105 * MQDescriptorSync will describe the wait-free synchronized
106 * flavor of FMQ.
107 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800108template<typename T>
109using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700110
Hridya Valsaraju56575782016-10-13 21:44:55 -0700111/*
112 * MQDescriptorUnsync will describe the unsynchronized write
113 * flavor of FMQ.
114 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800115template<typename T>
116using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>;
Hridya Valsaraju56575782016-10-13 21:44:55 -0700117
Marin Shalamanov08cfe862020-06-29 13:45:32 +0200118template <typename T, MQFlavor flavor>
119MQDescriptor<T, flavor>::MQDescriptor(const std::vector<GrantorDescriptor>& grantors,
120 native_handle_t* nhandle, size_t size)
121 : mHandle(nhandle), mQuantum(static_cast<uint32_t>(size)), mFlags(flavor) {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700122 mGrantors.resize(grantors.size());
123 for (size_t i = 0; i < grantors.size(); ++i) {
124 mGrantors[i] = grantors[i];
125 }
126}
127
Marin Shalamanov08cfe862020-06-29 13:45:32 +0200128template <typename T, MQFlavor flavor>
129MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
130 size_t messageSize, bool configureEventFlag)
131 : mHandle(nHandle), mQuantum(static_cast<uint32_t>(messageSize)), mFlags(flavor) {
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800132 /*
133 * If configureEventFlag is true, allocate an additional spot in mGrantor
134 * for containing the fd and offset for mmapping the EventFlag word.
135 */
Devin Mooref9893e82020-07-08 08:17:39 -0700136 mGrantors.resize(configureEventFlag ? details::kMinGrantorCountForEvFlagSupport
137 : details::kMinGrantorCount);
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800138
139 size_t memSize[] = {
Devin Mooref9893e82020-07-08 08:17:39 -0700140 sizeof(details::RingBufferPosition), /* memory to be allocated for read pointer counter
141 */
142 sizeof(details::RingBufferPosition), /* memory to be allocated for write pointer counter
143 */
144 bufferSize, /* memory to be allocated for data buffer */
145 sizeof(std::atomic<uint32_t>) /* memory to be allocated for EventFlag word */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800146 };
147
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700148 /*
149 * Create a default grantor descriptor for read, write pointers and
150 * the data buffer. fdIndex parameter is set to 0 by default and
151 * the offset for each grantor is contiguous.
152 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800153 for (size_t grantorPos = 0, offset = 0;
154 grantorPos < mGrantors.size();
155 offset += memSize[grantorPos++]) {
Devin Mooref9893e82020-07-08 08:17:39 -0700156 mGrantors[grantorPos] = {0 /* grantor flags */, 0 /* fdIndex */,
157 static_cast<uint32_t>(details::alignToWordBoundary(offset)),
158 memSize[grantorPos]};
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800159 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700160}
161
Steven Moreland6d503152018-10-09 15:12:20 -0700162template <typename T, MQFlavor flavor>
163MQDescriptor<T, flavor>& MQDescriptor<T, flavor>::operator=(const MQDescriptor& other) {
164 mGrantors = other.mGrantors;
165 if (mHandle != nullptr) {
166 native_handle_close(mHandle);
167 native_handle_delete(mHandle);
168 mHandle = nullptr;
169 }
170 mQuantum = other.mQuantum;
171 mFlags = other.mFlags;
172
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700173 if (other.mHandle != nullptr) {
174 mHandle = native_handle_create(
175 other.mHandle->numFds, other.mHandle->numInts);
176
177 for (int i = 0; i < other.mHandle->numFds; ++i) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800178 mHandle->data[i] = dup(other.mHandle->data[i]);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700179 }
180
Marin Shalamanova9bcfba2020-06-19 16:05:17 +0200181 memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds],
182 static_cast<size_t>(other.mHandle->numInts) * sizeof(int));
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700183 }
Steven Moreland6d503152018-10-09 15:12:20 -0700184
185 return *this;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700186}
187
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800188template<typename T, MQFlavor flavor>
189MQDescriptor<T, flavor>::MQDescriptor() : MQDescriptor(
Hridya Valsaraju0e326a02016-12-21 10:52:29 -0800190 std::vector<android::hardware::GrantorDescriptor>(),
191 nullptr /* nHandle */,
192 0 /* size */) {}
193
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800194template<typename T, MQFlavor flavor>
195MQDescriptor<T, flavor>::~MQDescriptor() {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700196 if (mHandle != nullptr) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800197 native_handle_close(mHandle);
198 native_handle_delete(mHandle);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700199 }
200}
201
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800202template<typename T, MQFlavor flavor>
203size_t MQDescriptor<T, flavor>::getSize() const {
Devin Mooref9893e82020-07-08 08:17:39 -0700204 return static_cast<size_t>(mGrantors[details::DATAPTRPOS].extent);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700205}
206
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800207template<typename T, MQFlavor flavor>
208size_t MQDescriptor<T, flavor>::getQuantum() const { return mQuantum; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700209
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800210template<typename T, MQFlavor flavor>
211int32_t MQDescriptor<T, flavor>::getFlags() const { return mFlags; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700212
Yifan Hongbe7a6882017-01-05 17:30:17 -0800213template<typename T, MQFlavor flavor>
214std::string toString(const MQDescriptor<T, flavor> &q) {
215 std::string os;
216 if (flavor & kSynchronizedReadWrite) {
217 os += "fmq_sync";
218 }
219 if (flavor & kUnsynchronizedWrite) {
220 os += "fmq_unsync";
221 }
222 os += " {"
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800223 + toString(q.grantors().size()) + " grantor(s), "
Yifan Hongbe7a6882017-01-05 17:30:17 -0800224 + "size = " + toString(q.getSize())
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800225 + ", .handle = " + toString(q.handle())
Yifan Hongbe7a6882017-01-05 17:30:17 -0800226 + ", .quantum = " + toString(q.getQuantum()) + "}";
227 return os;
228}
Yifan Hongbe7a6882017-01-05 17:30:17 -0800229
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700230} // namespace hardware
231} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700232
233#endif // FMSGQ_DESCRIPTOR_H