blob: 69ce0d1a13b97927c7d40ee817d7a2a6e1ca3e67 [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,
33 bool isReader) const {
34 if (isReader) {
Andreas Huber4c865b72016-09-14 15:26:27 -070035 std::string extra;
36 out << arg->type().getJavaType(&extra)
37 << extra
Andreas Huber2831d512016-08-15 09:33:47 -070038 << " "
39 << arg->name()
40 << " = ";
41 }
42
43 arg->type().emitJavaReaderWriter(out, parcelObj, arg->name(), isReader);
44}
45
Andreas Huber0fa9e392016-08-31 09:05:44 -070046status_t AST::generateJavaTypes(
Andreas Huberd29724f2016-09-14 09:33:13 -070047 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -070048 // Splits types.hal up into one java file per declared type.
49
Steven Morelandd537ab02016-09-12 10:32:01 -070050 for (const auto &type : mRootScope->getSubTypes()) {
51 std::string typeName = type->localName();
Andreas Huber85eabdb2016-08-25 11:24:49 -070052
53 if (type->isTypeDef()) {
54 continue;
55 }
56
Andreas Huberd29724f2016-09-14 09:33:13 -070057 if (!limitToType.empty() && typeName != limitToType) {
Andreas Huber0fa9e392016-08-31 09:05:44 -070058 continue;
59 }
60
Andreas Huber85eabdb2016-08-25 11:24:49 -070061 std::string path = outputPath;
62 path.append(mCoordinator->convertPackageRootToPath(mPackage));
63 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
64 path.append(typeName);
65 path.append(".java");
66
67 CHECK(Coordinator::MakeParentHierarchy(path));
68 FILE *file = fopen(path.c_str(), "w");
69
70 if (file == NULL) {
71 return -errno;
72 }
73
74 Formatter out(file);
75
76 std::vector<std::string> packageComponents;
77 getPackageAndVersionComponents(
78 &packageComponents, true /* cpp_compatible */);
79
80 out << "package " << mPackage.javaPackage() << ";\n\n";
81
82 out << "import android.os.HwBlob;\n";
83 out << "import android.os.HwParcel;\n\n";
84
Andreas Huberf630bc82016-09-09 14:52:25 -070085 out << "import java.util.Arrays;\n";
Andreas Huberf1abc2a2016-09-28 09:14:43 -070086 out << "import java.util.ArrayList;\n\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -070087
Iliyan Malchev800273d2016-09-02 15:25:07 -070088 for (const auto &item : mImportedNamesForJava) {
89 out << "import " << item.javaName() << ";\n";
90 }
91 out << "\n";
92
Andreas Huber85eabdb2016-08-25 11:24:49 -070093 status_t err =
94 type->emitJavaTypeDeclarations(out, true /* atTopLevel */);
95
96 if (err != OK) {
97 return err;
98 }
99 }
100
101 return OK;
102}
103
Andreas Huber0fa9e392016-08-31 09:05:44 -0700104status_t AST::generateJava(
Andreas Huberd29724f2016-09-14 09:33:13 -0700105 const std::string &outputPath, const std::string &limitToType) const {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700106 if (!isJavaCompatible()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700107 fprintf(stderr,
108 "ERROR: This interface is not Java compatible. The Java backend"
Andreas Huberf03332a2016-09-22 15:35:43 -0700109 " does NOT support union types nor native handles. "
110 "In addition, vectors of arrays are limited to at most "
Andreas Huber86a112b2016-10-19 14:25:16 -0700111 "one-dimensional arrays and vectors of {vectors,interfaces} are"
112 " not supported.\n");
Andreas Huber70a59e12016-08-16 12:57:01 -0700113
Andreas Huber2831d512016-08-15 09:33:47 -0700114 return UNKNOWN_ERROR;
115 }
116
Andreas Huber0fa9e392016-08-31 09:05:44 -0700117 std::string ifaceName;
118 if (!AST::isInterface(&ifaceName)) {
119 return generateJavaTypes(outputPath, limitToType);
120 }
121
122 const Interface *iface = mRootScope->getInterface();
123
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700124 const std::string baseName = iface->getBaseName();
Andreas Huber2831d512016-08-15 09:33:47 -0700125
126 std::string path = outputPath;
127 path.append(mCoordinator->convertPackageRootToPath(mPackage));
128 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
129 path.append(ifaceName);
130 path.append(".java");
131
132 CHECK(Coordinator::MakeParentHierarchy(path));
133 FILE *file = fopen(path.c_str(), "w");
134
135 if (file == NULL) {
136 return -errno;
137 }
138
139 Formatter out(file);
140
141 std::vector<std::string> packageComponents;
142 getPackageAndVersionComponents(
143 &packageComponents, true /* cpp_compatible */);
144
145 out << "package " << mPackage.javaPackage() << ";\n\n";
146
147 out << "import android.os.IHwBinder;\n";
148 out << "import android.os.IHwInterface;\n";
149 out << "import android.os.HwBinder;\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700150 out << "import android.os.HwBlob;\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700151 out << "import android.os.HwParcel;\n\n";
Andreas Huberf630bc82016-09-09 14:52:25 -0700152
153 out << "import java.util.Arrays;\n";
Andreas Huberf1abc2a2016-09-28 09:14:43 -0700154 out << "import java.util.ArrayList;\n\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700155
Andreas Huber85eabdb2016-08-25 11:24:49 -0700156 for (const auto &item : mImportedNamesForJava) {
Andreas Huber2831d512016-08-15 09:33:47 -0700157 out << "import " << item.javaName() << ";\n";
158 }
159
Andreas Huber85eabdb2016-08-25 11:24:49 -0700160 if (!mImportedNamesForJava.empty()) {
Andreas Huber2831d512016-08-15 09:33:47 -0700161 out << "\n";
162 }
163
164 out.setNamespace(mPackage.javaPackage() + ".");
165
Andreas Huber2831d512016-08-15 09:33:47 -0700166 const Interface *superType = iface->superType();
167
168 out << "public interface " << ifaceName << " extends ";
169
170 if (superType != NULL) {
171 out << superType->fullJavaName();
172 } else {
173 out << "IHwInterface";
174 }
175
176 out << " {\n";
177 out.indent();
178
179 out << "public static final String kInterfaceName = \""
180 << mPackage.string()
181 << "::"
182 << ifaceName
183 << "\";\n\n";
184
185 out << "public static "
186 << ifaceName
187 << " asInterface(IHwBinder binder) {\n";
188
189 out.indent();
190
191 out << "if (binder == null) {\n";
192 out.indent();
193 out << "return null;\n";
194 out.unindent();
195 out << "}\n\n";
196
197 out << "IHwInterface iface =\n";
198 out.indent();
199 out.indent();
200 out << "binder.queryLocalInterface(kInterfaceName);\n\n";
201 out.unindent();
202 out.unindent();
203
204 out << "if ((iface != null) && (iface instanceof "
205 << ifaceName
206 << ")) {\n";
207
208 out.indent();
209 out << "return (" << ifaceName << ")iface;\n";
210 out.unindent();
211 out << "}\n\n";
212
213 out << "return new " << ifaceName << ".Proxy(binder);\n";
214
215 out.unindent();
216 out << "}\n\n";
217
218 out << "public IHwBinder asBinder();\n\n";
219
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700220 out << "public static "
221 << ifaceName
222 << " getService(String serviceName) {\n";
223
224 out.indent();
225
226 out << "return "
227 << ifaceName
228 << ".asInterface(HwBinder.getService(serviceName, "
229 << mPackage.getPackageMajorVersion()
230 << ", "
231 << mPackage.getPackageMinorVersion()
232 << "));\n";
233
234 out.unindent();
235
236 out << "}\n\n";
237
Andreas Huber2831d512016-08-15 09:33:47 -0700238 status_t err = emitJavaTypeDeclarations(out);
239
240 if (err != OK) {
241 return err;
242 }
243
Andreas Huber2831d512016-08-15 09:33:47 -0700244 for (const auto &method : iface->methods()) {
245 const bool returnsValue = !method->results().empty();
246 const bool needsCallback = method->results().size() > 1;
247
248 if (needsCallback) {
249 out << "\npublic abstract class "
250 << method->name()
251 << "Callback {\n";
252
253 out.indent();
254
255 out << "public abstract void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700256 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700257 << ");\n";
258
259 out.unindent();
260 out << "}\n\n";
261 }
262
263 if (returnsValue && !needsCallback) {
Andreas Huber4c865b72016-09-14 15:26:27 -0700264 std::string extra;
265 out << method->results()[0]->type().getJavaType(&extra)
266 << extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700267 } else {
268 out << "void";
269 }
270
271 out << " "
272 << method->name()
273 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700274 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700275
276 if (needsCallback) {
277 if (!method->args().empty()) {
278 out << ", ";
279 }
280
281 out << method->name()
282 << "Callback cb";
283 }
284
285 out << ");\n";
286 }
287
288 out << "\npublic static final class Proxy implements "
289 << ifaceName
290 << " {\n";
291
292 out.indent();
293
294 out << "private IHwBinder mRemote;\n\n";
295 out << "public Proxy(IHwBinder remote) {\n";
296 out.indent();
297 out << "mRemote = remote;\n";
298 out.unindent();
299 out << "}\n\n";
300
301 out << "public IHwBinder asBinder() {\n";
302 out.indent();
303 out << "return mRemote;\n";
304 out.unindent();
305 out << "}\n\n";
306
307 std::vector<const Interface *> chain;
308 while (iface != NULL) {
309 chain.push_back(iface);
310 iface = iface->superType();
311 }
312
313 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
314 const Interface *superInterface = *it;
315
316 out << "// Methods from "
317 << superInterface->fullName()
318 << " follow.\n";
319
320 for (const auto &method : superInterface->methods()) {
321 const bool returnsValue = !method->results().empty();
322 const bool needsCallback = method->results().size() > 1;
323
324 out << "public ";
325 if (returnsValue && !needsCallback) {
Andreas Huber4c865b72016-09-14 15:26:27 -0700326 std::string extra;
327 out << method->results()[0]->type().getJavaType(&extra)
328 << extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700329 } else {
330 out << "void";
331 }
332
333 out << " "
334 << method->name()
335 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700336 << Method::GetJavaArgSignature(method->args());
Andreas Huber2831d512016-08-15 09:33:47 -0700337
338 if (needsCallback) {
339 if (!method->args().empty()) {
340 out << ", ";
341 }
342
343 out << method->name()
344 << "Callback cb";
345 }
346
347 out << ") {\n";
348 out.indent();
349
350 out << "HwParcel request = new HwParcel();\n";
351 out << "request.writeInterfaceToken("
352 << superInterface->fullJavaName()
353 << ".kInterfaceName);\n";
354
355 for (const auto &arg : method->args()) {
356 emitJavaReaderWriter(
357 out,
358 "request",
359 arg,
360 false /* isReader */);
361 }
362
363 out << "\nHwParcel reply = new HwParcel();\n"
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700364 << "mRemote.transact("
365 << method->getSerialId()
366 << " /* "
367 << method->name()
368 << " */, request, reply, ";
Andreas Huber8b5da222016-08-18 14:28:18 -0700369
370 if (method->isOneway()) {
371 out << "IHwBinder.FLAG_ONEWAY";
372 } else {
373 out << "0 /* flags */";
374 }
375
376 out << ");\n";
377
378 if (!method->isOneway()) {
379 out << "reply.verifySuccess();\n";
380 } else {
381 CHECK(!returnsValue);
382 }
383
384 out << "request.releaseTemporaryStorage();\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700385
386 if (returnsValue) {
387 out << "\n";
388
389 for (const auto &arg : method->results()) {
390 emitJavaReaderWriter(
391 out,
392 "reply",
393 arg,
394 true /* isReader */);
395 }
396
397 if (needsCallback) {
398 out << "cb.onValues(";
399
400 bool firstField = true;
401 for (const auto &arg : method->results()) {
402 if (!firstField) {
403 out << ", ";
404 }
405
406 out << arg->name();
407 firstField = false;
408 }
409
410 out << ");\n";
411 } else {
412 const std::string returnName = method->results()[0]->name();
413 out << "return " << returnName << ";\n";
414 }
415 }
416
417 out.unindent();
418 out << "}\n\n";
419 }
420 }
421
422 out.unindent();
423 out << "}\n";
424
425 ////////////////////////////////////////////////////////////////////////////
426
427 out << "\npublic static abstract class Stub extends HwBinder "
428 << "implements "
429 << ifaceName << " {\n";
430
431 out.indent();
432
433 out << "public IHwBinder asBinder() {\n";
434 out.indent();
435 out << "return this;\n";
436 out.unindent();
437 out << "}\n\n";
438
439 out << "public IHwInterface queryLocalInterface(String descriptor) {\n";
440 out.indent();
441 // XXX what about potential superClasses?
442 out << "if (kInterfaceName.equals(descriptor)) {\n";
443 out.indent();
444 out << "return this;\n";
445 out.unindent();
446 out << "}\n";
447 out << "return null;\n";
448 out.unindent();
449 out << "}\n\n";
450
Andreas Huber2bb6e1e2016-10-25 13:26:43 -0700451 out << "public void registerAsService(String serviceName) {\n";
452 out.indent();
453
454 out << "registerService(serviceName, "
455 << mPackage.getPackageMajorVersion()
456 << ", "
457 << mPackage.getPackageMinorVersion()
458 << ");\n";
459
460 out.unindent();
461 out << "}\n\n";
462
Andreas Huber2831d512016-08-15 09:33:47 -0700463 out << "public void onTransact("
464 << "int code, HwParcel request, final HwParcel reply, "
465 << "int flags) {\n";
466
467 out.indent();
468
469 out << "switch (code) {\n";
470
471 out.indent();
472
473 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
474 const Interface *superInterface = *it;
475
476 for (const auto &method : superInterface->methods()) {
477 const bool returnsValue = !method->results().empty();
478 const bool needsCallback = method->results().size() > 1;
479
480 out << "case "
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700481 << method->getSerialId()
482 << " /* "
483 << method->name()
484 << " */:\n{\n";
Andreas Huber2831d512016-08-15 09:33:47 -0700485
486 out.indent();
487
488 out << "request.enforceInterface("
489 << superInterface->fullJavaName()
490 << ".kInterfaceName);\n\n";
491
492 for (const auto &arg : method->args()) {
493 emitJavaReaderWriter(
494 out,
495 "request",
496 arg,
497 true /* isReader */);
498 }
499
500 if (!needsCallback && returnsValue) {
501 const TypedVar *returnArg = method->results()[0];
Andreas Huber4c865b72016-09-14 15:26:27 -0700502 std::string extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700503
Andreas Huber4c865b72016-09-14 15:26:27 -0700504 out << returnArg->type().getJavaType(&extra)
505 << extra
Andreas Huber2831d512016-08-15 09:33:47 -0700506 << " "
507 << returnArg->name()
508 << " = ";
509 }
510
511 out << method->name()
512 << "(";
513
514 bool firstField = true;
515 for (const auto &arg : method->args()) {
516 if (!firstField) {
517 out << ", ";
518 }
519
520 out << arg->name();
521
522 firstField = false;
523 }
524
525 if (needsCallback) {
526 if (!firstField) {
527 out << ", ";
528 }
529
530 out << "new " << method->name() << "Callback() {\n";
531 out.indent();
532
533 out << "@Override\n"
534 << "public void onValues("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700535 << Method::GetJavaArgSignature(method->results())
Andreas Huber2831d512016-08-15 09:33:47 -0700536 << ") {\n";
537
538 out.indent();
539 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
540
541 for (const auto &arg : method->results()) {
542 emitJavaReaderWriter(
543 out,
544 "reply",
545 arg,
546 false /* isReader */);
547 }
548
549 out << "reply.send();\n"
550 << "}}";
551
552 out.unindent();
553 out.unindent();
554 }
555
556 out << ");\n";
557
558 if (!needsCallback) {
559 out << "reply.writeStatus(HwParcel.STATUS_SUCCESS);\n";
560
561 if (returnsValue) {
562 const TypedVar *returnArg = method->results()[0];
563
564 emitJavaReaderWriter(
565 out,
566 "reply",
567 returnArg,
568 false /* isReader */);
569 }
570
571 out << "reply.send();\n";
572 }
573
574 out << "break;\n";
575 out.unindent();
576 out << "}\n\n";
577 }
578 }
579
580 out.unindent();
581 out << "}\n";
582
583 out.unindent();
584 out << "}\n";
585
586 out.unindent();
587 out << "}\n";
588
589 out.unindent();
590 out << "}\n";
591
592 return OK;
593}
594
595status_t AST::emitJavaTypeDeclarations(Formatter &out) const {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700596 return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */);
Andreas Huber2831d512016-08-15 09:33:47 -0700597}
598
599} // namespace android
600