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 | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | |
| 19 | #include "Coordinator.h" |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 20 | #include "Interface.h" |
| 21 | #include "Method.h" |
| 22 | #include "Scope.h" |
| 23 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 24 | #include <hidl-util/Formatter.h> |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 29 | void AST::emitJavaReaderWriter( |
| 30 | Formatter &out, |
| 31 | const std::string &parcelObj, |
| 32 | const TypedVar *arg, |
| 33 | bool isReader) const { |
| 34 | if (isReader) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 35 | out << arg->type().getJavaType() |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 36 | << " " |
| 37 | << arg->name() |
| 38 | << " = "; |
| 39 | } |
| 40 | |
| 41 | arg->type().emitJavaReaderWriter(out, parcelObj, arg->name(), isReader); |
| 42 | } |
| 43 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 44 | status_t AST::generateJavaTypes( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 45 | const std::string &outputPath, const std::string &limitToType) const { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 46 | // Splits types.hal up into one java file per declared type. |
| 47 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 48 | for (const auto &type : mRootScope->getSubTypes()) { |
| 49 | std::string typeName = type->localName(); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 50 | |
| 51 | if (type->isTypeDef()) { |
| 52 | continue; |
| 53 | } |
| 54 | |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 55 | if (!limitToType.empty() && typeName != limitToType) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 56 | continue; |
| 57 | } |
| 58 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 59 | std::string path = outputPath; |
| 60 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 61 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 62 | path.append(typeName); |
| 63 | path.append(".java"); |
| 64 | |
| 65 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 66 | FILE *file = fopen(path.c_str(), "w"); |
| 67 | |
| 68 | if (file == NULL) { |
| 69 | return -errno; |
| 70 | } |
| 71 | |
| 72 | Formatter out(file); |
| 73 | |
| 74 | std::vector<std::string> packageComponents; |
| 75 | getPackageAndVersionComponents( |
| 76 | &packageComponents, true /* cpp_compatible */); |
| 77 | |
| 78 | out << "package " << mPackage.javaPackage() << ";\n\n"; |
| 79 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 80 | for (const auto &item : mImportedNamesForJava) { |
| 81 | out << "import " << item.javaName() << ";\n"; |
| 82 | } |
| 83 | out << "\n"; |
| 84 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 85 | status_t err = |
| 86 | type->emitJavaTypeDeclarations(out, true /* atTopLevel */); |
| 87 | |
| 88 | if (err != OK) { |
| 89 | return err; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return OK; |
| 94 | } |
| 95 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 96 | status_t AST::generateJava( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 97 | const std::string &outputPath, const std::string &limitToType) const { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 98 | if (!isJavaCompatible()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 99 | fprintf(stderr, |
| 100 | "ERROR: This interface is not Java compatible. The Java backend" |
Andreas Huber | f03332a | 2016-09-22 15:35:43 -0700 | [diff] [blame] | 101 | " does NOT support union types nor native handles. " |
| 102 | "In addition, vectors of arrays are limited to at most " |
Andreas Huber | 86a112b | 2016-10-19 14:25:16 -0700 | [diff] [blame] | 103 | "one-dimensional arrays and vectors of {vectors,interfaces} are" |
| 104 | " not supported.\n"); |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 105 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 106 | return UNKNOWN_ERROR; |
| 107 | } |
| 108 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 109 | std::string ifaceName; |
| 110 | if (!AST::isInterface(&ifaceName)) { |
| 111 | return generateJavaTypes(outputPath, limitToType); |
| 112 | } |
| 113 | |
| 114 | const Interface *iface = mRootScope->getInterface(); |
| 115 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 116 | const std::string baseName = iface->getBaseName(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 117 | |
| 118 | std::string path = outputPath; |
| 119 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 120 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 121 | path.append(ifaceName); |
| 122 | path.append(".java"); |
| 123 | |
| 124 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 125 | FILE *file = fopen(path.c_str(), "w"); |
| 126 | |
| 127 | if (file == NULL) { |
| 128 | return -errno; |
| 129 | } |
| 130 | |
| 131 | Formatter out(file); |
| 132 | |
| 133 | std::vector<std::string> packageComponents; |
| 134 | getPackageAndVersionComponents( |
| 135 | &packageComponents, true /* cpp_compatible */); |
| 136 | |
| 137 | out << "package " << mPackage.javaPackage() << ";\n\n"; |
| 138 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 139 | for (const auto &item : mImportedNamesForJava) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 140 | out << "import " << item.javaName() << ";\n"; |
| 141 | } |
| 142 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 143 | if (!mImportedNamesForJava.empty()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 144 | out << "\n"; |
| 145 | } |
| 146 | |
| 147 | out.setNamespace(mPackage.javaPackage() + "."); |
| 148 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 149 | const Interface *superType = iface->superType(); |
| 150 | |
| 151 | out << "public interface " << ifaceName << " extends "; |
| 152 | |
| 153 | if (superType != NULL) { |
| 154 | out << superType->fullJavaName(); |
| 155 | } else { |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 156 | out << "android.os.IHwInterface"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | out << " {\n"; |
| 160 | out.indent(); |
| 161 | |
| 162 | out << "public static final String kInterfaceName = \"" |
| 163 | << mPackage.string() |
| 164 | << "::" |
| 165 | << ifaceName |
| 166 | << "\";\n\n"; |
| 167 | |
| 168 | out << "public static " |
| 169 | << ifaceName |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 170 | << " asInterface(android.os.IHwBinder binder) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 171 | |
| 172 | out.indent(); |
| 173 | |
| 174 | out << "if (binder == null) {\n"; |
| 175 | out.indent(); |
| 176 | out << "return null;\n"; |
| 177 | out.unindent(); |
| 178 | out << "}\n\n"; |
| 179 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 180 | out << "android.os.IHwInterface iface =\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 181 | out.indent(); |
| 182 | out.indent(); |
| 183 | out << "binder.queryLocalInterface(kInterfaceName);\n\n"; |
| 184 | out.unindent(); |
| 185 | out.unindent(); |
| 186 | |
| 187 | out << "if ((iface != null) && (iface instanceof " |
| 188 | << ifaceName |
| 189 | << ")) {\n"; |
| 190 | |
| 191 | out.indent(); |
| 192 | out << "return (" << ifaceName << ")iface;\n"; |
| 193 | out.unindent(); |
| 194 | out << "}\n\n"; |
| 195 | |
| 196 | out << "return new " << ifaceName << ".Proxy(binder);\n"; |
| 197 | |
| 198 | out.unindent(); |
| 199 | out << "}\n\n"; |
| 200 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 201 | out << "public android.os.IHwBinder asBinder();\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 202 | |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 203 | out << "public static " |
| 204 | << ifaceName |
| 205 | << " getService(String serviceName) {\n"; |
| 206 | |
| 207 | out.indent(); |
| 208 | |
| 209 | out << "return " |
| 210 | << ifaceName |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 211 | << ".asInterface(android.os.HwBinder.getService(\"" |
Steven Moreland | 7e367bf | 2016-11-04 11:31:41 -0700 | [diff] [blame] | 212 | << iface->fqName().string() |
| 213 | << "\",serviceName));\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 214 | |
| 215 | out.unindent(); |
| 216 | |
| 217 | out << "}\n\n"; |
| 218 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 219 | status_t err = emitJavaTypeDeclarations(out); |
| 220 | |
| 221 | if (err != OK) { |
| 222 | return err; |
| 223 | } |
| 224 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 225 | for (const auto &method : iface->methods()) { |
| 226 | const bool returnsValue = !method->results().empty(); |
| 227 | const bool needsCallback = method->results().size() > 1; |
| 228 | |
| 229 | if (needsCallback) { |
| 230 | out << "\npublic abstract class " |
| 231 | << method->name() |
| 232 | << "Callback {\n"; |
| 233 | |
| 234 | out.indent(); |
| 235 | |
| 236 | out << "public abstract void onValues(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 237 | << Method::GetJavaArgSignature(method->results()) |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 238 | << ");\n"; |
| 239 | |
| 240 | out.unindent(); |
| 241 | out << "}\n\n"; |
| 242 | } |
| 243 | |
| 244 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 245 | out << method->results()[0]->type().getJavaType(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 246 | } else { |
| 247 | out << "void"; |
| 248 | } |
| 249 | |
| 250 | out << " " |
| 251 | << method->name() |
| 252 | << "(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 253 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 254 | |
| 255 | if (needsCallback) { |
| 256 | if (!method->args().empty()) { |
| 257 | out << ", "; |
| 258 | } |
| 259 | |
| 260 | out << method->name() |
| 261 | << "Callback cb"; |
| 262 | } |
| 263 | |
| 264 | out << ");\n"; |
| 265 | } |
| 266 | |
| 267 | out << "\npublic static final class Proxy implements " |
| 268 | << ifaceName |
| 269 | << " {\n"; |
| 270 | |
| 271 | out.indent(); |
| 272 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 273 | out << "private android.os.IHwBinder mRemote;\n\n"; |
| 274 | out << "public Proxy(android.os.IHwBinder remote) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 275 | out.indent(); |
| 276 | out << "mRemote = remote;\n"; |
| 277 | out.unindent(); |
| 278 | out << "}\n\n"; |
| 279 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 280 | out << "public android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 281 | out.indent(); |
| 282 | out << "return mRemote;\n"; |
| 283 | out.unindent(); |
| 284 | out << "}\n\n"; |
| 285 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 286 | const Interface *prevInterface = nullptr; |
| 287 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 288 | const Method *method = tuple.method(); |
| 289 | const Interface *superInterface = tuple.interface(); |
| 290 | if (prevInterface != superInterface) { |
| 291 | out << "// Methods from " |
| 292 | << superInterface->fullName() |
| 293 | << " follow.\n"; |
| 294 | prevInterface = superInterface; |
| 295 | } |
| 296 | const bool returnsValue = !method->results().empty(); |
| 297 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 298 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 299 | out << "public "; |
| 300 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 301 | out << method->results()[0]->type().getJavaType(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 302 | } else { |
| 303 | out << "void"; |
| 304 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 305 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 306 | out << " " |
| 307 | << method->name() |
| 308 | << "(" |
| 309 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 310 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 311 | if (needsCallback) { |
| 312 | if (!method->args().empty()) { |
| 313 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 316 | out << method->name() |
| 317 | << "Callback cb"; |
| 318 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 319 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 320 | out << ") {\n"; |
| 321 | out.indent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 322 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 323 | out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n"; |
| 324 | out << "_hidl_request.writeInterfaceToken(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 325 | << superInterface->fullJavaName() |
| 326 | << ".kInterfaceName);\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 327 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 328 | for (const auto &arg : method->args()) { |
| 329 | emitJavaReaderWriter( |
| 330 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 331 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 332 | arg, |
| 333 | false /* isReader */); |
| 334 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 335 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 336 | out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 337 | << "mRemote.transact(" |
| 338 | << method->getSerialId() |
| 339 | << " /* " |
| 340 | << method->name() |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 341 | << " */, _hidl_request, _hidl_reply, "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 342 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 343 | if (method->isOneway()) { |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 344 | out << "android.os.IHwBinder.FLAG_ONEWAY"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 345 | } else { |
| 346 | out << "0 /* flags */"; |
| 347 | } |
| 348 | |
| 349 | out << ");\n"; |
| 350 | |
| 351 | if (!method->isOneway()) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 352 | out << "_hidl_reply.verifySuccess();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 353 | } else { |
| 354 | CHECK(!returnsValue); |
| 355 | } |
| 356 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 357 | out << "_hidl_request.releaseTemporaryStorage();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 358 | |
| 359 | if (returnsValue) { |
| 360 | out << "\n"; |
| 361 | |
| 362 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 363 | emitJavaReaderWriter( |
| 364 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 365 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 366 | arg, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 367 | true /* isReader */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 370 | if (needsCallback) { |
| 371 | out << "cb.onValues("; |
Andreas Huber | 8b5da22 | 2016-08-18 14:28:18 -0700 | [diff] [blame] | 372 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 373 | bool firstField = true; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 374 | for (const auto &arg : method->results()) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 375 | if (!firstField) { |
| 376 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 379 | out << arg->name(); |
| 380 | firstField = false; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 381 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 382 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 383 | out << ");\n"; |
| 384 | } else { |
| 385 | const std::string returnName = method->results()[0]->name(); |
| 386 | out << "return " << returnName << ";\n"; |
| 387 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 388 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 389 | |
| 390 | out.unindent(); |
| 391 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | out.unindent(); |
| 395 | out << "}\n"; |
| 396 | |
| 397 | //////////////////////////////////////////////////////////////////////////// |
| 398 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 399 | out << "\npublic static abstract class Stub extends android.os.HwBinder " |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 400 | << "implements " |
| 401 | << ifaceName << " {\n"; |
| 402 | |
| 403 | out.indent(); |
| 404 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 405 | out << "public android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 406 | out.indent(); |
| 407 | out << "return this;\n"; |
| 408 | out.unindent(); |
| 409 | out << "}\n\n"; |
| 410 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 411 | // b/32383557 this is a hack. We need to change this if we have more reserved methods. |
| 412 | for (Method *method : iface->hidlReservedMethods()) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 413 | out << "public final " |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 414 | << method->results()[0]->type().getJavaType() |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 415 | << " " |
| 416 | << method->name() |
| 417 | << "() {\n"; |
| 418 | out.indent(); |
| 419 | method->javaImpl(out); |
| 420 | out.unindent(); |
| 421 | out << "\n}\n\n"; |
| 422 | } |
| 423 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 424 | out << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 425 | out.indent(); |
| 426 | // XXX what about potential superClasses? |
| 427 | out << "if (kInterfaceName.equals(descriptor)) {\n"; |
| 428 | out.indent(); |
| 429 | out << "return this;\n"; |
| 430 | out.unindent(); |
| 431 | out << "}\n"; |
| 432 | out << "return null;\n"; |
| 433 | out.unindent(); |
| 434 | out << "}\n\n"; |
| 435 | |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 436 | out << "public void registerAsService(String serviceName) {\n"; |
| 437 | out.indent(); |
| 438 | |
Steven Moreland | 7e367bf | 2016-11-04 11:31:41 -0700 | [diff] [blame] | 439 | out << "registerService(interfaceChain(), serviceName);\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 440 | |
| 441 | out.unindent(); |
| 442 | out << "}\n\n"; |
| 443 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 444 | out << "public void onTransact(" |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 445 | << "int _hidl_code, " |
| 446 | << "android.os.HwParcel _hidl_request, " |
| 447 | << "final android.os.HwParcel _hidl_reply, " |
| 448 | << "int _hidl_flags) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 449 | |
| 450 | out.indent(); |
| 451 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 452 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 453 | |
| 454 | out.indent(); |
| 455 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 456 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 457 | const Method *method = tuple.method(); |
| 458 | const Interface *superInterface = tuple.interface(); |
| 459 | const bool returnsValue = !method->results().empty(); |
| 460 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 461 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 462 | out << "case " |
| 463 | << method->getSerialId() |
| 464 | << " /* " |
| 465 | << method->name() |
| 466 | << " */:\n{\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 467 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 468 | out.indent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 469 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 470 | out << "_hidl_request.enforceInterface(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 471 | << superInterface->fullJavaName() |
| 472 | << ".kInterfaceName);\n\n"; |
| 473 | |
| 474 | for (const auto &arg : method->args()) { |
| 475 | emitJavaReaderWriter( |
| 476 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 477 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 478 | arg, |
| 479 | true /* isReader */); |
| 480 | } |
| 481 | |
| 482 | if (!needsCallback && returnsValue) { |
| 483 | const TypedVar *returnArg = method->results()[0]; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 484 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 485 | out << returnArg->type().getJavaType() |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 486 | << " " |
| 487 | << returnArg->name() |
| 488 | << " = "; |
| 489 | } |
| 490 | |
| 491 | out << method->name() |
| 492 | << "("; |
| 493 | |
| 494 | bool firstField = true; |
| 495 | for (const auto &arg : method->args()) { |
| 496 | if (!firstField) { |
| 497 | out << ", "; |
| 498 | } |
| 499 | |
| 500 | out << arg->name(); |
| 501 | |
| 502 | firstField = false; |
| 503 | } |
| 504 | |
| 505 | if (needsCallback) { |
| 506 | if (!firstField) { |
| 507 | out << ", "; |
| 508 | } |
| 509 | |
| 510 | out << "new " << method->name() << "Callback() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 511 | out.indent(); |
| 512 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 513 | out << "@Override\n" |
| 514 | << "public void onValues(" |
| 515 | << Method::GetJavaArgSignature(method->results()) |
| 516 | << ") {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 517 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 518 | out.indent(); |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 519 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 520 | |
| 521 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 522 | emitJavaReaderWriter( |
| 523 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 524 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 525 | arg, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 526 | false /* isReader */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 529 | out << "_hidl_reply.send();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 530 | << "}}"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 531 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 532 | out.unindent(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 533 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 534 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 535 | |
| 536 | out << ");\n"; |
| 537 | |
| 538 | if (!needsCallback) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 539 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 540 | |
| 541 | if (returnsValue) { |
| 542 | const TypedVar *returnArg = method->results()[0]; |
| 543 | |
| 544 | emitJavaReaderWriter( |
| 545 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 546 | "_hidl_reply", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 547 | returnArg, |
| 548 | false /* isReader */); |
| 549 | } |
| 550 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 551 | out << "_hidl_reply.send();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | out << "break;\n"; |
| 555 | out.unindent(); |
| 556 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | out.unindent(); |
| 560 | out << "}\n"; |
| 561 | |
| 562 | out.unindent(); |
| 563 | out << "}\n"; |
| 564 | |
| 565 | out.unindent(); |
| 566 | out << "}\n"; |
| 567 | |
| 568 | out.unindent(); |
| 569 | out << "}\n"; |
| 570 | |
| 571 | return OK; |
| 572 | } |
| 573 | |
| 574 | status_t AST::emitJavaTypeDeclarations(Formatter &out) const { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 575 | return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | } // namespace android |
| 579 | |