blob: 56ac2d6835e34040bd9b8db3f20070a5e445cdad [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
Steven Morelanddad1b302016-12-20 15:56:00 -0800142 out << "import android.os.RemoteException;\n\n";
143
Andreas Huber2831d512016-08-15 09:33:47 -0700144 out.setNamespace(mPackage.javaPackage() + ".");
145
Andreas Huber2831d512016-08-15 09:33:47 -0700146 const Interface *superType = iface->superType();
147
148 out << "public interface " << ifaceName << " extends ";
149
150 if (superType != NULL) {
151 out << superType->fullJavaName();
152 } else {
Yifan Hong1af73532016-11-09 14:32:58 -0800153 out << "android.os.IHwInterface";
Andreas Huber2831d512016-08-15 09:33:47 -0700154 }
155
156 out << " {\n";
157 out.indent();
158
159 out << "public static final String kInterfaceName = \""
160 << mPackage.string()
161 << "::"
162 << ifaceName
163 << "\";\n\n";
164
165 out << "public static "
166 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800167 << " asInterface(android.os.IHwBinder binder) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700168
169 out.indent();
170
171 out << "if (binder == null) {\n";
172 out.indent();
173 out << "return null;\n";
174 out.unindent();
175 out << "}\n\n";
176
Yifan Hong1af73532016-11-09 14:32:58 -0800177 out << "android.os.IHwInterface iface =\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700178 out.indent();
179 out.indent();
180 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
181 out.unindent();
182 out.unindent();
183
184 out << "if ((iface != null) && (iface instanceof "
185 << ifaceName
186 << ")) {\n";
187
188 out.indent();
189 out << "return (" << ifaceName << ")iface;\n";
190 out.unindent();
191 out << "}\n\n";
192
193 out << "return new " << ifaceName << ".Proxy(binder);\n";
194
195 out.unindent();
196 out << "}\n\n";
197
Yifan Hong084d11f2016-12-21 15:33:43 -0800198 out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700199
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700200 out << "public static "
201 << ifaceName
Steven Moreland64af5e82017-01-04 10:58:55 -0800202 << " getService(String serviceName) throws RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700203
204 out.indent();
205
206 out << "return "
207 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800208 << ".asInterface(android.os.HwBinder.getService(\""
Steven Moreland7e367bf2016-11-04 11:31:41 -0700209 << iface->fqName().string()
210 << "\",serviceName));\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700211
212 out.unindent();
213
214 out << "}\n\n";
215
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800216 out << "public static "
217 << ifaceName
218 << " getService() throws RemoteException {\n";
219
220 out.indent();
221
222 out << "return "
223 << ifaceName
224 << ".asInterface(android.os.HwBinder.getService(\""
225 << iface->fqName().string()
226 << "\",\"default\"));\n";
227
228 out.unindent();
229
230 out << "}\n\n";
231
Andreas Huber2831d512016-08-15 09:33:47 -0700232 status_t err = emitJavaTypeDeclarations(out);
233
234 if (err != OK) {
235 return err;
236 }
237
Andreas Huber2831d512016-08-15 09:33:47 -0700238 for (const auto &method : iface->methods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800239 if (method->isHiddenFromJava()) {
240 continue;
241 }
242
Andreas Huber2831d512016-08-15 09:33:47 -0700243 const bool returnsValue = !method->results().empty();
244 const bool needsCallback = method->results().size() > 1;
245
246 if (needsCallback) {
Steven Morelandbcd79032016-12-16 20:50:23 -0800247 out << "\npublic interface "
Andreas Huber2831d512016-08-15 09:33:47 -0700248 << method->name()
249 << "Callback {\n";
250
251 out.indent();
252
Steven Morelandbcd79032016-12-16 20:50:23 -0800253 out << "public void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700254 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700255 << ");\n";
256
257 out.unindent();
258 out << "}\n\n";
259 }
260
261 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700262 out << method->results()[0]->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700263 } else {
264 out << "void";
265 }
266
267 out << " "
268 << method->name()
269 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700270 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700271
272 if (needsCallback) {
273 if (!method->args().empty()) {
274 out << ", ";
275 }
276
277 out << method->name()
278 << "Callback cb";
279 }
280
Steven Morelanddad1b302016-12-20 15:56:00 -0800281 out << ")\n";
282 out.indent();
283 out << "throws RemoteException;\n";
284 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700285 }
286
287 out << "\npublic static final class Proxy implements "
288 << ifaceName
289 << " {\n";
290
291 out.indent();
292
Yifan Hong1af73532016-11-09 14:32:58 -0800293 out << "private android.os.IHwBinder mRemote;\n\n";
294 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700295 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800296 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700297 out.unindent();
298 out << "}\n\n";
299
Yifan Hong084d11f2016-12-21 15:33:43 -0800300 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700301 out.indent();
302 out << "return mRemote;\n";
303 out.unindent();
304 out << "}\n\n";
305
Yifan Hong10fe0b52016-10-19 14:20:17 -0700306 const Interface *prevInterface = nullptr;
307 for (const auto &tuple : iface->allMethodsFromRoot()) {
308 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800309
310 if (method->isHiddenFromJava()) {
311 continue;
312 }
313
Yifan Hong10fe0b52016-10-19 14:20:17 -0700314 const Interface *superInterface = tuple.interface();
315 if (prevInterface != superInterface) {
316 out << "// Methods from "
317 << superInterface->fullName()
318 << " follow.\n";
319 prevInterface = superInterface;
320 }
321 const bool returnsValue = !method->results().empty();
322 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700323
Yifan Hong084d11f2016-12-21 15:33:43 -0800324 out << "@Override\npublic ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700325 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700326 out << method->results()[0]->type().getJavaType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700327 } else {
328 out << "void";
329 }
Andreas Huber2831d512016-08-15 09:33:47 -0700330
Yifan Hong10fe0b52016-10-19 14:20:17 -0700331 out << " "
332 << method->name()
333 << "("
334 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700335
Yifan Hong10fe0b52016-10-19 14:20:17 -0700336 if (needsCallback) {
337 if (!method->args().empty()) {
338 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700339 }
340
Yifan Hong10fe0b52016-10-19 14:20:17 -0700341 out << method->name()
342 << "Callback cb";
343 }
Andreas Huber2831d512016-08-15 09:33:47 -0700344
Steven Morelanddad1b302016-12-20 15:56:00 -0800345 out << ")\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700346 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800347 out.indent();
348 out << "throws RemoteException {\n";
349 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700350
Martijn Coenen115d4282016-12-19 05:14:04 +0100351 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
352 method->javaImpl(IMPL_PROXY, out);
353 out.unindent();
354 out << "}\n";
355 continue;
356 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800357 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
358 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700359 << superInterface->fullJavaName()
360 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700361
Yifan Hong10fe0b52016-10-19 14:20:17 -0700362 for (const auto &arg : method->args()) {
363 emitJavaReaderWriter(
364 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800365 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700366 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800367 false /* isReader */,
368 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700369 }
Andreas Huber2831d512016-08-15 09:33:47 -0700370
Steven Moreland4b39bc12016-11-16 10:42:24 -0800371 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700372 << "mRemote.transact("
373 << method->getSerialId()
374 << " /* "
375 << method->name()
Steven Moreland4b39bc12016-11-16 10:42:24 -0800376 << " */, _hidl_request, _hidl_reply, ";
Andreas Huber2831d512016-08-15 09:33:47 -0700377
Yifan Hong10fe0b52016-10-19 14:20:17 -0700378 if (method->isOneway()) {
Yifan Hong1af73532016-11-09 14:32:58 -0800379 out << "android.os.IHwBinder.FLAG_ONEWAY";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700380 } else {
381 out << "0 /* flags */";
382 }
383
384 out << ");\n";
385
386 if (!method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800387 out << "_hidl_reply.verifySuccess();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700388 } else {
389 CHECK(!returnsValue);
390 }
391
Steven Moreland4b39bc12016-11-16 10:42:24 -0800392 out << "_hidl_request.releaseTemporaryStorage();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700393
394 if (returnsValue) {
395 out << "\n";
396
397 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700398 emitJavaReaderWriter(
399 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800400 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700401 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800402 true /* isReader */,
403 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700404 }
405
Yifan Hong10fe0b52016-10-19 14:20:17 -0700406 if (needsCallback) {
407 out << "cb.onValues(";
Andreas Huber8b5da222016-08-18 14:28:18 -0700408
Yifan Hong10fe0b52016-10-19 14:20:17 -0700409 bool firstField = true;
Andreas Huber2831d512016-08-15 09:33:47 -0700410 for (const auto &arg : method->results()) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700411 if (!firstField) {
412 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700413 }
414
Yifan Honga47eef32016-12-12 10:38:54 -0800415 out << "_hidl_out_" << arg->name();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700416 firstField = false;
Andreas Huber2831d512016-08-15 09:33:47 -0700417 }
Andreas Huber2831d512016-08-15 09:33:47 -0700418
Yifan Hong10fe0b52016-10-19 14:20:17 -0700419 out << ");\n";
420 } else {
421 const std::string returnName = method->results()[0]->name();
Yifan Honga47eef32016-12-12 10:38:54 -0800422 out << "return _hidl_out_" << returnName << ";\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700423 }
Andreas Huber2831d512016-08-15 09:33:47 -0700424 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700425
426 out.unindent();
427 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700428 }
429
430 out.unindent();
431 out << "}\n";
432
433 ////////////////////////////////////////////////////////////////////////////
434
Yifan Hong1af73532016-11-09 14:32:58 -0800435 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700436 << "implements "
437 << ifaceName << " {\n";
438
439 out.indent();
440
Yifan Hong084d11f2016-12-21 15:33:43 -0800441 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700442 out.indent();
443 out << "return this;\n";
444 out.unindent();
445 out << "}\n\n";
446
Yifan Hong10fe0b52016-10-19 14:20:17 -0700447 for (Method *method : iface->hidlReservedMethods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800448 if (method->isHiddenFromJava()) {
449 continue;
450 }
451
Martijn Coenenaf712c02016-11-16 15:26:27 +0100452 // b/32383557 this is a hack. We need to change this if we have more reserved methods.
453 CHECK_LE(method->results().size(), 1u);
454 std::string resultType = method->results().size() == 0 ? "void" :
455 method->results()[0]->type().getJavaType();
Yifan Hong084d11f2016-12-21 15:33:43 -0800456 out << "@Override\npublic final "
Martijn Coenenaf712c02016-11-16 15:26:27 +0100457 << resultType
Yifan Hong10fe0b52016-10-19 14:20:17 -0700458 << " "
459 << method->name()
Martijn Coenen115d4282016-12-19 05:14:04 +0100460 << "("
461 << Method::GetJavaArgSignature(method->args())
462 << ") {\n";
463
Yifan Hong10fe0b52016-10-19 14:20:17 -0700464 out.indent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100465 method->javaImpl(IMPL_HEADER, out);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700466 out.unindent();
467 out << "\n}\n\n";
468 }
469
Yifan Hong084d11f2016-12-21 15:33:43 -0800470 out << "@Override\n"
471 << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700472 out.indent();
473 // XXX what about potential superClasses?
474 out << "if (kInterfaceName.equals(descriptor)) {\n";
475 out.indent();
476 out << "return this;\n";
477 out.unindent();
478 out << "}\n";
479 out << "return null;\n";
480 out.unindent();
481 out << "}\n\n";
482
Steven Moreland64af5e82017-01-04 10:58:55 -0800483 out << "public void registerAsService(String serviceName) throws RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700484 out.indent();
485
Steven Moreland7e367bf2016-11-04 11:31:41 -0700486 out << "registerService(interfaceChain(), serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700487
488 out.unindent();
489 out << "}\n\n";
490
Yifan Hong084d11f2016-12-21 15:33:43 -0800491 out << "@Override\n"
492 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800493 << "int _hidl_code, "
494 << "android.os.HwParcel _hidl_request, "
495 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800496 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700497 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800498 out.indent();
499 out << "throws RemoteException {\n";
500 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700501
Steven Moreland4b39bc12016-11-16 10:42:24 -0800502 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700503
504 out.indent();
505
Yifan Hong10fe0b52016-10-19 14:20:17 -0700506 for (const auto &tuple : iface->allMethodsFromRoot()) {
507 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800508
Yifan Hong10fe0b52016-10-19 14:20:17 -0700509 const Interface *superInterface = tuple.interface();
510 const bool returnsValue = !method->results().empty();
511 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700512
Yifan Hong10fe0b52016-10-19 14:20:17 -0700513 out << "case "
514 << method->getSerialId()
515 << " /* "
516 << method->name()
517 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700518
Yifan Hong10fe0b52016-10-19 14:20:17 -0700519 out.indent();
Andreas Huber37065d62017-02-07 14:36:54 -0800520
Martijn Coenen115d4282016-12-19 05:14:04 +0100521 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
522 method->javaImpl(IMPL_STUB, out);
523 out.unindent();
524 out << "break;\n";
525 out << "}\n\n";
526 continue;
527 }
Andreas Huber2831d512016-08-15 09:33:47 -0700528
Steven Moreland4b39bc12016-11-16 10:42:24 -0800529 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700530 << superInterface->fullJavaName()
531 << ".kInterfaceName);\n\n";
532
Andreas Huber37065d62017-02-07 14:36:54 -0800533 if (method->isHiddenFromJava()) {
534 // This is a method hidden from the Java side of things, it must not
535 // return any value and will simply signal success.
536 CHECK(!returnsValue);
537
538 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
539 out << "_hidl_reply.send();\n";
540 out << "break;\n";
541 out.unindent();
542 out << "}\n\n";
543 continue;
544 }
545
Yifan Hong10fe0b52016-10-19 14:20:17 -0700546 for (const auto &arg : method->args()) {
547 emitJavaReaderWriter(
548 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800549 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700550 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800551 true /* isReader */,
552 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700553 }
554
555 if (!needsCallback && returnsValue) {
556 const TypedVar *returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700557
Yifan Hong4ed13472016-11-02 10:44:11 -0700558 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800559 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700560 << returnArg->name()
561 << " = ";
562 }
563
564 out << method->name()
565 << "(";
566
567 bool firstField = true;
568 for (const auto &arg : method->args()) {
569 if (!firstField) {
570 out << ", ";
571 }
572
573 out << arg->name();
574
575 firstField = false;
576 }
577
578 if (needsCallback) {
579 if (!firstField) {
580 out << ", ";
581 }
582
583 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700584 out.indent();
585
Yifan Hong10fe0b52016-10-19 14:20:17 -0700586 out << "@Override\n"
587 << "public void onValues("
588 << Method::GetJavaArgSignature(method->results())
589 << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700590
Yifan Hong10fe0b52016-10-19 14:20:17 -0700591 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800592 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700593
594 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700595 emitJavaReaderWriter(
596 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800597 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700598 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800599 false /* isReader */,
600 false /* addPrefixToName */);
601 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700602 }
603
Steven Moreland4b39bc12016-11-16 10:42:24 -0800604 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700605 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700606
Andreas Huber2831d512016-08-15 09:33:47 -0700607 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700608 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700609 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700610
611 out << ");\n";
612
Martijn Coenen0bb0d412017-02-08 10:21:30 +0100613 if (!needsCallback && !method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800614 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700615
616 if (returnsValue) {
617 const TypedVar *returnArg = method->results()[0];
618
619 emitJavaReaderWriter(
620 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800621 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700622 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800623 false /* isReader */,
624 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700625 }
626
Steven Moreland4b39bc12016-11-16 10:42:24 -0800627 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700628 }
629
630 out << "break;\n";
631 out.unindent();
632 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700633 }
634
635 out.unindent();
636 out << "}\n";
637
638 out.unindent();
639 out << "}\n";
640
641 out.unindent();
642 out << "}\n";
643
644 out.unindent();
645 out << "}\n";
646
647 return OK;
648}
649
650status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700651 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700652}
653
654} // namespace android