Add test message handler to be used for testing

It is like message handler but it does not process any
inferences. They are simply queue and never executed.

Change-Id: I131c3c779b616e82d650ff03e3723dc607de58bf
diff --git a/applications/message_handler/CMakeLists.txt b/applications/message_handler/CMakeLists.txt
index 6928c2d..5e95bdd 100644
--- a/applications/message_handler/CMakeLists.txt
+++ b/applications/message_handler/CMakeLists.txt
@@ -16,13 +16,13 @@
 # limitations under the License.
 #
 
-if (NOT TARGET freertos_kernel)
-    message("Not building ethosu_message_dispatcher, required freertos not built.")
+if(NOT TARGET freertos_kernel)
+    message("Skipping message handler")
     return()
 endif()
 
 # Split total tensor arena equally for each NPU
-if (TARGET ethosu_core_driver AND ETHOSU_TARGET_NPU_COUNT GREATER 0)
+if(TARGET ethosu_core_driver AND ETHOSU_TARGET_NPU_COUNT GREATER 0)
     set(NUM_ARENAS ${ETHOSU_TARGET_NPU_COUNT})
 else()
     set(NUM_ARENAS 1)
@@ -41,10 +41,10 @@
 
 ethosu_add_executable(message_handler
     SOURCES
-        main.cpp
+    main.cpp
     LIBRARIES
-        message_handler_lib
-        freertos_kernel)
+    message_handler_lib
+    freertos_kernel)
 
 target_include_directories(message_handler PRIVATE
     indexed_networks
diff --git a/applications/message_handler/test/CMakeLists.txt b/applications/message_handler/test/CMakeLists.txt
index 7cba4c3..85976ba 100644
--- a/applications/message_handler/test/CMakeLists.txt
+++ b/applications/message_handler/test/CMakeLists.txt
@@ -16,6 +16,8 @@
 # limitations under the License.
 #
 
+add_subdirectory(test_message_handler)
+
 set(TEST_MESSAGE_HANDLER_MODEL_0 "model.h" CACHE STRING "Path to built in model 0")
 set(TEST_MESSAGE_HANDLER_MODEL_1 "" CACHE STRING "Path to built in model 1")
 set(TEST_MESSAGE_HANDLER_MODEL_2 "" CACHE STRING "Path to built in model 2")
diff --git a/applications/message_handler/test/test_message_handler/CMakeLists.txt b/applications/message_handler/test/test_message_handler/CMakeLists.txt
new file mode 100644
index 0000000..bdccf54
--- /dev/null
+++ b/applications/message_handler/test/test_message_handler/CMakeLists.txt
@@ -0,0 +1,38 @@
+#
+# Copyright (c) 2020-2022 Arm Limited.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+if(NOT BUILD_TEST_MESSAGE_HANDLER OR NOT TARGET freertos_kernel)
+    message("Skipping test message handler")
+    return()
+endif()
+
+ethosu_add_executable(test_message_handler
+    SOURCES
+    main.cpp
+    LIBRARIES
+    message_handler_lib
+    freertos_kernel)
+
+target_include_directories(test_message_handler PRIVATE
+    ../../indexed_networks
+    ${LINUX_DRIVER_STACK_PATH}/kernel)
+
+install(FILES $<TARGET_FILE:test_message_handler>
+    DESTINATION "lib/firmware"
+    RENAME "arm-test-${ETHOSU_TARGET_NPU_CONFIG}.fw"
+)
diff --git a/applications/message_handler/test/test_message_handler/main.cpp b/applications/message_handler/test/test_message_handler/main.cpp
new file mode 100644
index 0000000..0a83a56
--- /dev/null
+++ b/applications/message_handler/test/test_message_handler/main.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2019-2022 Arm Limited.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/****************************************************************************
+ * Includes
+ ****************************************************************************/
+
+#include "FreeRTOS.h"
+#include "queue.h"
+#include "semphr.h"
+#include "task.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include "ethosu_core_interface.h"
+#include "indexed_networks.hpp"
+#include "message_handler.hpp"
+#include "message_queue.hpp"
+#include "networks.hpp"
+
+#include <mailbox.hpp>
+#if defined(MHU_V2)
+#include <mhu_v2.hpp>
+#elif defined(MHU_JUNO)
+#include <mhu_juno.hpp>
+#else
+#include <mhu_dummy.hpp>
+#endif
+
+/* Disable semihosting */
+__asm(".global __use_no_semihosting\n\t");
+
+using namespace EthosU;
+using namespace MessageHandler;
+
+/****************************************************************************
+ * Defines
+ ****************************************************************************/
+
+// Message queue from remote host
+__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue;
+
+// Message queue to remote host
+__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue;
+
+namespace {
+
+// Mailbox driver
+#ifdef MHU_V2
+Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
+#elif defined(MHU_JUNO)
+Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
+#else
+Mailbox::MHUDummy mailbox;
+#endif
+
+} // namespace
+
+/****************************************************************************
+ * Application
+ ****************************************************************************/
+namespace {
+
+struct TaskParams {
+    TaskParams() :
+        messageNotify(xSemaphoreCreateBinary()),
+        inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()),
+        inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))),
+        networks(std::make_shared<WithIndexedNetworks>()) {}
+
+    SemaphoreHandle_t messageNotify;
+    // Used to pass inference requests to the inference runner task
+    std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue;
+    // Queue for message responses to the remote host
+    QueueHandle_t inferenceOutputQueue;
+    // Networks provider
+    std::shared_ptr<Networks> networks;
+};
+
+#ifdef MHU_IRQ
+void mailboxIrqHandler() {
+    mailbox.handleMessage();
+}
+#endif
+
+void messageTask(void *pvParameters) {
+    printf("Starting message task\n");
+    TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);
+
+    IncomingMessageHandler process(*inputMessageQueue.toQueue(),
+                                   *outputMessageQueue.toQueue(),
+                                   mailbox,
+                                   params->inferenceInputQueue,
+                                   params->inferenceOutputQueue,
+                                   params->messageNotify,
+                                   params->networks);
+
+#ifdef MHU_IRQ
+    // Register mailbox interrupt handler
+    NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
+    NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
+#endif
+
+    process.run();
+}
+
+/*
+ * Keep task parameters as global data as FreeRTOS resets the stack when the
+ * scheduler is started.
+ */
+TaskParams taskParams;
+
+} // namespace
+
+// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
+int main() {
+    BaseType_t ret;
+
+    if (!mailbox.verifyHardware()) {
+        printf("Failed to verify mailbox hardware\n");
+        return 1;
+    }
+
+    // Task for handling incoming /outgoing messages from the remote host
+    ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr);
+    if (ret != pdPASS) {
+        printf("Failed to create 'messageTask'\n");
+        return ret;
+    }
+
+    // Start Scheduler
+    vTaskStartScheduler();
+
+    return 1;
+}