Proper runtime error handling for _hidl_cb.

If _hidl_cb is called a number of times != 1, we LOG_ALWAYS_FATAL.

Bug: 32943424
Test: hidl_test
Change-Id: Ia56ae7bbe172c7952d9a4327d8db1df646f47543
diff --git a/generateCpp.cpp b/generateCpp.cpp
index c9683b2..b7ed184 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -785,7 +785,7 @@
 
     Formatter out(file);
 
-
+    out << "#include <android/log.h>\n";
     out << "#include <cutils/trace.h>\n\n";
     if (isInterface) {
         // This is a no-op for IServiceManager itself.
@@ -1430,6 +1430,13 @@
 
             out << ") {\n";
             out.indent();
+            out << "if (_hidl_callbackCalled) {\n";
+            out.indent();
+            out << "LOG_ALWAYS_FATAL(\""
+                << method->name()
+                << ": _hidl_cb called a second time, but must be called once.\");\n";
+            out.unindent();
+            out << "}\n";
             out << "_hidl_callbackCalled = true;\n\n";
 
             out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
@@ -1474,24 +1481,18 @@
         }
         out << ");\n\n";
 
-        // What to do if the stub implementation has a synchronous callback
-        // which does not get invoked?  This is not a transport error but a
-        // service error of sorts. For now, return OK to the caller, as this is
-        // not a transport error.
-        //
-        // TODO(b/31365311) Figure out how to deal with this later.
-
         if (returnsValue) {
             out << "if (!_hidl_callbackCalled) {\n";
             out.indent();
-        }
-
-        out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
-            << "_hidl_reply);\n\n";
-
-        if (returnsValue) {
+            out << "LOG_ALWAYS_FATAL(\""
+                << method->name()
+                << ": _hidl_cb not called, but must be called once.\");\n";
             out.unindent();
             out << "}\n\n";
+        } else {
+            out << "::android::hardware::writeToParcel("
+                << "::android::hardware::Status::ok(), "
+                << "_hidl_reply);\n\n";
         }
     }