blob: e059b4dc14a8d00c449b79055ca26edca310df6d [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 Moreland55d2dcd2020-02-10 17:20:56 -080019#include "InternalStatic.h" // TODO(b/69122224): remove this include, for tryWrap
Steven Moreland9cbef742018-06-26 16:23:50 -070020
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);
Jiyong Park8ca9bd62018-01-16 16:54:04 +090032 if (func) {
33 return func(static_cast<void*>(iface.get()));
34 }
35 return nullptr;
36}
37
Steven Moreland0b1e62a2017-10-09 13:03:17 -070038sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
39 if (iface == nullptr || iface->isRemote()) {
40 // doesn't know how to handle it.
41 return iface;
42 }
Jiyong Park8ca9bd62018-01-16 16:54:04 +090043
44 // Consider the case when an AOSP interface is extended by partners.
45 // Then the partner's HAL interface library is loaded only in the vndk
46 // linker namespace, but not in the default linker namespace, where
47 // this code runs. As a result, BsConstructorMap in the latter does not
48 // have the entry for the descriptor name.
49 //
50 // Therefore, we try to wrap using the descript names of the parent
51 // types along the interface chain, instead of always using the descriptor
52 // name of the current interface.
53 sp<IBase> base;
54 auto ret = iface->interfaceChain([&](const auto& types) {
55 for (const std::string& descriptor : types) {
56 base = tryWrap(descriptor, iface);
57 if (base != nullptr) {
58 break; // wrap is successful. no need to lookup further.
59 }
60 }
61 });
62
63 if (!ret.isOk()) {
Steven Moreland0b1e62a2017-10-09 13:03:17 -070064 return nullptr;
65 }
Steven Moreland0b1e62a2017-10-09 13:03:17 -070066
Jiyong Park8ca9bd62018-01-16 16:54:04 +090067 // It is ensured that if this function is called with an instance of IType
68 // then the corresponding descriptor would be in the BsConstructorMap.
69 // This is because referencing IType implies that the interface library
70 // defining the type has already been loaded into the current linker
71 // namespace, and thus the library should have added an entry into the
72 // BsConstructorMap while executing the library's constructor.
Steven Moreland0b1e62a2017-10-09 13:03:17 -070073 return base;
74}
75
76} // namespace details
77} // namespace hardware
Yifan Hong97f88f32017-11-09 16:05:37 -080078} // namespace android