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" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 20 | #include "Method.h" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 21 | #include "StringType.h" |
| 22 | #include "VectorType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 25 | #include <hidl-util/Formatter.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 26 | #include <hidl-util/StringHelper.h> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 27 | #include <iostream> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 28 | #include <sstream> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 29 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 32 | /* It is very important that these values NEVER change. These values |
| 33 | * must remain unchanged over the lifetime of android. This is |
| 34 | * because the framework on a device will be updated independently of |
| 35 | * the hals on a device. If the hals are compiled with one set of |
| 36 | * transaction values, and the framework with another, then the |
| 37 | * interface between them will be destroyed, and the device will not |
| 38 | * work. |
| 39 | */ |
| 40 | enum { |
| 41 | // These values are defined in hardware::IBinder. |
| 42 | /////////////////// User defined transactions |
| 43 | FIRST_CALL_TRANSACTION = 0x00000001, |
| 44 | LAST_CALL_TRANSACTION = 0x00efffff, |
| 45 | /////////////////// HIDL reserved |
| 46 | FIRST_HIDL_TRANSACTION = 0x00f00000, |
| 47 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION, |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 48 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 49 | LAST_HIDL_TRANSACTION = 0x00ffffff, |
| 50 | }; |
| 51 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 52 | Interface::Interface(const char *localName, const Location &location, Interface *super) |
| 53 | : Scope(localName, location), |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 54 | mSuperType(super), |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 55 | mIsJavaCompatibleInProgress(false) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 56 | mReservedMethods.push_back(createDescriptorChainMethod()); |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 57 | mReservedMethods.push_back(createSyspropsChangedMethod()); |
| 58 | } |
| 59 | |
| 60 | Method *Interface::createSyspropsChangedMethod() const { |
| 61 | return new Method("notifySyspropsChanged", |
| 62 | new std::vector<TypedVar *>() /*args */, |
| 63 | new std::vector<TypedVar *>() /*results */, |
| 64 | true /*oneway */, |
| 65 | new std::vector<Annotation *>(), |
| 66 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
| 67 | [this](auto &out) { /*cppImpl */ |
| 68 | out << "::android::report_sysprop_change();\n"; |
| 69 | out << "return ::android::hardware::Void();"; |
| 70 | }, |
| 71 | [this](auto &out) { /* javaImpl */ |
| 72 | out << "android.os.SystemProperties.reportSyspropChanged();"; |
| 73 | } |
| 74 | ); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 77 | Method *Interface::createDescriptorChainMethod() const { |
| 78 | VectorType *vecType = new VectorType(); |
| 79 | vecType->setElementType(new StringType()); |
| 80 | std::vector<TypedVar *> *results = new std::vector<TypedVar *>(); |
| 81 | results->push_back(new TypedVar("indicator", vecType)); |
| 82 | |
| 83 | return new Method("interfaceChain", |
| 84 | new std::vector<TypedVar *>() /* args */, |
| 85 | results, |
| 86 | false /* oneway */, |
| 87 | new std::vector<Annotation *>(), |
| 88 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
| 89 | [this](auto &out) { /* cppImpl */ |
| 90 | std::vector<const Interface *> chain = typeChain(); |
| 91 | out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n"; |
| 92 | out << "_hidl_return.resize(" << chain.size() << ");\n"; |
| 93 | for (size_t i = 0; i < chain.size(); ++i) { |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 94 | out << "_hidl_return[" << i << "] = " |
| 95 | << chain[i]->fullName() |
| 96 | << "::descriptor;\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 97 | } |
| 98 | out << "_hidl_cb(_hidl_return);\n"; |
| 99 | out << "return ::android::hardware::Void();"; |
| 100 | }, |
| 101 | [this](auto &out) { /* javaImpl */ |
| 102 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 103 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 104 | out.indent(); out.indent(); |
| 105 | for (size_t i = 0; i < chain.size(); ++i) { |
| 106 | if (i != 0) |
| 107 | out << ",\n"; |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 108 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 109 | } |
| 110 | out << "));"; |
| 111 | out.unindent(); out.unindent(); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 116 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 117 | if (isIBase()) { |
| 118 | // ignore addMethod requests for IBase; they are all HIDL reserved methods. |
| 119 | return true; |
| 120 | } |
| 121 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 122 | CHECK(!method->isHidlReserved()); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 123 | if (lookupMethod(method->name()) != nullptr) { |
| 124 | LOG(ERROR) << "Redefinition of method " << method->name(); |
| 125 | return false; |
| 126 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 127 | size_t serial = FIRST_CALL_TRANSACTION; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 128 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 129 | serial += userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 130 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 131 | const Interface *ancestor = mSuperType; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 132 | while (ancestor != nullptr) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 133 | serial += ancestor->userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 134 | ancestor = ancestor->superType(); |
| 135 | } |
| 136 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 137 | CHECK(serial <= LAST_CALL_TRANSACTION) << "More than " |
| 138 | << LAST_CALL_TRANSACTION << " methods are not allowed."; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 139 | method->setSerialId(serial); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 140 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 141 | |
| 142 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 145 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 146 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 147 | return mSuperType; |
| 148 | } |
| 149 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 150 | std::vector<const Interface *> Interface::typeChain() const { |
| 151 | std::vector<const Interface *> v; |
| 152 | const Interface *iface = this; |
| 153 | while (iface != nullptr) { |
| 154 | v.push_back(iface); |
| 155 | iface = iface->mSuperType; |
| 156 | } |
| 157 | return v; |
| 158 | } |
| 159 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 160 | std::vector<const Interface *> Interface::superTypeChain() const { |
| 161 | return superType()->typeChain(); // should work even if superType is nullptr |
| 162 | } |
| 163 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 164 | bool Interface::isInterface() const { |
| 165 | return true; |
| 166 | } |
| 167 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 168 | bool Interface::isBinder() const { |
| 169 | return true; |
| 170 | } |
| 171 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 172 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 173 | return mUserMethods; |
| 174 | } |
| 175 | |
| 176 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 177 | return mReservedMethods; |
| 178 | } |
| 179 | |
| 180 | std::vector<Method *> Interface::methods() const { |
| 181 | std::vector<Method *> v(mUserMethods); |
| 182 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 183 | return v; |
| 184 | } |
| 185 | |
| 186 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 187 | std::vector<InterfaceAndMethod> v; |
| 188 | std::vector<const Interface *> chain = typeChain(); |
| 189 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 190 | const Interface *iface = *it; |
| 191 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 192 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 193 | } |
| 194 | } |
| 195 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 196 | v.push_back(InterfaceAndMethod( |
| 197 | *chain.rbegin(), // IBase |
| 198 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 199 | } |
| 200 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 203 | Method *Interface::lookupMethod(std::string name) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 204 | for (const auto &tuple : allMethodsFromRoot()) { |
| 205 | Method *method = tuple.method(); |
| 206 | if (method->name() == name) { |
| 207 | return method; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 208 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 214 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 215 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 218 | FQName Interface::getHwName() const { |
| 219 | return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName()); |
| 220 | } |
| 221 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 222 | FQName Interface::getProxyName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 223 | return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName()); |
| 224 | } |
| 225 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 226 | FQName Interface::getStubName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 227 | return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName()); |
| 228 | } |
| 229 | |
Yifan Hong | 60e52bd | 2016-11-09 14:47:45 -0800 | [diff] [blame] | 230 | FQName Interface::getPassthroughName() const { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 231 | return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName()); |
| 232 | } |
| 233 | |
| 234 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 235 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 236 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 237 | const std::string base = |
| 238 | std::string(specifyNamespaces ? "::android::" : "") |
| 239 | + "sp<" |
| 240 | + (specifyNamespaces ? fullName() : partialCppName()) |
| 241 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 242 | |
| 243 | switch (mode) { |
| 244 | case StorageMode_Stack: |
| 245 | case StorageMode_Result: |
| 246 | return base; |
| 247 | |
| 248 | case StorageMode_Argument: |
| 249 | return "const " + base + "&"; |
| 250 | } |
| 251 | } |
| 252 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 253 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 254 | return fullJavaName(); |
| 255 | } |
| 256 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 257 | std::string Interface::getVtsType() const { |
| 258 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 259 | return "TYPE_HIDL_CALLBACK"; |
| 260 | } else { |
| 261 | return "TYPE_HIDL_INTERFACE"; |
| 262 | } |
| 263 | } |
| 264 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 265 | void Interface::emitReaderWriter( |
| 266 | Formatter &out, |
| 267 | const std::string &name, |
| 268 | const std::string &parcelObj, |
| 269 | bool parcelObjIsPointer, |
| 270 | bool isReader, |
| 271 | ErrorMode mode) const { |
| 272 | const std::string parcelObjDeref = |
| 273 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 274 | |
| 275 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 276 | out << "{\n"; |
| 277 | out.indent(); |
| 278 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 279 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 280 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 281 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 282 | << binderName << ";\n"; |
| 283 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 284 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 285 | out << parcelObjDeref |
| 286 | << "readNullableStrongBinder(&" |
| 287 | << binderName |
| 288 | << ");\n"; |
| 289 | |
| 290 | handleError(out, mode); |
| 291 | |
| 292 | out << name |
| 293 | << " = " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 294 | << fqName().cppNamespace() |
| 295 | << "::IHw" |
| 296 | << getBaseName() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 297 | << "::asInterface(" |
| 298 | << binderName |
| 299 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 300 | |
| 301 | out.unindent(); |
| 302 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 303 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 304 | out << "if (" << name << " == nullptr) {\n"; |
| 305 | out.indent(); |
| 306 | out << "_hidl_err = "; |
| 307 | out << parcelObjDeref |
| 308 | << "writeStrongBinder(nullptr);\n"; |
| 309 | out.unindent(); |
| 310 | out << "} else {\n"; |
| 311 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 312 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 313 | << "::android::hardware::toBinder<\n"; |
| 314 | out.indentBlock(2, [&] { |
| 315 | out << fqName().cppNamespace() |
| 316 | << "::I" |
| 317 | << getBaseName() |
| 318 | << ", " |
| 319 | << fqName().cppNamespace() |
| 320 | << "::IHw" |
| 321 | << getBaseName() |
| 322 | << ">(" |
| 323 | << name |
| 324 | << ");\n"; |
| 325 | }); |
| 326 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
| 327 | out.indentBlock([&] { |
| 328 | out << "_hidl_err = " |
| 329 | << parcelObjDeref |
| 330 | << "writeStrongBinder(_hidl_binder);\n"; |
| 331 | }); |
| 332 | out << "} else {\n"; |
| 333 | out.indentBlock([&] { |
| 334 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 335 | }); |
| 336 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 337 | out.unindent(); |
| 338 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 339 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 340 | handleError(out, mode); |
| 341 | } |
| 342 | } |
| 343 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 344 | void Interface::emitJavaReaderWriter( |
| 345 | Formatter &out, |
| 346 | const std::string &parcelObj, |
| 347 | const std::string &argName, |
| 348 | bool isReader) const { |
| 349 | if (isReader) { |
| 350 | out << fullJavaName() |
| 351 | << ".asInterface(" |
| 352 | << parcelObj |
| 353 | << ".readStrongBinder());\n"; |
| 354 | } else { |
| 355 | out << parcelObj |
| 356 | << ".writeStrongBinder(" |
| 357 | << argName |
| 358 | << " == null ? null : " |
| 359 | << argName |
| 360 | << ".asBinder());\n"; |
| 361 | } |
| 362 | } |
| 363 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 364 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 365 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 366 | // Skip for TypeDef as it is just an alias of a defined type. |
| 367 | if (type->isTypeDef()) { |
| 368 | continue; |
| 369 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 370 | out << "attribute: {\n"; |
| 371 | out.indent(); |
| 372 | status_t status = type->emitVtsTypeDeclarations(out); |
| 373 | if (status != OK) { |
| 374 | return status; |
| 375 | } |
| 376 | out.unindent(); |
| 377 | out << "}\n\n"; |
| 378 | } |
| 379 | return OK; |
| 380 | } |
| 381 | |
| 382 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 383 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 384 | if (method->isHidlReserved()) { |
| 385 | continue; |
| 386 | } |
| 387 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 388 | out << "api: {\n"; |
| 389 | out.indent(); |
| 390 | out << "name: \"" << method->name() << "\"\n"; |
| 391 | // Generate declaration for each return value. |
| 392 | for (const auto &result : method->results()) { |
| 393 | out << "return_type_hidl: {\n"; |
| 394 | out.indent(); |
| 395 | status_t status = result->type().emitVtsAttributeType(out); |
| 396 | if (status != OK) { |
| 397 | return status; |
| 398 | } |
| 399 | out.unindent(); |
| 400 | out << "}\n"; |
| 401 | } |
| 402 | // Generate declaration for each input argument |
| 403 | for (const auto &arg : method->args()) { |
| 404 | out << "arg: {\n"; |
| 405 | out.indent(); |
| 406 | status_t status = arg->type().emitVtsAttributeType(out); |
| 407 | if (status != OK) { |
| 408 | return status; |
| 409 | } |
| 410 | out.unindent(); |
| 411 | out << "}\n"; |
| 412 | } |
| 413 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 414 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 415 | out << "callflow: {\n"; |
| 416 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 417 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 418 | if (name == "entry") { |
| 419 | out << "entry: true\n"; |
| 420 | } else if (name == "exit") { |
| 421 | out << "exit: true\n"; |
| 422 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 423 | const AnnotationParam *param = |
| 424 | annotation->getParam("next"); |
| 425 | if (param != nullptr) { |
| 426 | for (auto value : *param->getValues()) { |
| 427 | out << "next: " << value << "\n"; |
| 428 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 429 | } |
| 430 | } else { |
| 431 | std::cerr << "Invalid annotation '" |
| 432 | << name << "' for method: " << method->name() |
| 433 | << ". Should be one of: entry, exit, callflow. \n"; |
| 434 | return UNKNOWN_ERROR; |
| 435 | } |
| 436 | out.unindent(); |
| 437 | out << "}\n"; |
| 438 | } |
| 439 | out.unindent(); |
| 440 | out << "}\n\n"; |
| 441 | } |
| 442 | return OK; |
| 443 | } |
| 444 | |
| 445 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 446 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 447 | << "predefined_type: \"" |
| 448 | << localName() |
Zhuoyao Zhang | 1993352 | 2016-08-29 15:06:38 -0700 | [diff] [blame] | 449 | << "\"\n" |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 450 | << "is_callback: " |
| 451 | << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false") |
| 452 | << "\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 453 | return OK; |
| 454 | } |
| 455 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 456 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 457 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 458 | if (method->isOneway()) { |
| 459 | return true; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | const Interface* superClass = superType(); |
| 464 | |
| 465 | if (superClass != nullptr) { |
| 466 | return superClass->hasOnewayMethods(); |
| 467 | } |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 472 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 473 | if (mIsJavaCompatibleInProgress) { |
| 474 | // We're currently trying to determine if this Interface is |
| 475 | // java-compatible and something is referencing this interface through |
| 476 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 477 | // the original invocation of Interface::isJavaCompatible() will then |
| 478 | // return the correct "false" result. |
| 479 | return true; |
| 480 | } |
| 481 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 482 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 483 | mIsJavaCompatibleInProgress = false; |
| 484 | return false; |
| 485 | } |
| 486 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 487 | mIsJavaCompatibleInProgress = true; |
| 488 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 489 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 490 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 494 | for (const auto &method : methods()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 495 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 496 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 501 | mIsJavaCompatibleInProgress = false; |
| 502 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 506 | } // namespace android |
| 507 | |