Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #pragma once |
| 18 | |
| 19 | #include <aidl/android/hardware/common/MQDescriptor.h> |
| 20 | #include <cutils/native_handle.h> |
| 21 | #include <fmq/AidlMQDescriptorShim.h> |
| 22 | #include <fmq/MessageQueueBase.h> |
| 23 | #include <utils/Log.h> |
| 24 | |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 27 | using aidl::android::hardware::common::MQDescriptor; |
| 28 | using android::details::AidlMQDescriptorShim; |
| 29 | using android::hardware::MQFlavor; |
| 30 | |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 31 | typedef uint64_t RingBufferPosition; |
| 32 | |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 33 | template <typename T, MQFlavor flavor> |
| 34 | struct AidlMessageQueue final : public MessageQueueBase<AidlMQDescriptorShim, T, flavor> { |
| 35 | typedef AidlMQDescriptorShim<T, flavor> Descriptor; |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 36 | /** |
| 37 | * This constructor uses the external descriptor used with AIDL interfaces. |
| 38 | * It will create an FMQ based on the descriptor that was obtained from |
| 39 | * another FMQ instance for communication. |
| 40 | * |
| 41 | * @param desc Descriptor from another FMQ that contains all of the |
| 42 | * information required to create a new instance of that queue. |
| 43 | * @param resetPointers Boolean indicating whether the read/write pointers |
| 44 | * should be reset or not. |
| 45 | */ |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 46 | AidlMessageQueue(const MQDescriptor& desc, bool resetPointers = true); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 47 | ~AidlMessageQueue() = default; |
| 48 | |
| 49 | /** |
| 50 | * This constructor uses Ashmem shared memory to create an FMQ |
| 51 | * that can contain a maximum of 'numElementsInQueue' elements of type T. |
| 52 | * |
| 53 | * @param numElementsInQueue Capacity of the AidlMessageQueue in terms of T. |
| 54 | * @param configureEventFlagWord Boolean that specifies if memory should |
| 55 | * also be allocated and mapped for an EventFlag word. |
| 56 | */ |
| 57 | AidlMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord = false); |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 58 | MQDescriptor dupeDesc(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | AidlMessageQueue(const AidlMessageQueue& other) = delete; |
| 62 | AidlMessageQueue& operator=(const AidlMessageQueue& other) = delete; |
| 63 | AidlMessageQueue() = delete; |
| 64 | }; |
| 65 | |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 66 | template <typename T, MQFlavor flavor> |
| 67 | AidlMessageQueue<T, flavor>::AidlMessageQueue(const MQDescriptor& desc, bool resetPointers) |
| 68 | : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(Descriptor(desc), resetPointers) {} |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 69 | |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 70 | template <typename T, MQFlavor flavor> |
| 71 | AidlMessageQueue<T, flavor>::AidlMessageQueue(size_t numElementsInQueue, |
| 72 | bool configureEventFlagWord) |
| 73 | : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(numElementsInQueue, |
| 74 | configureEventFlagWord) {} |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 75 | |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 76 | template <typename T, MQFlavor flavor> |
| 77 | MQDescriptor AidlMessageQueue<T, flavor>::dupeDesc() { |
| 78 | auto* shim = MessageQueueBase<AidlMQDescriptorShim, T, flavor>::getDesc(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 79 | if (shim) { |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 80 | std::vector<aidl::android::hardware::common::GrantorDescriptor> grantors; |
| 81 | for (const auto& grantor : shim->grantors()) { |
| 82 | grantors.push_back(aidl::android::hardware::common::GrantorDescriptor{ |
| 83 | .offset = static_cast<int32_t>(grantor.offset), |
| 84 | .extent = static_cast<int64_t>(grantor.extent)}); |
| 85 | } |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 86 | return MQDescriptor{ |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 87 | .quantum = static_cast<int32_t>(shim->getQuantum()), |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 88 | .grantors = grantors, |
| 89 | .flags = static_cast<int32_t>(shim->getFlags()), |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 90 | .fileDescriptor = ndk::ScopedFileDescriptor(dup(shim->handle()->data[0])), |
| 91 | }; |
| 92 | } else { |
Diego Wilson | 1ed80be | 2020-09-21 19:36:49 +0000 | [diff] [blame] | 93 | return MQDescriptor(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | } // namespace android |