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 | |
Devin Moore | ec271a7 | 2020-10-09 10:42:57 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/common/fmq/MQDescriptor.h> |
| 20 | #include <aidl/android/hardware/common/fmq/SynchronizedReadWrite.h> |
| 21 | #include <aidl/android/hardware/common/fmq/UnsynchronizedWrite.h> |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
| 23 | #include <fmq/AidlMQDescriptorShim.h> |
| 24 | #include <fmq/MessageQueueBase.h> |
| 25 | #include <utils/Log.h> |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 26 | #include <type_traits> |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 27 | |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Devin Moore | ec271a7 | 2020-10-09 10:42:57 -0700 | [diff] [blame] | 30 | using aidl::android::hardware::common::fmq::MQDescriptor; |
| 31 | using aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 32 | using aidl::android::hardware::common::fmq::UnsynchronizedWrite; |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 33 | using android::details::AidlMQDescriptorShim; |
| 34 | using android::hardware::MQFlavor; |
| 35 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 36 | template <typename T> |
| 37 | struct FlavorTypeToValue; |
| 38 | |
| 39 | template <> |
| 40 | struct FlavorTypeToValue<SynchronizedReadWrite> { |
| 41 | static constexpr MQFlavor value = hardware::kSynchronizedReadWrite; |
| 42 | }; |
| 43 | |
| 44 | template <> |
| 45 | struct FlavorTypeToValue<UnsynchronizedWrite> { |
| 46 | static constexpr MQFlavor value = hardware::kUnsynchronizedWrite; |
| 47 | }; |
| 48 | |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 49 | typedef uint64_t RingBufferPosition; |
| 50 | |
Devin Moore | b9280aa | 2020-09-18 16:40:51 -0700 | [diff] [blame] | 51 | /* |
| 52 | * AIDL parcelables will have the typedef fixed_size. It is std::true_type when the |
| 53 | * parcelable is annotated with @FixedSize, and std::false_type when not. Other types |
| 54 | * should not have the fixed_size typedef, so they will always resolve to std::false_type. |
| 55 | */ |
| 56 | template <typename T, typename = void> |
| 57 | struct has_typedef_fixed_size : std::false_type {}; |
| 58 | |
| 59 | template <typename T> |
| 60 | struct has_typedef_fixed_size<T, std::void_t<typename T::fixed_size>> : T::fixed_size {}; |
| 61 | |
Devin Moore | 598bcc7 | 2020-10-13 10:49:36 -0700 | [diff] [blame^] | 62 | #define STATIC_AIDL_TYPE_CHECK(T) \ |
| 63 | static_assert(has_typedef_fixed_size<T>::value == true || std::is_fundamental<T>::value || \ |
| 64 | std::is_enum<T>::value, \ |
| 65 | "Only fundamental types, enums, and AIDL parcelables annotated with @FixedSize " \ |
| 66 | "and built for the NDK backend are supported as payload types(T)."); |
| 67 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 68 | template <typename T, typename U> |
| 69 | struct AidlMessageQueue final |
| 70 | : public MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value> { |
Devin Moore | 598bcc7 | 2020-10-13 10:49:36 -0700 | [diff] [blame^] | 71 | STATIC_AIDL_TYPE_CHECK(T); |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 72 | typedef AidlMQDescriptorShim<T, FlavorTypeToValue<U>::value> Descriptor; |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 73 | /** |
| 74 | * This constructor uses the external descriptor used with AIDL interfaces. |
| 75 | * It will create an FMQ based on the descriptor that was obtained from |
| 76 | * another FMQ instance for communication. |
| 77 | * |
| 78 | * @param desc Descriptor from another FMQ that contains all of the |
| 79 | * information required to create a new instance of that queue. |
| 80 | * @param resetPointers Boolean indicating whether the read/write pointers |
| 81 | * should be reset or not. |
| 82 | */ |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 83 | AidlMessageQueue(const MQDescriptor<T, U>& desc, bool resetPointers = true); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 84 | ~AidlMessageQueue() = default; |
| 85 | |
| 86 | /** |
| 87 | * This constructor uses Ashmem shared memory to create an FMQ |
| 88 | * that can contain a maximum of 'numElementsInQueue' elements of type T. |
| 89 | * |
| 90 | * @param numElementsInQueue Capacity of the AidlMessageQueue in terms of T. |
| 91 | * @param configureEventFlagWord Boolean that specifies if memory should |
| 92 | * also be allocated and mapped for an EventFlag word. |
| 93 | */ |
| 94 | AidlMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord = false); |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 95 | MQDescriptor<T, U> dupeDesc(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | AidlMessageQueue(const AidlMessageQueue& other) = delete; |
| 99 | AidlMessageQueue& operator=(const AidlMessageQueue& other) = delete; |
| 100 | AidlMessageQueue() = delete; |
| 101 | }; |
| 102 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 103 | template <typename T, typename U> |
| 104 | AidlMessageQueue<T, U>::AidlMessageQueue(const MQDescriptor<T, U>& desc, bool resetPointers) |
| 105 | : MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>(Descriptor(desc), |
| 106 | resetPointers) {} |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 107 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 108 | template <typename T, typename U> |
| 109 | AidlMessageQueue<T, U>::AidlMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord) |
| 110 | : MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>( |
| 111 | numElementsInQueue, configureEventFlagWord) {} |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 112 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 113 | template <typename T, typename U> |
| 114 | MQDescriptor<T, U> AidlMessageQueue<T, U>::dupeDesc() { |
| 115 | auto* shim = MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>::getDesc(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 116 | if (shim) { |
Devin Moore | ec271a7 | 2020-10-09 10:42:57 -0700 | [diff] [blame] | 117 | std::vector<aidl::android::hardware::common::fmq::GrantorDescriptor> grantors; |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 118 | for (const auto& grantor : shim->grantors()) { |
Devin Moore | ec271a7 | 2020-10-09 10:42:57 -0700 | [diff] [blame] | 119 | grantors.push_back(aidl::android::hardware::common::fmq::GrantorDescriptor{ |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 120 | .offset = static_cast<int32_t>(grantor.offset), |
| 121 | .extent = static_cast<int64_t>(grantor.extent)}); |
| 122 | } |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 123 | return MQDescriptor<T, U>{ |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 124 | .quantum = static_cast<int32_t>(shim->getQuantum()), |
Devin Moore | 7c04cfe | 2020-08-17 14:37:13 -0700 | [diff] [blame] | 125 | .grantors = grantors, |
| 126 | .flags = static_cast<int32_t>(shim->getFlags()), |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 127 | .fileDescriptor = ndk::ScopedFileDescriptor(dup(shim->handle()->data[0])), |
| 128 | }; |
| 129 | } else { |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 130 | return MQDescriptor<T, U>(); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace android |