HidlTransportUtils.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2016 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/HidlTransportUtils.h>
  17. #include <android/hidl/base/1.0/IBase.h>
  18. namespace android {
  19. namespace hardware {
  20. namespace details {
  21. using ::android::hidl::base::V1_0::IBase;
  22. Return<bool> canCastInterface(IBase* interface, const char* castTo, bool emitError) {
  23. if (interface == nullptr) {
  24. return false;
  25. }
  26. // b/68217907
  27. // Every HIDL interface is a base interface.
  28. if (std::string(IBase::descriptor) == castTo) {
  29. return true;
  30. }
  31. bool canCast = false;
  32. auto chainRet = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
  33. for (size_t i = 0; i < types.size(); i++) {
  34. if (types[i] == castTo) {
  35. canCast = true;
  36. break;
  37. }
  38. }
  39. });
  40. if (!chainRet.isOk()) {
  41. // call fails, propagate the error if emitError
  42. return emitError
  43. ? details::StatusOf<void, bool>(chainRet)
  44. : Return<bool>(false);
  45. }
  46. return canCast;
  47. }
  48. std::string getDescriptor(IBase* interface) {
  49. if (interface == nullptr) {
  50. return "";
  51. }
  52. std::string myDescriptor{};
  53. auto ret = interface->interfaceDescriptor([&](const hidl_string &types) {
  54. myDescriptor = types.c_str();
  55. });
  56. ret.isOk(); // ignored, return empty string if not isOk()
  57. return myDescriptor;
  58. }
  59. } // namespace details
  60. } // namespace hardware
  61. } // namespace android