blob: 33bd289d9b4d955486fff0f3e543cca8328f3898 [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"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070022#include "Reference.h"
Andreas Huber2831d512016-08-15 09:33:47 -070023#include "Scope.h"
24
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070025#include <hidl-util/Formatter.h>
Andreas Huber2831d512016-08-15 09:33:47 -070026#include <android-base/logging.h>
27
28namespace android {
29
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070030void AST::emitJavaReaderWriter(Formatter& out, const std::string& parcelObj,
31 const NamedReference<Type>* arg, bool isReader,
32 bool addPrefixToName) const {
Andreas Huber2831d512016-08-15 09:33:47 -070033 if (isReader) {
Yifan Hong4ed13472016-11-02 10:44:11 -070034 out << arg->type().getJavaType()
Andreas Huber2831d512016-08-15 09:33:47 -070035 << " "
Yifan Honga47eef32016-12-12 10:38:54 -080036 << (addPrefixToName ? "_hidl_out_" : "")
Andreas Huber2831d512016-08-15 09:33:47 -070037 << arg->name()
38 << " = ";
39 }
40
Yifan Honga47eef32016-12-12 10:38:54 -080041 arg->type().emitJavaReaderWriter(out, parcelObj,
42 (addPrefixToName ? "_hidl_out_" : "") + arg->name(),
43 isReader);
Andreas Huber2831d512016-08-15 09:33:47 -070044}
45
Steven Moreland368e4602018-02-16 14:21:49 -080046void AST::generateJavaTypes(Formatter& out, const std::string& limitToType) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070047 // Splits types.hal up into one java file per declared type.
Steven Moreland5abcf012018-02-08 18:50:18 -080048 CHECK(!limitToType.empty()) << getFilename();
Andreas Huber85eabdb2016-08-25 11:24:49 -070049
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070050 for (const auto& type : mRootScope.getSubTypes()) {
Steven Morelandd537ab02016-09-12 10:32:01 -070051 std::string typeName = type->localName();
Andreas Huber85eabdb2016-08-25 11:24:49 -070052
Steven Moreland5abcf012018-02-08 18:50:18 -080053 if (type->isTypeDef()) continue;
54 if (typeName != limitToType) continue;
Andreas Huber85eabdb2016-08-25 11:24:49 -070055
Andreas Huber85eabdb2016-08-25 11:24:49 -070056 std::vector<std::string> packageComponents;
57 getPackageAndVersionComponents(
58 &packageComponents, true /* cpp_compatible */);
59
Steven Moreland5abcf012018-02-08 18:50:18 -080060 out << "package " << mPackage.javaPackage() << ";\n\n\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -070061
Steven Moreland368e4602018-02-16 14:21:49 -080062 type->emitJavaTypeDeclarations(out, true /* atTopLevel */);
63 return;
Andreas Huber85eabdb2016-08-25 11:24:49 -070064 }
65
Steven Moreland5abcf012018-02-08 18:50:18 -080066 CHECK(false) << "generateJavaTypes could not find limitToType type";
Andreas Huber85eabdb2016-08-25 11:24:49 -070067}
68
Steven Moreland9bf5a092017-10-25 04:50:54 +000069void emitGetService(
70 Formatter& out,
71 const std::string& ifaceName,
72 const std::string& fqName,
73 bool isRetry) {
74 out << "public static "
75 << ifaceName
76 << " getService(String serviceName";
77 if (isRetry) {
78 out << ", boolean retry";
79 }
80 out << ") throws android.os.RemoteException ";
81 out.block([&] {
82 out << "return "
83 << ifaceName
84 << ".asInterface(android.os.HwBinder.getService(\""
85 << fqName
86 << "\", serviceName";
87 if (isRetry) {
88 out << ", retry";
89 }
90 out << "));\n";
91 }).endl().endl();
92
93 out << "public static "
94 << ifaceName
95 << " getService(";
96 if (isRetry) {
97 out << "boolean retry";
98 }
99 out << ") throws android.os.RemoteException ";
100 out.block([&] {
101 out << "return getService(\"default\"";
102 if (isRetry) {
103 out << ", retry";
104 }
105 out <<");\n";
106 }).endl().endl();
107}
108
Steven Moreland368e4602018-02-16 14:21:49 -0800109void AST::generateJava(Formatter& out, const std::string& limitToType) const {
110 CHECK(isJavaCompatible()) << getFilename();
Andreas Huber2831d512016-08-15 09:33:47 -0700111
Steven Moreland19f11b52017-05-12 18:22:21 -0700112 if (!AST::isInterface()) {
Steven Moreland368e4602018-02-16 14:21:49 -0800113 generateJavaTypes(out, limitToType);
114 return;
Andreas Huber0fa9e392016-08-31 09:05:44 -0700115 }
116
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700117 const Interface* iface = mRootScope.getInterface();
Steven Moreland5abcf012018-02-08 18:50:18 -0800118 const std::string ifaceName = iface->localName();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700119 const std::string baseName = iface->getBaseName();
Andreas Huber2831d512016-08-15 09:33:47 -0700120
Andreas Huber2831d512016-08-15 09:33:47 -0700121 std::vector<std::string> packageComponents;
122 getPackageAndVersionComponents(
123 &packageComponents, true /* cpp_compatible */);
124
125 out << "package " << mPackage.javaPackage() << ";\n\n";
126
Andreas Huber2831d512016-08-15 09:33:47 -0700127 out.setNamespace(mPackage.javaPackage() + ".");
128
Andreas Huber2831d512016-08-15 09:33:47 -0700129 const Interface *superType = iface->superType();
130
131 out << "public interface " << ifaceName << " extends ";
132
Yi Kong56758da2018-07-24 16:21:37 -0700133 if (superType != nullptr) {
Andreas Huber2831d512016-08-15 09:33:47 -0700134 out << superType->fullJavaName();
135 } else {
Yifan Hong1af73532016-11-09 14:32:58 -0800136 out << "android.os.IHwInterface";
Andreas Huber2831d512016-08-15 09:33:47 -0700137 }
138
139 out << " {\n";
140 out.indent();
141
142 out << "public static final String kInterfaceName = \""
143 << mPackage.string()
144 << "::"
145 << ifaceName
146 << "\";\n\n";
147
Andreas Hubera2abe982017-04-04 14:42:09 -0700148 out << "/* package private */ static "
Andreas Huber2831d512016-08-15 09:33:47 -0700149 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800150 << " asInterface(android.os.IHwBinder binder) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700151
152 out.indent();
153
154 out << "if (binder == null) {\n";
155 out.indent();
156 out << "return null;\n";
157 out.unindent();
158 out << "}\n\n";
159
Yifan Hong1af73532016-11-09 14:32:58 -0800160 out << "android.os.IHwInterface iface =\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700161 out.indent();
162 out.indent();
163 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
164 out.unindent();
165 out.unindent();
166
167 out << "if ((iface != null) && (iface instanceof "
168 << ifaceName
169 << ")) {\n";
170
171 out.indent();
172 out << "return (" << ifaceName << ")iface;\n";
173 out.unindent();
174 out << "}\n\n";
175
Andreas Hubera2abe982017-04-04 14:42:09 -0700176 out << ifaceName << " proxy = new " << ifaceName << ".Proxy(binder);\n\n";
177 out << "try {\n";
178 out.indent();
179 out << "for (String descriptor : proxy.interfaceChain()) {\n";
180 out.indent();
181 out << "if (descriptor.equals(kInterfaceName)) {\n";
182 out.indent();
183 out << "return proxy;\n";
184 out.unindent();
185 out << "}\n";
186 out.unindent();
187 out << "}\n";
188 out.unindent();
189 out << "} catch (android.os.RemoteException e) {\n";
190 out.indent();
191 out.unindent();
192 out << "}\n\n";
193
194 out << "return null;\n";
195
196 out.unindent();
197 out << "}\n\n";
198
199 out << "public static "
200 << ifaceName
201 << " castFrom(android.os.IHwInterface iface) {\n";
202 out.indent();
203
204 out << "return (iface == null) ? null : "
205 << ifaceName
206 << ".asInterface(iface.asBinder());\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700207
208 out.unindent();
209 out << "}\n\n";
210
Yifan Hong084d11f2016-12-21 15:33:43 -0800211 out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700212
Steven Moreland9bf5a092017-10-25 04:50:54 +0000213 emitGetService(out, ifaceName, iface->fqName().string(), true /* isRetry */);
214 emitGetService(out, ifaceName, iface->fqName().string(), false /* isRetry */);
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800215
Steven Moreland368e4602018-02-16 14:21:49 -0800216 emitJavaTypeDeclarations(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700217
Andreas Huber2831d512016-08-15 09:33:47 -0700218 for (const auto &method : iface->methods()) {
219 const bool returnsValue = !method->results().empty();
220 const bool needsCallback = method->results().size() > 1;
221
222 if (needsCallback) {
Yifan Hong601cfca2017-05-24 12:20:17 -0700223 out << "\n@java.lang.FunctionalInterface\npublic interface " << method->name()
Andreas Huber2831d512016-08-15 09:33:47 -0700224 << "Callback {\n";
225
226 out.indent();
227
Yifan Hong932464e2017-03-30 15:40:22 -0700228 out << "public void onValues(";
229 method->emitJavaResultSignature(out);
230 out << ");\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700231
232 out.unindent();
233 out << "}\n\n";
234 }
235
Steven Moreland49bad8d2018-05-17 15:45:26 -0700236 method->emitDocComment(out);
237
Andreas Huber2831d512016-08-15 09:33:47 -0700238 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700239 out << method->results()[0]->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700240 } else {
241 out << "void";
242 }
243
244 out << " "
245 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700246 << "(";
247 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700248
249 if (needsCallback) {
250 if (!method->args().empty()) {
251 out << ", ";
252 }
253
254 out << method->name()
Steven Moreland4ddd8332017-04-27 19:27:18 -0700255 << "Callback _hidl_cb";
Andreas Huber2831d512016-08-15 09:33:47 -0700256 }
257
Steven Morelanddad1b302016-12-20 15:56:00 -0800258 out << ")\n";
259 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700260 out << "throws android.os.RemoteException;\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800261 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700262 }
263
264 out << "\npublic static final class Proxy implements "
265 << ifaceName
266 << " {\n";
267
268 out.indent();
269
Yifan Hong1af73532016-11-09 14:32:58 -0800270 out << "private android.os.IHwBinder mRemote;\n\n";
271 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700272 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800273 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700274 out.unindent();
275 out << "}\n\n";
276
Yifan Hong084d11f2016-12-21 15:33:43 -0800277 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700278 out.indent();
279 out << "return mRemote;\n";
280 out.unindent();
281 out << "}\n\n";
282
Yifan Honge45b5302017-02-22 10:49:07 -0800283
284 out << "@Override\npublic String toString() ";
285 out.block([&] {
286 out.sTry([&] {
287 out << "return this.interfaceDescriptor() + \"@Proxy\";\n";
Yifan Hong320a3492017-03-27 10:33:09 -0700288 }).sCatch("android.os.RemoteException ex", [&] {
Yifan Honge45b5302017-02-22 10:49:07 -0800289 out << "/* ignored; handled below. */\n";
290 }).endl();
291 out << "return \"[class or subclass of \" + "
292 << ifaceName << ".kInterfaceName + \"]@Proxy\";\n";
293 }).endl().endl();
294
Yifan Hongf89e1062017-10-31 17:31:08 -0700295 // Equals when internal binder object is equal (even if the interface Proxy object
296 // itself is different). This is similar to interfacesEqual in C++.
297 out << "@Override\npublic final boolean equals(java.lang.Object other) ";
298 out.block([&] {
299 out << "return android.os.HidlSupport.interfacesEqual(this, other);\n";
300 }).endl().endl();
301
302 out << "@Override\npublic final int hashCode() ";
303 out.block([&] {
304 out << "return this.asBinder().hashCode();\n";
305 }).endl().endl();
306
Yifan Hong10fe0b52016-10-19 14:20:17 -0700307 const Interface *prevInterface = nullptr;
308 for (const auto &tuple : iface->allMethodsFromRoot()) {
309 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800310
Yifan Hong10fe0b52016-10-19 14:20:17 -0700311 const Interface *superInterface = tuple.interface();
312 if (prevInterface != superInterface) {
313 out << "// Methods from "
314 << superInterface->fullName()
315 << " follow.\n";
316 prevInterface = superInterface;
317 }
318 const bool returnsValue = !method->results().empty();
319 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700320
Yifan Hong084d11f2016-12-21 15:33:43 -0800321 out << "@Override\npublic ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700322 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700323 out << method->results()[0]->type().getJavaType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700324 } else {
325 out << "void";
326 }
Andreas Huber2831d512016-08-15 09:33:47 -0700327
Yifan Hong10fe0b52016-10-19 14:20:17 -0700328 out << " "
329 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700330 << "(";
331 method->emitJavaArgSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700332
Yifan Hong10fe0b52016-10-19 14:20:17 -0700333 if (needsCallback) {
334 if (!method->args().empty()) {
335 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700336 }
337
Yifan Hong10fe0b52016-10-19 14:20:17 -0700338 out << method->name()
Steven Moreland4ddd8332017-04-27 19:27:18 -0700339 << "Callback _hidl_cb";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700340 }
Andreas Huber2831d512016-08-15 09:33:47 -0700341
Steven Morelanddad1b302016-12-20 15:56:00 -0800342 out << ")\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700343 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800344 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700345 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800346 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700347
Martijn Coenen115d4282016-12-19 05:14:04 +0100348 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
349 method->javaImpl(IMPL_PROXY, out);
350 out.unindent();
351 out << "}\n";
352 continue;
353 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800354 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
355 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700356 << superInterface->fullJavaName()
357 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700358
Yifan Hong10fe0b52016-10-19 14:20:17 -0700359 for (const auto &arg : method->args()) {
360 emitJavaReaderWriter(
361 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800362 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700363 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800364 false /* isReader */,
365 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700366 }
Andreas Huber2831d512016-08-15 09:33:47 -0700367
Martijn Coenen4de083b2017-03-16 18:49:43 +0100368 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700369
Martijn Coenen4de083b2017-03-16 18:49:43 +0100370 out.sTry([&] {
371 out << "mRemote.transact("
372 << method->getSerialId()
373 << " /* "
374 << method->name()
375 << " */, _hidl_request, _hidl_reply, ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700376
Martijn Coenen4de083b2017-03-16 18:49:43 +0100377 if (method->isOneway()) {
Steven Moreland77943692018-08-09 12:53:42 -0700378 out << Interface::FLAG_ONE_WAY->javaValue();
Martijn Coenen4de083b2017-03-16 18:49:43 +0100379 } else {
380 out << "0 /* flags */";
Andreas Huber2831d512016-08-15 09:33:47 -0700381 }
382
Martijn Coenen4de083b2017-03-16 18:49:43 +0100383 out << ");\n";
Andreas Huber8b5da222016-08-18 14:28:18 -0700384
Martijn Coenen4de083b2017-03-16 18:49:43 +0100385 if (!method->isOneway()) {
386 out << "_hidl_reply.verifySuccess();\n";
387 } else {
388 CHECK(!returnsValue);
389 }
390
391 out << "_hidl_request.releaseTemporaryStorage();\n";
392
393 if (returnsValue) {
394 out << "\n";
395
Andreas Huber2831d512016-08-15 09:33:47 -0700396 for (const auto &arg : method->results()) {
Martijn Coenen4de083b2017-03-16 18:49:43 +0100397 emitJavaReaderWriter(
398 out,
399 "_hidl_reply",
400 arg,
401 true /* isReader */,
402 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700403 }
Andreas Huber2831d512016-08-15 09:33:47 -0700404
Martijn Coenen4de083b2017-03-16 18:49:43 +0100405 if (needsCallback) {
Steven Moreland4ddd8332017-04-27 19:27:18 -0700406 out << "_hidl_cb.onValues(";
Martijn Coenen4de083b2017-03-16 18:49:43 +0100407
408 bool firstField = true;
409 for (const auto &arg : method->results()) {
410 if (!firstField) {
411 out << ", ";
412 }
413
414 out << "_hidl_out_" << arg->name();
415 firstField = false;
416 }
417
418 out << ");\n";
419 } else {
420 const std::string returnName = method->results()[0]->name();
421 out << "return _hidl_out_" << returnName << ";\n";
422 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700423 }
Martijn Coenen4de083b2017-03-16 18:49:43 +0100424 }).sFinally([&] {
425 out << "_hidl_reply.release();\n";
426 }).endl();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700427
428 out.unindent();
429 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700430 }
431
432 out.unindent();
433 out << "}\n";
434
435 ////////////////////////////////////////////////////////////////////////////
436
Yifan Hong1af73532016-11-09 14:32:58 -0800437 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700438 << "implements "
439 << ifaceName << " {\n";
440
441 out.indent();
442
Yifan Hong084d11f2016-12-21 15:33:43 -0800443 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700444 out.indent();
Yifan Hongf89e1062017-10-31 17:31:08 -0700445 // If we change this behavior in the future and asBinder does not return "this",
446 // equals and hashCode should also be overridden.
Andreas Huber2831d512016-08-15 09:33:47 -0700447 out << "return this;\n";
448 out.unindent();
449 out << "}\n\n";
450
Yifan Hong10fe0b52016-10-19 14:20:17 -0700451 for (Method *method : iface->hidlReservedMethods()) {
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();
Steven Moreland327fd8b2018-08-10 15:40:41 -0700456
457 bool canBeOverriden = method->name() == "debug";
458
459 out << "@Override\npublic " << (canBeOverriden ? "" : "final ") << resultType << " "
460 << method->name() << "(";
Yifan Hong932464e2017-03-30 15:40:22 -0700461 method->emitJavaArgSignature(out);
462 out << ") {\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100463
Yifan Hong10fe0b52016-10-19 14:20:17 -0700464 out.indent();
Steven Moreland937408a2017-03-20 09:54:18 -0700465 method->javaImpl(IMPL_INTERFACE, 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
Yifan Hong320a3492017-03-27 10:33:09 -0700483 out << "public void registerAsService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700484 out.indent();
485
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100486 out << "registerService(serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700487
488 out.unindent();
489 out << "}\n\n";
490
Yifan Honge45b5302017-02-22 10:49:07 -0800491 out << "@Override\npublic String toString() ";
492 out.block([&] {
493 out << "return this.interfaceDescriptor() + \"@Stub\";\n";
494 }).endl().endl();
495
Yifan Hong084d11f2016-12-21 15:33:43 -0800496 out << "@Override\n"
497 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800498 << "int _hidl_code, "
499 << "android.os.HwParcel _hidl_request, "
500 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800501 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700502 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800503 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700504 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800505 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700506
Steven Moreland4b39bc12016-11-16 10:42:24 -0800507 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700508
509 out.indent();
510
Yifan Hong10fe0b52016-10-19 14:20:17 -0700511 for (const auto &tuple : iface->allMethodsFromRoot()) {
512 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800513
Yifan Hong10fe0b52016-10-19 14:20:17 -0700514 const Interface *superInterface = tuple.interface();
515 const bool returnsValue = !method->results().empty();
516 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700517
Yifan Hong10fe0b52016-10-19 14:20:17 -0700518 out << "case "
519 << method->getSerialId()
520 << " /* "
521 << method->name()
522 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700523
Yifan Hong10fe0b52016-10-19 14:20:17 -0700524 out.indent();
Andreas Huber37065d62017-02-07 14:36:54 -0800525
Steven Moreland77943692018-08-09 12:53:42 -0700526 out << "boolean _hidl_is_oneway = (_hidl_flags & " << Interface::FLAG_ONE_WAY->javaValue()
527 << ") != 0;\n";
Steven Moreland7dfba102018-03-19 14:37:53 -0700528 out << "if (_hidl_is_oneway != " << (method->isOneway() ? "true" : "false") << ") ";
529 out.block([&] {
530 out << "_hidl_reply.writeStatus(" << UNKNOWN_ERROR << ");\n";
531 out << "_hidl_reply.send();\n";
532 out << "break;\n";
533 });
534
Martijn Coenen115d4282016-12-19 05:14:04 +0100535 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
536 method->javaImpl(IMPL_STUB, out);
537 out.unindent();
538 out << "break;\n";
539 out << "}\n\n";
540 continue;
541 }
Andreas Huber2831d512016-08-15 09:33:47 -0700542
Steven Moreland4b39bc12016-11-16 10:42:24 -0800543 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700544 << superInterface->fullJavaName()
545 << ".kInterfaceName);\n\n";
546
547 for (const auto &arg : method->args()) {
548 emitJavaReaderWriter(
549 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800550 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700551 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800552 true /* isReader */,
553 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700554 }
555
556 if (!needsCallback && returnsValue) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700557 const NamedReference<Type>* returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700558
Yifan Hong4ed13472016-11-02 10:44:11 -0700559 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800560 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700561 << returnArg->name()
562 << " = ";
563 }
564
565 out << method->name()
566 << "(";
567
568 bool firstField = true;
569 for (const auto &arg : method->args()) {
570 if (!firstField) {
571 out << ", ";
572 }
573
574 out << arg->name();
575
576 firstField = false;
577 }
578
579 if (needsCallback) {
580 if (!firstField) {
581 out << ", ";
582 }
583
584 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700585 out.indent();
586
Yifan Hong10fe0b52016-10-19 14:20:17 -0700587 out << "@Override\n"
Yifan Hong932464e2017-03-30 15:40:22 -0700588 << "public void onValues(";
589 method->emitJavaResultSignature(out);
590 out << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700591
Yifan Hong10fe0b52016-10-19 14:20:17 -0700592 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800593 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700594
595 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700596 emitJavaReaderWriter(
597 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800598 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700599 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800600 false /* isReader */,
601 false /* addPrefixToName */);
602 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700603 }
604
Steven Moreland4b39bc12016-11-16 10:42:24 -0800605 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700606 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700607
Andreas Huber2831d512016-08-15 09:33:47 -0700608 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700609 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700610 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700611
612 out << ");\n";
613
Martijn Coenen0bb0d412017-02-08 10:21:30 +0100614 if (!needsCallback && !method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800615 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700616
617 if (returnsValue) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700618 const NamedReference<Type>* returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700619
620 emitJavaReaderWriter(
621 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800622 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700623 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800624 false /* isReader */,
625 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700626 }
627
Steven Moreland4b39bc12016-11-16 10:42:24 -0800628 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700629 }
630
631 out << "break;\n";
632 out.unindent();
633 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700634 }
635
636 out.unindent();
637 out << "}\n";
638
639 out.unindent();
640 out << "}\n";
641
642 out.unindent();
643 out << "}\n";
644
645 out.unindent();
646 out << "}\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700647}
648
Steven Moreland368e4602018-02-16 14:21:49 -0800649void AST::emitJavaTypeDeclarations(Formatter& out) const {
650 mRootScope.emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700651}
652
653} // namespace android