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