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" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 20 | #include "DeathRecipientType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | #include "Method.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 22 | #include "ScalarType.h" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 23 | #include "StringType.h" |
| 24 | #include "VectorType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 27 | #include <hidl-util/Formatter.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 28 | #include <hidl-util/StringHelper.h> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 29 | #include <iostream> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 30 | #include <sstream> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 31 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 34 | /* It is very important that these values NEVER change. These values |
| 35 | * must remain unchanged over the lifetime of android. This is |
| 36 | * because the framework on a device will be updated independently of |
| 37 | * the hals on a device. If the hals are compiled with one set of |
| 38 | * transaction values, and the framework with another, then the |
| 39 | * interface between them will be destroyed, and the device will not |
| 40 | * work. |
| 41 | */ |
| 42 | enum { |
| 43 | // These values are defined in hardware::IBinder. |
| 44 | /////////////////// User defined transactions |
| 45 | FIRST_CALL_TRANSACTION = 0x00000001, |
| 46 | LAST_CALL_TRANSACTION = 0x00efffff, |
| 47 | /////////////////// HIDL reserved |
| 48 | FIRST_HIDL_TRANSACTION = 0x00f00000, |
| 49 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION, |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 50 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 51 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 52 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame^] | 53 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 54 | LAST_HIDL_TRANSACTION = 0x00ffffff, |
| 55 | }; |
| 56 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 57 | Interface::Interface(const char *localName, const Location &location, Interface *super) |
| 58 | : Scope(localName, location), |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 59 | mSuperType(super), |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 60 | mIsJavaCompatibleInProgress(false) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 61 | mReservedMethods.push_back(createDescriptorChainMethod()); |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 62 | mReservedMethods.push_back(createSyspropsChangedMethod()); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 63 | mReservedMethods.push_back(createLinkToDeathMethod()); |
| 64 | mReservedMethods.push_back(createUnlinkToDeathMethod()); |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame^] | 65 | mReservedMethods.push_back(createSetHALInstrumentationMethod()); |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 68 | std::string Interface::typeName() const { |
| 69 | return "interface " + localName(); |
| 70 | } |
| 71 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 72 | Method *Interface::createLinkToDeathMethod() const { |
| 73 | auto *results = new std::vector<TypedVar *> { |
| 74 | new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) }; |
| 75 | auto *args = new std::vector<TypedVar *> { |
| 76 | new TypedVar("recipient", new DeathRecipientType()), |
| 77 | new TypedVar("cookie", new ScalarType(ScalarType::KIND_UINT64)) }; |
| 78 | |
| 79 | return new Method("linkToDeath", |
| 80 | args, |
| 81 | results, |
| 82 | false /*oneway */, |
| 83 | new std::vector<Annotation *>(), |
| 84 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 85 | { |
| 86 | {IMPL_HEADER, |
| 87 | [](auto &out) { |
| 88 | out << "(void)cookie;\n" |
| 89 | << "return (recipient != nullptr);\n"; |
| 90 | } |
| 91 | }, |
| 92 | {IMPL_PROXY, |
| 93 | [](auto &out) { |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 94 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 95 | out << "::android::hardware::hidl_binder_death_recipient *binder_recipient" |
| 96 | << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n" |
| 97 | << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 98 | << "_hidl_mDeathRecipients.push_back(binder_recipient);\n" |
| 99 | << "return (remote()->linkToDeath(binder_recipient)" |
| 100 | << " == ::android::OK);\n"; |
| 101 | } |
| 102 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 103 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 104 | }, /*cppImpl*/ |
| 105 | { |
| 106 | {IMPL_HEADER, |
| 107 | [this](auto &out) { |
| 108 | out << "return true;"; |
| 109 | } |
| 110 | }, |
| 111 | {IMPL_PROXY, |
| 112 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 113 | out << "return mRemote.linkToDeath(recipient, cookie);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 114 | } |
| 115 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 116 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 117 | } /*javaImpl*/ |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | Method *Interface::createUnlinkToDeathMethod() const { |
| 122 | auto *results = new std::vector<TypedVar *> { |
| 123 | new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) }; |
| 124 | auto *args = new std::vector<TypedVar *> { |
| 125 | new TypedVar("recipient", new DeathRecipientType()) }; |
| 126 | |
| 127 | return new Method("unlinkToDeath", |
| 128 | args, |
| 129 | results, |
| 130 | false /*oneway */, |
| 131 | new std::vector<Annotation *>(), |
| 132 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
| 133 | { |
| 134 | {IMPL_HEADER, |
| 135 | [](auto &out) { |
| 136 | out << "return (recipient != nullptr);\n"; |
| 137 | } |
| 138 | }, |
| 139 | {IMPL_PROXY, |
| 140 | [](auto &out) { |
| 141 | out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 142 | << "for (auto it = _hidl_mDeathRecipients.begin();" |
| 143 | << "it != _hidl_mDeathRecipients.end();" |
| 144 | << "++it) {\n"; |
| 145 | out.indent([&] { |
| 146 | out.sIf("(*it)->getRecipient() == recipient", [&] { |
| 147 | out << "::android::status_t status = remote()->unlinkToDeath(*it);\n" |
| 148 | << "_hidl_mDeathRecipients.erase(it);\n" |
| 149 | << "return status == ::android::OK;\n"; |
| 150 | }); |
| 151 | }); |
| 152 | out << "}\n"; |
| 153 | out << "return false;\n"; |
| 154 | } |
| 155 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 156 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 157 | }, /*cppImpl*/ |
| 158 | { |
| 159 | {IMPL_HEADER, |
| 160 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 161 | out << "return true;\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 162 | } |
| 163 | }, |
| 164 | {IMPL_PROXY, |
| 165 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 166 | out << "return mRemote.unlinkToDeath(recipient);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 167 | } |
| 168 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 169 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 170 | } /*javaImpl*/ |
| 171 | ); |
| 172 | } |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 173 | Method *Interface::createSyspropsChangedMethod() const { |
| 174 | return new Method("notifySyspropsChanged", |
| 175 | new std::vector<TypedVar *>() /*args */, |
| 176 | new std::vector<TypedVar *>() /*results */, |
| 177 | true /*oneway */, |
| 178 | new std::vector<Annotation *>(), |
| 179 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 180 | { { IMPL_HEADER, [this](auto &out) { |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 181 | out << "::android::report_sysprop_change();\n"; |
| 182 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 183 | } } }, /*cppImpl */ |
| 184 | { { IMPL_HEADER, [](auto &out) { /* javaImpl */ |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 185 | out << "android.os.SystemProperties.reportSyspropChanged();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 186 | } } } /*javaImpl */ |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 187 | ); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame^] | 190 | Method *Interface::createSetHALInstrumentationMethod() const { |
| 191 | return new Method("setHALInstrumentation", |
| 192 | new std::vector<TypedVar *>() /*args */, |
| 193 | new std::vector<TypedVar *>() /*results */, |
| 194 | true /*oneway */, |
| 195 | new std::vector<Annotation *>(), |
| 196 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
| 197 | { |
| 198 | {IMPL_HEADER, |
| 199 | [this](auto &out) { |
| 200 | // do nothing for base class. |
| 201 | out << "return ::android::hardware::Void();\n"; |
| 202 | } |
| 203 | }, |
| 204 | {IMPL_PROXY, |
| 205 | [](auto &out) { |
| 206 | out << "configureInstrumentation();\n"; |
| 207 | out << "return ::android::hardware::Void();\n"; |
| 208 | } |
| 209 | }, |
| 210 | {IMPL_STUB, |
| 211 | [](auto &out) { |
| 212 | out << "configureInstrumentation();\n"; |
| 213 | } |
| 214 | }, |
| 215 | {IMPL_PASSTHROUGH, |
| 216 | [](auto &out) { |
| 217 | out << "configureInstrumentation();\n"; |
| 218 | out << "return ::android::hardware::Void();\n"; |
| 219 | } |
| 220 | }, |
| 221 | }, /*cppImpl */ |
| 222 | { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */ |
| 223 | // Not support for Java Impl for now. |
| 224 | } } } /*javaImpl */ |
| 225 | ); |
| 226 | } |
| 227 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 228 | Method *Interface::createDescriptorChainMethod() const { |
| 229 | VectorType *vecType = new VectorType(); |
| 230 | vecType->setElementType(new StringType()); |
| 231 | std::vector<TypedVar *> *results = new std::vector<TypedVar *>(); |
| 232 | results->push_back(new TypedVar("indicator", vecType)); |
| 233 | |
| 234 | return new Method("interfaceChain", |
| 235 | new std::vector<TypedVar *>() /* args */, |
| 236 | results, |
| 237 | false /* oneway */, |
| 238 | new std::vector<Annotation *>(), |
| 239 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 240 | { { IMPL_HEADER, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 241 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 0393512 | 2016-12-19 11:10:40 -0800 | [diff] [blame] | 242 | out << "_hidl_cb("; |
| 243 | out.block([&] { |
| 244 | for (const Interface *iface : chain) { |
| 245 | out << iface->fullName() << "::descriptor,\n"; |
| 246 | } |
| 247 | }); |
| 248 | out << ");\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 249 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 250 | } } }, /* cppImpl */ |
| 251 | { { IMPL_HEADER, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 252 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 253 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 254 | out.indent(); out.indent(); |
| 255 | for (size_t i = 0; i < chain.size(); ++i) { |
| 256 | if (i != 0) |
| 257 | out << ",\n"; |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 258 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 259 | } |
| 260 | out << "));"; |
| 261 | out.unindent(); out.unindent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 262 | } } } /* javaImpl */ |
| 263 | ); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 267 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 268 | if (isIBase()) { |
| 269 | // ignore addMethod requests for IBase; they are all HIDL reserved methods. |
| 270 | return true; |
| 271 | } |
| 272 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 273 | CHECK(!method->isHidlReserved()); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 274 | if (lookupMethod(method->name()) != nullptr) { |
| 275 | LOG(ERROR) << "Redefinition of method " << method->name(); |
| 276 | return false; |
| 277 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 278 | size_t serial = FIRST_CALL_TRANSACTION; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 279 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 280 | serial += userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 281 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 282 | const Interface *ancestor = mSuperType; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 283 | while (ancestor != nullptr) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 284 | serial += ancestor->userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 285 | ancestor = ancestor->superType(); |
| 286 | } |
| 287 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 288 | CHECK(serial <= LAST_CALL_TRANSACTION) << "More than " |
| 289 | << LAST_CALL_TRANSACTION << " methods are not allowed."; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 290 | method->setSerialId(serial); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 291 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 292 | |
| 293 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 296 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 297 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 298 | return mSuperType; |
| 299 | } |
| 300 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 301 | std::vector<const Interface *> Interface::typeChain() const { |
| 302 | std::vector<const Interface *> v; |
| 303 | const Interface *iface = this; |
| 304 | while (iface != nullptr) { |
| 305 | v.push_back(iface); |
| 306 | iface = iface->mSuperType; |
| 307 | } |
| 308 | return v; |
| 309 | } |
| 310 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 311 | std::vector<const Interface *> Interface::superTypeChain() const { |
| 312 | return superType()->typeChain(); // should work even if superType is nullptr |
| 313 | } |
| 314 | |
Martijn Coenen | b40ef02 | 2017-01-02 15:21:46 +0100 | [diff] [blame] | 315 | bool Interface::isElidableType() const { |
| 316 | return true; |
| 317 | } |
| 318 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 319 | bool Interface::isInterface() const { |
| 320 | return true; |
| 321 | } |
| 322 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 323 | bool Interface::isBinder() const { |
| 324 | return true; |
| 325 | } |
| 326 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 327 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 328 | return mUserMethods; |
| 329 | } |
| 330 | |
| 331 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 332 | return mReservedMethods; |
| 333 | } |
| 334 | |
| 335 | std::vector<Method *> Interface::methods() const { |
| 336 | std::vector<Method *> v(mUserMethods); |
| 337 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 338 | return v; |
| 339 | } |
| 340 | |
| 341 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 342 | std::vector<InterfaceAndMethod> v; |
| 343 | std::vector<const Interface *> chain = typeChain(); |
| 344 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 345 | const Interface *iface = *it; |
| 346 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 347 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 348 | } |
| 349 | } |
| 350 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 351 | v.push_back(InterfaceAndMethod( |
| 352 | *chain.rbegin(), // IBase |
| 353 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 354 | } |
| 355 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 358 | Method *Interface::lookupMethod(std::string name) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 359 | for (const auto &tuple : allMethodsFromRoot()) { |
| 360 | Method *method = tuple.method(); |
| 361 | if (method->name() == name) { |
| 362 | return method; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 363 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 369 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 370 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 373 | std::string Interface::getProxyName() const { |
| 374 | return fqName().getInterfaceProxyName(); |
| 375 | } |
| 376 | |
| 377 | std::string Interface::getStubName() const { |
| 378 | return fqName().getInterfaceStubName(); |
| 379 | } |
| 380 | |
| 381 | std::string Interface::getHwName() const { |
| 382 | return fqName().getInterfaceHwName(); |
| 383 | } |
| 384 | |
| 385 | std::string Interface::getPassthroughName() const { |
| 386 | return fqName().getInterfacePassthroughName(); |
| 387 | } |
| 388 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 389 | FQName Interface::getProxyFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 390 | return fqName().getInterfaceProxyFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 393 | FQName Interface::getStubFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 394 | return fqName().getInterfaceStubFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 397 | FQName Interface::getPassthroughFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 398 | return fqName().getInterfacePassthroughFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 401 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 402 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 403 | const std::string base = |
| 404 | std::string(specifyNamespaces ? "::android::" : "") |
| 405 | + "sp<" |
| 406 | + (specifyNamespaces ? fullName() : partialCppName()) |
| 407 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 408 | |
| 409 | switch (mode) { |
| 410 | case StorageMode_Stack: |
| 411 | case StorageMode_Result: |
| 412 | return base; |
| 413 | |
| 414 | case StorageMode_Argument: |
| 415 | return "const " + base + "&"; |
| 416 | } |
| 417 | } |
| 418 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 419 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 420 | return fullJavaName(); |
| 421 | } |
| 422 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 423 | std::string Interface::getVtsType() const { |
| 424 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 425 | return "TYPE_HIDL_CALLBACK"; |
| 426 | } else { |
| 427 | return "TYPE_HIDL_INTERFACE"; |
| 428 | } |
| 429 | } |
| 430 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 431 | void Interface::emitReaderWriter( |
| 432 | Formatter &out, |
| 433 | const std::string &name, |
| 434 | const std::string &parcelObj, |
| 435 | bool parcelObjIsPointer, |
| 436 | bool isReader, |
| 437 | ErrorMode mode) const { |
| 438 | const std::string parcelObjDeref = |
| 439 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 440 | |
| 441 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 442 | out << "{\n"; |
| 443 | out.indent(); |
| 444 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 445 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 446 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 447 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 448 | << binderName << ";\n"; |
| 449 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 450 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 451 | out << parcelObjDeref |
| 452 | << "readNullableStrongBinder(&" |
| 453 | << binderName |
| 454 | << ");\n"; |
| 455 | |
| 456 | handleError(out, mode); |
| 457 | |
| 458 | out << name |
| 459 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 460 | << "::android::hardware::fromBinder<" |
| 461 | << fqName().cppName() |
| 462 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 463 | << getProxyFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 464 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 465 | << getStubFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 466 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 467 | << binderName |
| 468 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 469 | |
| 470 | out.unindent(); |
| 471 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 472 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 473 | out << "if (" << name << " == nullptr) {\n"; |
| 474 | out.indent(); |
| 475 | out << "_hidl_err = "; |
| 476 | out << parcelObjDeref |
| 477 | << "writeStrongBinder(nullptr);\n"; |
| 478 | out.unindent(); |
| 479 | out << "} else {\n"; |
| 480 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 481 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 482 | << "::android::hardware::toBinder<\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 483 | out.indent(2, [&] { |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 484 | out << fqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 485 | << ", " |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 486 | << getProxyFqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 487 | << ">(" |
| 488 | << name |
| 489 | << ");\n"; |
| 490 | }); |
| 491 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 492 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 493 | out << "_hidl_err = " |
| 494 | << parcelObjDeref |
| 495 | << "writeStrongBinder(_hidl_binder);\n"; |
| 496 | }); |
| 497 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 498 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 499 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 500 | }); |
| 501 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 502 | out.unindent(); |
| 503 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 504 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 505 | handleError(out, mode); |
| 506 | } |
| 507 | } |
| 508 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 509 | void Interface::emitJavaReaderWriter( |
| 510 | Formatter &out, |
| 511 | const std::string &parcelObj, |
| 512 | const std::string &argName, |
| 513 | bool isReader) const { |
| 514 | if (isReader) { |
| 515 | out << fullJavaName() |
| 516 | << ".asInterface(" |
| 517 | << parcelObj |
| 518 | << ".readStrongBinder());\n"; |
| 519 | } else { |
| 520 | out << parcelObj |
| 521 | << ".writeStrongBinder(" |
| 522 | << argName |
| 523 | << " == null ? null : " |
| 524 | << argName |
| 525 | << ".asBinder());\n"; |
| 526 | } |
| 527 | } |
| 528 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 529 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 530 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 531 | // Skip for TypeDef as it is just an alias of a defined type. |
| 532 | if (type->isTypeDef()) { |
| 533 | continue; |
| 534 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 535 | out << "attribute: {\n"; |
| 536 | out.indent(); |
| 537 | status_t status = type->emitVtsTypeDeclarations(out); |
| 538 | if (status != OK) { |
| 539 | return status; |
| 540 | } |
| 541 | out.unindent(); |
| 542 | out << "}\n\n"; |
| 543 | } |
| 544 | return OK; |
| 545 | } |
| 546 | |
| 547 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 548 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 549 | if (method->isHidlReserved()) { |
| 550 | continue; |
| 551 | } |
| 552 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 553 | out << "api: {\n"; |
| 554 | out.indent(); |
| 555 | out << "name: \"" << method->name() << "\"\n"; |
| 556 | // Generate declaration for each return value. |
| 557 | for (const auto &result : method->results()) { |
| 558 | out << "return_type_hidl: {\n"; |
| 559 | out.indent(); |
| 560 | status_t status = result->type().emitVtsAttributeType(out); |
| 561 | if (status != OK) { |
| 562 | return status; |
| 563 | } |
| 564 | out.unindent(); |
| 565 | out << "}\n"; |
| 566 | } |
| 567 | // Generate declaration for each input argument |
| 568 | for (const auto &arg : method->args()) { |
| 569 | out << "arg: {\n"; |
| 570 | out.indent(); |
| 571 | status_t status = arg->type().emitVtsAttributeType(out); |
| 572 | if (status != OK) { |
| 573 | return status; |
| 574 | } |
| 575 | out.unindent(); |
| 576 | out << "}\n"; |
| 577 | } |
| 578 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 579 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 580 | out << "callflow: {\n"; |
| 581 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 582 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 583 | if (name == "entry") { |
| 584 | out << "entry: true\n"; |
| 585 | } else if (name == "exit") { |
| 586 | out << "exit: true\n"; |
| 587 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 588 | const AnnotationParam *param = |
| 589 | annotation->getParam("next"); |
| 590 | if (param != nullptr) { |
| 591 | for (auto value : *param->getValues()) { |
| 592 | out << "next: " << value << "\n"; |
| 593 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 594 | } |
| 595 | } else { |
| 596 | std::cerr << "Invalid annotation '" |
| 597 | << name << "' for method: " << method->name() |
| 598 | << ". Should be one of: entry, exit, callflow. \n"; |
| 599 | return UNKNOWN_ERROR; |
| 600 | } |
| 601 | out.unindent(); |
| 602 | out << "}\n"; |
| 603 | } |
| 604 | out.unindent(); |
| 605 | out << "}\n\n"; |
| 606 | } |
| 607 | return OK; |
| 608 | } |
| 609 | |
| 610 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 611 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 612 | << "predefined_type: \"" |
| 613 | << localName() |
Zhuoyao Zhang | 1993352 | 2016-08-29 15:06:38 -0700 | [diff] [blame] | 614 | << "\"\n" |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 615 | << "is_callback: " |
| 616 | << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false") |
| 617 | << "\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 618 | return OK; |
| 619 | } |
| 620 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 621 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 622 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 623 | if (method->isOneway()) { |
| 624 | return true; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | const Interface* superClass = superType(); |
| 629 | |
| 630 | if (superClass != nullptr) { |
| 631 | return superClass->hasOnewayMethods(); |
| 632 | } |
| 633 | |
| 634 | return false; |
| 635 | } |
| 636 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 637 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 638 | if (mIsJavaCompatibleInProgress) { |
| 639 | // We're currently trying to determine if this Interface is |
| 640 | // java-compatible and something is referencing this interface through |
| 641 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 642 | // the original invocation of Interface::isJavaCompatible() will then |
| 643 | // return the correct "false" result. |
| 644 | return true; |
| 645 | } |
| 646 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 647 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 648 | mIsJavaCompatibleInProgress = false; |
| 649 | return false; |
| 650 | } |
| 651 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 652 | mIsJavaCompatibleInProgress = true; |
| 653 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 654 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 655 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 656 | return false; |
| 657 | } |
| 658 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 659 | for (const auto &method : methods()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 660 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 661 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 662 | return false; |
| 663 | } |
| 664 | } |
| 665 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 666 | mIsJavaCompatibleInProgress = false; |
| 667 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 671 | } // namespace android |
| 672 | |