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