1 /*
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
22 using ::android::hidl::base::V1_0::IBase;
23
24 namespace android {
25 namespace hardware {
26 namespace details {
27
tryWrap(const std::string & descriptor,sp<IBase> iface)28 static 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
wrapPassthroughInternal(sp<IBase> iface)39 sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
40 if (iface == nullptr || iface->isRemote()) {
41 // doesn't know how to handle it.
42 return iface;
43 }
44
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()) {
65 return nullptr;
66 }
67
68 // 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.
74 return base;
75 }
76
77 } // namespace details
78 } // namespace hardware
79 } // namespace android
80