blob: b7a33beec8a250762cf5029961b86ec361fd5f7f [file] [log] [blame]
Devin Moore133cb5e2020-07-07 16:31:22 -07001/*
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>
Devin Moore1b2d0a62020-09-22 17:20:54 +000020#include <aidl/android/hardware/common/SynchronizedReadWrite.h>
21#include <aidl/android/hardware/common/UnsynchronizedWrite.h>
Devin Moore133cb5e2020-07-07 16:31:22 -070022#include <cutils/native_handle.h>
23#include <fmq/AidlMQDescriptorShim.h>
24#include <fmq/MessageQueueBase.h>
25#include <utils/Log.h>
Devin Moore1b2d0a62020-09-22 17:20:54 +000026#include <type_traits>
Devin Moore133cb5e2020-07-07 16:31:22 -070027
Devin Moore7c04cfe2020-08-17 14:37:13 -070028namespace android {
29
Devin Moore133cb5e2020-07-07 16:31:22 -070030using aidl::android::hardware::common::MQDescriptor;
Devin Moore1b2d0a62020-09-22 17:20:54 +000031using aidl::android::hardware::common::SynchronizedReadWrite;
32using aidl::android::hardware::common::UnsynchronizedWrite;
Devin Moore133cb5e2020-07-07 16:31:22 -070033using android::details::AidlMQDescriptorShim;
34using android::hardware::MQFlavor;
35
Devin Moore1b2d0a62020-09-22 17:20:54 +000036template <typename T>
37struct FlavorTypeToValue;
38
39template <>
40struct FlavorTypeToValue<SynchronizedReadWrite> {
41 static constexpr MQFlavor value = hardware::kSynchronizedReadWrite;
42};
43
44template <>
45struct FlavorTypeToValue<UnsynchronizedWrite> {
46 static constexpr MQFlavor value = hardware::kUnsynchronizedWrite;
47};
48
Devin Moore133cb5e2020-07-07 16:31:22 -070049typedef uint64_t RingBufferPosition;
50
Devin Mooreb9280aa2020-09-18 16:40:51 -070051/*
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 */
56template <typename T, typename = void>
57struct has_typedef_fixed_size : std::false_type {};
58
59template <typename T>
60struct has_typedef_fixed_size<T, std::void_t<typename T::fixed_size>> : T::fixed_size {};
61
Devin Moore1b2d0a62020-09-22 17:20:54 +000062template <typename T, typename U>
63struct AidlMessageQueue final
64 : public MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value> {
Devin Mooreb9280aa2020-09-18 16:40:51 -070065 static_assert(has_typedef_fixed_size<T>::value == true || std::is_fundamental<T>::value ||
66 std::is_enum<T>::value,
67 "Only fundamental types, enums, and AIDL parcelables annotated with @FixedSize "
Devin Moorec1b837c2020-09-30 08:56:58 -070068 "and built for the NDK backend are supported as payload types(T).");
Devin Moore1b2d0a62020-09-22 17:20:54 +000069 typedef AidlMQDescriptorShim<T, FlavorTypeToValue<U>::value> Descriptor;
Devin Moore133cb5e2020-07-07 16:31:22 -070070 /**
71 * This constructor uses the external descriptor used with AIDL interfaces.
72 * It will create an FMQ based on the descriptor that was obtained from
73 * another FMQ instance for communication.
74 *
75 * @param desc Descriptor from another FMQ that contains all of the
76 * information required to create a new instance of that queue.
77 * @param resetPointers Boolean indicating whether the read/write pointers
78 * should be reset or not.
79 */
Devin Moore1b2d0a62020-09-22 17:20:54 +000080 AidlMessageQueue(const MQDescriptor<T, U>& desc, bool resetPointers = true);
Devin Moore133cb5e2020-07-07 16:31:22 -070081 ~AidlMessageQueue() = default;
82
83 /**
84 * This constructor uses Ashmem shared memory to create an FMQ
85 * that can contain a maximum of 'numElementsInQueue' elements of type T.
86 *
87 * @param numElementsInQueue Capacity of the AidlMessageQueue in terms of T.
88 * @param configureEventFlagWord Boolean that specifies if memory should
89 * also be allocated and mapped for an EventFlag word.
90 */
91 AidlMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord = false);
Devin Moore1b2d0a62020-09-22 17:20:54 +000092 MQDescriptor<T, U> dupeDesc();
Devin Moore133cb5e2020-07-07 16:31:22 -070093
94 private:
95 AidlMessageQueue(const AidlMessageQueue& other) = delete;
96 AidlMessageQueue& operator=(const AidlMessageQueue& other) = delete;
97 AidlMessageQueue() = delete;
98};
99
Devin Moore1b2d0a62020-09-22 17:20:54 +0000100template <typename T, typename U>
101AidlMessageQueue<T, U>::AidlMessageQueue(const MQDescriptor<T, U>& desc, bool resetPointers)
102 : MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>(Descriptor(desc),
103 resetPointers) {}
Devin Moore133cb5e2020-07-07 16:31:22 -0700104
Devin Moore1b2d0a62020-09-22 17:20:54 +0000105template <typename T, typename U>
106AidlMessageQueue<T, U>::AidlMessageQueue(size_t numElementsInQueue, bool configureEventFlagWord)
107 : MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>(
108 numElementsInQueue, configureEventFlagWord) {}
Devin Moore133cb5e2020-07-07 16:31:22 -0700109
Devin Moore1b2d0a62020-09-22 17:20:54 +0000110template <typename T, typename U>
111MQDescriptor<T, U> AidlMessageQueue<T, U>::dupeDesc() {
112 auto* shim = MessageQueueBase<AidlMQDescriptorShim, T, FlavorTypeToValue<U>::value>::getDesc();
Devin Moore133cb5e2020-07-07 16:31:22 -0700113 if (shim) {
Devin Moore7c04cfe2020-08-17 14:37:13 -0700114 std::vector<aidl::android::hardware::common::GrantorDescriptor> grantors;
115 for (const auto& grantor : shim->grantors()) {
116 grantors.push_back(aidl::android::hardware::common::GrantorDescriptor{
117 .offset = static_cast<int32_t>(grantor.offset),
118 .extent = static_cast<int64_t>(grantor.extent)});
119 }
Devin Moore1b2d0a62020-09-22 17:20:54 +0000120 return MQDescriptor<T, U>{
Devin Moore133cb5e2020-07-07 16:31:22 -0700121 .quantum = static_cast<int32_t>(shim->getQuantum()),
Devin Moore7c04cfe2020-08-17 14:37:13 -0700122 .grantors = grantors,
123 .flags = static_cast<int32_t>(shim->getFlags()),
Devin Moore133cb5e2020-07-07 16:31:22 -0700124 .fileDescriptor = ndk::ScopedFileDescriptor(dup(shim->handle()->data[0])),
125 };
126 } else {
Devin Moore1b2d0a62020-09-22 17:20:54 +0000127 return MQDescriptor<T, U>();
Devin Moore133cb5e2020-07-07 16:31:22 -0700128 }
129}
130
131} // namespace android