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