Consolidate error logging

Bug: 146020579
Test: ran "atest --host chre_chpp_linux_tests -c"
Change-Id: I962dcf3a72a2094de3ba6d16a005422beb93bf7d
diff --git a/chpp/app.c b/chpp/app.c
index 8605f14..289bce3 100644
--- a/chpp/app.c
+++ b/chpp/app.c
@@ -28,13 +28,13 @@
  *  Prototypes
  ***********************************************/
 
-static void chppProcessPredefinedClientRequest(struct ChppAppState *context,
+static bool chppProcessPredefinedClientRequest(struct ChppAppState *context,
                                                uint8_t *buf, size_t len);
-static void chppProcessPredefinedServiceResponse(struct ChppAppState *context,
+static bool chppProcessPredefinedServiceResponse(struct ChppAppState *context,
                                                  uint8_t *buf, size_t len);
-static void chppProcessPredefinedClientNotification(
+static bool chppProcessPredefinedClientNotification(
     struct ChppAppState *context, uint8_t *buf, size_t len);
-static void chppProcessPredefinedServiceNotification(
+static bool chppProcessPredefinedServiceNotification(
     struct ChppAppState *context, uint8_t *buf, size_t len);
 static bool chppDatagramLenIsOk(struct ChppAppState *context, uint8_t handle,
                                 size_t len);
@@ -64,26 +64,39 @@
  * @param context Maintains status for each app layer instance.
  * @param buf Input data. Cannot be null.
  * @param len Length of input data in bytes.
+ *
+ * @return False if handle is invalid. True otherwise
  */
-static void chppProcessPredefinedClientRequest(struct ChppAppState *context,
+static bool chppProcessPredefinedClientRequest(struct ChppAppState *context,
                                                uint8_t *buf, size_t len) {
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool handleValid = true;
+  bool dispatchResult = true;
 
   switch (rxHeader->handle) {
-    case CHPP_HANDLE_LOOPBACK:
-      chppDispatchLoopbackClientRequest(context, buf, len);
+    case CHPP_HANDLE_LOOPBACK: {
+      dispatchResult = chppDispatchLoopbackClientRequest(context, buf, len);
       break;
+    }
 
-    case CHPP_HANDLE_DISCOVERY:
-      chppDispatchDiscoveryClientRequest(context, buf, len);
+    case CHPP_HANDLE_DISCOVERY: {
+      dispatchResult = chppDispatchDiscoveryClientRequest(context, buf, len);
       break;
+    }
 
-    default:
-      LOGE(
-          "Client request received for an invalid predefined service handle "
-          "%" PRIu8,
-          rxHeader->handle);
+    default: {
+      handleValid = false;
+    }
   }
+
+  if (dispatchResult == false) {
+    LOGE("Handle = %" PRIu8
+         " received unknown client request command = %#x, transaction ID = "
+         "%" PRIu8,
+         rxHeader->handle, rxHeader->command, rxHeader->transaction);
+  }
+
+  return handleValid;
 }
 
 /**
@@ -93,26 +106,39 @@
  * @param context Maintains status for each app layer instance.
  * @param buf Input data. Cannot be null.
  * @param len Length of input data in bytes.
+ *
+ * @return False if handle is invalid. True otherwise
  */
-static void chppProcessPredefinedServiceResponse(struct ChppAppState *context,
+static bool chppProcessPredefinedServiceResponse(struct ChppAppState *context,
                                                  uint8_t *buf, size_t len) {
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool handleValid = true;
+  bool dispatchResult = true;
 
   switch (rxHeader->handle) {
-    case CHPP_HANDLE_LOOPBACK:
+    case CHPP_HANDLE_LOOPBACK: {
       // TODO
       break;
+    }
 
-    case CHPP_HANDLE_DISCOVERY:
-      chppDispatchDiscoveryClient(context, buf, len);
+    case CHPP_HANDLE_DISCOVERY: {
+      dispatchResult = chppDispatchDiscoveryServiceResponse(context, buf, len);
       break;
+    }
 
-    default:
-      LOGE(
-          "Service response received for an invalid predefined service handle "
-          "%" PRIu8,
-          rxHeader->handle);
+    default: {
+      handleValid = false;
+    }
   }
+
+  if (dispatchResult == false) {
+    LOGE("Handle = %" PRIu8
+         " received unknown server response command = %#x, transaction ID = "
+         "%" PRIu8,
+         rxHeader->handle, rxHeader->command, rxHeader->transaction);
+  }
+
+  return handleValid;
 }
 
 /**
@@ -122,18 +148,24 @@
  * @param context Maintains status for each app layer instance.
  * @param buf Input data. Cannot be null.
  * @param len Length of input data in bytes.
+ *
+ * @return False if handle is invalid. True otherwise
  */
-static void chppProcessPredefinedClientNotification(
+static bool chppProcessPredefinedClientNotification(
     struct ChppAppState *context, uint8_t *buf, size_t len) {
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool handleValid = true;
+  bool dispatchResult = true;
 
   // No predefined services support these yet
-  LOGE("Predefined service handle %" PRIu8
-       " does not support client notifications",
-       rxHeader->handle);
+  handleValid = false;
 
   UNUSED_VAR(context);
   UNUSED_VAR(len);
+  UNUSED_VAR(rxHeader);
+  UNUSED_VAR(dispatchResult);
+
+  return handleValid;
 }
 /**
  * Processes a service notification that is determined to be for a predefined
@@ -142,18 +174,24 @@
  * @param context Maintains status for each app layer instance.
  * @param buf Input data. Cannot be null.
  * @param len Length of input data in bytes.
+ *
+ * @return False if handle is invalid. True otherwise
  */
-static void chppProcessPredefinedServiceNotification(
+static bool chppProcessPredefinedServiceNotification(
     struct ChppAppState *context, uint8_t *buf, size_t len) {
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool handleValid = true;
+  bool dispatchResult = true;
 
   // No predefined clients support these yet
-  LOGE("Predefined client handle %" PRIu8
-       " does not support service notifications",
-       rxHeader->handle);
+  handleValid = false;
 
   UNUSED_VAR(context);
   UNUSED_VAR(len);
+  UNUSED_VAR(rxHeader);
+  UNUSED_VAR(dispatchResult);
+
+  return handleValid;
 }
 
 /**
@@ -389,21 +427,23 @@
     } else if (rxHeader->handle < CHPP_HANDLE_NEGOTIATED_RANGE_START) {
       // Predefined services / clients
 
+      bool success = true;
+
       switch (rxHeader->type) {
         case CHPP_MESSAGE_TYPE_CLIENT_REQUEST: {
-          chppProcessPredefinedClientRequest(context, buf, len);
+          success = chppProcessPredefinedClientRequest(context, buf, len);
           break;
         }
         case CHPP_MESSAGE_TYPE_CLIENT_NOTIFICATION: {
-          chppProcessPredefinedClientNotification(context, buf, len);
+          success = chppProcessPredefinedClientNotification(context, buf, len);
           break;
         }
         case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE: {
-          chppProcessPredefinedServiceResponse(context, buf, len);
+          success = chppProcessPredefinedServiceResponse(context, buf, len);
           break;
         }
         case CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION: {
-          chppProcessPredefinedServiceNotification(context, buf, len);
+          success = chppProcessPredefinedServiceNotification(context, buf, len);
           break;
         }
         default: {
@@ -416,6 +456,15 @@
         }
       }
 
+      if (success == false) {
+        LOGE("Predefined client/service with handle = %" PRIu8
+             " does not support message type = %#x (len = %zu, transaction ID "
+             "= %" PRIu8 ")",
+             rxHeader->handle, rxHeader->type, len, rxHeader->transaction);
+        chppEnqueueTxErrorDatagram(context->transportContext,
+                                   CHPP_TRANSPORT_ERROR_APPLAYER);
+      }
+
     } else {
       // Negotiated services / clients
 
diff --git a/chpp/clients/discovery.c b/chpp/clients/discovery.c
index fd2d525..a771e31 100644
--- a/chpp/clients/discovery.c
+++ b/chpp/clients/discovery.c
@@ -47,36 +47,20 @@
  *  Public Functions
  ***********************************************/
 
-void chppDispatchDiscoveryClient(struct ChppAppState *context,
-                                 const uint8_t *buf, size_t len) {
+bool chppDispatchDiscoveryServiceResponse(struct ChppAppState *context,
+                                          const uint8_t *buf, size_t len) {
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool success = true;
 
-  switch (rxHeader->type) {
-    case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE: {
-      // Received discovery response from service
-
-      switch (rxHeader->command) {
-        case CHPP_DISCOVERY_COMMAND_DISCOVER_ALL: {
-          chppDiscoveryProcessDiscoverAll(context, buf, len);
-          break;
-        }
-        default: {
-          LOGE(
-              "Received unknown discovery command: %#x, transaction ID = "
-              "%" PRIu8,
-              rxHeader->command, rxHeader->transaction);
-          break;
-        }
-      }
-
+  switch (rxHeader->command) {
+    case CHPP_DISCOVERY_COMMAND_DISCOVER_ALL: {
+      chppDiscoveryProcessDiscoverAll(context, buf, len);
       break;
     }
-
     default: {
-      LOGE("Received unknown discovery message type: %" PRIu8
-           ", command = %#x, transaction ID = %" PRIu8,
-           rxHeader->type, rxHeader->command, rxHeader->transaction);
+      success = false;
       break;
     }
   }
+  return success;
 }
diff --git a/chpp/include/chpp/clients/discovery.h b/chpp/include/chpp/clients/discovery.h
index df38a3a..c54edb1 100644
--- a/chpp/include/chpp/clients/discovery.h
+++ b/chpp/include/chpp/clients/discovery.h
@@ -43,8 +43,8 @@
  * @param buf Input (request) datagram. Cannot be null.
  * @param len Length of input data in bytes.
  */
-void chppDispatchDiscoveryClient(struct ChppAppState *context,
-                                 const uint8_t *buf, size_t len);
+bool chppDispatchDiscoveryServiceResponse(struct ChppAppState *context,
+                                          const uint8_t *buf, size_t len);
 
 #ifdef __cplusplus
 }
diff --git a/chpp/include/chpp/services/discovery.h b/chpp/include/chpp/services/discovery.h
index 51551dc..4a21c86 100644
--- a/chpp/include/chpp/services/discovery.h
+++ b/chpp/include/chpp/services/discovery.h
@@ -44,7 +44,7 @@
  * @param buf Input (request) datagram. Cannot be null.
  * @param len Length of input data in bytes.
  */
-void chppDispatchDiscoveryClientRequest(struct ChppAppState *context,
+bool chppDispatchDiscoveryClientRequest(struct ChppAppState *context,
                                         const uint8_t *buf, size_t len);
 
 #ifdef __cplusplus
diff --git a/chpp/include/chpp/services/loopback.h b/chpp/include/chpp/services/loopback.h
index 4a661b6..d226270 100644
--- a/chpp/include/chpp/services/loopback.h
+++ b/chpp/include/chpp/services/loopback.h
@@ -45,7 +45,7 @@
  * @param buf Input data. Cannot be null.
  * @param len Length of input data in bytes.
  */
-void chppDispatchLoopbackClientRequest(struct ChppAppState *context,
+bool chppDispatchLoopbackClientRequest(struct ChppAppState *context,
                                        uint8_t *buf, size_t len);
 
 #ifdef __cplusplus
diff --git a/chpp/services/discovery.c b/chpp/services/discovery.c
index c46ffa2..daa1ef0 100644
--- a/chpp/services/discovery.c
+++ b/chpp/services/discovery.c
@@ -65,10 +65,11 @@
  *  Public Functions
  ***********************************************/
 
-void chppDispatchDiscoveryClientRequest(struct ChppAppState *context,
+bool chppDispatchDiscoveryClientRequest(struct ChppAppState *context,
                                         const uint8_t *buf, size_t len) {
   UNUSED_VAR(len);
   struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
+  bool success = true;
 
   switch (rxHeader->command) {
     case CHPP_DISCOVERY_COMMAND_DISCOVER_ALL: {
@@ -77,11 +78,8 @@
       break;
     }
     default: {
-      LOGE(
-          "Received unknown discovery command: %#x, transaction ID = "
-          "%" PRIu8,
-          rxHeader->command, rxHeader->transaction);
-      break;
+      success = false;
     }
   }
+  return success;
 }
diff --git a/chpp/services/loopback.c b/chpp/services/loopback.c
index bc0ea81..fd76cae 100644
--- a/chpp/services/loopback.c
+++ b/chpp/services/loopback.c
@@ -20,7 +20,7 @@
  *  Public Functions
  ***********************************************/
 
-void chppDispatchLoopbackClientRequest(struct ChppAppState *context,
+bool chppDispatchLoopbackClientRequest(struct ChppAppState *context,
                                        uint8_t *buf, size_t len) {
   uint8_t *response = chppMalloc(len);
   if (response == NULL) {
@@ -39,4 +39,6 @@
     // Send out response datagram
     chppEnqueueTxDatagramOrFail(context->transportContext, response, len);
   }
+
+  return true;
 }