blob: 6e5d91b8534310b4e7a74612586fe76700ceb805 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huber2831d512016-08-15 09:33:47 -070017#include "AST.h"
18
19#include "Coordinator.h"
Andreas Huber2831d512016-08-15 09:33:47 -070020#include "Interface.h"
21#include "Method.h"
22#include "Scope.h"
23
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070024#include <hidl-util/Formatter.h>
Andreas Huber2831d512016-08-15 09:33:47 -070025#include <android-base/logging.h>
26
27namespace android {
28
Andreas Huber2831d512016-08-15 09:33:47 -070029void AST::emitJavaReaderWriter(
30 Formatter &out,
31 const std::string &parcelObj,
32 const TypedVar *arg,
Yifan Honga47eef32016-12-12 10:38:54 -080033 bool isReader,
34 bool addPrefixToName) const {
Andreas Huber2831d512016-08-15 09:33:47 -070035 if (isReader) {
Yifan Hong4ed13472016-11-02 10:44:11 -070036 out << arg->type().getJavaType()
Andreas Huber2831d512016-08-15 09:33:47 -070037 << " "
Yifan Honga47eef32016-12-12 10:38:54 -080038 << (addPrefixToName ? "_hidl_out_" : "")
Andreas Huber2831d512016-08-15 09:33:47 -070039 << arg->name()
40 << " = ";
41 }
42
Yifan Honga47eef32016-12-12 10:38:54 -080043 arg->type().emitJavaReaderWriter(out, parcelObj,
44 (addPrefixToName ? "_hidl_out_" : "") + arg->name(),
45 isReader);
Andreas Huber2831d512016-08-15 09:33:47 -070046}
47
Andreas Huber0fa9e392016-08-31 09:05:44 -070048status_t AST::generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070049 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070050 // Splits types.hal up into one java file per declared type.
51
Steven Morelandd537ab02016-09-12 10:32:01 -070052 for (const auto &type : mRootScope->getSubTypes()) {
53 std::string typeName = type->localName();
Andreas Huber85eabdb2016-08-25 11:24:49 -070054
55 if (type->isTypeDef()) {
56 continue;
57 }
58
Andreas Huberd29724f2016-09-14 09:33:13 -070059 if (!limitToType.empty() && typeName != limitToType) {
Andreas Huber0fa9e392016-08-31 09:05:44 -070060 continue;
61 }
62
Andreas Huber85eabdb2016-08-25 11:24:49 -070063 std::string path = outputPath;
64 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Yifan Hong97288ac2016-12-12 16:03:51 -080065 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */,
66 true /* sanitized */));
Andreas Huber85eabdb2016-08-25 11:24:49 -070067 path.append(typeName);
68 path.append(".java");
69
70 CHECK(Coordinator::MakeParentHierarchy(path));
71 FILE *file = fopen(path.c_str(), "w");
72
73 if (file == NULL) {
74 return -errno;
75 }
76
77 Formatter out(file);
78
79 std::vector<std::string> packageComponents;
80 getPackageAndVersionComponents(
81 &packageComponents, true /* cpp_compatible */);
82
83 out << "package " << mPackage.javaPackage() << ";\n\n";
84
Iliyan Malchev800273d2016-09-02 15:25:07 -070085 out << "\n";
86
Andreas Huber85eabdb2016-08-25 11:24:49 -070087 status_t err =
88 type->emitJavaTypeDeclarations(out, true /* atTopLevel */);
89
90 if (err != OK) {
91 return err;
92 }
93 }
94
95 return OK;
96}
97
Andreas Huber0fa9e392016-08-31 09:05:44 -070098status_t AST::generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -070099 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700100 if (!isJavaCompatible()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700101 fprintf(stderr,
102 "ERROR: This interface is not Java compatible. The Java backend"
Andreas Huberf03332a2016-09-22 15:35:43 -0700103 " does NOT support union types nor native handles. "
104 "In addition, vectors of arrays are limited to at most "
Andreas Huber86a112b2016-10-19 14:25:16 -0700105 "one-dimensional arrays and vectors of {vectors,interfaces} are"
106 " not supported.\n");
Andreas Huber70a59e12016-08-16 12:57:01 -0700107
Andreas Huber2831d512016-08-15 09:33:47 -0700108 return UNKNOWN_ERROR;
109 }
110
Andreas Huber0fa9e392016-08-31 09:05:44 -0700111 std::string ifaceName;
112 if (!AST::isInterface(&ifaceName)) {
113 return generateJavaTypes(outputPath, limitToType);
114 }
115
116 const Interface *iface = mRootScope->getInterface();
117
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700118 const std::string baseName = iface->getBaseName();
Andreas Huber2831d512016-08-15 09:33:47 -0700119
120 std::string path = outputPath;
121 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Yifan Hong97288ac2016-12-12 16:03:51 -0800122 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */,
123 true /* sanitized */));
Andreas Huber2831d512016-08-15 09:33:47 -0700124 path.append(ifaceName);
125 path.append(".java");
126
127 CHECK(Coordinator::MakeParentHierarchy(path));
128 FILE *file = fopen(path.c_str(), "w");
129
130 if (file == NULL) {
131 return -errno;
132 }
133
134 Formatter out(file);
135
136 std::vector<std::string> packageComponents;
137 getPackageAndVersionComponents(
138 &packageComponents, true /* cpp_compatible */);
139
140 out << "package " << mPackage.javaPackage() << ";\n\n";
141
Andreas Huber2831d512016-08-15 09:33:47 -0700142 out.setNamespace(mPackage.javaPackage() + ".");
143
Andreas Huber2831d512016-08-15 09:33:47 -0700144 const Interface *superType = iface->superType();
145
146 out << "public interface " << ifaceName << " extends ";
147
148 if (superType != NULL) {
149 out << superType->fullJavaName();
150 } else {
Yifan Hong1af73532016-11-09 14:32:58 -0800151 out << "android.os.IHwInterface";
Andreas Huber2831d512016-08-15 09:33:47 -0700152 }
153
154 out << " {\n";
155 out.indent();
156
157 out << "public static final String kInterfaceName = \""
158 << mPackage.string()
159 << "::"
160 << ifaceName
161 << "\";\n\n";
162
163 out << "public static "
164 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800165 << " asInterface(android.os.IHwBinder binder) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700166
167 out.indent();
168
169 out << "if (binder == null) {\n";
170 out.indent();
171 out << "return null;\n";
172 out.unindent();
173 out << "}\n\n";
174
Yifan Hong1af73532016-11-09 14:32:58 -0800175 out << "android.os.IHwInterface iface =\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700176 out.indent();
177 out.indent();
178 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
179 out.unindent();
180 out.unindent();
181
182 out << "if ((iface != null) && (iface instanceof "
183 << ifaceName
184 << ")) {\n";
185
186 out.indent();
187 out << "return (" << ifaceName << ")iface;\n";
188 out.unindent();
189 out << "}\n\n";
190
191 out << "return new " << ifaceName << ".Proxy(binder);\n";
192
193 out.unindent();
194 out << "}\n\n";
195
Yifan Hong084d11f2016-12-21 15:33:43 -0800196 out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700197
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700198 out << "public static "
199 << ifaceName
Yifan Hong320a3492017-03-27 10:33:09 -0700200 << " getService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700201
202 out.indent();
203
204 out << "return "
205 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800206 << ".asInterface(android.os.HwBinder.getService(\""
Steven Moreland7e367bf2016-11-04 11:31:41 -0700207 << iface->fqName().string()
208 << "\",serviceName));\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700209
210 out.unindent();
211
212 out << "}\n\n";
213
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800214 out << "public static "
215 << ifaceName
Yifan Hong320a3492017-03-27 10:33:09 -0700216 << " getService() throws android.os.RemoteException {\n";
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800217
218 out.indent();
219
220 out << "return "
221 << ifaceName
222 << ".asInterface(android.os.HwBinder.getService(\""
223 << iface->fqName().string()
224 << "\",\"default\"));\n";
225
226 out.unindent();
227
228 out << "}\n\n";
229
Andreas Huber2831d512016-08-15 09:33:47 -0700230 status_t err = emitJavaTypeDeclarations(out);
231
232 if (err != OK) {
233 return err;
234 }
235
Andreas Huber2831d512016-08-15 09:33:47 -0700236 for (const auto &method : iface->methods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800237 if (method->isHiddenFromJava()) {
238 continue;
239 }
240
Andreas Huber2831d512016-08-15 09:33:47 -0700241 const bool returnsValue = !method->results().empty();
242 const bool needsCallback = method->results().size() > 1;
243
244 if (needsCallback) {
Steven Morelandbcd79032016-12-16 20:50:23 -0800245 out << "\npublic interface "
Andreas Huber2831d512016-08-15 09:33:47 -0700246 << method->name()
247 << "Callback {\n";
248
249 out.indent();
250
Yifan Hong932464e2017-03-30 15:40:22 -0700251 out << "public void onValues(";
252 method->emitJavaResultSignature(out);
253 out << ");\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700254
255 out.unindent();
256 out << "}\n\n";
257 }
258
259 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700260 out << method->results()[0]->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700261 } else {
262 out << "void";
263 }
264
265 out << " "
266 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700267 << "(";
268 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700269
270 if (needsCallback) {
271 if (!method->args().empty()) {
272 out << ", ";
273 }
274
275 out << method->name()
276 << "Callback cb";
277 }
278
Steven Morelanddad1b302016-12-20 15:56:00 -0800279 out << ")\n";
280 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700281 out << "throws android.os.RemoteException;\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800282 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700283 }
284
285 out << "\npublic static final class Proxy implements "
286 << ifaceName
287 << " {\n";
288
289 out.indent();
290
Yifan Hong1af73532016-11-09 14:32:58 -0800291 out << "private android.os.IHwBinder mRemote;\n\n";
292 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700293 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800294 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700295 out.unindent();
296 out << "}\n\n";
297
Yifan Hong084d11f2016-12-21 15:33:43 -0800298 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700299 out.indent();
300 out << "return mRemote;\n";
301 out.unindent();
302 out << "}\n\n";
303
Yifan Honge45b5302017-02-22 10:49:07 -0800304
305 out << "@Override\npublic String toString() ";
306 out.block([&] {
307 out.sTry([&] {
308 out << "return this.interfaceDescriptor() + \"@Proxy\";\n";
Yifan Hong320a3492017-03-27 10:33:09 -0700309 }).sCatch("android.os.RemoteException ex", [&] {
Yifan Honge45b5302017-02-22 10:49:07 -0800310 out << "/* ignored; handled below. */\n";
311 }).endl();
312 out << "return \"[class or subclass of \" + "
313 << ifaceName << ".kInterfaceName + \"]@Proxy\";\n";
314 }).endl().endl();
315
Yifan Hong10fe0b52016-10-19 14:20:17 -0700316 const Interface *prevInterface = nullptr;
317 for (const auto &tuple : iface->allMethodsFromRoot()) {
318 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800319
320 if (method->isHiddenFromJava()) {
321 continue;
322 }
323
Yifan Hong10fe0b52016-10-19 14:20:17 -0700324 const Interface *superInterface = tuple.interface();
325 if (prevInterface != superInterface) {
326 out << "// Methods from "
327 << superInterface->fullName()
328 << " follow.\n";
329 prevInterface = superInterface;
330 }
331 const bool returnsValue = !method->results().empty();
332 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700333
Yifan Hong084d11f2016-12-21 15:33:43 -0800334 out << "@Override\npublic ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700335 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700336 out << method->results()[0]->type().getJavaType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700337 } else {
338 out << "void";
339 }
Andreas Huber2831d512016-08-15 09:33:47 -0700340
Yifan Hong10fe0b52016-10-19 14:20:17 -0700341 out << " "
342 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700343 << "(";
344 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700345
Yifan Hong10fe0b52016-10-19 14:20:17 -0700346 if (needsCallback) {
347 if (!method->args().empty()) {
348 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700349 }
350
Yifan Hong10fe0b52016-10-19 14:20:17 -0700351 out << method->name()
352 << "Callback cb";
353 }
Andreas Huber2831d512016-08-15 09:33:47 -0700354
Steven Morelanddad1b302016-12-20 15:56:00 -0800355 out << ")\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700356 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800357 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700358 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800359 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700360
Martijn Coenen115d4282016-12-19 05:14:04 +0100361 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
362 method->javaImpl(IMPL_PROXY, out);
363 out.unindent();
364 out << "}\n";
365 continue;
366 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800367 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
368 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700369 << superInterface->fullJavaName()
370 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700371
Yifan Hong10fe0b52016-10-19 14:20:17 -0700372 for (const auto &arg : method->args()) {
373 emitJavaReaderWriter(
374 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800375 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700376 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800377 false /* isReader */,
378 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700379 }
Andreas Huber2831d512016-08-15 09:33:47 -0700380
Martijn Coenen4de083b2017-03-16 18:49:43 +0100381 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700382
Martijn Coenen4de083b2017-03-16 18:49:43 +0100383 out.sTry([&] {
384 out << "mRemote.transact("
385 << method->getSerialId()
386 << " /* "
387 << method->name()
388 << " */, _hidl_request, _hidl_reply, ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700389
Martijn Coenen4de083b2017-03-16 18:49:43 +0100390 if (method->isOneway()) {
391 out << "android.os.IHwBinder.FLAG_ONEWAY";
392 } else {
393 out << "0 /* flags */";
Andreas Huber2831d512016-08-15 09:33:47 -0700394 }
395
Martijn Coenen4de083b2017-03-16 18:49:43 +0100396 out << ");\n";
Andreas Huber8b5da222016-08-18 14:28:18 -0700397
Martijn Coenen4de083b2017-03-16 18:49:43 +0100398 if (!method->isOneway()) {
399 out << "_hidl_reply.verifySuccess();\n";
400 } else {
401 CHECK(!returnsValue);
402 }
403
404 out << "_hidl_request.releaseTemporaryStorage();\n";
405
406 if (returnsValue) {
407 out << "\n";
408
Andreas Huber2831d512016-08-15 09:33:47 -0700409 for (const auto &arg : method->results()) {
Martijn Coenen4de083b2017-03-16 18:49:43 +0100410 emitJavaReaderWriter(
411 out,
412 "_hidl_reply",
413 arg,
414 true /* isReader */,
415 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700416 }
Andreas Huber2831d512016-08-15 09:33:47 -0700417
Martijn Coenen4de083b2017-03-16 18:49:43 +0100418 if (needsCallback) {
419 out << "cb.onValues(";
420
421 bool firstField = true;
422 for (const auto &arg : method->results()) {
423 if (!firstField) {
424 out << ", ";
425 }
426
427 out << "_hidl_out_" << arg->name();
428 firstField = false;
429 }
430
431 out << ");\n";
432 } else {
433 const std::string returnName = method->results()[0]->name();
434 out << "return _hidl_out_" << returnName << ";\n";
435 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700436 }
Martijn Coenen4de083b2017-03-16 18:49:43 +0100437 }).sFinally([&] {
438 out << "_hidl_reply.release();\n";
439 }).endl();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700440
441 out.unindent();
442 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700443 }
444
445 out.unindent();
446 out << "}\n";
447
448 ////////////////////////////////////////////////////////////////////////////
449
Yifan Hong1af73532016-11-09 14:32:58 -0800450 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700451 << "implements "
452 << ifaceName << " {\n";
453
454 out.indent();
455
Yifan Hong084d11f2016-12-21 15:33:43 -0800456 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700457 out.indent();
458 out << "return this;\n";
459 out.unindent();
460 out << "}\n\n";
461
Yifan Hong10fe0b52016-10-19 14:20:17 -0700462 for (Method *method : iface->hidlReservedMethods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800463 if (method->isHiddenFromJava()) {
464 continue;
465 }
466
Martijn Coenenaf712c02016-11-16 15:26:27 +0100467 // b/32383557 this is a hack. We need to change this if we have more reserved methods.
468 CHECK_LE(method->results().size(), 1u);
469 std::string resultType = method->results().size() == 0 ? "void" :
470 method->results()[0]->type().getJavaType();
Yifan Hong084d11f2016-12-21 15:33:43 -0800471 out << "@Override\npublic final "
Martijn Coenenaf712c02016-11-16 15:26:27 +0100472 << resultType
Yifan Hong10fe0b52016-10-19 14:20:17 -0700473 << " "
474 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700475 << "(";
476 method->emitJavaArgSignature(out);
477 out << ") {\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100478
Yifan Hong10fe0b52016-10-19 14:20:17 -0700479 out.indent();
Steven Moreland937408a2017-03-20 09:54:18 -0700480 method->javaImpl(IMPL_INTERFACE, out);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700481 out.unindent();
482 out << "\n}\n\n";
483 }
484
Yifan Hong084d11f2016-12-21 15:33:43 -0800485 out << "@Override\n"
486 << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700487 out.indent();
488 // XXX what about potential superClasses?
489 out << "if (kInterfaceName.equals(descriptor)) {\n";
490 out.indent();
491 out << "return this;\n";
492 out.unindent();
493 out << "}\n";
494 out << "return null;\n";
495 out.unindent();
496 out << "}\n\n";
497
Yifan Hong320a3492017-03-27 10:33:09 -0700498 out << "public void registerAsService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700499 out.indent();
500
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100501 out << "registerService(serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700502
503 out.unindent();
504 out << "}\n\n";
505
Yifan Honge45b5302017-02-22 10:49:07 -0800506 out << "@Override\npublic String toString() ";
507 out.block([&] {
508 out << "return this.interfaceDescriptor() + \"@Stub\";\n";
509 }).endl().endl();
510
Yifan Hong084d11f2016-12-21 15:33:43 -0800511 out << "@Override\n"
512 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800513 << "int _hidl_code, "
514 << "android.os.HwParcel _hidl_request, "
515 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800516 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700517 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800518 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700519 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800520 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700521
Steven Moreland4b39bc12016-11-16 10:42:24 -0800522 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700523
524 out.indent();
525
Yifan Hong10fe0b52016-10-19 14:20:17 -0700526 for (const auto &tuple : iface->allMethodsFromRoot()) {
527 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800528
Yifan Hong10fe0b52016-10-19 14:20:17 -0700529 const Interface *superInterface = tuple.interface();
530 const bool returnsValue = !method->results().empty();
531 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700532
Yifan Hong10fe0b52016-10-19 14:20:17 -0700533 out << "case "
534 << method->getSerialId()
535 << " /* "
536 << method->name()
537 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700538
Yifan Hong10fe0b52016-10-19 14:20:17 -0700539 out.indent();
Andreas Huber37065d62017-02-07 14:36:54 -0800540
Martijn Coenen115d4282016-12-19 05:14:04 +0100541 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
542 method->javaImpl(IMPL_STUB, out);
543 out.unindent();
544 out << "break;\n";
545 out << "}\n\n";
546 continue;
547 }
Andreas Huber2831d512016-08-15 09:33:47 -0700548
Steven Moreland4b39bc12016-11-16 10:42:24 -0800549 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700550 << superInterface->fullJavaName()
551 << ".kInterfaceName);\n\n";
552
Andreas Huber37065d62017-02-07 14:36:54 -0800553 if (method->isHiddenFromJava()) {
554 // This is a method hidden from the Java side of things, it must not
555 // return any value and will simply signal success.
556 CHECK(!returnsValue);
557
558 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
559 out << "_hidl_reply.send();\n";
560 out << "break;\n";
561 out.unindent();
562 out << "}\n\n";
563 continue;
564 }
565
Yifan Hong10fe0b52016-10-19 14:20:17 -0700566 for (const auto &arg : method->args()) {
567 emitJavaReaderWriter(
568 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800569 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700570 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800571 true /* isReader */,
572 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700573 }
574
575 if (!needsCallback && returnsValue) {
576 const TypedVar *returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700577
Yifan Hong4ed13472016-11-02 10:44:11 -0700578 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800579 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700580 << returnArg->name()
581 << " = ";
582 }
583
584 out << method->name()
585 << "(";
586
587 bool firstField = true;
588 for (const auto &arg : method->args()) {
589 if (!firstField) {
590 out << ", ";
591 }
592
593 out << arg->name();
594
595 firstField = false;
596 }
597
598 if (needsCallback) {
599 if (!firstField) {
600 out << ", ";
601 }
602
603 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700604 out.indent();
605
Yifan Hong10fe0b52016-10-19 14:20:17 -0700606 out << "@Override\n"
Yifan Hong932464e2017-03-30 15:40:22 -0700607 << "public void onValues(";
608 method->emitJavaResultSignature(out);
609 out << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700610
Yifan Hong10fe0b52016-10-19 14:20:17 -0700611 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800612 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700613
614 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700615 emitJavaReaderWriter(
616 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800617 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700618 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800619 false /* isReader */,
620 false /* addPrefixToName */);
621 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700622 }
623
Steven Moreland4b39bc12016-11-16 10:42:24 -0800624 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700625 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700626
Andreas Huber2831d512016-08-15 09:33:47 -0700627 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700628 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700629 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700630
631 out << ");\n";
632
Martijn Coenen0bb0d412017-02-08 10:21:30 +0100633 if (!needsCallback && !method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800634 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700635
636 if (returnsValue) {
637 const TypedVar *returnArg = method->results()[0];
638
639 emitJavaReaderWriter(
640 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800641 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700642 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800643 false /* isReader */,
644 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700645 }
646
Steven Moreland4b39bc12016-11-16 10:42:24 -0800647 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700648 }
649
650 out << "break;\n";
651 out.unindent();
652 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700653 }
654
655 out.unindent();
656 out << "}\n";
657
658 out.unindent();
659 out << "}\n";
660
661 out.unindent();
662 out << "}\n";
663
664 out.unindent();
665 out << "}\n";
666
667 return OK;
668}
669
670status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700671 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700672}
673
674} // namespace android