Add toBinder to all interfaces.
Test: hidl_test
Bug: 32001926
Change-Id: Ibe45c3791ed2ab750cc246a56c4cb7b0483386fc
diff --git a/Interface.cpp b/Interface.cpp
index 1d0f37a..d348101 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -252,33 +252,12 @@
out.unindent();
out << "}\n\n";
} else {
+ out << "_hidl_err = "
+ << parcelObjDeref
+ << "writeStrongBinder("
+ << name
+ << "->toBinder());\n";
- out << "if (" << name << "->isRemote()) {\n";
- out.indent();
- out << "_hidl_err = ";
- out << parcelObjDeref
- << "writeStrongBinder("
- << fqName().cppNamespace()
- << "::IHw"
- << getBaseName()
- << "::asBinder(static_cast<"
- << fqName().cppNamespace()
- << "::IHw"
- << getBaseName()
- << "*>("
- << name << ".get()"
- << ")));\n";
- out.unindent();
- out << "} else {\n";
- out.indent();
- out << "_hidl_err = ";
- out << parcelObjDeref
- << "writeStrongBinder("
- << "new " << fqName().cppNamespace()
- << "::Bn" << getBaseName() << " "
- << "(" << name <<"));\n";
- out.unindent();
- out << "}\n";
handleError(out, mode);
}
}
diff --git a/generateCpp.cpp b/generateCpp.cpp
index 7f136a0..69ded55 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -200,6 +200,8 @@
out.unindent();
out << "}\n\n";
out << "virtual bool isRemote() const override { return false; }\n\n";
+ out << "virtual ::android::sp<::android::hardware::IBinder> "
+ << "toBinder() override;\n\n";
bool haveCallbacks = false;
for (const auto &method : iface->methods()) {
const bool returnsValue = !method->results().empty();
@@ -1610,6 +1612,28 @@
status_t AST::generateInterfaceSource(Formatter &out) const {
const Interface *iface = mRootScope->getInterface();
+
+ // generate toBinder functions
+ out << "::android::sp<::android::hardware::IBinder> I"
+ << iface->getBaseName()
+ << "::toBinder() {\n";
+ out.indent();
+ out << "if (isRemote()) {\n";
+ out.indent();
+ out << "return ::android::hardware::IInterface::asBinder("
+ << "static_cast<IHw"
+ << iface->getBaseName()
+ << " *>(this));\n";
+ out.unindent();
+ out << "} else {\n";
+ out.indent();
+ out << "return new Bn" << iface->getBaseName() << "(this);\n";
+ out.unindent();
+ out << "}\n";
+ out.unindent();
+ out << "}\n\n";
+
+ // generate castFrom functions
if (!iface->isRootType()) {
std::string childTypeExtra;
std::string childTypeResult = iface->getCppResultType(&childTypeExtra);