blob: 8c612817d126bf8ba9a1669d09de489b1d353e46 [file] [log] [blame]
Steven Moreland108d09d2017-05-05 16:15:38 -07001/*
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
17#include <hidl/HidlTransportUtils.h>
18
Steven Moreland1ab46132017-11-03 16:18:19 -070019#include <android/hidl/base/1.0/IBase.h>
20
Steven Moreland108d09d2017-05-05 16:15:38 -070021namespace android {
22namespace hardware {
23namespace details {
24
Steven Moreland1ab46132017-11-03 16:18:19 -070025using ::android::hidl::base::V1_0::IBase;
26
27Return<bool> canCastInterface(IBase* interface, const char* castTo, bool emitError) {
Steven Moreland108d09d2017-05-05 16:15:38 -070028 if (interface == nullptr) {
29 return false;
30 }
31
Steven Moreland1ab46132017-11-03 16:18:19 -070032 // b/68217907
33 // Every HIDL interface is a base interface.
34 if (std::string(IBase::descriptor) == castTo) {
35 return true;
36 }
37
Steven Moreland108d09d2017-05-05 16:15:38 -070038 bool canCast = false;
39 auto chainRet = interface->interfaceChain([&](const hidl_vec<hidl_string> &types) {
40 for (size_t i = 0; i < types.size(); i++) {
41 if (types[i] == castTo) {
42 canCast = true;
43 break;
44 }
45 }
46 });
47
48 if (!chainRet.isOk()) {
49 // call fails, propagate the error if emitError
50 return emitError
51 ? details::StatusOf<void, bool>(chainRet)
52 : Return<bool>(false);
53 }
54
55 return canCast;
56}
57
Steven Moreland1ab46132017-11-03 16:18:19 -070058std::string getDescriptor(IBase* interface) {
Steven Morelandea2fb552019-01-24 18:09:16 -080059 if (interface == nullptr) {
60 return "";
61 }
62
Steven Moreland108d09d2017-05-05 16:15:38 -070063 std::string myDescriptor{};
64 auto ret = interface->interfaceDescriptor([&](const hidl_string &types) {
65 myDescriptor = types.c_str();
66 });
67 ret.isOk(); // ignored, return empty string if not isOk()
68 return myDescriptor;
69}
70
71} // namespace details
72} // namespace hardware
73} // namespace android