blob: f6504423a82f7858991b33f27594432c7d6a5511 [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>
20#include <cutils/native_handle.h>
21#include <fmq/AidlMQDescriptorShim.h>
22#include <fmq/MessageQueueBase.h>
23#include <utils/Log.h>
24
25using aidl::android::hardware::common::MQDescriptor;
26using android::details::AidlMQDescriptorShim;
27using android::hardware::MQFlavor;
28
29namespace android {
30
31typedef uint64_t RingBufferPosition;
32
33template <typename T, MQFlavor flavor>
34struct AidlMessageQueue final : public MessageQueueBase<AidlMQDescriptorShim, T, flavor> {
35 typedef AidlMQDescriptorShim<T, flavor> Descriptor;
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 */
46 AidlMessageQueue(const MQDescriptor& desc, bool resetPointers = true);
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);
58 MQDescriptor dupeDesc();
59
60 private:
61 AidlMessageQueue(const AidlMessageQueue& other) = delete;
62 AidlMessageQueue& operator=(const AidlMessageQueue& other) = delete;
63 AidlMessageQueue() = delete;
64};
65
66template <typename T, MQFlavor flavor>
67AidlMessageQueue<T, flavor>::AidlMessageQueue(const MQDescriptor& desc, bool resetPointers)
68 : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(Descriptor(desc), resetPointers) {}
69
70template <typename T, MQFlavor flavor>
71AidlMessageQueue<T, flavor>::AidlMessageQueue(size_t numElementsInQueue,
72 bool configureEventFlagWord)
73 : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(numElementsInQueue,
74 configureEventFlagWord) {}
75
76template <typename T, MQFlavor flavor>
77MQDescriptor AidlMessageQueue<T, flavor>::dupeDesc() {
78 auto* shim = MessageQueueBase<AidlMQDescriptorShim, T, flavor>::getDesc();
79 if (shim) {
80 return MQDescriptor{
81 .quantum = static_cast<int32_t>(shim->getQuantum()),
82 .grantors = shim->grantors(),
83 .flags = shim->getFlags(),
84 .fileDescriptor = ndk::ScopedFileDescriptor(dup(shim->handle()->data[0])),
85 };
86 } else {
87 return MQDescriptor();
88 }
89}
90
91} // namespace android