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" |
| 21 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame^] | 22 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 23 | #include <hidl-util/Formatter.h> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 24 | #include <iostream> |
| 25 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 28 | Interface::Interface(const char *localName, Interface *super) |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 29 | : Scope(localName), |
| 30 | mSuperType(super), |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 31 | mIsJavaCompatibleInProgress(false) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame^] | 34 | bool Interface::addMethod(Method *method) { |
| 35 | if (lookupMethod(method->name()) != nullptr) { |
| 36 | LOG(ERROR) << "Redefinition of method " << method->name(); |
| 37 | return false; |
| 38 | } |
| 39 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 40 | /* It is very important that these values NEVER change. These values |
| 41 | * must remain unchanged over the lifetime of android. This is |
| 42 | * because the framework on a device will be updated independently of |
| 43 | * the hals on a device. If the hals are compiled with one set of |
| 44 | * transaction values, and the framework with another, then the |
| 45 | * interface between them will be destroyed, and the device will not |
| 46 | * work. |
| 47 | */ |
| 48 | size_t serial = 1; // hardware::IBinder::FIRST_CALL_TRANSACTION; |
| 49 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame^] | 50 | const Interface *ancestor = this; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 51 | while (ancestor != nullptr) { |
| 52 | serial += ancestor->methods().size(); |
| 53 | ancestor = ancestor->superType(); |
| 54 | } |
| 55 | |
| 56 | method->setSerialId(serial); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 57 | mMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame^] | 58 | |
| 59 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 62 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 63 | return mSuperType; |
| 64 | } |
| 65 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 66 | bool Interface::isInterface() const { |
| 67 | return true; |
| 68 | } |
| 69 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 70 | bool Interface::isBinder() const { |
| 71 | return true; |
| 72 | } |
| 73 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 74 | const std::vector<Method *> &Interface::methods() const { |
| 75 | return mMethods; |
| 76 | } |
| 77 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame^] | 78 | Method *Interface::lookupMethod(std::string name) const { |
| 79 | const Interface *ancestor = this; |
| 80 | while (ancestor != nullptr) { |
| 81 | for (const auto &method : mMethods) { |
| 82 | if (method->name() == name) { |
| 83 | return method; |
| 84 | } |
| 85 | } |
| 86 | ancestor = ancestor->superType(); |
| 87 | } |
| 88 | |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 92 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 93 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 96 | std::string Interface::getCppType(StorageMode mode, |
| 97 | std::string *extra, |
| 98 | bool specifyNamespaces) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 99 | extra->clear(); |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 100 | const std::string base = |
| 101 | std::string(specifyNamespaces ? "::android::" : "") |
| 102 | + "sp<" |
| 103 | + (specifyNamespaces ? fullName() : partialCppName()) |
| 104 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 105 | |
| 106 | switch (mode) { |
| 107 | case StorageMode_Stack: |
| 108 | case StorageMode_Result: |
| 109 | return base; |
| 110 | |
| 111 | case StorageMode_Argument: |
| 112 | return "const " + base + "&"; |
| 113 | } |
| 114 | } |
| 115 | |
Andreas Huber | 4c865b7 | 2016-09-14 15:26:27 -0700 | [diff] [blame] | 116 | std::string Interface::getJavaType( |
| 117 | std::string *extra, bool /* forInitializer */) const { |
| 118 | extra->clear(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 119 | return fullJavaName(); |
| 120 | } |
| 121 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 122 | void Interface::emitReaderWriter( |
| 123 | Formatter &out, |
| 124 | const std::string &name, |
| 125 | const std::string &parcelObj, |
| 126 | bool parcelObjIsPointer, |
| 127 | bool isReader, |
| 128 | ErrorMode mode) const { |
| 129 | const std::string parcelObjDeref = |
| 130 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 131 | |
| 132 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 133 | out << "{\n"; |
| 134 | out.indent(); |
| 135 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 136 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 137 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 138 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 139 | << binderName << ";\n"; |
| 140 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 141 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 142 | out << parcelObjDeref |
| 143 | << "readNullableStrongBinder(&" |
| 144 | << binderName |
| 145 | << ");\n"; |
| 146 | |
| 147 | handleError(out, mode); |
| 148 | |
| 149 | out << name |
| 150 | << " = " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 151 | << fqName().cppNamespace() |
| 152 | << "::IHw" |
| 153 | << getBaseName() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 154 | << "::asInterface(" |
| 155 | << binderName |
| 156 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 157 | |
| 158 | out.unindent(); |
| 159 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 160 | } else { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 161 | |
| 162 | out << "if (" << name << "->isRemote()) {\n"; |
| 163 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 164 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 165 | out << parcelObjDeref |
| 166 | << "writeStrongBinder(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 167 | << fqName().cppNamespace() |
| 168 | << "::IHw" |
| 169 | << getBaseName() |
| 170 | << "::asBinder(static_cast<" |
| 171 | << fqName().cppNamespace() |
| 172 | << "::IHw" |
| 173 | << getBaseName() |
| 174 | << "*>(" |
| 175 | << name << ".get()" |
| 176 | << ")));\n"; |
| 177 | out.unindent(); |
| 178 | out << "} else {\n"; |
| 179 | out.indent(); |
| 180 | out << "_hidl_err = "; |
| 181 | out << parcelObjDeref |
| 182 | << "writeStrongBinder(" |
| 183 | << "new " << fqName().cppNamespace() |
| 184 | << "::Bn" << getBaseName() << " " |
| 185 | << "(" << name <<"));\n"; |
| 186 | out.unindent(); |
| 187 | out << "}\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 188 | handleError(out, mode); |
| 189 | } |
| 190 | } |
| 191 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 192 | void Interface::emitJavaReaderWriter( |
| 193 | Formatter &out, |
| 194 | const std::string &parcelObj, |
| 195 | const std::string &argName, |
| 196 | bool isReader) const { |
| 197 | if (isReader) { |
| 198 | out << fullJavaName() |
| 199 | << ".asInterface(" |
| 200 | << parcelObj |
| 201 | << ".readStrongBinder());\n"; |
| 202 | } else { |
| 203 | out << parcelObj |
| 204 | << ".writeStrongBinder(" |
| 205 | << argName |
| 206 | << " == null ? null : " |
| 207 | << argName |
| 208 | << ".asBinder());\n"; |
| 209 | } |
| 210 | } |
| 211 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 212 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 213 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 214 | // Skip for TypeDef as it is just an alias of a defined type. |
| 215 | if (type->isTypeDef()) { |
| 216 | continue; |
| 217 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 218 | out << "attribute: {\n"; |
| 219 | out.indent(); |
| 220 | status_t status = type->emitVtsTypeDeclarations(out); |
| 221 | if (status != OK) { |
| 222 | return status; |
| 223 | } |
| 224 | out.unindent(); |
| 225 | out << "}\n\n"; |
| 226 | } |
| 227 | return OK; |
| 228 | } |
| 229 | |
| 230 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
| 231 | for (const auto &method : mMethods) { |
| 232 | out << "api: {\n"; |
| 233 | out.indent(); |
| 234 | out << "name: \"" << method->name() << "\"\n"; |
| 235 | // Generate declaration for each return value. |
| 236 | for (const auto &result : method->results()) { |
| 237 | out << "return_type_hidl: {\n"; |
| 238 | out.indent(); |
| 239 | status_t status = result->type().emitVtsAttributeType(out); |
| 240 | if (status != OK) { |
| 241 | return status; |
| 242 | } |
| 243 | out.unindent(); |
| 244 | out << "}\n"; |
| 245 | } |
| 246 | // Generate declaration for each input argument |
| 247 | for (const auto &arg : method->args()) { |
| 248 | out << "arg: {\n"; |
| 249 | out.indent(); |
| 250 | status_t status = arg->type().emitVtsAttributeType(out); |
| 251 | if (status != OK) { |
| 252 | return status; |
| 253 | } |
| 254 | out.unindent(); |
| 255 | out << "}\n"; |
| 256 | } |
| 257 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 258 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 259 | out << "callflow: {\n"; |
| 260 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 261 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 262 | if (name == "entry") { |
| 263 | out << "entry: true\n"; |
| 264 | } else if (name == "exit") { |
| 265 | out << "exit: true\n"; |
| 266 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 267 | const AnnotationParam *param = |
| 268 | annotation->getParam("next"); |
| 269 | if (param != nullptr) { |
| 270 | for (auto value : *param->getValues()) { |
| 271 | out << "next: " << value << "\n"; |
| 272 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 273 | } |
| 274 | } else { |
| 275 | std::cerr << "Invalid annotation '" |
| 276 | << name << "' for method: " << method->name() |
| 277 | << ". Should be one of: entry, exit, callflow. \n"; |
| 278 | return UNKNOWN_ERROR; |
| 279 | } |
| 280 | out.unindent(); |
| 281 | out << "}\n"; |
| 282 | } |
| 283 | out.unindent(); |
| 284 | out << "}\n\n"; |
| 285 | } |
| 286 | return OK; |
| 287 | } |
| 288 | |
| 289 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 290 | out << "type: TYPE_HIDL_CALLBACK\n" |
| 291 | << "predefined_type: \"" |
| 292 | << localName() |
Zhuoyao Zhang | 1993352 | 2016-08-29 15:06:38 -0700 | [diff] [blame] | 293 | << "\"\n" |
| 294 | << "is_callback: true\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 295 | return OK; |
| 296 | } |
| 297 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 298 | |
| 299 | bool Interface::hasOnewayMethods() const { |
| 300 | for (auto const &method : mMethods) { |
| 301 | if (method->isOneway()) { |
| 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | const Interface* superClass = superType(); |
| 307 | |
| 308 | if (superClass != nullptr) { |
| 309 | return superClass->hasOnewayMethods(); |
| 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 315 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 316 | if (mIsJavaCompatibleInProgress) { |
| 317 | // We're currently trying to determine if this Interface is |
| 318 | // java-compatible and something is referencing this interface through |
| 319 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 320 | // the original invocation of Interface::isJavaCompatible() will then |
| 321 | // return the correct "false" result. |
| 322 | return true; |
| 323 | } |
| 324 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 325 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 326 | mIsJavaCompatibleInProgress = false; |
| 327 | return false; |
| 328 | } |
| 329 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 330 | mIsJavaCompatibleInProgress = true; |
| 331 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 332 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 333 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 334 | return false; |
| 335 | } |
| 336 | |
| 337 | for (const auto &method : mMethods) { |
| 338 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 339 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | } |
| 343 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 344 | mIsJavaCompatibleInProgress = false; |
| 345 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 349 | } // namespace android |
| 350 | |