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 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 275 | bool Interface::isInterface() const { |
| 276 | return true; |
| 277 | } |
| 278 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 279 | bool Interface::isBinder() const { |
| 280 | return true; |
| 281 | } |
| 282 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 283 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 284 | return mUserMethods; |
| 285 | } |
| 286 | |
| 287 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 288 | return mReservedMethods; |
| 289 | } |
| 290 | |
| 291 | std::vector<Method *> Interface::methods() const { |
| 292 | std::vector<Method *> v(mUserMethods); |
| 293 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 294 | return v; |
| 295 | } |
| 296 | |
| 297 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 298 | std::vector<InterfaceAndMethod> v; |
| 299 | std::vector<const Interface *> chain = typeChain(); |
| 300 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 301 | const Interface *iface = *it; |
| 302 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 303 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 304 | } |
| 305 | } |
| 306 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 307 | v.push_back(InterfaceAndMethod( |
| 308 | *chain.rbegin(), // IBase |
| 309 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 310 | } |
| 311 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 314 | Method *Interface::lookupMethod(std::string name) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 315 | for (const auto &tuple : allMethodsFromRoot()) { |
| 316 | Method *method = tuple.method(); |
| 317 | if (method->name() == name) { |
| 318 | return method; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 319 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return nullptr; |
| 323 | } |
| 324 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 325 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 326 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 329 | FQName Interface::getProxyName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 330 | return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName()); |
| 331 | } |
| 332 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 333 | FQName Interface::getStubName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 334 | return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName()); |
| 335 | } |
| 336 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 337 | FQName Interface::getPassthroughName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 338 | return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName()); |
| 339 | } |
| 340 | |
| 341 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 342 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 343 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 344 | const std::string base = |
| 345 | std::string(specifyNamespaces ? "::android::" : "") |
| 346 | + "sp<" |
| 347 | + (specifyNamespaces ? fullName() : partialCppName()) |
| 348 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 349 | |
| 350 | switch (mode) { |
| 351 | case StorageMode_Stack: |
| 352 | case StorageMode_Result: |
| 353 | return base; |
| 354 | |
| 355 | case StorageMode_Argument: |
| 356 | return "const " + base + "&"; |
| 357 | } |
| 358 | } |
| 359 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 360 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 361 | return fullJavaName(); |
| 362 | } |
| 363 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 364 | std::string Interface::getVtsType() const { |
| 365 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 366 | return "TYPE_HIDL_CALLBACK"; |
| 367 | } else { |
| 368 | return "TYPE_HIDL_INTERFACE"; |
| 369 | } |
| 370 | } |
| 371 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 372 | void Interface::emitReaderWriter( |
| 373 | Formatter &out, |
| 374 | const std::string &name, |
| 375 | const std::string &parcelObj, |
| 376 | bool parcelObjIsPointer, |
| 377 | bool isReader, |
| 378 | ErrorMode mode) const { |
| 379 | const std::string parcelObjDeref = |
| 380 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 381 | |
| 382 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 383 | out << "{\n"; |
| 384 | out.indent(); |
| 385 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 386 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 387 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 388 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 389 | << binderName << ";\n"; |
| 390 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 391 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 392 | out << parcelObjDeref |
| 393 | << "readNullableStrongBinder(&" |
| 394 | << binderName |
| 395 | << ");\n"; |
| 396 | |
| 397 | handleError(out, mode); |
| 398 | |
| 399 | out << name |
| 400 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 401 | << "::android::hardware::fromBinder<" |
| 402 | << fqName().cppName() |
| 403 | << "," |
| 404 | << getProxyName().cppName() |
| 405 | << "," |
| 406 | << getStubName().cppName() |
| 407 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 408 | << binderName |
| 409 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 410 | |
| 411 | out.unindent(); |
| 412 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 413 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 414 | out << "if (" << name << " == nullptr) {\n"; |
| 415 | out.indent(); |
| 416 | out << "_hidl_err = "; |
| 417 | out << parcelObjDeref |
| 418 | << "writeStrongBinder(nullptr);\n"; |
| 419 | out.unindent(); |
| 420 | out << "} else {\n"; |
| 421 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 422 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 423 | << "::android::hardware::toBinder<\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 424 | out.indent(2, [&] { |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 425 | out << fqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 426 | << ", " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 427 | << getProxyName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 428 | << ">(" |
| 429 | << name |
| 430 | << ");\n"; |
| 431 | }); |
| 432 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 433 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 434 | out << "_hidl_err = " |
| 435 | << parcelObjDeref |
| 436 | << "writeStrongBinder(_hidl_binder);\n"; |
| 437 | }); |
| 438 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 439 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 440 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 441 | }); |
| 442 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 443 | out.unindent(); |
| 444 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 445 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 446 | handleError(out, mode); |
| 447 | } |
| 448 | } |
| 449 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 450 | void Interface::emitJavaReaderWriter( |
| 451 | Formatter &out, |
| 452 | const std::string &parcelObj, |
| 453 | const std::string &argName, |
| 454 | bool isReader) const { |
| 455 | if (isReader) { |
| 456 | out << fullJavaName() |
| 457 | << ".asInterface(" |
| 458 | << parcelObj |
| 459 | << ".readStrongBinder());\n"; |
| 460 | } else { |
| 461 | out << parcelObj |
| 462 | << ".writeStrongBinder(" |
| 463 | << argName |
| 464 | << " == null ? null : " |
| 465 | << argName |
| 466 | << ".asBinder());\n"; |
| 467 | } |
| 468 | } |
| 469 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 470 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 471 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 472 | // Skip for TypeDef as it is just an alias of a defined type. |
| 473 | if (type->isTypeDef()) { |
| 474 | continue; |
| 475 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 476 | out << "attribute: {\n"; |
| 477 | out.indent(); |
| 478 | status_t status = type->emitVtsTypeDeclarations(out); |
| 479 | if (status != OK) { |
| 480 | return status; |
| 481 | } |
| 482 | out.unindent(); |
| 483 | out << "}\n\n"; |
| 484 | } |
| 485 | return OK; |
| 486 | } |
| 487 | |
| 488 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 489 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 490 | if (method->isHidlReserved()) { |
| 491 | continue; |
| 492 | } |
| 493 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 494 | out << "api: {\n"; |
| 495 | out.indent(); |
| 496 | out << "name: \"" << method->name() << "\"\n"; |
| 497 | // Generate declaration for each return value. |
| 498 | for (const auto &result : method->results()) { |
| 499 | out << "return_type_hidl: {\n"; |
| 500 | out.indent(); |
| 501 | status_t status = result->type().emitVtsAttributeType(out); |
| 502 | if (status != OK) { |
| 503 | return status; |
| 504 | } |
| 505 | out.unindent(); |
| 506 | out << "}\n"; |
| 507 | } |
| 508 | // Generate declaration for each input argument |
| 509 | for (const auto &arg : method->args()) { |
| 510 | out << "arg: {\n"; |
| 511 | out.indent(); |
| 512 | status_t status = arg->type().emitVtsAttributeType(out); |
| 513 | if (status != OK) { |
| 514 | return status; |
| 515 | } |
| 516 | out.unindent(); |
| 517 | out << "}\n"; |
| 518 | } |
| 519 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 520 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 521 | out << "callflow: {\n"; |
| 522 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 523 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 524 | if (name == "entry") { |
| 525 | out << "entry: true\n"; |
| 526 | } else if (name == "exit") { |
| 527 | out << "exit: true\n"; |
| 528 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 529 | const AnnotationParam *param = |
| 530 | annotation->getParam("next"); |
| 531 | if (param != nullptr) { |
| 532 | for (auto value : *param->getValues()) { |
| 533 | out << "next: " << value << "\n"; |
| 534 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 535 | } |
| 536 | } else { |
| 537 | std::cerr << "Invalid annotation '" |
| 538 | << name << "' for method: " << method->name() |
| 539 | << ". Should be one of: entry, exit, callflow. \n"; |
| 540 | return UNKNOWN_ERROR; |
| 541 | } |
| 542 | out.unindent(); |
| 543 | out << "}\n"; |
| 544 | } |
| 545 | out.unindent(); |
| 546 | out << "}\n\n"; |
| 547 | } |
| 548 | return OK; |
| 549 | } |
| 550 | |
| 551 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 552 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 553 | << "predefined_type: \"" |
| 554 | << localName() |
Zhuoyao Zhang | 1993352 | 2016-08-29 15:06:38 -0700 | [diff] [blame] | 555 | << "\"\n" |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 556 | << "is_callback: " |
| 557 | << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false") |
| 558 | << "\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 559 | return OK; |
| 560 | } |
| 561 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 562 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 563 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 564 | if (method->isOneway()) { |
| 565 | return true; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | const Interface* superClass = superType(); |
| 570 | |
| 571 | if (superClass != nullptr) { |
| 572 | return superClass->hasOnewayMethods(); |
| 573 | } |
| 574 | |
| 575 | return false; |
| 576 | } |
| 577 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 578 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 579 | if (mIsJavaCompatibleInProgress) { |
| 580 | // We're currently trying to determine if this Interface is |
| 581 | // java-compatible and something is referencing this interface through |
| 582 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 583 | // the original invocation of Interface::isJavaCompatible() will then |
| 584 | // return the correct "false" result. |
| 585 | return true; |
| 586 | } |
| 587 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 588 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 589 | mIsJavaCompatibleInProgress = false; |
| 590 | return false; |
| 591 | } |
| 592 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 593 | mIsJavaCompatibleInProgress = true; |
| 594 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 595 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 596 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 597 | return false; |
| 598 | } |
| 599 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 600 | for (const auto &method : methods()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 601 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 602 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 607 | mIsJavaCompatibleInProgress = false; |
| 608 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 612 | } // namespace android |
| 613 | |