Steven Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 55d2dcd | 2020-02-10 17:20:56 -0800 | [diff] [blame] | 19 | #include "InternalStatic.h" // TODO(b/69122224): remove this include, for tryWrap |
Steven Moreland | 9cbef74 | 2018-06-26 16:23:50 -0700 | [diff] [blame] | 20 | |
Steven Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 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 | |
Jiyong Park | 8ca9bd6 | 2018-01-16 16:54:04 +0900 | [diff] [blame] | 30 | static sp<IBase> tryWrap(const std::string& descriptor, sp<IBase> iface) { |
| 31 | auto func = getBsConstructorMap().get(descriptor, nullptr); |
Jiyong Park | 8ca9bd6 | 2018-01-16 16:54:04 +0900 | [diff] [blame] | 32 | if (func) { |
| 33 | return func(static_cast<void*>(iface.get())); |
| 34 | } |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
Steven Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 38 | sp<IBase> wrapPassthroughInternal(sp<IBase> iface) { |
| 39 | if (iface == nullptr || iface->isRemote()) { |
| 40 | // doesn't know how to handle it. |
| 41 | return iface; |
| 42 | } |
Jiyong Park | 8ca9bd6 | 2018-01-16 16:54:04 +0900 | [diff] [blame] | 43 | |
| 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 Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 64 | return nullptr; |
| 65 | } |
Steven Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 66 | |
Jiyong Park | 8ca9bd6 | 2018-01-16 16:54:04 +0900 | [diff] [blame] | 67 | // 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 Moreland | 0b1e62a | 2017-10-09 13:03:17 -0700 | [diff] [blame] | 73 | return base; |
| 74 | } |
| 75 | |
| 76 | } // namespace details |
| 77 | } // namespace hardware |
Yifan Hong | 97f88f3 | 2017-11-09 16:05:37 -0800 | [diff] [blame] | 78 | } // namespace android |