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