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 | |
| 20 | #include <android-base/macros.h> |
| 21 | #include <cutils/native_handle.h> |
| 22 | #include <hidl/HidlSupport.h> |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 23 | #include <utils/NativeHandle.h> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 28 | typedef uint64_t RingBufferPosition; |
| 29 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 30 | struct GrantorDescriptor { |
| 31 | uint32_t flags; |
| 32 | uint32_t fdIndex; |
| 33 | uint32_t offset; |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 34 | size_t extent; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame^] | 37 | enum 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 | */ |
| 43 | kSynchronizedReadWrite = 0x01 |
| 44 | }; |
| 45 | |
| 46 | template <MQFlavor flavor> |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 47 | struct MQDescriptor { |
| 48 | MQDescriptor( |
| 49 | const std::vector<GrantorDescriptor>& grantors, |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame^] | 50 | native_handle_t* nHandle, size_t size); |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 51 | |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame^] | 52 | MQDescriptor(size_t bufferSize, native_handle_t* nHandle, |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 53 | size_t messageSize); |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 54 | |
| 55 | ~MQDescriptor(); |
| 56 | |
| 57 | explicit MQDescriptor(const MQDescriptor &other); |
| 58 | MQDescriptor &operator=(const MQDescriptor &other) = delete; |
| 59 | |
| 60 | size_t getSize() const; |
| 61 | |
| 62 | size_t getQuantum() const; |
| 63 | |
| 64 | int32_t getFlags() const; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 65 | |
| 66 | ::android::status_t readEmbeddedFromParcel( |
| 67 | const ::android::hardware::Parcel &parcel, |
| 68 | size_t parentHandle, |
| 69 | size_t parentOffset); |
| 70 | |
| 71 | ::android::status_t writeEmbeddedToParcel( |
| 72 | ::android::hardware::Parcel *parcel, |
| 73 | size_t parentHandle, |
| 74 | size_t parentOffset) const; |
| 75 | |
| 76 | bool isHandleValid() const { return mHandle != nullptr; } |
| 77 | size_t countGrantors() const { return mGrantors.size(); } |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 78 | std::vector<GrantorDescriptor> getGrantors() const; |
| 79 | const sp<NativeHandle> getNativeHandle() const; |
Hridya Valsaraju | 62bb7a0 | 2016-09-23 10:44:04 -0700 | [diff] [blame] | 80 | /* |
| 81 | * There should atleast be GrantorDescriptors for the read counter, write |
| 82 | * counter and data buffer. |
| 83 | */ |
| 84 | static constexpr int32_t kMinGrantorCount = 3; |
| 85 | enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS }; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 86 | private: |
| 87 | ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors; |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 88 | ::native_handle_t *mHandle; |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 89 | uint32_t mQuantum; |
| 90 | uint32_t mFlags; |
| 91 | }; |
Hridya Valsaraju | 24fb4e2 | 2016-09-26 10:37:01 -0700 | [diff] [blame^] | 92 | |
| 93 | /* |
| 94 | * MQDescriptorSync will describe the wait-free synchronized |
| 95 | * flavor of FMQ. |
| 96 | */ |
| 97 | using MQDescriptorSync = MQDescriptor<kSynchronizedReadWrite>; |
| 98 | |
| 99 | template<MQFlavor flavor> |
| 100 | MQDescriptor<flavor>::MQDescriptor( |
| 101 | const std::vector<GrantorDescriptor>& grantors, |
| 102 | native_handle_t* nhandle, |
| 103 | size_t size) |
| 104 | : mHandle(nhandle), |
| 105 | mQuantum(size), |
| 106 | mFlags(flavor) { |
| 107 | mGrantors.resize(grantors.size()); |
| 108 | for (size_t i = 0; i < grantors.size(); ++i) { |
| 109 | mGrantors[i] = grantors[i]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | template<MQFlavor flavor> |
| 114 | MQDescriptor<flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle, |
| 115 | size_t messageSize) |
| 116 | : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) { |
| 117 | mGrantors.resize(kMinGrantorCount); |
| 118 | /* |
| 119 | * Create a default grantor descriptor for read, write pointers and |
| 120 | * the data buffer. fdIndex parameter is set to 0 by default and |
| 121 | * the offset for each grantor is contiguous. |
| 122 | */ |
| 123 | mGrantors[READPTRPOS] = { |
| 124 | 0 /* grantor flags */, 0 /* fdIndex */, 0 /* offset */, |
| 125 | sizeof(RingBufferPosition) /* extent */ |
| 126 | }; |
| 127 | |
| 128 | mGrantors[WRITEPTRPOS] = { |
| 129 | 0 /* grantor flags */, |
| 130 | 0 /* fdIndex */, |
| 131 | sizeof(RingBufferPosition) /* offset */, |
| 132 | sizeof(RingBufferPosition) /* extent */ |
| 133 | }; |
| 134 | mGrantors[DATAPTRPOS] = { |
| 135 | 0 /* grantor flags */, 0 /* fdIndex */, |
| 136 | 2 * sizeof(RingBufferPosition) /* offset */, bufferSize /* extent */ |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | template<MQFlavor flavor> |
| 141 | MQDescriptor<flavor>::MQDescriptor(const MQDescriptor<flavor> &other) |
| 142 | : mGrantors(other.mGrantors), |
| 143 | mHandle(nullptr), |
| 144 | mQuantum(other.mQuantum), |
| 145 | mFlags(other.mFlags) { |
| 146 | if (other.mHandle != nullptr) { |
| 147 | mHandle = native_handle_create( |
| 148 | other.mHandle->numFds, other.mHandle->numInts); |
| 149 | |
| 150 | for (int i = 0; i < other.mHandle->numFds; ++i) { |
| 151 | const_cast<native_handle_t *>(mHandle)->data[i] = |
| 152 | dup(other.mHandle->data[i]); |
| 153 | } |
| 154 | |
| 155 | memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds], |
| 156 | &other.mHandle->data[other.mHandle->numFds], |
| 157 | other.mHandle->numInts * sizeof(int)); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | template<MQFlavor flavor> |
| 162 | MQDescriptor<flavor>::~MQDescriptor() { |
| 163 | if (mHandle != nullptr) { |
| 164 | native_handle_close(const_cast<native_handle_t *>(mHandle)); |
| 165 | native_handle_delete(const_cast<native_handle_t *>(mHandle)); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | template<MQFlavor flavor> |
| 170 | ::android::status_t MQDescriptor<flavor>::readEmbeddedFromParcel( |
| 171 | const Parcel &parcel, |
| 172 | size_t parentHandle, |
| 173 | size_t parentOffset) { |
| 174 | ::android::status_t _hidl_err = ::android::OK; |
| 175 | |
| 176 | size_t _hidl_grantors_child; |
| 177 | |
| 178 | _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>( |
| 179 | &mGrantors)->readEmbeddedFromParcel( |
| 180 | parcel, |
| 181 | parentHandle, |
| 182 | parentOffset + offsetof(MQDescriptor, mGrantors), |
| 183 | &_hidl_grantors_child); |
| 184 | |
| 185 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 186 | |
| 187 | const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle( |
| 188 | parentHandle, |
| 189 | parentOffset + offsetof(MQDescriptor, mHandle)); |
| 190 | |
| 191 | if (_hidl_mq_handle_ptr == nullptr) { |
| 192 | _hidl_err = ::android::UNKNOWN_ERROR; |
| 193 | return _hidl_err; |
| 194 | } |
| 195 | |
| 196 | return _hidl_err; |
| 197 | } |
| 198 | |
| 199 | template<MQFlavor flavor> |
| 200 | ::android::status_t MQDescriptor<flavor>::writeEmbeddedToParcel( |
| 201 | Parcel *parcel, |
| 202 | size_t parentHandle, |
| 203 | size_t parentOffset) const { |
| 204 | ::android::status_t _hidl_err = ::android::OK; |
| 205 | |
| 206 | size_t _hidl_grantors_child; |
| 207 | |
| 208 | _hidl_err = mGrantors.writeEmbeddedToParcel( |
| 209 | parcel, |
| 210 | parentHandle, |
| 211 | parentOffset + offsetof(MQDescriptor, mGrantors), |
| 212 | &_hidl_grantors_child); |
| 213 | |
| 214 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 215 | |
| 216 | _hidl_err = parcel->writeEmbeddedNativeHandle( |
| 217 | mHandle, |
| 218 | parentHandle, |
| 219 | parentOffset + offsetof(MQDescriptor, mHandle)); |
| 220 | |
| 221 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 222 | |
| 223 | return _hidl_err; |
| 224 | } |
| 225 | |
| 226 | template<MQFlavor flavor> |
| 227 | size_t MQDescriptor<flavor>::getSize() const { |
| 228 | return mGrantors[DATAPTRPOS].extent; |
| 229 | } |
| 230 | |
| 231 | template<MQFlavor flavor> |
| 232 | size_t MQDescriptor<flavor>::getQuantum() const { return mQuantum; } |
| 233 | |
| 234 | template<MQFlavor flavor> |
| 235 | int32_t MQDescriptor<flavor>::getFlags() const { return mFlags; } |
| 236 | |
| 237 | template<MQFlavor flavor> |
| 238 | std::vector<GrantorDescriptor> MQDescriptor<flavor>::getGrantors() const { |
| 239 | size_t grantor_count = mGrantors.size(); |
| 240 | std::vector<GrantorDescriptor> grantors(grantor_count); |
| 241 | for (size_t i = 0; i < grantor_count; i++) { |
| 242 | grantors[i] = mGrantors[i]; |
| 243 | } |
| 244 | return grantors; |
| 245 | } |
| 246 | |
| 247 | template<MQFlavor flavor> |
| 248 | const sp<NativeHandle> MQDescriptor<flavor>::getNativeHandle() const { |
| 249 | /* |
| 250 | * Create an sp<NativeHandle> from mHandle. |
| 251 | */ |
| 252 | return NativeHandle::create(mHandle, false /* ownsHandle */); |
| 253 | } |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame] | 254 | } // namespace hardware |
| 255 | } // namespace android |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 256 | |
| 257 | #endif // FMSGQ_DESCRIPTOR_H |