blob: 6d9058e7ff7ddf245eded400c71567abd6e42666 [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) {
Steven Moreland7645fbd2019-03-12 18:49:28 -070074 if (isRetry) {
75 DocComment(
76 "This will invoke the equivalent of the C++ getService(std::string) if retry is\n"
77 "true or tryGetService(std::string) if retry is false. If the service is\n"
78 "available on the device and retry is true, this will wait for the service to\n"
79 "start. Otherwise, it will return immediately even if the service is null.")
80 .emit(out);
81 } else {
82 DocComment(
83 "Warning: this will not wait for the interface to come up if it hasn't yet\n"
84 "started. See getService(String,boolean) instead.")
85 .emit(out);
86 }
Steven Moreland9bf5a092017-10-25 04:50:54 +000087 out << "public static "
88 << ifaceName
89 << " getService(String serviceName";
90 if (isRetry) {
91 out << ", boolean retry";
92 }
93 out << ") throws android.os.RemoteException ";
94 out.block([&] {
95 out << "return "
96 << ifaceName
97 << ".asInterface(android.os.HwBinder.getService(\""
98 << fqName
99 << "\", serviceName";
100 if (isRetry) {
101 out << ", retry";
102 }
103 out << "));\n";
104 }).endl().endl();
105
Steven Moreland7645fbd2019-03-12 18:49:28 -0700106 if (isRetry) {
107 DocComment("Calls getService(\"default\",retry).").emit(out);
108 } else {
109 DocComment(
110 "Warning: this will not wait for the interface to come up if it hasn't yet "
111 "started. See getService(String,boolean) instead.")
112 .emit(out);
113 }
Steven Moreland9bf5a092017-10-25 04:50:54 +0000114 out << "public static "
115 << ifaceName
116 << " getService(";
117 if (isRetry) {
118 out << "boolean retry";
119 }
120 out << ") throws android.os.RemoteException ";
121 out.block([&] {
122 out << "return getService(\"default\"";
123 if (isRetry) {
124 out << ", retry";
125 }
126 out <<");\n";
127 }).endl().endl();
128}
129
Steven Moreland368e4602018-02-16 14:21:49 -0800130void AST::generateJava(Formatter& out, const std::string& limitToType) const {
131 CHECK(isJavaCompatible()) << getFilename();
Andreas Huber2831d512016-08-15 09:33:47 -0700132
Steven Moreland19f11b52017-05-12 18:22:21 -0700133 if (!AST::isInterface()) {
Steven Moreland368e4602018-02-16 14:21:49 -0800134 generateJavaTypes(out, limitToType);
135 return;
Andreas Huber0fa9e392016-08-31 09:05:44 -0700136 }
137
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700138 const Interface* iface = mRootScope.getInterface();
Steven Moreland5abcf012018-02-08 18:50:18 -0800139 const std::string ifaceName = iface->localName();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700140 const std::string baseName = iface->getBaseName();
Andreas Huber2831d512016-08-15 09:33:47 -0700141
Andreas Huber2831d512016-08-15 09:33:47 -0700142 out << "package " << mPackage.javaPackage() << ";\n\n";
143
Andreas Huber2831d512016-08-15 09:33:47 -0700144 const Interface *superType = iface->superType();
145
Steven Moreland70cb55e2019-03-12 17:20:54 -0700146 iface->emitDocComment(out);
147
Andreas Huber2831d512016-08-15 09:33:47 -0700148 out << "public interface " << ifaceName << " extends ";
149
Yi Kong56758da2018-07-24 16:21:37 -0700150 if (superType != nullptr) {
Andreas Huber2831d512016-08-15 09:33:47 -0700151 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
Steven Moreland7645fbd2019-03-12 18:49:28 -0700159 DocComment("Fully-qualified interface name for this interface.").emit(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700160 out << "public static final String kInterfaceName = \""
161 << mPackage.string()
162 << "::"
163 << ifaceName
164 << "\";\n\n";
165
Steven Moreland7645fbd2019-03-12 18:49:28 -0700166 DocComment("Does a checked conversion from a binder to this class.").emit(out);
Andreas Hubera2abe982017-04-04 14:42:09 -0700167 out << "/* package private */ static "
Andreas Huber2831d512016-08-15 09:33:47 -0700168 << ifaceName
Yifan Hong1af73532016-11-09 14:32:58 -0800169 << " asInterface(android.os.IHwBinder binder) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700170
171 out.indent();
172
173 out << "if (binder == null) {\n";
174 out.indent();
175 out << "return null;\n";
176 out.unindent();
177 out << "}\n\n";
178
Yifan Hong1af73532016-11-09 14:32:58 -0800179 out << "android.os.IHwInterface iface =\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700180 out.indent();
181 out.indent();
182 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
183 out.unindent();
184 out.unindent();
185
186 out << "if ((iface != null) && (iface instanceof "
187 << ifaceName
188 << ")) {\n";
189
190 out.indent();
191 out << "return (" << ifaceName << ")iface;\n";
192 out.unindent();
193 out << "}\n\n";
194
Andreas Hubera2abe982017-04-04 14:42:09 -0700195 out << ifaceName << " proxy = new " << ifaceName << ".Proxy(binder);\n\n";
196 out << "try {\n";
197 out.indent();
198 out << "for (String descriptor : proxy.interfaceChain()) {\n";
199 out.indent();
200 out << "if (descriptor.equals(kInterfaceName)) {\n";
201 out.indent();
202 out << "return proxy;\n";
203 out.unindent();
204 out << "}\n";
205 out.unindent();
206 out << "}\n";
207 out.unindent();
208 out << "} catch (android.os.RemoteException e) {\n";
209 out.indent();
210 out.unindent();
211 out << "}\n\n";
212
213 out << "return null;\n";
214
215 out.unindent();
216 out << "}\n\n";
217
Steven Moreland7645fbd2019-03-12 18:49:28 -0700218 DocComment("Does a checked conversion from any interface to this class.").emit(out);
Andreas Hubera2abe982017-04-04 14:42:09 -0700219 out << "public static "
220 << ifaceName
221 << " castFrom(android.os.IHwInterface iface) {\n";
222 out.indent();
223
224 out << "return (iface == null) ? null : "
225 << ifaceName
226 << ".asInterface(iface.asBinder());\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700227
228 out.unindent();
229 out << "}\n\n";
230
Yifan Hong084d11f2016-12-21 15:33:43 -0800231 out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700232
Steven Moreland9bf5a092017-10-25 04:50:54 +0000233 emitGetService(out, ifaceName, iface->fqName().string(), true /* isRetry */);
234 emitGetService(out, ifaceName, iface->fqName().string(), false /* isRetry */);
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800235
Steven Moreland70cb55e2019-03-12 17:20:54 -0700236 iface->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700237
Andreas Huber2831d512016-08-15 09:33:47 -0700238 for (const auto &method : iface->methods()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700239 const bool needsCallback = method->results().size() > 1;
240
241 if (needsCallback) {
Yifan Hong601cfca2017-05-24 12:20:17 -0700242 out << "\n@java.lang.FunctionalInterface\npublic interface " << method->name()
Andreas Huber2831d512016-08-15 09:33:47 -0700243 << "Callback {\n";
244
245 out.indent();
246
Yifan Hong932464e2017-03-30 15:40:22 -0700247 out << "public void onValues(";
248 method->emitJavaResultSignature(out);
249 out << ");\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700250
251 out.unindent();
252 out << "}\n\n";
253 }
254
Steven Moreland49bad8d2018-05-17 15:45:26 -0700255 method->emitDocComment(out);
256
Neel Mehta5b447c02019-05-23 16:12:24 -0700257 method->emitJavaSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700258
Neel Mehta5b447c02019-05-23 16:12:24 -0700259 out << "\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800260 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700261 out << "throws android.os.RemoteException;\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800262 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700263 }
264
265 out << "\npublic static final class Proxy implements "
266 << ifaceName
267 << " {\n";
268
269 out.indent();
270
Yifan Hong1af73532016-11-09 14:32:58 -0800271 out << "private android.os.IHwBinder mRemote;\n\n";
272 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700273 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800274 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700275 out.unindent();
276 out << "}\n\n";
277
Yifan Hong084d11f2016-12-21 15:33:43 -0800278 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700279 out.indent();
280 out << "return mRemote;\n";
281 out.unindent();
282 out << "}\n\n";
283
Yifan Honge45b5302017-02-22 10:49:07 -0800284
285 out << "@Override\npublic String toString() ";
286 out.block([&] {
287 out.sTry([&] {
288 out << "return this.interfaceDescriptor() + \"@Proxy\";\n";
Yifan Hong320a3492017-03-27 10:33:09 -0700289 }).sCatch("android.os.RemoteException ex", [&] {
Yifan Honge45b5302017-02-22 10:49:07 -0800290 out << "/* ignored; handled below. */\n";
291 }).endl();
292 out << "return \"[class or subclass of \" + "
293 << ifaceName << ".kInterfaceName + \"]@Proxy\";\n";
294 }).endl().endl();
295
Yifan Hongf89e1062017-10-31 17:31:08 -0700296 // Equals when internal binder object is equal (even if the interface Proxy object
297 // itself is different). This is similar to interfacesEqual in C++.
298 out << "@Override\npublic final boolean equals(java.lang.Object other) ";
299 out.block([&] {
300 out << "return android.os.HidlSupport.interfacesEqual(this, other);\n";
301 }).endl().endl();
302
303 out << "@Override\npublic final int hashCode() ";
304 out.block([&] {
305 out << "return this.asBinder().hashCode();\n";
306 }).endl().endl();
307
Yifan Hong10fe0b52016-10-19 14:20:17 -0700308 const Interface *prevInterface = nullptr;
309 for (const auto &tuple : iface->allMethodsFromRoot()) {
310 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800311
Yifan Hong10fe0b52016-10-19 14:20:17 -0700312 const Interface *superInterface = tuple.interface();
313 if (prevInterface != superInterface) {
314 out << "// Methods from "
315 << superInterface->fullName()
316 << " follow.\n";
317 prevInterface = superInterface;
318 }
319 const bool returnsValue = !method->results().empty();
320 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700321
Yifan Hong084d11f2016-12-21 15:33:43 -0800322 out << "@Override\npublic ";
Neel Mehta5b447c02019-05-23 16:12:24 -0700323 method->emitJavaSignature(out);
Andreas Huber2831d512016-08-15 09:33:47 -0700324
Neel Mehta5b447c02019-05-23 16:12:24 -0700325 out << "\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700326 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800327 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700328 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800329 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700330
Martijn Coenen115d4282016-12-19 05:14:04 +0100331 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
332 method->javaImpl(IMPL_PROXY, out);
333 out.unindent();
334 out << "}\n";
335 continue;
336 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800337 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
338 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700339 << superInterface->fullJavaName()
340 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700341
Yifan Hong10fe0b52016-10-19 14:20:17 -0700342 for (const auto &arg : method->args()) {
343 emitJavaReaderWriter(
344 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800345 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700346 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800347 false /* isReader */,
348 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700349 }
Andreas Huber2831d512016-08-15 09:33:47 -0700350
Martijn Coenen4de083b2017-03-16 18:49:43 +0100351 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700352
Martijn Coenen4de083b2017-03-16 18:49:43 +0100353 out.sTry([&] {
354 out << "mRemote.transact("
355 << method->getSerialId()
356 << " /* "
357 << method->name()
358 << " */, _hidl_request, _hidl_reply, ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700359
Martijn Coenen4de083b2017-03-16 18:49:43 +0100360 if (method->isOneway()) {
Steven Moreland77943692018-08-09 12:53:42 -0700361 out << Interface::FLAG_ONE_WAY->javaValue();
Martijn Coenen4de083b2017-03-16 18:49:43 +0100362 } else {
363 out << "0 /* flags */";
Andreas Huber2831d512016-08-15 09:33:47 -0700364 }
365
Martijn Coenen4de083b2017-03-16 18:49:43 +0100366 out << ");\n";
Andreas Huber8b5da222016-08-18 14:28:18 -0700367
Martijn Coenen4de083b2017-03-16 18:49:43 +0100368 if (!method->isOneway()) {
369 out << "_hidl_reply.verifySuccess();\n";
370 } else {
371 CHECK(!returnsValue);
372 }
373
374 out << "_hidl_request.releaseTemporaryStorage();\n";
375
376 if (returnsValue) {
377 out << "\n";
378
Andreas Huber2831d512016-08-15 09:33:47 -0700379 for (const auto &arg : method->results()) {
Martijn Coenen4de083b2017-03-16 18:49:43 +0100380 emitJavaReaderWriter(
381 out,
382 "_hidl_reply",
383 arg,
384 true /* isReader */,
385 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700386 }
Andreas Huber2831d512016-08-15 09:33:47 -0700387
Martijn Coenen4de083b2017-03-16 18:49:43 +0100388 if (needsCallback) {
Steven Moreland4ddd8332017-04-27 19:27:18 -0700389 out << "_hidl_cb.onValues(";
Martijn Coenen4de083b2017-03-16 18:49:43 +0100390
391 bool firstField = true;
392 for (const auto &arg : method->results()) {
393 if (!firstField) {
394 out << ", ";
395 }
396
397 out << "_hidl_out_" << arg->name();
398 firstField = false;
399 }
400
401 out << ");\n";
402 } else {
403 const std::string returnName = method->results()[0]->name();
404 out << "return _hidl_out_" << returnName << ";\n";
405 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700406 }
Martijn Coenen4de083b2017-03-16 18:49:43 +0100407 }).sFinally([&] {
408 out << "_hidl_reply.release();\n";
409 }).endl();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700410
411 out.unindent();
412 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700413 }
414
415 out.unindent();
416 out << "}\n";
417
418 ////////////////////////////////////////////////////////////////////////////
419
Yifan Hong1af73532016-11-09 14:32:58 -0800420 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700421 << "implements "
422 << ifaceName << " {\n";
423
424 out.indent();
425
Yifan Hong084d11f2016-12-21 15:33:43 -0800426 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700427 out.indent();
Yifan Hongf89e1062017-10-31 17:31:08 -0700428 // If we change this behavior in the future and asBinder does not return "this",
429 // equals and hashCode should also be overridden.
Andreas Huber2831d512016-08-15 09:33:47 -0700430 out << "return this;\n";
431 out.unindent();
432 out << "}\n\n";
433
Yifan Hong10fe0b52016-10-19 14:20:17 -0700434 for (Method *method : iface->hidlReservedMethods()) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100435 // b/32383557 this is a hack. We need to change this if we have more reserved methods.
436 CHECK_LE(method->results().size(), 1u);
437 std::string resultType = method->results().size() == 0 ? "void" :
438 method->results()[0]->type().getJavaType();
Steven Moreland327fd8b2018-08-10 15:40:41 -0700439
440 bool canBeOverriden = method->name() == "debug";
441
442 out << "@Override\npublic " << (canBeOverriden ? "" : "final ") << resultType << " "
443 << method->name() << "(";
Yifan Hong932464e2017-03-30 15:40:22 -0700444 method->emitJavaArgSignature(out);
445 out << ") {\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100446
Yifan Hong10fe0b52016-10-19 14:20:17 -0700447 out.indent();
Steven Moreland937408a2017-03-20 09:54:18 -0700448 method->javaImpl(IMPL_INTERFACE, out);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700449 out.unindent();
450 out << "\n}\n\n";
451 }
452
Yifan Hong084d11f2016-12-21 15:33:43 -0800453 out << "@Override\n"
454 << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700455 out.indent();
456 // XXX what about potential superClasses?
457 out << "if (kInterfaceName.equals(descriptor)) {\n";
458 out.indent();
459 out << "return this;\n";
460 out.unindent();
461 out << "}\n";
462 out << "return null;\n";
463 out.unindent();
464 out << "}\n\n";
465
Yifan Hong320a3492017-03-27 10:33:09 -0700466 out << "public void registerAsService(String serviceName) throws android.os.RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700467 out.indent();
468
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100469 out << "registerService(serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700470
471 out.unindent();
472 out << "}\n\n";
473
Yifan Honge45b5302017-02-22 10:49:07 -0800474 out << "@Override\npublic String toString() ";
475 out.block([&] {
476 out << "return this.interfaceDescriptor() + \"@Stub\";\n";
477 }).endl().endl();
478
Yifan Hong084d11f2016-12-21 15:33:43 -0800479 out << "@Override\n"
480 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800481 << "int _hidl_code, "
482 << "android.os.HwParcel _hidl_request, "
483 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800484 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700485 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800486 out.indent();
Yifan Hong320a3492017-03-27 10:33:09 -0700487 out << "throws android.os.RemoteException {\n";
Steven Morelanddad1b302016-12-20 15:56:00 -0800488 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700489
Steven Moreland4b39bc12016-11-16 10:42:24 -0800490 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700491
492 out.indent();
493
Yifan Hong10fe0b52016-10-19 14:20:17 -0700494 for (const auto &tuple : iface->allMethodsFromRoot()) {
495 const Method *method = tuple.method();
Andreas Huber37065d62017-02-07 14:36:54 -0800496
Yifan Hong10fe0b52016-10-19 14:20:17 -0700497 const Interface *superInterface = tuple.interface();
498 const bool returnsValue = !method->results().empty();
499 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700500
Yifan Hong10fe0b52016-10-19 14:20:17 -0700501 out << "case "
502 << method->getSerialId()
503 << " /* "
504 << method->name()
505 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700506
Yifan Hong10fe0b52016-10-19 14:20:17 -0700507 out.indent();
Andreas Huber37065d62017-02-07 14:36:54 -0800508
Martijn Coenen115d4282016-12-19 05:14:04 +0100509 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
510 method->javaImpl(IMPL_STUB, out);
511 out.unindent();
512 out << "break;\n";
513 out << "}\n\n";
514 continue;
515 }
Andreas Huber2831d512016-08-15 09:33:47 -0700516
Steven Moreland4b39bc12016-11-16 10:42:24 -0800517 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700518 << superInterface->fullJavaName()
519 << ".kInterfaceName);\n\n";
520
521 for (const auto &arg : method->args()) {
522 emitJavaReaderWriter(
523 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800524 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700525 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800526 true /* isReader */,
527 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700528 }
529
530 if (!needsCallback && returnsValue) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700531 const NamedReference<Type>* returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700532
Yifan Hong4ed13472016-11-02 10:44:11 -0700533 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800534 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700535 << returnArg->name()
536 << " = ";
537 }
538
539 out << method->name()
540 << "(";
541
542 bool firstField = true;
543 for (const auto &arg : method->args()) {
544 if (!firstField) {
545 out << ", ";
546 }
547
548 out << arg->name();
549
550 firstField = false;
551 }
552
553 if (needsCallback) {
554 if (!firstField) {
555 out << ", ";
556 }
557
558 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700559 out.indent();
560
Yifan Hong10fe0b52016-10-19 14:20:17 -0700561 out << "@Override\n"
Yifan Hong932464e2017-03-30 15:40:22 -0700562 << "public void onValues(";
563 method->emitJavaResultSignature(out);
564 out << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700565
Yifan Hong10fe0b52016-10-19 14:20:17 -0700566 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800567 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700568
569 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700570 emitJavaReaderWriter(
571 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800572 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700573 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800574 false /* isReader */,
575 false /* addPrefixToName */);
576 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700577 }
578
Steven Moreland4b39bc12016-11-16 10:42:24 -0800579 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700580 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700581
Andreas Huber2831d512016-08-15 09:33:47 -0700582 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700583 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700584 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700585
586 out << ");\n";
587
Martijn Coenen0bb0d412017-02-08 10:21:30 +0100588 if (!needsCallback && !method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800589 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700590
591 if (returnsValue) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700592 const NamedReference<Type>* returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700593
594 emitJavaReaderWriter(
595 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800596 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700597 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800598 false /* isReader */,
599 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700600 }
601
Steven Moreland4b39bc12016-11-16 10:42:24 -0800602 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700603 }
604
605 out << "break;\n";
606 out.unindent();
607 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700608 }
609
610 out.unindent();
611 out << "}\n";
612
613 out.unindent();
614 out << "}\n";
615
616 out.unindent();
617 out << "}\n";
618
619 out.unindent();
620 out << "}\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700621}
622
Andreas Huber2831d512016-08-15 09:33:47 -0700623} // namespace android