xref: /aosp_15_r20/system/libfmq/include/fmq/AidlMQDescriptorShim.h (revision be431cd81a9a2349eaea34eb56fcf6d1608da596)
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 #pragma once
17 #include <cutils/native_handle.h>
18 #include <limits>
19 #include <type_traits>
20 
21 #include <aidl/android/hardware/common/fmq/MQDescriptor.h>
22 #include <fmq/MQDescriptorBase.h>
23 #include "AidlMQDescriptorShimBase.h"
24 
25 namespace android {
26 namespace details {
27 using aidl::android::hardware::common::fmq::MQDescriptor;
28 using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
29 using aidl::android::hardware::common::fmq::UnsynchronizedWrite;
30 using android::hardware::MQFlavor;
31 
32 struct BackendTypesStore {
33     template <typename T, typename flavor>
34     using MQDescriptorType = aidl::android::hardware::common::fmq::MQDescriptor<T, flavor>;
35     using SynchronizedReadWriteType = aidl::android::hardware::common::fmq::SynchronizedReadWrite;
36     using UnsynchronizedWriteType = aidl::android::hardware::common::fmq::UnsynchronizedWrite;
37 };
38 
39 template <typename T, MQFlavor flavor>
40 struct AidlMQDescriptorShim : public AidlMQDescriptorShimBase<T, flavor, BackendTypesStore> {
41     // Takes ownership of handle
42     AidlMQDescriptorShim(const std::vector<android::hardware::GrantorDescriptor>& grantors,
43                          native_handle_t* nHandle, size_t size);
44 
45     // Takes ownership of handle
46     AidlMQDescriptorShim(
47             const MQDescriptor<
48                     T, typename std::conditional<flavor == hardware::kSynchronizedReadWrite,
49                                                  SynchronizedReadWrite, UnsynchronizedWrite>::type>&
50                     desc);
51 
52     // Takes ownership of handle
53     AidlMQDescriptorShim(size_t bufferSize, native_handle_t* nHandle, size_t messageSize,
54                          bool configureEventFlag = false);
55 
AidlMQDescriptorShimAidlMQDescriptorShim56     explicit AidlMQDescriptorShim(const AidlMQDescriptorShim& other)
57         : AidlMQDescriptorShim(0, nullptr, 0) {
58         *this = other;
59     }
60 };
61 
62 template <typename T, MQFlavor flavor>
AidlMQDescriptorShim(const MQDescriptor<T,typename std::conditional<flavor==hardware::kSynchronizedReadWrite,SynchronizedReadWrite,UnsynchronizedWrite>::type> & desc)63 AidlMQDescriptorShim<T, flavor>::AidlMQDescriptorShim(
64         const MQDescriptor<T, typename std::conditional<flavor == hardware::kSynchronizedReadWrite,
65                                                         SynchronizedReadWrite,
66                                                         UnsynchronizedWrite>::type>& desc)
67     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStore>(desc) {}
68 
69 template <typename T, MQFlavor flavor>
AidlMQDescriptorShim(const std::vector<android::hardware::GrantorDescriptor> & grantors,native_handle_t * nhandle,size_t size)70 AidlMQDescriptorShim<T, flavor>::AidlMQDescriptorShim(
71         const std::vector<android::hardware::GrantorDescriptor>& grantors, native_handle_t* nhandle,
72         size_t size)
73     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStore>(grantors, nhandle, size) {}
74 
75 template <typename T, MQFlavor flavor>
AidlMQDescriptorShim(size_t bufferSize,native_handle_t * nHandle,size_t messageSize,bool configureEventFlag)76 AidlMQDescriptorShim<T, flavor>::AidlMQDescriptorShim(size_t bufferSize, native_handle_t* nHandle,
77                                                       size_t messageSize, bool configureEventFlag)
78     : AidlMQDescriptorShimBase<T, flavor, BackendTypesStore>(bufferSize, nHandle, messageSize,
79                                                              configureEventFlag) {}
80 }  // namespace details
81 }  // namespace android
82