Allow nullptr interfaces to be passed.

Bug: 32410513
Test: mma, hidl_test
Change-Id: I0ed27dc503f6b4d71b2106e03f70b1994800de52
diff --git a/Interface.cpp b/Interface.cpp
index d348101..f03f3aa 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -252,11 +252,21 @@
         out.unindent();
         out << "}\n\n";
     } else {
+        out << "if (" << name << " == nullptr) {\n";
+        out.indent();
+        out << "_hidl_err = ";
+        out << parcelObjDeref
+            << "writeStrongBinder(nullptr);\n";
+        out.unindent();
+        out << "} else {\n";
+        out.indent();
         out << "_hidl_err = "
             << parcelObjDeref
             << "writeStrongBinder("
             << name
             << "->toBinder());\n";
+        out.unindent();
+        out << "}\n";
 
         handleError(out, mode);
     }
diff --git a/test/main.cpp b/test/main.cpp
index 50bada6..8aa3da8 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -641,6 +641,14 @@
     }
 }
 
+TEST_F(HidlTest, FooNullCallbackTest) {
+    EXPECT_OK(foo->echoNullInterface(nullptr,
+                [](const auto receivedNull, const auto &intf) {
+                   EXPECT_TRUE(receivedNull);
+                   EXPECT_EQ(intf, nullptr);
+                }));
+}
+
 TEST_F(HidlTest, FooNonNullCallbackTest) {
     hidl_array<hidl_string, 5, 3> in;