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