| Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "Interface.h" |
| 18 | |
| Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 20 | #include "ArrayType.h" |
| 21 | #include "ConstantExpression.h" |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 22 | #include "DeathRecipientType.h" |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | #include "Method.h" |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 24 | #include "ScalarType.h" |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 25 | #include "StringType.h" |
| 26 | #include "VectorType.h" |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 30 | #include <iostream> |
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 31 | #include <memory> |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 32 | #include <sstream> |
| Neel Mehta | 69920a6 | 2019-07-22 16:22:13 -0700 | [diff] [blame] | 33 | #include <string> |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 35 | |
| Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 36 | #include <android-base/logging.h> |
| Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 37 | #include <hidl-util/Formatter.h> |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 38 | #include <hidl-util/StringHelper.h> |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 39 | |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
| Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 42 | #define B_PACK_CHARS(c1, c2, c3, c4) \ |
| 43 | ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) |
| 44 | |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 45 | /* It is very important that these values NEVER change. These values |
| 46 | * must remain unchanged over the lifetime of android. This is |
| 47 | * because the framework on a device will be updated independently of |
| 48 | * the hals on a device. If the hals are compiled with one set of |
| 49 | * transaction values, and the framework with another, then the |
| 50 | * interface between them will be destroyed, and the device will not |
| 51 | * work. |
| 52 | */ |
| 53 | enum { |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 54 | /////////////////// User defined transactions |
| 55 | FIRST_CALL_TRANSACTION = 0x00000001, |
| Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 56 | LAST_CALL_TRANSACTION = 0x0effffff, |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 57 | /////////////////// HIDL reserved |
| Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 58 | FIRST_HIDL_TRANSACTION = 0x0f000000, |
| 59 | HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'), |
| 60 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'), |
| 61 | HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'), |
| 62 | HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'), |
| 63 | HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'), |
| 64 | HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'), |
| 65 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'), |
| 66 | HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'), |
| 67 | HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'), |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 68 | HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'), |
| Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 69 | LAST_HIDL_TRANSACTION = 0x0fffffff, |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| Steven Moreland | 7794369 | 2018-08-09 12:53:42 -0700 | [diff] [blame] | 72 | const std::unique_ptr<ConstantExpression> Interface::FLAG_ONE_WAY = |
| 73 | std::make_unique<LiteralConstantExpression>(ScalarType::KIND_UINT32, 0x01, "oneway"); |
| 74 | |
| Neel Mehta | 69920a6 | 2019-07-22 16:22:13 -0700 | [diff] [blame] | 75 | Interface::Interface(const std::string& localName, const FQName& fullName, const Location& location, |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 76 | Scope* parent, const Reference<Type>& superType, const Hash* fileHash) |
| 77 | : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {} |
| Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 78 | |
| Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 79 | std::string Interface::typeName() const { |
| Neel Mehta | 257cce0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 80 | return "interface " + definedName(); |
| Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 83 | const Hash* Interface::getFileHash() const { |
| 84 | return mFileHash; |
| 85 | } |
| 86 | |
| Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 87 | bool Interface::fillPingMethod(Method *method) const { |
| 88 | if (method->name() != "ping") { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | method->fillImplementation( |
| 93 | HIDL_PING_TRANSACTION, |
| 94 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 95 | {IMPL_INTERFACE, |
| Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 96 | [](auto &out) { |
| 97 | out << "return ::android::hardware::Void();\n"; |
| 98 | } |
| 99 | }, |
| 100 | {IMPL_STUB_IMPL, |
| 101 | [](auto &out) { |
| 102 | out << "return ::android::hardware::Void();\n"; |
| 103 | } |
| 104 | } |
| 105 | }, /*cppImpl*/ |
| 106 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 107 | {IMPL_INTERFACE, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 108 | [](auto &out) { |
| Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 109 | out << "return;\n"; |
| 110 | } |
| 111 | }, |
| Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 112 | } /*javaImpl*/ |
| 113 | ); |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 118 | bool Interface::fillLinkToDeathMethod(Method *method) const { |
| 119 | if (method->name() != "linkToDeath") { |
| 120 | return false; |
| 121 | } |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 122 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 123 | method->fillImplementation( |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 124 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 125 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 126 | {IMPL_INTERFACE, |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 127 | [](auto &out) { |
| 128 | out << "(void)cookie;\n" |
| 129 | << "return (recipient != nullptr);\n"; |
| 130 | } |
| 131 | }, |
| 132 | {IMPL_PROXY, |
| 133 | [](auto &out) { |
| Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 134 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 135 | out << "::android::hardware::hidl_binder_death_recipient *binder_recipient" |
| 136 | << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n" |
| 137 | << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 138 | << "_hidl_mDeathRecipients.push_back(binder_recipient);\n" |
| 139 | << "return (remote()->linkToDeath(binder_recipient)" |
| 140 | << " == ::android::OK);\n"; |
| 141 | } |
| 142 | }, |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 143 | {IMPL_STUB, nullptr} |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 144 | }, /*cppImpl*/ |
| 145 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 146 | {IMPL_INTERFACE, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 147 | [](auto &out) { |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 148 | out << "return true;\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 149 | } |
| 150 | }, |
| 151 | {IMPL_PROXY, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 152 | [](auto &out) { |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 153 | out << "return mRemote.linkToDeath(recipient, cookie);\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 154 | } |
| 155 | }, |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 156 | {IMPL_STUB, nullptr} |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 157 | } /*javaImpl*/ |
| 158 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 159 | return true; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 162 | bool Interface::fillUnlinkToDeathMethod(Method *method) const { |
| 163 | if (method->name() != "unlinkToDeath") { |
| 164 | return false; |
| 165 | } |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 166 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 167 | method->fillImplementation( |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 168 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
| 169 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 170 | {IMPL_INTERFACE, |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 171 | [](auto &out) { |
| 172 | out << "return (recipient != nullptr);\n"; |
| 173 | } |
| 174 | }, |
| 175 | {IMPL_PROXY, |
| 176 | [](auto &out) { |
| 177 | out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| Steven Moreland | ba140f4 | 2018-07-24 13:13:15 -0700 | [diff] [blame] | 178 | << "for (auto it = _hidl_mDeathRecipients.rbegin();" |
| 179 | << "it != _hidl_mDeathRecipients.rend();" |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 180 | << "++it) {\n"; |
| 181 | out.indent([&] { |
| 182 | out.sIf("(*it)->getRecipient() == recipient", [&] { |
| 183 | out << "::android::status_t status = remote()->unlinkToDeath(*it);\n" |
| Steven Moreland | ba140f4 | 2018-07-24 13:13:15 -0700 | [diff] [blame] | 184 | << "_hidl_mDeathRecipients.erase(it.base()-1);\n" |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 185 | << "return status == ::android::OK;\n"; |
| 186 | }); |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 187 | }).endl(); |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 188 | out << "}\n"; |
| 189 | out << "return false;\n"; |
| 190 | } |
| 191 | }, |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 192 | {IMPL_STUB, nullptr /* don't generate code */} |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 193 | }, /*cppImpl*/ |
| 194 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 195 | {IMPL_INTERFACE, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 196 | [](auto &out) { |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 197 | out << "return true;\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 198 | } |
| 199 | }, |
| 200 | {IMPL_PROXY, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 201 | [](auto &out) { |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 202 | out << "return mRemote.unlinkToDeath(recipient);\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 203 | } |
| 204 | }, |
| Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 205 | {IMPL_STUB, nullptr /* don't generate code */} |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 206 | } /*javaImpl*/ |
| 207 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 208 | return true; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 209 | } |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 210 | bool Interface::fillSyspropsChangedMethod(Method *method) const { |
| 211 | if (method->name() != "notifySyspropsChanged") { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | method->fillImplementation( |
| Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 216 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 217 | { { IMPL_INTERFACE, [](auto &out) { |
| Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 218 | out << "::android::report_sysprop_change();\n"; |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 219 | out << "return ::android::hardware::Void();\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 220 | } } }, /*cppImpl */ |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 221 | { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */ |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 222 | out << "android.os.HwBinder.enableInstrumentation();\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 223 | } } } /*javaImpl */ |
| Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 224 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 225 | return true; |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 228 | bool Interface::fillSetHALInstrumentationMethod(Method *method) const { |
| 229 | if (method->name() != "setHALInstrumentation") { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | method->fillImplementation( |
| Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 234 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
| 235 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 236 | {IMPL_INTERFACE, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 237 | [](auto &out) { |
| Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 238 | // do nothing for base class. |
| 239 | out << "return ::android::hardware::Void();\n"; |
| 240 | } |
| 241 | }, |
| Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 242 | {IMPL_STUB, |
| 243 | [](auto &out) { |
| 244 | out << "configureInstrumentation();\n"; |
| 245 | } |
| 246 | }, |
| 247 | {IMPL_PASSTHROUGH, |
| 248 | [](auto &out) { |
| 249 | out << "configureInstrumentation();\n"; |
| 250 | out << "return ::android::hardware::Void();\n"; |
| 251 | } |
| 252 | }, |
| 253 | }, /*cppImpl */ |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 254 | { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */ |
| Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 255 | // Not support for Java Impl for now. |
| 256 | } } } /*javaImpl */ |
| 257 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 258 | return true; |
| Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 261 | bool Interface::fillDescriptorChainMethod(Method *method) const { |
| 262 | if (method->name() != "interfaceChain") { |
| 263 | return false; |
| 264 | } |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 265 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 266 | method->fillImplementation( |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 267 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 268 | { { IMPL_INTERFACE, [this](auto &out) { |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 269 | std::vector<const Interface *> chain = typeChain(); |
| Yifan Hong | 0393512 | 2016-12-19 11:10:40 -0800 | [diff] [blame] | 270 | out << "_hidl_cb("; |
| 271 | out.block([&] { |
| 272 | for (const Interface *iface : chain) { |
| 273 | out << iface->fullName() << "::descriptor,\n"; |
| 274 | } |
| 275 | }); |
| 276 | out << ");\n"; |
| Steven Moreland | 3768f42 | 2019-05-09 12:29:12 -0700 | [diff] [blame] | 277 | out << "return ::android::hardware::Void();\n"; |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 278 | } } }, /* cppImpl */ |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 279 | { { IMPL_INTERFACE, [this](auto &out) { |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 280 | std::vector<const Interface *> chain = typeChain(); |
| Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 281 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 282 | out.indent(); out.indent(); |
| 283 | for (size_t i = 0; i < chain.size(); ++i) { |
| 284 | if (i != 0) |
| 285 | out << ",\n"; |
| Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 286 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 287 | } |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 288 | out << "));\n"; |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 289 | out.unindent(); out.unindent(); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 290 | } } } /* javaImpl */ |
| Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 291 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 292 | return true; |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 295 | void Interface::emitDigestChain( |
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 296 | Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain, |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 297 | std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const { |
| 298 | out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) { |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 299 | out << prefix; |
| 300 | out << "{"; |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 301 | out.join( |
| 302 | iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",", |
| 303 | [&](const auto& e) { |
| 304 | // Use ConstantExpression::cppValue / javaValue |
| 305 | // because Java used signed byte for uint8_t. |
| 306 | out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e)); |
| 307 | }); |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 308 | out << "} /* "; |
| Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 309 | out << iface->getFileHash()->hexString(); |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 310 | out << " */"; |
| 311 | }); |
| 312 | } |
| 313 | |
| 314 | bool Interface::fillHashChainMethod(Method *method) const { |
| 315 | if (method->name() != "getHashChain") { |
| 316 | return false; |
| 317 | } |
| 318 | const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type()); |
| 319 | const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType()); |
| 320 | |
| 321 | method->fillImplementation( |
| 322 | HIDL_HASH_CHAIN_TRANSACTION, |
| 323 | { { IMPL_INTERFACE, [this, digestType](auto &out) { |
| 324 | std::vector<const Interface *> chain = typeChain(); |
| 325 | out << "_hidl_cb("; |
| 326 | out.block([&] { |
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 327 | emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain, |
| 328 | [](const auto& e) { return e->cppValue(); }); |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 329 | }); |
| 330 | out << ");\n"; |
| 331 | out << "return ::android::hardware::Void();\n"; |
| 332 | } } }, /* cppImpl */ |
| 333 | { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) { |
| 334 | std::vector<const Interface *> chain = typeChain(); |
| 335 | out << "return new " |
| 336 | << chainType->getJavaType(false /* forInitializer */) |
| 337 | << "(java.util.Arrays.asList(\n"; |
| 338 | out.indent(2, [&] { |
| 339 | // No need for dimensions when elements are explicitly provided. |
| 340 | emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */), |
| Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 341 | chain, [](const auto& e) { return e->javaValue(); }); |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 342 | }); |
| 343 | out << "));\n"; |
| 344 | } } } /* javaImpl */ |
| 345 | ); |
| 346 | return true; |
| 347 | } |
| 348 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 349 | bool Interface::fillGetDescriptorMethod(Method *method) const { |
| 350 | if (method->name() != "interfaceDescriptor") { |
| 351 | return false; |
| 352 | } |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 353 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 354 | method->fillImplementation( |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 355 | HIDL_GET_DESCRIPTOR_TRANSACTION, |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 356 | { { IMPL_INTERFACE, [this](auto &out) { |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 357 | out << "_hidl_cb(" |
| 358 | << fullName() |
| 359 | << "::descriptor);\n" |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 360 | << "return ::android::hardware::Void();\n"; |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 361 | } } }, /* cppImpl */ |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 362 | { { IMPL_INTERFACE, [this](auto &out) { |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 363 | out << "return " |
| 364 | << fullJavaName() |
| 365 | << ".kInterfaceName;\n"; |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 366 | } } } /* javaImpl */ |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 367 | ); |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 368 | return true; |
| Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 369 | } |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 370 | |
| Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 371 | bool Interface::fillGetDebugInfoMethod(Method *method) const { |
| 372 | if (method->name() != "getDebugInfo") { |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
| Yifan Hong | df1ea3f | 2017-03-02 17:02:10 -0800 | [diff] [blame] | 376 | static const std::string sArch = |
| 377 | "#if defined(__LP64__)\n" |
| 378 | "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n" |
| 379 | "#else\n" |
| 380 | "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n" |
| 381 | "#endif\n"; |
| 382 | |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 383 | method->fillImplementation( |
| 384 | HIDL_GET_REF_INFO_TRANSACTION, |
| 385 | { |
| Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 386 | {IMPL_INTERFACE, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 387 | [](auto &out) { |
| Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 388 | // getDebugInfo returns N/A for local objects. |
| Steven Moreland | 523b12c | 2019-04-23 16:03:18 -0700 | [diff] [blame] | 389 | out << "::android::hidl::base::V1_0::DebugInfo info = {};\n"; |
| 390 | out << "info.pid = -1;\n"; |
| 391 | out << "info.ptr = 0;\n"; |
| 392 | out << "info.arch = \n" << sArch << ";\n"; |
| 393 | out << "_hidl_cb(info);\n"; |
| 394 | out << "return ::android::hardware::Void();\n"; |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 395 | } |
| 396 | }, |
| 397 | {IMPL_STUB_IMPL, |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 398 | [](auto &out) { |
| Steven Moreland | 523b12c | 2019-04-23 16:03:18 -0700 | [diff] [blame] | 399 | out << "::android::hidl::base::V1_0::DebugInfo info = {};\n"; |
| 400 | out << "info.pid = ::android::hardware::details::getPidIfSharable();\n"; |
| 401 | out << "info.ptr = ::android::hardware::details::debuggable()" |
| 402 | << "? reinterpret_cast<uint64_t>(this) : 0;\n"; |
| 403 | out << "info.arch = \n" << sArch << ";\n"; |
| 404 | out << "_hidl_cb(info);\n"; |
| 405 | out << "return ::android::hardware::Void();\n"; |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | }, /* cppImpl */ |
| Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 409 | { { IMPL_INTERFACE, [method](auto &out) { |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 410 | const Type &refInfo = method->results().front()->type(); |
| 411 | out << refInfo.getJavaType(false /* forInitializer */) << " info = new " |
| 412 | << refInfo.getJavaType(true /* forInitializer */) << "();\n" |
| Yifan Hong | 96bb063 | 2017-11-14 16:08:37 -0800 | [diff] [blame] | 413 | << "info.pid = android.os.HidlSupport.getPidIfSharable();\n" |
| Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 414 | << "info.ptr = 0;\n" |
| Yifan Hong | 96bb063 | 2017-11-14 16:08:37 -0800 | [diff] [blame] | 415 | << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n" |
| Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 416 | << "return info;\n"; |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 417 | } } } /* javaImpl */ |
| 418 | ); |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
| Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 423 | bool Interface::fillDebugMethod(Method *method) const { |
| 424 | if (method->name() != "debug") { |
| 425 | return false; |
| 426 | } |
| 427 | |
| Steven Moreland | 327fd8b | 2018-08-10 15:40:41 -0700 | [diff] [blame] | 428 | method->fillImplementation(HIDL_DEBUG_TRANSACTION, |
| 429 | { |
| 430 | {IMPL_INTERFACE, |
| 431 | [](auto& out) { |
| 432 | out << "(void)fd;\n" |
| 433 | << "(void)options;\n" |
| 434 | << "return ::android::hardware::Void();\n"; |
| 435 | }}, |
| 436 | }, /* cppImpl */ |
| 437 | { |
| 438 | {IMPL_INTERFACE, [](auto& out) { out << "return;\n"; }}, |
| 439 | } /* javaImpl */ |
| Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 440 | ); |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 445 | void Interface::addUserDefinedMethod(Method* method) { |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 446 | CHECK(!method->isHidlReserved()); |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 447 | mUserMethods.push_back(method); |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 450 | std::vector<const Reference<Type>*> Interface::getReferences() const { |
| 451 | std::vector<const Reference<Type>*> ret; |
| Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 452 | |
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 453 | if (!isIBase()) { |
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 454 | ret.push_back(&mSuperType); |
| Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | for (const auto* method : methods()) { |
| 458 | const auto& references = method->getReferences(); |
| 459 | ret.insert(ret.end(), references.begin(), references.end()); |
| 460 | } |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 465 | std::vector<const Reference<Type>*> Interface::getStrongReferences() const { |
| Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 466 | // Interface is a special case as a reference: |
| 467 | // its definiton must be completed for extension but |
| 468 | // not necessary for other references. |
| Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 469 | |
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 470 | std::vector<const Reference<Type>*> ret; |
| Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 471 | if (!isIBase()) { |
| Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 472 | ret.push_back(&mSuperType); |
| Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 473 | } |
| Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 474 | |
| 475 | for (const auto* method : methods()) { |
| 476 | const auto& references = method->getStrongReferences(); |
| 477 | ret.insert(ret.end(), references.begin(), references.end()); |
| 478 | } |
| 479 | |
| Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 480 | return ret; |
| 481 | } |
| 482 | |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 483 | status_t Interface::resolveInheritance() { |
| 484 | size_t serial = FIRST_CALL_TRANSACTION; |
| 485 | for (const auto* ancestor : superTypeChain()) { |
| 486 | serial += ancestor->mUserMethods.size(); |
| 487 | } |
| 488 | |
| 489 | for (Method* method : mUserMethods) { |
| 490 | if (serial > LAST_CALL_TRANSACTION) { |
| 491 | std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION |
| 492 | << " methods (including super and reserved) are not allowed at " << location() |
| Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 493 | << std::endl; |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 494 | return UNKNOWN_ERROR; |
| 495 | } |
| 496 | |
| 497 | method->setSerialId(serial); |
| 498 | serial++; |
| 499 | } |
| 500 | |
| 501 | return Scope::resolveInheritance(); |
| 502 | } |
| 503 | |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 504 | status_t Interface::validate() const { |
| 505 | CHECK(isIBase() == mSuperType.isEmptyReference()); |
| 506 | |
| Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 507 | if (!isIBase() && !mSuperType->isInterface()) { |
| Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 508 | std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location() |
| 509 | << std::endl; |
| Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 510 | return UNKNOWN_ERROR; |
| 511 | } |
| 512 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 513 | status_t err; |
| 514 | |
| 515 | err = validateUniqueNames(); |
| 516 | if (err != OK) return err; |
| 517 | |
| 518 | err = validateAnnotations(); |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 519 | if (err != OK) return err; |
| 520 | |
| 521 | return Scope::validate(); |
| 522 | } |
| 523 | |
| Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 524 | void Interface::getAlignmentAndSize(size_t* align, size_t* size) const { |
| 525 | *align = 8; |
| 526 | *size = 8; |
| 527 | } |
| 528 | |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 529 | status_t Interface::validateUniqueNames() const { |
| 530 | std::unordered_map<std::string, const Interface*> registeredMethodNames; |
| 531 | for (auto const& tuple : allSuperMethodsFromRoot()) { |
| 532 | // No need to check super method uniqueness |
| 533 | registeredMethodNames[tuple.method()->name()] = tuple.interface(); |
| 534 | } |
| 535 | |
| 536 | for (const Method* method : mUserMethods) { |
| 537 | auto registered = registeredMethodNames.find(method->name()); |
| 538 | |
| 539 | if (registered != registeredMethodNames.end()) { |
| 540 | const Interface* definedInType = registered->second; |
| 541 | |
| 542 | if (definedInType == this) { |
| 543 | // Defined in this interface |
| 544 | std::cerr << "ERROR: Redefinition of method '" << method->name() << "'"; |
| 545 | } else if (definedInType->isIBase()) { |
| 546 | // Defined in IBase |
| 547 | std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'"; |
| 548 | } else { |
| 549 | // Defined in super not IBase |
| 550 | std::cerr << "ERROR: Redefinition of method '" << method->name() |
| Timur Iskhakov | 4c9c20b | 2017-09-13 21:16:59 -0700 | [diff] [blame] | 551 | << "' defined in interface '" << definedInType->fullName() << "'"; |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 552 | } |
| Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 553 | std::cerr << " at " << method->location() << std::endl; |
| Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 554 | return UNKNOWN_ERROR; |
| 555 | } |
| 556 | |
| 557 | registeredMethodNames[method->name()] = this; |
| 558 | } |
| 559 | |
| 560 | return OK; |
| 561 | } |
| 562 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 563 | status_t Interface::validateAnnotations() const { |
| 564 | for (const Method* method : methods()) { |
| 565 | for (const Annotation* annotation : method->annotations()) { |
| 566 | const std::string name = annotation->name(); |
| 567 | |
| 568 | if (name == "entry" || name == "exit" || name == "callflow") { |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | std::cerr << "ERROR: Unrecognized annotation '" << name |
| 573 | << "' for method: " << method->name() << ". An annotation should be one of: " |
| 574 | << "entry, exit, callflow." << std::endl; |
| 575 | return UNKNOWN_ERROR; |
| 576 | } |
| 577 | } |
| 578 | return OK; |
| 579 | } |
| 580 | |
| Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 581 | bool Interface::addAllReservedMethods(const std::map<std::string, Method*>& allReservedMethods) { |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 582 | // use a sorted map to insert them in serial ID order. |
| 583 | std::map<int32_t, Method *> reservedMethodsById; |
| Neel Mehta | 0ee353f | 2019-05-30 17:40:29 -0700 | [diff] [blame] | 584 | for (const auto& pair : allReservedMethods) { |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 585 | Method *method = pair.second->copySignature(); |
| Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 586 | bool fillSuccess = fillPingMethod(method) |
| 587 | || fillDescriptorChainMethod(method) |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 588 | || fillGetDescriptorMethod(method) |
| Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 589 | || fillHashChainMethod(method) |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 590 | || fillSyspropsChangedMethod(method) |
| 591 | || fillLinkToDeathMethod(method) |
| 592 | || fillUnlinkToDeathMethod(method) |
| Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 593 | || fillSetHALInstrumentationMethod(method) |
| Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 594 | || fillGetDebugInfoMethod(method) |
| 595 | || fillDebugMethod(method); |
| 596 | |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 597 | if (!fillSuccess) { |
| Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 598 | std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name() |
| 599 | << std::endl; |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | if (!reservedMethodsById.emplace(method->getSerialId(), method).second) { |
| Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 603 | std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name() |
| 604 | << " and " << reservedMethodsById[method->getSerialId()]->name() |
| 605 | << ", serialId = " << method->getSerialId() << std::endl; |
| Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 606 | return false; |
| 607 | } |
| 608 | } |
| 609 | for (const auto &pair : reservedMethodsById) { |
| 610 | this->mReservedMethods.push_back(pair.second); |
| 611 | } |
| 612 | return true; |
| 613 | } |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 614 | |
| Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 615 | const Interface* Interface::superType() const { |
| Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 616 | if (isIBase()) return nullptr; |
| 617 | if (!mSuperType->isInterface()) { |
| 618 | // This is actually an error |
| 619 | // that would be caught in validate |
| 620 | return nullptr; |
| 621 | } |
| Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 622 | return static_cast<const Interface*>(mSuperType.get()); |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 625 | std::vector<const Interface *> Interface::typeChain() const { |
| 626 | std::vector<const Interface *> v; |
| 627 | const Interface *iface = this; |
| 628 | while (iface != nullptr) { |
| 629 | v.push_back(iface); |
| Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 630 | iface = iface->superType(); |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 631 | } |
| 632 | return v; |
| 633 | } |
| 634 | |
| Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 635 | std::vector<const Interface *> Interface::superTypeChain() const { |
| Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 636 | return isIBase() ? std::vector<const Interface*>() : superType()->typeChain(); |
| Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| Martijn Coenen | b40ef02 | 2017-01-02 15:21:46 +0100 | [diff] [blame] | 639 | bool Interface::isElidableType() const { |
| 640 | return true; |
| 641 | } |
| 642 | |
| Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 643 | bool Interface::isInterface() const { |
| 644 | return true; |
| 645 | } |
| 646 | |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 647 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 648 | return mUserMethods; |
| 649 | } |
| 650 | |
| 651 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 652 | return mReservedMethods; |
| 653 | } |
| 654 | |
| 655 | std::vector<Method *> Interface::methods() const { |
| 656 | std::vector<Method *> v(mUserMethods); |
| 657 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 658 | return v; |
| 659 | } |
| 660 | |
| 661 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 662 | std::vector<InterfaceAndMethod> v; |
| 663 | std::vector<const Interface *> chain = typeChain(); |
| 664 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 665 | const Interface *iface = *it; |
| 666 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 667 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 668 | } |
| 669 | } |
| 670 | for (Method *reservedMethod : hidlReservedMethods()) { |
| Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 671 | v.push_back(InterfaceAndMethod( |
| 672 | *chain.rbegin(), // IBase |
| 673 | reservedMethod)); |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 674 | } |
| 675 | return v; |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| Timur Iskhakov | f1b902d | 2017-08-13 20:14:31 -0700 | [diff] [blame] | 678 | std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const { |
| 679 | return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot(); |
| 680 | } |
| 681 | |
| Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 682 | std::string Interface::getBaseName() const { |
| Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 683 | return fqName().getInterfaceBaseName(); |
| Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 686 | std::string Interface::getAdapterName() const { |
| 687 | return fqName().getInterfaceAdapterName(); |
| 688 | } |
| 689 | |
| Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 690 | std::string Interface::getProxyName() const { |
| 691 | return fqName().getInterfaceProxyName(); |
| 692 | } |
| 693 | |
| 694 | std::string Interface::getStubName() const { |
| 695 | return fqName().getInterfaceStubName(); |
| 696 | } |
| 697 | |
| 698 | std::string Interface::getHwName() const { |
| 699 | return fqName().getInterfaceHwName(); |
| 700 | } |
| 701 | |
| 702 | std::string Interface::getPassthroughName() const { |
| 703 | return fqName().getInterfacePassthroughName(); |
| 704 | } |
| 705 | |
| Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 706 | FQName Interface::getProxyFqName() const { |
| Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 707 | return fqName().getInterfaceProxyFqName(); |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 710 | FQName Interface::getStubFqName() const { |
| Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 711 | return fqName().getInterfaceStubFqName(); |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 714 | FQName Interface::getPassthroughFqName() const { |
| Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 715 | return fqName().getInterfacePassthroughFqName(); |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 718 | std::string Interface::getCppType(StorageMode mode, |
| Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 719 | bool specifyNamespaces) const { |
| Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 720 | const std::string base = |
| 721 | std::string(specifyNamespaces ? "::android::" : "") |
| 722 | + "sp<" |
| Steven Moreland | e30ee9b | 2017-05-09 13:31:01 -0700 | [diff] [blame] | 723 | + fullName() |
| Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 724 | + ">"; |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 725 | |
| 726 | switch (mode) { |
| 727 | case StorageMode_Stack: |
| 728 | case StorageMode_Result: |
| 729 | return base; |
| 730 | |
| 731 | case StorageMode_Argument: |
| 732 | return "const " + base + "&"; |
| 733 | } |
| 734 | } |
| 735 | |
| Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 736 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
| Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 737 | return fullJavaName(); |
| 738 | } |
| 739 | |
| Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 740 | std::string Interface::getVtsType() const { |
| Neel Mehta | 257cce0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 741 | if (StringHelper::EndsWith(definedName(), "Callback")) { |
| Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 742 | return "TYPE_HIDL_CALLBACK"; |
| 743 | } else { |
| 744 | return "TYPE_HIDL_INTERFACE"; |
| 745 | } |
| 746 | } |
| 747 | |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 748 | void Interface::emitReaderWriter( |
| 749 | Formatter &out, |
| 750 | const std::string &name, |
| 751 | const std::string &parcelObj, |
| 752 | bool parcelObjIsPointer, |
| 753 | bool isReader, |
| 754 | ErrorMode mode) const { |
| 755 | const std::string parcelObjDeref = |
| 756 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 757 | |
| 758 | if (isReader) { |
| Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 759 | out << "{\n"; |
| 760 | out.indent(); |
| 761 | |
| Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 762 | const std::string binderName = "_hidl_binder"; |
| Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 763 | out << "::android::sp<::android::hardware::IBinder> " |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 764 | << binderName << ";\n"; |
| 765 | |
| Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 766 | out << "_hidl_err = "; |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 767 | out << parcelObjDeref |
| 768 | << "readNullableStrongBinder(&" |
| 769 | << binderName |
| 770 | << ");\n"; |
| 771 | |
| 772 | handleError(out, mode); |
| 773 | |
| 774 | out << name |
| 775 | << " = " |
| Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 776 | << "::android::hardware::fromBinder<" |
| 777 | << fqName().cppName() |
| 778 | << "," |
| Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 779 | << getProxyFqName().cppName() |
| Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 780 | << "," |
| Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 781 | << getStubFqName().cppName() |
| Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 782 | << ">(" |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 783 | << binderName |
| 784 | << ");\n"; |
| Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 785 | |
| 786 | out.unindent(); |
| 787 | out << "}\n\n"; |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 788 | } else { |
| Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 789 | out << "if (" << name << " == nullptr) {\n"; |
| 790 | out.indent(); |
| 791 | out << "_hidl_err = "; |
| 792 | out << parcelObjDeref |
| 793 | << "writeStrongBinder(nullptr);\n"; |
| 794 | out.unindent(); |
| 795 | out << "} else {\n"; |
| 796 | out.indent(); |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 797 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| Steven Moreland | 731dbb1 | 2018-12-03 18:09:46 -0800 | [diff] [blame] | 798 | << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n"; |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 799 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
| Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 800 | out.indent([&] { |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 801 | out << "_hidl_err = " |
| 802 | << parcelObjDeref |
| 803 | << "writeStrongBinder(_hidl_binder);\n"; |
| 804 | }); |
| 805 | out << "} else {\n"; |
| Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 806 | out.indent([&] { |
| Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 807 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 808 | }); |
| 809 | out << "}\n"; |
| Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 810 | out.unindent(); |
| 811 | out << "}\n"; |
| Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 812 | |
| Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 813 | handleError(out, mode); |
| 814 | } |
| 815 | } |
| 816 | |
| Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 817 | void Interface::emitHidlDefinition(Formatter& out) const { |
| 818 | if (getDocComment() != nullptr) getDocComment()->emit(out); |
| 819 | out << typeName() << " "; |
| 820 | |
| 821 | const Interface* super = superType(); |
| 822 | if (super != nullptr && !super->isIBase()) { |
| 823 | out << "extends " << super->fqName().getRelativeFQName(fqName()) << " "; |
| 824 | } |
| 825 | |
| Neel Mehta | c554d42 | 2019-08-09 16:57:02 -0700 | [diff] [blame] | 826 | out << "{"; |
| Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 827 | |
| 828 | out.indent([&] { |
| 829 | const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes(); |
| Neel Mehta | c554d42 | 2019-08-09 16:57:02 -0700 | [diff] [blame] | 830 | if (definedTypes.size() > 0 || userDefinedMethods().size() > 0) out << "\n"; |
| 831 | |
| Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 832 | out.join(definedTypes.begin(), definedTypes.end(), "\n", |
| 833 | [&](auto t) { t->emitHidlDefinition(out); }); |
| 834 | |
| 835 | if (definedTypes.size() > 0 && userDefinedMethods().size() > 0) out << "\n"; |
| 836 | |
| 837 | out.join(userDefinedMethods().begin(), userDefinedMethods().end(), "\n", |
| 838 | [&](auto method) { method->emitHidlDefinition(out); }); |
| 839 | }); |
| 840 | |
| 841 | out << "};\n"; |
| 842 | } |
| 843 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 844 | void Interface::emitPackageTypeDeclarations(Formatter& out) const { |
| 845 | Scope::emitPackageTypeDeclarations(out); |
| Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 846 | |
| Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 847 | out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n"; |
| 848 | } |
| 849 | |
| 850 | void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const { |
| 851 | Scope::emitPackageTypeHeaderDefinitions(out); |
| 852 | |
| Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 853 | out << "static inline std::string toString(" << getCppArgumentType() << " o) "; |
| 854 | |
| 855 | out.block([&] { |
| 856 | out << "std::string os = \"[class or subclass of \";\n" |
| 857 | << "os += " << fullName() << "::descriptor;\n" |
| 858 | << "os += \"]\";\n" |
| 859 | << "os += o->isRemote() ? \"@remote\" : \"@local\";\n" |
| 860 | << "return os;\n"; |
| 861 | }).endl().endl(); |
| Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 864 | void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const { |
| Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 865 | std::string space = prefix.empty() ? "" : (prefix + "::"); |
| Steven Moreland | d8c7a29 | 2017-10-27 18:32:06 -0700 | [diff] [blame] | 866 | |
| Neel Mehta | 257cce0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 867 | Scope::emitTypeDefinitions(out, space + definedName()); |
| Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 870 | void Interface::emitJavaReaderWriter( |
| 871 | Formatter &out, |
| 872 | const std::string &parcelObj, |
| 873 | const std::string &argName, |
| 874 | bool isReader) const { |
| 875 | if (isReader) { |
| 876 | out << fullJavaName() |
| 877 | << ".asInterface(" |
| 878 | << parcelObj |
| 879 | << ".readStrongBinder());\n"; |
| 880 | } else { |
| 881 | out << parcelObj |
| 882 | << ".writeStrongBinder(" |
| 883 | << argName |
| 884 | << " == null ? null : " |
| 885 | << argName |
| 886 | << ".asBinder());\n"; |
| 887 | } |
| 888 | } |
| 889 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 890 | void Interface::emitVtsAttributeDeclaration(Formatter& out) const { |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 891 | for (const auto &type : getSubTypes()) { |
| Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 892 | // Skip for TypeDef as it is just an alias of a defined type. |
| 893 | if (type->isTypeDef()) { |
| 894 | continue; |
| 895 | } |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 896 | out << "attribute: {\n"; |
| 897 | out.indent(); |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 898 | type->emitVtsTypeDeclarations(out); |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 899 | out.unindent(); |
| 900 | out << "}\n\n"; |
| 901 | } |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| Zhuoyao Zhang | e59c933 | 2018-07-20 14:16:04 -0700 | [diff] [blame] | 904 | void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const { |
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 905 | for (const auto &method : methods()) { |
| Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 906 | if (method->isHidlReserved()) { |
| 907 | continue; |
| 908 | } |
| 909 | |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 910 | out << "api: {\n"; |
| 911 | out.indent(); |
| 912 | out << "name: \"" << method->name() << "\"\n"; |
| Zhuoyao Zhang | e59c933 | 2018-07-20 14:16:04 -0700 | [diff] [blame] | 913 | out << "is_inherited: " << (isInherited ? "true" : "false") << "\n"; |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 914 | // Generate declaration for each return value. |
| 915 | for (const auto &result : method->results()) { |
| 916 | out << "return_type_hidl: {\n"; |
| 917 | out.indent(); |
| Socrates Li | 7b0a42b | 2018-07-16 13:23:17 -0700 | [diff] [blame] | 918 | out << "name: \"" << result->name() << "\"\n"; |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 919 | result->type().emitVtsAttributeType(out); |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 920 | out.unindent(); |
| 921 | out << "}\n"; |
| 922 | } |
| 923 | // Generate declaration for each input argument |
| 924 | for (const auto &arg : method->args()) { |
| 925 | out << "arg: {\n"; |
| 926 | out.indent(); |
| Socrates Li | 7b0a42b | 2018-07-16 13:23:17 -0700 | [diff] [blame] | 927 | out << "name: \"" << arg->name() << "\"\n"; |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 928 | arg->type().emitVtsAttributeType(out); |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 929 | out.unindent(); |
| 930 | out << "}\n"; |
| 931 | } |
| 932 | // Generate declaration for each annotation. |
| Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 933 | for (const auto &annotation : method->annotations()) { |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 934 | out << "callflow: {\n"; |
| 935 | out.indent(); |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 936 | const std::string name = annotation->name(); |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 937 | if (name == "entry") { |
| 938 | out << "entry: true\n"; |
| 939 | } else if (name == "exit") { |
| 940 | out << "exit: true\n"; |
| 941 | } else if (name == "callflow") { |
| Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 942 | const AnnotationParam *param = |
| 943 | annotation->getParam("next"); |
| 944 | if (param != nullptr) { |
| Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 945 | for (const auto& value : param->getValues()) { |
| Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 946 | out << "next: " << value << "\n"; |
| 947 | } |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 948 | } |
| 949 | } else { |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 950 | CHECK(false); |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 951 | } |
| 952 | out.unindent(); |
| 953 | out << "}\n"; |
| 954 | } |
| 955 | out.unindent(); |
| 956 | out << "}\n\n"; |
| 957 | } |
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 960 | void Interface::emitVtsAttributeType(Formatter& out) const { |
| Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 961 | out << "type: " << getVtsType() << "\n" |
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 962 | << "predefined_type: \"" |
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 963 | << fullName() |
| 964 | << "\"\n"; |
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 967 | bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const { |
| 968 | if (superType() != nullptr && !superType()->isJavaCompatible(visited)) { |
| Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 969 | return false; |
| 970 | } |
| 971 | |
| Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 972 | for (const auto* method : methods()) { |
| 973 | if (!method->deepIsJavaCompatible(visited)) { |
| Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | } |
| 977 | |
| Steven Moreland | b0da6fc | 2018-10-16 17:15:11 -0700 | [diff] [blame] | 978 | return Scope::deepIsJavaCompatible(visited); |
| Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 981 | bool Interface::isNeverStrongReference() const { |
| 982 | return true; |
| 983 | } |
| 984 | |
| Steven Moreland | ad81efd | 2020-02-26 13:15:10 -0800 | [diff] [blame] | 985 | const FQName gIBaseFqName = FQName("android.hidl.base", "1.0", "IBase"); |
| 986 | const FQName gIManagerFqName = FQName("android.hidl.manager", "1.0", "IServiceManager"); |
| 987 | |
| Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 988 | } // namespace android |
| 989 | |