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