1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AST.h"
#include "Coordinator.h"
#include "Interface.h"
#include "Method.h"
#include "Reference.h"
#include "Scope.h"
#include <hidl-util/Formatter.h>
#include <android-base/logging.h>
namespace android {
void AST::emitJavaReaderWriter(Formatter& out, const std::string& parcelObj,
const NamedReference<Type>* arg, bool isReader,
bool addPrefixToName) const {
if (isReader) {
out << arg->type().getJavaType()
<< " "
<< (addPrefixToName ? "_hidl_out_" : "")
<< arg->name()
<< " = ";
}
arg->type().emitJavaReaderWriter(out, parcelObj,
(addPrefixToName ? "_hidl_out_" : "") + arg->name(),
isReader);
}
void AST::generateJavaTypes(Formatter& out, const std::string& limitToType) const {
// Splits types.hal up into one java file per declared type.
CHECK(!limitToType.empty()) << getFilename();
for (const auto& type : mRootScope.getSubTypes()) {
std::string typeName = type->localName();
if (type->isTypeDef()) continue;
if (typeName != limitToType) continue;
std::vector<std::string> packageComponents;
getPackageAndVersionComponents(
&packageComponents, true /* cpp_compatible */);
out << "package " << mPackage.javaPackage() << ";\n\n\n";
type->emitJavaTypeDeclarations(out, true /* atTopLevel */);
return;
}
CHECK(false) << "generateJavaTypes could not find limitToType type";
}
void emitGetService(
Formatter& out,
const std::string& ifaceName,
const std::string& fqName,
bool isRetry) {
if (isRetry) {
DocComment(
"This will invoke the equivalent of the C++ getService(std::string) if retry is\n"
"true or tryGetService(std::string) if retry is false. If the service is\n"
"available on the device and retry is true, this will wait for the service to\n"
"start. Otherwise, it will return immediately even if the service is null.")
.emit(out);
} else {
DocComment(
"Warning: this will not wait for the interface to come up if it hasn't yet\n"
"started. See getService(String,boolean) instead.")
.emit(out);
}
out << "public static "
<< ifaceName
<< " getService(String serviceName";
if (isRetry) {
out << ", boolean retry";
}
out << ") throws android.os.RemoteException ";
out.block([&] {
out << "return "
<< ifaceName
<< ".asInterface(android.os.HwBinder.getService(\""
<< fqName
<< "\", serviceName";
if (isRetry) {
out << ", retry";
}
out << "));\n";
}).endl().endl();
if (isRetry) {
DocComment("Calls getService(\"default\",retry).").emit(out);
} else {
DocComment(
"Warning: this will not wait for the interface to come up if it hasn't yet "
"started. See getService(String,boolean) instead.")
.emit(out);
}
out << "public static "
<< ifaceName
<< " getService(";
if (isRetry) {
out << "boolean retry";
}
out << ") throws android.os.RemoteException ";
out.block([&] {
out << "return getService(\"default\"";
if (isRetry) {
out << ", retry";
}
out <<");\n";
}).endl().endl();
}
void AST::generateJava(Formatter& out, const std::string& limitToType) const {
CHECK(isJavaCompatible()) << getFilename();
if (!AST::isInterface()) {
generateJavaTypes(out, limitToType);
return;
}
const Interface* iface = mRootScope.getInterface();
const std::string ifaceName = iface->localName();
const std::string baseName = iface->getBaseName();
std::vector<std::string> packageComponents;
getPackageAndVersionComponents(
&packageComponents, true /* cpp_compatible */);
out << "package " << mPackage.javaPackage() << ";\n\n";
out.setNamespace(mPackage.javaPackage() + ".");
const Interface *superType = iface->superType();
iface->emitDocComment(out);
out << "public interface " << ifaceName << " extends ";
if (superType != nullptr) {
out << superType->fullJavaName();
} else {
out << "android.os.IHwInterface";
}
out << " {\n";
out.indent();
DocComment("Fully-qualified interface name for this interface.").emit(out);
out << "public static final String kInterfaceName = \""
<< mPackage.string()
<< "::"
<< ifaceName
<< "\";\n\n";
DocComment("Does a checked conversion from a binder to this class.").emit(out);
out << "/* package private */ static "
<< ifaceName
<< " asInterface(android.os.IHwBinder binder) {\n";
out.indent();
out << "if (binder == null) {\n";
out.indent();
out << "return null;\n";
out.unindent();
out << "}\n\n";
out << "android.os.IHwInterface iface =\n";
out.indent();
out.indent();
out << "binder.queryLocalInterface(kInterfaceName);\n\n";
out.unindent();
out.unindent();
out << "if ((iface != null) && (iface instanceof "
<< ifaceName
<< ")) {\n";
out.indent();
out << "return (" << ifaceName << ")iface;\n";
out.unindent();
out << "}\n\n";
out << ifaceName << " proxy = new " << ifaceName << ".Proxy(binder);\n\n";
out << "try {\n";
out.indent();
out << "for (String descriptor : proxy.interfaceChain()) {\n";
out.indent();
out << "if (descriptor.equals(kInterfaceName)) {\n";
out.indent();
out << "return proxy;\n";
out.unindent();
out << "}\n";
out.unindent();
out << "}\n";
out.unindent();
out << "} catch (android.os.RemoteException e) {\n";
out.indent();
out.unindent();
out << "}\n\n";
out << "return null;\n";
out.unindent();
out << "}\n\n";
DocComment("Does a checked conversion from any interface to this class.").emit(out);
out << "public static "
<< ifaceName
<< " castFrom(android.os.IHwInterface iface) {\n";
out.indent();
out << "return (iface == null) ? null : "
<< ifaceName
<< ".asInterface(iface.asBinder());\n";
out.unindent();
out << "}\n\n";
out << "@Override\npublic android.os.IHwBinder asBinder();\n\n";
emitGetService(out, ifaceName, iface->fqName().string(), true /* isRetry */);
emitGetService(out, ifaceName, iface->fqName().string(), false /* isRetry */);
iface->emitJavaTypeDeclarations(out, false /* atTopLevel */);
for (const auto &method : iface->methods()) {
const bool returnsValue = !method->results().empty();
const bool needsCallback = method->results().size() > 1;
if (needsCallback) {
out << "\n@java.lang.FunctionalInterface\npublic interface " << method->name()
<< "Callback {\n";
out.indent();
out << "public void onValues(";
method->emitJavaResultSignature(out);
out << ");\n";
out.unindent();
out << "}\n\n";
}
method->emitDocComment(out);
if (returnsValue && !needsCallback) {
out << method->results()[0]->type().getJavaType();
} else {
out << "void";
}
out << " "
<< method->name()
<< "(";
method->emitJavaArgSignature(out);
if (needsCallback) {
if (!method->args().empty()) {
out << ", ";
}
out << method->name()
<< "Callback _hidl_cb";
}
out << ")\n";
out.indent();
out << "throws android.os.RemoteException;\n";
out.unindent();
}
out << "\npublic static final class Proxy implements "
<< ifaceName
<< " {\n";
out.indent();
out << "private android.os.IHwBinder mRemote;\n\n";
out << "public Proxy(android.os.IHwBinder remote) {\n";
out.indent();
out << "mRemote = java.util.Objects.requireNonNull(remote);\n";
out.unindent();
out << "}\n\n";
out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
out.indent();
out << "return mRemote;\n";
out.unindent();
out << "}\n\n";
out << "@Override\npublic String toString() ";
out.block([&] {
out.sTry([&] {
out << "return this.interfaceDescriptor() + \"@Proxy\";\n";
}).sCatch("android.os.RemoteException ex", [&] {
out << "/* ignored; handled below. */\n";
}).endl();
out << "return \"[class or subclass of \" + "
<< ifaceName << ".kInterfaceName + \"]@Proxy\";\n";
}).endl().endl();
// Equals when internal binder object is equal (even if the interface Proxy object
// itself is different). This is similar to interfacesEqual in C++.
out << "@Override\npublic final boolean equals(java.lang.Object other) ";
out.block([&] {
out << "return android.os.HidlSupport.interfacesEqual(this, other);\n";
}).endl().endl();
out << "@Override\npublic final int hashCode() ";
out.block([&] {
out << "return this.asBinder().hashCode();\n";
}).endl().endl();
const Interface *prevInterface = nullptr;
for (const auto &tuple : iface->allMethodsFromRoot()) {
const Method *method = tuple.method();
const Interface *superInterface = tuple.interface();
if (prevInterface != superInterface) {
out << "// Methods from "
<< superInterface->fullName()
<< " follow.\n";
prevInterface = superInterface;
}
const bool returnsValue = !method->results().empty();
const bool needsCallback = method->results().size() > 1;
out << "@Override\npublic ";
if (returnsValue && !needsCallback) {
out << method->results()[0]->type().getJavaType();
} else {
out << "void";
}
out << " "
<< method->name()
<< "(";
method->emitJavaArgSignature(out);
if (needsCallback) {
if (!method->args().empty()) {
out << ", ";
}
out << method->name()
<< "Callback _hidl_cb";
}
out << ")\n";
out.indent();
out.indent();
out << "throws android.os.RemoteException {\n";
out.unindent();
if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) {
method->javaImpl(IMPL_PROXY, out);
out.unindent();
out << "}\n";
continue;
}
out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n";
out << "_hidl_request.writeInterfaceToken("
<< superInterface->fullJavaName()
<< ".kInterfaceName);\n";
for (const auto &arg : method->args()) {
emitJavaReaderWriter(
out,
"_hidl_request",
arg,
false /* isReader */,
false /* addPrefixToName */);
}
out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n";
out.sTry([&] {
out << "mRemote.transact("
<< method->getSerialId()
<< " /* "
<< method->name()
<< " */, _hidl_request, _hidl_reply, ";
if (method->isOneway()) {
out << Interface::FLAG_ONE_WAY->javaValue();
} else {
out << "0 /* flags */";
}
out << ");\n";
if (!method->isOneway()) {
out << "_hidl_reply.verifySuccess();\n";
} else {
CHECK(!returnsValue);
}
out << "_hidl_request.releaseTemporaryStorage();\n";
if (returnsValue) {
out << "\n";
for (const auto &arg : method->results()) {
emitJavaReaderWriter(
out,
"_hidl_reply",
arg,
true /* isReader */,
true /* addPrefixToName */);
}
if (needsCallback) {
out << "_hidl_cb.onValues(";
bool firstField = true;
for (const auto &arg : method->results()) {
if (!firstField) {
out << ", ";
}
out << "_hidl_out_" << arg->name();
firstField = false;
}
out << ");\n";
} else {
const std::string returnName = method->results()[0]->name();
out << "return _hidl_out_" << returnName << ";\n";
}
}
}).sFinally([&] {
out << "_hidl_reply.release();\n";
}).endl();
out.unindent();
out << "}\n\n";
}
out.unindent();
out << "}\n";
////////////////////////////////////////////////////////////////////////////
out << "\npublic static abstract class Stub extends android.os.HwBinder "
<< "implements "
<< ifaceName << " {\n";
out.indent();
out << "@Override\npublic android.os.IHwBinder asBinder() {\n";
out.indent();
// If we change this behavior in the future and asBinder does not return "this",
// equals and hashCode should also be overridden.
out << "return this;\n";
out.unindent();
out << "}\n\n";
for (Method *method : iface->hidlReservedMethods()) {
// b/32383557 this is a hack. We need to change this if we have more reserved methods.
CHECK_LE(method->results().size(), 1u);
std::string resultType = method->results().size() == 0 ? "void" :
method->results()[0]->type().getJavaType();
bool canBeOverriden = method->name() == "debug";
out << "@Override\npublic " << (canBeOverriden ? "" : "final ") << resultType << " "
<< method->name() << "(";
method->emitJavaArgSignature(out);
out << ") {\n";
out.indent();
method->javaImpl(IMPL_INTERFACE, out);
out.unindent();
out << "\n}\n\n";
}
out << "@Override\n"
<< "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n";
out.indent();
// XXX what about potential superClasses?
out << "if (kInterfaceName.equals(descriptor)) {\n";
out.indent();
out << "return this;\n";
out.unindent();
out << "}\n";
out << "return null;\n";
out.unindent();
out << "}\n\n";
out << "public void registerAsService(String serviceName) throws android.os.RemoteException {\n";
out.indent();
out << "registerService(serviceName);\n";
out.unindent();
out << "}\n\n";
out << "@Override\npublic String toString() ";
out.block([&] {
out << "return this.interfaceDescriptor() + \"@Stub\";\n";
}).endl().endl();
out << "@Override\n"
<< "public void onTransact("
<< "int _hidl_code, "
<< "android.os.HwParcel _hidl_request, "
<< "final android.os.HwParcel _hidl_reply, "
<< "int _hidl_flags)\n";
out.indent();
out.indent();
out << "throws android.os.RemoteException {\n";
out.unindent();
out << "switch (_hidl_code) {\n";
out.indent();
for (const auto &tuple : iface->allMethodsFromRoot()) {
const Method *method = tuple.method();
const Interface *superInterface = tuple.interface();
const bool returnsValue = !method->results().empty();
const bool needsCallback = method->results().size() > 1;
out << "case "
<< method->getSerialId()
<< " /* "
<< method->name()
<< " */:\n{\n";
out.indent();
out << "boolean _hidl_is_oneway = (_hidl_flags & " << Interface::FLAG_ONE_WAY->javaValue()
<< ") != 0;\n";
out << "if (_hidl_is_oneway != " << (method->isOneway() ? "true" : "false") << ") ";
out.block([&] {
out << "_hidl_reply.writeStatus(" << UNKNOWN_ERROR << ");\n";
out << "_hidl_reply.send();\n";
out << "break;\n";
});
if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) {
method->javaImpl(IMPL_STUB, out);
out.unindent();
out << "break;\n";
out << "}\n\n";
continue;
}
out << "_hidl_request.enforceInterface("
<< superInterface->fullJavaName()
<< ".kInterfaceName);\n\n";
for (const auto &arg : method->args()) {
emitJavaReaderWriter(
out,
"_hidl_request",
arg,
true /* isReader */,
false /* addPrefixToName */);
}
if (!needsCallback && returnsValue) {
const NamedReference<Type>* returnArg = method->results()[0];
out << returnArg->type().getJavaType()
<< " _hidl_out_"
<< returnArg->name()
<< " = ";
}
out << method->name()
<< "(";
bool firstField = true;
for (const auto &arg : method->args()) {
if (!firstField) {
out << ", ";
}
out << arg->name();
firstField = false;
}
if (needsCallback) {
if (!firstField) {
out << ", ";
}
out << "new " << method->name() << "Callback() {\n";
out.indent();
out << "@Override\n"
<< "public void onValues(";
method->emitJavaResultSignature(out);
out << ") {\n";
out.indent();
out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
for (const auto &arg : method->results()) {
emitJavaReaderWriter(
out,
"_hidl_reply",
arg,
false /* isReader */,
false /* addPrefixToName */);
// no need to add _hidl_out because out vars are are scoped
}
out << "_hidl_reply.send();\n"
<< "}}";
out.unindent();
out.unindent();
}
out << ");\n";
if (!needsCallback && !method->isOneway()) {
out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n";
if (returnsValue) {
const NamedReference<Type>* returnArg = method->results()[0];
emitJavaReaderWriter(
out,
"_hidl_reply",
returnArg,
false /* isReader */,
true /* addPrefixToName */);
}
out << "_hidl_reply.send();\n";
}
out << "break;\n";
out.unindent();
out << "}\n\n";
}
out.unindent();
out << "}\n";
out.unindent();
out << "}\n";
out.unindent();
out << "}\n";
out.unindent();
out << "}\n";
}
} // namespace android
|