Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 141a4d3 | 2017-04-28 17:05:50 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
Devin Moore | f9893e8 | 2020-07-08 08:17:39 -0700 | [diff] [blame] | 23 | #include <fmq/MQDescriptorBase.h> |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 24 | #include <hidl/HidlInternal.h> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 25 | #include <hidl/HidlSupport.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 30 | template <typename T, MQFlavor flavor> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 31 | struct MQDescriptor { |
Steven Moreland | 4cee37c | 2018-10-09 15:54:27 -0700 | [diff] [blame] | 32 | // Takes ownership of handle |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 33 | MQDescriptor( |
| 34 | const std::vector<GrantorDescriptor>& grantors, |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 35 | native_handle_t* nHandle, size_t size); |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 36 | |
Steven Moreland | 4cee37c | 2018-10-09 15:54:27 -0700 | [diff] [blame] | 37 | // Takes ownership of handle |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 38 | MQDescriptor(size_t bufferSize, native_handle_t* nHandle, |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 39 | size_t messageSize, bool configureEventFlag = false); |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 40 | |
Hridya Valsaraju | 0e326a0 | 2016-12-21 10:52:29 -0800 | [diff] [blame] | 41 | MQDescriptor(); |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 42 | ~MQDescriptor(); |
| 43 | |
Steven Moreland | 6d50315 | 2018-10-09 15:12:20 -0700 | [diff] [blame] | 44 | explicit MQDescriptor(const MQDescriptor& other) : MQDescriptor() { *this = other; } |
| 45 | MQDescriptor& operator=(const MQDescriptor& other); |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 46 | |
| 47 | size_t getSize() const; |
| 48 | |
| 49 | size_t getQuantum() const; |
| 50 | |
| 51 | int32_t getFlags() const; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 52 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 53 | bool isHandleValid() const { return mHandle != nullptr; } |
| 54 | size_t countGrantors() const { return mGrantors.size(); } |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 55 | |
| 56 | inline const ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() const { |
| 57 | return mGrantors; |
| 58 | } |
| 59 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 60 | 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 Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 70 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 71 | private: |
| 72 | ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors; |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 73 | ::android::hardware::details::hidl_pointer<native_handle_t> mHandle; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 74 | uint32_t mQuantum; |
| 75 | uint32_t mFlags; |
| 76 | }; |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 77 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 78 | template<typename T, MQFlavor flavor> |
| 79 | const size_t MQDescriptor<T, flavor>::kOffsetOfGrantors = offsetof(MQDescriptor, mGrantors); |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 80 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 81 | template<typename T, MQFlavor flavor> |
| 82 | const size_t MQDescriptor<T, flavor>::kOffsetOfHandle = offsetof(MQDescriptor, mHandle); |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 83 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 84 | /* |
| 85 | * MQDescriptorSync will describe the wait-free synchronized |
| 86 | * flavor of FMQ. |
| 87 | */ |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 88 | template<typename T> |
| 89 | using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>; |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 90 | |
Hridya Valsaraju | 5657578 | 2016-10-13 21:44:55 -0700 | [diff] [blame] | 91 | /* |
| 92 | * MQDescriptorUnsync will describe the unsynchronized write |
| 93 | * flavor of FMQ. |
| 94 | */ |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 95 | template<typename T> |
| 96 | using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>; |
Hridya Valsaraju | 5657578 | 2016-10-13 21:44:55 -0700 | [diff] [blame] | 97 | |
Marin Shalamanov | 08cfe86 | 2020-06-29 13:45:32 +0200 | [diff] [blame] | 98 | template <typename T, MQFlavor flavor> |
| 99 | MQDescriptor<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 Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 102 | mGrantors.resize(grantors.size()); |
| 103 | for (size_t i = 0; i < grantors.size(); ++i) { |
| 104 | mGrantors[i] = grantors[i]; |
| 105 | } |
| 106 | } |
| 107 | |
Marin Shalamanov | 08cfe86 | 2020-06-29 13:45:32 +0200 | [diff] [blame] | 108 | template <typename T, MQFlavor flavor> |
| 109 | MQDescriptor<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 Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 112 | /* |
| 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 Moore | f9893e8 | 2020-07-08 08:17:39 -0700 | [diff] [blame] | 116 | mGrantors.resize(configureEventFlag ? details::kMinGrantorCountForEvFlagSupport |
| 117 | : details::kMinGrantorCount); |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 118 | |
| 119 | size_t memSize[] = { |
Devin Moore | f9893e8 | 2020-07-08 08:17:39 -0700 | [diff] [blame] | 120 | 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 Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 128 | /* |
| 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 Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 133 | for (size_t grantorPos = 0, offset = 0; |
| 134 | grantorPos < mGrantors.size(); |
| 135 | offset += memSize[grantorPos++]) { |
Devin Moore | f9893e8 | 2020-07-08 08:17:39 -0700 | [diff] [blame] | 136 | mGrantors[grantorPos] = {0 /* grantor flags */, 0 /* fdIndex */, |
| 137 | static_cast<uint32_t>(details::alignToWordBoundary(offset)), |
| 138 | memSize[grantorPos]}; |
Hridya Valsaraju | 9ec5730 | 2016-12-14 09:00:31 -0800 | [diff] [blame] | 139 | } |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Steven Moreland | 6d50315 | 2018-10-09 15:12:20 -0700 | [diff] [blame] | 142 | template <typename T, MQFlavor flavor> |
| 143 | MQDescriptor<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 Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 153 | 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 Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 158 | mHandle->data[i] = dup(other.mHandle->data[i]); |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Marin Shalamanov | a9bcfba | 2020-06-19 16:05:17 +0200 | [diff] [blame] | 161 | memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds], |
| 162 | static_cast<size_t>(other.mHandle->numInts) * sizeof(int)); |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 163 | } |
Steven Moreland | 6d50315 | 2018-10-09 15:12:20 -0700 | [diff] [blame] | 164 | |
| 165 | return *this; |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 168 | template<typename T, MQFlavor flavor> |
| 169 | MQDescriptor<T, flavor>::MQDescriptor() : MQDescriptor( |
Hridya Valsaraju | 0e326a0 | 2016-12-21 10:52:29 -0800 | [diff] [blame] | 170 | std::vector<android::hardware::GrantorDescriptor>(), |
| 171 | nullptr /* nHandle */, |
| 172 | 0 /* size */) {} |
| 173 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 174 | template<typename T, MQFlavor flavor> |
| 175 | MQDescriptor<T, flavor>::~MQDescriptor() { |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 176 | if (mHandle != nullptr) { |
Hridya Valsaraju | 4484344 | 2016-12-09 13:38:55 -0800 | [diff] [blame] | 177 | native_handle_close(mHandle); |
| 178 | native_handle_delete(mHandle); |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 182 | template<typename T, MQFlavor flavor> |
| 183 | size_t MQDescriptor<T, flavor>::getSize() const { |
Devin Moore | 2b686fc | 2022-06-01 23:47:43 +0000 | [diff] [blame^] | 184 | if (mGrantors.size() > details::DATAPTRPOS) { |
| 185 | return static_cast<size_t>(mGrantors[details::DATAPTRPOS].extent); |
| 186 | } else { |
| 187 | return 0; |
| 188 | } |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 191 | template<typename T, MQFlavor flavor> |
| 192 | size_t MQDescriptor<T, flavor>::getQuantum() const { return mQuantum; } |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 193 | |
Hridya Valsaraju | 7e6404d | 2016-12-27 09:13:34 -0800 | [diff] [blame] | 194 | template<typename T, MQFlavor flavor> |
| 195 | int32_t MQDescriptor<T, flavor>::getFlags() const { return mFlags; } |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame] | 196 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 197 | template<typename T, MQFlavor flavor> |
| 198 | std::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 Valsaraju | ed4e714 | 2017-02-24 11:00:48 -0800 | [diff] [blame] | 207 | + toString(q.grantors().size()) + " grantor(s), " |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 208 | + "size = " + toString(q.getSize()) |
Hridya Valsaraju | ed4e714 | 2017-02-24 11:00:48 -0800 | [diff] [blame] | 209 | + ", .handle = " + toString(q.handle()) |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 210 | + ", .quantum = " + toString(q.getQuantum()) + "}"; |
| 211 | return os; |
| 212 | } |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 213 | |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 214 | } // namespace hardware |
| 215 | } // namespace android |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 216 | |
| 217 | #endif // FMSGQ_DESCRIPTOR_H |