HidlPassthroughSupport.cpp 2.9 KB

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