mgmt: ec_host_cmd: add a function to send response

A function to send Host Command response is needed for commands that
that sends IN_PROGRESS status or doesn't return e.g. perform reboot.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
diff --git a/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h b/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h
index dfa1829..72f0d73 100644
--- a/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h
+++ b/include/zephyr/mgmt/ec_host_cmd/ec_host_cmd.h
@@ -247,6 +247,20 @@
 int ec_host_cmd_init(struct ec_host_cmd_backend *backend);
 
 /**
+ * @brief Send the host command response
+ *
+ * This routine sends the host command response. It should be used to send IN_PROGRESS status or
+ * if the host command handler doesn't return e.g. reboot command.
+ *
+ * @param[in] status        Host command status to be sent.
+ * @param[in] args          Pointer of a structure passed to the handler.
+ *
+ * @retval 0 if successful.
+ */
+int ec_host_cmd_send_response(enum ec_host_cmd_status status,
+		const struct ec_host_cmd_handler_args *args);
+
+/**
  * @brief Get the main ec host command structure
  *
  * This routine returns a pointer to the main host command structure.
diff --git a/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c b/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c
index 2e29ec9..e3922ea 100644
--- a/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c
+++ b/subsys/mgmt/ec_host_cmd/ec_host_cmd_handler.c
@@ -60,13 +60,14 @@
 	return (uint8_t)(-checksum);
 }
 
-static void send_error_response(const struct ec_host_cmd_backend *backend,
-				struct ec_host_cmd_tx_buf *tx, const enum ec_host_cmd_status error)
+static void send_status_response(const struct ec_host_cmd_backend *backend,
+				 struct ec_host_cmd_tx_buf *tx,
+				 const enum ec_host_cmd_status status)
 {
 	struct ec_host_cmd_response_header *const tx_header = (void *)tx->buf;
 
 	tx_header->prtcl_ver = 3;
-	tx_header->result = error;
+	tx_header->result = status;
 	tx_header->data_len = 0;
 	tx_header->reserved = 0;
 	tx_header->checksum = 0;
@@ -153,6 +154,26 @@
 	return EC_HOST_CMD_SUCCESS;
 }
 
+int ec_host_cmd_send_response(enum ec_host_cmd_status status,
+			      const struct ec_host_cmd_handler_args *args)
+{
+	struct ec_host_cmd *hc = &ec_host_cmd;
+	struct ec_host_cmd_tx_buf *tx = &hc->tx;
+
+	if (status != EC_HOST_CMD_SUCCESS) {
+		send_status_response(hc->backend, tx, status);
+		return status;
+	}
+
+	status = prepare_response(tx, args->output_buf_size);
+	if (status != EC_HOST_CMD_SUCCESS) {
+		send_status_response(hc->backend, tx, status);
+		return status;
+	}
+
+	return hc->backend->api->send(hc->backend);
+}
+
 FUNC_NORETURN static void ec_host_cmd_thread(void *hc_handle, void *arg2, void *arg3)
 {
 	ARG_UNUSED(arg2);
@@ -177,7 +198,7 @@
 
 		status = verify_rx(rx);
 		if (status != EC_HOST_CMD_SUCCESS) {
-			send_error_response(hc->backend, tx, status);
+			send_status_response(hc->backend, tx, status);
 			continue;
 		}
 
@@ -192,7 +213,7 @@
 
 		/* No handler in this image for requested command */
 		if (found_handler == NULL) {
-			send_error_response(hc->backend, tx, EC_HOST_CMD_INVALID_COMMAND);
+			send_status_response(hc->backend, tx, EC_HOST_CMD_INVALID_COMMAND);
 			continue;
 		}
 
@@ -203,23 +224,13 @@
 
 		status = validate_handler(found_handler, &args);
 		if (status != EC_HOST_CMD_SUCCESS) {
-			send_error_response(hc->backend, tx, status);
+			send_status_response(hc->backend, tx, status);
 			continue;
 		}
 
 		status = found_handler->handler(&args);
-		if (status != EC_HOST_CMD_SUCCESS) {
-			send_error_response(hc->backend, tx, status);
-			continue;
-		}
 
-		status = prepare_response(tx, args.output_buf_size);
-		if (status != EC_HOST_CMD_SUCCESS) {
-			send_error_response(hc->backend, tx, status);
-			continue;
-		}
-
-		hc->backend->api->send(hc->backend);
+		ec_host_cmd_send_response(status, &args);
 	}
 }