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 | |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 60 | std::string Interface::typeName() const { |
| 61 | return "interface " + localName(); |
| 62 | } |
| 63 | |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 64 | Method *Interface::createSyspropsChangedMethod() const { |
| 65 | return new Method("notifySyspropsChanged", |
| 66 | new std::vector<TypedVar *>() /*args */, |
| 67 | new std::vector<TypedVar *>() /*results */, |
| 68 | true /*oneway */, |
| 69 | new std::vector<Annotation *>(), |
| 70 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
| 71 | [this](auto &out) { /*cppImpl */ |
| 72 | out << "::android::report_sysprop_change();\n"; |
| 73 | out << "return ::android::hardware::Void();"; |
| 74 | }, |
| 75 | [this](auto &out) { /* javaImpl */ |
| 76 | out << "android.os.SystemProperties.reportSyspropChanged();"; |
| 77 | } |
| 78 | ); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 81 | Method *Interface::createDescriptorChainMethod() const { |
| 82 | VectorType *vecType = new VectorType(); |
| 83 | vecType->setElementType(new StringType()); |
| 84 | std::vector<TypedVar *> *results = new std::vector<TypedVar *>(); |
| 85 | results->push_back(new TypedVar("indicator", vecType)); |
| 86 | |
| 87 | return new Method("interfaceChain", |
| 88 | new std::vector<TypedVar *>() /* args */, |
| 89 | results, |
| 90 | false /* oneway */, |
| 91 | new std::vector<Annotation *>(), |
| 92 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
| 93 | [this](auto &out) { /* cppImpl */ |
| 94 | std::vector<const Interface *> chain = typeChain(); |
| 95 | out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n"; |
| 96 | out << "_hidl_return.resize(" << chain.size() << ");\n"; |
| 97 | for (size_t i = 0; i < chain.size(); ++i) { |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 98 | out << "_hidl_return[" << i << "] = " |
| 99 | << chain[i]->fullName() |
| 100 | << "::descriptor;\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 101 | } |
| 102 | out << "_hidl_cb(_hidl_return);\n"; |
| 103 | out << "return ::android::hardware::Void();"; |
| 104 | }, |
| 105 | [this](auto &out) { /* javaImpl */ |
| 106 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 107 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 108 | out.indent(); out.indent(); |
| 109 | for (size_t i = 0; i < chain.size(); ++i) { |
| 110 | if (i != 0) |
| 111 | out << ",\n"; |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 112 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 113 | } |
| 114 | out << "));"; |
| 115 | out.unindent(); out.unindent(); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 120 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 121 | if (isIBase()) { |
| 122 | // ignore addMethod requests for IBase; they are all HIDL reserved methods. |
| 123 | return true; |
| 124 | } |
| 125 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 126 | CHECK(!method->isHidlReserved()); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 127 | if (lookupMethod(method->name()) != nullptr) { |
| 128 | LOG(ERROR) << "Redefinition of method " << method->name(); |
| 129 | return false; |
| 130 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 131 | size_t serial = FIRST_CALL_TRANSACTION; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 132 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 133 | serial += userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 134 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 135 | const Interface *ancestor = mSuperType; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 136 | while (ancestor != nullptr) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 137 | serial += ancestor->userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 138 | ancestor = ancestor->superType(); |
| 139 | } |
| 140 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 141 | CHECK(serial <= LAST_CALL_TRANSACTION) << "More than " |
| 142 | << LAST_CALL_TRANSACTION << " methods are not allowed."; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 143 | method->setSerialId(serial); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 144 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 145 | |
| 146 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 149 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 150 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 151 | return mSuperType; |
| 152 | } |
| 153 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 154 | std::vector<const Interface *> Interface::typeChain() const { |
| 155 | std::vector<const Interface *> v; |
| 156 | const Interface *iface = this; |
| 157 | while (iface != nullptr) { |
| 158 | v.push_back(iface); |
| 159 | iface = iface->mSuperType; |
| 160 | } |
| 161 | return v; |
| 162 | } |
| 163 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 164 | std::vector<const Interface *> Interface::superTypeChain() const { |
| 165 | return superType()->typeChain(); // should work even if superType is nullptr |
| 166 | } |
| 167 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 168 | bool Interface::isInterface() const { |
| 169 | return true; |
| 170 | } |
| 171 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 172 | bool Interface::isBinder() const { |
| 173 | return true; |
| 174 | } |
| 175 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 176 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 177 | return mUserMethods; |
| 178 | } |
| 179 | |
| 180 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 181 | return mReservedMethods; |
| 182 | } |
| 183 | |
| 184 | std::vector<Method *> Interface::methods() const { |
| 185 | std::vector<Method *> v(mUserMethods); |
| 186 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 187 | return v; |
| 188 | } |
| 189 | |
| 190 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 191 | std::vector<InterfaceAndMethod> v; |
| 192 | std::vector<const Interface *> chain = typeChain(); |
| 193 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 194 | const Interface *iface = *it; |
| 195 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 196 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 197 | } |
| 198 | } |
| 199 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 200 | v.push_back(InterfaceAndMethod( |
| 201 | *chain.rbegin(), // IBase |
| 202 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 203 | } |
| 204 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 207 | Method *Interface::lookupMethod(std::string name) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 208 | for (const auto &tuple : allMethodsFromRoot()) { |
| 209 | Method *method = tuple.method(); |
| 210 | if (method->name() == name) { |
| 211 | return method; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 212 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 218 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 219 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 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 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 294 | << "::android::hardware::fromBinder<" |
| 295 | << fqName().cppName() |
| 296 | << "," |
| 297 | << getProxyName().cppName() |
| 298 | << "," |
| 299 | << getStubName().cppName() |
| 300 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 301 | << binderName |
| 302 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 303 | |
| 304 | out.unindent(); |
| 305 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 306 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 307 | out << "if (" << name << " == nullptr) {\n"; |
| 308 | out.indent(); |
| 309 | out << "_hidl_err = "; |
| 310 | out << parcelObjDeref |
| 311 | << "writeStrongBinder(nullptr);\n"; |
| 312 | out.unindent(); |
| 313 | out << "} else {\n"; |
| 314 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 315 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 316 | << "::android::hardware::toBinder<\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 317 | out.indent(2, [&] { |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 318 | out << fqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 319 | << ", " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 320 | << getProxyName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 321 | << ">(" |
| 322 | << name |
| 323 | << ");\n"; |
| 324 | }); |
| 325 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 326 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 327 | out << "_hidl_err = " |
| 328 | << parcelObjDeref |
| 329 | << "writeStrongBinder(_hidl_binder);\n"; |
| 330 | }); |
| 331 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 332 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 333 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 334 | }); |
| 335 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 336 | out.unindent(); |
| 337 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 338 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 339 | handleError(out, mode); |
| 340 | } |
| 341 | } |
| 342 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 343 | void Interface::emitJavaReaderWriter( |
| 344 | Formatter &out, |
| 345 | const std::string &parcelObj, |
| 346 | const std::string &argName, |
| 347 | bool isReader) const { |
| 348 | if (isReader) { |
| 349 | out << fullJavaName() |
| 350 | << ".asInterface(" |
| 351 | << parcelObj |
| 352 | << ".readStrongBinder());\n"; |
| 353 | } else { |
| 354 | out << parcelObj |
| 355 | << ".writeStrongBinder(" |
| 356 | << argName |
| 357 | << " == null ? null : " |
| 358 | << argName |
| 359 | << ".asBinder());\n"; |
| 360 | } |
| 361 | } |
| 362 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 363 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 364 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 365 | // Skip for TypeDef as it is just an alias of a defined type. |
| 366 | if (type->isTypeDef()) { |
| 367 | continue; |
| 368 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 369 | out << "attribute: {\n"; |
| 370 | out.indent(); |
| 371 | status_t status = type->emitVtsTypeDeclarations(out); |
| 372 | if (status != OK) { |
| 373 | return status; |
| 374 | } |
| 375 | out.unindent(); |
| 376 | out << "}\n\n"; |
| 377 | } |
| 378 | return OK; |
| 379 | } |
| 380 | |
| 381 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 382 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 383 | if (method->isHidlReserved()) { |
| 384 | continue; |
| 385 | } |
| 386 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 387 | out << "api: {\n"; |
| 388 | out.indent(); |
| 389 | out << "name: \"" << method->name() << "\"\n"; |
| 390 | // Generate declaration for each return value. |
| 391 | for (const auto &result : method->results()) { |
| 392 | out << "return_type_hidl: {\n"; |
| 393 | out.indent(); |
| 394 | status_t status = result->type().emitVtsAttributeType(out); |
| 395 | if (status != OK) { |
| 396 | return status; |
| 397 | } |
| 398 | out.unindent(); |
| 399 | out << "}\n"; |
| 400 | } |
| 401 | // Generate declaration for each input argument |
| 402 | for (const auto &arg : method->args()) { |
| 403 | out << "arg: {\n"; |
| 404 | out.indent(); |
| 405 | status_t status = arg->type().emitVtsAttributeType(out); |
| 406 | if (status != OK) { |
| 407 | return status; |
| 408 | } |
| 409 | out.unindent(); |
| 410 | out << "}\n"; |
| 411 | } |
| 412 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 413 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 414 | out << "callflow: {\n"; |
| 415 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 416 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 417 | if (name == "entry") { |
| 418 | out << "entry: true\n"; |
| 419 | } else if (name == "exit") { |
| 420 | out << "exit: true\n"; |
| 421 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 422 | const AnnotationParam *param = |
| 423 | annotation->getParam("next"); |
| 424 | if (param != nullptr) { |
| 425 | for (auto value : *param->getValues()) { |
| 426 | out << "next: " << value << "\n"; |
| 427 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 428 | } |
| 429 | } else { |
| 430 | std::cerr << "Invalid annotation '" |
| 431 | << name << "' for method: " << method->name() |
| 432 | << ". Should be one of: entry, exit, callflow. \n"; |
| 433 | return UNKNOWN_ERROR; |
| 434 | } |
| 435 | out.unindent(); |
| 436 | out << "}\n"; |
| 437 | } |
| 438 | out.unindent(); |
| 439 | out << "}\n\n"; |
| 440 | } |
| 441 | return OK; |
| 442 | } |
| 443 | |
| 444 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 445 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 446 | << "predefined_type: \"" |
| 447 | << localName() |
Zhuoyao Zhang | 1993352 | 2016-08-29 15:06:38 -0700 | [diff] [blame] | 448 | << "\"\n" |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 449 | << "is_callback: " |
| 450 | << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false") |
| 451 | << "\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 452 | return OK; |
| 453 | } |
| 454 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 455 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 456 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 457 | if (method->isOneway()) { |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | const Interface* superClass = superType(); |
| 463 | |
| 464 | if (superClass != nullptr) { |
| 465 | return superClass->hasOnewayMethods(); |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 471 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 472 | if (mIsJavaCompatibleInProgress) { |
| 473 | // We're currently trying to determine if this Interface is |
| 474 | // java-compatible and something is referencing this interface through |
| 475 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 476 | // the original invocation of Interface::isJavaCompatible() will then |
| 477 | // return the correct "false" result. |
| 478 | return true; |
| 479 | } |
| 480 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 481 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 482 | mIsJavaCompatibleInProgress = false; |
| 483 | return false; |
| 484 | } |
| 485 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 486 | mIsJavaCompatibleInProgress = true; |
| 487 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 488 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 489 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 493 | for (const auto &method : methods()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 494 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 495 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 500 | mIsJavaCompatibleInProgress = false; |
| 501 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 502 | return true; |
| 503 | } |
| 504 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 505 | } // namespace android |
| 506 | |