blob: bc44d081661339ec81b6875261dd44fbddc5e265 [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
Andreas Huber2831d512016-08-15 09:33:47 -0700216 status_t err = emitJavaTypeDeclarations(out);
217
218 if (err != OK) {
219 return err;
220 }
221
Andreas Huber2831d512016-08-15 09:33:47 -0700222 for (const auto &method : iface->methods()) {
223 const bool returnsValue = !method->results().empty();
224 const bool needsCallback = method->results().size() > 1;
225
226 if (needsCallback) {
Steven Morelandbcd79032016-12-16 20:50:23 -0800227 out << "\npublic interface "
Andreas Huber2831d512016-08-15 09:33:47 -0700228 << method->name()
229 << "Callback {\n";
230
231 out.indent();
232
Steven Morelandbcd79032016-12-16 20:50:23 -0800233 out << "public void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700234 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700235 << ");\n";
236
237 out.unindent();
238 out << "}\n\n";
239 }
240
241 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700242 out << method->results()[0]->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700243 } else {
244 out << "void";
245 }
246
247 out << " "
248 << method->name()
249 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700250 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700251
252 if (needsCallback) {
253 if (!method->args().empty()) {
254 out << ", ";
255 }
256
257 out << method->name()
258 << "Callback cb";
259 }
260
Steven Morelanddad1b302016-12-20 15:56:00 -0800261 out << ")\n";
262 out.indent();
263 out << "throws RemoteException;\n";
264 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700265 }
266
267 out << "\npublic static final class Proxy implements "
268 << ifaceName
269 << " {\n";
270
271 out.indent();
272
Yifan Hong1af73532016-11-09 14:32:58 -0800273 out << "private android.os.IHwBinder mRemote;\n\n";
274 out << "public Proxy(android.os.IHwBinder remote) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700275 out.indent();
Yifan Hong729e7962016-12-21 16:04:27 -0800276 out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700277 out.unindent();
278 out << "}\n\n";
279
Yifan Hong084d11f2016-12-21 15:33:43 -0800280 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700281 out.indent();
282 out << "return mRemote;\n";
283 out.unindent();
284 out << "}\n\n";
285
Yifan Hong10fe0b52016-10-19 14:20:17 -0700286 const Interface *prevInterface = nullptr;
287 for (const auto &tuple : iface->allMethodsFromRoot()) {
288 const Method *method = tuple.method();
289 const Interface *superInterface = tuple.interface();
290 if (prevInterface != superInterface) {
291 out << "// Methods from "
292 << superInterface->fullName()
293 << " follow.\n";
294 prevInterface = superInterface;
295 }
296 const bool returnsValue = !method->results().empty();
297 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700298
Yifan Hong084d11f2016-12-21 15:33:43 -0800299 out << "@Override\npublic ";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700300 if (returnsValue && !needsCallback) {
Yifan Hong4ed13472016-11-02 10:44:11 -0700301 out << method->results()[0]->type().getJavaType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700302 } else {
303 out << "void";
304 }
Andreas Huber2831d512016-08-15 09:33:47 -0700305
Yifan Hong10fe0b52016-10-19 14:20:17 -0700306 out << " "
307 << method->name()
308 << "("
309 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700310
Yifan Hong10fe0b52016-10-19 14:20:17 -0700311 if (needsCallback) {
312 if (!method->args().empty()) {
313 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700314 }
315
Yifan Hong10fe0b52016-10-19 14:20:17 -0700316 out << method->name()
317 << "Callback cb";
318 }
Andreas Huber2831d512016-08-15 09:33:47 -0700319
Steven Morelanddad1b302016-12-20 15:56:00 -0800320 out << ")\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700321 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800322 out.indent();
323 out << "throws RemoteException {\n";
324 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700325
Martijn Coenen115d4282016-12-19 05:14:04 +0100326 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
327 method->javaImpl(IMPL_PROXY, out);
328 out.unindent();
329 out << "}\n";
330 continue;
331 }
Steven Moreland4b39bc12016-11-16 10:42:24 -0800332 out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
333 out << "_hidl_request.writeInterfaceToken("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700334 << superInterface->fullJavaName()
335 << ".kInterfaceName);\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700336
Yifan Hong10fe0b52016-10-19 14:20:17 -0700337 for (const auto &arg : method->args()) {
338 emitJavaReaderWriter(
339 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800340 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700341 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800342 false /* isReader */,
343 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700344 }
Andreas Huber2831d512016-08-15 09:33:47 -0700345
Steven Moreland4b39bc12016-11-16 10:42:24 -0800346 out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700347 << "mRemote.transact("
348 << method->getSerialId()
349 << " /* "
350 << method->name()
Steven Moreland4b39bc12016-11-16 10:42:24 -0800351 << " */, _hidl_request, _hidl_reply, ";
Andreas Huber2831d512016-08-15 09:33:47 -0700352
Yifan Hong10fe0b52016-10-19 14:20:17 -0700353 if (method->isOneway()) {
Yifan Hong1af73532016-11-09 14:32:58 -0800354 out << "android.os.IHwBinder.FLAG_ONEWAY";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700355 } else {
356 out << "0 /* flags */";
357 }
358
359 out << ");\n";
360
361 if (!method->isOneway()) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800362 out << "_hidl_reply.verifySuccess();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700363 } else {
364 CHECK(!returnsValue);
365 }
366
Steven Moreland4b39bc12016-11-16 10:42:24 -0800367 out << "_hidl_request.releaseTemporaryStorage();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700368
369 if (returnsValue) {
370 out << "\n";
371
372 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700373 emitJavaReaderWriter(
374 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800375 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700376 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800377 true /* isReader */,
378 true /* addPrefixToName */);
Andreas Huber2831d512016-08-15 09:33:47 -0700379 }
380
Yifan Hong10fe0b52016-10-19 14:20:17 -0700381 if (needsCallback) {
382 out << "cb.onValues(";
Andreas Huber8b5da222016-08-18 14:28:18 -0700383
Yifan Hong10fe0b52016-10-19 14:20:17 -0700384 bool firstField = true;
Andreas Huber2831d512016-08-15 09:33:47 -0700385 for (const auto &arg : method->results()) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700386 if (!firstField) {
387 out << ", ";
Andreas Huber2831d512016-08-15 09:33:47 -0700388 }
389
Yifan Honga47eef32016-12-12 10:38:54 -0800390 out << "_hidl_out_" << arg->name();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700391 firstField = false;
Andreas Huber2831d512016-08-15 09:33:47 -0700392 }
Andreas Huber2831d512016-08-15 09:33:47 -0700393
Yifan Hong10fe0b52016-10-19 14:20:17 -0700394 out << ");\n";
395 } else {
396 const std::string returnName = method->results()[0]->name();
Yifan Honga47eef32016-12-12 10:38:54 -0800397 out << "return _hidl_out_" << returnName << ";\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700398 }
Andreas Huber2831d512016-08-15 09:33:47 -0700399 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700400
401 out.unindent();
402 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700403 }
404
405 out.unindent();
406 out << "}\n";
407
408 ////////////////////////////////////////////////////////////////////////////
409
Yifan Hong1af73532016-11-09 14:32:58 -0800410 out << "\npublic static abstract class Stub extends android.os.HwBinder "
Andreas Huber2831d512016-08-15 09:33:47 -0700411 << "implements "
412 << ifaceName << " {\n";
413
414 out.indent();
415
Yifan Hong084d11f2016-12-21 15:33:43 -0800416 out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700417 out.indent();
418 out << "return this;\n";
419 out.unindent();
420 out << "}\n\n";
421
Yifan Hong10fe0b52016-10-19 14:20:17 -0700422 for (Method *method : iface->hidlReservedMethods()) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100423 // b/32383557 this is a hack. We need to change this if we have more reserved methods.
424 CHECK_LE(method->results().size(), 1u);
425 std::string resultType = method->results().size() == 0 ? "void" :
426 method->results()[0]->type().getJavaType();
Yifan Hong084d11f2016-12-21 15:33:43 -0800427 out << "@Override\npublic final "
Martijn Coenenaf712c02016-11-16 15:26:27 +0100428 << resultType
Yifan Hong10fe0b52016-10-19 14:20:17 -0700429 << " "
430 << method->name()
Martijn Coenen115d4282016-12-19 05:14:04 +0100431 << "("
432 << Method::GetJavaArgSignature(method->args())
433 << ") {\n";
434
Yifan Hong10fe0b52016-10-19 14:20:17 -0700435 out.indent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100436 method->javaImpl(IMPL_HEADER, out);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700437 out.unindent();
438 out << "\n}\n\n";
439 }
440
Yifan Hong084d11f2016-12-21 15:33:43 -0800441 out << "@Override\n"
442 << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700443 out.indent();
444 // XXX what about potential superClasses?
445 out << "if (kInterfaceName.equals(descriptor)) {\n";
446 out.indent();
447 out << "return this;\n";
448 out.unindent();
449 out << "}\n";
450 out << "return null;\n";
451 out.unindent();
452 out << "}\n\n";
453
Steven Moreland64af5e82017-01-04 10:58:55 -0800454 out << "public void registerAsService(String serviceName) throws RemoteException {\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700455 out.indent();
456
Steven Moreland7e367bf2016-11-04 11:31:41 -0700457 out << "registerService(interfaceChain(), serviceName);\n";
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700458
459 out.unindent();
460 out << "}\n\n";
461
Yifan Hong084d11f2016-12-21 15:33:43 -0800462 out << "@Override\n"
463 << "public void onTransact("
Steven Moreland4b39bc12016-11-16 10:42:24 -0800464 << "int _hidl_code, "
465 << "android.os.HwParcel _hidl_request, "
466 << "final android.os.HwParcel _hidl_reply, "
Steven Morelanddad1b302016-12-20 15:56:00 -0800467 << "int _hidl_flags)\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700468 out.indent();
Steven Morelanddad1b302016-12-20 15:56:00 -0800469 out.indent();
470 out << "throws RemoteException {\n";
471 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700472
Steven Moreland4b39bc12016-11-16 10:42:24 -0800473 out << "switch (_hidl_code) {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700474
475 out.indent();
476
Yifan Hong10fe0b52016-10-19 14:20:17 -0700477 for (const auto &tuple : iface->allMethodsFromRoot()) {
478 const Method *method = tuple.method();
479 const Interface *superInterface = tuple.interface();
480 const bool returnsValue = !method->results().empty();
481 const bool needsCallback = method->results().size() > 1;
Andreas Huber2831d512016-08-15 09:33:47 -0700482
Yifan Hong10fe0b52016-10-19 14:20:17 -0700483 out << "case "
484 << method->getSerialId()
485 << " /* "
486 << method->name()
487 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700488
Yifan Hong10fe0b52016-10-19 14:20:17 -0700489 out.indent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100490 if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
491 method->javaImpl(IMPL_STUB, out);
492 out.unindent();
493 out << "break;\n";
494 out << "}\n\n";
495 continue;
496 }
Andreas Huber2831d512016-08-15 09:33:47 -0700497
Steven Moreland4b39bc12016-11-16 10:42:24 -0800498 out << "_hidl_request.enforceInterface("
Yifan Hong10fe0b52016-10-19 14:20:17 -0700499 << superInterface->fullJavaName()
500 << ".kInterfaceName);\n\n";
501
502 for (const auto &arg : method->args()) {
503 emitJavaReaderWriter(
504 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800505 "_hidl_request",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700506 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800507 true /* isReader */,
508 false /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700509 }
510
511 if (!needsCallback && returnsValue) {
512 const TypedVar *returnArg = method->results()[0];
Yifan Hong10fe0b52016-10-19 14:20:17 -0700513
Yifan Hong4ed13472016-11-02 10:44:11 -0700514 out << returnArg->type().getJavaType()
Yifan Honga47eef32016-12-12 10:38:54 -0800515 << " _hidl_out_"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700516 << returnArg->name()
517 << " = ";
518 }
519
520 out << method->name()
521 << "(";
522
523 bool firstField = true;
524 for (const auto &arg : method->args()) {
525 if (!firstField) {
526 out << ", ";
527 }
528
529 out << arg->name();
530
531 firstField = false;
532 }
533
534 if (needsCallback) {
535 if (!firstField) {
536 out << ", ";
537 }
538
539 out << "new " << method->name() << "Callback() {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700540 out.indent();
541
Yifan Hong10fe0b52016-10-19 14:20:17 -0700542 out << "@Override\n"
543 << "public void onValues("
544 << Method::GetJavaArgSignature(method->results())
545 << ") {\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700546
Yifan Hong10fe0b52016-10-19 14:20:17 -0700547 out.indent();
Steven Moreland4b39bc12016-11-16 10:42:24 -0800548 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700549
550 for (const auto &arg : method->results()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700551 emitJavaReaderWriter(
552 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800553 "_hidl_reply",
Andreas Huber2831d512016-08-15 09:33:47 -0700554 arg,
Yifan Honga47eef32016-12-12 10:38:54 -0800555 false /* isReader */,
556 false /* addPrefixToName */);
557 // no need to add _hidl_out because out vars are are scoped
Andreas Huber2831d512016-08-15 09:33:47 -0700558 }
559
Steven Moreland4b39bc12016-11-16 10:42:24 -0800560 out << "_hidl_reply.send();\n"
Yifan Hong10fe0b52016-10-19 14:20:17 -0700561 << "}}";
Andreas Huber2831d512016-08-15 09:33:47 -0700562
Andreas Huber2831d512016-08-15 09:33:47 -0700563 out.unindent();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700564 out.unindent();
Andreas Huber2831d512016-08-15 09:33:47 -0700565 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700566
567 out << ");\n";
568
569 if (!needsCallback) {
Steven Moreland4b39bc12016-11-16 10:42:24 -0800570 out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700571
572 if (returnsValue) {
573 const TypedVar *returnArg = method->results()[0];
574
575 emitJavaReaderWriter(
576 out,
Steven Moreland4b39bc12016-11-16 10:42:24 -0800577 "_hidl_reply",
Yifan Hong10fe0b52016-10-19 14:20:17 -0700578 returnArg,
Yifan Honga47eef32016-12-12 10:38:54 -0800579 false /* isReader */,
580 true /* addPrefixToName */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700581 }
582
Steven Moreland4b39bc12016-11-16 10:42:24 -0800583 out << "_hidl_reply.send();\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700584 }
585
586 out << "break;\n";
587 out.unindent();
588 out << "}\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700589 }
590
591 out.unindent();
592 out << "}\n";
593
594 out.unindent();
595 out << "}\n";
596
597 out.unindent();
598 out << "}\n";
599
600 out.unindent();
601 out << "}\n";
602
603 return OK;
604}
605
606status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700607 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700608}
609
610} // namespace android
611