blob: c0f876801b0f55a1dad0f125d11260d36db2ae29 [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
Steven Morelandf7fa0682017-05-11 16:14:55 -070070 CHECK(Coordinator::MakeParentHierarchy(path)) << path;
Andreas Huber85eabdb2016-08-25 11:24:49 -070071 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
Andreas Hubera2abe982017-04-04 14:42:09 -0700163 out << "/* package private */ static "
Andreas Huber2831d512016-08-15 09:33:47 -0700164 << 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
Andreas Hubera2abe982017-04-04 14:42:09 -0700191 out << ifaceName << " proxy = new " << ifaceName << ".Proxy(binder);\n\n";
192 out << "try {\n";
193 out.indent();
194 out << "for (String descriptor : proxy.interfaceChain()) {\n";
195 out.indent();
196 out << "if (descriptor.equals(kInterfaceName)) {\n";
197 out.indent();
198 out << "return proxy;\n";
199 out.unindent();
200 out << "}\n";
201 out.unindent();
202 out << "}\n";
203 out.unindent();
204 out << "} catch (android.os.RemoteException e) {\n";
205 out.indent();
206 out.unindent();
207 out << "}\n\n";
208
209 out << "return null;\n";
210
211 out.unindent();
212 out << "}\n\n";
213
214 out << "public static "
215 << ifaceName
216 << " castFrom(android.os.IHwInterface iface) {\n";
217 out.indent();
218
219 out << "return (iface == null) ? null : "
220 << ifaceName
221 << ".asInterface(iface.asBinder());\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700222
223 out.unindent();
224 out << "}\n\n";
225
Yifan Hong084d11f2016-12-21 15:33:43 -0800226 out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700227
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700228 out << "public static "
229 << ifaceName
Yifan Hong320a3492017-03-27 10:33:09 -0700230 << " getService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700231
232 out.indent();
233
234 out << "return "
235 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800236 << ".asInterface(android.os.HwBinder.getService(\""
Steven Moreland7e367bf2016-11-04 11:31:41 -0700237 << iface->fqName().string()
238 << "\",serviceName));\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700239
240 out.unindent();
241
242 out << "}\n\n";
243
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800244 out << "public static "
245 << ifaceName
Yifan Hong320a3492017-03-27 10:33:09 -0700246 << " getService() throws android.os.RemoteException {\n";
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800247
248 out.indent();
249
250 out << "return "
251 << ifaceName
252 << ".asInterface(android.os.HwBinder.getService(\""
253 << iface->fqName().string()
254 << "\",\"default\"));\n";
255
256 out.unindent();
257
258 out << "}\n\n";
259
Andreas Huber2831d512016-08-15 09:33:47 -0700260 status_t err = emitJavaTypeDeclarations(out);
261
262 if (err != OK) {
263 return err;
264 }
265
Andreas Huber2831d512016-08-15 09:33:47 -0700266 for (const auto &method : iface->methods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800267 if (method->isHiddenFromJava()) {
268 continue;
269 }
270
Andreas Huber2831d512016-08-15 09:33:47 -0700271 const bool returnsValue = !method->results().empty();
272 const bool needsCallback = method->results().size() > 1;
273
274 if (needsCallback) {
Steven Morelandbcd79032016-12-16 20:50:23 -0800275 out << "\npublic interface "
Andreas Huber2831d512016-08-15 09:33:47 -0700276 << method->name()
277 << "Callback {\n";
278
279 out.indent();
280
Yifan Hong932464e2017-03-30 15:40:22 -0700281 out << "public void onValues(";
282 method->emitJavaResultSignature(out);
283 out << ");\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700284
285 out.unindent();
286 out << "}\n\n";
287 }
288
289 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700290 out << method->results()[0]->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700291 } else {
292 out << "void";
293 }
294
295 out << " "
296 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700297 << "(";
298 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700299
300 if (needsCallback) {
301 if (!method->args().empty()) {
302 out << ", ";
303 }
304
305 out << method->name()
Steven Moreland4ddd8332017-04-27 19:27:18 -0700306 << "Callback _hidl_cb";
Andreas Huber2831d512016-08-15 09:33:47 -0700307 }
308
Steven Morelanddad1b302016-12-20 15:56:00 -0800309 out << ")\n";
310 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700311 out << "throws android.os.RemoteException;\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800312 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700313 }
314
315 out << "\npublic static final class Proxy implements "
316 << ifaceName
317 << " {\n";
318
319 out.indent();
320
Yifan Hong1af73532016-11-09 14:32:58 -0800321 out << "private android.os.IHwBinder mRemote;\n\n";
322 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700323 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800324 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700325 out.unindent();
326 out << "}\n\n";
327
Yifan Hong084d11f2016-12-21 15:33:43 -0800328 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700329 out.indent();
330 out << "return mRemote;\n";
331 out.unindent();
332 out << "}\n\n";
333
Yifan Honge45b5302017-02-22 10:49:07 -0800334
335 out << "@Override\npublic String toString() ";
336 out.block([&] {
337 out.sTry([&] {
338 out << "return this.interfaceDescriptor() + \"@Proxy\";\n";
Yifan Hong320a3492017-03-27 10:33:09 -0700339 }).sCatch("android.os.RemoteException ex", [&] {
Yifan Honge45b5302017-02-22 10:49:07 -0800340 out << "/* ignored; handled below. */\n";
341 }).endl();
342 out << "return \"[class or subclass of \" + "
343 << ifaceName << ".kInterfaceName + \"]@Proxy\";\n";
344 }).endl().endl();
345
Yifan Hong10fe0b52016-10-19 14:20:17 -0700346 const Interface *prevInterface = nullptr;
347 for (const auto &tuple : iface->allMethodsFromRoot()) {
348 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800349
350 if (method->isHiddenFromJava()) {
351 continue;
352 }
353
Yifan Hong10fe0b52016-10-19 14:20:17 -0700354 const Interface *superInterface = tuple.interface();
355 if (prevInterface != superInterface) {
356 out << "// Methods from "
357 << superInterface->fullName()
358 << " follow.\n";
359 prevInterface = superInterface;
360 }
361 const bool returnsValue = !method->results().empty();
362 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700363
Yifan Hong084d11f2016-12-21 15:33:43 -0800364 out << "@Override\npublic ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700365 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700366 out << method->results()[0]->type().getJavaType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700367 } else {
368 out << "void";
369 }
Andreas Huber2831d512016-08-15 09:33:47 -0700370
Yifan Hong10fe0b52016-10-19 14:20:17 -0700371 out << " "
372 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700373 << "(";
374 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700375
Yifan Hong10fe0b52016-10-19 14:20:17 -0700376 if (needsCallback) {
377 if (!method->args().empty()) {
378 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700379 }
380
Yifan Hong10fe0b52016-10-19 14:20:17 -0700381 out << method->name()
Steven Moreland4ddd8332017-04-27 19:27:18 -0700382 << "Callback _hidl_cb";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700383 }
Andreas Huber2831d512016-08-15 09:33:47 -0700384
Steven Morelanddad1b302016-12-20 15:56:00 -0800385 out << ")\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700386 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800387 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700388 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800389 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700390
Martijn Coenen115d4282016-12-19 05:14:04 +0100391 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
392 method->javaImpl(IMPL_PROXY, out);
393 out.unindent();
394 out << "}\n";
395 continue;
396 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800397 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
398 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700399 << superInterface->fullJavaName()
400 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700401
Yifan Hong10fe0b52016-10-19 14:20:17 -0700402 for (const auto &arg : method->args()) {
403 emitJavaReaderWriter(
404 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800405 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700406 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800407 false /* isReader */,
408 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700409 }
Andreas Huber2831d512016-08-15 09:33:47 -0700410
Martijn Coenen4de083b2017-03-16 18:49:43 +0100411 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700412
Martijn Coenen4de083b2017-03-16 18:49:43 +0100413 out.sTry([&] {
414 out << "mRemote.transact("
415 << method->getSerialId()
416 << " /* "
417 << method->name()
418 << " */, _hidl_request, _hidl_reply, ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700419
Martijn Coenen4de083b2017-03-16 18:49:43 +0100420 if (method->isOneway()) {
421 out << "android.os.IHwBinder.FLAG_ONEWAY";
422 } else {
423 out << "0 /* flags */";
Andreas Huber2831d512016-08-15 09:33:47 -0700424 }
425
Martijn Coenen4de083b2017-03-16 18:49:43 +0100426 out << ");\n";
Andreas Huber8b5da222016-08-18 14:28:18 -0700427
Martijn Coenen4de083b2017-03-16 18:49:43 +0100428 if (!method->isOneway()) {
429 out << "_hidl_reply.verifySuccess();\n";
430 } else {
431 CHECK(!returnsValue);
432 }
433
434 out << "_hidl_request.releaseTemporaryStorage();\n";
435
436 if (returnsValue) {
437 out << "\n";
438
Andreas Huber2831d512016-08-15 09:33:47 -0700439 for (const auto &arg : method->results()) {
Martijn Coenen4de083b2017-03-16 18:49:43 +0100440 emitJavaReaderWriter(
441 out,
442 "_hidl_reply",
443 arg,
444 true /* isReader */,
445 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700446 }
Andreas Huber2831d512016-08-15 09:33:47 -0700447
Martijn Coenen4de083b2017-03-16 18:49:43 +0100448 if (needsCallback) {
Steven Moreland4ddd8332017-04-27 19:27:18 -0700449 out << "_hidl_cb.onValues(";
Martijn Coenen4de083b2017-03-16 18:49:43 +0100450
451 bool firstField = true;
452 for (const auto &arg : method->results()) {
453 if (!firstField) {
454 out << ", ";
455 }
456
457 out << "_hidl_out_" << arg->name();
458 firstField = false;
459 }
460
461 out << ");\n";
462 } else {
463 const std::string returnName = method->results()[0]->name();
464 out << "return _hidl_out_" << returnName << ";\n";
465 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700466 }
Martijn Coenen4de083b2017-03-16 18:49:43 +0100467 }).sFinally([&] {
468 out << "_hidl_reply.release();\n";
469 }).endl();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700470
471 out.unindent();
472 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700473 }
474
475 out.unindent();
476 out << "}\n";
477
478 ////////////////////////////////////////////////////////////////////////////
479
Yifan Hong1af73532016-11-09 14:32:58 -0800480 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700481 << "implements "
482 << ifaceName << " {\n";
483
484 out.indent();
485
Yifan Hong084d11f2016-12-21 15:33:43 -0800486 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700487 out.indent();
488 out << "return this;\n";
489 out.unindent();
490 out << "}\n\n";
491
Yifan Hong10fe0b52016-10-19 14:20:17 -0700492 for (Method *method : iface->hidlReservedMethods()) {
Andreas Huber37065d62017-02-07 14:36:54 -0800493 if (method->isHiddenFromJava()) {
494 continue;
495 }
496
Martijn Coenenaf712c02016-11-16 15:26:27 +0100497 // b/32383557 this is a hack. We need to change this if we have more reserved methods.
498 CHECK_LE(method->results().size(), 1u);
499 std::string resultType = method->results().size() == 0 ? "void" :
500 method->results()[0]->type().getJavaType();
Yifan Hong084d11f2016-12-21 15:33:43 -0800501 out << "@Override\npublic final "
Martijn Coenenaf712c02016-11-16 15:26:27 +0100502 << resultType
Yifan Hong10fe0b52016-10-19 14:20:17 -0700503 << " "
504 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700505 << "(";
506 method->emitJavaArgSignature(out);
507 out << ") {\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100508
Yifan Hong10fe0b52016-10-19 14:20:17 -0700509 out.indent();
Steven Moreland937408a2017-03-20 09:54:18 -0700510 method->javaImpl(IMPL_INTERFACE, out);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700511 out.unindent();
512 out << "\n}\n\n";
513 }
514
Yifan Hong084d11f2016-12-21 15:33:43 -0800515 out << "@Override\n"
516 << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700517 out.indent();
518 // XXX what about potential superClasses?
519 out << "if (kInterfaceName.equals(descriptor)) {\n";
520 out.indent();
521 out << "return this;\n";
522 out.unindent();
523 out << "}\n";
524 out << "return null;\n";
525 out.unindent();
526 out << "}\n\n";
527
Yifan Hong320a3492017-03-27 10:33:09 -0700528 out << "public void registerAsService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700529 out.indent();
530
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100531 out << "registerService(serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700532
533 out.unindent();
534 out << "}\n\n";
535
Yifan Honge45b5302017-02-22 10:49:07 -0800536 out << "@Override\npublic String toString() ";
537 out.block([&] {
538 out << "return this.interfaceDescriptor() + \"@Stub\";\n";
539 }).endl().endl();
540
Yifan Hong084d11f2016-12-21 15:33:43 -0800541 out << "@Override\n"
542 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800543 << "int _hidl_code, "
544 << "android.os.HwParcel _hidl_request, "
545 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800546 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700547 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800548 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700549 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800550 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700551
Steven Moreland4b39bc12016-11-16 10:42:24 -0800552 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700553
554 out.indent();
555
Yifan Hong10fe0b52016-10-19 14:20:17 -0700556 for (const auto &tuple : iface->allMethodsFromRoot()) {
557 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800558
Yifan Hong10fe0b52016-10-19 14:20:17 -0700559 const Interface *superInterface = tuple.interface();
560 const bool returnsValue = !method->results().empty();
561 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700562
Yifan Hong10fe0b52016-10-19 14:20:17 -0700563 out << "case "
564 << method->getSerialId()
565 << " /* "
566 << method->name()
567 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700568
Yifan Hong10fe0b52016-10-19 14:20:17 -0700569 out.indent();
Andreas Huber37065d62017-02-07 14:36:54 -0800570
Martijn Coenen115d4282016-12-19 05:14:04 +0100571 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
572 method->javaImpl(IMPL_STUB, out);
573 out.unindent();
574 out << "break;\n";
575 out << "}\n\n";
576 continue;
577 }
Andreas Huber2831d512016-08-15 09:33:47 -0700578
Steven Moreland4b39bc12016-11-16 10:42:24 -0800579 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700580 << superInterface->fullJavaName()
581 << ".kInterfaceName);\n\n";
582
Andreas Huber37065d62017-02-07 14:36:54 -0800583 if (method->isHiddenFromJava()) {
584 // This is a method hidden from the Java side of things, it must not
585 // return any value and will simply signal success.
586 CHECK(!returnsValue);
587
588 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
589 out << "_hidl_reply.send();\n";
590 out << "break;\n";
591 out.unindent();
592 out << "}\n\n";
593 continue;
594 }
595
Yifan Hong10fe0b52016-10-19 14:20:17 -0700596 for (const auto &arg : method->args()) {
597 emitJavaReaderWriter(
598 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800599 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700600 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800601 true /* isReader */,
602 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700603 }
604
605 if (!needsCallback && returnsValue) {
606 const TypedVar *returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700607
Yifan Hong4ed13472016-11-02 10:44:11 -0700608 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800609 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700610 << returnArg->name()
611 << " = ";
612 }
613
614 out << method->name()
615 << "(";
616
617 bool firstField = true;
618 for (const auto &arg : method->args()) {
619 if (!firstField) {
620 out << ", ";
621 }
622
623 out << arg->name();
624
625 firstField = false;
626 }
627
628 if (needsCallback) {
629 if (!firstField) {
630 out << ", ";
631 }
632
633 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700634 out.indent();
635
Yifan Hong10fe0b52016-10-19 14:20:17 -0700636 out << "@Override\n"
Yifan Hong932464e2017-03-30 15:40:22 -0700637 << "public void onValues(";
638 method->emitJavaResultSignature(out);
639 out << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700640
Yifan Hong10fe0b52016-10-19 14:20:17 -0700641 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800642 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700643
644 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700645 emitJavaReaderWriter(
646 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800647 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700648 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800649 false /* isReader */,
650 false /* addPrefixToName */);
651 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700652 }
653
Steven Moreland4b39bc12016-11-16 10:42:24 -0800654 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700655 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700656
Andreas Huber2831d512016-08-15 09:33:47 -0700657 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700658 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700659 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700660
661 out << ");\n";
662
Martijn Coenen0bb0d412017-02-08 10:21:30 +0100663 if (!needsCallback && !method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800664 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700665
666 if (returnsValue) {
667 const TypedVar *returnArg = method->results()[0];
668
669 emitJavaReaderWriter(
670 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800671 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700672 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800673 false /* isReader */,
674 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700675 }
676
Steven Moreland4b39bc12016-11-16 10:42:24 -0800677 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700678 }
679
680 out << "break;\n";
681 out.unindent();
682 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700683 }
684
685 out.unindent();
686 out << "}\n";
687
688 out.unindent();
689 out << "}\n";
690
691 out.unindent();
692 out << "}\n";
693
694 out.unindent();
695 out << "}\n";
696
697 return OK;
698}
699
700status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700701 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700702}
703
704} // namespace android