blob: fe4edc41de6c2fc1d464df9e2fd228be44c41f2f [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
Devin Moore7c04cfe2020-08-17 14:37:13 -070025namespace android {
26
Devin Moore133cb5e2020-07-07 16:31:22 -070027using aidl::android::hardware::common::MQDescriptor;
28using android::details::AidlMQDescriptorShim;
29using android::hardware::MQFlavor;
30
Devin Moore133cb5e2020-07-07 16:31:22 -070031typedef uint64_t RingBufferPosition;
32
Diego Wilson1ed80be2020-09-21 19:36:49 +000033template <typename T, MQFlavor flavor>
34struct AidlMessageQueue final : public MessageQueueBase<AidlMQDescriptorShim, T, flavor> {
35 typedef AidlMQDescriptorShim<T, flavor> Descriptor;
Devin Moore133cb5e2020-07-07 16:31:22 -070036 /**
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 Wilson1ed80be2020-09-21 19:36:49 +000046 AidlMessageQueue(const MQDescriptor& desc, bool resetPointers = true);
Devin Moore133cb5e2020-07-07 16:31:22 -070047 ~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 Wilson1ed80be2020-09-21 19:36:49 +000058 MQDescriptor dupeDesc();
Devin Moore133cb5e2020-07-07 16:31:22 -070059
60 private:
61 AidlMessageQueue(const AidlMessageQueue& other) = delete;
62 AidlMessageQueue& operator=(const AidlMessageQueue& other) = delete;
63 AidlMessageQueue() = delete;
64};
65
Diego Wilson1ed80be2020-09-21 19:36:49 +000066template <typename T, MQFlavor flavor>
67AidlMessageQueue<T, flavor>::AidlMessageQueue(const MQDescriptor& desc, bool resetPointers)
68 : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(Descriptor(desc), resetPointers) {}
Devin Moore133cb5e2020-07-07 16:31:22 -070069
Diego Wilson1ed80be2020-09-21 19:36:49 +000070template <typename T, MQFlavor flavor>
71AidlMessageQueue<T, flavor>::AidlMessageQueue(size_t numElementsInQueue,
72 bool configureEventFlagWord)
73 : MessageQueueBase<AidlMQDescriptorShim, T, flavor>(numElementsInQueue,
74 configureEventFlagWord) {}
Devin Moore133cb5e2020-07-07 16:31:22 -070075
Diego Wilson1ed80be2020-09-21 19:36:49 +000076template <typename T, MQFlavor flavor>
77MQDescriptor AidlMessageQueue<T, flavor>::dupeDesc() {
78 auto* shim = MessageQueueBase<AidlMQDescriptorShim, T, flavor>::getDesc();
Devin Moore133cb5e2020-07-07 16:31:22 -070079 if (shim) {
Devin Moore7c04cfe2020-08-17 14:37:13 -070080 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 Wilson1ed80be2020-09-21 19:36:49 +000086 return MQDescriptor{
Devin Moore133cb5e2020-07-07 16:31:22 -070087 .quantum = static_cast<int32_t>(shim->getQuantum()),
Devin Moore7c04cfe2020-08-17 14:37:13 -070088 .grantors = grantors,
89 .flags = static_cast<int32_t>(shim->getFlags()),
Devin Moore133cb5e2020-07-07 16:31:22 -070090 .fileDescriptor = ndk::ScopedFileDescriptor(dup(shim->handle()->data[0])),
91 };
92 } else {
Diego Wilson1ed80be2020-09-21 19:36:49 +000093 return MQDescriptor();
Devin Moore133cb5e2020-07-07 16:31:22 -070094 }
95}
96
97} // namespace android