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