blob: 38ac325eb04be6332b3ab23f36305010fb557377 [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
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080030template <typename T, MQFlavor flavor>
Andreas Huber0a451282016-08-30 11:27:24 -070031struct MQDescriptor {
Steven Moreland4cee37c2018-10-09 15:54:27 -070032 // Takes ownership of handle
Andreas Huber0a451282016-08-30 11:27:24 -070033 MQDescriptor(
34 const std::vector<GrantorDescriptor>& grantors,
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070035 native_handle_t* nHandle, size_t size);
Hridya Valsaraju62bb7a02016-09-23 10:44:04 -070036
Steven Moreland4cee37c2018-10-09 15:54:27 -070037 // Takes ownership of handle
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070038 MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080039 size_t messageSize, bool configureEventFlag = false);
Andreas Huber0a451282016-08-30 11:27:24 -070040
Hridya Valsaraju0e326a02016-12-21 10:52:29 -080041 MQDescriptor();
Andreas Huber0a451282016-08-30 11:27:24 -070042 ~MQDescriptor();
43
Steven Moreland6d503152018-10-09 15:12:20 -070044 explicit MQDescriptor(const MQDescriptor& other) : MQDescriptor() { *this = other; }
45 MQDescriptor& operator=(const MQDescriptor& other);
Andreas Huber0a451282016-08-30 11:27:24 -070046
47 size_t getSize() const;
48
49 size_t getQuantum() const;
50
51 int32_t getFlags() const;
Andreas Huber0a451282016-08-30 11:27:24 -070052
Andreas Huber0a451282016-08-30 11:27:24 -070053 bool isHandleValid() const { return mHandle != nullptr; }
54 size_t countGrantors() const { return mGrantors.size(); }
Yifan Hong089ae132016-11-11 11:32:52 -080055
56 inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const {
57 return mGrantors;
58 }
59
Yifan Hong089ae132016-11-11 11:32:52 -080060 inline const ::native_handle_t *handle() const {
61 return mHandle;
62 }
63
64 inline ::native_handle_t *handle() {
65 return mHandle;
66 }
67
68 static const size_t kOffsetOfGrantors;
69 static const size_t kOffsetOfHandle;
Hridya Valsaraju9ec57302016-12-14 09:00:31 -080070
Andreas Huber0a451282016-08-30 11:27:24 -070071private:
72 ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
Hridya Valsaraju44843442016-12-09 13:38:55 -080073 ::android::hardware::details::hidl_pointer<native_handle_t> mHandle;
Andreas Huber0a451282016-08-30 11:27:24 -070074 uint32_t mQuantum;
75 uint32_t mFlags;
76};
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070077
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080078template<typename T, MQFlavor flavor>
79const size_t MQDescriptor<T, flavor>::kOffsetOfGrantors = offsetof(MQDescriptor, mGrantors);
Yifan Hong089ae132016-11-11 11:32:52 -080080
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080081template<typename T, MQFlavor flavor>
82const size_t MQDescriptor<T, flavor>::kOffsetOfHandle = offsetof(MQDescriptor, mHandle);
Yifan Hong089ae132016-11-11 11:32:52 -080083
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070084/*
85 * MQDescriptorSync will describe the wait-free synchronized
86 * flavor of FMQ.
87 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080088template<typename T>
89using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -070090
Hridya Valsaraju56575782016-10-13 21:44:55 -070091/*
92 * MQDescriptorUnsync will describe the unsynchronized write
93 * flavor of FMQ.
94 */
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -080095template<typename T>
96using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>;
Hridya Valsaraju56575782016-10-13 21:44:55 -070097
Marin Shalamanov08cfe862020-06-29 13:45:32 +020098template <typename T, MQFlavor flavor>
99MQDescriptor<T, flavor>::MQDescriptor(const std::vector<GrantorDescriptor>& grantors,
100 native_handle_t* nhandle, size_t size)
101 : mHandle(nhandle), mQuantum(static_cast<uint32_t>(size)), mFlags(flavor) {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700102 mGrantors.resize(grantors.size());
103 for (size_t i = 0; i < grantors.size(); ++i) {
104 mGrantors[i] = grantors[i];
105 }
106}
107
Marin Shalamanov08cfe862020-06-29 13:45:32 +0200108template <typename T, MQFlavor flavor>
109MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t* nHandle,
110 size_t messageSize, bool configureEventFlag)
111 : mHandle(nHandle), mQuantum(static_cast<uint32_t>(messageSize)), mFlags(flavor) {
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800112 /*
113 * If configureEventFlag is true, allocate an additional spot in mGrantor
114 * for containing the fd and offset for mmapping the EventFlag word.
115 */
Devin Mooref9893e82020-07-08 08:17:39 -0700116 mGrantors.resize(configureEventFlag ? details::kMinGrantorCountForEvFlagSupport
117 : details::kMinGrantorCount);
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800118
119 size_t memSize[] = {
Devin Mooref9893e82020-07-08 08:17:39 -0700120 sizeof(details::RingBufferPosition), /* memory to be allocated for read pointer counter
121 */
122 sizeof(details::RingBufferPosition), /* memory to be allocated for write pointer counter
123 */
124 bufferSize, /* memory to be allocated for data buffer */
125 sizeof(std::atomic<uint32_t>) /* memory to be allocated for EventFlag word */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800126 };
127
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700128 /*
129 * Create a default grantor descriptor for read, write pointers and
130 * the data buffer. fdIndex parameter is set to 0 by default and
131 * the offset for each grantor is contiguous.
132 */
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800133 for (size_t grantorPos = 0, offset = 0;
134 grantorPos < mGrantors.size();
135 offset += memSize[grantorPos++]) {
Devin Mooref9893e82020-07-08 08:17:39 -0700136 mGrantors[grantorPos] = {0 /* grantor flags */, 0 /* fdIndex */,
137 static_cast<uint32_t>(details::alignToWordBoundary(offset)),
138 memSize[grantorPos]};
Hridya Valsaraju9ec57302016-12-14 09:00:31 -0800139 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700140}
141
Steven Moreland6d503152018-10-09 15:12:20 -0700142template <typename T, MQFlavor flavor>
143MQDescriptor<T, flavor>& MQDescriptor<T, flavor>::operator=(const MQDescriptor& other) {
144 mGrantors = other.mGrantors;
145 if (mHandle != nullptr) {
146 native_handle_close(mHandle);
147 native_handle_delete(mHandle);
148 mHandle = nullptr;
149 }
150 mQuantum = other.mQuantum;
151 mFlags = other.mFlags;
152
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700153 if (other.mHandle != nullptr) {
154 mHandle = native_handle_create(
155 other.mHandle->numFds, other.mHandle->numInts);
156
157 for (int i = 0; i < other.mHandle->numFds; ++i) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800158 mHandle->data[i] = dup(other.mHandle->data[i]);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700159 }
160
Marin Shalamanova9bcfba2020-06-19 16:05:17 +0200161 memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds],
162 static_cast<size_t>(other.mHandle->numInts) * sizeof(int));
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700163 }
Steven Moreland6d503152018-10-09 15:12:20 -0700164
165 return *this;
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700166}
167
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800168template<typename T, MQFlavor flavor>
169MQDescriptor<T, flavor>::MQDescriptor() : MQDescriptor(
Hridya Valsaraju0e326a02016-12-21 10:52:29 -0800170 std::vector<android::hardware::GrantorDescriptor>(),
171 nullptr /* nHandle */,
172 0 /* size */) {}
173
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800174template<typename T, MQFlavor flavor>
175MQDescriptor<T, flavor>::~MQDescriptor() {
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700176 if (mHandle != nullptr) {
Hridya Valsaraju44843442016-12-09 13:38:55 -0800177 native_handle_close(mHandle);
178 native_handle_delete(mHandle);
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700179 }
180}
181
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800182template<typename T, MQFlavor flavor>
183size_t MQDescriptor<T, flavor>::getSize() const {
Devin Moore2b686fc2022-06-01 23:47:43 +0000184 if (mGrantors.size() > details::DATAPTRPOS) {
185 return static_cast<size_t>(mGrantors[details::DATAPTRPOS].extent);
186 } else {
187 return 0;
188 }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700189}
190
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800191template<typename T, MQFlavor flavor>
192size_t MQDescriptor<T, flavor>::getQuantum() const { return mQuantum; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700193
Hridya Valsaraju7e6404d2016-12-27 09:13:34 -0800194template<typename T, MQFlavor flavor>
195int32_t MQDescriptor<T, flavor>::getFlags() const { return mFlags; }
Hridya Valsaraju24fb4e22016-09-26 10:37:01 -0700196
Yifan Hongbe7a6882017-01-05 17:30:17 -0800197template<typename T, MQFlavor flavor>
198std::string toString(const MQDescriptor<T, flavor> &q) {
199 std::string os;
200 if (flavor & kSynchronizedReadWrite) {
201 os += "fmq_sync";
202 }
203 if (flavor & kUnsynchronizedWrite) {
204 os += "fmq_unsync";
205 }
206 os += " {"
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800207 + toString(q.grantors().size()) + " grantor(s), "
Yifan Hongbe7a6882017-01-05 17:30:17 -0800208 + "size = " + toString(q.getSize())
Hridya Valsarajued4e7142017-02-24 11:00:48 -0800209 + ", .handle = " + toString(q.handle())
Yifan Hongbe7a6882017-01-05 17:30:17 -0800210 + ", .quantum = " + toString(q.getQuantum()) + "}";
211 return os;
212}
Yifan Hongbe7a6882017-01-05 17:30:17 -0800213
Hridya Valsaraju4fe70f02016-09-20 21:02:51 -0700214} // namespace hardware
215} // namespace android
Andreas Huber0a451282016-08-30 11:27:24 -0700216
217#endif // FMSGQ_DESCRIPTOR_H