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 "InternalStatic.h"  // TODO(b/69122224): remove this include, for tryWrap
20 
21 #include <hidl/HidlTransportUtils.h>
22 #include <hidl/Static.h>
23 
24 using ::android::hidl::base::V1_0::IBase;
25 
26 namespace android {
27 namespace hardware {
28 namespace details {
29 
30 static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) {
31     auto func = getBsConstructorMap().get(descriptor, nullptr);
32     if (!func) {
33         // TODO(b/69122224): remove this when prebuilts don't reference it
34         func = gBsConstructorMap->get(descriptor, nullptr);
35     }
36     if (func) {
37         return func(static_cast<void*>(iface.get()));
38     }
39     return nullptr;
40 }
41 
42 sp<IBase> wrapPassthroughInternal(sp<IBase> iface) {
43     if (iface == nullptr || iface->isRemote()) {
44         // doesn't know how to handle it.
45         return iface;
46     }
47 
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()) {
68         return nullptr;
69     }
70 
71     // 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.
77     return base;
78 }
79 
80 }  // namespace details
81 }  // namespace hardware
82 }  // namespace android
83