blob: e5eb16495fac13ce51aee627f645250723442ccb [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
19#include <hidl/HidlTransportUtils.h>
20#include <hidl/Static.h>
21
22using ::android::hidl::base::V1_0::IBase;
23
24namespace android {
25namespace hardware {
26namespace details {
27
Jiyong Park8ca9bd62018-01-16 16:54:04 +090028static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) {
29 auto func = getBsConstructorMap().get(descriptor, nullptr);
30 if (!func) {
31 func = gBsConstructorMap.get(descriptor, nullptr);
32 }
33 if (func) {
34 return func(static_cast<void*>(iface.get()));
35 }
36 return nullptr;
37}
38
Steven Moreland0b1e62a2017-10-09 13:03:17 -070039sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
40 if (iface == nullptr || iface->isRemote()) {
41 // doesn't know how to handle it.
42 return iface;
43 }
Jiyong Park8ca9bd62018-01-16 16:54:04 +090044
45 // Consider the case when an AOSP interface is extended by partners.
46 // Then the partner's HAL interface library is loaded only in the vndk
47 // linker namespace, but not in the default linker namespace, where
48 // this code runs. As a result, BsConstructorMap in the latter does not
49 // have the entry for the descriptor name.
50 //
51 // Therefore, we try to wrap using the descript names of the parent
52 // types along the interface chain, instead of always using the descriptor
53 // name of the current interface.
54 sp<IBase> base;
55 auto ret = iface->interfaceChain([&](const auto& types) {
56 for (const std::string& descriptor : types) {
57 base = tryWrap(descriptor, iface);
58 if (base != nullptr) {
59 break; // wrap is successful. no need to lookup further.
60 }
61 }
62 });
63
64 if (!ret.isOk()) {
Steven Moreland0b1e62a2017-10-09 13:03:17 -070065 return nullptr;
66 }
Steven Moreland0b1e62a2017-10-09 13:03:17 -070067
Jiyong Park8ca9bd62018-01-16 16:54:04 +090068 // It is ensured that if this function is called with an instance of IType
69 // then the corresponding descriptor would be in the BsConstructorMap.
70 // This is because referencing IType implies that the interface library
71 // defining the type has already been loaded into the current linker
72 // namespace, and thus the library should have added an entry into the
73 // BsConstructorMap while executing the library's constructor.
Steven Moreland0b1e62a2017-10-09 13:03:17 -070074 return base;
75}
76
77} // namespace details
78} // namespace hardware
Yifan Hong97f88f32017-11-09 16:05:37 -080079} // namespace android