blob: bc6765622c04bdea1ddc64e6c3e5760e807af97e [file] [log] [blame]
Steven Moreland0b1e62a2017-10-09 13:03:17 -07001/*
2 * Copyright (C) 2017 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 <hidl/HidlPassthroughSupport.h>
18
Steven Moreland9cbef742018-06-26 16:23:50 -070019#include <InternalStatic.h> // TODO(b/69122224): remove this include, for tryWrap
20
Steven Moreland0b1e62a2017-10-09 13:03:17 -070021#include <hidl/HidlTransportUtils.h>
22#include <hidl/Static.h>
23
24using ::android::hidl::base::V1_0::IBase;
25
26namespace android {
27namespace hardware {
28namespace details {
29
Jiyong Park8ca9bd62018-01-16 16:54:04 +090030static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) {
31 auto func = getBsConstructorMap().get(descriptor, nullptr);
Steven Moreland6538e752018-02-08 16:49:23 +000032 if (!func) {
Steven Moreland9cbef742018-06-26 16:23:50 -070033 // TODO(b/69122224): remove this when prebuilts don't reference it
Steven Morelandd0a754c2019-04-22 10:27:47 -070034 func = gBsConstructorMap->get(descriptor, nullptr);
Steven Moreland6538e752018-02-08 16:49:23 +000035 }
Jiyong Park8ca9bd62018-01-16 16:54:04 +090036 if (func) {
37 return func(static_cast<void*>(iface.get()));
38 }
39 return nullptr;
40}
41
Steven Moreland0b1e62a2017-10-09 13:03:17 -070042sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
43 if (iface == nullptr || iface->isRemote()) {
44 // doesn't know how to handle it.
45 return iface;
46 }
Jiyong Park8ca9bd62018-01-16 16:54:04 +090047
48 // Consider the case when an AOSP interface is extended by partners.
49 // Then the partner's HAL interface library is loaded only in the vndk
50 // linker namespace, but not in the default linker namespace, where
51 // this code runs. As a result, BsConstructorMap in the latter does not
52 // have the entry for the descriptor name.
53 //
54 // Therefore, we try to wrap using the descript names of the parent
55 // types along the interface chain, instead of always using the descriptor
56 // name of the current interface.
57 sp<IBase> base;
58 auto ret = iface->interfaceChain([&](const auto& types) {
59 for (const std::string& descriptor : types) {
60 base = tryWrap(descriptor, iface);
61 if (base != nullptr) {
62 break; // wrap is successful. no need to lookup further.
63 }
64 }
65 });
66
67 if (!ret.isOk()) {
Steven Moreland0b1e62a2017-10-09 13:03:17 -070068 return nullptr;
69 }
Steven Moreland0b1e62a2017-10-09 13:03:17 -070070
Jiyong Park8ca9bd62018-01-16 16:54:04 +090071 // It is ensured that if this function is called with an instance of IType
72 // then the corresponding descriptor would be in the BsConstructorMap.
73 // This is because referencing IType implies that the interface library
74 // defining the type has already been loaded into the current linker
75 // namespace, and thus the library should have added an entry into the
76 // BsConstructorMap while executing the library's constructor.
Steven Moreland0b1e62a2017-10-09 13:03:17 -070077 return base;
78}
79
80} // namespace details
81} // namespace hardware
Yifan Hong97f88f32017-11-09 16:05:37 -080082} // namespace android