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