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