blob: 36f6128cf1696b3d948627b144f9df0f26f40213 [file] [log] [blame]
Mikhail Naganovbfbb75b2023-04-21 18:48:16 -07001/*
2 * Copyright (C) 2023 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#include <algorithm>
18#include <type_traits>
19
20#define LOG_TAG "AidlConversionNdkCpp"
21#include <utils/Log.h>
22
23#include <android-base/expected.h>
24#include <android/binder_auto_utils.h>
25#include <android/binder_enums.h>
26#include <android/binder_parcel.h>
27#include <binder/Enums.h>
28#include <media/AidlConversionNdkCpp.h>
29#include <media/AidlConversionUtil.h>
30
31using aidl::android::aidl_utils::statusTFromBinderStatusT;
32
33namespace android {
34
35namespace {
36
37// cpp2ndk and ndk2cpp are universal converters which work for any type,
38// however they are not the most efficient way to convert due to extra
39// marshaling / unmarshaling step.
40
41template<typename NdkType, typename CppType>
42ConversionResult<NdkType> cpp2ndk(const CppType& cpp) {
43 Parcel cppParcel;
44 RETURN_IF_ERROR(cpp.writeToParcel(&cppParcel));
45 ::ndk::ScopedAParcel ndkParcel(AParcel_create());
46 const int32_t ndkParcelBegin = AParcel_getDataPosition(ndkParcel.get());
47 RETURN_IF_ERROR(statusTFromBinderStatusT(AParcel_unmarshal(
48 ndkParcel.get(), cppParcel.data(), cppParcel.dataSize())));
49 RETURN_IF_ERROR(statusTFromBinderStatusT(AParcel_setDataPosition(
50 ndkParcel.get(), ndkParcelBegin)));
51 NdkType ndk;
52 RETURN_IF_ERROR(statusTFromBinderStatusT(ndk.readFromParcel(ndkParcel.get())));
53 return ndk;
54}
55
56template<typename CppType, typename NdkType>
57ConversionResult<CppType> ndk2cpp(const NdkType& ndk) {
58 ::ndk::ScopedAParcel ndkParcel(AParcel_create());
59 RETURN_IF_ERROR(statusTFromBinderStatusT(ndk.writeToParcel(ndkParcel.get())));
60 const int32_t ndkParcelDataSize = AParcel_getDataSize(ndkParcel.get());
61 if (ndkParcelDataSize < 0) {
62 return base::unexpected(BAD_VALUE);
63 }
64 // Parcel does not expose its data in a mutable form, we have to use an intermediate buffer.
65 std::vector<uint8_t> parcelData(static_cast<size_t>(ndkParcelDataSize));
66 RETURN_IF_ERROR(statusTFromBinderStatusT(AParcel_marshal(
67 ndkParcel.get(), parcelData.data(), 0, ndkParcelDataSize)));
68 Parcel cppParcel;
69 RETURN_IF_ERROR(cppParcel.setData(parcelData.data(), parcelData.size()));
70 CppType cpp;
71 RETURN_IF_ERROR(cpp.readFromParcel(&cppParcel));
72 return cpp;
73}
74
75// cpp2ndk_Enum and ndk2cpp_Enum are more efficient implementations specifically for enums.
76
77template<typename OutEnum, typename OutEnumRange, typename InEnum>
78 ConversionResult<OutEnum> convertEnum(const OutEnumRange& range, InEnum e) {
79 using InIntType = std::underlying_type_t<InEnum>;
80 static_assert(std::is_same_v<InIntType, std::underlying_type_t<OutEnum>>);
81
82 InIntType inEnumIndex = static_cast<InIntType>(e);
83 OutEnum outEnum = static_cast<OutEnum>(inEnumIndex);
84 if (std::find(range.begin(), range.end(), outEnum) == range.end()) {
85 return base::unexpected(BAD_VALUE);
86 }
87 return outEnum;
88}
89
90template<typename NdkEnum, typename CppEnum>
91 ConversionResult<NdkEnum> cpp2ndk_Enum(CppEnum cpp) {
92 return convertEnum<NdkEnum>(::ndk::enum_range<NdkEnum>(), cpp);
93}
94
95template<typename CppEnum, typename NdkEnum>
96 ConversionResult<CppEnum> ndk2cpp_Enum(NdkEnum ndk) {
97 return convertEnum<CppEnum>(enum_range<CppEnum>(), ndk);
98}
99
100} // namespace
101
102#define GENERATE_CONVERTERS(packageName, className) \
103 ConversionResult<::aidl::packageName::className> cpp2ndk_##className( \
104 const ::packageName::className& cpp) { \
105 return cpp2ndk<::aidl::packageName::className>(cpp); \
106 } \
107 ConversionResult<::packageName::className> ndk2cpp_##className( \
108 const ::aidl::packageName::className& ndk) { \
109 return ndk2cpp<::packageName::className>(ndk); \
110 }
111
112#define GENERATE_ENUM_CONVERTERS(packageName, className) \
113 ConversionResult<::aidl::packageName::className> cpp2ndk_##className( \
114 const ::packageName::className& cpp) { \
115 return cpp2ndk_Enum<::aidl::packageName::className>(cpp); \
116 } \
117 ConversionResult<::packageName::className> ndk2cpp_##className( \
118 const ::aidl::packageName::className& ndk) { \
119 return ndk2cpp_Enum<::packageName::className>(ndk); \
120}
121
122GENERATE_CONVERTERS(android::media::audio::common, AudioFormatDescription);
123GENERATE_CONVERTERS(android::media::audio::common, AudioHalEngineConfig);
124GENERATE_CONVERTERS(android::media::audio::common, AudioMMapPolicyInfo);
125GENERATE_ENUM_CONVERTERS(android::media::audio::common, AudioMMapPolicyType);
126GENERATE_CONVERTERS(android::media::audio::common, AudioPort);
127
128} // namespace android